org.el 542 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618
  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 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.13
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. (require 'org-list)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.13"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-modules'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (when (org-bound-and-true-p org-modules)
  125. (let ((a (member 'org-infojs org-modules)))
  126. (and a (setcar a 'org-jsinfo))))
  127. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  128. "Modules that should always be loaded together with org.el.
  129. If a description starts with <C>, the file is not part of Emacs
  130. and loading it will require that you have downloaded and properly installed
  131. the org-mode distribution.
  132. You can also use this system to load external packages (i.e. neither Org
  133. core modules, not modules from the CONTRIB directory). Just add symbols
  134. to the end of the list. If the package is called org-xyz.el, then you need
  135. to add the symbol `xyz', and the package must have a call to
  136. (provide 'org-xyz)"
  137. :group 'org
  138. :set 'org-set-modules
  139. :type
  140. '(set :greedy t
  141. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  142. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  143. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  144. (const :tag " id: Global id's for identifying entries" org-id)
  145. (const :tag " info: Links to Info nodes" org-info)
  146. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  147. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  148. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  149. (const :tag " mew Links to Mew folders/messages" org-mew)
  150. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  151. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  152. (const :tag " vm: Links to VM folders/messages" org-vm)
  153. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  154. (const :tag " mouse: Additional mouse support" org-mouse)
  155. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  156. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  157. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  158. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  159. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  160. (const :tag "C eval: Include command output as text" org-eval)
  161. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  162. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  163. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  164. (const :tag "C id: Global id's for identifying entries" org-id)
  165. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  166. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  167. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  168. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  169. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  170. (const :tag "C registry: A registry for Org links" org-registry)
  171. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  172. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  173. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  174. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  175. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  176. (defgroup org-startup nil
  177. "Options concerning startup of Org-mode."
  178. :tag "Org Startup"
  179. :group 'org)
  180. (defcustom org-startup-folded t
  181. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  182. This can also be configured on a per-file basis by adding one of
  183. the following lines anywhere in the buffer:
  184. #+STARTUP: fold
  185. #+STARTUP: nofold
  186. #+STARTUP: content"
  187. :group 'org-startup
  188. :type '(choice
  189. (const :tag "nofold: show all" nil)
  190. (const :tag "fold: overview" t)
  191. (const :tag "content: all headlines" content)))
  192. (defcustom org-startup-truncated t
  193. "Non-nil means, entering Org-mode will set `truncate-lines'.
  194. This is useful since some lines containing links can be very long and
  195. uninteresting. Also tables look terrible when wrapped."
  196. :group 'org-startup
  197. :type 'boolean)
  198. (defcustom org-startup-align-all-tables nil
  199. "Non-nil means, align all tables when visiting a file.
  200. This is useful when the column width in tables is forced with <N> cookies
  201. in table fields. Such tables will look correct only after the first re-align.
  202. This can also be configured on a per-file basis by adding one of
  203. the following lines anywhere in the buffer:
  204. #+STARTUP: align
  205. #+STARTUP: noalign"
  206. :group 'org-startup
  207. :type 'boolean)
  208. (defcustom org-insert-mode-line-in-empty-file nil
  209. "Non-nil means insert the first line setting Org-mode in empty files.
  210. When the function `org-mode' is called interactively in an empty file, this
  211. normally means that the file name does not automatically trigger Org-mode.
  212. To ensure that the file will always be in Org-mode in the future, a
  213. line enforcing Org-mode will be inserted into the buffer, if this option
  214. has been set."
  215. :group 'org-startup
  216. :type 'boolean)
  217. (defcustom org-replace-disputed-keys nil
  218. "Non-nil means use alternative key bindings for some keys.
  219. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  220. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  221. If you want to use Org-mode together with one of these other modes,
  222. or more generally if you would like to move some Org-mode commands to
  223. other keys, set this variable and configure the keys with the variable
  224. `org-disputed-keys'.
  225. This option is only relevant at load-time of Org-mode, and must be set
  226. *before* org.el is loaded. Changing it requires a restart of Emacs to
  227. become effective."
  228. :group 'org-startup
  229. :type 'boolean)
  230. (defcustom org-use-extra-keys nil
  231. "Non-nil means use extra key sequence definitions for certain
  232. commands. This happens automatically if you run XEmacs or if
  233. window-system is nil. This variable lets you do the same
  234. manually. You must set it before loading org.
  235. Example: on Carbon Emacs 22 running graphically, with an external
  236. keyboard on a Powerbook, the default way of setting M-left might
  237. not work for either Alt or ESC. Setting this variable will make
  238. it work for ESC."
  239. :group 'org-startup
  240. :type 'boolean)
  241. (if (fboundp 'defvaralias)
  242. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  243. (defcustom org-disputed-keys
  244. '(([(shift up)] . [(meta p)])
  245. ([(shift down)] . [(meta n)])
  246. ([(shift left)] . [(meta -)])
  247. ([(shift right)] . [(meta +)])
  248. ([(control shift right)] . [(meta shift +)])
  249. ([(control shift left)] . [(meta shift -)]))
  250. "Keys for which Org-mode and other modes compete.
  251. This is an alist, cars are the default keys, second element specifies
  252. the alternative to use when `org-replace-disputed-keys' is t.
  253. Keys can be specified in any syntax supported by `define-key'.
  254. The value of this option takes effect only at Org-mode's startup,
  255. therefore you'll have to restart Emacs to apply it after changing."
  256. :group 'org-startup
  257. :type 'alist)
  258. (defun org-key (key)
  259. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  260. Or return the original if not disputed."
  261. (if org-replace-disputed-keys
  262. (let* ((nkey (key-description key))
  263. (x (org-find-if (lambda (x)
  264. (equal (key-description (car x)) nkey))
  265. org-disputed-keys)))
  266. (if x (cdr x) key))
  267. key))
  268. (defun org-find-if (predicate seq)
  269. (catch 'exit
  270. (while seq
  271. (if (funcall predicate (car seq))
  272. (throw 'exit (car seq))
  273. (pop seq)))))
  274. (defun org-defkey (keymap key def)
  275. "Define a key, possibly translated, as returned by `org-key'."
  276. (define-key keymap (org-key key) def))
  277. (defcustom org-ellipsis nil
  278. "The ellipsis to use in the Org-mode outline.
  279. When nil, just use the standard three dots. When a string, use that instead,
  280. When a face, use the standart 3 dots, but with the specified face.
  281. The change affects only Org-mode (which will then use its own display table).
  282. Changing this requires executing `M-x org-mode' in a buffer to become
  283. effective."
  284. :group 'org-startup
  285. :type '(choice (const :tag "Default" nil)
  286. (face :tag "Face" :value org-warning)
  287. (string :tag "String" :value "...#")))
  288. (defvar org-display-table nil
  289. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  290. (defgroup org-keywords nil
  291. "Keywords in Org-mode."
  292. :tag "Org Keywords"
  293. :group 'org)
  294. (defcustom org-deadline-string "DEADLINE:"
  295. "String to mark deadline entries.
  296. A deadline is this string, followed by a time stamp. Should be a word,
  297. terminated by a colon. You can insert a schedule keyword and
  298. a timestamp with \\[org-deadline].
  299. Changes become only effective after restarting Emacs."
  300. :group 'org-keywords
  301. :type 'string)
  302. (defcustom org-scheduled-string "SCHEDULED:"
  303. "String to mark scheduled TODO entries.
  304. A schedule is this string, followed by a time stamp. Should be a word,
  305. terminated by a colon. You can insert a schedule keyword and
  306. a timestamp with \\[org-schedule].
  307. Changes become only effective after restarting Emacs."
  308. :group 'org-keywords
  309. :type 'string)
  310. (defcustom org-closed-string "CLOSED:"
  311. "String used as the prefix for timestamps logging closing a TODO entry."
  312. :group 'org-keywords
  313. :type 'string)
  314. (defcustom org-clock-string "CLOCK:"
  315. "String used as prefix for timestamps clocking work hours on an item."
  316. :group 'org-keywords
  317. :type 'string)
  318. (defcustom org-comment-string "COMMENT"
  319. "Entries starting with this keyword will never be exported.
  320. An entry can be toggled between COMMENT and normal with
  321. \\[org-toggle-comment].
  322. Changes become only effective after restarting Emacs."
  323. :group 'org-keywords
  324. :type 'string)
  325. (defcustom org-quote-string "QUOTE"
  326. "Entries starting with this keyword will be exported in fixed-width font.
  327. Quoting applies only to the text in the entry following the headline, and does
  328. not extend beyond the next headline, even if that is lower level.
  329. An entry can be toggled between QUOTE and normal with
  330. \\[org-toggle-fixed-width-section]."
  331. :group 'org-keywords
  332. :type 'string)
  333. (defconst org-repeat-re
  334. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  335. "Regular expression for specifying repeated events.
  336. After a match, group 1 contains the repeat expression.")
  337. (defgroup org-structure nil
  338. "Options concerning the general structure of Org-mode files."
  339. :tag "Org Structure"
  340. :group 'org)
  341. (defgroup org-reveal-location nil
  342. "Options about how to make context of a location visible."
  343. :tag "Org Reveal Location"
  344. :group 'org-structure)
  345. (defconst org-context-choice
  346. '(choice
  347. (const :tag "Always" t)
  348. (const :tag "Never" nil)
  349. (repeat :greedy t :tag "Individual contexts"
  350. (cons
  351. (choice :tag "Context"
  352. (const agenda)
  353. (const org-goto)
  354. (const occur-tree)
  355. (const tags-tree)
  356. (const link-search)
  357. (const mark-goto)
  358. (const bookmark-jump)
  359. (const isearch)
  360. (const default))
  361. (boolean))))
  362. "Contexts for the reveal options.")
  363. (defcustom org-show-hierarchy-above '((default . t))
  364. "Non-nil means, show full hierarchy when revealing a location.
  365. Org-mode often shows locations in an org-mode file which might have
  366. been invisible before. When this is set, the hierarchy of headings
  367. above the exposed location is shown.
  368. Turning this off for example for sparse trees makes them very compact.
  369. Instead of t, this can also be an alist specifying this option for different
  370. contexts. Valid contexts are
  371. agenda when exposing an entry from the agenda
  372. org-goto when using the command `org-goto' on key C-c C-j
  373. occur-tree when using the command `org-occur' on key C-c /
  374. tags-tree when constructing a sparse tree based on tags matches
  375. link-search when exposing search matches associated with a link
  376. mark-goto when exposing the jump goal of a mark
  377. bookmark-jump when exposing a bookmark location
  378. isearch when exiting from an incremental search
  379. default default for all contexts not set explicitly"
  380. :group 'org-reveal-location
  381. :type org-context-choice)
  382. (defcustom org-show-following-heading '((default . nil))
  383. "Non-nil means, show following heading when revealing a location.
  384. Org-mode often shows locations in an org-mode file which might have
  385. been invisible before. When this is set, the heading following the
  386. match is shown.
  387. Turning this off for example for sparse trees makes them very compact,
  388. but makes it harder to edit the location of the match. In such a case,
  389. use the command \\[org-reveal] to show more context.
  390. Instead of t, this can also be an alist specifying this option for different
  391. contexts. See `org-show-hierarchy-above' for valid contexts."
  392. :group 'org-reveal-location
  393. :type org-context-choice)
  394. (defcustom org-show-siblings '((default . nil) (isearch t))
  395. "Non-nil means, show all sibling heading when revealing a location.
  396. Org-mode often shows locations in an org-mode file which might have
  397. been invisible before. When this is set, the sibling of the current entry
  398. heading are all made visible. If `org-show-hierarchy-above' is t,
  399. the same happens on each level of the hierarchy above the current entry.
  400. By default this is on for the isearch context, off for all other contexts.
  401. Turning this off for example for sparse trees makes them very compact,
  402. but makes it harder to edit the location of the match. In such a case,
  403. use the command \\[org-reveal] to show more context.
  404. Instead of t, this can also be an alist specifying this option for different
  405. contexts. See `org-show-hierarchy-above' for valid contexts."
  406. :group 'org-reveal-location
  407. :type org-context-choice)
  408. (defcustom org-show-entry-below '((default . nil))
  409. "Non-nil means, show the entry below a headline when revealing a location.
  410. Org-mode often shows locations in an org-mode file which might have
  411. been invisible before. When this is set, the text below the headline that is
  412. exposed is also shown.
  413. By default this is off for all contexts.
  414. Instead of t, this can also be an alist specifying this option for different
  415. contexts. See `org-show-hierarchy-above' for valid contexts."
  416. :group 'org-reveal-location
  417. :type org-context-choice)
  418. (defcustom org-indirect-buffer-display 'other-window
  419. "How should indirect tree buffers be displayed?
  420. This applies to indirect buffers created with the commands
  421. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  422. Valid values are:
  423. current-window Display in the current window
  424. other-window Just display in another window.
  425. dedicated-frame Create one new frame, and re-use it each time.
  426. new-frame Make a new frame each time. Note that in this case
  427. previously-made indirect buffers are kept, and you need to
  428. kill these buffers yourself."
  429. :group 'org-structure
  430. :group 'org-agenda-windows
  431. :type '(choice
  432. (const :tag "In current window" current-window)
  433. (const :tag "In current frame, other window" other-window)
  434. (const :tag "Each time a new frame" new-frame)
  435. (const :tag "One dedicated frame" dedicated-frame)))
  436. (defgroup org-cycle nil
  437. "Options concerning visibility cycling in Org-mode."
  438. :tag "Org Cycle"
  439. :group 'org-structure)
  440. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  441. "Names of drawers. Drawers are not opened by cycling on the headline above.
  442. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  443. this:
  444. :DRAWERNAME:
  445. .....
  446. :END:
  447. The drawer \"PROPERTIES\" is special for capturing properties through
  448. the property API.
  449. Drawers can be defined on the per-file basis with a line like:
  450. #+DRAWERS: HIDDEN STATE PROPERTIES"
  451. :group 'org-structure
  452. :type '(repeat (string :tag "Drawer Name")))
  453. (defcustom org-cycle-global-at-bob nil
  454. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  455. This makes it possible to do global cycling without having to use S-TAB or
  456. C-u TAB. For this special case to work, the first line of the buffer
  457. must not be a headline - it may be empty ot some other text. When used in
  458. this way, `org-cycle-hook' is disables temporarily, to make sure the
  459. cursor stays at the beginning of the buffer.
  460. When this option is nil, don't do anything special at the beginning
  461. of the buffer."
  462. :group 'org-cycle
  463. :type 'boolean)
  464. (defcustom org-cycle-emulate-tab t
  465. "Where should `org-cycle' emulate TAB.
  466. nil Never
  467. white Only in completely white lines
  468. whitestart Only at the beginning of lines, before the first non-white char
  469. t Everywhere except in headlines
  470. exc-hl-bol Everywhere except at the start of a headline
  471. If TAB is used in a place where it does not emulate TAB, the current subtree
  472. visibility is cycled."
  473. :group 'org-cycle
  474. :type '(choice (const :tag "Never" nil)
  475. (const :tag "Only in completely white lines" white)
  476. (const :tag "Before first char in a line" whitestart)
  477. (const :tag "Everywhere except in headlines" t)
  478. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  479. ))
  480. (defcustom org-cycle-separator-lines 2
  481. "Number of empty lines needed to keep an empty line between collapsed trees.
  482. If you leave an empty line between the end of a subtree and the following
  483. headline, this empty line is hidden when the subtree is folded.
  484. Org-mode will leave (exactly) one empty line visible if the number of
  485. empty lines is equal or larger to the number given in this variable.
  486. So the default 2 means, at least 2 empty lines after the end of a subtree
  487. are needed to produce free space between a collapsed subtree and the
  488. following headline.
  489. Special case: when 0, never leave empty lines in collapsed view."
  490. :group 'org-cycle
  491. :type 'integer)
  492. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  493. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  494. org-cycle-hide-drawers
  495. org-cycle-show-empty-lines
  496. org-optimize-window-after-visibility-change)
  497. "Hook that is run after `org-cycle' has changed the buffer visibility.
  498. The function(s) in this hook must accept a single argument which indicates
  499. the new state that was set by the most recent `org-cycle' command. The
  500. argument is a symbol. After a global state change, it can have the values
  501. `overview', `content', or `all'. After a local state change, it can have
  502. the values `folded', `children', or `subtree'."
  503. :group 'org-cycle
  504. :type 'hook)
  505. (defgroup org-edit-structure nil
  506. "Options concerning structure editing in Org-mode."
  507. :tag "Org Edit Structure"
  508. :group 'org-structure)
  509. (defcustom org-odd-levels-only nil
  510. "Non-nil means, skip even levels and only use odd levels for the outline.
  511. This has the effect that two stars are being added/taken away in
  512. promotion/demotion commands. It also influences how levels are
  513. handled by the exporters.
  514. Changing it requires restart of `font-lock-mode' to become effective
  515. for fontification also in regions already fontified.
  516. You may also set this on a per-file basis by adding one of the following
  517. lines to the buffer:
  518. #+STARTUP: odd
  519. #+STARTUP: oddeven"
  520. :group 'org-edit-structure
  521. :group 'org-font-lock
  522. :type 'boolean)
  523. (defcustom org-adapt-indentation t
  524. "Non-nil means, adapt indentation when promoting and demoting.
  525. When this is set and the *entire* text in an entry is indented, the
  526. indentation is increased by one space in a demotion command, and
  527. decreased by one in a promotion command. If any line in the entry
  528. body starts at column 0, indentation is not changed at all."
  529. :group 'org-edit-structure
  530. :type 'boolean)
  531. (defcustom org-special-ctrl-a/e nil
  532. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  533. When t, `C-a' will bring back the cursor to the beginning of the
  534. headline text, i.e. after the stars and after a possible TODO keyword.
  535. In an item, this will be the position after the bullet.
  536. When the cursor is already at that position, another `C-a' will bring
  537. it to the beginning of the line.
  538. `C-e' will jump to the end of the headline, ignoring the presence of tags
  539. in the headline. A second `C-e' will then jump to the true end of the
  540. line, after any tags.
  541. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  542. and only a directly following, identical keypress will bring the cursor
  543. to the special positions."
  544. :group 'org-edit-structure
  545. :type '(choice
  546. (const :tag "off" nil)
  547. (const :tag "after bullet first" t)
  548. (const :tag "border first" reversed)))
  549. (if (fboundp 'defvaralias)
  550. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  551. (defcustom org-special-ctrl-k nil
  552. "Non-nil means `C-k' will behave specially in headlines.
  553. When nil, `C-k' will call the default `kill-line' command.
  554. When t, the following will happen while the cursor is in the headline:
  555. - When the cursor is at the beginning of a headline, kill the entire
  556. line and possible the folded subtree below the line.
  557. - When in the middle of the headline text, kill the headline up to the tags.
  558. - When after the headline text, kill the tags."
  559. :group 'org-edit-structure
  560. :type 'boolean)
  561. (defcustom org-yank-folded-subtrees t
  562. "Non-nil means, when yanking subtrees, fold them.
  563. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  564. it starts with a heading and all other headings in it are either children
  565. or siblings, then fold all the subtrees. However, do this only if no
  566. text after the yank would be swallowed into a folded tree by this action."
  567. :group 'org-edit-structure
  568. :type 'boolean)
  569. (defcustom org-yank-adjusted-subtrees t
  570. "Non-nil means, when yanking subtrees, adjust the level.
  571. With this setting, `org-paste-subtree' is used to insert the subtree, see
  572. this function for details."
  573. :group 'org-edit-structure
  574. :type 'boolean)
  575. (defcustom org-M-RET-may-split-line '((default . t))
  576. "Non-nil means, M-RET will split the line at the cursor position.
  577. When nil, it will go to the end of the line before making a
  578. new line.
  579. You may also set this option in a different way for different
  580. contexts. Valid contexts are:
  581. headline when creating a new headline
  582. item when creating a new item
  583. table in a table field
  584. default the value to be used for all contexts not explicitly
  585. customized"
  586. :group 'org-structure
  587. :group 'org-table
  588. :type '(choice
  589. (const :tag "Always" t)
  590. (const :tag "Never" nil)
  591. (repeat :greedy t :tag "Individual contexts"
  592. (cons
  593. (choice :tag "Context"
  594. (const headline)
  595. (const item)
  596. (const table)
  597. (const default))
  598. (boolean)))))
  599. (defcustom org-insert-heading-respect-content nil
  600. "Non-nil means, insert new headings after the current subtree.
  601. When nil, the new heading is created directly after the current line.
  602. The commands \\[org-insert-heading-respect-content] and
  603. \\[org-insert-todo-heading-respect-content] turn this variable on
  604. for the duration of the command."
  605. :group 'org-structure
  606. :type 'boolean)
  607. (defcustom org-blank-before-new-entry '((heading . nil)
  608. (plain-list-item . nil))
  609. "Should `org-insert-heading' leave a blank line before new heading/item?
  610. The value is an alist, with `heading' and `plain-list-item' as car,
  611. and a boolean flag as cdr."
  612. :group 'org-edit-structure
  613. :type '(list
  614. (cons (const heading) (boolean))
  615. (cons (const plain-list-item) (boolean))))
  616. (defcustom org-insert-heading-hook nil
  617. "Hook being run after inserting a new heading."
  618. :group 'org-edit-structure
  619. :type 'hook)
  620. (defcustom org-enable-fixed-width-editor t
  621. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  622. This currently only means, they are never auto-wrapped.
  623. When nil, such lines will be treated like ordinary lines.
  624. See also the QUOTE keyword."
  625. :group 'org-edit-structure
  626. :type 'boolean)
  627. (defcustom org-edit-src-region-extra nil
  628. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  629. For examples see the function `org-edit-src-find-region-and-lang'.
  630. The regular expression identifying the begin marker should end with a newline,
  631. and the regexp marking the end line should start with a newline, to make sure
  632. there are kept outside the narrowed region."
  633. :group 'org-edit-structure
  634. :type '(repeat
  635. (list
  636. (regexp :tag "begin regexp")
  637. (regexp :tag "end regexp")
  638. (choice :tag "language"
  639. (string :tag "specify")
  640. (integer :tag "from match group")
  641. (const :tag "from `lang' element")
  642. (const :tag "from `style' element")))))
  643. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  644. "The mode that should be used to edit fixed-width regions.
  645. These are the regions where each line starts with a colon."
  646. :group 'org-edit-structure
  647. :type '(choice
  648. (const artist-mode)
  649. (const picture-mode)
  650. (const fundamental-mode)
  651. (function :tag "Other (specify)")))
  652. (defcustom org-goto-auto-isearch t
  653. "Non-nil means, typing characters in org-goto starts incremental search."
  654. :group 'org-edit-structure
  655. :type 'boolean)
  656. (defgroup org-sparse-trees nil
  657. "Options concerning sparse trees in Org-mode."
  658. :tag "Org Sparse Trees"
  659. :group 'org-structure)
  660. (defcustom org-highlight-sparse-tree-matches t
  661. "Non-nil means, highlight all matches that define a sparse tree.
  662. The highlights will automatically disappear the next time the buffer is
  663. changed by an edit command."
  664. :group 'org-sparse-trees
  665. :type 'boolean)
  666. (defcustom org-remove-highlights-with-change t
  667. "Non-nil means, any change to the buffer will remove temporary highlights.
  668. Such highlights are created by `org-occur' and `org-clock-display'.
  669. When nil, `C-c C-c needs to be used to get rid of the highlights.
  670. The highlights created by `org-preview-latex-fragment' always need
  671. `C-c C-c' to be removed."
  672. :group 'org-sparse-trees
  673. :group 'org-time
  674. :type 'boolean)
  675. (defcustom org-occur-hook '(org-first-headline-recenter)
  676. "Hook that is run after `org-occur' has constructed a sparse tree.
  677. This can be used to recenter the window to show as much of the structure
  678. as possible."
  679. :group 'org-sparse-trees
  680. :type 'hook)
  681. (defgroup org-imenu-and-speedbar nil
  682. "Options concerning imenu and speedbar in Org-mode."
  683. :tag "Org Imenu and Speedbar"
  684. :group 'org-structure)
  685. (defcustom org-imenu-depth 2
  686. "The maximum level for Imenu access to Org-mode headlines.
  687. This also applied for speedbar access."
  688. :group 'org-imenu-and-speedbar
  689. :type 'number)
  690. (defgroup org-table nil
  691. "Options concerning tables in Org-mode."
  692. :tag "Org Table"
  693. :group 'org)
  694. (defcustom org-enable-table-editor 'optimized
  695. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  696. When nil, such lines will be treated like ordinary lines.
  697. When equal to the symbol `optimized', the table editor will be optimized to
  698. do the following:
  699. - Automatic overwrite mode in front of whitespace in table fields.
  700. This makes the structure of the table stay in tact as long as the edited
  701. field does not exceed the column width.
  702. - Minimize the number of realigns. Normally, the table is aligned each time
  703. TAB or RET are pressed to move to another field. With optimization this
  704. happens only if changes to a field might have changed the column width.
  705. Optimization requires replacing the functions `self-insert-command',
  706. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  707. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  708. very good at guessing when a re-align will be necessary, but you can always
  709. force one with \\[org-ctrl-c-ctrl-c].
  710. If you would like to use the optimized version in Org-mode, but the
  711. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  712. This variable can be used to turn on and off the table editor during a session,
  713. but in order to toggle optimization, a restart is required.
  714. See also the variable `org-table-auto-blank-field'."
  715. :group 'org-table
  716. :type '(choice
  717. (const :tag "off" nil)
  718. (const :tag "on" t)
  719. (const :tag "on, optimized" optimized)))
  720. (defcustom org-table-tab-recognizes-table.el t
  721. "Non-nil means, TAB will automatically notice a table.el table.
  722. When it sees such a table, it moves point into it and - if necessary -
  723. calls `table-recognize-table'."
  724. :group 'org-table-editing
  725. :type 'boolean)
  726. (defgroup org-link nil
  727. "Options concerning links in Org-mode."
  728. :tag "Org Link"
  729. :group 'org)
  730. (defvar org-link-abbrev-alist-local nil
  731. "Buffer-local version of `org-link-abbrev-alist', which see.
  732. The value of this is taken from the #+LINK lines.")
  733. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  734. (defcustom org-link-abbrev-alist nil
  735. "Alist of link abbreviations.
  736. The car of each element is a string, to be replaced at the start of a link.
  737. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  738. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  739. [[linkkey:tag][description]]
  740. If REPLACE is a string, the tag will simply be appended to create the link.
  741. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  742. the placeholder \"%h\" will cause a url-encoded version of the tag to
  743. be inserted at that point (see the function `url-hexify-string').
  744. REPLACE may also be a function that will be called with the tag as the
  745. only argument to create the link, which should be returned as a string.
  746. See the manual for examples."
  747. :group 'org-link
  748. :type '(repeat
  749. (cons
  750. (string :tag "Protocol")
  751. (choice
  752. (string :tag "Format")
  753. (function)))))
  754. (defcustom org-descriptive-links t
  755. "Non-nil means, hide link part and only show description of bracket links.
  756. Bracket links are like [[link][descritpion]]. This variable sets the initial
  757. state in new org-mode buffers. The setting can then be toggled on a
  758. per-buffer basis from the Org->Hyperlinks menu."
  759. :group 'org-link
  760. :type 'boolean)
  761. (defcustom org-link-file-path-type 'adaptive
  762. "How the path name in file links should be stored.
  763. Valid values are:
  764. relative Relative to the current directory, i.e. the directory of the file
  765. into which the link is being inserted.
  766. absolute Absolute path, if possible with ~ for home directory.
  767. noabbrev Absolute path, no abbreviation of home directory.
  768. adaptive Use relative path for files in the current directory and sub-
  769. directories of it. For other files, use an absolute path."
  770. :group 'org-link
  771. :type '(choice
  772. (const relative)
  773. (const absolute)
  774. (const noabbrev)
  775. (const adaptive)))
  776. (defcustom org-activate-links '(bracket angle plain radio tag date)
  777. "Types of links that should be activated in Org-mode files.
  778. This is a list of symbols, each leading to the activation of a certain link
  779. type. In principle, it does not hurt to turn on most link types - there may
  780. be a small gain when turning off unused link types. The types are:
  781. bracket The recommended [[link][description]] or [[link]] links with hiding.
  782. angular Links in angular brackes that may contain whitespace like
  783. <bbdb:Carsten Dominik>.
  784. plain Plain links in normal text, no whitespace, like http://google.com.
  785. radio Text that is matched by a radio target, see manual for details.
  786. tag Tag settings in a headline (link to tag search).
  787. date Time stamps (link to calendar).
  788. Changing this variable requires a restart of Emacs to become effective."
  789. :group 'org-link
  790. :type '(set (const :tag "Double bracket links (new style)" bracket)
  791. (const :tag "Angular bracket links (old style)" angular)
  792. (const :tag "Plain text links" plain)
  793. (const :tag "Radio target matches" radio)
  794. (const :tag "Tags" tag)
  795. (const :tag "Timestamps" date)))
  796. (defcustom org-make-link-description-function nil
  797. "Function to use to generate link descriptions from links. If
  798. nil the link location will be used. This function must take two
  799. parameters; the first is the link and the second the description
  800. org-insert-link has generated, and should return the description
  801. to use."
  802. :group 'org-link
  803. :type 'function)
  804. (defgroup org-link-store nil
  805. "Options concerning storing links in Org-mode."
  806. :tag "Org Store Link"
  807. :group 'org-link)
  808. (defcustom org-email-link-description-format "Email %c: %.30s"
  809. "Format of the description part of a link to an email or usenet message.
  810. The following %-excapes will be replaced by corresponding information:
  811. %F full \"From\" field
  812. %f name, taken from \"From\" field, address if no name
  813. %T full \"To\" field
  814. %t first name in \"To\" field, address if no name
  815. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  816. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  817. %s subject
  818. %m message-id.
  819. You may use normal field width specification between the % and the letter.
  820. This is for example useful to limit the length of the subject.
  821. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  822. :group 'org-link-store
  823. :type 'string)
  824. (defcustom org-from-is-user-regexp
  825. (let (r1 r2)
  826. (when (and user-mail-address (not (string= user-mail-address "")))
  827. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  828. (when (and user-full-name (not (string= user-full-name "")))
  829. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  830. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  831. "Regexp mached against the \"From:\" header of an email or usenet message.
  832. It should match if the message is from the user him/herself."
  833. :group 'org-link-store
  834. :type 'regexp)
  835. (defcustom org-context-in-file-links t
  836. "Non-nil means, file links from `org-store-link' contain context.
  837. A search string will be added to the file name with :: as separator and
  838. used to find the context when the link is activated by the command
  839. `org-open-at-point'.
  840. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  841. negates this setting for the duration of the command."
  842. :group 'org-link-store
  843. :type 'boolean)
  844. (defcustom org-keep-stored-link-after-insertion nil
  845. "Non-nil means, keep link in list for entire session.
  846. The command `org-store-link' adds a link pointing to the current
  847. location to an internal list. These links accumulate during a session.
  848. The command `org-insert-link' can be used to insert links into any
  849. Org-mode file (offering completion for all stored links). When this
  850. option is nil, every link which has been inserted once using \\[org-insert-link]
  851. will be removed from the list, to make completing the unused links
  852. more efficient."
  853. :group 'org-link-store
  854. :type 'boolean)
  855. (defgroup org-link-follow nil
  856. "Options concerning following links in Org-mode."
  857. :tag "Org Follow Link"
  858. :group 'org-link)
  859. (defcustom org-link-translation-function nil
  860. "Function to translate links with different syntax to Org syntax.
  861. This can be used to translate links created for example by the Planner
  862. or emacs-wiki packages to Org syntax.
  863. The function must accept two parameters, a TYPE containing the link
  864. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  865. which is everything after the link protocol. It should return a cons
  866. with possibly modifed values of type and path.
  867. Org contains a function for this, so if you set this variable to
  868. `org-translate-link-from-planner', you should be able follow many
  869. links created by planner."
  870. :group 'org-link-follow
  871. :type 'function)
  872. (defcustom org-follow-link-hook nil
  873. "Hook that is run after a link has been followed."
  874. :group 'org-link-follow
  875. :type 'hook)
  876. (defcustom org-tab-follows-link nil
  877. "Non-nil means, on links TAB will follow the link.
  878. Needs to be set before org.el is loaded."
  879. :group 'org-link-follow
  880. :type 'boolean)
  881. (defcustom org-return-follows-link nil
  882. "Non-nil means, on links RET will follow the link.
  883. Needs to be set before org.el is loaded."
  884. :group 'org-link-follow
  885. :type 'boolean)
  886. (defcustom org-mouse-1-follows-link
  887. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  888. "Non-nil means, mouse-1 on a link will follow the link.
  889. A longer mouse click will still set point. Does not work on XEmacs.
  890. Needs to be set before org.el is loaded."
  891. :group 'org-link-follow
  892. :type 'boolean)
  893. (defcustom org-mark-ring-length 4
  894. "Number of different positions to be recorded in the ring
  895. Changing this requires a restart of Emacs to work correctly."
  896. :group 'org-link-follow
  897. :type 'interger)
  898. (defcustom org-link-frame-setup
  899. '((vm . vm-visit-folder-other-frame)
  900. (gnus . gnus-other-frame)
  901. (file . find-file-other-window))
  902. "Setup the frame configuration for following links.
  903. When following a link with Emacs, it may often be useful to display
  904. this link in another window or frame. This variable can be used to
  905. set this up for the different types of links.
  906. For VM, use any of
  907. `vm-visit-folder'
  908. `vm-visit-folder-other-frame'
  909. For Gnus, use any of
  910. `gnus'
  911. `gnus-other-frame'
  912. `org-gnus-no-new-news'
  913. For FILE, use any of
  914. `find-file'
  915. `find-file-other-window'
  916. `find-file-other-frame'
  917. For the calendar, use the variable `calendar-setup'.
  918. For BBDB, it is currently only possible to display the matches in
  919. another window."
  920. :group 'org-link-follow
  921. :type '(list
  922. (cons (const vm)
  923. (choice
  924. (const vm-visit-folder)
  925. (const vm-visit-folder-other-window)
  926. (const vm-visit-folder-other-frame)))
  927. (cons (const gnus)
  928. (choice
  929. (const gnus)
  930. (const gnus-other-frame)
  931. (const org-gnus-no-new-news)))
  932. (cons (const file)
  933. (choice
  934. (const find-file)
  935. (const find-file-other-window)
  936. (const find-file-other-frame)))))
  937. (defcustom org-display-internal-link-with-indirect-buffer nil
  938. "Non-nil means, use indirect buffer to display infile links.
  939. Activating internal links (from one location in a file to another location
  940. in the same file) normally just jumps to the location. When the link is
  941. activated with a C-u prefix (or with mouse-3), the link is displayed in
  942. another window. When this option is set, the other window actually displays
  943. an indirect buffer clone of the current buffer, to avoid any visibility
  944. changes to the current buffer."
  945. :group 'org-link-follow
  946. :type 'boolean)
  947. (defcustom org-open-non-existing-files nil
  948. "Non-nil means, `org-open-file' will open non-existing files.
  949. When nil, an error will be generated."
  950. :group 'org-link-follow
  951. :type 'boolean)
  952. (defcustom org-open-directory-means-index-dot-org nil
  953. "Non-nil means, a link to a directory really means to index.org.
  954. When nil, following a directory link will run dired or open a finder/explorer
  955. window on that directory."
  956. :group 'org-link-follow
  957. :type 'boolean)
  958. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  959. "Function and arguments to call for following mailto links.
  960. This is a list with the first element being a lisp function, and the
  961. remaining elements being arguments to the function. In string arguments,
  962. %a will be replaced by the address, and %s will be replaced by the subject
  963. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  964. :group 'org-link-follow
  965. :type '(choice
  966. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  967. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  968. (const :tag "message-mail" (message-mail "%a" "%s"))
  969. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  970. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  971. "Non-nil means, ask for confirmation before executing shell links.
  972. Shell links can be dangerous: just think about a link
  973. [[shell:rm -rf ~/*][Google Search]]
  974. This link would show up in your Org-mode document as \"Google Search\",
  975. but really it would remove your entire home directory.
  976. Therefore we advise against setting this variable to nil.
  977. Just change it to `y-or-n-p' of you want to confirm with a
  978. single keystroke rather than having to type \"yes\"."
  979. :group 'org-link-follow
  980. :type '(choice
  981. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  982. (const :tag "with y-or-n (faster)" y-or-n-p)
  983. (const :tag "no confirmation (dangerous)" nil)))
  984. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  985. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  986. Elisp links can be dangerous: just think about a link
  987. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  988. This link would show up in your Org-mode document as \"Google Search\",
  989. but really it would remove your entire home directory.
  990. Therefore we advise against setting this variable to nil.
  991. Just change it to `y-or-n-p' of you want to confirm with a
  992. single keystroke rather than having to type \"yes\"."
  993. :group 'org-link-follow
  994. :type '(choice
  995. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  996. (const :tag "with y-or-n (faster)" y-or-n-p)
  997. (const :tag "no confirmation (dangerous)" nil)))
  998. (defconst org-file-apps-defaults-gnu
  999. '((remote . emacs)
  1000. (system . mailcap)
  1001. (t . mailcap))
  1002. "Default file applications on a UNIX or GNU/Linux system.
  1003. See `org-file-apps'.")
  1004. (defconst org-file-apps-defaults-macosx
  1005. '((remote . emacs)
  1006. (t . "open %s")
  1007. (system . "open %s")
  1008. ("ps.gz" . "gv %s")
  1009. ("eps.gz" . "gv %s")
  1010. ("dvi" . "xdvi %s")
  1011. ("fig" . "xfig %s"))
  1012. "Default file applications on a MacOS X system.
  1013. The system \"open\" is known as a default, but we use X11 applications
  1014. for some files for which the OS does not have a good default.
  1015. See `org-file-apps'.")
  1016. (defconst org-file-apps-defaults-windowsnt
  1017. (list
  1018. '(remote . emacs)
  1019. (cons t
  1020. (list (if (featurep 'xemacs)
  1021. 'mswindows-shell-execute
  1022. 'w32-shell-execute)
  1023. "open" 'file))
  1024. (cons 'system
  1025. (list (if (featurep 'xemacs)
  1026. 'mswindows-shell-execute
  1027. 'w32-shell-execute)
  1028. "open" 'file)))
  1029. "Default file applications on a Windows NT system.
  1030. The system \"open\" is used for most files.
  1031. See `org-file-apps'.")
  1032. (defcustom org-file-apps
  1033. '(
  1034. (auto-mode . emacs)
  1035. ("\\.x?html?\\'" . default)
  1036. ("\\.pdf\\'" . default)
  1037. )
  1038. "External applications for opening `file:path' items in a document.
  1039. Org-mode uses system defaults for different file types, but
  1040. you can use this variable to set the application for a given file
  1041. extension. The entries in this list are cons cells where the car identifies
  1042. files and the cdr the corresponding command. Possible values for the
  1043. file identifier are
  1044. \"regex\" Regular expression matched against the file name. For backward
  1045. compatibility, this can also be a string with only alphanumeric
  1046. characters, which is then interpreted as an extension.
  1047. `directory' Matches a directory
  1048. `remote' Matches a remote file, accessible through tramp or efs.
  1049. Remote files most likely should be visited through Emacs
  1050. because external applications cannot handle such paths.
  1051. `auto-mode' Matches files that are mached by any entry in `auto-mode-alist',
  1052. so all files Emacs knows how to handle. Using this with
  1053. command `emacs' will open most files in Emacs. Beware that this
  1054. will also open html files insite Emacs, unless you add
  1055. (\"html\" . default) to the list as well.
  1056. t Default for files not matched by any of the other options.
  1057. `system' The system command to open files, like `open' on Windows
  1058. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1059. that will be selected if you call `C-c C-o' with a double
  1060. `C-u C-u' prefix.
  1061. Possible values for the command are:
  1062. `emacs' The file will be visited by the current Emacs process.
  1063. `default' Use the default application for this file type, which is the
  1064. association for t in the list, most likely in the system-specific
  1065. part.
  1066. This can be used to overrule an unwanted seting in the
  1067. system-specific variable.
  1068. `system' Use the system command for opening files, like \"open\".
  1069. This command is specified by the entry whose car is `system'.
  1070. Most likely, the system-specific version of this variable
  1071. does define this command, but you can overrule/replace it
  1072. here.
  1073. string A command to be executed by a shell; %s will be replaced
  1074. by the path to the file.
  1075. sexp A Lisp form which will be evaluated. The file path will
  1076. be available in the Lisp variable `file'.
  1077. For more examples, see the system specific constants
  1078. `org-file-apps-defaults-macosx'
  1079. `org-file-apps-defaults-windowsnt'
  1080. `org-file-apps-defaults-gnu'."
  1081. :group 'org-link-follow
  1082. :type '(repeat
  1083. (cons (choice :value ""
  1084. (string :tag "Extension")
  1085. (const :tag "System command to open files" system)
  1086. (const :tag "Default for unrecognized files" t)
  1087. (const :tag "Remote file" remote)
  1088. (const :tag "Links to a directory" directory)
  1089. (const :tag "Any files that have Emacs modes"
  1090. auto-mode))
  1091. (choice :value ""
  1092. (const :tag "Visit with Emacs" emacs)
  1093. (const :tag "Use default" default)
  1094. (const :tag "Use the system command" system)
  1095. (string :tag "Command")
  1096. (sexp :tag "Lisp form")))))
  1097. (defgroup org-refile nil
  1098. "Options concerning refiling entries in Org-mode."
  1099. :tag "Org Refile"
  1100. :group 'org)
  1101. (defcustom org-directory "~/org"
  1102. "Directory with org files.
  1103. This directory will be used as default to prompt for org files.
  1104. Used by the hooks for remember.el."
  1105. :group 'org-refile
  1106. :group 'org-remember
  1107. :type 'directory)
  1108. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1109. "Default target for storing notes.
  1110. Used by the hooks for remember.el. This can be a string, or nil to mean
  1111. the value of `remember-data-file'.
  1112. You can set this on a per-template basis with the variable
  1113. `org-remember-templates'."
  1114. :group 'org-refile
  1115. :group 'org-remember
  1116. :type '(choice
  1117. (const :tag "Default from remember-data-file" nil)
  1118. file))
  1119. (defcustom org-goto-interface 'outline
  1120. "The default interface to be used for `org-goto'.
  1121. Allowed vaues are:
  1122. outline The interface shows an outline of the relevant file
  1123. and the correct heading is found by moving through
  1124. the outline or by searching with incremental search.
  1125. outline-path-completion Headlines in the current buffer are offered via
  1126. completion. This is the interface also used by
  1127. the refile command."
  1128. :group 'org-refile
  1129. :type '(choice
  1130. (const :tag "Outline" outline)
  1131. (const :tag "Outline-path-completion" outline-path-completion)))
  1132. (defcustom org-reverse-note-order nil
  1133. "Non-nil means, store new notes at the beginning of a file or entry.
  1134. When nil, new notes will be filed to the end of a file or entry.
  1135. This can also be a list with cons cells of regular expressions that
  1136. are matched against file names, and values."
  1137. :group 'org-remember
  1138. :group 'org-refile
  1139. :type '(choice
  1140. (const :tag "Reverse always" t)
  1141. (const :tag "Reverse never" nil)
  1142. (repeat :tag "By file name regexp"
  1143. (cons regexp boolean))))
  1144. (defcustom org-refile-targets nil
  1145. "Targets for refiling entries with \\[org-refile].
  1146. This is list of cons cells. Each cell contains:
  1147. - a specification of the files to be considered, either a list of files,
  1148. or a symbol whose function or variable value will be used to retrieve
  1149. a file name or a list of file names. Nil means, refile to a different
  1150. heading in the current buffer.
  1151. - A specification of how to find candidate refile targets. This may be
  1152. any of
  1153. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1154. This tag has to be present in all target headlines, inheritance will
  1155. not be considered.
  1156. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1157. todo keyword.
  1158. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1159. headlines that are refiling targets.
  1160. - a cons cell (:level . N). Any headline of level N is considered a target.
  1161. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1162. When this variable is nil, all top-level headlines in the current buffer
  1163. are used, equivalent to the value `((nil . (:level . 1))'."
  1164. :group 'org-refile
  1165. :type '(repeat
  1166. (cons
  1167. (choice :value org-agenda-files
  1168. (const :tag "All agenda files" org-agenda-files)
  1169. (const :tag "Current buffer" nil)
  1170. (function) (variable) (file))
  1171. (choice :tag "Identify target headline by"
  1172. (cons :tag "Specific tag" (const :value :tag) (string))
  1173. (cons :tag "TODO keyword" (const :value :todo) (string))
  1174. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1175. (cons :tag "Level number" (const :value :level) (integer))
  1176. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1177. (defcustom org-refile-use-outline-path nil
  1178. "Non-nil means, provide refile targets as paths.
  1179. So a level 3 headline will be available as level1/level2/level3.
  1180. When the value is `file', also include the file name (without directory)
  1181. into the path. When `full-file-path', include the full file path."
  1182. :group 'org-refile
  1183. :type '(choice
  1184. (const :tag "Not" nil)
  1185. (const :tag "Yes" t)
  1186. (const :tag "Start with file name" file)
  1187. (const :tag "Start with full file path" full-file-path)))
  1188. (defcustom org-outline-path-complete-in-steps t
  1189. "Non-nil means, complete the outline path in hierarchical steps.
  1190. When Org-mode uses the refile interface to select an outline path
  1191. \(see variable `org-refile-use-outline-path'), the completion of
  1192. the path can be done is a single go, or if can be done in steps down
  1193. the headline hierarchy. Going in steps is probably the best if you
  1194. do not use a special completion package like `ido' or `icicles'.
  1195. However, when using these packages, going in one step can be very
  1196. fast, while still showing the whole path to the entry."
  1197. :group 'org-refile
  1198. :type 'boolean)
  1199. (defgroup org-todo nil
  1200. "Options concerning TODO items in Org-mode."
  1201. :tag "Org TODO"
  1202. :group 'org)
  1203. (defgroup org-progress nil
  1204. "Options concerning Progress logging in Org-mode."
  1205. :tag "Org Progress"
  1206. :group 'org-time)
  1207. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1208. "List of TODO entry keyword sequences and their interpretation.
  1209. \\<org-mode-map>This is a list of sequences.
  1210. Each sequence starts with a symbol, either `sequence' or `type',
  1211. indicating if the keywords should be interpreted as a sequence of
  1212. action steps, or as different types of TODO items. The first
  1213. keywords are states requiring action - these states will select a headline
  1214. for inclusion into the global TODO list Org-mode produces. If one of
  1215. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1216. signify that no further action is necessary. If \"|\" is not found,
  1217. the last keyword is treated as the only DONE state of the sequence.
  1218. The command \\[org-todo] cycles an entry through these states, and one
  1219. additional state where no keyword is present. For details about this
  1220. cycling, see the manual.
  1221. TODO keywords and interpretation can also be set on a per-file basis with
  1222. the special #+SEQ_TODO and #+TYP_TODO lines.
  1223. Each keyword can optionally specify a character for fast state selection
  1224. \(in combination with the variable `org-use-fast-todo-selection')
  1225. and specifiers for state change logging, using the same syntax
  1226. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1227. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1228. indicates to record a time stamp each time this state is selected.
  1229. Each keyword may also specify if a timestamp or a note should be
  1230. recorded when entering or leaving the state, by adding additional
  1231. characters in the parenthesis after the keyword. This looks like this:
  1232. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1233. record only the time of the state change. With X and Y being either
  1234. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1235. Y when leaving the state if and only if the *target* state does not
  1236. define X. You may omit any of the fast-selection key or X or /Y,
  1237. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1238. For backward compatibility, this variable may also be just a list
  1239. of keywords - in this case the interptetation (sequence or type) will be
  1240. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1241. :group 'org-todo
  1242. :group 'org-keywords
  1243. :type '(choice
  1244. (repeat :tag "Old syntax, just keywords"
  1245. (string :tag "Keyword"))
  1246. (repeat :tag "New syntax"
  1247. (cons
  1248. (choice
  1249. :tag "Interpretation"
  1250. (const :tag "Sequence (cycling hits every state)" sequence)
  1251. (const :tag "Type (cycling directly to DONE)" type))
  1252. (repeat
  1253. (string :tag "Keyword"))))))
  1254. (defvar org-todo-keywords-1 nil
  1255. "All TODO and DONE keywords active in a buffer.")
  1256. (make-variable-buffer-local 'org-todo-keywords-1)
  1257. (defvar org-todo-keywords-for-agenda nil)
  1258. (defvar org-done-keywords-for-agenda nil)
  1259. (defvar org-todo-keyword-alist-for-agenda nil)
  1260. (defvar org-tag-alist-for-agenda nil)
  1261. (defvar org-agenda-contributing-files nil)
  1262. (defvar org-not-done-keywords nil)
  1263. (make-variable-buffer-local 'org-not-done-keywords)
  1264. (defvar org-done-keywords nil)
  1265. (make-variable-buffer-local 'org-done-keywords)
  1266. (defvar org-todo-heads nil)
  1267. (make-variable-buffer-local 'org-todo-heads)
  1268. (defvar org-todo-sets nil)
  1269. (make-variable-buffer-local 'org-todo-sets)
  1270. (defvar org-todo-log-states nil)
  1271. (make-variable-buffer-local 'org-todo-log-states)
  1272. (defvar org-todo-kwd-alist nil)
  1273. (make-variable-buffer-local 'org-todo-kwd-alist)
  1274. (defvar org-todo-key-alist nil)
  1275. (make-variable-buffer-local 'org-todo-key-alist)
  1276. (defvar org-todo-key-trigger nil)
  1277. (make-variable-buffer-local 'org-todo-key-trigger)
  1278. (defcustom org-todo-interpretation 'sequence
  1279. "Controls how TODO keywords are interpreted.
  1280. This variable is in principle obsolete and is only used for
  1281. backward compatibility, if the interpretation of todo keywords is
  1282. not given already in `org-todo-keywords'. See that variable for
  1283. more information."
  1284. :group 'org-todo
  1285. :group 'org-keywords
  1286. :type '(choice (const sequence)
  1287. (const type)))
  1288. (defcustom org-use-fast-todo-selection 'prefix
  1289. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1290. This variable describes if and under what circumstances the cycling
  1291. mechanism for TODO keywords will be replaced by a single-key, direct
  1292. selection scheme.
  1293. When nil, fast selection is never used.
  1294. When the symbol `prefix', it will be used when `org-todo' is called with
  1295. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1296. in an agenda buffer.
  1297. When t, fast selection is used by default. In this case, the prefix
  1298. argument forces cycling instead.
  1299. In all cases, the special interface is only used if access keys have actually
  1300. been assigned by the user, i.e. if keywords in the configuration are followed
  1301. by a letter in parenthesis, like TODO(t)."
  1302. :group 'org-todo
  1303. :type '(choice
  1304. (const :tag "Never" nil)
  1305. (const :tag "By default" t)
  1306. (const :tag "Only with C-u C-c C-t" prefix)))
  1307. (defcustom org-provide-todo-statistics t
  1308. "Non-nil means, update todo statistics after insert and toggle.
  1309. When this is set, todo statistics is updated in the parent of the current
  1310. entry each time a todo state is changed."
  1311. :group 'org-todo
  1312. :type 'boolean)
  1313. (defcustom org-after-todo-state-change-hook nil
  1314. "Hook which is run after the state of a TODO item was changed.
  1315. The new state (a string with a TODO keyword, or nil) is available in the
  1316. Lisp variable `state'."
  1317. :group 'org-todo
  1318. :type 'hook)
  1319. (defcustom org-todo-state-tags-triggers nil
  1320. "Tag changes that should be triggered by TODO state changes.
  1321. This is a list. Each entry is
  1322. (state-change (tag . flag) .......)
  1323. State-change can be a string with a state, and empty string to indicate the
  1324. state that has no TODO keyword, or it can be one of the symbols `todo'
  1325. or `done', meaning any not-done or done state, respectively."
  1326. :group 'org-todo
  1327. :group 'org-tags
  1328. :type '(repeat
  1329. (cons (choice :tag "When changing to"
  1330. (const :tag "Not-done state" todo)
  1331. (const :tag "Done state" done)
  1332. (string :tag "State"))
  1333. (repeat
  1334. (cons :tag "Tag action"
  1335. (string :tag "Tag")
  1336. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1337. (defcustom org-log-done nil
  1338. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1339. When equal to the list (done), also prompt for a closing note.
  1340. This can also be configured on a per-file basis by adding one of
  1341. the following lines anywhere in the buffer:
  1342. #+STARTUP: logdone
  1343. #+STARTUP: lognotedone
  1344. #+STARTUP: nologdone"
  1345. :group 'org-todo
  1346. :group 'org-progress
  1347. :type '(choice
  1348. (const :tag "No logging" nil)
  1349. (const :tag "Record CLOSED timestamp" time)
  1350. (const :tag "Record CLOSED timestamp with closing note." note)))
  1351. ;; Normalize old uses of org-log-done.
  1352. (cond
  1353. ((eq org-log-done t) (setq org-log-done 'time))
  1354. ((and (listp org-log-done) (memq 'done org-log-done))
  1355. (setq org-log-done 'note)))
  1356. (defcustom org-log-note-clock-out nil
  1357. "Non-nil means, record a note when clocking out of an item.
  1358. This can also be configured on a per-file basis by adding one of
  1359. the following lines anywhere in the buffer:
  1360. #+STARTUP: lognoteclock-out
  1361. #+STARTUP: nolognoteclock-out"
  1362. :group 'org-todo
  1363. :group 'org-progress
  1364. :type 'boolean)
  1365. (defcustom org-log-done-with-time t
  1366. "Non-nil means, the CLOSED time stamp will contain date and time.
  1367. When nil, only the date will be recorded."
  1368. :group 'org-progress
  1369. :type 'boolean)
  1370. (defcustom org-log-note-headings
  1371. '((done . "CLOSING NOTE %t")
  1372. (state . "State %-12s %t")
  1373. (note . "Note taken on %t")
  1374. (clock-out . ""))
  1375. "Headings for notes added to entries.
  1376. The value is an alist, with the car being a symbol indicating the note
  1377. context, and the cdr is the heading to be used. The heading may also be the
  1378. empty string.
  1379. %t in the heading will be replaced by a time stamp.
  1380. %s will be replaced by the new TODO state, in double quotes.
  1381. %u will be replaced by the user name.
  1382. %U will be replaced by the full user name."
  1383. :group 'org-todo
  1384. :group 'org-progress
  1385. :type '(list :greedy t
  1386. (cons (const :tag "Heading when closing an item" done) string)
  1387. (cons (const :tag
  1388. "Heading when changing todo state (todo sequence only)"
  1389. state) string)
  1390. (cons (const :tag "Heading when just taking a note" note) string)
  1391. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1392. (unless (assq 'note org-log-note-headings)
  1393. (push '(note . "%t") org-log-note-headings))
  1394. (defcustom org-log-state-notes-insert-after-drawers nil
  1395. "Non-nil means, insert state change notes after any drawers in entry.
  1396. Only the drawers that *immediately* follow the headline and the
  1397. deadline/scheduled line are skipped.
  1398. When nil, insert notes right after the heading and perhaps the line
  1399. with deadline/scheduling if present."
  1400. :group 'org-todo
  1401. :group 'org-progress
  1402. :type 'boolean)
  1403. (defcustom org-log-states-order-reversed t
  1404. "Non-nil means, the latest state change note will be directly after heading.
  1405. When nil, the notes will be orderer according to time."
  1406. :group 'org-todo
  1407. :group 'org-progress
  1408. :type 'boolean)
  1409. (defcustom org-log-repeat 'time
  1410. "Non-nil means, record moving through the DONE state when triggering repeat.
  1411. An auto-repeating tasks is immediately switched back to TODO when marked
  1412. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1413. the TODO keyword definition, or recording a closing note by setting
  1414. `org-log-done', there will be no record of the task moving through DONE.
  1415. This variable forces taking a note anyway. Possible values are:
  1416. nil Don't force a record
  1417. time Record a time stamp
  1418. note Record a note
  1419. This option can also be set with on a per-file-basis with
  1420. #+STARTUP: logrepeat
  1421. #+STARTUP: lognoterepeat
  1422. #+STARTUP: nologrepeat
  1423. You can have local logging settings for a subtree by setting the LOGGING
  1424. property to one or more of these keywords."
  1425. :group 'org-todo
  1426. :group 'org-progress
  1427. :type '(choice
  1428. (const :tag "Don't force a record" nil)
  1429. (const :tag "Force recording the DONE state" time)
  1430. (const :tag "Force recording a note with the DONE state" note)))
  1431. (defgroup org-priorities nil
  1432. "Priorities in Org-mode."
  1433. :tag "Org Priorities"
  1434. :group 'org-todo)
  1435. (defcustom org-highest-priority ?A
  1436. "The highest priority of TODO items. A character like ?A, ?B etc.
  1437. Must have a smaller ASCII number than `org-lowest-priority'."
  1438. :group 'org-priorities
  1439. :type 'character)
  1440. (defcustom org-lowest-priority ?C
  1441. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1442. Must have a larger ASCII number than `org-highest-priority'."
  1443. :group 'org-priorities
  1444. :type 'character)
  1445. (defcustom org-default-priority ?B
  1446. "The default priority of TODO items.
  1447. This is the priority an item get if no explicit priority is given."
  1448. :group 'org-priorities
  1449. :type 'character)
  1450. (defcustom org-priority-start-cycle-with-default t
  1451. "Non-nil means, start with default priority when starting to cycle.
  1452. When this is nil, the first step in the cycle will be (depending on the
  1453. command used) one higher or lower that the default priority."
  1454. :group 'org-priorities
  1455. :type 'boolean)
  1456. (defgroup org-time nil
  1457. "Options concerning time stamps and deadlines in Org-mode."
  1458. :tag "Org Time"
  1459. :group 'org)
  1460. (defcustom org-insert-labeled-timestamps-at-point nil
  1461. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1462. When nil, these labeled time stamps are forces into the second line of an
  1463. entry, just after the headline. When scheduling from the global TODO list,
  1464. the time stamp will always be forced into the second line."
  1465. :group 'org-time
  1466. :type 'boolean)
  1467. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1468. "Formats for `format-time-string' which are used for time stamps.
  1469. It is not recommended to change this constant.")
  1470. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1471. "Number of minutes to round time stamps to.
  1472. These are two values, the first applies when first creating a time stamp.
  1473. The second applies when changing it with the commands `S-up' and `S-down'.
  1474. When changing the time stamp, this means that it will change in steps
  1475. of N minutes, as given by the second value.
  1476. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1477. numbers should be factors of 60, so for example 5, 10, 15.
  1478. When this is larger than 1, you can still force an exact time-stamp by using
  1479. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1480. and by using a prefix arg to `S-up/down' to specify the exact number
  1481. of minutes to shift."
  1482. :group 'org-time
  1483. :get '(lambda (var) ; Make sure all entries have 5 elements
  1484. (if (integerp (default-value var))
  1485. (list (default-value var) 5)
  1486. (default-value var)))
  1487. :type '(list
  1488. (integer :tag "when inserting times")
  1489. (integer :tag "when modifying times")))
  1490. ;; Normalize old customizations of this variable.
  1491. (when (integerp org-time-stamp-rounding-minutes)
  1492. (setq org-time-stamp-rounding-minutes
  1493. (list org-time-stamp-rounding-minutes
  1494. org-time-stamp-rounding-minutes)))
  1495. (defcustom org-display-custom-times nil
  1496. "Non-nil means, overlay custom formats over all time stamps.
  1497. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1498. To turn this on on a per-file basis, insert anywhere in the file:
  1499. #+STARTUP: customtime"
  1500. :group 'org-time
  1501. :set 'set-default
  1502. :type 'sexp)
  1503. (make-variable-buffer-local 'org-display-custom-times)
  1504. (defcustom org-time-stamp-custom-formats
  1505. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1506. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1507. These are overlayed over the default ISO format if the variable
  1508. `org-display-custom-times' is set. Time like %H:%M should be at the
  1509. end of the second format."
  1510. :group 'org-time
  1511. :type 'sexp)
  1512. (defun org-time-stamp-format (&optional long inactive)
  1513. "Get the right format for a time string."
  1514. (let ((f (if long (cdr org-time-stamp-formats)
  1515. (car org-time-stamp-formats))))
  1516. (if inactive
  1517. (concat "[" (substring f 1 -1) "]")
  1518. f)))
  1519. (defcustom org-time-clocksum-format "%d:%02d"
  1520. "The format string used when creating CLOCKSUM lines, or when
  1521. org-mode generates a time duration."
  1522. :group 'org-time
  1523. :type 'string)
  1524. (defcustom org-deadline-warning-days 14
  1525. "No. of days before expiration during which a deadline becomes active.
  1526. This variable governs the display in sparse trees and in the agenda.
  1527. When 0 or negative, it means use this number (the absolute value of it)
  1528. even if a deadline has a different individual lead time specified."
  1529. :group 'org-time
  1530. :group 'org-agenda-daily/weekly
  1531. :type 'number)
  1532. (defcustom org-read-date-prefer-future t
  1533. "Non-nil means, assume future for incomplete date input from user.
  1534. This affects the following situations:
  1535. 1. The user gives a day, but no month.
  1536. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1537. read this as the third of *next* month. However, if you enter \"17\",
  1538. it will be considered as *this* month.
  1539. 2. The user gives a month but not a year.
  1540. For example, if it is april and you enter \"feb 2\", this will be read
  1541. as feb 2, *next* year. \"May 5\", however, will be this year.
  1542. Currently this does not work for ISO week specifications.
  1543. When this option is nil, the current month and year will always be used
  1544. as defaults."
  1545. :group 'org-time
  1546. :type 'boolean)
  1547. (defcustom org-read-date-display-live t
  1548. "Non-nil means, display current interpretation of date prompt live.
  1549. This display will be in an overlay, in the minibuffer."
  1550. :group 'org-time
  1551. :type 'boolean)
  1552. (defcustom org-read-date-popup-calendar t
  1553. "Non-nil means, pop up a calendar when prompting for a date.
  1554. In the calendar, the date can be selected with mouse-1. However, the
  1555. minibuffer will also be active, and you can simply enter the date as well.
  1556. When nil, only the minibuffer will be available."
  1557. :group 'org-time
  1558. :type 'boolean)
  1559. (if (fboundp 'defvaralias)
  1560. (defvaralias 'org-popup-calendar-for-date-prompt
  1561. 'org-read-date-popup-calendar))
  1562. (defcustom org-extend-today-until 0
  1563. "The hour when your day really ends. Must be an integer.
  1564. This has influence for the following applications:
  1565. - When switching the agenda to \"today\". It it is still earlier than
  1566. the time given here, the day recognized as TODAY is actually yesterday.
  1567. - When a date is read from the user and it is still before the time given
  1568. here, the current date and time will be assumed to be yesterday, 23:59.
  1569. Also, timestamps inserted in remember templates follow this rule.
  1570. IMPORTANT: This is a feature whose implementation is and likely will
  1571. remain incomplete. Really, it is only here because past midnight seems to
  1572. be the favorite working time of John Wiegley :-)"
  1573. :group 'org-time
  1574. :type 'number)
  1575. (defcustom org-edit-timestamp-down-means-later nil
  1576. "Non-nil means, S-down will increase the time in a time stamp.
  1577. When nil, S-up will increase."
  1578. :group 'org-time
  1579. :type 'boolean)
  1580. (defcustom org-calendar-follow-timestamp-change t
  1581. "Non-nil means, make the calendar window follow timestamp changes.
  1582. When a timestamp is modified and the calendar window is visible, it will be
  1583. moved to the new date."
  1584. :group 'org-time
  1585. :type 'boolean)
  1586. (defgroup org-tags nil
  1587. "Options concerning tags in Org-mode."
  1588. :tag "Org Tags"
  1589. :group 'org)
  1590. (defcustom org-tag-alist nil
  1591. "List of tags allowed in Org-mode files.
  1592. When this list is nil, Org-mode will base TAG input on what is already in the
  1593. buffer.
  1594. The value of this variable is an alist, the car of each entry must be a
  1595. keyword as a string, the cdr may be a character that is used to select
  1596. that tag through the fast-tag-selection interface.
  1597. See the manual for details."
  1598. :group 'org-tags
  1599. :type '(repeat
  1600. (choice
  1601. (cons (string :tag "Tag name")
  1602. (character :tag "Access char"))
  1603. (const :tag "Start radio group" (:startgroup))
  1604. (const :tag "End radio group" (:endgroup)))))
  1605. (defvar org-file-tags nil
  1606. "List of tags that can be inherited by all entries in the file.
  1607. The tags will be inherited if the variable `org-use-tag-inheritance'
  1608. says they should be.
  1609. This variable is populated from #+TAG lines.")
  1610. (defcustom org-use-fast-tag-selection 'auto
  1611. "Non-nil means, use fast tag selection scheme.
  1612. This is a special interface to select and deselect tags with single keys.
  1613. When nil, fast selection is never used.
  1614. When the symbol `auto', fast selection is used if and only if selection
  1615. characters for tags have been configured, either through the variable
  1616. `org-tag-alist' or through a #+TAGS line in the buffer.
  1617. When t, fast selection is always used and selection keys are assigned
  1618. automatically if necessary."
  1619. :group 'org-tags
  1620. :type '(choice
  1621. (const :tag "Always" t)
  1622. (const :tag "Never" nil)
  1623. (const :tag "When selection characters are configured" 'auto)))
  1624. (defcustom org-fast-tag-selection-single-key nil
  1625. "Non-nil means, fast tag selection exits after first change.
  1626. When nil, you have to press RET to exit it.
  1627. During fast tag selection, you can toggle this flag with `C-c'.
  1628. This variable can also have the value `expert'. In this case, the window
  1629. displaying the tags menu is not even shown, until you press C-c again."
  1630. :group 'org-tags
  1631. :type '(choice
  1632. (const :tag "No" nil)
  1633. (const :tag "Yes" t)
  1634. (const :tag "Expert" expert)))
  1635. (defvar org-fast-tag-selection-include-todo nil
  1636. "Non-nil means, fast tags selection interface will also offer TODO states.
  1637. This is an undocumented feature, you should not rely on it.")
  1638. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1639. "The column to which tags should be indented in a headline.
  1640. If this number is positive, it specifies the column. If it is negative,
  1641. it means that the tags should be flushright to that column. For example,
  1642. -80 works well for a normal 80 character screen."
  1643. :group 'org-tags
  1644. :type 'integer)
  1645. (defcustom org-auto-align-tags t
  1646. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1647. These operations change the length of a headline and therefore shift
  1648. the tags around. With this options turned on, after each such operation
  1649. the tags are again aligned to `org-tags-column'."
  1650. :group 'org-tags
  1651. :type 'boolean)
  1652. (defcustom org-use-tag-inheritance t
  1653. "Non-nil means, tags in levels apply also for sublevels.
  1654. When nil, only the tags directly given in a specific line apply there.
  1655. If this option is t, a match early-on in a tree can lead to a large
  1656. number of matches in the subtree. If you only want to see the first
  1657. match in a tree during a search, check out the variable
  1658. `org-tags-match-list-sublevels'.
  1659. This may also be a list of tags that should be inherited, or a regexp that
  1660. matches tags that should be inherited."
  1661. :group 'org-tags
  1662. :type '(choice
  1663. (const :tag "Not" nil)
  1664. (const :tag "Always" t)
  1665. (repeat :tag "Specific tags" (string :tag "Tag"))
  1666. (regexp :tag "Tags matched by regexp")))
  1667. (defun org-tag-inherit-p (tag)
  1668. "Check if TAG is one that should be inherited."
  1669. (cond
  1670. ((eq org-use-tag-inheritance t) t)
  1671. ((not org-use-tag-inheritance) nil)
  1672. ((stringp org-use-tag-inheritance)
  1673. (string-match org-use-tag-inheritance tag))
  1674. ((listp org-use-tag-inheritance)
  1675. (member tag org-use-tag-inheritance))
  1676. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1677. (defcustom org-tags-match-list-sublevels t
  1678. "Non-nil means list also sublevels of headlines matching tag search.
  1679. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1680. the sublevels of a headline matching a tag search often also match
  1681. the same search. Listing all of them can create very long lists.
  1682. Setting this variable to nil causes subtrees of a match to be skipped.
  1683. This option is off by default, because inheritance in on. If you turn
  1684. inheritance off, you very likely want to turn this option on.
  1685. As a special case, if the tag search is restricted to TODO items, the
  1686. value of this variable is ignored and sublevels are always checked, to
  1687. make sure all corresponding TODO items find their way into the list."
  1688. :group 'org-tags
  1689. :type 'boolean)
  1690. (defvar org-tags-history nil
  1691. "History of minibuffer reads for tags.")
  1692. (defvar org-last-tags-completion-table nil
  1693. "The last used completion table for tags.")
  1694. (defvar org-after-tags-change-hook nil
  1695. "Hook that is run after the tags in a line have changed.")
  1696. (defgroup org-properties nil
  1697. "Options concerning properties in Org-mode."
  1698. :tag "Org Properties"
  1699. :group 'org)
  1700. (defcustom org-property-format "%-10s %s"
  1701. "How property key/value pairs should be formatted by `indent-line'.
  1702. When `indent-line' hits a property definition, it will format the line
  1703. according to this format, mainly to make sure that the values are
  1704. lined-up with respect to each other."
  1705. :group 'org-properties
  1706. :type 'string)
  1707. (defcustom org-use-property-inheritance nil
  1708. "Non-nil means, properties apply also for sublevels.
  1709. This setting is chiefly used during property searches. Turning it on can
  1710. cause significant overhead when doing a search, which is why it is not
  1711. on by default.
  1712. When nil, only the properties directly given in the current entry count.
  1713. When t, every property is inherited. The value may also be a list of
  1714. properties that should have inheritance, or a regular expression matching
  1715. properties that should be inherited.
  1716. However, note that some special properties use inheritance under special
  1717. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1718. and the properties ending in \"_ALL\" when they are used as descriptor
  1719. for valid values of a property.
  1720. Note for programmers:
  1721. When querying an entry with `org-entry-get', you can control if inheritance
  1722. should be used. By default, `org-entry-get' looks only at the local
  1723. properties. You can request inheritance by setting the inherit argument
  1724. to t (to force inheritance) or to `selective' (to respect the setting
  1725. in this variable)."
  1726. :group 'org-properties
  1727. :type '(choice
  1728. (const :tag "Not" nil)
  1729. (const :tag "Always" t)
  1730. (repeat :tag "Specific properties" (string :tag "Property"))
  1731. (regexp :tag "Properties matched by regexp")))
  1732. (defun org-property-inherit-p (property)
  1733. "Check if PROPERTY is one that should be inherited."
  1734. (cond
  1735. ((eq org-use-property-inheritance t) t)
  1736. ((not org-use-property-inheritance) nil)
  1737. ((stringp org-use-property-inheritance)
  1738. (string-match org-use-property-inheritance property))
  1739. ((listp org-use-property-inheritance)
  1740. (member property org-use-property-inheritance))
  1741. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1742. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1743. "The default column format, if no other format has been defined.
  1744. This variable can be set on the per-file basis by inserting a line
  1745. #+COLUMNS: %25ITEM ....."
  1746. :group 'org-properties
  1747. :type 'string)
  1748. (defcustom org-columns-ellipses ".."
  1749. "The ellipses to be used when a field in column view is truncated.
  1750. When this is the empty string, as many characters as possible are shown,
  1751. but then there will be no visual indication that the field has been truncated.
  1752. When this is a string of length N, the last N characters of a truncated
  1753. field are replaced by this string. If the column is narrower than the
  1754. ellipses string, only part of the ellipses string will be shown."
  1755. :group 'org-properties
  1756. :type 'string)
  1757. (defcustom org-columns-modify-value-for-display-function nil
  1758. "Function that modifies values for display in column view.
  1759. For example, it can be used to cut out a certain part from a time stamp.
  1760. The function must take 2 arguments:
  1761. column-title The tite of the column (*not* the property name)
  1762. value The value that should be modified.
  1763. The function should return the value that should be displayed,
  1764. or nil if the normal value should be used."
  1765. :group 'org-properties
  1766. :type 'function)
  1767. (defcustom org-effort-property "Effort"
  1768. "The property that is being used to keep track of effort estimates.
  1769. Effort estimates given in this property need to have the format H:MM."
  1770. :group 'org-properties
  1771. :group 'org-progress
  1772. :type '(string :tag "Property"))
  1773. (defconst org-global-properties-fixed
  1774. '(("VISIBILITY_ALL" . "folded children content all"))
  1775. "List of property/value pairs that can be inherited by any entry.
  1776. These are fixed values, for the preset properties.")
  1777. (defcustom org-global-properties nil
  1778. "List of property/value pairs that can be inherited by any entry.
  1779. You can set buffer-local values for the same purpose in the variable
  1780. `org-file-properties' this by adding lines like
  1781. #+PROPERTY: NAME VALUE"
  1782. :group 'org-properties
  1783. :type '(repeat
  1784. (cons (string :tag "Property")
  1785. (string :tag "Value"))))
  1786. (defvar org-file-properties nil
  1787. "List of property/value pairs that can be inherited by any entry.
  1788. Valid for the current buffer.
  1789. This variable is populated from #+PROPERTY lines.")
  1790. (make-variable-buffer-local 'org-file-properties)
  1791. (defgroup org-agenda nil
  1792. "Options concerning agenda views in Org-mode."
  1793. :tag "Org Agenda"
  1794. :group 'org)
  1795. (defvar org-category nil
  1796. "Variable used by org files to set a category for agenda display.
  1797. Such files should use a file variable to set it, for example
  1798. # -*- mode: org; org-category: \"ELisp\"
  1799. or contain a special line
  1800. #+CATEGORY: ELisp
  1801. If the file does not specify a category, then file's base name
  1802. is used instead.")
  1803. (make-variable-buffer-local 'org-category)
  1804. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1805. (defcustom org-agenda-files nil
  1806. "The files to be used for agenda display.
  1807. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1808. \\[org-remove-file]. You can also use customize to edit the list.
  1809. If an entry is a directory, all files in that directory that are matched by
  1810. `org-agenda-file-regexp' will be part of the file list.
  1811. If the value of the variable is not a list but a single file name, then
  1812. the list of agenda files is actually stored and maintained in that file, one
  1813. agenda file per line."
  1814. :group 'org-agenda
  1815. :type '(choice
  1816. (repeat :tag "List of files and directories" file)
  1817. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1818. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1819. "Regular expression to match files for `org-agenda-files'.
  1820. If any element in the list in that variable contains a directory instead
  1821. of a normal file, all files in that directory that are matched by this
  1822. regular expression will be included."
  1823. :group 'org-agenda
  1824. :type 'regexp)
  1825. (defcustom org-agenda-text-search-extra-files nil
  1826. "List of extra files to be searched by text search commands.
  1827. These files will be search in addition to the agenda files by the
  1828. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1829. Note that these files will only be searched for text search commands,
  1830. not for the other agenda views like todo lists, tag searches or the weekly
  1831. agenda. This variable is intended to list notes and possibly archive files
  1832. that should also be searched by these two commands.
  1833. In fact, if the first element in the list is the symbol `agenda-archives',
  1834. than all archive files of all agenda files will be added to the search
  1835. scope."
  1836. :group 'org-agenda
  1837. :type '(set :greedy t
  1838. (const :tag "Agenda Archives" agenda-archives)
  1839. (repeat :inline t (file))))
  1840. (if (fboundp 'defvaralias)
  1841. (defvaralias 'org-agenda-multi-occur-extra-files
  1842. 'org-agenda-text-search-extra-files))
  1843. (defcustom org-agenda-skip-unavailable-files nil
  1844. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1845. A nil value means to remove them, after a query, from the list."
  1846. :group 'org-agenda
  1847. :type 'boolean)
  1848. (defcustom org-calendar-to-agenda-key [?c]
  1849. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1850. The command `org-calendar-goto-agenda' will be bound to this key. The
  1851. default is the character `c' because then `c' can be used to switch back and
  1852. forth between agenda and calendar."
  1853. :group 'org-agenda
  1854. :type 'sexp)
  1855. (defcustom org-calendar-agenda-action-key [?k]
  1856. "The key to be installed in `calendar-mode-map' for agenda-action.
  1857. The command `org-agenda-action' will be bound to this key. The
  1858. default is the character `k' because we use the same key in the agenda."
  1859. :group 'org-agenda
  1860. :type 'sexp)
  1861. (eval-after-load "calendar"
  1862. '(progn
  1863. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1864. 'org-calendar-goto-agenda)
  1865. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1866. 'org-agenda-action)))
  1867. (defgroup org-latex nil
  1868. "Options for embedding LaTeX code into Org-mode."
  1869. :tag "Org LaTeX"
  1870. :group 'org)
  1871. (defcustom org-format-latex-options
  1872. '(:foreground default :background default :scale 1.0
  1873. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1874. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1875. "Options for creating images from LaTeX fragments.
  1876. This is a property list with the following properties:
  1877. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1878. `default' means use the foreground of the default face.
  1879. :background the background color, or \"Transparent\".
  1880. `default' means use the background of the default face.
  1881. :scale a scaling factor for the size of the images.
  1882. :html-foreground, :html-background, :html-scale
  1883. the same numbers for HTML export.
  1884. :matchers a list indicating which matchers should be used to
  1885. find LaTeX fragments. Valid members of this list are:
  1886. \"begin\" find environments
  1887. \"$\" find math expressions surrounded by $...$
  1888. \"$$\" find math expressions surrounded by $$....$$
  1889. \"\\(\" find math expressions surrounded by \\(...\\)
  1890. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1891. :group 'org-latex
  1892. :type 'plist)
  1893. (defcustom org-format-latex-header "\\documentclass{article}
  1894. \\usepackage{fullpage} % do not remove
  1895. \\usepackage{amssymb}
  1896. \\usepackage[usenames]{color}
  1897. \\usepackage{amsmath}
  1898. \\usepackage{latexsym}
  1899. \\usepackage[mathscr]{eucal}
  1900. \\pagestyle{empty} % do not remove"
  1901. "The document header used for processing LaTeX fragments."
  1902. :group 'org-latex
  1903. :type 'string)
  1904. (defgroup org-font-lock nil
  1905. "Font-lock settings for highlighting in Org-mode."
  1906. :tag "Org Font Lock"
  1907. :group 'org)
  1908. (defcustom org-level-color-stars-only nil
  1909. "Non-nil means fontify only the stars in each headline.
  1910. When nil, the entire headline is fontified.
  1911. Changing it requires restart of `font-lock-mode' to become effective
  1912. also in regions already fontified."
  1913. :group 'org-font-lock
  1914. :type 'boolean)
  1915. (defcustom org-hide-leading-stars nil
  1916. "Non-nil means, hide the first N-1 stars in a headline.
  1917. This works by using the face `org-hide' for these stars. This
  1918. face is white for a light background, and black for a dark
  1919. background. You may have to customize the face `org-hide' to
  1920. make this work.
  1921. Changing it requires restart of `font-lock-mode' to become effective
  1922. also in regions already fontified.
  1923. You may also set this on a per-file basis by adding one of the following
  1924. lines to the buffer:
  1925. #+STARTUP: hidestars
  1926. #+STARTUP: showstars"
  1927. :group 'org-font-lock
  1928. :type 'boolean)
  1929. (defcustom org-fontify-done-headline nil
  1930. "Non-nil means, change the face of a headline if it is marked DONE.
  1931. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1932. When this is non-nil, the headline after the keyword is set to the
  1933. `org-headline-done' as an additional indication."
  1934. :group 'org-font-lock
  1935. :type 'boolean)
  1936. (defcustom org-fontify-emphasized-text t
  1937. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1938. Changing this variable requires a restart of Emacs to take effect."
  1939. :group 'org-font-lock
  1940. :type 'boolean)
  1941. (defcustom org-highlight-latex-fragments-and-specials nil
  1942. "Non-nil means, fontify what is treated specially by the exporters."
  1943. :group 'org-font-lock
  1944. :type 'boolean)
  1945. (defcustom org-hide-emphasis-markers nil
  1946. "Non-nil mean font-lock should hide the emphasis marker characters."
  1947. :group 'org-font-lock
  1948. :type 'boolean)
  1949. (defvar org-emph-re nil
  1950. "Regular expression for matching emphasis.")
  1951. (defvar org-verbatim-re nil
  1952. "Regular expression for matching verbatim text.")
  1953. (defvar org-emphasis-regexp-components) ; defined just below
  1954. (defvar org-emphasis-alist) ; defined just below
  1955. (defun org-set-emph-re (var val)
  1956. "Set variable and compute the emphasis regular expression."
  1957. (set var val)
  1958. (when (and (boundp 'org-emphasis-alist)
  1959. (boundp 'org-emphasis-regexp-components)
  1960. org-emphasis-alist org-emphasis-regexp-components)
  1961. (let* ((e org-emphasis-regexp-components)
  1962. (pre (car e))
  1963. (post (nth 1 e))
  1964. (border (nth 2 e))
  1965. (body (nth 3 e))
  1966. (nl (nth 4 e))
  1967. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1968. (body1 (concat body "*?"))
  1969. (markers (mapconcat 'car org-emphasis-alist ""))
  1970. (vmarkers (mapconcat
  1971. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1972. org-emphasis-alist "")))
  1973. ;; make sure special characters appear at the right position in the class
  1974. (if (string-match "\\^" markers)
  1975. (setq markers (concat (replace-match "" t t markers) "^")))
  1976. (if (string-match "-" markers)
  1977. (setq markers (concat (replace-match "" t t markers) "-")))
  1978. (if (string-match "\\^" vmarkers)
  1979. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1980. (if (string-match "-" vmarkers)
  1981. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1982. (if (> nl 0)
  1983. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1984. (int-to-string nl) "\\}")))
  1985. ;; Make the regexp
  1986. (setq org-emph-re
  1987. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1988. "\\("
  1989. "\\([" markers "]\\)"
  1990. "\\("
  1991. "[^" border "]\\|"
  1992. "[^" border (if (and nil stacked) markers) "]"
  1993. body1
  1994. "[^" border (if (and nil stacked) markers) "]"
  1995. "\\)"
  1996. "\\3\\)"
  1997. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1998. (setq org-verbatim-re
  1999. (concat "\\([" pre "]\\|^\\)"
  2000. "\\("
  2001. "\\([" vmarkers "]\\)"
  2002. "\\("
  2003. "[^" border "]\\|"
  2004. "[^" border "]"
  2005. body1
  2006. "[^" border "]"
  2007. "\\)"
  2008. "\\3\\)"
  2009. "\\([" post "]\\|$\\)")))))
  2010. (defcustom org-emphasis-regexp-components
  2011. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2012. "Components used to build the regular expression for emphasis.
  2013. This is a list with 6 entries. Terminology: In an emphasis string
  2014. like \" *strong word* \", we call the initial space PREMATCH, the final
  2015. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2016. and \"trong wor\" is the body. The different components in this variable
  2017. specify what is allowed/forbidden in each part:
  2018. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2019. post Chars allowed as postmatch. End of line will be allowed too.
  2020. border The chars *forbidden* as border characters.
  2021. body-regexp A regexp like \".\" to match a body character. Don't use
  2022. non-shy groups here, and don't allow newline here.
  2023. newline The maximum number of newlines allowed in an emphasis exp.
  2024. Use customize to modify this, or restart Emacs after changing it."
  2025. :group 'org-font-lock
  2026. :set 'org-set-emph-re
  2027. :type '(list
  2028. (sexp :tag "Allowed chars in pre ")
  2029. (sexp :tag "Allowed chars in post ")
  2030. (sexp :tag "Forbidden chars in border ")
  2031. (sexp :tag "Regexp for body ")
  2032. (integer :tag "number of newlines allowed")
  2033. (option (boolean :tag "Please ignore this button"))))
  2034. (defcustom org-emphasis-alist
  2035. `(("*" bold "<b>" "</b>")
  2036. ("/" italic "<i>" "</i>")
  2037. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2038. ("=" org-code "<code>" "</code>" verbatim)
  2039. ("~" org-verbatim "<code>" "</code>" verbatim)
  2040. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2041. "<del>" "</del>")
  2042. )
  2043. "Special syntax for emphasized text.
  2044. Text starting and ending with a special character will be emphasized, for
  2045. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2046. characters, the face to be used by font-lock for highlighting in Org-mode
  2047. Emacs buffers, and the HTML tags to be used for this.
  2048. Use customize to modify this, or restart Emacs after changing it."
  2049. :group 'org-font-lock
  2050. :set 'org-set-emph-re
  2051. :type '(repeat
  2052. (list
  2053. (string :tag "Marker character")
  2054. (choice
  2055. (face :tag "Font-lock-face")
  2056. (plist :tag "Face property list"))
  2057. (string :tag "HTML start tag")
  2058. (string :tag "HTML end tag")
  2059. (option (const verbatim)))))
  2060. ;;; Miscellaneous options
  2061. (defgroup org-completion nil
  2062. "Completion in Org-mode."
  2063. :tag "Org Completion"
  2064. :group 'org)
  2065. (defcustom org-completion-use-ido nil
  2066. "Non-ni means, use ido completion wherever possible."
  2067. :group 'org-completion
  2068. :type 'boolean)
  2069. (defcustom org-completion-fallback-command 'hippie-expand
  2070. "The expansion command called by \\[org-complete] in normal context.
  2071. Normal means, no org-mode-specific context."
  2072. :group 'org-completion
  2073. :type 'function)
  2074. ;;; Functions and variables from ther packages
  2075. ;; Declared here to avoid compiler warnings
  2076. ;; XEmacs only
  2077. (defvar outline-mode-menu-heading)
  2078. (defvar outline-mode-menu-show)
  2079. (defvar outline-mode-menu-hide)
  2080. (defvar zmacs-regions) ; XEmacs regions
  2081. ;; Emacs only
  2082. (defvar mark-active)
  2083. ;; Various packages
  2084. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2085. (declare-function calendar-forward-day "cal-move" (arg))
  2086. (declare-function calendar-goto-date "cal-move" (date))
  2087. (declare-function calendar-goto-today "cal-move" ())
  2088. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2089. (defvar calc-embedded-close-formula)
  2090. (defvar calc-embedded-open-formula)
  2091. (declare-function cdlatex-tab "ext:cdlatex" ())
  2092. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2093. (defvar font-lock-unfontify-region-function)
  2094. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2095. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2096. (defvar iswitchb-temp-buflist)
  2097. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2098. (declare-function org-agenda-skip "org-agenda" ())
  2099. (declare-function org-format-agenda-item "org-agenda"
  2100. (extra txt &optional category tags dotime noprefix remove-re))
  2101. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2102. (declare-function org-agenda-change-all-lines "org-agenda"
  2103. (newhead hdmarker &optional fixface))
  2104. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2105. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2106. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2107. (beg end))
  2108. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2109. (declare-function parse-time-string "parse-time" (string))
  2110. (declare-function remember "remember" (&optional initial))
  2111. (declare-function remember-buffer-desc "remember" ())
  2112. (declare-function remember-finalize "remember" ())
  2113. (defvar remember-save-after-remembering)
  2114. (defvar remember-data-file)
  2115. (defvar remember-register)
  2116. (defvar remember-buffer)
  2117. (defvar remember-handler-functions)
  2118. (defvar remember-annotation-functions)
  2119. (defvar texmathp-why)
  2120. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2121. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2122. (defvar w3m-current-url)
  2123. (defvar w3m-current-title)
  2124. (defvar org-latex-regexps)
  2125. ;;; Autoload and prepare some org modules
  2126. ;; Some table stuff that needs to be defined here, because it is used
  2127. ;; by the functions setting up org-mode or checking for table context.
  2128. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2129. "Detects an org-type or table-type table.")
  2130. (defconst org-table-line-regexp "^[ \t]*|"
  2131. "Detects an org-type table line.")
  2132. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2133. "Detects an org-type table line.")
  2134. (defconst org-table-hline-regexp "^[ \t]*|-"
  2135. "Detects an org-type table hline.")
  2136. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2137. "Detects a table-type table hline.")
  2138. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2139. "Searching from within a table (any type) this finds the first line
  2140. outside the table.")
  2141. ;; Autoload the functions in org-table.el that are needed by functions here.
  2142. (eval-and-compile
  2143. (org-autoload "org-table"
  2144. '(org-table-align org-table-begin org-table-blank-field
  2145. org-table-convert org-table-convert-region org-table-copy-down
  2146. org-table-copy-region org-table-create
  2147. org-table-create-or-convert-from-region
  2148. org-table-create-with-table.el org-table-current-dline
  2149. org-table-cut-region org-table-delete-column org-table-edit-field
  2150. org-table-edit-formulas org-table-end org-table-eval-formula
  2151. org-table-export org-table-field-info
  2152. org-table-get-stored-formulas org-table-goto-column
  2153. org-table-hline-and-move org-table-import org-table-insert-column
  2154. org-table-insert-hline org-table-insert-row org-table-iterate
  2155. org-table-justify-field-maybe org-table-kill-row
  2156. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2157. org-table-move-column org-table-move-column-left
  2158. org-table-move-column-right org-table-move-row
  2159. org-table-move-row-down org-table-move-row-up
  2160. org-table-next-field org-table-next-row org-table-paste-rectangle
  2161. org-table-previous-field org-table-recalculate
  2162. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2163. org-table-toggle-coordinate-overlays
  2164. org-table-toggle-formula-debugger org-table-wrap-region
  2165. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2166. (defun org-at-table-p (&optional table-type)
  2167. "Return t if the cursor is inside an org-type table.
  2168. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2169. (if org-enable-table-editor
  2170. (save-excursion
  2171. (beginning-of-line 1)
  2172. (looking-at (if table-type org-table-any-line-regexp
  2173. org-table-line-regexp)))
  2174. nil))
  2175. (defsubst org-table-p () (org-at-table-p))
  2176. (defun org-at-table.el-p ()
  2177. "Return t if and only if we are at a table.el table."
  2178. (and (org-at-table-p 'any)
  2179. (save-excursion
  2180. (goto-char (org-table-begin 'any))
  2181. (looking-at org-table1-hline-regexp))))
  2182. (defun org-table-recognize-table.el ()
  2183. "If there is a table.el table nearby, recognize it and move into it."
  2184. (if org-table-tab-recognizes-table.el
  2185. (if (org-at-table.el-p)
  2186. (progn
  2187. (beginning-of-line 1)
  2188. (if (looking-at org-table-dataline-regexp)
  2189. nil
  2190. (if (looking-at org-table1-hline-regexp)
  2191. (progn
  2192. (beginning-of-line 2)
  2193. (if (looking-at org-table-any-border-regexp)
  2194. (beginning-of-line -1)))))
  2195. (if (re-search-forward "|" (org-table-end t) t)
  2196. (progn
  2197. (require 'table)
  2198. (if (table--at-cell-p (point))
  2199. t
  2200. (message "recognizing table.el table...")
  2201. (table-recognize-table)
  2202. (message "recognizing table.el table...done")))
  2203. (error "This should not happen..."))
  2204. t)
  2205. nil)
  2206. nil))
  2207. (defun org-at-table-hline-p ()
  2208. "Return t if the cursor is inside a hline in a table."
  2209. (if org-enable-table-editor
  2210. (save-excursion
  2211. (beginning-of-line 1)
  2212. (looking-at org-table-hline-regexp))
  2213. nil))
  2214. (defvar org-table-clean-did-remove-column nil)
  2215. (defun org-table-map-tables (function)
  2216. "Apply FUNCTION to the start of all tables in the buffer."
  2217. (save-excursion
  2218. (save-restriction
  2219. (widen)
  2220. (goto-char (point-min))
  2221. (while (re-search-forward org-table-any-line-regexp nil t)
  2222. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2223. (beginning-of-line 1)
  2224. (if (looking-at org-table-line-regexp)
  2225. (save-excursion (funcall function)))
  2226. (re-search-forward org-table-any-border-regexp nil 1))))
  2227. (message "Mapping tables: done"))
  2228. ;; Declare and autoload functions from org-exp.el
  2229. (declare-function org-default-export-plist "org-exp")
  2230. (declare-function org-infile-export-plist "org-exp")
  2231. (declare-function org-get-current-options "org-exp")
  2232. (eval-and-compile
  2233. (org-autoload "org-exp"
  2234. '(org-export org-export-as-ascii org-export-visible
  2235. org-insert-export-options-template org-export-as-html-and-open
  2236. org-export-as-html-batch org-export-as-html-to-buffer
  2237. org-replace-region-by-html org-export-region-as-html
  2238. org-export-as-html org-export-icalendar-this-file
  2239. org-export-icalendar-all-agenda-files
  2240. org-table-clean-before-export
  2241. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2242. ;; Declare and autoload functions from org-agenda.el
  2243. (eval-and-compile
  2244. (org-autoload "org-agenda"
  2245. '(org-agenda org-agenda-list org-search-view
  2246. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2247. org-diary org-agenda-to-appt)))
  2248. ;; Autoload org-remember
  2249. (eval-and-compile
  2250. (org-autoload "org-remember"
  2251. '(org-remember-insinuate org-remember-annotation
  2252. org-remember-apply-template org-remember org-remember-handler)))
  2253. ;; Autoload org-clock.el
  2254. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2255. (beg end))
  2256. (declare-function org-update-mode-line "org-clock" ())
  2257. (defvar org-clock-start-time)
  2258. (defvar org-clock-marker (make-marker)
  2259. "Marker recording the last clock-in.")
  2260. (eval-and-compile
  2261. (org-autoload
  2262. "org-clock"
  2263. '(org-clock-in org-clock-out org-clock-cancel
  2264. org-clock-goto org-clock-sum org-clock-display
  2265. org-remove-clock-overlays org-clock-report
  2266. org-clocktable-shift org-dblock-write:clocktable
  2267. org-get-clocktable)))
  2268. (defun org-clock-update-time-maybe ()
  2269. "If this is a CLOCK line, update it and return t.
  2270. Otherwise, return nil."
  2271. (interactive)
  2272. (save-excursion
  2273. (beginning-of-line 1)
  2274. (skip-chars-forward " \t")
  2275. (when (looking-at org-clock-string)
  2276. (let ((re (concat "[ \t]*" org-clock-string
  2277. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2278. "\\([ \t]*=>.*\\)?\\)?"))
  2279. ts te h m s neg)
  2280. (cond
  2281. ((not (looking-at re))
  2282. nil)
  2283. ((not (match-end 2))
  2284. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2285. (> org-clock-marker (point))
  2286. (<= org-clock-marker (point-at-eol)))
  2287. ;; The clock is running here
  2288. (setq org-clock-start-time
  2289. (apply 'encode-time
  2290. (org-parse-time-string (match-string 1))))
  2291. (org-update-mode-line)))
  2292. (t
  2293. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2294. (end-of-line 1)
  2295. (setq ts (match-string 1)
  2296. te (match-string 3))
  2297. (setq s (- (time-to-seconds
  2298. (apply 'encode-time (org-parse-time-string te)))
  2299. (time-to-seconds
  2300. (apply 'encode-time (org-parse-time-string ts))))
  2301. neg (< s 0)
  2302. s (abs s)
  2303. h (floor (/ s 3600))
  2304. s (- s (* 3600 h))
  2305. m (floor (/ s 60))
  2306. s (- s (* 60 s)))
  2307. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2308. t))))))
  2309. (defun org-check-running-clock ()
  2310. "Check if the current buffer contains the running clock.
  2311. If yes, offer to stop it and to save the buffer with the changes."
  2312. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2313. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2314. (buffer-name))))
  2315. (org-clock-out)
  2316. (when (y-or-n-p "Save changed buffer?")
  2317. (save-buffer))))
  2318. (defun org-clocktable-try-shift (dir n)
  2319. "Check if this line starts a clock table, if yes, shift the time block."
  2320. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2321. (org-clocktable-shift dir n)))
  2322. ;; Autoload archiving code
  2323. ;; The stuff that is needed for cycling and tags has to be defined here.
  2324. (defgroup org-archive nil
  2325. "Options concerning archiving in Org-mode."
  2326. :tag "Org Archive"
  2327. :group 'org-structure)
  2328. (defcustom org-archive-location "%s_archive::"
  2329. "The location where subtrees should be archived.
  2330. The value of this variable is a string, consisting of two parts,
  2331. separated by a double-colon. The first part is a filename and
  2332. the second part is a headline.
  2333. When the filename is omitted, archiving happens in the same file.
  2334. %s in the filename will be replaced by the current file
  2335. name (without the directory part). Archiving to a different file
  2336. is useful to keep archived entries from contributing to the
  2337. Org-mode Agenda.
  2338. The archived entries will be filed as subtrees of the specified
  2339. headline. When the headline is omitted, the subtrees are simply
  2340. filed away at the end of the file, as top-level entries.
  2341. Here are a few examples:
  2342. \"%s_archive::\"
  2343. If the current file is Projects.org, archive in file
  2344. Projects.org_archive, as top-level trees. This is the default.
  2345. \"::* Archived Tasks\"
  2346. Archive in the current file, under the top-level headline
  2347. \"* Archived Tasks\".
  2348. \"~/org/archive.org::\"
  2349. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2350. \"basement::** Finished Tasks\"
  2351. Archive in file ./basement (relative path), as level 3 trees
  2352. below the level 2 heading \"** Finished Tasks\".
  2353. You may set this option on a per-file basis by adding to the buffer a
  2354. line like
  2355. #+ARCHIVE: basement::** Finished Tasks
  2356. You may also define it locally for a subtree by setting an ARCHIVE property
  2357. in the entry. If such a property is found in an entry, or anywhere up
  2358. the hierarchy, it will be used."
  2359. :group 'org-archive
  2360. :type 'string)
  2361. (defcustom org-archive-tag "ARCHIVE"
  2362. "The tag that marks a subtree as archived.
  2363. An archived subtree does not open during visibility cycling, and does
  2364. not contribute to the agenda listings.
  2365. After changing this, font-lock must be restarted in the relevant buffers to
  2366. get the proper fontification."
  2367. :group 'org-archive
  2368. :group 'org-keywords
  2369. :type 'string)
  2370. (defcustom org-agenda-skip-archived-trees t
  2371. "Non-nil means, the agenda will skip any items located in archived trees.
  2372. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2373. variable is no longer recommended, you should leave it at the value t.
  2374. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2375. :group 'org-archive
  2376. :group 'org-agenda-skip
  2377. :type 'boolean)
  2378. (defcustom org-cycle-open-archived-trees nil
  2379. "Non-nil means, `org-cycle' will open archived trees.
  2380. An archived tree is a tree marked with the tag ARCHIVE.
  2381. When nil, archived trees will stay folded. You can still open them with
  2382. normal outline commands like `show-all', but not with the cycling commands."
  2383. :group 'org-archive
  2384. :group 'org-cycle
  2385. :type 'boolean)
  2386. (defcustom org-sparse-tree-open-archived-trees nil
  2387. "Non-nil means sparse tree construction shows matches in archived trees.
  2388. When nil, matches in these trees are highlighted, but the trees are kept in
  2389. collapsed state."
  2390. :group 'org-archive
  2391. :group 'org-sparse-trees
  2392. :type 'boolean)
  2393. (defun org-cycle-hide-archived-subtrees (state)
  2394. "Re-hide all archived subtrees after a visibility state change."
  2395. (when (and (not org-cycle-open-archived-trees)
  2396. (not (memq state '(overview folded))))
  2397. (save-excursion
  2398. (let* ((globalp (memq state '(contents all)))
  2399. (beg (if globalp (point-min) (point)))
  2400. (end (if globalp (point-max) (org-end-of-subtree t))))
  2401. (org-hide-archived-subtrees beg end)
  2402. (goto-char beg)
  2403. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2404. (message "%s" (substitute-command-keys
  2405. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2406. (defun org-force-cycle-archived ()
  2407. "Cycle subtree even if it is archived."
  2408. (interactive)
  2409. (setq this-command 'org-cycle)
  2410. (let ((org-cycle-open-archived-trees t))
  2411. (call-interactively 'org-cycle)))
  2412. (defun org-hide-archived-subtrees (beg end)
  2413. "Re-hide all archived subtrees after a visibility state change."
  2414. (save-excursion
  2415. (let* ((re (concat ":" org-archive-tag ":")))
  2416. (goto-char beg)
  2417. (while (re-search-forward re end t)
  2418. (and (org-on-heading-p) (hide-subtree))
  2419. (org-end-of-subtree t)))))
  2420. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2421. (eval-and-compile
  2422. (org-autoload "org-archive"
  2423. '(org-add-archive-files org-archive-subtree
  2424. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2425. ;; Autoload Column View Code
  2426. (declare-function org-columns-number-to-string "org-colview")
  2427. (declare-function org-columns-get-format-and-top-level "org-colview")
  2428. (declare-function org-columns-compute "org-colview")
  2429. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2430. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2431. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2432. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2433. ;; Autoload ID code
  2434. (org-autoload "org-id"
  2435. '(org-id-get-create org-id-new org-id-copy org-id-get
  2436. org-id-get-with-outline-path-completion
  2437. org-id-get-with-outline-drilling
  2438. org-id-goto org-id-find))
  2439. ;;; Variables for pre-computed regular expressions, all buffer local
  2440. (defvar org-drawer-regexp nil
  2441. "Matches first line of a hidden block.")
  2442. (make-variable-buffer-local 'org-drawer-regexp)
  2443. (defvar org-todo-regexp nil
  2444. "Matches any of the TODO state keywords.")
  2445. (make-variable-buffer-local 'org-todo-regexp)
  2446. (defvar org-not-done-regexp nil
  2447. "Matches any of the TODO state keywords except the last one.")
  2448. (make-variable-buffer-local 'org-not-done-regexp)
  2449. (defvar org-todo-line-regexp nil
  2450. "Matches a headline and puts TODO state into group 2 if present.")
  2451. (make-variable-buffer-local 'org-todo-line-regexp)
  2452. (defvar org-complex-heading-regexp nil
  2453. "Matches a headline and puts everything into groups:
  2454. group 1: the stars
  2455. group 2: The todo keyword, maybe
  2456. group 3: Priority cookie
  2457. group 4: True headline
  2458. group 5: Tags")
  2459. (make-variable-buffer-local 'org-complex-heading-regexp)
  2460. (defvar org-todo-line-tags-regexp nil
  2461. "Matches a headline and puts TODO state into group 2 if present.
  2462. Also put tags into group 4 if tags are present.")
  2463. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2464. (defvar org-nl-done-regexp nil
  2465. "Matches newline followed by a headline with the DONE keyword.")
  2466. (make-variable-buffer-local 'org-nl-done-regexp)
  2467. (defvar org-looking-at-done-regexp nil
  2468. "Matches the DONE keyword a point.")
  2469. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2470. (defvar org-ds-keyword-length 12
  2471. "Maximum length of the Deadline and SCHEDULED keywords.")
  2472. (make-variable-buffer-local 'org-ds-keyword-length)
  2473. (defvar org-deadline-regexp nil
  2474. "Matches the DEADLINE keyword.")
  2475. (make-variable-buffer-local 'org-deadline-regexp)
  2476. (defvar org-deadline-time-regexp nil
  2477. "Matches the DEADLINE keyword together with a time stamp.")
  2478. (make-variable-buffer-local 'org-deadline-time-regexp)
  2479. (defvar org-deadline-line-regexp nil
  2480. "Matches the DEADLINE keyword and the rest of the line.")
  2481. (make-variable-buffer-local 'org-deadline-line-regexp)
  2482. (defvar org-scheduled-regexp nil
  2483. "Matches the SCHEDULED keyword.")
  2484. (make-variable-buffer-local 'org-scheduled-regexp)
  2485. (defvar org-scheduled-time-regexp nil
  2486. "Matches the SCHEDULED keyword together with a time stamp.")
  2487. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2488. (defvar org-closed-time-regexp nil
  2489. "Matches the CLOSED keyword together with a time stamp.")
  2490. (make-variable-buffer-local 'org-closed-time-regexp)
  2491. (defvar org-keyword-time-regexp nil
  2492. "Matches any of the 4 keywords, together with the time stamp.")
  2493. (make-variable-buffer-local 'org-keyword-time-regexp)
  2494. (defvar org-keyword-time-not-clock-regexp nil
  2495. "Matches any of the 3 keywords, together with the time stamp.")
  2496. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2497. (defvar org-maybe-keyword-time-regexp nil
  2498. "Matches a timestamp, possibly preceeded by a keyword.")
  2499. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2500. (defvar org-planning-or-clock-line-re nil
  2501. "Matches a line with planning or clock info.")
  2502. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2503. (defconst org-plain-time-of-day-regexp
  2504. (concat
  2505. "\\(\\<[012]?[0-9]"
  2506. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2507. "\\(--?"
  2508. "\\(\\<[012]?[0-9]"
  2509. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2510. "\\)?")
  2511. "Regular expression to match a plain time or time range.
  2512. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2513. groups carry important information:
  2514. 0 the full match
  2515. 1 the first time, range or not
  2516. 8 the second time, if it is a range.")
  2517. (defconst org-plain-time-extension-regexp
  2518. (concat
  2519. "\\(\\<[012]?[0-9]"
  2520. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2521. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2522. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2523. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2524. groups carry important information:
  2525. 0 the full match
  2526. 7 hours of duration
  2527. 9 minutes of duration")
  2528. (defconst org-stamp-time-of-day-regexp
  2529. (concat
  2530. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2531. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2532. "\\(--?"
  2533. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2534. "Regular expression to match a timestamp time or time range.
  2535. After a match, the following groups carry important information:
  2536. 0 the full match
  2537. 1 date plus weekday, for backreferencing to make sure both times on same day
  2538. 2 the first time, range or not
  2539. 4 the second time, if it is a range.")
  2540. (defconst org-startup-options
  2541. '(("fold" org-startup-folded t)
  2542. ("overview" org-startup-folded t)
  2543. ("nofold" org-startup-folded nil)
  2544. ("showall" org-startup-folded nil)
  2545. ("content" org-startup-folded content)
  2546. ("hidestars" org-hide-leading-stars t)
  2547. ("showstars" org-hide-leading-stars nil)
  2548. ("odd" org-odd-levels-only t)
  2549. ("oddeven" org-odd-levels-only nil)
  2550. ("align" org-startup-align-all-tables t)
  2551. ("noalign" org-startup-align-all-tables nil)
  2552. ("customtime" org-display-custom-times t)
  2553. ("logdone" org-log-done time)
  2554. ("lognotedone" org-log-done note)
  2555. ("nologdone" org-log-done nil)
  2556. ("lognoteclock-out" org-log-note-clock-out t)
  2557. ("nolognoteclock-out" org-log-note-clock-out nil)
  2558. ("logrepeat" org-log-repeat state)
  2559. ("lognoterepeat" org-log-repeat note)
  2560. ("nologrepeat" org-log-repeat nil)
  2561. ("constcgs" constants-unit-system cgs)
  2562. ("constSI" constants-unit-system SI))
  2563. "Variable associated with STARTUP options for org-mode.
  2564. Each element is a list of three items: The startup options as written
  2565. in the #+STARTUP line, the corresponding variable, and the value to
  2566. set this variable to if the option is found. An optional forth element PUSH
  2567. means to push this value onto the list in the variable.")
  2568. (defun org-set-regexps-and-options ()
  2569. "Precompute regular expressions for current buffer."
  2570. (when (org-mode-p)
  2571. (org-set-local 'org-todo-kwd-alist nil)
  2572. (org-set-local 'org-todo-key-alist nil)
  2573. (org-set-local 'org-todo-key-trigger nil)
  2574. (org-set-local 'org-todo-keywords-1 nil)
  2575. (org-set-local 'org-done-keywords nil)
  2576. (org-set-local 'org-todo-heads nil)
  2577. (org-set-local 'org-todo-sets nil)
  2578. (org-set-local 'org-todo-log-states nil)
  2579. (org-set-local 'org-file-properties nil)
  2580. (org-set-local 'org-file-tags nil)
  2581. (let ((re (org-make-options-regexp
  2582. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2583. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2584. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2585. (splitre "[ \t]+")
  2586. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2587. tail sep kws1 prio props ftags drawers
  2588. ext-setup-or-nil setup-contents (start 0))
  2589. (save-excursion
  2590. (save-restriction
  2591. (widen)
  2592. (goto-char (point-min))
  2593. (while (or (and ext-setup-or-nil
  2594. (string-match re ext-setup-or-nil start)
  2595. (setq start (match-end 0)))
  2596. (and (setq ext-setup-or-nil nil start 0)
  2597. (re-search-forward re nil t)))
  2598. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2599. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2600. (cond
  2601. ((equal key "CATEGORY")
  2602. (if (string-match "[ \t]+$" value)
  2603. (setq value (replace-match "" t t value)))
  2604. (setq cat value))
  2605. ((member key '("SEQ_TODO" "TODO"))
  2606. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2607. ((equal key "TYP_TODO")
  2608. (push (cons 'type (org-split-string value splitre)) kwds))
  2609. ((equal key "TAGS")
  2610. (setq tags (append tags (org-split-string value splitre))))
  2611. ((equal key "COLUMNS")
  2612. (org-set-local 'org-columns-default-format value))
  2613. ((equal key "LINK")
  2614. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2615. (push (cons (match-string 1 value)
  2616. (org-trim (match-string 2 value)))
  2617. links)))
  2618. ((equal key "PRIORITIES")
  2619. (setq prio (org-split-string value " +")))
  2620. ((equal key "PROPERTY")
  2621. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2622. (push (cons (match-string 1 value) (match-string 2 value))
  2623. props)))
  2624. ((equal key "FILETAGS")
  2625. (when (string-match "\\S-" value)
  2626. (setq ftags
  2627. (append
  2628. ftags
  2629. (apply 'append
  2630. (mapcar (lambda (x) (org-split-string x ":"))
  2631. (org-split-string value)))))))
  2632. ((equal key "DRAWERS")
  2633. (setq drawers (org-split-string value splitre)))
  2634. ((equal key "CONSTANTS")
  2635. (setq const (append const (org-split-string value splitre))))
  2636. ((equal key "STARTUP")
  2637. (let ((opts (org-split-string value splitre))
  2638. l var val)
  2639. (while (setq l (pop opts))
  2640. (when (setq l (assoc l org-startup-options))
  2641. (setq var (nth 1 l) val (nth 2 l))
  2642. (if (not (nth 3 l))
  2643. (set (make-local-variable var) val)
  2644. (if (not (listp (symbol-value var)))
  2645. (set (make-local-variable var) nil))
  2646. (set (make-local-variable var) (symbol-value var))
  2647. (add-to-list var val))))))
  2648. ((equal key "ARCHIVE")
  2649. (string-match " *$" value)
  2650. (setq arch (replace-match "" t t value))
  2651. (remove-text-properties 0 (length arch)
  2652. '(face t fontified t) arch))
  2653. ((equal key "SETUPFILE")
  2654. (setq setup-contents (org-file-contents
  2655. (expand-file-name
  2656. (org-remove-double-quotes value))
  2657. 'noerror))
  2658. (if (not ext-setup-or-nil)
  2659. (setq ext-setup-or-nil setup-contents start 0)
  2660. (setq ext-setup-or-nil
  2661. (concat (substring ext-setup-or-nil 0 start)
  2662. "\n" setup-contents "\n"
  2663. (substring ext-setup-or-nil start)))))
  2664. ))))
  2665. (when cat
  2666. (org-set-local 'org-category (intern cat))
  2667. (push (cons "CATEGORY" cat) props))
  2668. (when prio
  2669. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2670. (setq prio (mapcar 'string-to-char prio))
  2671. (org-set-local 'org-highest-priority (nth 0 prio))
  2672. (org-set-local 'org-lowest-priority (nth 1 prio))
  2673. (org-set-local 'org-default-priority (nth 2 prio)))
  2674. (and props (org-set-local 'org-file-properties (nreverse props)))
  2675. (and ftags (org-set-local 'org-file-tags ftags))
  2676. (and drawers (org-set-local 'org-drawers drawers))
  2677. (and arch (org-set-local 'org-archive-location arch))
  2678. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2679. ;; Process the TODO keywords
  2680. (unless kwds
  2681. ;; Use the global values as if they had been given locally.
  2682. (setq kwds (default-value 'org-todo-keywords))
  2683. (if (stringp (car kwds))
  2684. (setq kwds (list (cons org-todo-interpretation
  2685. (default-value 'org-todo-keywords)))))
  2686. (setq kwds (reverse kwds)))
  2687. (setq kwds (nreverse kwds))
  2688. (let (inter kws kw)
  2689. (while (setq kws (pop kwds))
  2690. (setq inter (pop kws) sep (member "|" kws)
  2691. kws0 (delete "|" (copy-sequence kws))
  2692. kwsa nil
  2693. kws1 (mapcar
  2694. (lambda (x)
  2695. ;; 1 2
  2696. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2697. (progn
  2698. (setq kw (match-string 1 x)
  2699. key (and (match-end 2) (match-string 2 x))
  2700. log (org-extract-log-state-settings x))
  2701. (push (cons kw (and key (string-to-char key))) kwsa)
  2702. (and log (push log org-todo-log-states))
  2703. kw)
  2704. (error "Invalid TODO keyword %s" x)))
  2705. kws0)
  2706. kwsa (if kwsa (append '((:startgroup))
  2707. (nreverse kwsa)
  2708. '((:endgroup))))
  2709. hw (car kws1)
  2710. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2711. tail (list inter hw (car dws) (org-last dws)))
  2712. (add-to-list 'org-todo-heads hw 'append)
  2713. (push kws1 org-todo-sets)
  2714. (setq org-done-keywords (append org-done-keywords dws nil))
  2715. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2716. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2717. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2718. (setq org-todo-sets (nreverse org-todo-sets)
  2719. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2720. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2721. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2722. ;; Process the constants
  2723. (when const
  2724. (let (e cst)
  2725. (while (setq e (pop const))
  2726. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2727. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2728. (setq org-table-formula-constants-local cst)))
  2729. ;; Process the tags.
  2730. (when tags
  2731. (let (e tgs)
  2732. (while (setq e (pop tags))
  2733. (cond
  2734. ((equal e "{") (push '(:startgroup) tgs))
  2735. ((equal e "}") (push '(:endgroup) tgs))
  2736. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2737. (push (cons (match-string 1 e)
  2738. (string-to-char (match-string 2 e)))
  2739. tgs))
  2740. (t (push (list e) tgs))))
  2741. (org-set-local 'org-tag-alist nil)
  2742. (while (setq e (pop tgs))
  2743. (or (and (stringp (car e))
  2744. (assoc (car e) org-tag-alist))
  2745. (push e org-tag-alist)))))
  2746. ;; Compute the regular expressions and other local variables
  2747. (if (not org-done-keywords)
  2748. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2749. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2750. (length org-scheduled-string)
  2751. (length org-clock-string)
  2752. (length org-closed-string)))
  2753. org-drawer-regexp
  2754. (concat "^[ \t]*:\\("
  2755. (mapconcat 'regexp-quote org-drawers "\\|")
  2756. "\\):[ \t]*$")
  2757. org-not-done-keywords
  2758. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2759. org-todo-regexp
  2760. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2761. "\\|") "\\)\\>")
  2762. org-not-done-regexp
  2763. (concat "\\<\\("
  2764. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2765. "\\)\\>")
  2766. org-todo-line-regexp
  2767. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2768. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2769. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2770. org-complex-heading-regexp
  2771. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2772. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2773. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2774. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2775. org-nl-done-regexp
  2776. (concat "\n\\*+[ \t]+"
  2777. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2778. "\\)" "\\>")
  2779. org-todo-line-tags-regexp
  2780. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2781. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2782. (org-re
  2783. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2784. org-looking-at-done-regexp
  2785. (concat "^" "\\(?:"
  2786. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2787. "\\>")
  2788. org-deadline-regexp (concat "\\<" org-deadline-string)
  2789. org-deadline-time-regexp
  2790. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2791. org-deadline-line-regexp
  2792. (concat "\\<\\(" org-deadline-string "\\).*")
  2793. org-scheduled-regexp
  2794. (concat "\\<" org-scheduled-string)
  2795. org-scheduled-time-regexp
  2796. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2797. org-closed-time-regexp
  2798. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2799. org-keyword-time-regexp
  2800. (concat "\\<\\(" org-scheduled-string
  2801. "\\|" org-deadline-string
  2802. "\\|" org-closed-string
  2803. "\\|" org-clock-string "\\)"
  2804. " *[[<]\\([^]>]+\\)[]>]")
  2805. org-keyword-time-not-clock-regexp
  2806. (concat "\\<\\(" org-scheduled-string
  2807. "\\|" org-deadline-string
  2808. "\\|" org-closed-string
  2809. "\\)"
  2810. " *[[<]\\([^]>]+\\)[]>]")
  2811. org-maybe-keyword-time-regexp
  2812. (concat "\\(\\<\\(" org-scheduled-string
  2813. "\\|" org-deadline-string
  2814. "\\|" org-closed-string
  2815. "\\|" org-clock-string "\\)\\)?"
  2816. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2817. org-planning-or-clock-line-re
  2818. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2819. "\\|" org-deadline-string
  2820. "\\|" org-closed-string "\\|" org-clock-string
  2821. "\\)\\>\\)")
  2822. )
  2823. (org-compute-latex-and-specials-regexp)
  2824. (org-set-font-lock-defaults))))
  2825. (defun org-file-contents (file &optional noerror)
  2826. "Return the contents of FILE, as a string."
  2827. (if (or (not file)
  2828. (not (file-readable-p file)))
  2829. (if noerror
  2830. (progn
  2831. (message "Cannot read file %s" file)
  2832. (ding) (sit-for 2)
  2833. "")
  2834. (error "Cannot read file %s" file))
  2835. (with-temp-buffer
  2836. (insert-file-contents file)
  2837. (buffer-string))))
  2838. (defun org-extract-log-state-settings (x)
  2839. "Extract the log state setting from a TODO keyword string.
  2840. This will extract info from a string like \"WAIT(w@/!)\"."
  2841. (let (kw key log1 log2)
  2842. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2843. (setq kw (match-string 1 x)
  2844. key (and (match-end 2) (match-string 2 x))
  2845. log1 (and (match-end 3) (match-string 3 x))
  2846. log2 (and (match-end 4) (match-string 4 x)))
  2847. (and (or log1 log2)
  2848. (list kw
  2849. (and log1 (if (equal log1 "!") 'time 'note))
  2850. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2851. (defun org-remove-keyword-keys (list)
  2852. "Remove a pair of parenthesis at the end of each string in LIST."
  2853. (mapcar (lambda (x)
  2854. (if (string-match "(.*)$" x)
  2855. (substring x 0 (match-beginning 0))
  2856. x))
  2857. list))
  2858. ;; FIXME: this could be done much better, using second characters etc.
  2859. (defun org-assign-fast-keys (alist)
  2860. "Assign fast keys to a keyword-key alist.
  2861. Respect keys that are already there."
  2862. (let (new e k c c1 c2 (char ?a))
  2863. (while (setq e (pop alist))
  2864. (cond
  2865. ((equal e '(:startgroup)) (push e new))
  2866. ((equal e '(:endgroup)) (push e new))
  2867. (t
  2868. (setq k (car e) c2 nil)
  2869. (if (cdr e)
  2870. (setq c (cdr e))
  2871. ;; automatically assign a character.
  2872. (setq c1 (string-to-char
  2873. (downcase (substring
  2874. k (if (= (string-to-char k) ?@) 1 0)))))
  2875. (if (or (rassoc c1 new) (rassoc c1 alist))
  2876. (while (or (rassoc char new) (rassoc char alist))
  2877. (setq char (1+ char)))
  2878. (setq c2 c1))
  2879. (setq c (or c2 char)))
  2880. (push (cons k c) new))))
  2881. (nreverse new)))
  2882. ;;; Some variables used in various places
  2883. (defvar org-window-configuration nil
  2884. "Used in various places to store a window configuration.")
  2885. (defvar org-finish-function nil
  2886. "Function to be called when `C-c C-c' is used.
  2887. This is for getting out of special buffers like remember.")
  2888. ;; FIXME: Occasionally check by commenting these, to make sure
  2889. ;; no other functions uses these, forgetting to let-bind them.
  2890. (defvar entry)
  2891. (defvar state)
  2892. (defvar last-state)
  2893. (defvar date)
  2894. (defvar description)
  2895. ;; Defined somewhere in this file, but used before definition.
  2896. (defvar org-html-entities)
  2897. (defvar org-struct-menu)
  2898. (defvar org-org-menu)
  2899. (defvar org-tbl-menu)
  2900. (defvar org-agenda-keymap)
  2901. ;;;; Define the Org-mode
  2902. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2903. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
  2904. ;; We use a before-change function to check if a table might need
  2905. ;; an update.
  2906. (defvar org-table-may-need-update t
  2907. "Indicates that a table might need an update.
  2908. This variable is set by `org-before-change-function'.
  2909. `org-table-align' sets it back to nil.")
  2910. (defun org-before-change-function (beg end)
  2911. "Every change indicates that a table might need an update."
  2912. (setq org-table-may-need-update t))
  2913. (defvar org-mode-map)
  2914. (defvar org-mode-hook nil)
  2915. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2916. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2917. (defvar org-table-buffer-is-an nil)
  2918. (defconst org-outline-regexp "\\*+ ")
  2919. ;;;###autoload
  2920. (define-derived-mode org-mode outline-mode "Org"
  2921. "Outline-based notes management and organizer, alias
  2922. \"Carsten's outline-mode for keeping track of everything.\"
  2923. Org-mode develops organizational tasks around a NOTES file which
  2924. contains information about projects as plain text. Org-mode is
  2925. implemented on top of outline-mode, which is ideal to keep the content
  2926. of large files well structured. It supports ToDo items, deadlines and
  2927. time stamps, which magically appear in the diary listing of the Emacs
  2928. calendar. Tables are easily created with a built-in table editor.
  2929. Plain text URL-like links connect to websites, emails (VM), Usenet
  2930. messages (Gnus), BBDB entries, and any files related to the project.
  2931. For printing and sharing of notes, an Org-mode file (or a part of it)
  2932. can be exported as a structured ASCII or HTML file.
  2933. The following commands are available:
  2934. \\{org-mode-map}"
  2935. ;; Get rid of Outline menus, they are not needed
  2936. ;; Need to do this here because define-derived-mode sets up
  2937. ;; the keymap so late. Still, it is a waste to call this each time
  2938. ;; we switch another buffer into org-mode.
  2939. (if (featurep 'xemacs)
  2940. (when (boundp 'outline-mode-menu-heading)
  2941. ;; Assume this is Greg's port, it used easymenu
  2942. (easy-menu-remove outline-mode-menu-heading)
  2943. (easy-menu-remove outline-mode-menu-show)
  2944. (easy-menu-remove outline-mode-menu-hide))
  2945. (define-key org-mode-map [menu-bar headings] 'undefined)
  2946. (define-key org-mode-map [menu-bar hide] 'undefined)
  2947. (define-key org-mode-map [menu-bar show] 'undefined))
  2948. (org-load-modules-maybe)
  2949. (easy-menu-add org-org-menu)
  2950. (easy-menu-add org-tbl-menu)
  2951. (org-install-agenda-files-menu)
  2952. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2953. (org-add-to-invisibility-spec '(org-cwidth))
  2954. (when (featurep 'xemacs)
  2955. (org-set-local 'line-move-ignore-invisible t))
  2956. (org-set-local 'outline-regexp org-outline-regexp)
  2957. (org-set-local 'outline-level 'org-outline-level)
  2958. (when (and org-ellipsis
  2959. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2960. (fboundp 'make-glyph-code))
  2961. (unless org-display-table
  2962. (setq org-display-table (make-display-table)))
  2963. (set-display-table-slot
  2964. org-display-table 4
  2965. (vconcat (mapcar
  2966. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2967. org-ellipsis)))
  2968. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2969. (setq buffer-display-table org-display-table))
  2970. (org-set-regexps-and-options)
  2971. ;; Calc embedded
  2972. (org-set-local 'calc-embedded-open-mode "# ")
  2973. (modify-syntax-entry ?# "<")
  2974. (modify-syntax-entry ?@ "w")
  2975. (if org-startup-truncated (setq truncate-lines t))
  2976. (org-set-local 'font-lock-unfontify-region-function
  2977. 'org-unfontify-region)
  2978. ;; Activate before-change-function
  2979. (org-set-local 'org-table-may-need-update t)
  2980. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2981. 'local)
  2982. ;; Check for running clock before killing a buffer
  2983. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2984. ;; Paragraphs and auto-filling
  2985. (org-set-autofill-regexps)
  2986. (setq indent-line-function 'org-indent-line-function)
  2987. (org-update-radio-target-regexp)
  2988. ;; Comment characters
  2989. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2990. (org-set-local 'comment-padding " ")
  2991. ;; Align options lines
  2992. (org-set-local
  2993. 'align-mode-rules-list
  2994. '((org-in-buffer-settings
  2995. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2996. (modes . '(org-mode)))))
  2997. ;; Imenu
  2998. (org-set-local 'imenu-create-index-function
  2999. 'org-imenu-get-tree)
  3000. ;; Make isearch reveal context
  3001. (if (or (featurep 'xemacs)
  3002. (not (boundp 'outline-isearch-open-invisible-function)))
  3003. ;; Emacs 21 and XEmacs make use of the hook
  3004. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3005. ;; Emacs 22 deals with this through a special variable
  3006. (org-set-local 'outline-isearch-open-invisible-function
  3007. (lambda (&rest ignore) (org-show-context 'isearch))))
  3008. ;; If empty file that did not turn on org-mode automatically, make it to.
  3009. (if (and org-insert-mode-line-in-empty-file
  3010. (interactive-p)
  3011. (= (point-min) (point-max)))
  3012. (insert "# -*- mode: org -*-\n\n"))
  3013. (unless org-inhibit-startup
  3014. (when org-startup-align-all-tables
  3015. (let ((bmp (buffer-modified-p)))
  3016. (org-table-map-tables 'org-table-align)
  3017. (set-buffer-modified-p bmp)))
  3018. (org-set-startup-visibility)))
  3019. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3020. (defun org-current-time ()
  3021. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3022. (if (> (car org-time-stamp-rounding-minutes) 1)
  3023. (let ((r (car org-time-stamp-rounding-minutes))
  3024. (time (decode-time)))
  3025. (apply 'encode-time
  3026. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3027. (nthcdr 2 time))))
  3028. (current-time)))
  3029. ;;;; Font-Lock stuff, including the activators
  3030. (defvar org-mouse-map (make-sparse-keymap))
  3031. (org-defkey org-mouse-map
  3032. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3033. (org-defkey org-mouse-map
  3034. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3035. (when org-mouse-1-follows-link
  3036. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3037. (when org-tab-follows-link
  3038. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3039. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3040. (when org-return-follows-link
  3041. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3042. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3043. (require 'font-lock)
  3044. (defconst org-non-link-chars "]\t\n\r<>")
  3045. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3046. "shell" "elisp"))
  3047. (defvar org-link-types-re nil
  3048. "Matches a link that has a url-like prefix like \"http:\"")
  3049. (defvar org-link-re-with-space nil
  3050. "Matches a link with spaces, optional angular brackets around it.")
  3051. (defvar org-link-re-with-space2 nil
  3052. "Matches a link with spaces, optional angular brackets around it.")
  3053. (defvar org-link-re-with-space3 nil
  3054. "Matches a link with spaces, only for internal part in bracket links.")
  3055. (defvar org-angle-link-re nil
  3056. "Matches link with angular brackets, spaces are allowed.")
  3057. (defvar org-plain-link-re nil
  3058. "Matches plain link, without spaces.")
  3059. (defvar org-bracket-link-regexp nil
  3060. "Matches a link in double brackets.")
  3061. (defvar org-bracket-link-analytic-regexp nil
  3062. "Regular expression used to analyze links.
  3063. Here is what the match groups contain after a match:
  3064. 1: http:
  3065. 2: http
  3066. 3: path
  3067. 4: [desc]
  3068. 5: desc")
  3069. (defvar org-any-link-re nil
  3070. "Regular expression matching any link.")
  3071. (defun org-make-link-regexps ()
  3072. "Update the link regular expressions.
  3073. This should be called after the variable `org-link-types' has changed."
  3074. (setq org-link-types-re
  3075. (concat
  3076. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3077. org-link-re-with-space
  3078. (concat
  3079. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3080. "\\([^" org-non-link-chars " ]"
  3081. "[^" org-non-link-chars "]*"
  3082. "[^" org-non-link-chars " ]\\)>?")
  3083. org-link-re-with-space2
  3084. (concat
  3085. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3086. "\\([^" org-non-link-chars " ]"
  3087. "[^\t\n\r]*"
  3088. "[^" org-non-link-chars " ]\\)>?")
  3089. org-link-re-with-space3
  3090. (concat
  3091. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3092. "\\([^" org-non-link-chars " ]"
  3093. "[^\t\n\r]*\\)")
  3094. org-angle-link-re
  3095. (concat
  3096. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3097. "\\([^" org-non-link-chars " ]"
  3098. "[^" org-non-link-chars "]*"
  3099. "\\)>")
  3100. org-plain-link-re
  3101. (concat
  3102. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3103. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3104. org-bracket-link-regexp
  3105. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3106. org-bracket-link-analytic-regexp
  3107. (concat
  3108. "\\[\\["
  3109. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3110. "\\([^]]+\\)"
  3111. "\\]"
  3112. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3113. "\\]")
  3114. org-any-link-re
  3115. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3116. org-angle-link-re "\\)\\|\\("
  3117. org-plain-link-re "\\)")))
  3118. (org-make-link-regexps)
  3119. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3120. "Regular expression for fast time stamp matching.")
  3121. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3122. "Regular expression for fast time stamp matching.")
  3123. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3124. "Regular expression matching time strings for analysis.
  3125. This one does not require the space after the date, so it can be used
  3126. on a string that terminates immediately after the date.")
  3127. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3128. "Regular expression matching time strings for analysis.")
  3129. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3130. "Regular expression matching time stamps, with groups.")
  3131. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3132. "Regular expression matching time stamps (also [..]), with groups.")
  3133. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3134. "Regular expression matching a time stamp range.")
  3135. (defconst org-tr-regexp-both
  3136. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3137. "Regular expression matching a time stamp range.")
  3138. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3139. org-ts-regexp "\\)?")
  3140. "Regular expression matching a time stamp or time stamp range.")
  3141. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3142. org-ts-regexp-both "\\)?")
  3143. "Regular expression matching a time stamp or time stamp range.
  3144. The time stamps may be either active or inactive.")
  3145. (defvar org-emph-face nil)
  3146. (defun org-do-emphasis-faces (limit)
  3147. "Run through the buffer and add overlays to links."
  3148. (let (rtn)
  3149. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3150. (if (not (= (char-after (match-beginning 3))
  3151. (char-after (match-beginning 4))))
  3152. (progn
  3153. (setq rtn t)
  3154. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3155. 'face
  3156. (nth 1 (assoc (match-string 3)
  3157. org-emphasis-alist)))
  3158. (add-text-properties (match-beginning 2) (match-end 2)
  3159. '(font-lock-multiline t))
  3160. (when org-hide-emphasis-markers
  3161. (add-text-properties (match-end 4) (match-beginning 5)
  3162. '(invisible org-link))
  3163. (add-text-properties (match-beginning 3) (match-end 3)
  3164. '(invisible org-link)))))
  3165. (backward-char 1))
  3166. rtn))
  3167. (defun org-emphasize (&optional char)
  3168. "Insert or change an emphasis, i.e. a font like bold or italic.
  3169. If there is an active region, change that region to a new emphasis.
  3170. If there is no region, just insert the marker characters and position
  3171. the cursor between them.
  3172. CHAR should be either the marker character, or the first character of the
  3173. HTML tag associated with that emphasis. If CHAR is a space, the means
  3174. to remove the emphasis of the selected region.
  3175. If char is not given (for example in an interactive call) it
  3176. will be prompted for."
  3177. (interactive)
  3178. (let ((eal org-emphasis-alist) e det
  3179. (erc org-emphasis-regexp-components)
  3180. (prompt "")
  3181. (string "") beg end move tag c s)
  3182. (if (org-region-active-p)
  3183. (setq beg (region-beginning) end (region-end)
  3184. string (buffer-substring beg end))
  3185. (setq move t))
  3186. (while (setq e (pop eal))
  3187. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3188. c (aref tag 0))
  3189. (push (cons c (string-to-char (car e))) det)
  3190. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3191. (substring tag 1)))))
  3192. (setq det (nreverse det))
  3193. (unless char
  3194. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3195. (setq char (read-char-exclusive)))
  3196. (setq char (or (cdr (assoc char det)) char))
  3197. (if (equal char ?\ )
  3198. (setq s "" move nil)
  3199. (unless (assoc (char-to-string char) org-emphasis-alist)
  3200. (error "No such emphasis marker: \"%c\"" char))
  3201. (setq s (char-to-string char)))
  3202. (while (and (> (length string) 1)
  3203. (equal (substring string 0 1) (substring string -1))
  3204. (assoc (substring string 0 1) org-emphasis-alist))
  3205. (setq string (substring string 1 -1)))
  3206. (setq string (concat s string s))
  3207. (if beg (delete-region beg end))
  3208. (unless (or (bolp)
  3209. (string-match (concat "[" (nth 0 erc) "\n]")
  3210. (char-to-string (char-before (point)))))
  3211. (insert " "))
  3212. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3213. (char-to-string (char-after (point))))
  3214. (insert " ") (backward-char 1))
  3215. (insert string)
  3216. (and move (backward-char 1))))
  3217. (defconst org-nonsticky-props
  3218. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3219. (defun org-activate-plain-links (limit)
  3220. "Run through the buffer and add overlays to links."
  3221. (catch 'exit
  3222. (let (f)
  3223. (while (re-search-forward org-plain-link-re limit t)
  3224. (setq f (get-text-property (match-beginning 0) 'face))
  3225. (if (or (eq f 'org-tag)
  3226. (and (listp f) (memq 'org-tag f)))
  3227. nil
  3228. (add-text-properties (match-beginning 0) (match-end 0)
  3229. (list 'mouse-face 'highlight
  3230. 'rear-nonsticky org-nonsticky-props
  3231. 'keymap org-mouse-map
  3232. ))
  3233. (throw 'exit t))))))
  3234. (defun org-activate-code (limit)
  3235. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3236. (progn
  3237. (remove-text-properties (match-beginning 0) (match-end 0)
  3238. '(display t invisible t intangible t))
  3239. t)))
  3240. (defun org-activate-angle-links (limit)
  3241. "Run through the buffer and add overlays to links."
  3242. (if (re-search-forward org-angle-link-re limit t)
  3243. (progn
  3244. (add-text-properties (match-beginning 0) (match-end 0)
  3245. (list 'mouse-face 'highlight
  3246. 'rear-nonsticky org-nonsticky-props
  3247. 'keymap org-mouse-map
  3248. ))
  3249. t)))
  3250. (defun org-activate-bracket-links (limit)
  3251. "Run through the buffer and add overlays to bracketed links."
  3252. (if (re-search-forward org-bracket-link-regexp limit t)
  3253. (let* ((help (concat "LINK: "
  3254. (org-match-string-no-properties 1)))
  3255. ;; FIXME: above we should remove the escapes.
  3256. ;; but that requires another match, protecting match data,
  3257. ;; a lot of overhead for font-lock.
  3258. (ip (org-maybe-intangible
  3259. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3260. 'keymap org-mouse-map 'mouse-face 'highlight
  3261. 'font-lock-multiline t 'help-echo help)))
  3262. (vp (list 'rear-nonsticky org-nonsticky-props
  3263. 'keymap org-mouse-map 'mouse-face 'highlight
  3264. ' font-lock-multiline t 'help-echo help)))
  3265. ;; We need to remove the invisible property here. Table narrowing
  3266. ;; may have made some of this invisible.
  3267. (remove-text-properties (match-beginning 0) (match-end 0)
  3268. '(invisible nil))
  3269. (if (match-end 3)
  3270. (progn
  3271. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3272. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3273. (add-text-properties (match-end 3) (match-end 0) ip))
  3274. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3275. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3276. (add-text-properties (match-end 1) (match-end 0) ip))
  3277. t)))
  3278. (defun org-activate-dates (limit)
  3279. "Run through the buffer and add overlays to dates."
  3280. (if (re-search-forward org-tsr-regexp-both limit t)
  3281. (progn
  3282. (add-text-properties (match-beginning 0) (match-end 0)
  3283. (list 'mouse-face 'highlight
  3284. 'rear-nonsticky org-nonsticky-props
  3285. 'keymap org-mouse-map))
  3286. (when org-display-custom-times
  3287. (if (match-end 3)
  3288. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3289. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3290. t)))
  3291. (defvar org-target-link-regexp nil
  3292. "Regular expression matching radio targets in plain text.")
  3293. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3294. "Regular expression matching a link target.")
  3295. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3296. "Regular expression matching a radio target.")
  3297. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3298. "Regular expression matching any target.")
  3299. (defun org-activate-target-links (limit)
  3300. "Run through the buffer and add overlays to target matches."
  3301. (when org-target-link-regexp
  3302. (let ((case-fold-search t))
  3303. (if (re-search-forward org-target-link-regexp limit t)
  3304. (progn
  3305. (add-text-properties (match-beginning 0) (match-end 0)
  3306. (list 'mouse-face 'highlight
  3307. 'rear-nonsticky org-nonsticky-props
  3308. 'keymap org-mouse-map
  3309. 'help-echo "Radio target link"
  3310. 'org-linked-text t))
  3311. t)))))
  3312. (defun org-update-radio-target-regexp ()
  3313. "Find all radio targets in this file and update the regular expression."
  3314. (interactive)
  3315. (when (memq 'radio org-activate-links)
  3316. (setq org-target-link-regexp
  3317. (org-make-target-link-regexp (org-all-targets 'radio)))
  3318. (org-restart-font-lock)))
  3319. (defun org-hide-wide-columns (limit)
  3320. (let (s e)
  3321. (setq s (text-property-any (point) (or limit (point-max))
  3322. 'org-cwidth t))
  3323. (when s
  3324. (setq e (next-single-property-change s 'org-cwidth))
  3325. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3326. (goto-char e)
  3327. t)))
  3328. (defvar org-latex-and-specials-regexp nil
  3329. "Regular expression for highlighting export special stuff.")
  3330. (defvar org-match-substring-regexp)
  3331. (defvar org-match-substring-with-braces-regexp)
  3332. (defvar org-export-html-special-string-regexps)
  3333. (defun org-compute-latex-and-specials-regexp ()
  3334. "Compute regular expression for stuff treated specially by exporters."
  3335. (if (not org-highlight-latex-fragments-and-specials)
  3336. (org-set-local 'org-latex-and-specials-regexp nil)
  3337. (require 'org-exp)
  3338. (let*
  3339. ((matchers (plist-get org-format-latex-options :matchers))
  3340. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3341. org-latex-regexps)))
  3342. (options (org-combine-plists (org-default-export-plist)
  3343. (org-infile-export-plist)))
  3344. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3345. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3346. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3347. (org-export-html-expand (plist-get options :expand-quoted-html))
  3348. (org-export-with-special-strings (plist-get options :special-strings))
  3349. (re-sub
  3350. (cond
  3351. ((equal org-export-with-sub-superscripts '{})
  3352. (list org-match-substring-with-braces-regexp))
  3353. (org-export-with-sub-superscripts
  3354. (list org-match-substring-regexp))
  3355. (t nil)))
  3356. (re-latex
  3357. (if org-export-with-LaTeX-fragments
  3358. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3359. (re-macros
  3360. (if org-export-with-TeX-macros
  3361. (list (concat "\\\\"
  3362. (regexp-opt
  3363. (append (mapcar 'car org-html-entities)
  3364. (if (boundp 'org-latex-entities)
  3365. org-latex-entities nil))
  3366. 'words))) ; FIXME
  3367. ))
  3368. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3369. (re-special (if org-export-with-special-strings
  3370. (mapcar (lambda (x) (car x))
  3371. org-export-html-special-string-regexps)))
  3372. (re-rest
  3373. (delq nil
  3374. (list
  3375. (if org-export-html-expand "@<[^>\n]+>")
  3376. ))))
  3377. (org-set-local
  3378. 'org-latex-and-specials-regexp
  3379. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3380. re-rest) "\\|")))))
  3381. (defun org-do-latex-and-special-faces (limit)
  3382. "Run through the buffer and add overlays to links."
  3383. (when org-latex-and-specials-regexp
  3384. (let (rtn d)
  3385. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3386. limit t))
  3387. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3388. 'face))
  3389. '(org-code org-verbatim underline)))
  3390. (progn
  3391. (setq rtn t
  3392. d (cond ((member (char-after (1+ (match-beginning 0)))
  3393. '(?_ ?^)) 1)
  3394. (t 0)))
  3395. (font-lock-prepend-text-property
  3396. (+ d (match-beginning 0)) (match-end 0)
  3397. 'face 'org-latex-and-export-specials)
  3398. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3399. '(font-lock-multiline t)))))
  3400. rtn)))
  3401. (defun org-restart-font-lock ()
  3402. "Restart font-lock-mode, to force refontification."
  3403. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3404. (font-lock-mode -1)
  3405. (font-lock-mode 1)))
  3406. (defun org-all-targets (&optional radio)
  3407. "Return a list of all targets in this file.
  3408. With optional argument RADIO, only find radio targets."
  3409. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3410. rtn)
  3411. (save-excursion
  3412. (goto-char (point-min))
  3413. (while (re-search-forward re nil t)
  3414. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3415. rtn)))
  3416. (defun org-make-target-link-regexp (targets)
  3417. "Make regular expression matching all strings in TARGETS.
  3418. The regular expression finds the targets also if there is a line break
  3419. between words."
  3420. (and targets
  3421. (concat
  3422. "\\<\\("
  3423. (mapconcat
  3424. (lambda (x)
  3425. (while (string-match " +" x)
  3426. (setq x (replace-match "\\s-+" t t x)))
  3427. x)
  3428. targets
  3429. "\\|")
  3430. "\\)\\>")))
  3431. (defun org-activate-tags (limit)
  3432. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3433. (progn
  3434. (add-text-properties (match-beginning 1) (match-end 1)
  3435. (list 'mouse-face 'highlight
  3436. 'rear-nonsticky org-nonsticky-props
  3437. 'keymap org-mouse-map))
  3438. t)))
  3439. (defun org-outline-level ()
  3440. (save-excursion
  3441. (looking-at outline-regexp)
  3442. (if (match-beginning 1)
  3443. (+ (org-get-string-indentation (match-string 1)) 1000)
  3444. (1- (- (match-end 0) (match-beginning 0))))))
  3445. (defvar org-font-lock-keywords nil)
  3446. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3447. "Regular expression matching a property line.")
  3448. (defvar org-font-lock-hook nil
  3449. "Functions to be called for special font lock stuff.")
  3450. (defun org-font-lock-hook (limit)
  3451. (run-hook-with-args 'org-font-lock-hook limit))
  3452. (defun org-set-font-lock-defaults ()
  3453. (let* ((em org-fontify-emphasized-text)
  3454. (lk org-activate-links)
  3455. (org-font-lock-extra-keywords
  3456. (list
  3457. ;; Call the hook
  3458. '(org-font-lock-hook)
  3459. ;; Headlines
  3460. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3461. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3462. ;; Table lines
  3463. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3464. (1 'org-table t))
  3465. ;; Table internals
  3466. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3467. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3468. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3469. ;; Drawers
  3470. (list org-drawer-regexp '(0 'org-special-keyword t))
  3471. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3472. ;; Properties
  3473. (list org-property-re
  3474. '(1 'org-special-keyword t)
  3475. '(3 'org-property-value t))
  3476. (if org-format-transports-properties-p
  3477. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3478. ;; Links
  3479. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3480. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3481. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3482. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3483. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3484. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3485. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3486. '(org-hide-wide-columns (0 nil append))
  3487. ;; TODO lines
  3488. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3489. '(1 (org-get-todo-face 1) t))
  3490. ;; DONE
  3491. (if org-fontify-done-headline
  3492. (list (concat "^[*]+ +\\<\\("
  3493. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3494. "\\)\\(.*\\)")
  3495. '(2 'org-headline-done t))
  3496. nil)
  3497. ;; Priorities
  3498. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3499. ;; Special keywords
  3500. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3501. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3502. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3503. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3504. ;; Emphasis
  3505. (if em
  3506. (if (featurep 'xemacs)
  3507. '(org-do-emphasis-faces (0 nil append))
  3508. '(org-do-emphasis-faces)))
  3509. ;; Checkboxes
  3510. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3511. 2 'bold prepend)
  3512. (if org-provide-checkbox-statistics
  3513. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3514. (0 (org-get-checkbox-statistics-face) t)))
  3515. ;; Description list items
  3516. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3517. 2 'bold prepend)
  3518. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3519. '(1 'org-archived prepend))
  3520. ;; Specials
  3521. '(org-do-latex-and-special-faces)
  3522. ;; Code
  3523. '(org-activate-code (1 'org-code t))
  3524. ;; COMMENT
  3525. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3526. "\\|" org-quote-string "\\)\\>")
  3527. '(1 'org-special-keyword t))
  3528. '("^#.*" (0 'font-lock-comment-face t))
  3529. )))
  3530. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3531. ;; Now set the full font-lock-keywords
  3532. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3533. (org-set-local 'font-lock-defaults
  3534. '(org-font-lock-keywords t nil nil backward-paragraph))
  3535. (kill-local-variable 'font-lock-keywords) nil))
  3536. (defvar org-m nil)
  3537. (defvar org-l nil)
  3538. (defvar org-f nil)
  3539. (defun org-get-level-face (n)
  3540. "Get the right face for match N in font-lock matching of healdines."
  3541. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3542. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3543. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3544. (cond
  3545. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3546. ((eq n 2) org-f)
  3547. (t (if org-level-color-stars-only nil org-f))))
  3548. (defun org-get-todo-face (kwd)
  3549. "Get the right face for a TODO keyword KWD.
  3550. If KWD is a number, get the corresponding match group."
  3551. (if (numberp kwd) (setq kwd (match-string kwd)))
  3552. (or (cdr (assoc kwd org-todo-keyword-faces))
  3553. (and (member kwd org-done-keywords) 'org-done)
  3554. 'org-todo))
  3555. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3556. "Remove fontification and activation overlays from links."
  3557. (font-lock-default-unfontify-region beg end)
  3558. (let* ((buffer-undo-list t)
  3559. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3560. (inhibit-modification-hooks t)
  3561. deactivate-mark buffer-file-name buffer-file-truename)
  3562. (remove-text-properties beg end
  3563. '(mouse-face t keymap t org-linked-text t
  3564. invisible t intangible t))))
  3565. ;;;; Visibility cycling, including org-goto and indirect buffer
  3566. ;;; Cycling
  3567. (defvar org-cycle-global-status nil)
  3568. (make-variable-buffer-local 'org-cycle-global-status)
  3569. (defvar org-cycle-subtree-status nil)
  3570. (make-variable-buffer-local 'org-cycle-subtree-status)
  3571. ;;;###autoload
  3572. (defun org-cycle (&optional arg)
  3573. "Visibility cycling for Org-mode.
  3574. - When this function is called with a prefix argument, rotate the entire
  3575. buffer through 3 states (global cycling)
  3576. 1. OVERVIEW: Show only top-level headlines.
  3577. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3578. 3. SHOW ALL: Show everything.
  3579. When called with two C-u C-u prefixes, switch to the startup visibility,
  3580. determined by the variable `org-startup-folded', and by any VISIBILITY
  3581. properties in the buffer.
  3582. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3583. including drawers.
  3584. - When point is at the beginning of a headline, rotate the subtree started
  3585. by this line through 3 different states (local cycling)
  3586. 1. FOLDED: Only the main headline is shown.
  3587. 2. CHILDREN: The main headline and the direct children are shown.
  3588. From this state, you can move to one of the children
  3589. and zoom in further.
  3590. 3. SUBTREE: Show the entire subtree, including body text.
  3591. - When there is a numeric prefix, go up to a heading with level ARG, do
  3592. a `show-subtree' and return to the previous cursor position. If ARG
  3593. is negative, go up that many levels.
  3594. - When point is not at the beginning of a headline, execute the global
  3595. binding for TAB, which is re-indenting the line. See the option
  3596. `org-cycle-emulate-tab' for details.
  3597. - Special case: if point is at the beginning of the buffer and there is
  3598. no headline in line 1, this function will act as if called with prefix arg.
  3599. But only if also the variable `org-cycle-global-at-bob' is t."
  3600. (interactive "P")
  3601. (org-load-modules-maybe)
  3602. (let* ((outline-regexp
  3603. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3604. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3605. outline-regexp))
  3606. (bob-special (and org-cycle-global-at-bob (bobp)
  3607. (not (looking-at outline-regexp))))
  3608. (org-cycle-hook
  3609. (if bob-special
  3610. (delq 'org-optimize-window-after-visibility-change
  3611. (copy-sequence org-cycle-hook))
  3612. org-cycle-hook))
  3613. (pos (point)))
  3614. (if (or bob-special (equal arg '(4)))
  3615. ;; special case: use global cycling
  3616. (setq arg t))
  3617. (cond
  3618. ((equal arg '(16))
  3619. (org-set-startup-visibility)
  3620. (message "Startup visibility, plus VISIBILITY properties"))
  3621. ((equal arg '(64))
  3622. (show-all)
  3623. (message "Entire buffer visible, including drawers"))
  3624. ((org-at-table-p 'any)
  3625. ;; Enter the table or move to the next field in the table
  3626. (or (org-table-recognize-table.el)
  3627. (progn
  3628. (if arg (org-table-edit-field t)
  3629. (org-table-justify-field-maybe)
  3630. (call-interactively 'org-table-next-field)))))
  3631. ((eq arg t) ;; Global cycling
  3632. (cond
  3633. ((and (eq last-command this-command)
  3634. (eq org-cycle-global-status 'overview))
  3635. ;; We just created the overview - now do table of contents
  3636. ;; This can be slow in very large buffers, so indicate action
  3637. (message "CONTENTS...")
  3638. (org-content)
  3639. (message "CONTENTS...done")
  3640. (setq org-cycle-global-status 'contents)
  3641. (run-hook-with-args 'org-cycle-hook 'contents))
  3642. ((and (eq last-command this-command)
  3643. (eq org-cycle-global-status 'contents))
  3644. ;; We just showed the table of contents - now show everything
  3645. (show-all)
  3646. (message "SHOW ALL")
  3647. (setq org-cycle-global-status 'all)
  3648. (run-hook-with-args 'org-cycle-hook 'all))
  3649. (t
  3650. ;; Default action: go to overview
  3651. (org-overview)
  3652. (message "OVERVIEW")
  3653. (setq org-cycle-global-status 'overview)
  3654. (run-hook-with-args 'org-cycle-hook 'overview))))
  3655. ((and org-drawers org-drawer-regexp
  3656. (save-excursion
  3657. (beginning-of-line 1)
  3658. (looking-at org-drawer-regexp)))
  3659. ;; Toggle block visibility
  3660. (org-flag-drawer
  3661. (not (get-char-property (match-end 0) 'invisible))))
  3662. ((integerp arg)
  3663. ;; Show-subtree, ARG levels up from here.
  3664. (save-excursion
  3665. (org-back-to-heading)
  3666. (outline-up-heading (if (< arg 0) (- arg)
  3667. (- (funcall outline-level) arg)))
  3668. (org-show-subtree)))
  3669. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3670. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3671. ;; At a heading: rotate between three different views
  3672. (org-back-to-heading)
  3673. (let ((goal-column 0) eoh eol eos)
  3674. ;; First, some boundaries
  3675. (save-excursion
  3676. (org-back-to-heading)
  3677. (save-excursion
  3678. (beginning-of-line 2)
  3679. (while (and (not (eobp)) ;; this is like `next-line'
  3680. (get-char-property (1- (point)) 'invisible))
  3681. (beginning-of-line 2)) (setq eol (point)))
  3682. (outline-end-of-heading) (setq eoh (point))
  3683. (org-end-of-subtree t)
  3684. (unless (eobp)
  3685. (skip-chars-forward " \t\n")
  3686. (beginning-of-line 1) ; in case this is an item
  3687. )
  3688. (setq eos (1- (point))))
  3689. ;; Find out what to do next and set `this-command'
  3690. (cond
  3691. ((= eos eoh)
  3692. ;; Nothing is hidden behind this heading
  3693. (message "EMPTY ENTRY")
  3694. (setq org-cycle-subtree-status nil)
  3695. (save-excursion
  3696. (goto-char eos)
  3697. (outline-next-heading)
  3698. (if (org-invisible-p) (org-flag-heading nil))))
  3699. ((or (>= eol eos)
  3700. (not (string-match "\\S-" (buffer-substring eol eos))))
  3701. ;; Entire subtree is hidden in one line: open it
  3702. (org-show-entry)
  3703. (show-children)
  3704. (message "CHILDREN")
  3705. (save-excursion
  3706. (goto-char eos)
  3707. (outline-next-heading)
  3708. (if (org-invisible-p) (org-flag-heading nil)))
  3709. (setq org-cycle-subtree-status 'children)
  3710. (run-hook-with-args 'org-cycle-hook 'children))
  3711. ((and (eq last-command this-command)
  3712. (eq org-cycle-subtree-status 'children))
  3713. ;; We just showed the children, now show everything.
  3714. (org-show-subtree)
  3715. (message "SUBTREE")
  3716. (setq org-cycle-subtree-status 'subtree)
  3717. (run-hook-with-args 'org-cycle-hook 'subtree))
  3718. (t
  3719. ;; Default action: hide the subtree.
  3720. (hide-subtree)
  3721. (message "FOLDED")
  3722. (setq org-cycle-subtree-status 'folded)
  3723. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3724. ;; TAB emulation and template completion
  3725. (buffer-read-only (org-back-to-heading))
  3726. ((org-try-structure-completion))
  3727. ((org-try-cdlatex-tab))
  3728. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3729. (or (not (bolp))
  3730. (not (looking-at outline-regexp))))
  3731. (call-interactively (global-key-binding "\t")))
  3732. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3733. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3734. (or (and (eq org-cycle-emulate-tab 'white)
  3735. (= (match-end 0) (point-at-eol)))
  3736. (and (eq org-cycle-emulate-tab 'whitestart)
  3737. (>= (match-end 0) pos))))
  3738. t
  3739. (eq org-cycle-emulate-tab t))
  3740. (call-interactively (global-key-binding "\t")))
  3741. (t (save-excursion
  3742. (org-back-to-heading)
  3743. (org-cycle))))))
  3744. ;;;###autoload
  3745. (defun org-global-cycle (&optional arg)
  3746. "Cycle the global visibility. For details see `org-cycle'.
  3747. With C-u prefix arg, switch to startup visibility.
  3748. With a numeric prefix, show all headlines up to that level."
  3749. (interactive "P")
  3750. (let ((org-cycle-include-plain-lists
  3751. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3752. (cond
  3753. ((integerp arg)
  3754. (show-all)
  3755. (hide-sublevels arg)
  3756. (setq org-cycle-global-status 'contents))
  3757. ((equal arg '(4))
  3758. (org-set-startup-visibility)
  3759. (message "Startup visibility, plus VISIBILITY properties."))
  3760. (t
  3761. (org-cycle '(4))))))
  3762. (defun org-set-startup-visibility ()
  3763. "Set the visibility required by startup options and properties."
  3764. (cond
  3765. ((eq org-startup-folded t)
  3766. (org-cycle '(4)))
  3767. ((eq org-startup-folded 'content)
  3768. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3769. (org-cycle '(4)) (org-cycle '(4)))))
  3770. (org-set-visibility-according-to-property 'no-cleanup)
  3771. (org-cycle-hide-archived-subtrees 'all)
  3772. (org-cycle-hide-drawers 'all)
  3773. (org-cycle-show-empty-lines 'all))
  3774. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3775. "Switch subtree visibilities according to :VISIBILITY: property."
  3776. (interactive)
  3777. (let (state)
  3778. (save-excursion
  3779. (goto-char (point-min))
  3780. (while (re-search-forward
  3781. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3782. nil t)
  3783. (setq state (match-string 1))
  3784. (save-excursion
  3785. (org-back-to-heading t)
  3786. (hide-subtree)
  3787. (org-reveal)
  3788. (cond
  3789. ((equal state '("fold" "folded"))
  3790. (hide-subtree))
  3791. ((equal state "children")
  3792. (org-show-hidden-entry)
  3793. (show-children))
  3794. ((equal state "content")
  3795. (save-excursion
  3796. (save-restriction
  3797. (org-narrow-to-subtree)
  3798. (org-content))))
  3799. ((member state '("all" "showall"))
  3800. (show-subtree)))))
  3801. (unless no-cleanup
  3802. (org-cycle-hide-archived-subtrees 'all)
  3803. (org-cycle-hide-drawers 'all)
  3804. (org-cycle-show-empty-lines 'all)))))
  3805. (defun org-overview ()
  3806. "Switch to overview mode, shoing only top-level headlines.
  3807. Really, this shows all headlines with level equal or greater than the level
  3808. of the first headline in the buffer. This is important, because if the
  3809. first headline is not level one, then (hide-sublevels 1) gives confusing
  3810. results."
  3811. (interactive)
  3812. (let ((level (save-excursion
  3813. (goto-char (point-min))
  3814. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3815. (progn
  3816. (goto-char (match-beginning 0))
  3817. (funcall outline-level))))))
  3818. (and level (hide-sublevels level))))
  3819. (defun org-content (&optional arg)
  3820. "Show all headlines in the buffer, like a table of contents.
  3821. With numerical argument N, show content up to level N."
  3822. (interactive "P")
  3823. (save-excursion
  3824. ;; Visit all headings and show their offspring
  3825. (and (integerp arg) (org-overview))
  3826. (goto-char (point-max))
  3827. (catch 'exit
  3828. (while (and (progn (condition-case nil
  3829. (outline-previous-visible-heading 1)
  3830. (error (goto-char (point-min))))
  3831. t)
  3832. (looking-at outline-regexp))
  3833. (if (integerp arg)
  3834. (show-children (1- arg))
  3835. (show-branches))
  3836. (if (bobp) (throw 'exit nil))))))
  3837. (defun org-optimize-window-after-visibility-change (state)
  3838. "Adjust the window after a change in outline visibility.
  3839. This function is the default value of the hook `org-cycle-hook'."
  3840. (when (get-buffer-window (current-buffer))
  3841. (cond
  3842. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3843. ; ((eq state 'overview) (org-beginning-of-line))
  3844. ((eq state 'content) nil)
  3845. ((eq state 'all) nil)
  3846. ((eq state 'folded) nil)
  3847. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3848. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3849. (defun org-compact-display-after-subtree-move ()
  3850. (let (beg end)
  3851. (save-excursion
  3852. (if (org-up-heading-safe)
  3853. (progn
  3854. (hide-subtree)
  3855. (show-entry)
  3856. (show-children)
  3857. (org-cycle-show-empty-lines 'children)
  3858. (org-cycle-hide-drawers 'children))
  3859. (org-overview)))))
  3860. (defun org-cycle-show-empty-lines (state)
  3861. "Show empty lines above all visible headlines.
  3862. The region to be covered depends on STATE when called through
  3863. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3864. entire buffer covered. Note that an empty line is only shown if there
  3865. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3866. (when (> org-cycle-separator-lines 0)
  3867. (save-excursion
  3868. (let* ((n org-cycle-separator-lines)
  3869. (re (cond
  3870. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3871. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3872. (t (let ((ns (number-to-string (- n 2))))
  3873. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3874. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3875. beg end)
  3876. (cond
  3877. ((memq state '(overview contents t))
  3878. (setq beg (point-min) end (point-max)))
  3879. ((memq state '(children folded))
  3880. (setq beg (point) end (progn (org-end-of-subtree t t)
  3881. (beginning-of-line 2)
  3882. (point)))))
  3883. (when beg
  3884. (goto-char beg)
  3885. (while (re-search-forward re end t)
  3886. (if (not (get-char-property (match-end 1) 'invisible))
  3887. (outline-flag-region
  3888. (match-beginning 1) (match-end 1) nil)))))))
  3889. ;; Never hide empty lines at the end of the file.
  3890. (save-excursion
  3891. (goto-char (point-max))
  3892. (outline-previous-heading)
  3893. (outline-end-of-heading)
  3894. (if (and (looking-at "[ \t\n]+")
  3895. (= (match-end 0) (point-max)))
  3896. (outline-flag-region (point) (match-end 0) nil))))
  3897. (defun org-show-empty-lines-in-parent ()
  3898. "Move to the parent and re-show empty lines before visible headlines."
  3899. (save-excursion
  3900. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3901. (org-cycle-show-empty-lines context))))
  3902. (defun org-cycle-hide-drawers (state)
  3903. "Re-hide all drawers after a visibility state change."
  3904. (when (and (org-mode-p)
  3905. (not (memq state '(overview folded))))
  3906. (save-excursion
  3907. (let* ((globalp (memq state '(contents all)))
  3908. (beg (if globalp (point-min) (point)))
  3909. (end (if globalp (point-max) (org-end-of-subtree t))))
  3910. (goto-char beg)
  3911. (while (re-search-forward org-drawer-regexp end t)
  3912. (org-flag-drawer t))))))
  3913. (defun org-flag-drawer (flag)
  3914. (save-excursion
  3915. (beginning-of-line 1)
  3916. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3917. (let ((b (match-end 0))
  3918. (outline-regexp org-outline-regexp))
  3919. (if (re-search-forward
  3920. "^[ \t]*:END:"
  3921. (save-excursion (outline-next-heading) (point)) t)
  3922. (outline-flag-region b (point-at-eol) flag)
  3923. (error ":END: line missing"))))))
  3924. (defun org-subtree-end-visible-p ()
  3925. "Is the end of the current subtree visible?"
  3926. (pos-visible-in-window-p
  3927. (save-excursion (org-end-of-subtree t) (point))))
  3928. (defun org-first-headline-recenter (&optional N)
  3929. "Move cursor to the first headline and recenter the headline.
  3930. Optional argument N means, put the headline into the Nth line of the window."
  3931. (goto-char (point-min))
  3932. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3933. (beginning-of-line)
  3934. (recenter (prefix-numeric-value N))))
  3935. ;;; Org-goto
  3936. (defvar org-goto-window-configuration nil)
  3937. (defvar org-goto-marker nil)
  3938. (defvar org-goto-map
  3939. (let ((map (make-sparse-keymap)))
  3940. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3941. (while (setq cmd (pop cmds))
  3942. (substitute-key-definition cmd cmd map global-map)))
  3943. (suppress-keymap map)
  3944. (org-defkey map "\C-m" 'org-goto-ret)
  3945. (org-defkey map [(return)] 'org-goto-ret)
  3946. (org-defkey map [(left)] 'org-goto-left)
  3947. (org-defkey map [(right)] 'org-goto-right)
  3948. (org-defkey map [(control ?g)] 'org-goto-quit)
  3949. (org-defkey map "\C-i" 'org-cycle)
  3950. (org-defkey map [(tab)] 'org-cycle)
  3951. (org-defkey map [(down)] 'outline-next-visible-heading)
  3952. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3953. (if org-goto-auto-isearch
  3954. (if (fboundp 'define-key-after)
  3955. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3956. nil)
  3957. (org-defkey map "q" 'org-goto-quit)
  3958. (org-defkey map "n" 'outline-next-visible-heading)
  3959. (org-defkey map "p" 'outline-previous-visible-heading)
  3960. (org-defkey map "f" 'outline-forward-same-level)
  3961. (org-defkey map "b" 'outline-backward-same-level)
  3962. (org-defkey map "u" 'outline-up-heading))
  3963. (org-defkey map "/" 'org-occur)
  3964. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3965. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3966. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3967. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3968. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3969. map))
  3970. (defconst org-goto-help
  3971. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3972. RET=jump to location [Q]uit and return to previous location
  3973. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3974. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3975. ;; FIXME: Docstring doe not mention both interfaces
  3976. (defun org-goto (&optional alternative-interface)
  3977. "Look up a different location in the current file, keeping current visibility.
  3978. When you want look-up or go to a different location in a document, the
  3979. fastest way is often to fold the entire buffer and then dive into the tree.
  3980. This method has the disadvantage, that the previous location will be folded,
  3981. which may not be what you want.
  3982. This command works around this by showing a copy of the current buffer
  3983. in an indirect buffer, in overview mode. You can dive into the tree in
  3984. that copy, use org-occur and incremental search to find a location.
  3985. When pressing RET or `Q', the command returns to the original buffer in
  3986. which the visibility is still unchanged. After RET is will also jump to
  3987. the location selected in the indirect buffer and expose the
  3988. the headline hierarchy above."
  3989. (interactive "P")
  3990. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3991. (org-refile-use-outline-path t)
  3992. (interface
  3993. (if (not alternative-interface)
  3994. org-goto-interface
  3995. (if (eq org-goto-interface 'outline)
  3996. 'outline-path-completion
  3997. 'outline)))
  3998. (org-goto-start-pos (point))
  3999. (selected-point
  4000. (if (eq interface 'outline)
  4001. (car (org-get-location (current-buffer) org-goto-help))
  4002. (nth 3 (org-refile-get-location "Goto: ")))))
  4003. (if selected-point
  4004. (progn
  4005. (org-mark-ring-push org-goto-start-pos)
  4006. (goto-char selected-point)
  4007. (if (or (org-invisible-p) (org-invisible-p2))
  4008. (org-show-context 'org-goto)))
  4009. (message "Quit"))))
  4010. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4011. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4012. (defvar org-goto-local-auto-isearch-map) ; defined below
  4013. (defun org-get-location (buf help)
  4014. "Let the user select a location in the Org-mode buffer BUF.
  4015. This function uses a recursive edit. It returns the selected position
  4016. or nil."
  4017. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4018. (isearch-hide-immediately nil)
  4019. (isearch-search-fun-function
  4020. (lambda () 'org-goto-local-search-headings))
  4021. (org-goto-selected-point org-goto-exit-command))
  4022. (save-excursion
  4023. (save-window-excursion
  4024. (delete-other-windows)
  4025. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4026. (switch-to-buffer
  4027. (condition-case nil
  4028. (make-indirect-buffer (current-buffer) "*org-goto*")
  4029. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4030. (with-output-to-temp-buffer "*Help*"
  4031. (princ help))
  4032. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4033. (setq buffer-read-only nil)
  4034. (let ((org-startup-truncated t)
  4035. (org-startup-folded nil)
  4036. (org-startup-align-all-tables nil))
  4037. (org-mode)
  4038. (org-overview))
  4039. (setq buffer-read-only t)
  4040. (if (and (boundp 'org-goto-start-pos)
  4041. (integer-or-marker-p org-goto-start-pos))
  4042. (let ((org-show-hierarchy-above t)
  4043. (org-show-siblings t)
  4044. (org-show-following-heading t))
  4045. (goto-char org-goto-start-pos)
  4046. (and (org-invisible-p) (org-show-context)))
  4047. (goto-char (point-min)))
  4048. (org-beginning-of-line)
  4049. (message "Select location and press RET")
  4050. (use-local-map org-goto-map)
  4051. (recursive-edit)
  4052. ))
  4053. (kill-buffer "*org-goto*")
  4054. (cons org-goto-selected-point org-goto-exit-command)))
  4055. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4056. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4057. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4058. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4059. (defun org-goto-local-search-headings (string bound noerror)
  4060. "Search and make sure that any matches are in headlines."
  4061. (catch 'return
  4062. (while (if isearch-forward
  4063. (search-forward string bound noerror)
  4064. (search-backward string bound noerror))
  4065. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4066. (and (member :headline context)
  4067. (not (member :tags context))))
  4068. (throw 'return (point))))))
  4069. (defun org-goto-local-auto-isearch ()
  4070. "Start isearch."
  4071. (interactive)
  4072. (goto-char (point-min))
  4073. (let ((keys (this-command-keys)))
  4074. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4075. (isearch-mode t)
  4076. (isearch-process-search-char (string-to-char keys)))))
  4077. (defun org-goto-ret (&optional arg)
  4078. "Finish `org-goto' by going to the new location."
  4079. (interactive "P")
  4080. (setq org-goto-selected-point (point)
  4081. org-goto-exit-command 'return)
  4082. (throw 'exit nil))
  4083. (defun org-goto-left ()
  4084. "Finish `org-goto' by going to the new location."
  4085. (interactive)
  4086. (if (org-on-heading-p)
  4087. (progn
  4088. (beginning-of-line 1)
  4089. (setq org-goto-selected-point (point)
  4090. org-goto-exit-command 'left)
  4091. (throw 'exit nil))
  4092. (error "Not on a heading")))
  4093. (defun org-goto-right ()
  4094. "Finish `org-goto' by going to the new location."
  4095. (interactive)
  4096. (if (org-on-heading-p)
  4097. (progn
  4098. (setq org-goto-selected-point (point)
  4099. org-goto-exit-command 'right)
  4100. (throw 'exit nil))
  4101. (error "Not on a heading")))
  4102. (defun org-goto-quit ()
  4103. "Finish `org-goto' without cursor motion."
  4104. (interactive)
  4105. (setq org-goto-selected-point nil)
  4106. (setq org-goto-exit-command 'quit)
  4107. (throw 'exit nil))
  4108. ;;; Indirect buffer display of subtrees
  4109. (defvar org-indirect-dedicated-frame nil
  4110. "This is the frame being used for indirect tree display.")
  4111. (defvar org-last-indirect-buffer nil)
  4112. (defun org-tree-to-indirect-buffer (&optional arg)
  4113. "Create indirect buffer and narrow it to current subtree.
  4114. With numerical prefix ARG, go up to this level and then take that tree.
  4115. If ARG is negative, go up that many levels.
  4116. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4117. indirect buffer previously made with this command, to avoid proliferation of
  4118. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4119. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4120. is kept so that you can work with several indirect buffers at the same time.
  4121. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4122. requests that a new frame be made for the new buffer, so that the dedicated
  4123. frame is not changed."
  4124. (interactive "P")
  4125. (let ((cbuf (current-buffer))
  4126. (cwin (selected-window))
  4127. (pos (point))
  4128. beg end level heading ibuf)
  4129. (save-excursion
  4130. (org-back-to-heading t)
  4131. (when (numberp arg)
  4132. (setq level (org-outline-level))
  4133. (if (< arg 0) (setq arg (+ level arg)))
  4134. (while (> (setq level (org-outline-level)) arg)
  4135. (outline-up-heading 1 t)))
  4136. (setq beg (point)
  4137. heading (org-get-heading))
  4138. (org-end-of-subtree t) (setq end (point)))
  4139. (if (and (buffer-live-p org-last-indirect-buffer)
  4140. (not (eq org-indirect-buffer-display 'new-frame))
  4141. (not arg))
  4142. (kill-buffer org-last-indirect-buffer))
  4143. (setq ibuf (org-get-indirect-buffer cbuf)
  4144. org-last-indirect-buffer ibuf)
  4145. (cond
  4146. ((or (eq org-indirect-buffer-display 'new-frame)
  4147. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4148. (select-frame (make-frame))
  4149. (delete-other-windows)
  4150. (switch-to-buffer ibuf)
  4151. (org-set-frame-title heading))
  4152. ((eq org-indirect-buffer-display 'dedicated-frame)
  4153. (raise-frame
  4154. (select-frame (or (and org-indirect-dedicated-frame
  4155. (frame-live-p org-indirect-dedicated-frame)
  4156. org-indirect-dedicated-frame)
  4157. (setq org-indirect-dedicated-frame (make-frame)))))
  4158. (delete-other-windows)
  4159. (switch-to-buffer ibuf)
  4160. (org-set-frame-title (concat "Indirect: " heading)))
  4161. ((eq org-indirect-buffer-display 'current-window)
  4162. (switch-to-buffer ibuf))
  4163. ((eq org-indirect-buffer-display 'other-window)
  4164. (pop-to-buffer ibuf))
  4165. (t (error "Invalid value.")))
  4166. (if (featurep 'xemacs)
  4167. (save-excursion (org-mode) (turn-on-font-lock)))
  4168. (narrow-to-region beg end)
  4169. (show-all)
  4170. (goto-char pos)
  4171. (and (window-live-p cwin) (select-window cwin))))
  4172. (defun org-get-indirect-buffer (&optional buffer)
  4173. (setq buffer (or buffer (current-buffer)))
  4174. (let ((n 1) (base (buffer-name buffer)) bname)
  4175. (while (buffer-live-p
  4176. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4177. (setq n (1+ n)))
  4178. (condition-case nil
  4179. (make-indirect-buffer buffer bname 'clone)
  4180. (error (make-indirect-buffer buffer bname)))))
  4181. (defun org-set-frame-title (title)
  4182. "Set the title of the current frame to the string TITLE."
  4183. ;; FIXME: how to name a single frame in XEmacs???
  4184. (unless (featurep 'xemacs)
  4185. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4186. ;;;; Structure editing
  4187. ;;; Inserting headlines
  4188. (defun org-insert-heading (&optional force-heading)
  4189. "Insert a new heading or item with same depth at point.
  4190. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4191. If point is at the beginning of a headline, insert a sibling before the
  4192. current headline. If point is not at the beginning, do not split the line,
  4193. but create the new headline after the current line."
  4194. (interactive "P")
  4195. (if (= (buffer-size) 0)
  4196. (insert "\n* ")
  4197. (when (or force-heading (not (org-insert-item)))
  4198. (let* ((head (save-excursion
  4199. (condition-case nil
  4200. (progn
  4201. (org-back-to-heading)
  4202. (match-string 0))
  4203. (error "*"))))
  4204. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4205. pos hide-previous previous-pos)
  4206. (cond
  4207. ((and (org-on-heading-p) (bolp)
  4208. (or (bobp)
  4209. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4210. ;; insert before the current line
  4211. (open-line (if blank 2 1)))
  4212. ((and (bolp)
  4213. (or (bobp)
  4214. (save-excursion
  4215. (backward-char 1) (not (org-invisible-p)))))
  4216. ;; insert right here
  4217. nil)
  4218. (t
  4219. ;; somewhere in the line
  4220. (save-excursion
  4221. (setq previous-pos (point-at-bol))
  4222. (end-of-line)
  4223. (setq hide-previous (org-invisible-p)))
  4224. (and org-insert-heading-respect-content (org-show-subtree))
  4225. (let ((split
  4226. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4227. (save-excursion
  4228. (let ((p (point)))
  4229. (goto-char (point-at-bol))
  4230. (and (looking-at org-complex-heading-regexp)
  4231. (> p (match-beginning 4)))))))
  4232. tags pos)
  4233. (cond
  4234. (org-insert-heading-respect-content
  4235. (org-end-of-subtree nil t)
  4236. (or (bolp) (newline))
  4237. (open-line 1))
  4238. ((org-on-heading-p)
  4239. (when hide-previous
  4240. (show-children)
  4241. (org-show-entry))
  4242. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4243. (setq tags (and (match-end 2) (match-string 2)))
  4244. (and (match-end 1)
  4245. (delete-region (match-beginning 1) (match-end 1)))
  4246. (setq pos (point-at-bol))
  4247. (or split (end-of-line 1))
  4248. (delete-horizontal-space)
  4249. (newline (if blank 2 1))
  4250. (when tags
  4251. (save-excursion
  4252. (goto-char pos)
  4253. (end-of-line 1)
  4254. (insert " " tags)
  4255. (org-set-tags nil 'align))))
  4256. (t
  4257. (or split (end-of-line 1))
  4258. (newline (if blank 2 1)))))))
  4259. (insert head) (just-one-space)
  4260. (setq pos (point))
  4261. (end-of-line 1)
  4262. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4263. (when (and org-insert-heading-respect-content hide-previous)
  4264. (save-excursion
  4265. (goto-char previous-pos)
  4266. (hide-subtree)))
  4267. (run-hooks 'org-insert-heading-hook)))))
  4268. (defun org-get-heading (&optional no-tags)
  4269. "Return the heading of the current entry, without the stars."
  4270. (save-excursion
  4271. (org-back-to-heading t)
  4272. (if (looking-at
  4273. (if no-tags
  4274. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4275. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4276. (match-string 1) "")))
  4277. (defun org-insert-heading-after-current ()
  4278. "Insert a new heading with same level as current, after current subtree."
  4279. (interactive)
  4280. (org-back-to-heading)
  4281. (org-insert-heading)
  4282. (org-move-subtree-down)
  4283. (end-of-line 1))
  4284. (defun org-insert-heading-respect-content ()
  4285. (interactive)
  4286. (let ((org-insert-heading-respect-content t))
  4287. (org-insert-heading t)))
  4288. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4289. (interactive "P")
  4290. (let ((org-insert-heading-respect-content t))
  4291. (org-insert-todo-heading force-state t)))
  4292. (defun org-insert-todo-heading (arg &optional force-heading)
  4293. "Insert a new heading with the same level and TODO state as current heading.
  4294. If the heading has no TODO state, or if the state is DONE, use the first
  4295. state (TODO by default). Also with prefix arg, force first state."
  4296. (interactive "P")
  4297. (when (or force-heading (not (org-insert-item 'checkbox)))
  4298. (org-insert-heading force-heading)
  4299. (save-excursion
  4300. (org-back-to-heading)
  4301. (outline-previous-heading)
  4302. (looking-at org-todo-line-regexp))
  4303. (if (or arg
  4304. (not (match-beginning 2))
  4305. (member (match-string 2) org-done-keywords))
  4306. (insert (car org-todo-keywords-1) " ")
  4307. (insert (match-string 2) " "))
  4308. (when org-provide-todo-statistics
  4309. (org-update-parent-todo-statistics))))
  4310. (defun org-insert-subheading (arg)
  4311. "Insert a new subheading and demote it.
  4312. Works for outline headings and for plain lists alike."
  4313. (interactive "P")
  4314. (org-insert-heading arg)
  4315. (cond
  4316. ((org-on-heading-p) (org-do-demote))
  4317. ((org-at-item-p) (org-indent-item 1))))
  4318. (defun org-insert-todo-subheading (arg)
  4319. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4320. Works for outline headings and for plain lists alike."
  4321. (interactive "P")
  4322. (org-insert-todo-heading arg)
  4323. (cond
  4324. ((org-on-heading-p) (org-do-demote))
  4325. ((org-at-item-p) (org-indent-item 1))))
  4326. ;;; Promotion and Demotion
  4327. (defun org-promote-subtree ()
  4328. "Promote the entire subtree.
  4329. See also `org-promote'."
  4330. (interactive)
  4331. (save-excursion
  4332. (org-map-tree 'org-promote))
  4333. (org-fix-position-after-promote))
  4334. (defun org-demote-subtree ()
  4335. "Demote the entire subtree. See `org-demote'.
  4336. See also `org-promote'."
  4337. (interactive)
  4338. (save-excursion
  4339. (org-map-tree 'org-demote))
  4340. (org-fix-position-after-promote))
  4341. (defun org-do-promote ()
  4342. "Promote the current heading higher up the tree.
  4343. If the region is active in `transient-mark-mode', promote all headings
  4344. in the region."
  4345. (interactive)
  4346. (save-excursion
  4347. (if (org-region-active-p)
  4348. (org-map-region 'org-promote (region-beginning) (region-end))
  4349. (org-promote)))
  4350. (org-fix-position-after-promote))
  4351. (defun org-do-demote ()
  4352. "Demote the current heading lower down the tree.
  4353. If the region is active in `transient-mark-mode', demote all headings
  4354. in the region."
  4355. (interactive)
  4356. (save-excursion
  4357. (if (org-region-active-p)
  4358. (org-map-region 'org-demote (region-beginning) (region-end))
  4359. (org-demote)))
  4360. (org-fix-position-after-promote))
  4361. (defun org-fix-position-after-promote ()
  4362. "Make sure that after pro/demotion cursor position is right."
  4363. (let ((pos (point)))
  4364. (when (save-excursion
  4365. (beginning-of-line 1)
  4366. (looking-at org-todo-line-regexp)
  4367. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4368. (cond ((eobp) (insert " "))
  4369. ((eolp) (insert " "))
  4370. ((equal (char-after) ?\ ) (forward-char 1))))))
  4371. (defun org-reduced-level (l)
  4372. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4373. (defun org-get-valid-level (level &optional change)
  4374. "Rectify a level change under the influence of `org-odd-levels-only'
  4375. LEVEL is a current level, CHANGE is by how much the level should be
  4376. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4377. even level numbers will become the next higher odd number."
  4378. (if org-odd-levels-only
  4379. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4380. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4381. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4382. (max 1 (+ level change))))
  4383. (if (boundp 'define-obsolete-function-alias)
  4384. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4385. (define-obsolete-function-alias 'org-get-legal-level
  4386. 'org-get-valid-level)
  4387. (define-obsolete-function-alias 'org-get-legal-level
  4388. 'org-get-valid-level "23.1")))
  4389. (defun org-promote ()
  4390. "Promote the current heading higher up the tree.
  4391. If the region is active in `transient-mark-mode', promote all headings
  4392. in the region."
  4393. (org-back-to-heading t)
  4394. (let* ((level (save-match-data (funcall outline-level)))
  4395. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4396. (diff (abs (- level (length up-head) -1))))
  4397. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4398. (replace-match up-head nil t)
  4399. ;; Fixup tag positioning
  4400. (and org-auto-align-tags (org-set-tags nil t))
  4401. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4402. (defun org-demote ()
  4403. "Demote the current heading lower down the tree.
  4404. If the region is active in `transient-mark-mode', demote all headings
  4405. in the region."
  4406. (org-back-to-heading t)
  4407. (let* ((level (save-match-data (funcall outline-level)))
  4408. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4409. (diff (abs (- level (length down-head) -1))))
  4410. (replace-match down-head nil t)
  4411. ;; Fixup tag positioning
  4412. (and org-auto-align-tags (org-set-tags nil t))
  4413. (if org-adapt-indentation (org-fixup-indentation diff))))
  4414. (defun org-map-tree (fun)
  4415. "Call FUN for every heading underneath the current one."
  4416. (org-back-to-heading)
  4417. (let ((level (funcall outline-level)))
  4418. (save-excursion
  4419. (funcall fun)
  4420. (while (and (progn
  4421. (outline-next-heading)
  4422. (> (funcall outline-level) level))
  4423. (not (eobp)))
  4424. (funcall fun)))))
  4425. (defun org-map-region (fun beg end)
  4426. "Call FUN for every heading between BEG and END."
  4427. (let ((org-ignore-region t))
  4428. (save-excursion
  4429. (setq end (copy-marker end))
  4430. (goto-char beg)
  4431. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4432. (< (point) end))
  4433. (funcall fun))
  4434. (while (and (progn
  4435. (outline-next-heading)
  4436. (< (point) end))
  4437. (not (eobp)))
  4438. (funcall fun)))))
  4439. (defun org-fixup-indentation (diff)
  4440. "Change the indentation in the current entry by DIFF
  4441. However, if any line in the current entry has no indentation, or if it
  4442. would end up with no indentation after the change, nothing at all is done."
  4443. (save-excursion
  4444. (let ((end (save-excursion (outline-next-heading)
  4445. (point-marker)))
  4446. (prohibit (if (> diff 0)
  4447. "^\\S-"
  4448. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4449. col)
  4450. (unless (save-excursion (end-of-line 1)
  4451. (re-search-forward prohibit end t))
  4452. (while (and (< (point) end)
  4453. (re-search-forward "^[ \t]+" end t))
  4454. (goto-char (match-end 0))
  4455. (setq col (current-column))
  4456. (if (< diff 0) (replace-match ""))
  4457. (org-indent-to-column (+ diff col))))
  4458. (move-marker end nil))))
  4459. (defun org-convert-to-odd-levels ()
  4460. "Convert an org-mode file with all levels allowed to one with odd levels.
  4461. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4462. level 5 etc."
  4463. (interactive)
  4464. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4465. (let ((org-odd-levels-only nil) n)
  4466. (save-excursion
  4467. (goto-char (point-min))
  4468. (while (re-search-forward "^\\*\\*+ " nil t)
  4469. (setq n (- (length (match-string 0)) 2))
  4470. (while (>= (setq n (1- n)) 0)
  4471. (org-demote))
  4472. (end-of-line 1))))))
  4473. (defun org-convert-to-oddeven-levels ()
  4474. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4475. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4476. section with an even level, conversion would destroy the structure of the file. An error
  4477. is signaled in this case."
  4478. (interactive)
  4479. (goto-char (point-min))
  4480. ;; First check if there are no even levels
  4481. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4482. (org-show-context t)
  4483. (error "Not all levels are odd in this file. Conversion not possible."))
  4484. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4485. (let ((org-odd-levels-only nil) n)
  4486. (save-excursion
  4487. (goto-char (point-min))
  4488. (while (re-search-forward "^\\*\\*+ " nil t)
  4489. (setq n (/ (1- (length (match-string 0))) 2))
  4490. (while (>= (setq n (1- n)) 0)
  4491. (org-promote))
  4492. (end-of-line 1))))))
  4493. (defun org-tr-level (n)
  4494. "Make N odd if required."
  4495. (if org-odd-levels-only (1+ (/ n 2)) n))
  4496. ;;; Vertical tree motion, cutting and pasting of subtrees
  4497. (defun org-move-subtree-up (&optional arg)
  4498. "Move the current subtree up past ARG headlines of the same level."
  4499. (interactive "p")
  4500. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4501. (defun org-move-subtree-down (&optional arg)
  4502. "Move the current subtree down past ARG headlines of the same level."
  4503. (interactive "p")
  4504. (setq arg (prefix-numeric-value arg))
  4505. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4506. 'outline-get-last-sibling))
  4507. (ins-point (make-marker))
  4508. (cnt (abs arg))
  4509. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4510. ;; Select the tree
  4511. (org-back-to-heading)
  4512. (setq beg0 (point))
  4513. (save-excursion
  4514. (setq ne-beg (org-back-over-empty-lines))
  4515. (setq beg (point)))
  4516. (save-match-data
  4517. (save-excursion (outline-end-of-heading)
  4518. (setq folded (org-invisible-p)))
  4519. (outline-end-of-subtree))
  4520. (outline-next-heading)
  4521. (setq ne-end (org-back-over-empty-lines))
  4522. (setq end (point))
  4523. (goto-char beg0)
  4524. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4525. ;; include less whitespace
  4526. (save-excursion
  4527. (goto-char beg)
  4528. (forward-line (- ne-beg ne-end))
  4529. (setq beg (point))))
  4530. ;; Find insertion point, with error handling
  4531. (while (> cnt 0)
  4532. (or (and (funcall movfunc) (looking-at outline-regexp))
  4533. (progn (goto-char beg0)
  4534. (error "Cannot move past superior level or buffer limit")))
  4535. (setq cnt (1- cnt)))
  4536. (if (> arg 0)
  4537. ;; Moving forward - still need to move over subtree
  4538. (progn (org-end-of-subtree t t)
  4539. (save-excursion
  4540. (org-back-over-empty-lines)
  4541. (or (bolp) (newline)))))
  4542. (setq ne-ins (org-back-over-empty-lines))
  4543. (move-marker ins-point (point))
  4544. (setq txt (buffer-substring beg end))
  4545. (org-save-markers-in-region beg end)
  4546. (delete-region beg end)
  4547. (outline-flag-region (1- beg) beg nil)
  4548. (outline-flag-region (1- (point)) (point) nil)
  4549. (let ((bbb (point)))
  4550. (insert-before-markers txt)
  4551. (org-reinstall-markers-in-region bbb)
  4552. (move-marker ins-point bbb))
  4553. (or (bolp) (insert "\n"))
  4554. (setq ins-end (point))
  4555. (goto-char ins-point)
  4556. (org-skip-whitespace)
  4557. (when (and (< arg 0)
  4558. (org-first-sibling-p)
  4559. (> ne-ins ne-beg))
  4560. ;; Move whitespace back to beginning
  4561. (save-excursion
  4562. (goto-char ins-end)
  4563. (let ((kill-whole-line t))
  4564. (kill-line (- ne-ins ne-beg)) (point)))
  4565. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4566. (move-marker ins-point nil)
  4567. (org-compact-display-after-subtree-move)
  4568. (org-show-empty-lines-in-parent)
  4569. (unless folded
  4570. (org-show-entry)
  4571. (show-children)
  4572. (org-cycle-hide-drawers 'children))))
  4573. (defvar org-subtree-clip ""
  4574. "Clipboard for cut and paste of subtrees.
  4575. This is actually only a copy of the kill, because we use the normal kill
  4576. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4577. (defvar org-subtree-clip-folded nil
  4578. "Was the last copied subtree folded?
  4579. This is used to fold the tree back after pasting.")
  4580. (defun org-cut-subtree (&optional n)
  4581. "Cut the current subtree into the clipboard.
  4582. With prefix arg N, cut this many sequential subtrees.
  4583. This is a short-hand for marking the subtree and then cutting it."
  4584. (interactive "p")
  4585. (org-copy-subtree n 'cut))
  4586. (defun org-copy-subtree (&optional n cut force-store-markers)
  4587. "Cut the current subtree into the clipboard.
  4588. With prefix arg N, cut this many sequential subtrees.
  4589. This is a short-hand for marking the subtree and then copying it.
  4590. If CUT is non-nil, actually cut the subtree.
  4591. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4592. of some markers in the region, even if CUT is non-nil. This is
  4593. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4594. (interactive "p")
  4595. (let (beg end folded (beg0 (point)))
  4596. (if (interactive-p)
  4597. (org-back-to-heading nil) ; take what looks like a subtree
  4598. (org-back-to-heading t)) ; take what is really there
  4599. (org-back-over-empty-lines)
  4600. (setq beg (point))
  4601. (skip-chars-forward " \t\r\n")
  4602. (save-match-data
  4603. (save-excursion (outline-end-of-heading)
  4604. (setq folded (org-invisible-p)))
  4605. (condition-case nil
  4606. (outline-forward-same-level (1- n))
  4607. (error nil))
  4608. (org-end-of-subtree t t))
  4609. (org-back-over-empty-lines)
  4610. (setq end (point))
  4611. (goto-char beg0)
  4612. (when (> end beg)
  4613. (setq org-subtree-clip-folded folded)
  4614. (when (or cut force-store-markers)
  4615. (org-save-markers-in-region beg end))
  4616. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4617. (setq org-subtree-clip (current-kill 0))
  4618. (message "%s: Subtree(s) with %d characters"
  4619. (if cut "Cut" "Copied")
  4620. (length org-subtree-clip)))))
  4621. (defun org-paste-subtree (&optional level tree for-yank)
  4622. "Paste the clipboard as a subtree, with modification of headline level.
  4623. The entire subtree is promoted or demoted in order to match a new headline
  4624. level.
  4625. If the cursor is at the beginning of a headline, the same level as
  4626. that headline is used to paste the tree
  4627. If not, the new level is derived from the *visible* headings
  4628. before and after the insertion point, and taken to be the inferior headline
  4629. level of the two. So if the previous visible heading is level 3 and the
  4630. next is level 4 (or vice versa), level 4 will be used for insertion.
  4631. This makes sure that the subtree remains an independent subtree and does
  4632. not swallow low level entries.
  4633. You can also force a different level, either by using a numeric prefix
  4634. argument, or by inserting the heading marker by hand. For example, if the
  4635. cursor is after \"*****\", then the tree will be shifted to level 5.
  4636. If optional TREE is given, use this text instead of the kill ring.
  4637. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4638. move back over whitespace before inserting, and move point to the end of
  4639. the inserted text when done."
  4640. (interactive "P")
  4641. (unless (org-kill-is-subtree-p tree)
  4642. (error "%s"
  4643. (substitute-command-keys
  4644. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4645. (let* ((visp (not (org-invisible-p)))
  4646. (txt (or tree (and kill-ring (current-kill 0))))
  4647. (^re (concat "^\\(" outline-regexp "\\)"))
  4648. (re (concat "\\(" outline-regexp "\\)"))
  4649. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4650. (old-level (if (string-match ^re txt)
  4651. (- (match-end 0) (match-beginning 0) 1)
  4652. -1))
  4653. (force-level (cond (level (prefix-numeric-value level))
  4654. ((and (looking-at "[ \t]*$")
  4655. (string-match
  4656. ^re_ (buffer-substring
  4657. (point-at-bol) (point))))
  4658. (- (match-end 1) (match-beginning 1)))
  4659. ((and (bolp)
  4660. (looking-at org-outline-regexp))
  4661. (- (match-end 0) (point) 1))
  4662. (t nil)))
  4663. (previous-level (save-excursion
  4664. (condition-case nil
  4665. (progn
  4666. (outline-previous-visible-heading 1)
  4667. (if (looking-at re)
  4668. (- (match-end 0) (match-beginning 0) 1)
  4669. 1))
  4670. (error 1))))
  4671. (next-level (save-excursion
  4672. (condition-case nil
  4673. (progn
  4674. (or (looking-at outline-regexp)
  4675. (outline-next-visible-heading 1))
  4676. (if (looking-at re)
  4677. (- (match-end 0) (match-beginning 0) 1)
  4678. 1))
  4679. (error 1))))
  4680. (new-level (or force-level (max previous-level next-level)))
  4681. (shift (if (or (= old-level -1)
  4682. (= new-level -1)
  4683. (= old-level new-level))
  4684. 0
  4685. (- new-level old-level)))
  4686. (delta (if (> shift 0) -1 1))
  4687. (func (if (> shift 0) 'org-demote 'org-promote))
  4688. (org-odd-levels-only nil)
  4689. beg end newend)
  4690. ;; Remove the forced level indicator
  4691. (if force-level
  4692. (delete-region (point-at-bol) (point)))
  4693. ;; Paste
  4694. (beginning-of-line 1)
  4695. (unless for-yank (org-back-over-empty-lines))
  4696. (setq beg (point))
  4697. (insert-before-markers txt)
  4698. (unless (string-match "\n\\'" txt) (insert "\n"))
  4699. (setq newend (point))
  4700. (org-reinstall-markers-in-region beg)
  4701. (setq end (point))
  4702. (goto-char beg)
  4703. (skip-chars-forward " \t\n\r")
  4704. (setq beg (point))
  4705. (if (and (org-invisible-p) visp)
  4706. (save-excursion (outline-show-heading)))
  4707. ;; Shift if necessary
  4708. (unless (= shift 0)
  4709. (save-restriction
  4710. (narrow-to-region beg end)
  4711. (while (not (= shift 0))
  4712. (org-map-region func (point-min) (point-max))
  4713. (setq shift (+ delta shift)))
  4714. (goto-char (point-min))
  4715. (setq newend (point-max))))
  4716. (when (or (interactive-p) for-yank)
  4717. (message "Clipboard pasted as level %d subtree" new-level))
  4718. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4719. kill-ring
  4720. (eq org-subtree-clip (current-kill 0))
  4721. org-subtree-clip-folded)
  4722. ;; The tree was folded before it was killed/copied
  4723. (hide-subtree))
  4724. (and for-yank (goto-char newend))))
  4725. (defun org-kill-is-subtree-p (&optional txt)
  4726. "Check if the current kill is an outline subtree, or a set of trees.
  4727. Returns nil if kill does not start with a headline, or if the first
  4728. headline level is not the largest headline level in the tree.
  4729. So this will actually accept several entries of equal levels as well,
  4730. which is OK for `org-paste-subtree'.
  4731. If optional TXT is given, check this string instead of the current kill."
  4732. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4733. (start-level (and kill
  4734. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4735. org-outline-regexp "\\)")
  4736. kill)
  4737. (- (match-end 2) (match-beginning 2) 1)))
  4738. (re (concat "^" org-outline-regexp))
  4739. (start (1+ (or (match-beginning 2) -1))))
  4740. (if (not start-level)
  4741. (progn
  4742. nil) ;; does not even start with a heading
  4743. (catch 'exit
  4744. (while (setq start (string-match re kill (1+ start)))
  4745. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4746. (throw 'exit nil)))
  4747. t))))
  4748. (defvar org-markers-to-move nil
  4749. "Markers that should be moved with a cut-and-paste operation.
  4750. Those markers are stored together with their positions relative to
  4751. the start of the region.")
  4752. (defun org-save-markers-in-region (beg end)
  4753. "Check markers in region.
  4754. If these markers are between BEG and END, record their position relative
  4755. to BEG, so that after moving the block of text, we can put the markers back
  4756. into place.
  4757. This function gets called just before an entry or tree gets cut from the
  4758. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4759. called immediately, to move the markers with the entries."
  4760. (setq org-markers-to-move nil)
  4761. (when (featurep 'org-clock)
  4762. (org-clock-save-markers-for-cut-and-paste beg end))
  4763. (when (featurep 'org-agenda)
  4764. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4765. (defun org-check-and-save-marker (marker beg end)
  4766. "Check if MARKER is between BEG and END.
  4767. If yes, remember the marker and the distance to BEG."
  4768. (when (and (marker-buffer marker)
  4769. (equal (marker-buffer marker) (current-buffer)))
  4770. (if (and (>= marker beg) (< marker end))
  4771. (push (cons marker (- marker beg)) org-markers-to-move))))
  4772. (defun org-reinstall-markers-in-region (beg)
  4773. "Move all remembered markers to their position relative to BEG."
  4774. (mapc (lambda (x)
  4775. (move-marker (car x) (+ beg (cdr x))))
  4776. org-markers-to-move)
  4777. (setq org-markers-to-move nil))
  4778. (defun org-narrow-to-subtree ()
  4779. "Narrow buffer to the current subtree."
  4780. (interactive)
  4781. (save-excursion
  4782. (save-match-data
  4783. (narrow-to-region
  4784. (progn (org-back-to-heading) (point))
  4785. (progn (org-end-of-subtree t) (point))))))
  4786. ;;; Outline Sorting
  4787. (defun org-sort (with-case)
  4788. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4789. Optional argument WITH-CASE means sort case-sensitively."
  4790. (interactive "P")
  4791. (if (org-at-table-p)
  4792. (org-call-with-arg 'org-table-sort-lines with-case)
  4793. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4794. (defun org-sort-remove-invisible (s)
  4795. (remove-text-properties 0 (length s) org-rm-props s)
  4796. (while (string-match org-bracket-link-regexp s)
  4797. (setq s (replace-match (if (match-end 2)
  4798. (match-string 3 s)
  4799. (match-string 1 s)) t t s)))
  4800. s)
  4801. (defvar org-priority-regexp) ; defined later in the file
  4802. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4803. "Sort entries on a certain level of an outline tree.
  4804. If there is an active region, the entries in the region are sorted.
  4805. Else, if the cursor is before the first entry, sort the top-level items.
  4806. Else, the children of the entry at point are sorted.
  4807. Sorting can be alphabetically, numerically, and by date/time as given by
  4808. the first time stamp in the entry. The command prompts for the sorting
  4809. type unless it has been given to the function through the SORTING-TYPE
  4810. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4811. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4812. called with point at the beginning of the record. It must return either
  4813. a string or a number that should serve as the sorting key for that record.
  4814. Comparing entries ignores case by default. However, with an optional argument
  4815. WITH-CASE, the sorting considers case as well."
  4816. (interactive "P")
  4817. (let ((case-func (if with-case 'identity 'downcase))
  4818. start beg end stars re re2
  4819. txt what tmp plain-list-p)
  4820. ;; Find beginning and end of region to sort
  4821. (cond
  4822. ((org-region-active-p)
  4823. ;; we will sort the region
  4824. (setq end (region-end)
  4825. what "region")
  4826. (goto-char (region-beginning))
  4827. (if (not (org-on-heading-p)) (outline-next-heading))
  4828. (setq start (point)))
  4829. ((org-at-item-p)
  4830. ;; we will sort this plain list
  4831. (org-beginning-of-item-list) (setq start (point))
  4832. (org-end-of-item-list) (setq end (point))
  4833. (goto-char start)
  4834. (setq plain-list-p t
  4835. what "plain list"))
  4836. ((or (org-on-heading-p)
  4837. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4838. ;; we will sort the children of the current headline
  4839. (org-back-to-heading)
  4840. (setq start (point)
  4841. end (progn (org-end-of-subtree t t)
  4842. (org-back-over-empty-lines)
  4843. (point))
  4844. what "children")
  4845. (goto-char start)
  4846. (show-subtree)
  4847. (outline-next-heading))
  4848. (t
  4849. ;; we will sort the top-level entries in this file
  4850. (goto-char (point-min))
  4851. (or (org-on-heading-p) (outline-next-heading))
  4852. (setq start (point) end (point-max) what "top-level")
  4853. (goto-char start)
  4854. (show-all)))
  4855. (setq beg (point))
  4856. (if (>= beg end) (error "Nothing to sort"))
  4857. (unless plain-list-p
  4858. (looking-at "\\(\\*+\\)")
  4859. (setq stars (match-string 1)
  4860. re (concat "^" (regexp-quote stars) " +")
  4861. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4862. txt (buffer-substring beg end))
  4863. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4864. (if (and (not (equal stars "*")) (string-match re2 txt))
  4865. (error "Region to sort contains a level above the first entry")))
  4866. (unless sorting-type
  4867. (message
  4868. (if plain-list-p
  4869. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4870. "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:")
  4871. what)
  4872. (setq sorting-type (read-char-exclusive))
  4873. (and (= (downcase sorting-type) ?f)
  4874. (setq getkey-func
  4875. (org-ido-completing-read "Sort using function: "
  4876. obarray 'fboundp t nil nil))
  4877. (setq getkey-func (intern getkey-func)))
  4878. (and (= (downcase sorting-type) ?r)
  4879. (setq property
  4880. (org-ido-completing-read "Property: "
  4881. (mapcar 'list (org-buffer-property-keys t))
  4882. nil t))))
  4883. (message "Sorting entries...")
  4884. (save-restriction
  4885. (narrow-to-region start end)
  4886. (let ((dcst (downcase sorting-type))
  4887. (now (current-time)))
  4888. (sort-subr
  4889. (/= dcst sorting-type)
  4890. ;; This function moves to the beginning character of the "record" to
  4891. ;; be sorted.
  4892. (if plain-list-p
  4893. (lambda nil
  4894. (if (org-at-item-p) t (goto-char (point-max))))
  4895. (lambda nil
  4896. (if (re-search-forward re nil t)
  4897. (goto-char (match-beginning 0))
  4898. (goto-char (point-max)))))
  4899. ;; This function moves to the last character of the "record" being
  4900. ;; sorted.
  4901. (if plain-list-p
  4902. 'org-end-of-item
  4903. (lambda nil
  4904. (save-match-data
  4905. (condition-case nil
  4906. (outline-forward-same-level 1)
  4907. (error
  4908. (goto-char (point-max)))))))
  4909. ;; This function returns the value that gets sorted against.
  4910. (if plain-list-p
  4911. (lambda nil
  4912. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4913. (cond
  4914. ((= dcst ?n)
  4915. (string-to-number (buffer-substring (match-end 0)
  4916. (point-at-eol))))
  4917. ((= dcst ?a)
  4918. (buffer-substring (match-end 0) (point-at-eol)))
  4919. ((= dcst ?t)
  4920. (if (re-search-forward org-ts-regexp
  4921. (point-at-eol) t)
  4922. (org-time-string-to-time (match-string 0))
  4923. now))
  4924. ((= dcst ?f)
  4925. (if getkey-func
  4926. (progn
  4927. (setq tmp (funcall getkey-func))
  4928. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4929. tmp)
  4930. (error "Invalid key function `%s'" getkey-func)))
  4931. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4932. (lambda nil
  4933. (cond
  4934. ((= dcst ?n)
  4935. (if (looking-at org-complex-heading-regexp)
  4936. (string-to-number (match-string 4))
  4937. nil))
  4938. ((= dcst ?a)
  4939. (if (looking-at org-complex-heading-regexp)
  4940. (funcall case-func (match-string 4))
  4941. nil))
  4942. ((= dcst ?t)
  4943. (if (re-search-forward org-ts-regexp
  4944. (save-excursion
  4945. (forward-line 2)
  4946. (point)) t)
  4947. (org-time-string-to-time (match-string 0))
  4948. now))
  4949. ((= dcst ?p)
  4950. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4951. (string-to-char (match-string 2))
  4952. org-default-priority))
  4953. ((= dcst ?r)
  4954. (or (org-entry-get nil property) ""))
  4955. ((= dcst ?o)
  4956. (if (looking-at org-complex-heading-regexp)
  4957. (- 9999 (length (member (match-string 2)
  4958. org-todo-keywords-1)))))
  4959. ((= dcst ?f)
  4960. (if getkey-func
  4961. (progn
  4962. (setq tmp (funcall getkey-func))
  4963. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4964. tmp)
  4965. (error "Invalid key function `%s'" getkey-func)))
  4966. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4967. nil
  4968. (cond
  4969. ((= dcst ?a) 'string<)
  4970. ((= dcst ?t) 'time-less-p)
  4971. (t nil)))))
  4972. (message "Sorting entries...done")))
  4973. (defun org-do-sort (table what &optional with-case sorting-type)
  4974. "Sort TABLE of WHAT according to SORTING-TYPE.
  4975. The user will be prompted for the SORTING-TYPE if the call to this
  4976. function does not specify it. WHAT is only for the prompt, to indicate
  4977. what is being sorted. The sorting key will be extracted from
  4978. the car of the elements of the table.
  4979. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4980. (unless sorting-type
  4981. (message
  4982. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4983. what)
  4984. (setq sorting-type (read-char-exclusive)))
  4985. (let ((dcst (downcase sorting-type))
  4986. extractfun comparefun)
  4987. ;; Define the appropriate functions
  4988. (cond
  4989. ((= dcst ?n)
  4990. (setq extractfun 'string-to-number
  4991. comparefun (if (= dcst sorting-type) '< '>)))
  4992. ((= dcst ?a)
  4993. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4994. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4995. comparefun (if (= dcst sorting-type)
  4996. 'string<
  4997. (lambda (a b) (and (not (string< a b))
  4998. (not (string= a b)))))))
  4999. ((= dcst ?t)
  5000. (setq extractfun
  5001. (lambda (x)
  5002. (if (string-match org-ts-regexp x)
  5003. (time-to-seconds
  5004. (org-time-string-to-time (match-string 0 x)))
  5005. 0))
  5006. comparefun (if (= dcst sorting-type) '< '>)))
  5007. (t (error "Invalid sorting type `%c'" sorting-type)))
  5008. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5009. table)
  5010. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5011. ;;; Editing source examples
  5012. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5013. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5014. (defvar org-edit-src-force-single-line nil)
  5015. (defvar org-edit-src-from-org-mode nil)
  5016. (defvar org-edit-src-picture nil)
  5017. (define-minor-mode org-exit-edit-mode
  5018. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5019. (defun org-edit-src-code ()
  5020. "Edit the source code example at point.
  5021. An indirect buffer is created, and that buffer is then narrowed to the
  5022. example at point and switched to the correct language mode. When done,
  5023. exit by killing the buffer with \\[org-edit-src-exit]."
  5024. (interactive)
  5025. (let ((line (org-current-line))
  5026. (case-fold-search t)
  5027. (msg (substitute-command-keys
  5028. "Edit, then exit with C-c ' (C-c and single quote)"))
  5029. (info (org-edit-src-find-region-and-lang))
  5030. (org-mode-p (eq major-mode 'org-mode))
  5031. beg end lang lang-f single)
  5032. (if (not info)
  5033. nil
  5034. (setq beg (nth 0 info)
  5035. end (nth 1 info)
  5036. lang (nth 2 info)
  5037. single (nth 3 info)
  5038. lang-f (intern (concat lang "-mode")))
  5039. (unless (functionp lang-f)
  5040. (error "No such language mode: %s" lang-f))
  5041. (goto-line line)
  5042. (if (get-buffer "*Org Edit Src Example*")
  5043. (kill-buffer "*Org Edit Src Example*"))
  5044. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5045. "*Org Edit Src Example*"))
  5046. (narrow-to-region beg end)
  5047. (remove-text-properties beg end '(display nil invisible nil
  5048. intangible nil))
  5049. (let ((org-inhibit-startup t))
  5050. (funcall lang-f))
  5051. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5052. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5053. (when org-mode-p
  5054. (goto-char (point-min))
  5055. (while (re-search-forward "^," nil t)
  5056. (replace-match "")))
  5057. (goto-line line)
  5058. (org-exit-edit-mode)
  5059. (org-set-local 'header-line-format msg)
  5060. (message "%s" msg)
  5061. t)))
  5062. (defun org-edit-fixed-width-region ()
  5063. "Edit the fixed-width ascii drawing at point.
  5064. This must be a region where each line starts with ca colon followed by
  5065. a space character.
  5066. An indirect buffer is created, and that buffer is then narrowed to the
  5067. example at point and switched to artist-mode. When done,
  5068. exit by killing the buffer with \\[org-edit-src-exit]."
  5069. (interactive)
  5070. (let ((line (org-current-line))
  5071. (case-fold-search t)
  5072. (msg (substitute-command-keys
  5073. "Edit, then exit with C-c ' (C-c and single quote)"))
  5074. (org-mode-p (eq major-mode 'org-mode))
  5075. beg end lang lang-f)
  5076. (beginning-of-line 1)
  5077. (if (looking-at "[ \t]*[^:\n \t]")
  5078. nil
  5079. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5080. (setq beg (point) end beg)
  5081. (save-excursion
  5082. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5083. (setq beg (point-at-bol 2))
  5084. (setq beg (point))))
  5085. (save-excursion
  5086. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5087. (setq end (1- (match-beginning 0)))
  5088. (setq end (point))))
  5089. (goto-line line))
  5090. (if (get-buffer "*Org Edit Picture*")
  5091. (kill-buffer "*Org Edit Picture*"))
  5092. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5093. "*Org Edit Picture*"))
  5094. (narrow-to-region beg end)
  5095. (remove-text-properties beg end '(display nil invisible nil
  5096. intangible nil))
  5097. (when (fboundp 'font-lock-unfontify-region)
  5098. (font-lock-unfontify-region (point-min) (point-max)))
  5099. (cond
  5100. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5101. (fundamental-mode)
  5102. (artist-mode 1))
  5103. (t (funcall org-edit-fixed-width-region-mode)))
  5104. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5105. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5106. (set (make-local-variable 'org-edit-src-picture) t)
  5107. (goto-char (point-min))
  5108. (while (re-search-forward "^[ \t]*: ?" nil t)
  5109. (replace-match ""))
  5110. (goto-line line)
  5111. (org-exit-edit-mode)
  5112. (org-set-local 'header-line-format msg)
  5113. (message "%s" msg)
  5114. t)))
  5115. (defun org-edit-src-find-region-and-lang ()
  5116. "Find the region and language for a local edit.
  5117. Return a list with beginning and end of the region, a string representing
  5118. the language, a switch telling of the content should be in a single line."
  5119. (let ((re-list
  5120. (append
  5121. org-edit-src-region-extra
  5122. '(
  5123. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5124. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5125. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5126. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5127. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5128. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5129. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5130. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5131. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5132. ("^#\\+html:" "\n" "html" single-line)
  5133. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5134. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5135. ("^#\\+latex:" "\n" "latex" single-line)
  5136. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5137. ("^#\\+ascii:" "\n" "ascii" single-line)
  5138. )))
  5139. (pos (point))
  5140. re re1 re2 single beg end lang)
  5141. (catch 'exit
  5142. (while (setq entry (pop re-list))
  5143. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5144. single (nth 3 entry))
  5145. (save-excursion
  5146. (if (or (looking-at re1)
  5147. (re-search-backward re1 nil t))
  5148. (progn
  5149. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5150. (if (and (re-search-forward re2 nil t)
  5151. (>= (match-end 0) pos))
  5152. (throw 'exit (list beg (match-beginning 0) lang single))))
  5153. (if (or (looking-at re2)
  5154. (re-search-forward re2 nil t))
  5155. (progn
  5156. (setq end (match-beginning 0))
  5157. (if (and (re-search-backward re1 nil t)
  5158. (<= (match-beginning 0) pos))
  5159. (throw 'exit
  5160. (list (match-end 0) end
  5161. (org-edit-src-get-lang lang) single)))))))))))
  5162. (defun org-edit-src-get-lang (lang)
  5163. "Extract the src language."
  5164. (let ((m (match-string 0)))
  5165. (cond
  5166. ((stringp lang) lang)
  5167. ((integerp lang) (match-string lang))
  5168. ((and (eq lang 'lang)
  5169. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5170. (match-string 1 m))
  5171. ((and (eq lang 'style)
  5172. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5173. (match-string 1 m))
  5174. (t "fundamental"))))
  5175. (defun org-edit-src-exit ()
  5176. "Exit special edit and protect problematic lines."
  5177. (interactive)
  5178. (unless (buffer-base-buffer (current-buffer))
  5179. (error "This is not an indirect buffer, something is wrong..."))
  5180. (unless (> (point-min) 1)
  5181. (error "This buffer is not narrowed, something is wrong..."))
  5182. (goto-char (point-min))
  5183. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5184. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5185. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5186. (goto-char (point-min))
  5187. (while (re-search-forward "\n" nil t)
  5188. (replace-match " "))
  5189. (goto-char (point-min))
  5190. (if (looking-at "\\s-*") (replace-match " "))
  5191. (if (re-search-forward "\\s-+\\'" nil t)
  5192. (replace-match "")))
  5193. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5194. (goto-char (point-min))
  5195. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5196. (replace-match ",\\1"))
  5197. (when font-lock-mode
  5198. (font-lock-unfontify-region (point-min) (point-max)))
  5199. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5200. (when (org-bound-and-true-p org-edit-src-picture)
  5201. (untabify (point-min) (point-max))
  5202. (goto-char (point-min))
  5203. (while (re-search-forward "^" nil t)
  5204. (replace-match ": "))
  5205. (when font-lock-mode
  5206. (font-lock-unfontify-region (point-min) (point-max)))
  5207. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5208. (kill-buffer (current-buffer))
  5209. (and (org-mode-p) (org-restart-font-lock)))
  5210. ;;; The orgstruct minor mode
  5211. ;; Define a minor mode which can be used in other modes in order to
  5212. ;; integrate the org-mode structure editing commands.
  5213. ;; This is really a hack, because the org-mode structure commands use
  5214. ;; keys which normally belong to the major mode. Here is how it
  5215. ;; works: The minor mode defines all the keys necessary to operate the
  5216. ;; structure commands, but wraps the commands into a function which
  5217. ;; tests if the cursor is currently at a headline or a plain list
  5218. ;; item. If that is the case, the structure command is used,
  5219. ;; temporarily setting many Org-mode variables like regular
  5220. ;; expressions for filling etc. However, when any of those keys is
  5221. ;; used at a different location, function uses `key-binding' to look
  5222. ;; up if the key has an associated command in another currently active
  5223. ;; keymap (minor modes, major mode, global), and executes that
  5224. ;; command. There might be problems if any of the keys is otherwise
  5225. ;; used as a prefix key.
  5226. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5227. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5228. ;; addresses this by checking explicitly for both bindings.
  5229. (defvar orgstruct-mode-map (make-sparse-keymap)
  5230. "Keymap for the minor `orgstruct-mode'.")
  5231. (defvar org-local-vars nil
  5232. "List of local variables, for use by `orgstruct-mode'")
  5233. ;;;###autoload
  5234. (define-minor-mode orgstruct-mode
  5235. "Toggle the minor more `orgstruct-mode'.
  5236. This mode is for using Org-mode structure commands in other modes.
  5237. The following key behave as if Org-mode was active, if the cursor
  5238. is on a headline, or on a plain list item (both in the definition
  5239. of Org-mode).
  5240. M-up Move entry/item up
  5241. M-down Move entry/item down
  5242. M-left Promote
  5243. M-right Demote
  5244. M-S-up Move entry/item up
  5245. M-S-down Move entry/item down
  5246. M-S-left Promote subtree
  5247. M-S-right Demote subtree
  5248. M-q Fill paragraph and items like in Org-mode
  5249. C-c ^ Sort entries
  5250. C-c - Cycle list bullet
  5251. TAB Cycle item visibility
  5252. M-RET Insert new heading/item
  5253. S-M-RET Insert new TODO heading / Chekbox item
  5254. C-c C-c Set tags / toggle checkbox"
  5255. nil " OrgStruct" nil
  5256. (org-load-modules-maybe)
  5257. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5258. ;;;###autoload
  5259. (defun turn-on-orgstruct ()
  5260. "Unconditionally turn on `orgstruct-mode'."
  5261. (orgstruct-mode 1))
  5262. ;;;###autoload
  5263. (defun turn-on-orgstruct++ ()
  5264. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5265. In addition to setting orgstruct-mode, this also exports all indentation and
  5266. autofilling variables from org-mode into the buffer. Note that turning
  5267. off orgstruct-mode will *not* remove these additional settings."
  5268. (orgstruct-mode 1)
  5269. (let (var val)
  5270. (mapc
  5271. (lambda (x)
  5272. (when (string-match
  5273. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5274. (symbol-name (car x)))
  5275. (setq var (car x) val (nth 1 x))
  5276. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5277. org-local-vars)))
  5278. (defun orgstruct-error ()
  5279. "Error when there is no default binding for a structure key."
  5280. (interactive)
  5281. (error "This key has no function outside structure elements"))
  5282. (defun orgstruct-setup ()
  5283. "Setup orgstruct keymaps."
  5284. (let ((nfunc 0)
  5285. (bindings
  5286. (list
  5287. '([(meta up)] org-metaup)
  5288. '([(meta down)] org-metadown)
  5289. '([(meta left)] org-metaleft)
  5290. '([(meta right)] org-metaright)
  5291. '([(meta shift up)] org-shiftmetaup)
  5292. '([(meta shift down)] org-shiftmetadown)
  5293. '([(meta shift left)] org-shiftmetaleft)
  5294. '([(meta shift right)] org-shiftmetaright)
  5295. '([(shift up)] org-shiftup)
  5296. '([(shift down)] org-shiftdown)
  5297. '([(shift left)] org-shiftleft)
  5298. '([(shift right)] org-shiftright)
  5299. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5300. '("\M-q" fill-paragraph)
  5301. '("\C-c^" org-sort)
  5302. '("\C-c-" org-cycle-list-bullet)))
  5303. elt key fun cmd)
  5304. (while (setq elt (pop bindings))
  5305. (setq nfunc (1+ nfunc))
  5306. (setq key (org-key (car elt))
  5307. fun (nth 1 elt)
  5308. cmd (orgstruct-make-binding fun nfunc key))
  5309. (org-defkey orgstruct-mode-map key cmd))
  5310. ;; Special treatment needed for TAB and RET
  5311. (org-defkey orgstruct-mode-map [(tab)]
  5312. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5313. (org-defkey orgstruct-mode-map "\C-i"
  5314. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5315. (org-defkey orgstruct-mode-map "\M-\C-m"
  5316. (orgstruct-make-binding 'org-insert-heading 105
  5317. "\M-\C-m" [(meta return)]))
  5318. (org-defkey orgstruct-mode-map [(meta return)]
  5319. (orgstruct-make-binding 'org-insert-heading 106
  5320. [(meta return)] "\M-\C-m"))
  5321. (org-defkey orgstruct-mode-map [(shift meta return)]
  5322. (orgstruct-make-binding 'org-insert-todo-heading 107
  5323. [(meta return)] "\M-\C-m"))
  5324. (unless org-local-vars
  5325. (setq org-local-vars (org-get-local-variables)))
  5326. t))
  5327. (defun orgstruct-make-binding (fun n &rest keys)
  5328. "Create a function for binding in the structure minor mode.
  5329. FUN is the command to call inside a table. N is used to create a unique
  5330. command name. KEYS are keys that should be checked in for a command
  5331. to execute outside of tables."
  5332. (eval
  5333. (list 'defun
  5334. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5335. '(arg)
  5336. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5337. "Outside of structure, run the binding of `"
  5338. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5339. "'.")
  5340. '(interactive "p")
  5341. (list 'if
  5342. '(org-context-p 'headline 'item)
  5343. (list 'org-run-like-in-org-mode (list 'quote fun))
  5344. (list 'let '(orgstruct-mode)
  5345. (list 'call-interactively
  5346. (append '(or)
  5347. (mapcar (lambda (k)
  5348. (list 'key-binding k))
  5349. keys)
  5350. '('orgstruct-error))))))))
  5351. (defun org-context-p (&rest contexts)
  5352. "Check if local context is any of CONTEXTS.
  5353. Possible values in the list of contexts are `table', `headline', and `item'."
  5354. (let ((pos (point)))
  5355. (goto-char (point-at-bol))
  5356. (prog1 (or (and (memq 'table contexts)
  5357. (looking-at "[ \t]*|"))
  5358. (and (memq 'headline contexts)
  5359. ;;????????? (looking-at "\\*+"))
  5360. (looking-at outline-regexp))
  5361. (and (memq 'item contexts)
  5362. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5363. (goto-char pos))))
  5364. (defun org-get-local-variables ()
  5365. "Return a list of all local variables in an org-mode buffer."
  5366. (let (varlist)
  5367. (with-current-buffer (get-buffer-create "*Org tmp*")
  5368. (erase-buffer)
  5369. (org-mode)
  5370. (setq varlist (buffer-local-variables)))
  5371. (kill-buffer "*Org tmp*")
  5372. (delq nil
  5373. (mapcar
  5374. (lambda (x)
  5375. (setq x
  5376. (if (symbolp x)
  5377. (list x)
  5378. (list (car x) (list 'quote (cdr x)))))
  5379. (if (string-match
  5380. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5381. (symbol-name (car x)))
  5382. x nil))
  5383. varlist))))
  5384. ;;;###autoload
  5385. (defun org-run-like-in-org-mode (cmd)
  5386. (org-load-modules-maybe)
  5387. (unless org-local-vars
  5388. (setq org-local-vars (org-get-local-variables)))
  5389. (eval (list 'let org-local-vars
  5390. (list 'call-interactively (list 'quote cmd)))))
  5391. ;;;; Archiving
  5392. (defun org-get-category (&optional pos)
  5393. "Get the category applying to position POS."
  5394. (get-text-property (or pos (point)) 'org-category))
  5395. (defun org-refresh-category-properties ()
  5396. "Refresh category text properties in the buffer."
  5397. (let ((def-cat (cond
  5398. ((null org-category)
  5399. (if buffer-file-name
  5400. (file-name-sans-extension
  5401. (file-name-nondirectory buffer-file-name))
  5402. "???"))
  5403. ((symbolp org-category) (symbol-name org-category))
  5404. (t org-category)))
  5405. beg end cat pos optionp)
  5406. (org-unmodified
  5407. (save-excursion
  5408. (save-restriction
  5409. (widen)
  5410. (goto-char (point-min))
  5411. (put-text-property (point) (point-max) 'org-category def-cat)
  5412. (while (re-search-forward
  5413. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5414. (setq pos (match-end 0)
  5415. optionp (equal (char-after (match-beginning 0)) ?#)
  5416. cat (org-trim (match-string 2)))
  5417. (if optionp
  5418. (setq beg (point-at-bol) end (point-max))
  5419. (org-back-to-heading t)
  5420. (setq beg (point) end (org-end-of-subtree t t)))
  5421. (put-text-property beg end 'org-category cat)
  5422. (goto-char pos)))))))
  5423. ;;;; Link Stuff
  5424. ;;; Link abbreviations
  5425. (defun org-link-expand-abbrev (link)
  5426. "Apply replacements as defined in `org-link-abbrev-alist."
  5427. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5428. (let* ((key (match-string 1 link))
  5429. (as (or (assoc key org-link-abbrev-alist-local)
  5430. (assoc key org-link-abbrev-alist)))
  5431. (tag (and (match-end 2) (match-string 3 link)))
  5432. rpl)
  5433. (if (not as)
  5434. link
  5435. (setq rpl (cdr as))
  5436. (cond
  5437. ((symbolp rpl) (funcall rpl tag))
  5438. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5439. ((string-match "%h" rpl)
  5440. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5441. (t (concat rpl tag)))))
  5442. link))
  5443. ;;; Storing and inserting links
  5444. (defvar org-insert-link-history nil
  5445. "Minibuffer history for links inserted with `org-insert-link'.")
  5446. (defvar org-stored-links nil
  5447. "Contains the links stored with `org-store-link'.")
  5448. (defvar org-store-link-plist nil
  5449. "Plist with info about the most recently link created with `org-store-link'.")
  5450. (defvar org-link-protocols nil
  5451. "Link protocols added to Org-mode using `org-add-link-type'.")
  5452. (defvar org-store-link-functions nil
  5453. "List of functions that are called to create and store a link.
  5454. Each function will be called in turn until one returns a non-nil
  5455. value. Each function should check if it is responsible for creating
  5456. this link (for example by looking at the major mode).
  5457. If not, it must exit and return nil.
  5458. If yes, it should return a non-nil value after a calling
  5459. `org-store-link-props' with a list of properties and values.
  5460. Special properties are:
  5461. :type The link prefix. like \"http\". This must be given.
  5462. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5463. This is obligatory as well.
  5464. :description Optional default description for the second pair
  5465. of brackets in an Org-mode link. The user can still change
  5466. this when inserting this link into an Org-mode buffer.
  5467. In addition to these, any additional properties can be specified
  5468. and then used in remember templates.")
  5469. (defun org-add-link-type (type &optional follow export)
  5470. "Add TYPE to the list of `org-link-types'.
  5471. Re-compute all regular expressions depending on `org-link-types'
  5472. FOLLOW and EXPORT are two functions.
  5473. FOLLOW should take the link path as the single argument and do whatever
  5474. is necessary to follow the link, for example find a file or display
  5475. a mail message.
  5476. EXPORT should format the link path for export to one of the export formats.
  5477. It should be a function accepting three arguments:
  5478. path the path of the link, the text after the prefix (like \"http:\")
  5479. desc the description of the link, if any, nil if there was no descripton
  5480. format the export format, a symbol like `html' or `latex'.
  5481. The function may use the FORMAT information to return different values
  5482. depending on the format. The return value will be put literally into
  5483. the exported file.
  5484. Org-mode has a built-in default for exporting links. If you are happy with
  5485. this default, there is no need to define an export function for the link
  5486. type. For a simple example of an export function, see `org-bbdb.el'."
  5487. (add-to-list 'org-link-types type t)
  5488. (org-make-link-regexps)
  5489. (if (assoc type org-link-protocols)
  5490. (setcdr (assoc type org-link-protocols) (list follow export))
  5491. (push (list type follow export) org-link-protocols)))
  5492. ;;;###autoload
  5493. (defun org-store-link (arg)
  5494. "\\<org-mode-map>Store an org-link to the current location.
  5495. This link is added to `org-stored-links' and can later be inserted
  5496. into an org-buffer with \\[org-insert-link].
  5497. For some link types, a prefix arg is interpreted:
  5498. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5499. For file links, arg negates `org-context-in-file-links'."
  5500. (interactive "P")
  5501. (org-load-modules-maybe)
  5502. (setq org-store-link-plist nil) ; reset
  5503. (let (link cpltxt desc description search txt)
  5504. (cond
  5505. ((run-hook-with-args-until-success 'org-store-link-functions)
  5506. (setq link (plist-get org-store-link-plist :link)
  5507. desc (or (plist-get org-store-link-plist :description) link)))
  5508. ((eq major-mode 'calendar-mode)
  5509. (let ((cd (calendar-cursor-to-date)))
  5510. (setq link
  5511. (format-time-string
  5512. (car org-time-stamp-formats)
  5513. (apply 'encode-time
  5514. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5515. nil nil nil))))
  5516. (org-store-link-props :type "calendar" :date cd)))
  5517. ((eq major-mode 'w3-mode)
  5518. (setq cpltxt (url-view-url t)
  5519. link (org-make-link cpltxt))
  5520. (org-store-link-props :type "w3" :url (url-view-url t)))
  5521. ((eq major-mode 'w3m-mode)
  5522. (setq cpltxt (or w3m-current-title w3m-current-url)
  5523. link (org-make-link w3m-current-url))
  5524. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5525. ((setq search (run-hook-with-args-until-success
  5526. 'org-create-file-search-functions))
  5527. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5528. "::" search))
  5529. (setq cpltxt (or description link)))
  5530. ((eq major-mode 'image-mode)
  5531. (setq cpltxt (concat "file:"
  5532. (abbreviate-file-name buffer-file-name))
  5533. link (org-make-link cpltxt))
  5534. (org-store-link-props :type "image" :file buffer-file-name))
  5535. ((eq major-mode 'dired-mode)
  5536. ;; link to the file in the current line
  5537. (setq cpltxt (concat "file:"
  5538. (abbreviate-file-name
  5539. (expand-file-name
  5540. (dired-get-filename nil t))))
  5541. link (org-make-link cpltxt)))
  5542. ((and buffer-file-name (org-mode-p))
  5543. ;; Just link to current headline
  5544. (setq cpltxt (concat "file:"
  5545. (abbreviate-file-name buffer-file-name)))
  5546. ;; Add a context search string
  5547. (when (org-xor org-context-in-file-links arg)
  5548. ;; Check if we are on a target
  5549. (if (org-in-regexp "<<\\(.*?\\)>>")
  5550. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5551. (setq txt (cond
  5552. ((org-on-heading-p) nil)
  5553. ((org-region-active-p)
  5554. (buffer-substring (region-beginning) (region-end)))
  5555. (t nil)))
  5556. (when (or (null txt) (string-match "\\S-" txt))
  5557. (setq cpltxt
  5558. (concat cpltxt "::"
  5559. (condition-case nil
  5560. (org-make-org-heading-search-string txt)
  5561. (error "")))
  5562. desc "NONE"))))
  5563. (if (string-match "::\\'" cpltxt)
  5564. (setq cpltxt (substring cpltxt 0 -2)))
  5565. (setq link (org-make-link cpltxt)))
  5566. ((buffer-file-name (buffer-base-buffer))
  5567. ;; Just link to this file here.
  5568. (setq cpltxt (concat "file:"
  5569. (abbreviate-file-name
  5570. (buffer-file-name (buffer-base-buffer)))))
  5571. ;; Add a context string
  5572. (when (org-xor org-context-in-file-links arg)
  5573. (setq txt (if (org-region-active-p)
  5574. (buffer-substring (region-beginning) (region-end))
  5575. (buffer-substring (point-at-bol) (point-at-eol))))
  5576. ;; Only use search option if there is some text.
  5577. (when (string-match "\\S-" txt)
  5578. (setq cpltxt
  5579. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5580. desc "NONE")))
  5581. (setq link (org-make-link cpltxt)))
  5582. ((interactive-p)
  5583. (error "Cannot link to a buffer which is not visiting a file"))
  5584. (t (setq link nil)))
  5585. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5586. (setq link (or link cpltxt)
  5587. desc (or desc cpltxt))
  5588. (if (equal desc "NONE") (setq desc nil))
  5589. (if (and (interactive-p) link)
  5590. (progn
  5591. (setq org-stored-links
  5592. (cons (list link desc) org-stored-links))
  5593. (message "Stored: %s" (or desc link)))
  5594. (and link (org-make-link-string link desc)))))
  5595. (defun org-store-link-props (&rest plist)
  5596. "Store link properties, extract names and addresses."
  5597. (let (x adr)
  5598. (when (setq x (plist-get plist :from))
  5599. (setq adr (mail-extract-address-components x))
  5600. (setq plist (plist-put plist :fromname (car adr)))
  5601. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5602. (when (setq x (plist-get plist :to))
  5603. (setq adr (mail-extract-address-components x))
  5604. (setq plist (plist-put plist :toname (car adr)))
  5605. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5606. (let ((from (plist-get plist :from))
  5607. (to (plist-get plist :to)))
  5608. (when (and from to org-from-is-user-regexp)
  5609. (setq plist
  5610. (plist-put plist :fromto
  5611. (if (string-match org-from-is-user-regexp from)
  5612. (concat "to %t")
  5613. (concat "from %f"))))))
  5614. (setq org-store-link-plist plist))
  5615. (defun org-add-link-props (&rest plist)
  5616. "Add these properties to the link property list."
  5617. (let (key value)
  5618. (while plist
  5619. (setq key (pop plist) value (pop plist))
  5620. (setq org-store-link-plist
  5621. (plist-put org-store-link-plist key value)))))
  5622. (defun org-email-link-description (&optional fmt)
  5623. "Return the description part of an email link.
  5624. This takes information from `org-store-link-plist' and formats it
  5625. according to FMT (default from `org-email-link-description-format')."
  5626. (setq fmt (or fmt org-email-link-description-format))
  5627. (let* ((p org-store-link-plist)
  5628. (to (plist-get p :toaddress))
  5629. (from (plist-get p :fromaddress))
  5630. (table
  5631. (list
  5632. (cons "%c" (plist-get p :fromto))
  5633. (cons "%F" (plist-get p :from))
  5634. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5635. (cons "%T" (plist-get p :to))
  5636. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5637. (cons "%s" (plist-get p :subject))
  5638. (cons "%m" (plist-get p :message-id)))))
  5639. (when (string-match "%c" fmt)
  5640. ;; Check if the user wrote this message
  5641. (if (and org-from-is-user-regexp from to
  5642. (save-match-data (string-match org-from-is-user-regexp from)))
  5643. (setq fmt (replace-match "to %t" t t fmt))
  5644. (setq fmt (replace-match "from %f" t t fmt))))
  5645. (org-replace-escapes fmt table)))
  5646. (defun org-make-org-heading-search-string (&optional string heading)
  5647. "Make search string for STRING or current headline."
  5648. (interactive)
  5649. (let ((s (or string (org-get-heading))))
  5650. (unless (and string (not heading))
  5651. ;; We are using a headline, clean up garbage in there.
  5652. (if (string-match org-todo-regexp s)
  5653. (setq s (replace-match "" t t s)))
  5654. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5655. (setq s (replace-match "" t t s)))
  5656. (setq s (org-trim s))
  5657. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5658. org-comment-string "\\)") s)
  5659. (setq s (replace-match "" t t s)))
  5660. (while (string-match org-ts-regexp s)
  5661. (setq s (replace-match "" t t s))))
  5662. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5663. (setq s (replace-match " " t t s)))
  5664. (or string (setq s (concat "*" s))) ; Add * for headlines
  5665. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5666. (defun org-make-link (&rest strings)
  5667. "Concatenate STRINGS."
  5668. (apply 'concat strings))
  5669. (defun org-make-link-string (link &optional description)
  5670. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5671. (unless (string-match "\\S-" link)
  5672. (error "Empty link"))
  5673. (when (stringp description)
  5674. ;; Remove brackets from the description, they are fatal.
  5675. (while (string-match "\\[" description)
  5676. (setq description (replace-match "{" t t description)))
  5677. (while (string-match "\\]" description)
  5678. (setq description (replace-match "}" t t description))))
  5679. (when (equal (org-link-escape link) description)
  5680. ;; No description needed, it is identical
  5681. (setq description nil))
  5682. (when (and (not description)
  5683. (not (equal link (org-link-escape link))))
  5684. (setq description (org-extract-attributes link)))
  5685. (concat "[[" (org-link-escape link) "]"
  5686. (if description (concat "[" description "]") "")
  5687. "]"))
  5688. (defconst org-link-escape-chars
  5689. '((?\ . "%20")
  5690. (?\[ . "%5B")
  5691. (?\] . "%5D")
  5692. (?\340 . "%E0") ; `a
  5693. (?\342 . "%E2") ; ^a
  5694. (?\347 . "%E7") ; ,c
  5695. (?\350 . "%E8") ; `e
  5696. (?\351 . "%E9") ; 'e
  5697. (?\352 . "%EA") ; ^e
  5698. (?\356 . "%EE") ; ^i
  5699. (?\364 . "%F4") ; ^o
  5700. (?\371 . "%F9") ; `u
  5701. (?\373 . "%FB") ; ^u
  5702. (?\; . "%3B")
  5703. (?? . "%3F")
  5704. (?= . "%3D")
  5705. (?+ . "%2B")
  5706. )
  5707. "Association list of escapes for some characters problematic in links.
  5708. This is the list that is used for internal purposes.")
  5709. (defconst org-link-escape-chars-browser
  5710. '((?\ . "%20")) ; 32 for the SPC char
  5711. "Association list of escapes for some characters problematic in links.
  5712. This is the list that is used before handing over to the browser.")
  5713. (defun org-link-escape (text &optional table)
  5714. "Escape charaters in TEXT that are problematic for links."
  5715. (setq table (or table org-link-escape-chars))
  5716. (when text
  5717. (let ((re (mapconcat (lambda (x) (regexp-quote
  5718. (char-to-string (car x))))
  5719. table "\\|")))
  5720. (while (string-match re text)
  5721. (setq text
  5722. (replace-match
  5723. (cdr (assoc (string-to-char (match-string 0 text))
  5724. table))
  5725. t t text)))
  5726. text)))
  5727. (defun org-link-unescape (text &optional table)
  5728. "Reverse the action of `org-link-escape'."
  5729. (setq table (or table org-link-escape-chars))
  5730. (when text
  5731. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5732. table "\\|")))
  5733. (while (string-match re text)
  5734. (setq text
  5735. (replace-match
  5736. (char-to-string (car (rassoc (match-string 0 text) table)))
  5737. t t text)))
  5738. text)))
  5739. (defun org-xor (a b)
  5740. "Exclusive or."
  5741. (if a (not b) b))
  5742. (defun org-get-header (header)
  5743. "Find a header field in the current buffer."
  5744. (save-excursion
  5745. (goto-char (point-min))
  5746. (let ((case-fold-search t) s)
  5747. (cond
  5748. ((eq header 'from)
  5749. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5750. (setq s (match-string 1)))
  5751. (while (string-match "\"" s)
  5752. (setq s (replace-match "" t t s)))
  5753. (if (string-match "[<(].*" s)
  5754. (setq s (replace-match "" t t s))))
  5755. ((eq header 'message-id)
  5756. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5757. (setq s (match-string 1))))
  5758. ((eq header 'subject)
  5759. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5760. (setq s (match-string 1)))))
  5761. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5762. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5763. s)))
  5764. (defun org-fixup-message-id-for-http (s)
  5765. "Replace special characters in a message id, so it can be used in an http query."
  5766. (while (string-match "<" s)
  5767. (setq s (replace-match "%3C" t t s)))
  5768. (while (string-match ">" s)
  5769. (setq s (replace-match "%3E" t t s)))
  5770. (while (string-match "@" s)
  5771. (setq s (replace-match "%40" t t s)))
  5772. s)
  5773. ;;;###autoload
  5774. (defun org-insert-link-global ()
  5775. "Insert a link like Org-mode does.
  5776. This command can be called in any mode to insert a link in Org-mode syntax."
  5777. (interactive)
  5778. (org-load-modules-maybe)
  5779. (org-run-like-in-org-mode 'org-insert-link))
  5780. (defun org-insert-link (&optional complete-file link-location)
  5781. "Insert a link. At the prompt, enter the link.
  5782. Completion can be used to insert any of the link protocol prefixes like
  5783. http or ftp in use.
  5784. The history can be used to select a link previously stored with
  5785. `org-store-link'. When the empty string is entered (i.e. if you just
  5786. press RET at the prompt), the link defaults to the most recently
  5787. stored link. As SPC triggers completion in the minibuffer, you need to
  5788. use M-SPC or C-q SPC to force the insertion of a space character.
  5789. You will also be prompted for a description, and if one is given, it will
  5790. be displayed in the buffer instead of the link.
  5791. If there is already a link at point, this command will allow you to edit link
  5792. and description parts.
  5793. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5794. be selected using completion. The path to the file will be relative to the
  5795. current directory if the file is in the current directory or a subdirectory.
  5796. Otherwise, the link will be the absolute path as completed in the minibuffer
  5797. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5798. option `org-link-file-path-type'.
  5799. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5800. the current directory or below.
  5801. With three \\[universal-argument] prefixes, negate the meaning of
  5802. `org-keep-stored-link-after-insertion'.
  5803. If `org-make-link-description-function' is non-nil, this function will be
  5804. called with the link target, and the result will be the default
  5805. link description.
  5806. If the LINK-LOCATION parameter is non-nil, this value will be
  5807. used as the link location instead of reading one interactively."
  5808. (interactive "P")
  5809. (let* ((wcf (current-window-configuration))
  5810. (region (if (org-region-active-p)
  5811. (buffer-substring (region-beginning) (region-end))))
  5812. (remove (and region (list (region-beginning) (region-end))))
  5813. (desc region)
  5814. tmphist ; byte-compile incorrectly complains about this
  5815. (link link-location)
  5816. entry file)
  5817. (cond
  5818. (link-location) ; specified by arg, just use it.
  5819. ((org-in-regexp org-bracket-link-regexp 1)
  5820. ;; We do have a link at point, and we are going to edit it.
  5821. (setq remove (list (match-beginning 0) (match-end 0)))
  5822. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5823. (setq link (read-string "Link: "
  5824. (org-link-unescape
  5825. (org-match-string-no-properties 1)))))
  5826. ((or (org-in-regexp org-angle-link-re)
  5827. (org-in-regexp org-plain-link-re))
  5828. ;; Convert to bracket link
  5829. (setq remove (list (match-beginning 0) (match-end 0))
  5830. link (read-string "Link: "
  5831. (org-remove-angle-brackets (match-string 0)))))
  5832. ((member complete-file '((4) (16)))
  5833. ;; Completing read for file names.
  5834. (setq file (read-file-name "File: "))
  5835. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5836. (pwd1 (file-name-as-directory (abbreviate-file-name
  5837. (expand-file-name ".")))))
  5838. (cond
  5839. ((equal complete-file '(16))
  5840. (setq link (org-make-link
  5841. "file:"
  5842. (abbreviate-file-name (expand-file-name file)))))
  5843. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5844. (setq link (org-make-link "file:" (match-string 1 file))))
  5845. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5846. (expand-file-name file))
  5847. (setq link (org-make-link
  5848. "file:" (match-string 1 (expand-file-name file)))))
  5849. (t (setq link (org-make-link "file:" file))))))
  5850. (t
  5851. ;; Read link, with completion for stored links.
  5852. (with-output-to-temp-buffer "*Org Links*"
  5853. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5854. (when org-stored-links
  5855. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5856. (princ (mapconcat
  5857. (lambda (x)
  5858. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5859. (reverse org-stored-links) "\n"))))
  5860. (let ((cw (selected-window)))
  5861. (select-window (get-buffer-window "*Org Links*"))
  5862. (org-fit-window-to-buffer)
  5863. (setq truncate-lines t)
  5864. (select-window cw))
  5865. ;; Fake a link history, containing the stored links.
  5866. (setq tmphist (append (mapcar 'car org-stored-links)
  5867. org-insert-link-history))
  5868. (unwind-protect
  5869. (setq link (org-completing-read
  5870. "Link: "
  5871. (append
  5872. (mapcar (lambda (x) (list (concat (car x) ":")))
  5873. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5874. (mapcar (lambda (x) (list (concat x ":")))
  5875. org-link-types))
  5876. nil nil nil
  5877. 'tmphist
  5878. (or (car (car org-stored-links)))))
  5879. (set-window-configuration wcf)
  5880. (kill-buffer "*Org Links*"))
  5881. (setq entry (assoc link org-stored-links))
  5882. (or entry (push link org-insert-link-history))
  5883. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5884. (not org-keep-stored-link-after-insertion))
  5885. (setq org-stored-links (delq (assoc link org-stored-links)
  5886. org-stored-links)))
  5887. (setq desc (or desc (nth 1 entry)))))
  5888. (if (string-match org-plain-link-re link)
  5889. ;; URL-like link, normalize the use of angular brackets.
  5890. (setq link (org-make-link (org-remove-angle-brackets link))))
  5891. ;; Check if we are linking to the current file with a search option
  5892. ;; If yes, simplify the link by using only the search option.
  5893. (when (and buffer-file-name
  5894. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  5895. (let* ((path (match-string 1 link))
  5896. (case-fold-search nil)
  5897. (search (match-string 2 link)))
  5898. (save-match-data
  5899. (if (equal (file-truename buffer-file-name) (file-truename path))
  5900. ;; We are linking to this same file, with a search option
  5901. (setq link search)))))
  5902. ;; Check if we can/should use a relative path. If yes, simplify the link
  5903. (when (string-match "^file:\\(.*\\)" link)
  5904. (let* ((path (match-string 1 link))
  5905. (origpath path)
  5906. (case-fold-search nil))
  5907. (cond
  5908. ((or (eq org-link-file-path-type 'absolute)
  5909. (equal complete-file '(16)))
  5910. (setq path (abbreviate-file-name (expand-file-name path))))
  5911. ((eq org-link-file-path-type 'noabbrev)
  5912. (setq path (expand-file-name path)))
  5913. ((eq org-link-file-path-type 'relative)
  5914. (setq path (file-relative-name path)))
  5915. (t
  5916. (save-match-data
  5917. (if (string-match (concat "^" (regexp-quote
  5918. (file-name-as-directory
  5919. (expand-file-name "."))))
  5920. (expand-file-name path))
  5921. ;; We are linking a file with relative path name.
  5922. (setq path (substring (expand-file-name path)
  5923. (match-end 0)))
  5924. (setq path (abbreviate-file-name (expand-file-name path)))))))
  5925. (setq link (concat "file:" path))
  5926. (if (equal desc origpath)
  5927. (setq desc path))))
  5928. (if org-make-link-description-function
  5929. (setq desc (funcall org-make-link-description-function link desc)))
  5930. (setq desc (read-string "Description: " desc))
  5931. (unless (string-match "\\S-" desc) (setq desc nil))
  5932. (if remove (apply 'delete-region remove))
  5933. (insert (org-make-link-string link desc))))
  5934. (defun org-completing-read (&rest args)
  5935. "Completing-read with SPACE being a normal character."
  5936. (let ((minibuffer-local-completion-map
  5937. (copy-keymap minibuffer-local-completion-map)))
  5938. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5939. (apply 'org-ido-completing-read args)))
  5940. (defun org-ido-completing-read (&rest args)
  5941. "Completing-read using `ido-mode' speedups if available"
  5942. (if (and org-completion-use-ido
  5943. (fboundp 'ido-completing-read)
  5944. (boundp 'ido-mode) ido-mode
  5945. (listp (second args)))
  5946. (apply 'ido-completing-read (concat (car args)) (cdr args))
  5947. (apply 'completing-read args)))
  5948. (defun org-extract-attributes (s)
  5949. "Extract the attributes cookie from a string and set as text property."
  5950. (let (a attr (start 0) key value)
  5951. (save-match-data
  5952. (when (string-match "{{\\([^}]+\\)}}$" s)
  5953. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  5954. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  5955. (setq key (match-string 1 a) value (match-string 2 a)
  5956. start (match-end 0)
  5957. attr (plist-put attr (intern key) value))))
  5958. (org-add-props s nil 'org-attributes attr))
  5959. s))
  5960. (defun org-attributes-to-string (plist)
  5961. "Format a property list into an HTML attribute list."
  5962. (let ((s "") key value)
  5963. (while plist
  5964. (setq key (pop plist) value (pop plist))
  5965. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  5966. s))
  5967. ;;; Opening/following a link
  5968. (defvar org-link-search-failed nil)
  5969. (defun org-next-link ()
  5970. "Move forward to the next link.
  5971. If the link is in hidden text, expose it."
  5972. (interactive)
  5973. (when (and org-link-search-failed (eq this-command last-command))
  5974. (goto-char (point-min))
  5975. (message "Link search wrapped back to beginning of buffer"))
  5976. (setq org-link-search-failed nil)
  5977. (let* ((pos (point))
  5978. (ct (org-context))
  5979. (a (assoc :link ct)))
  5980. (if a (goto-char (nth 2 a)))
  5981. (if (re-search-forward org-any-link-re nil t)
  5982. (progn
  5983. (goto-char (match-beginning 0))
  5984. (if (org-invisible-p) (org-show-context)))
  5985. (goto-char pos)
  5986. (setq org-link-search-failed t)
  5987. (error "No further link found"))))
  5988. (defun org-previous-link ()
  5989. "Move backward to the previous link.
  5990. If the link is in hidden text, expose it."
  5991. (interactive)
  5992. (when (and org-link-search-failed (eq this-command last-command))
  5993. (goto-char (point-max))
  5994. (message "Link search wrapped back to end of buffer"))
  5995. (setq org-link-search-failed nil)
  5996. (let* ((pos (point))
  5997. (ct (org-context))
  5998. (a (assoc :link ct)))
  5999. (if a (goto-char (nth 1 a)))
  6000. (if (re-search-backward org-any-link-re nil t)
  6001. (progn
  6002. (goto-char (match-beginning 0))
  6003. (if (org-invisible-p) (org-show-context)))
  6004. (goto-char pos)
  6005. (setq org-link-search-failed t)
  6006. (error "No further link found"))))
  6007. (defun org-translate-link (s)
  6008. "Translate a link string if a translation function has been defined."
  6009. (if (and org-link-translation-function
  6010. (fboundp org-link-translation-function)
  6011. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6012. (progn
  6013. (setq s (funcall org-link-translation-function
  6014. (match-string 1) (match-string 2)))
  6015. (concat (car s) ":" (cdr s)))
  6016. s))
  6017. (defun org-translate-link-from-planner (type path)
  6018. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6019. This is still an experimental function, your mileage may vary."
  6020. (cond
  6021. ((member type '("http" "https" "news" "ftp"))
  6022. ;; standard Internet links are the same.
  6023. nil)
  6024. ((and (equal type "irc") (string-match "^//" path))
  6025. ;; Planner has two / at the beginning of an irc link, we have 1.
  6026. ;; We should have zero, actually....
  6027. (setq path (substring path 1)))
  6028. ((and (equal type "lisp") (string-match "^/" path))
  6029. ;; Planner has a slash, we do not.
  6030. (setq type "elisp" path (substring path 1)))
  6031. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6032. ;; A typical message link. Planner has the id after the fina slash,
  6033. ;; we separate it with a hash mark
  6034. (setq path (concat (match-string 1 path) "#"
  6035. (org-remove-angle-brackets (match-string 2 path)))))
  6036. )
  6037. (cons type path))
  6038. (defun org-find-file-at-mouse (ev)
  6039. "Open file link or URL at mouse."
  6040. (interactive "e")
  6041. (mouse-set-point ev)
  6042. (org-open-at-point 'in-emacs))
  6043. (defun org-open-at-mouse (ev)
  6044. "Open file link or URL at mouse."
  6045. (interactive "e")
  6046. (mouse-set-point ev)
  6047. (if (eq major-mode 'org-agenda-mode)
  6048. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6049. (org-open-at-point))
  6050. (defvar org-window-config-before-follow-link nil
  6051. "The window configuration before following a link.
  6052. This is saved in case the need arises to restore it.")
  6053. (defvar org-open-link-marker (make-marker)
  6054. "Marker pointing to the location where `org-open-at-point; was called.")
  6055. ;;;###autoload
  6056. (defun org-open-at-point-global ()
  6057. "Follow a link like Org-mode does.
  6058. This command can be called in any mode to follow a link that has
  6059. Org-mode syntax."
  6060. (interactive)
  6061. (org-run-like-in-org-mode 'org-open-at-point))
  6062. ;;;###autoload
  6063. (defun org-open-link-from-string (s &optional arg)
  6064. "Open a link in the string S, as if it was in Org-mode."
  6065. (interactive "sLink: \nP")
  6066. (with-temp-buffer
  6067. (let ((org-inhibit-startup t))
  6068. (org-mode)
  6069. (insert s)
  6070. (goto-char (point-min))
  6071. (org-open-at-point arg))))
  6072. (defun org-open-at-point (&optional in-emacs)
  6073. "Open link at or after point.
  6074. If there is no link at point, this function will search forward up to
  6075. the end of the current subtree.
  6076. Normally, files will be opened by an appropriate application. If the
  6077. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6078. With a double prefix argument, try to open outside of Emacs, in the
  6079. application the system uses for this file type."
  6080. (interactive "P")
  6081. (org-load-modules-maybe)
  6082. (move-marker org-open-link-marker (point))
  6083. (setq org-window-config-before-follow-link (current-window-configuration))
  6084. (org-remove-occur-highlights nil nil t)
  6085. (if (org-at-timestamp-p t)
  6086. (org-follow-timestamp-link)
  6087. (let (type path link line search (pos (point)))
  6088. (catch 'match
  6089. (save-excursion
  6090. (skip-chars-forward "^]\n\r")
  6091. (when (org-in-regexp org-bracket-link-regexp)
  6092. (setq link (org-extract-attributes
  6093. (org-link-unescape (org-match-string-no-properties 1))))
  6094. (while (string-match " *\n *" link)
  6095. (setq link (replace-match " " t t link)))
  6096. (setq link (org-link-expand-abbrev link))
  6097. (cond
  6098. ((or (file-name-absolute-p link)
  6099. (string-match "^\\.\\.?/" link))
  6100. (setq type "file" path link))
  6101. ((string-match org-link-re-with-space3 link)
  6102. (setq type (match-string 1 link) path (match-string 2 link)))
  6103. (t (setq type "thisfile" path link)))
  6104. (throw 'match t)))
  6105. (when (get-text-property (point) 'org-linked-text)
  6106. (setq type "thisfile"
  6107. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6108. (1+ (point)) (point))
  6109. path (buffer-substring
  6110. (previous-single-property-change pos 'org-linked-text)
  6111. (next-single-property-change pos 'org-linked-text)))
  6112. (throw 'match t))
  6113. (save-excursion
  6114. (when (or (org-in-regexp org-angle-link-re)
  6115. (org-in-regexp org-plain-link-re))
  6116. (setq type (match-string 1) path (match-string 2))
  6117. (throw 'match t)))
  6118. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6119. (setq type "tree-match"
  6120. path (match-string 1))
  6121. (throw 'match t))
  6122. (save-excursion
  6123. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6124. (setq type "tags"
  6125. path (match-string 1))
  6126. (while (string-match ":" path)
  6127. (setq path (replace-match "+" t t path)))
  6128. (throw 'match t))))
  6129. (unless path
  6130. (error "No link found"))
  6131. ;; Remove any trailing spaces in path
  6132. (if (string-match " +\\'" path)
  6133. (setq path (replace-match "" t t path)))
  6134. (if (and org-link-translation-function
  6135. (fboundp org-link-translation-function))
  6136. ;; Check if we need to translate the link
  6137. (let ((tmp (funcall org-link-translation-function type path)))
  6138. (setq type (car tmp) path (cdr tmp))))
  6139. (cond
  6140. ((assoc type org-link-protocols)
  6141. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6142. ((equal type "mailto")
  6143. (let ((cmd (car org-link-mailto-program))
  6144. (args (cdr org-link-mailto-program)) args1
  6145. (address path) (subject "") a)
  6146. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6147. (setq address (match-string 1 path)
  6148. subject (org-link-escape (match-string 2 path))))
  6149. (while args
  6150. (cond
  6151. ((not (stringp (car args))) (push (pop args) args1))
  6152. (t (setq a (pop args))
  6153. (if (string-match "%a" a)
  6154. (setq a (replace-match address t t a)))
  6155. (if (string-match "%s" a)
  6156. (setq a (replace-match subject t t a)))
  6157. (push a args1))))
  6158. (apply cmd (nreverse args1))))
  6159. ((member type '("http" "https" "ftp" "news"))
  6160. (browse-url (concat type ":" (org-link-escape
  6161. path org-link-escape-chars-browser))))
  6162. ((member type '("message"))
  6163. (browse-url (concat type ":" path)))
  6164. ((string= type "tags")
  6165. (org-tags-view in-emacs path))
  6166. ((string= type "thisfile")
  6167. (if in-emacs
  6168. (switch-to-buffer-other-window
  6169. (org-get-buffer-for-internal-link (current-buffer)))
  6170. (org-mark-ring-push))
  6171. (let ((cmd `(org-link-search
  6172. ,path
  6173. ,(cond ((equal in-emacs '(4)) 'occur)
  6174. ((equal in-emacs '(16)) 'org-occur)
  6175. (t nil))
  6176. ,pos)))
  6177. (condition-case nil (eval cmd)
  6178. (error (progn (widen) (eval cmd))))))
  6179. ((string= type "tree-match")
  6180. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6181. ((string= type "file")
  6182. (if (string-match "::\\([0-9]+\\)\\'" path)
  6183. (setq line (string-to-number (match-string 1 path))
  6184. path (substring path 0 (match-beginning 0)))
  6185. (if (string-match "::\\(.+\\)\\'" path)
  6186. (setq search (match-string 1 path)
  6187. path (substring path 0 (match-beginning 0)))))
  6188. (if (string-match "[*?{]" (file-name-nondirectory path))
  6189. (dired path)
  6190. (org-open-file path in-emacs line search)))
  6191. ((string= type "news")
  6192. (require 'org-gnus)
  6193. (org-gnus-follow-link path))
  6194. ((string= type "shell")
  6195. (let ((cmd path))
  6196. (if (or (not org-confirm-shell-link-function)
  6197. (funcall org-confirm-shell-link-function
  6198. (format "Execute \"%s\" in shell? "
  6199. (org-add-props cmd nil
  6200. 'face 'org-warning))))
  6201. (progn
  6202. (message "Executing %s" cmd)
  6203. (shell-command cmd))
  6204. (error "Abort"))))
  6205. ((string= type "elisp")
  6206. (let ((cmd path))
  6207. (if (or (not org-confirm-elisp-link-function)
  6208. (funcall org-confirm-elisp-link-function
  6209. (format "Execute \"%s\" as elisp? "
  6210. (org-add-props cmd nil
  6211. 'face 'org-warning))))
  6212. (message "%s => %s" cmd
  6213. (if (equal (string-to-char cmd) ?\()
  6214. (eval (read cmd))
  6215. (call-interactively (read cmd))))
  6216. (error "Abort"))))
  6217. (t
  6218. (browse-url-at-point)))))
  6219. (move-marker org-open-link-marker nil)
  6220. (run-hook-with-args 'org-follow-link-hook))
  6221. ;;;; Time estimates
  6222. (defun org-get-effort (&optional pom)
  6223. "Get the effort estimate for the current entry."
  6224. (org-entry-get pom org-effort-property))
  6225. ;;; File search
  6226. (defvar org-create-file-search-functions nil
  6227. "List of functions to construct the right search string for a file link.
  6228. These functions are called in turn with point at the location to
  6229. which the link should point.
  6230. A function in the hook should first test if it would like to
  6231. handle this file type, for example by checking the major-mode or
  6232. the file extension. If it decides not to handle this file, it
  6233. should just return nil to give other functions a chance. If it
  6234. does handle the file, it must return the search string to be used
  6235. when following the link. The search string will be part of the
  6236. file link, given after a double colon, and `org-open-at-point'
  6237. will automatically search for it. If special measures must be
  6238. taken to make the search successful, another function should be
  6239. added to the companion hook `org-execute-file-search-functions',
  6240. which see.
  6241. A function in this hook may also use `setq' to set the variable
  6242. `description' to provide a suggestion for the descriptive text to
  6243. be used for this link when it gets inserted into an Org-mode
  6244. buffer with \\[org-insert-link].")
  6245. (defvar org-execute-file-search-functions nil
  6246. "List of functions to execute a file search triggered by a link.
  6247. Functions added to this hook must accept a single argument, the
  6248. search string that was part of the file link, the part after the
  6249. double colon. The function must first check if it would like to
  6250. handle this search, for example by checking the major-mode or the
  6251. file extension. If it decides not to handle this search, it
  6252. should just return nil to give other functions a chance. If it
  6253. does handle the search, it must return a non-nil value to keep
  6254. other functions from trying.
  6255. Each function can access the current prefix argument through the
  6256. variable `current-prefix-argument'. Note that a single prefix is
  6257. used to force opening a link in Emacs, so it may be good to only
  6258. use a numeric or double prefix to guide the search function.
  6259. In case this is needed, a function in this hook can also restore
  6260. the window configuration before `org-open-at-point' was called using:
  6261. (set-window-configuration org-window-config-before-follow-link)")
  6262. (defun org-link-search (s &optional type avoid-pos)
  6263. "Search for a link search option.
  6264. If S is surrounded by forward slashes, it is interpreted as a
  6265. regular expression. In org-mode files, this will create an `org-occur'
  6266. sparse tree. In ordinary files, `occur' will be used to list matches.
  6267. If the current buffer is in `dired-mode', grep will be used to search
  6268. in all files. If AVOID-POS is given, ignore matches near that position."
  6269. (let ((case-fold-search t)
  6270. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6271. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6272. (append '(("") (" ") ("\t") ("\n"))
  6273. org-emphasis-alist)
  6274. "\\|") "\\)"))
  6275. (pos (point))
  6276. (pre nil) (post nil)
  6277. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6278. (cond
  6279. ;; First check if there are any special
  6280. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6281. ;; Now try the builtin stuff
  6282. ((save-excursion
  6283. (goto-char (point-min))
  6284. (and
  6285. (re-search-forward
  6286. (concat "<<" (regexp-quote s0) ">>") nil t)
  6287. (setq type 'dedicated
  6288. pos (match-beginning 0))))
  6289. ;; There is an exact target for this
  6290. (goto-char pos))
  6291. ((string-match "^/\\(.*\\)/$" s)
  6292. ;; A regular expression
  6293. (cond
  6294. ((org-mode-p)
  6295. (org-occur (match-string 1 s)))
  6296. ;;((eq major-mode 'dired-mode)
  6297. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6298. (t (org-do-occur (match-string 1 s)))))
  6299. (t
  6300. ;; A normal search strings
  6301. (when (equal (string-to-char s) ?*)
  6302. ;; Anchor on headlines, post may include tags.
  6303. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6304. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6305. s (substring s 1)))
  6306. (remove-text-properties
  6307. 0 (length s)
  6308. '(face nil mouse-face nil keymap nil fontified nil) s)
  6309. ;; Make a series of regular expressions to find a match
  6310. (setq words (org-split-string s "[ \n\r\t]+")
  6311. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6312. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6313. "\\)" markers)
  6314. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6315. re2a (concat "[ \t\r\n]" re2a_)
  6316. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6317. re4 (concat "[^a-zA-Z_]" re4_)
  6318. re1 (concat pre re2 post)
  6319. re3 (concat pre (if pre re4_ re4) post)
  6320. re5 (concat pre ".*" re4)
  6321. re2 (concat pre re2)
  6322. re2a (concat pre (if pre re2a_ re2a))
  6323. re4 (concat pre (if pre re4_ re4))
  6324. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6325. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6326. re5 "\\)"
  6327. ))
  6328. (cond
  6329. ((eq type 'org-occur) (org-occur reall))
  6330. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6331. (t (goto-char (point-min))
  6332. (setq type 'fuzzy)
  6333. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6334. (org-search-not-self 1 re1 nil t)
  6335. (org-search-not-self 1 re2 nil t)
  6336. (org-search-not-self 1 re2a nil t)
  6337. (org-search-not-self 1 re3 nil t)
  6338. (org-search-not-self 1 re4 nil t)
  6339. (org-search-not-self 1 re5 nil t)
  6340. )
  6341. (goto-char (match-beginning 1))
  6342. (goto-char pos)
  6343. (error "No match")))))
  6344. (t
  6345. ;; Normal string-search
  6346. (goto-char (point-min))
  6347. (if (search-forward s nil t)
  6348. (goto-char (match-beginning 0))
  6349. (error "No match"))))
  6350. (and (org-mode-p) (org-show-context 'link-search))
  6351. type))
  6352. (defun org-search-not-self (group &rest args)
  6353. "Execute `re-search-forward', but only accept matches that do not
  6354. enclose the position of `org-open-link-marker'."
  6355. (let ((m org-open-link-marker))
  6356. (catch 'exit
  6357. (while (apply 're-search-forward args)
  6358. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6359. (goto-char (match-end group))
  6360. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6361. (> (match-beginning 0) (marker-position m))
  6362. (< (match-end 0) (marker-position m)))
  6363. (save-match-data
  6364. (or (not (org-in-regexp
  6365. org-bracket-link-analytic-regexp 1))
  6366. (not (match-end 4)) ; no description
  6367. (and (<= (match-beginning 4) (point))
  6368. (>= (match-end 4) (point))))))
  6369. (throw 'exit (point))))))))
  6370. (defun org-get-buffer-for-internal-link (buffer)
  6371. "Return a buffer to be used for displaying the link target of internal links."
  6372. (cond
  6373. ((not org-display-internal-link-with-indirect-buffer)
  6374. buffer)
  6375. ((string-match "(Clone)$" (buffer-name buffer))
  6376. (message "Buffer is already a clone, not making another one")
  6377. ;; we also do not modify visibility in this case
  6378. buffer)
  6379. (t ; make a new indirect buffer for displaying the link
  6380. (let* ((bn (buffer-name buffer))
  6381. (ibn (concat bn "(Clone)"))
  6382. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6383. (with-current-buffer ib (org-overview))
  6384. ib))))
  6385. (defun org-do-occur (regexp &optional cleanup)
  6386. "Call the Emacs command `occur'.
  6387. If CLEANUP is non-nil, remove the printout of the regular expression
  6388. in the *Occur* buffer. This is useful if the regex is long and not useful
  6389. to read."
  6390. (occur regexp)
  6391. (when cleanup
  6392. (let ((cwin (selected-window)) win beg end)
  6393. (when (setq win (get-buffer-window "*Occur*"))
  6394. (select-window win))
  6395. (goto-char (point-min))
  6396. (when (re-search-forward "match[a-z]+" nil t)
  6397. (setq beg (match-end 0))
  6398. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6399. (setq end (1- (match-beginning 0)))))
  6400. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6401. (goto-char (point-min))
  6402. (select-window cwin))))
  6403. ;;; The mark ring for links jumps
  6404. (defvar org-mark-ring nil
  6405. "Mark ring for positions before jumps in Org-mode.")
  6406. (defvar org-mark-ring-last-goto nil
  6407. "Last position in the mark ring used to go back.")
  6408. ;; Fill and close the ring
  6409. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6410. (loop for i from 1 to org-mark-ring-length do
  6411. (push (make-marker) org-mark-ring))
  6412. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6413. org-mark-ring)
  6414. (defun org-mark-ring-push (&optional pos buffer)
  6415. "Put the current position or POS into the mark ring and rotate it."
  6416. (interactive)
  6417. (setq pos (or pos (point)))
  6418. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6419. (move-marker (car org-mark-ring)
  6420. (or pos (point))
  6421. (or buffer (current-buffer)))
  6422. (message "%s"
  6423. (substitute-command-keys
  6424. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6425. (defun org-mark-ring-goto (&optional n)
  6426. "Jump to the previous position in the mark ring.
  6427. With prefix arg N, jump back that many stored positions. When
  6428. called several times in succession, walk through the entire ring.
  6429. Org-mode commands jumping to a different position in the current file,
  6430. or to another Org-mode file, automatically push the old position
  6431. onto the ring."
  6432. (interactive "p")
  6433. (let (p m)
  6434. (if (eq last-command this-command)
  6435. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6436. (setq p org-mark-ring))
  6437. (setq org-mark-ring-last-goto p)
  6438. (setq m (car p))
  6439. (switch-to-buffer (marker-buffer m))
  6440. (goto-char m)
  6441. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6442. (defun org-remove-angle-brackets (s)
  6443. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6444. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6445. s)
  6446. (defun org-add-angle-brackets (s)
  6447. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6448. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6449. s)
  6450. (defun org-remove-double-quotes (s)
  6451. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6452. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6453. s)
  6454. ;;; Following specific links
  6455. (defun org-follow-timestamp-link ()
  6456. (cond
  6457. ((org-at-date-range-p t)
  6458. (let ((org-agenda-start-on-weekday)
  6459. (t1 (match-string 1))
  6460. (t2 (match-string 2)))
  6461. (setq t1 (time-to-days (org-time-string-to-time t1))
  6462. t2 (time-to-days (org-time-string-to-time t2)))
  6463. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6464. ((org-at-timestamp-p t)
  6465. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6466. (substring (match-string 1) 0 10)))
  6467. 1))
  6468. (t (error "This should not happen"))))
  6469. ;;; Following file links
  6470. (defvar org-wait nil)
  6471. (defun org-open-file (path &optional in-emacs line search)
  6472. "Open the file at PATH.
  6473. First, this expands any special file name abbreviations. Then the
  6474. configuration variable `org-file-apps' is checked if it contains an
  6475. entry for this file type, and if yes, the corresponding command is launched.
  6476. If no application is found, Emacs simply visits the file.
  6477. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6478. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6479. and o use an external application to visit the file.
  6480. Optional LINE specifies a line to go to, optional SEARCH a string to
  6481. search for. If LINE or SEARCH is given, the file will always be
  6482. opened in Emacs.
  6483. If the file does not exist, an error is thrown."
  6484. (setq in-emacs (or in-emacs line search))
  6485. (let* ((file (if (equal path "")
  6486. buffer-file-name
  6487. (substitute-in-file-name (expand-file-name path))))
  6488. (apps (append org-file-apps (org-default-apps)))
  6489. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6490. (dirp (if remp nil (file-directory-p file)))
  6491. (file (if (and dirp org-open-directory-means-index-dot-org)
  6492. (concat (file-name-as-directory file) "index.org")
  6493. file))
  6494. (a-m-a-p (assq 'auto-mode apps))
  6495. (dfile (downcase file))
  6496. (old-buffer (current-buffer))
  6497. (old-pos (point))
  6498. (old-mode major-mode)
  6499. ext cmd)
  6500. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6501. (setq ext (match-string 1 dfile))
  6502. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6503. (setq ext (match-string 1 dfile))))
  6504. (cond
  6505. ((equal in-emacs '(16))
  6506. (setq cmd (cdr (assoc 'system apps))))
  6507. (in-emacs (setq cmd 'emacs))
  6508. (t
  6509. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6510. (and dirp (cdr (assoc 'directory apps)))
  6511. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6512. 'string-match)
  6513. (cdr (assoc ext apps))
  6514. (cdr (assoc t apps))))))
  6515. (when (eq cmd 'system)
  6516. (setq cmd (cdr (assoc 'system apps))))
  6517. (when (eq cmd 'default)
  6518. (setq cmd (cdr (assoc t apps))))
  6519. (when (eq cmd 'mailcap)
  6520. (require 'mailcap)
  6521. (mailcap-parse-mailcaps)
  6522. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6523. (command (mailcap-mime-info mime-type)))
  6524. (if (stringp command)
  6525. (setq cmd command)
  6526. (setq cmd 'emacs))))
  6527. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6528. (not (file-exists-p file))
  6529. (not org-open-non-existing-files))
  6530. (error "No such file: %s" file))
  6531. (cond
  6532. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6533. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6534. (while (string-match "['\"]%s['\"]" cmd)
  6535. (setq cmd (replace-match "%s" t t cmd)))
  6536. (while (string-match "%s" cmd)
  6537. (setq cmd (replace-match
  6538. (save-match-data
  6539. (shell-quote-argument
  6540. (convert-standard-filename file)))
  6541. t t cmd)))
  6542. (save-window-excursion
  6543. (start-process-shell-command cmd nil cmd)
  6544. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6545. ))
  6546. ((or (stringp cmd)
  6547. (eq cmd 'emacs))
  6548. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6549. (widen)
  6550. (if line (goto-line line)
  6551. (if search (org-link-search search))))
  6552. ((consp cmd)
  6553. (let ((file (convert-standard-filename file)))
  6554. (eval cmd)))
  6555. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6556. (and (org-mode-p) (eq old-mode 'org-mode)
  6557. (or (not (equal old-buffer (current-buffer)))
  6558. (not (equal old-pos (point))))
  6559. (org-mark-ring-push old-pos old-buffer))))
  6560. (defun org-default-apps ()
  6561. "Return the default applications for this operating system."
  6562. (cond
  6563. ((eq system-type 'darwin)
  6564. org-file-apps-defaults-macosx)
  6565. ((eq system-type 'windows-nt)
  6566. org-file-apps-defaults-windowsnt)
  6567. (t org-file-apps-defaults-gnu)))
  6568. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6569. "Convert extensions to regular expressions in the cars of LIST.
  6570. Also, weed out any non-string entries, because the return value is used
  6571. only for regexp matching.
  6572. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6573. point to the symbol `emacs', indicating that the file should
  6574. be opened in Emacs."
  6575. (append
  6576. (delq nil
  6577. (mapcar (lambda (x)
  6578. (if (not (stringp (car x)))
  6579. nil
  6580. (if (string-match "\\W" (car x))
  6581. x
  6582. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6583. list))
  6584. (if add-auto-mode
  6585. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6586. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6587. (defun org-file-remote-p (file)
  6588. "Test whether FILE specifies a location on a remote system.
  6589. Return non-nil if the location is indeed remote.
  6590. For example, the filename \"/user@host:/foo\" specifies a location
  6591. on the system \"/user@host:\"."
  6592. (cond ((fboundp 'file-remote-p)
  6593. (file-remote-p file))
  6594. ((fboundp 'tramp-handle-file-remote-p)
  6595. (tramp-handle-file-remote-p file))
  6596. ((and (boundp 'ange-ftp-name-format)
  6597. (string-match (car ange-ftp-name-format) file))
  6598. t)
  6599. (t nil)))
  6600. ;;;; Refiling
  6601. (defun org-get-org-file ()
  6602. "Read a filename, with default directory `org-directory'."
  6603. (let ((default (or org-default-notes-file remember-data-file)))
  6604. (read-file-name (format "File name [%s]: " default)
  6605. (file-name-as-directory org-directory)
  6606. default)))
  6607. (defun org-notes-order-reversed-p ()
  6608. "Check if the current file should receive notes in reversed order."
  6609. (cond
  6610. ((not org-reverse-note-order) nil)
  6611. ((eq t org-reverse-note-order) t)
  6612. ((not (listp org-reverse-note-order)) nil)
  6613. (t (catch 'exit
  6614. (let ((all org-reverse-note-order)
  6615. entry)
  6616. (while (setq entry (pop all))
  6617. (if (string-match (car entry) buffer-file-name)
  6618. (throw 'exit (cdr entry))))
  6619. nil)))))
  6620. (defvar org-refile-target-table nil
  6621. "The list of refile targets, created by `org-refile'.")
  6622. (defvar org-agenda-new-buffers nil
  6623. "Buffers created to visit agenda files.")
  6624. (defun org-get-refile-targets (&optional default-buffer)
  6625. "Produce a table with refile targets."
  6626. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6627. targets txt re files f desc descre)
  6628. (with-current-buffer (or default-buffer (current-buffer))
  6629. (while (setq entry (pop entries))
  6630. (setq files (car entry) desc (cdr entry))
  6631. (cond
  6632. ((null files) (setq files (list (current-buffer))))
  6633. ((eq files 'org-agenda-files)
  6634. (setq files (org-agenda-files 'unrestricted)))
  6635. ((and (symbolp files) (fboundp files))
  6636. (setq files (funcall files)))
  6637. ((and (symbolp files) (boundp files))
  6638. (setq files (symbol-value files))))
  6639. (if (stringp files) (setq files (list files)))
  6640. (cond
  6641. ((eq (car desc) :tag)
  6642. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6643. ((eq (car desc) :todo)
  6644. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6645. ((eq (car desc) :regexp)
  6646. (setq descre (cdr desc)))
  6647. ((eq (car desc) :level)
  6648. (setq descre (concat "^\\*\\{" (number-to-string
  6649. (if org-odd-levels-only
  6650. (1- (* 2 (cdr desc)))
  6651. (cdr desc)))
  6652. "\\}[ \t]")))
  6653. ((eq (car desc) :maxlevel)
  6654. (setq descre (concat "^\\*\\{1," (number-to-string
  6655. (if org-odd-levels-only
  6656. (1- (* 2 (cdr desc)))
  6657. (cdr desc)))
  6658. "\\}[ \t]")))
  6659. (t (error "Bad refiling target description %s" desc)))
  6660. (while (setq f (pop files))
  6661. (save-excursion
  6662. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6663. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6664. (save-excursion
  6665. (save-restriction
  6666. (widen)
  6667. (goto-char (point-min))
  6668. (while (re-search-forward descre nil t)
  6669. (goto-char (point-at-bol))
  6670. (when (looking-at org-complex-heading-regexp)
  6671. (setq txt (org-link-display-format (match-string 4))
  6672. re (concat "^" (regexp-quote
  6673. (buffer-substring (match-beginning 1)
  6674. (match-end 4)))))
  6675. (if (match-end 5) (setq re (concat re "[ \t]+"
  6676. (regexp-quote
  6677. (match-string 5)))))
  6678. (setq re (concat re "[ \t]*$"))
  6679. (when org-refile-use-outline-path
  6680. (setq txt (mapconcat 'org-protect-slash
  6681. (append
  6682. (if (eq org-refile-use-outline-path 'file)
  6683. (list (file-name-nondirectory
  6684. (buffer-file-name (buffer-base-buffer))))
  6685. (if (eq org-refile-use-outline-path 'full-file-path)
  6686. (list (buffer-file-name (buffer-base-buffer)))))
  6687. (org-get-outline-path)
  6688. (list txt))
  6689. "/")))
  6690. (push (list txt f re (point)) targets))
  6691. (goto-char (point-at-eol))))))))
  6692. (nreverse targets))))
  6693. (defun org-protect-slash (s)
  6694. (while (string-match "/" s)
  6695. (setq s (replace-match "\\" t t s)))
  6696. s)
  6697. (defun org-get-outline-path ()
  6698. "Return the outline path to the current entry, as a list."
  6699. (let (rtn)
  6700. (save-excursion
  6701. (while (org-up-heading-safe)
  6702. (when (looking-at org-complex-heading-regexp)
  6703. (push (org-match-string-no-properties 4) rtn)))
  6704. rtn)))
  6705. (defvar org-refile-history nil
  6706. "History for refiling operations.")
  6707. (defun org-refile (&optional goto default-buffer)
  6708. "Move the entry at point to another heading.
  6709. The list of target headings is compiled using the information in
  6710. `org-refile-targets', which see. This list is created before each use
  6711. and will therefore always be up-to-date.
  6712. At the target location, the entry is filed as a subitem of the target heading.
  6713. Depending on `org-reverse-note-order', the new subitem will either be the
  6714. first or the last subitem.
  6715. If there is an active region, all entries in that region will be moved.
  6716. However, the region must fulfil the requirement that the first heading
  6717. is the first one sets the top-level of the moved text - at most siblings
  6718. below it are allowed.
  6719. With prefix arg GOTO, the command will only visit the target location,
  6720. not actually move anything.
  6721. With a double prefix `C-u C-u', go to the location where the last refiling
  6722. operation has put the subtree."
  6723. (interactive "P")
  6724. (let* ((cbuf (current-buffer))
  6725. (regionp (org-region-active-p))
  6726. (region-start (and regionp (region-beginning)))
  6727. (region-end (and regionp (region-end)))
  6728. (region-length (and regionp (- region-end region-start)))
  6729. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6730. pos it nbuf file re level reversed)
  6731. (when regionp (goto-char region-start)
  6732. (unless (org-kill-is-subtree-p
  6733. (buffer-substring region-start region-end))
  6734. (error "The region is not a (sequence of) subtree(s)")))
  6735. (if (equal goto '(16))
  6736. (org-refile-goto-last-stored)
  6737. (when (setq it (org-refile-get-location
  6738. (if goto "Goto: " "Refile to: ") default-buffer))
  6739. (setq file (nth 1 it)
  6740. re (nth 2 it)
  6741. pos (nth 3 it))
  6742. (setq nbuf (or (find-buffer-visiting file)
  6743. (find-file-noselect file)))
  6744. (if goto
  6745. (progn
  6746. (switch-to-buffer nbuf)
  6747. (goto-char pos)
  6748. (org-show-context 'org-goto))
  6749. (if regionp
  6750. (progn
  6751. (kill-new (buffer-substring region-start region-end))
  6752. (org-save-markers-in-region region-start region-end))
  6753. (org-copy-subtree 1 nil t))
  6754. (save-excursion
  6755. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6756. (find-file-noselect file))))
  6757. (setq reversed (org-notes-order-reversed-p))
  6758. (save-excursion
  6759. (save-restriction
  6760. (widen)
  6761. (goto-char pos)
  6762. (looking-at outline-regexp)
  6763. (setq level (org-get-valid-level (funcall outline-level) 1))
  6764. (goto-char
  6765. (if reversed
  6766. (or (outline-next-heading) (point-max))
  6767. (or (save-excursion (outline-get-next-sibling))
  6768. (org-end-of-subtree t t)
  6769. (point-max))))
  6770. (if (not (bolp)) (newline))
  6771. (bookmark-set "org-refile-last-stored")
  6772. (org-paste-subtree level))))
  6773. (if regionp
  6774. (delete-region (point) (+ (point) region-length))
  6775. (org-cut-subtree))
  6776. (setq org-markers-to-move nil)
  6777. (message "Refiled to \"%s\"" (car it)))))))
  6778. (defun org-refile-goto-last-stored ()
  6779. "Go to the location where the last refile was stored."
  6780. (interactive)
  6781. (bookmark-jump "org-refile-last-stored")
  6782. (message "This is the location of the last refile"))
  6783. (defun org-refile-get-location (&optional prompt default-buffer)
  6784. "Prompt the user for a refile location, using PROMPT."
  6785. (let ((org-refile-targets org-refile-targets)
  6786. (org-refile-use-outline-path org-refile-use-outline-path))
  6787. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6788. (unless org-refile-target-table
  6789. (error "No refile targets"))
  6790. (let* ((cbuf (current-buffer))
  6791. (cfunc (if (and org-refile-use-outline-path
  6792. org-outline-path-complete-in-steps)
  6793. 'org-olpath-completing-read
  6794. 'org-ido-completing-read))
  6795. (extra (if org-refile-use-outline-path "/" ""))
  6796. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6797. (fname (and filename (file-truename filename)))
  6798. (tbl (mapcar
  6799. (lambda (x)
  6800. (if (not (equal fname (file-truename (nth 1 x))))
  6801. (cons (concat (car x) extra " ("
  6802. (file-name-nondirectory (nth 1 x)) ")")
  6803. (cdr x))
  6804. (cons (concat (car x) extra) (cdr x))))
  6805. org-refile-target-table))
  6806. (completion-ignore-case t))
  6807. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6808. tbl)))
  6809. (defun org-olpath-completing-read (prompt collection &rest args)
  6810. "Read an outline path like a file name."
  6811. (let ((thetable collection))
  6812. (apply
  6813. 'org-ido-completing-read prompt
  6814. (lambda (string predicate &optional flag)
  6815. (let (rtn r s f (l (length string)))
  6816. (cond
  6817. ((eq flag nil)
  6818. ;; try completion
  6819. (try-completion string thetable))
  6820. ((eq flag t)
  6821. ;; all-completions
  6822. (setq rtn (all-completions string thetable predicate))
  6823. (mapcar
  6824. (lambda (x)
  6825. (setq r (substring x l))
  6826. (if (string-match " ([^)]*)$" x)
  6827. (setq f (match-string 0 x))
  6828. (setq f ""))
  6829. (if (string-match "/" r)
  6830. (concat string (substring r 0 (match-end 0)) f)
  6831. x))
  6832. rtn))
  6833. ((eq flag 'lambda)
  6834. ;; exact match?
  6835. (assoc string thetable)))
  6836. ))
  6837. args)))
  6838. ;;;; Dynamic blocks
  6839. (defun org-find-dblock (name)
  6840. "Find the first dynamic block with name NAME in the buffer.
  6841. If not found, stay at current position and return nil."
  6842. (let (pos)
  6843. (save-excursion
  6844. (goto-char (point-min))
  6845. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6846. nil t)
  6847. (match-beginning 0))))
  6848. (if pos (goto-char pos))
  6849. pos))
  6850. (defconst org-dblock-start-re
  6851. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6852. "Matches the startline of a dynamic block, with parameters.")
  6853. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6854. "Matches the end of a dyhamic block.")
  6855. (defun org-create-dblock (plist)
  6856. "Create a dynamic block section, with parameters taken from PLIST.
  6857. PLIST must containe a :name entry which is used as name of the block."
  6858. (unless (bolp) (newline))
  6859. (let ((name (plist-get plist :name)))
  6860. (insert "#+BEGIN: " name)
  6861. (while plist
  6862. (if (eq (car plist) :name)
  6863. (setq plist (cddr plist))
  6864. (insert " " (prin1-to-string (pop plist)))))
  6865. (insert "\n\n#+END:\n")
  6866. (beginning-of-line -2)))
  6867. (defun org-prepare-dblock ()
  6868. "Prepare dynamic block for refresh.
  6869. This empties the block, puts the cursor at the insert position and returns
  6870. the property list including an extra property :name with the block name."
  6871. (unless (looking-at org-dblock-start-re)
  6872. (error "Not at a dynamic block"))
  6873. (let* ((begdel (1+ (match-end 0)))
  6874. (name (org-no-properties (match-string 1)))
  6875. (params (append (list :name name)
  6876. (read (concat "(" (match-string 3) ")")))))
  6877. (unless (re-search-forward org-dblock-end-re nil t)
  6878. (error "Dynamic block not terminated"))
  6879. (setq params
  6880. (append params
  6881. (list :content (buffer-substring
  6882. begdel (match-beginning 0)))))
  6883. (delete-region begdel (match-beginning 0))
  6884. (goto-char begdel)
  6885. (open-line 1)
  6886. params))
  6887. (defun org-map-dblocks (&optional command)
  6888. "Apply COMMAND to all dynamic blocks in the current buffer.
  6889. If COMMAND is not given, use `org-update-dblock'."
  6890. (let ((cmd (or command 'org-update-dblock))
  6891. pos)
  6892. (save-excursion
  6893. (goto-char (point-min))
  6894. (while (re-search-forward org-dblock-start-re nil t)
  6895. (goto-char (setq pos (match-beginning 0)))
  6896. (condition-case nil
  6897. (funcall cmd)
  6898. (error (message "Error during update of dynamic block")))
  6899. (goto-char pos)
  6900. (unless (re-search-forward org-dblock-end-re nil t)
  6901. (error "Dynamic block not terminated"))))))
  6902. (defun org-dblock-update (&optional arg)
  6903. "User command for updating dynamic blocks.
  6904. Update the dynamic block at point. With prefix ARG, update all dynamic
  6905. blocks in the buffer."
  6906. (interactive "P")
  6907. (if arg
  6908. (org-update-all-dblocks)
  6909. (or (looking-at org-dblock-start-re)
  6910. (org-beginning-of-dblock))
  6911. (org-update-dblock)))
  6912. (defun org-update-dblock ()
  6913. "Update the dynamic block at point
  6914. This means to empty the block, parse for parameters and then call
  6915. the correct writing function."
  6916. (save-window-excursion
  6917. (let* ((pos (point))
  6918. (line (org-current-line))
  6919. (params (org-prepare-dblock))
  6920. (name (plist-get params :name))
  6921. (cmd (intern (concat "org-dblock-write:" name))))
  6922. (message "Updating dynamic block `%s' at line %d..." name line)
  6923. (funcall cmd params)
  6924. (message "Updating dynamic block `%s' at line %d...done" name line)
  6925. (goto-char pos))))
  6926. (defun org-beginning-of-dblock ()
  6927. "Find the beginning of the dynamic block at point.
  6928. Error if there is no scuh block at point."
  6929. (let ((pos (point))
  6930. beg)
  6931. (end-of-line 1)
  6932. (if (and (re-search-backward org-dblock-start-re nil t)
  6933. (setq beg (match-beginning 0))
  6934. (re-search-forward org-dblock-end-re nil t)
  6935. (> (match-end 0) pos))
  6936. (goto-char beg)
  6937. (goto-char pos)
  6938. (error "Not in a dynamic block"))))
  6939. (defun org-update-all-dblocks ()
  6940. "Update all dynamic blocks in the buffer.
  6941. This function can be used in a hook."
  6942. (when (org-mode-p)
  6943. (org-map-dblocks 'org-update-dblock)))
  6944. ;;;; Completion
  6945. (defconst org-additional-option-like-keywords
  6946. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6947. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6948. "BEGIN_EXAMPLE" "END_EXAMPLE"
  6949. "BEGIN_QUOTE" "END_QUOTE"
  6950. "BEGIN_VERSE" "END_VERSE"
  6951. "BEGIN_SRC" "END_SRC"))
  6952. (defcustom org-structure-template-alist
  6953. '(
  6954. ("s" "#+begin_src ?\n\n#+end_src"
  6955. "<src lang=\"?\">\n\n</src>")
  6956. ("e" "#+begin_example\n?\n#+end_example"
  6957. "<example>\n?\n</example>")
  6958. ("q" "#+begin_quote\n?\n#+end_quote"
  6959. "<quote>\n?\n</quote>")
  6960. ("v" "#+begin_verse\n?\n#+end_verse"
  6961. "<verse>\n?\n/verse>")
  6962. ("l" "#+begin_latex\n?\n#+end_latex"
  6963. "<literal style=\"latex\">\n?\n</literal>")
  6964. ("L" "#+latex: "
  6965. "<literal style=\"latex\">?</literal>")
  6966. ("h" "#+begin_html\n?\n#+end_html"
  6967. "<literal style=\"html\">\n?\n</literal>")
  6968. ("H" "#+html: "
  6969. "<literal style=\"html\">?</literal>")
  6970. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6971. ("A" "#+ascii: ")
  6972. ("i" "#+include %file ?"
  6973. "<include file=%file markup=\"?\">")
  6974. )
  6975. "Structure completion elements.
  6976. This is a list of abbreviation keys and values. The value gets inserted
  6977. it you type @samp{.} followed by the key and then the completion key,
  6978. usually `M-TAB'. %file will be replaced by a file name after prompting
  6979. for the file uning completion.
  6980. There are two templates for each key, the first uses the original Org syntax,
  6981. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  6982. the default when the /org-mtags.el/ module has been loaded. See also the
  6983. variable `org-mtags-prefer-muse-templates'.
  6984. This is an experimental feature, it is undecided if it is going to stay in."
  6985. :group 'org-completion
  6986. :type '(repeat
  6987. (string :tag "Key")
  6988. (string :tag "Template")
  6989. (string :tag "Muse Template")))
  6990. (defun org-try-structure-completion ()
  6991. "Try to complete a structure template before point.
  6992. This looks for strings like \"<e\" on an otherwise empty line and
  6993. expands them."
  6994. (let ((l (buffer-substring (point-at-bol) (point)))
  6995. a)
  6996. (when (and (looking-at "[ \t]*$")
  6997. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  6998. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  6999. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7000. (match-beginning 1)) a)
  7001. t)))
  7002. (defun org-complete-expand-structure-template (start cell)
  7003. "Expand a structure template."
  7004. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7005. (rpl (nth (if musep 2 1) cell)))
  7006. (delete-region start (point))
  7007. (when (string-match "\\`#\\+" rpl)
  7008. (cond
  7009. ((bolp))
  7010. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7011. (delete-region (point-at-bol) (point)))
  7012. (t (newline))))
  7013. (setq start (point))
  7014. (if (string-match "%file" rpl)
  7015. (setq rpl (replace-match
  7016. (concat
  7017. "\""
  7018. (save-match-data
  7019. (abbreviate-file-name (read-file-name "Include file: ")))
  7020. "\"")
  7021. t t rpl)))
  7022. (insert rpl)
  7023. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7024. (defun org-complete (&optional arg)
  7025. "Perform completion on word at point.
  7026. At the beginning of a headline, this completes TODO keywords as given in
  7027. `org-todo-keywords'.
  7028. If the current word is preceded by a backslash, completes the TeX symbols
  7029. that are supported for HTML support.
  7030. If the current word is preceded by \"#+\", completes special words for
  7031. setting file options.
  7032. In the line after \"#+STARTUP:, complete valid keywords.\"
  7033. At all other locations, this simply calls the value of
  7034. `org-completion-fallback-command'."
  7035. (interactive "P")
  7036. (org-without-partial-completion
  7037. (catch 'exit
  7038. (let* ((a nil)
  7039. (end (point))
  7040. (beg1 (save-excursion
  7041. (skip-chars-backward (org-re "[:alnum:]_@"))
  7042. (point)))
  7043. (beg (save-excursion
  7044. (skip-chars-backward "a-zA-Z0-9_:$")
  7045. (point)))
  7046. (confirm (lambda (x) (stringp (car x))))
  7047. (searchhead (equal (char-before beg) ?*))
  7048. (struct
  7049. (when (and (member (char-before beg1) '(?. ?<))
  7050. (setq a (assoc (buffer-substring beg1 (point))
  7051. org-structure-template-alist)))
  7052. (org-complete-expand-structure-template (1- beg1) a)
  7053. (throw 'exit t)))
  7054. (tag (and (equal (char-before beg1) ?:)
  7055. (equal (char-after (point-at-bol)) ?*)))
  7056. (prop (and (equal (char-before beg1) ?:)
  7057. (not (equal (char-after (point-at-bol)) ?*))))
  7058. (texp (equal (char-before beg) ?\\))
  7059. (link (equal (char-before beg) ?\[))
  7060. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7061. beg)
  7062. "#+"))
  7063. (startup (string-match "^#\\+STARTUP:.*"
  7064. (buffer-substring (point-at-bol) (point))))
  7065. (completion-ignore-case opt)
  7066. (type nil)
  7067. (tbl nil)
  7068. (table (cond
  7069. (opt
  7070. (setq type :opt)
  7071. (require 'org-exp)
  7072. (append
  7073. (mapcar
  7074. (lambda (x)
  7075. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7076. (cons (match-string 2 x) (match-string 1 x)))
  7077. (org-split-string (org-get-current-options) "\n"))
  7078. (mapcar 'list org-additional-option-like-keywords)))
  7079. (startup
  7080. (setq type :startup)
  7081. org-startup-options)
  7082. (link (append org-link-abbrev-alist-local
  7083. org-link-abbrev-alist))
  7084. (texp
  7085. (setq type :tex)
  7086. org-html-entities)
  7087. ((string-match "\\`\\*+[ \t]+\\'"
  7088. (buffer-substring (point-at-bol) beg))
  7089. (setq type :todo)
  7090. (mapcar 'list org-todo-keywords-1))
  7091. (searchhead
  7092. (setq type :searchhead)
  7093. (save-excursion
  7094. (goto-char (point-min))
  7095. (while (re-search-forward org-todo-line-regexp nil t)
  7096. (push (list
  7097. (org-make-org-heading-search-string
  7098. (match-string 3) t))
  7099. tbl)))
  7100. tbl)
  7101. (tag (setq type :tag beg beg1)
  7102. (or org-tag-alist (org-get-buffer-tags)))
  7103. (prop (setq type :prop beg beg1)
  7104. (mapcar 'list (org-buffer-property-keys nil t t)))
  7105. (t (progn
  7106. (call-interactively org-completion-fallback-command)
  7107. (throw 'exit nil)))))
  7108. (pattern (buffer-substring-no-properties beg end))
  7109. (completion (try-completion pattern table confirm)))
  7110. (cond ((eq completion t)
  7111. (if (not (assoc (upcase pattern) table))
  7112. (message "Already complete")
  7113. (if (and (equal type :opt)
  7114. (not (member (car (assoc (upcase pattern) table))
  7115. org-additional-option-like-keywords)))
  7116. (insert (substring (cdr (assoc (upcase pattern) table))
  7117. (length pattern)))
  7118. (if (memq type '(:tag :prop)) (insert ":")))))
  7119. ((null completion)
  7120. (message "Can't find completion for \"%s\"" pattern)
  7121. (ding))
  7122. ((not (string= pattern completion))
  7123. (delete-region beg end)
  7124. (if (string-match " +$" completion)
  7125. (setq completion (replace-match "" t t completion)))
  7126. (insert completion)
  7127. (if (get-buffer-window "*Completions*")
  7128. (delete-window (get-buffer-window "*Completions*")))
  7129. (if (assoc completion table)
  7130. (if (eq type :todo) (insert " ")
  7131. (if (memq type '(:tag :prop)) (insert ":"))))
  7132. (if (and (equal type :opt) (assoc completion table))
  7133. (message "%s" (substitute-command-keys
  7134. "Press \\[org-complete] again to insert example settings"))))
  7135. (t
  7136. (message "Making completion list...")
  7137. (let ((list (sort (all-completions pattern table confirm)
  7138. 'string<)))
  7139. (with-output-to-temp-buffer "*Completions*"
  7140. (condition-case nil
  7141. ;; Protection needed for XEmacs and emacs 21
  7142. (display-completion-list list pattern)
  7143. (error (display-completion-list list)))))
  7144. (message "Making completion list...%s" "done")))))))
  7145. ;;;; TODO, DEADLINE, Comments
  7146. (defun org-toggle-comment ()
  7147. "Change the COMMENT state of an entry."
  7148. (interactive)
  7149. (save-excursion
  7150. (org-back-to-heading)
  7151. (let (case-fold-search)
  7152. (if (looking-at (concat outline-regexp
  7153. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7154. (replace-match "" t t nil 1)
  7155. (if (looking-at outline-regexp)
  7156. (progn
  7157. (goto-char (match-end 0))
  7158. (insert org-comment-string " ")))))))
  7159. (defvar org-last-todo-state-is-todo nil
  7160. "This is non-nil when the last TODO state change led to a TODO state.
  7161. If the last change removed the TODO tag or switched to DONE, then
  7162. this is nil.")
  7163. (defvar org-setting-tags nil) ; dynamically skiped
  7164. (defun org-parse-local-options (string var)
  7165. "Parse STRING for startup setting relevant for variable VAR."
  7166. (let ((rtn (symbol-value var))
  7167. e opts)
  7168. (save-match-data
  7169. (if (or (not string) (not (string-match "\\S-" string)))
  7170. rtn
  7171. (setq opts (delq nil (mapcar (lambda (x)
  7172. (setq e (assoc x org-startup-options))
  7173. (if (eq (nth 1 e) var) e nil))
  7174. (org-split-string string "[ \t]+"))))
  7175. (if (not opts)
  7176. rtn
  7177. (setq rtn nil)
  7178. (while (setq e (pop opts))
  7179. (if (not (nth 3 e))
  7180. (setq rtn (nth 2 e))
  7181. (if (not (listp rtn)) (setq rtn nil))
  7182. (push (nth 2 e) rtn)))
  7183. rtn)))))
  7184. (defvar org-blocker-hook nil
  7185. "Hook for functions that are allowed to block a state change.
  7186. Each function gets as its single argument a property list, see
  7187. `org-trigger-hook' for more information about this list.
  7188. If any of the functions in this hook returns nil, the state change
  7189. is blocked.")
  7190. (defvar org-trigger-hook nil
  7191. "Hook for functions that are triggered by a state change.
  7192. Each function gets as its single argument a property list with at least
  7193. the following elements:
  7194. (:type type-of-change :position pos-at-entry-start
  7195. :from old-state :to new-state)
  7196. Depending on the type, more properties may be present.
  7197. This mechanism is currently implemented for:
  7198. TODO state changes
  7199. ------------------
  7200. :type todo-state-change
  7201. :from previous state (keyword as a string), or nil
  7202. :to new state (keyword as a string), or nil")
  7203. (defvar org-agenda-headline-snapshot-before-repeat)
  7204. (defun org-todo (&optional arg)
  7205. "Change the TODO state of an item.
  7206. The state of an item is given by a keyword at the start of the heading,
  7207. like
  7208. *** TODO Write paper
  7209. *** DONE Call mom
  7210. The different keywords are specified in the variable `org-todo-keywords'.
  7211. By default the available states are \"TODO\" and \"DONE\".
  7212. So for this example: when the item starts with TODO, it is changed to DONE.
  7213. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7214. DONE are present, add TODO at the beginning of the heading.
  7215. With C-u prefix arg, use completion to determine the new state.
  7216. With numeric prefix arg, switch to that state.
  7217. For calling through lisp, arg is also interpreted in the following way:
  7218. 'none -> empty state
  7219. \"\"(empty string) -> switch to empty state
  7220. 'done -> switch to DONE
  7221. 'nextset -> switch to the next set of keywords
  7222. 'previousset -> switch to the previous set of keywords
  7223. \"WAITING\" -> switch to the specified keyword, but only if it
  7224. really is a member of `org-todo-keywords'."
  7225. (interactive "P")
  7226. (save-excursion
  7227. (catch 'exit
  7228. (org-back-to-heading)
  7229. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7230. (or (looking-at (concat " +" org-todo-regexp " *"))
  7231. (looking-at " *"))
  7232. (let* ((match-data (match-data))
  7233. (startpos (point-at-bol))
  7234. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7235. (org-log-done org-log-done)
  7236. (org-log-repeat org-log-repeat)
  7237. (org-todo-log-states org-todo-log-states)
  7238. (this (match-string 1))
  7239. (hl-pos (match-beginning 0))
  7240. (head (org-get-todo-sequence-head this))
  7241. (ass (assoc head org-todo-kwd-alist))
  7242. (interpret (nth 1 ass))
  7243. (done-word (nth 3 ass))
  7244. (final-done-word (nth 4 ass))
  7245. (last-state (or this ""))
  7246. (completion-ignore-case t)
  7247. (member (member this org-todo-keywords-1))
  7248. (tail (cdr member))
  7249. (state (cond
  7250. ((and org-todo-key-trigger
  7251. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7252. (and (not arg) org-use-fast-todo-selection
  7253. (not (eq org-use-fast-todo-selection 'prefix)))))
  7254. ;; Use fast selection
  7255. (org-fast-todo-selection))
  7256. ((and (equal arg '(4))
  7257. (or (not org-use-fast-todo-selection)
  7258. (not org-todo-key-trigger)))
  7259. ;; Read a state with completion
  7260. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7261. org-todo-keywords-1)
  7262. nil t))
  7263. ((eq arg 'right)
  7264. (if this
  7265. (if tail (car tail) nil)
  7266. (car org-todo-keywords-1)))
  7267. ((eq arg 'left)
  7268. (if (equal member org-todo-keywords-1)
  7269. nil
  7270. (if this
  7271. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7272. org-todo-keywords-1)
  7273. (org-last org-todo-keywords-1))))
  7274. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7275. (setq arg nil))) ; hack to fall back to cycling
  7276. (arg
  7277. ;; user or caller requests a specific state
  7278. (cond
  7279. ((equal arg "") nil)
  7280. ((eq arg 'none) nil)
  7281. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7282. ((eq arg 'nextset)
  7283. (or (car (cdr (member head org-todo-heads)))
  7284. (car org-todo-heads)))
  7285. ((eq arg 'previousset)
  7286. (let ((org-todo-heads (reverse org-todo-heads)))
  7287. (or (car (cdr (member head org-todo-heads)))
  7288. (car org-todo-heads))))
  7289. ((car (member arg org-todo-keywords-1)))
  7290. ((nth (1- (prefix-numeric-value arg))
  7291. org-todo-keywords-1))))
  7292. ((null member) (or head (car org-todo-keywords-1)))
  7293. ((equal this final-done-word) nil) ;; -> make empty
  7294. ((null tail) nil) ;; -> first entry
  7295. ((eq interpret 'sequence)
  7296. (car tail))
  7297. ((memq interpret '(type priority))
  7298. (if (eq this-command last-command)
  7299. (car tail)
  7300. (if (> (length tail) 0)
  7301. (or done-word (car org-done-keywords))
  7302. nil)))
  7303. (t nil)))
  7304. (next (if state (concat " " state " ") " "))
  7305. (change-plist (list :type 'todo-state-change :from this :to state
  7306. :position startpos))
  7307. dolog now-done-p)
  7308. (when org-blocker-hook
  7309. (unless (save-excursion
  7310. (save-match-data
  7311. (run-hook-with-args-until-failure
  7312. 'org-blocker-hook change-plist)))
  7313. (if (interactive-p)
  7314. (error "TODO state change from %s to %s blocked" this state)
  7315. ;; fail silently
  7316. (message "TODO state change from %s to %s blocked" this state)
  7317. (throw 'exit nil))))
  7318. (store-match-data match-data)
  7319. (replace-match next t t)
  7320. (unless (pos-visible-in-window-p hl-pos)
  7321. (message "TODO state changed to %s" (org-trim next)))
  7322. (unless head
  7323. (setq head (org-get-todo-sequence-head state)
  7324. ass (assoc head org-todo-kwd-alist)
  7325. interpret (nth 1 ass)
  7326. done-word (nth 3 ass)
  7327. final-done-word (nth 4 ass)))
  7328. (when (memq arg '(nextset previousset))
  7329. (message "Keyword-Set %d/%d: %s"
  7330. (- (length org-todo-sets) -1
  7331. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7332. (length org-todo-sets)
  7333. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7334. (setq org-last-todo-state-is-todo
  7335. (not (member state org-done-keywords)))
  7336. (setq now-done-p (and (member state org-done-keywords)
  7337. (not (member this org-done-keywords))))
  7338. (and logging (org-local-logging logging))
  7339. (when (and (or org-todo-log-states org-log-done)
  7340. (not (memq arg '(nextset previousset))))
  7341. ;; we need to look at recording a time and note
  7342. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7343. (nth 2 (assoc this org-todo-log-states))))
  7344. (when (and state
  7345. (member state org-not-done-keywords)
  7346. (not (member this org-not-done-keywords)))
  7347. ;; This is now a todo state and was not one before
  7348. ;; If there was a CLOSED time stamp, get rid of it.
  7349. (org-add-planning-info nil nil 'closed))
  7350. (when (and now-done-p org-log-done)
  7351. ;; It is now done, and it was not done before
  7352. (org-add-planning-info 'closed (org-current-time))
  7353. (if (and (not dolog) (eq 'note org-log-done))
  7354. (org-add-log-setup 'done state 'findpos 'note)))
  7355. (when (and state dolog)
  7356. ;; This is a non-nil state, and we need to log it
  7357. (org-add-log-setup 'state state 'findpos dolog)))
  7358. ;; Fixup tag positioning
  7359. (org-todo-trigger-tag-changes state)
  7360. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7361. (when org-provide-todo-statistics
  7362. (org-update-parent-todo-statistics))
  7363. (run-hooks 'org-after-todo-state-change-hook)
  7364. (if (and arg (not (member state org-done-keywords)))
  7365. (setq head (org-get-todo-sequence-head state)))
  7366. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7367. ;; Do we need to trigger a repeat?
  7368. (when now-done-p
  7369. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7370. ;; This is for the agenda, take a snapshot of the headline.
  7371. (save-match-data
  7372. (setq org-agenda-headline-snapshot-before-repeat
  7373. (org-get-heading))))
  7374. (org-auto-repeat-maybe state))
  7375. ;; Fixup cursor location if close to the keyword
  7376. (if (and (outline-on-heading-p)
  7377. (not (bolp))
  7378. (save-excursion (beginning-of-line 1)
  7379. (looking-at org-todo-line-regexp))
  7380. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7381. (progn
  7382. (goto-char (or (match-end 2) (match-end 1)))
  7383. (just-one-space)))
  7384. (when org-trigger-hook
  7385. (save-excursion
  7386. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7387. (defun org-update-parent-todo-statistics ()
  7388. "Update any statistics cookie in the parent of the current headline."
  7389. (interactive)
  7390. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7391. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7392. (catch 'exit
  7393. (save-excursion
  7394. (setq level (org-up-heading-safe))
  7395. (unless (and level
  7396. (re-search-forward box-re (point-at-eol) t))
  7397. (throw 'exit nil))
  7398. (setq is-percent (match-end 2))
  7399. (save-match-data
  7400. (unless (outline-next-heading) (throw 'exit nil))
  7401. (while (looking-at org-todo-line-regexp)
  7402. (setq kwd (match-string 2))
  7403. (and kwd (setq cnt-all (1+ cnt-all)))
  7404. (and (member kwd org-done-keywords)
  7405. (setq cnt-done (1+ cnt-done)))
  7406. (condition-case nil
  7407. (org-forward-same-level 1)
  7408. (error (end-of-line 1)))))
  7409. (replace-match
  7410. (if is-percent
  7411. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7412. (format "[%d/%d]" cnt-done cnt-all)))
  7413. (run-hook-with-args 'org-after-todo-statistics-hook
  7414. cnt-done (- cnt-all cnt-done))))))
  7415. (defvar org-after-todo-statistics-hook nil
  7416. "Hook that is called after a TODO statistics cookie has been updated.
  7417. Each function is called with two arguments: the number of not-done entries
  7418. and the number of done entries.
  7419. For example, the following function, when added to this hook, will switch
  7420. an entry to DONE when all children are done, and back to TODO when new
  7421. entries are set to a TODO status. Note that this hook is only called
  7422. when there is a statistics cookie in the headline!
  7423. (defun org-summary-todo (n-done n-not-done)
  7424. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7425. (let (org-log-done org-log-states) ; turn off logging
  7426. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7427. ")
  7428. (defun org-todo-trigger-tag-changes (state)
  7429. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7430. (let ((l org-todo-state-tags-triggers)
  7431. changes)
  7432. (when (or (not state) (equal state ""))
  7433. (setq changes (append changes (cdr (assoc "" l)))))
  7434. (when (and (stringp state) (> (length state) 0))
  7435. (setq changes (append changes (cdr (assoc state l)))))
  7436. (when (member state org-not-done-keywords)
  7437. (setq changes (append changes (cdr (assoc 'todo l)))))
  7438. (when (member state org-done-keywords)
  7439. (setq changes (append changes (cdr (assoc 'done l)))))
  7440. (dolist (c changes)
  7441. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7442. (defun org-local-logging (value)
  7443. "Get logging settings from a property VALUE."
  7444. (let* (words w a)
  7445. ;; directly set the variables, they are already local.
  7446. (setq org-log-done nil
  7447. org-log-repeat nil
  7448. org-todo-log-states nil)
  7449. (setq words (org-split-string value))
  7450. (while (setq w (pop words))
  7451. (cond
  7452. ((setq a (assoc w org-startup-options))
  7453. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7454. (set (nth 1 a) (nth 2 a))))
  7455. ((setq a (org-extract-log-state-settings w))
  7456. (and (member (car a) org-todo-keywords-1)
  7457. (push a org-todo-log-states)))))))
  7458. (defun org-get-todo-sequence-head (kwd)
  7459. "Return the head of the TODO sequence to which KWD belongs.
  7460. If KWD is not set, check if there is a text property remembering the
  7461. right sequence."
  7462. (let (p)
  7463. (cond
  7464. ((not kwd)
  7465. (or (get-text-property (point-at-bol) 'org-todo-head)
  7466. (progn
  7467. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7468. nil (point-at-eol)))
  7469. (get-text-property p 'org-todo-head))))
  7470. ((not (member kwd org-todo-keywords-1))
  7471. (car org-todo-keywords-1))
  7472. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7473. (defun org-fast-todo-selection ()
  7474. "Fast TODO keyword selection with single keys.
  7475. Returns the new TODO keyword, or nil if no state change should occur."
  7476. (let* ((fulltable org-todo-key-alist)
  7477. (done-keywords org-done-keywords) ;; needed for the faces.
  7478. (maxlen (apply 'max (mapcar
  7479. (lambda (x)
  7480. (if (stringp (car x)) (string-width (car x)) 0))
  7481. fulltable)))
  7482. (expert nil)
  7483. (fwidth (+ maxlen 3 1 3))
  7484. (ncol (/ (- (window-width) 4) fwidth))
  7485. tg cnt e c tbl
  7486. groups ingroup)
  7487. (save-window-excursion
  7488. (if expert
  7489. (set-buffer (get-buffer-create " *Org todo*"))
  7490. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7491. (erase-buffer)
  7492. (org-set-local 'org-done-keywords done-keywords)
  7493. (setq tbl fulltable cnt 0)
  7494. (while (setq e (pop tbl))
  7495. (cond
  7496. ((equal e '(:startgroup))
  7497. (push '() groups) (setq ingroup t)
  7498. (when (not (= cnt 0))
  7499. (setq cnt 0)
  7500. (insert "\n"))
  7501. (insert "{ "))
  7502. ((equal e '(:endgroup))
  7503. (setq ingroup nil cnt 0)
  7504. (insert "}\n"))
  7505. (t
  7506. (setq tg (car e) c (cdr e))
  7507. (if ingroup (push tg (car groups)))
  7508. (setq tg (org-add-props tg nil 'face
  7509. (org-get-todo-face tg)))
  7510. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7511. (insert "[" c "] " tg (make-string
  7512. (- fwidth 4 (length tg)) ?\ ))
  7513. (when (= (setq cnt (1+ cnt)) ncol)
  7514. (insert "\n")
  7515. (if ingroup (insert " "))
  7516. (setq cnt 0)))))
  7517. (insert "\n")
  7518. (goto-char (point-min))
  7519. (if (not expert) (org-fit-window-to-buffer))
  7520. (message "[a-z..]:Set [SPC]:clear")
  7521. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7522. (cond
  7523. ((or (= c ?\C-g)
  7524. (and (= c ?q) (not (rassoc c fulltable))))
  7525. (setq quit-flag t))
  7526. ((= c ?\ ) nil)
  7527. ((setq e (rassoc c fulltable) tg (car e))
  7528. tg)
  7529. (t (setq quit-flag t))))))
  7530. (defun org-entry-is-todo-p ()
  7531. (member (org-get-todo-state) org-not-done-keywords))
  7532. (defun org-entry-is-done-p ()
  7533. (member (org-get-todo-state) org-done-keywords))
  7534. (defun org-get-todo-state ()
  7535. (save-excursion
  7536. (org-back-to-heading t)
  7537. (and (looking-at org-todo-line-regexp)
  7538. (match-end 2)
  7539. (match-string 2))))
  7540. (defun org-at-date-range-p (&optional inactive-ok)
  7541. "Is the cursor inside a date range?"
  7542. (interactive)
  7543. (save-excursion
  7544. (catch 'exit
  7545. (let ((pos (point)))
  7546. (skip-chars-backward "^[<\r\n")
  7547. (skip-chars-backward "<[")
  7548. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7549. (>= (match-end 0) pos)
  7550. (throw 'exit t))
  7551. (skip-chars-backward "^<[\r\n")
  7552. (skip-chars-backward "<[")
  7553. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7554. (>= (match-end 0) pos)
  7555. (throw 'exit t)))
  7556. nil)))
  7557. (defun org-get-repeat ()
  7558. "Check if there is a deadline/schedule with repeater in this entry."
  7559. (save-match-data
  7560. (save-excursion
  7561. (org-back-to-heading t)
  7562. (if (re-search-forward
  7563. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7564. (match-string 1)))))
  7565. (defvar org-last-changed-timestamp)
  7566. (defvar org-last-inserted-timestamp)
  7567. (defvar org-log-post-message)
  7568. (defvar org-log-note-purpose)
  7569. (defvar org-log-note-how)
  7570. (defvar org-log-note-extra)
  7571. (defun org-auto-repeat-maybe (done-word)
  7572. "Check if the current headline contains a repeated deadline/schedule.
  7573. If yes, set TODO state back to what it was and change the base date
  7574. of repeating deadline/scheduled time stamps to new date.
  7575. This function is run automatically after each state change to a DONE state."
  7576. ;; last-state is dynamically scoped into this function
  7577. (let* ((repeat (org-get-repeat))
  7578. (aa (assoc last-state org-todo-kwd-alist))
  7579. (interpret (nth 1 aa))
  7580. (head (nth 2 aa))
  7581. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7582. (msg "Entry repeats: ")
  7583. (org-log-done nil)
  7584. (org-todo-log-states nil)
  7585. (nshiftmax 10) (nshift 0)
  7586. re type n what ts mb0 time)
  7587. (when repeat
  7588. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7589. (org-todo (if (eq interpret 'type) last-state head))
  7590. (when org-log-repeat
  7591. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7592. (memq 'org-add-log-note post-command-hook))
  7593. ;; OK, we are already setup for some record
  7594. (if (eq org-log-repeat 'note)
  7595. ;; make sure we take a note, not only a time stamp
  7596. (setq org-log-note-how 'note))
  7597. ;; Set up for taking a record
  7598. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7599. 'findpos org-log-repeat)))
  7600. (org-back-to-heading t)
  7601. (org-add-planning-info nil nil 'closed)
  7602. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7603. org-deadline-time-regexp "\\)\\|\\("
  7604. org-ts-regexp "\\)"))
  7605. (while (re-search-forward
  7606. re (save-excursion (outline-next-heading) (point)) t)
  7607. (setq type (if (match-end 1) org-scheduled-string
  7608. (if (match-end 3) org-deadline-string "Plain:"))
  7609. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7610. mb0 (match-beginning 0))
  7611. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7612. (setq n (string-to-number (match-string 2 ts))
  7613. what (match-string 3 ts))
  7614. (if (equal what "w") (setq n (* n 7) what "d"))
  7615. ;; Preparation, see if we need to modify the start date for the change
  7616. (when (match-end 1)
  7617. (setq time (save-match-data (org-time-string-to-time ts)))
  7618. (cond
  7619. ((equal (match-string 1 ts) ".")
  7620. ;; Shift starting date to today
  7621. (org-timestamp-change
  7622. (- (time-to-days (current-time)) (time-to-days time))
  7623. 'day))
  7624. ((equal (match-string 1 ts) "+")
  7625. (while (or (= nshift 0)
  7626. (<= (time-to-days time) (time-to-days (current-time))))
  7627. (when (= (incf nshift) nshiftmax)
  7628. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7629. (error "Abort")))
  7630. (org-timestamp-change n (cdr (assoc what whata)))
  7631. (org-at-timestamp-p t)
  7632. (setq ts (match-string 1))
  7633. (setq time (save-match-data (org-time-string-to-time ts))))
  7634. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7635. ;; rematch, so that we have everything in place for the real shift
  7636. (org-at-timestamp-p t)
  7637. (setq ts (match-string 1))
  7638. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7639. (org-timestamp-change n (cdr (assoc what whata)))
  7640. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7641. (setq org-log-post-message msg)
  7642. (message "%s" msg))))
  7643. (defun org-show-todo-tree (arg)
  7644. "Make a compact tree which shows all headlines marked with TODO.
  7645. The tree will show the lines where the regexp matches, and all higher
  7646. headlines above the match.
  7647. With a \\[universal-argument] prefix, also show the DONE entries.
  7648. With a numeric prefix N, construct a sparse tree for the Nth element
  7649. of `org-todo-keywords-1'."
  7650. (interactive "P")
  7651. (let ((case-fold-search nil)
  7652. (kwd-re
  7653. (cond ((null arg) org-not-done-regexp)
  7654. ((equal arg '(4))
  7655. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7656. (mapcar 'list org-todo-keywords-1))))
  7657. (concat "\\("
  7658. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7659. "\\)\\>")))
  7660. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7661. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7662. org-todo-keywords-1)))
  7663. (t (error "Invalid prefix argument: %s" arg)))))
  7664. (message "%d TODO entries found"
  7665. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7666. (defun org-deadline (&optional remove time)
  7667. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7668. With argument REMOVE, remove any deadline from the item.
  7669. When TIME is set, it should be an internal time specification, and the
  7670. scheduling will use the corresponding date."
  7671. (interactive "P")
  7672. (if remove
  7673. (progn
  7674. (org-remove-timestamp-with-keyword org-deadline-string)
  7675. (message "Item no longer has a deadline."))
  7676. (if (org-get-repeat)
  7677. (error "Cannot change deadline on task with repeater, please do that by hand")
  7678. (org-add-planning-info 'deadline time 'closed)
  7679. (message "Deadline on %s" org-last-inserted-timestamp))))
  7680. (defun org-schedule (&optional remove time)
  7681. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7682. With argument REMOVE, remove any scheduling date from the item.
  7683. When TIME is set, it should be an internal time specification, and the
  7684. scheduling will use the corresponding date."
  7685. (interactive "P")
  7686. (if remove
  7687. (progn
  7688. (org-remove-timestamp-with-keyword org-scheduled-string)
  7689. (message "Item is no longer scheduled."))
  7690. (if (org-get-repeat)
  7691. (error "Cannot reschedule task with repeater, please do that by hand")
  7692. (org-add-planning-info 'scheduled time 'closed)
  7693. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7694. (defun org-remove-timestamp-with-keyword (keyword)
  7695. "Remove all time stamps with KEYWORD in the current entry."
  7696. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7697. beg)
  7698. (save-excursion
  7699. (org-back-to-heading t)
  7700. (setq beg (point))
  7701. (org-end-of-subtree t t)
  7702. (while (re-search-backward re beg t)
  7703. (replace-match "")
  7704. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7705. (equal (char-before) ?\ ))
  7706. (backward-delete-char 1)
  7707. (if (string-match "^[ \t]*$" (buffer-substring
  7708. (point-at-bol) (point-at-eol)))
  7709. (delete-region (point-at-bol)
  7710. (min (point-max) (1+ (point-at-eol))))))))))
  7711. (defun org-add-planning-info (what &optional time &rest remove)
  7712. "Insert new timestamp with keyword in the line directly after the headline.
  7713. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7714. If non is given, the user is prompted for a date.
  7715. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7716. be removed."
  7717. (interactive)
  7718. (let (org-time-was-given org-end-time-was-given ts
  7719. end default-time default-input)
  7720. (when (and (not time) (memq what '(scheduled deadline)))
  7721. ;; Try to get a default date/time from existing timestamp
  7722. (save-excursion
  7723. (org-back-to-heading t)
  7724. (setq end (save-excursion (outline-next-heading) (point)))
  7725. (when (re-search-forward (if (eq what 'scheduled)
  7726. org-scheduled-time-regexp
  7727. org-deadline-time-regexp)
  7728. end t)
  7729. (setq ts (match-string 1)
  7730. default-time
  7731. (apply 'encode-time (org-parse-time-string ts))
  7732. default-input (and ts (org-get-compact-tod ts))))))
  7733. (when what
  7734. ;; If necessary, get the time from the user
  7735. (setq time (or time (org-read-date nil 'to-time nil nil
  7736. default-time default-input))))
  7737. (when (and org-insert-labeled-timestamps-at-point
  7738. (member what '(scheduled deadline)))
  7739. (insert
  7740. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7741. (org-insert-time-stamp time org-time-was-given
  7742. nil nil nil (list org-end-time-was-given))
  7743. (setq what nil))
  7744. (save-excursion
  7745. (save-restriction
  7746. (let (col list elt ts buffer-invisibility-spec)
  7747. (org-back-to-heading t)
  7748. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7749. (goto-char (match-end 1))
  7750. (setq col (current-column))
  7751. (goto-char (match-end 0))
  7752. (if (eobp) (insert "\n") (forward-char 1))
  7753. (if (and (not (looking-at outline-regexp))
  7754. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7755. "[^\r\n]*"))
  7756. (not (equal (match-string 1) org-clock-string)))
  7757. (narrow-to-region (match-beginning 0) (match-end 0))
  7758. (insert-before-markers "\n")
  7759. (backward-char 1)
  7760. (narrow-to-region (point) (point))
  7761. (and org-adapt-indentation (org-indent-to-column col)))
  7762. ;; Check if we have to remove something.
  7763. (setq list (cons what remove))
  7764. (while list
  7765. (setq elt (pop list))
  7766. (goto-char (point-min))
  7767. (when (or (and (eq elt 'scheduled)
  7768. (re-search-forward org-scheduled-time-regexp nil t))
  7769. (and (eq elt 'deadline)
  7770. (re-search-forward org-deadline-time-regexp nil t))
  7771. (and (eq elt 'closed)
  7772. (re-search-forward org-closed-time-regexp nil t)))
  7773. (replace-match "")
  7774. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7775. (if (looking-at " +") (replace-match ""))))
  7776. (goto-char (point-max))
  7777. (when what
  7778. (insert
  7779. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7780. (cond ((eq what 'scheduled) org-scheduled-string)
  7781. ((eq what 'deadline) org-deadline-string)
  7782. ((eq what 'closed) org-closed-string))
  7783. " ")
  7784. (setq ts (org-insert-time-stamp
  7785. time
  7786. (or org-time-was-given
  7787. (and (eq what 'closed) org-log-done-with-time))
  7788. (eq what 'closed)
  7789. nil nil (list org-end-time-was-given)))
  7790. (end-of-line 1))
  7791. (goto-char (point-min))
  7792. (widen)
  7793. (if (and (looking-at "[ \t]+\n")
  7794. (equal (char-before) ?\n))
  7795. (delete-region (1- (point)) (point-at-eol)))
  7796. ts)))))
  7797. (defvar org-log-note-marker (make-marker))
  7798. (defvar org-log-note-purpose nil)
  7799. (defvar org-log-note-state nil)
  7800. (defvar org-log-note-how nil)
  7801. (defvar org-log-note-extra nil)
  7802. (defvar org-log-note-window-configuration nil)
  7803. (defvar org-log-note-return-to (make-marker))
  7804. (defvar org-log-post-message nil
  7805. "Message to be displayed after a log note has been stored.
  7806. The auto-repeater uses this.")
  7807. (defun org-add-note ()
  7808. "Add a note to the current entry.
  7809. This is done in the same way as adding a state change note."
  7810. (interactive)
  7811. (org-add-log-setup 'note nil 'findpos nil))
  7812. (defvar org-property-end-re)
  7813. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7814. "Set up the post command hook to take a note.
  7815. If this is about to TODO state change, the new state is expected in STATE.
  7816. When FINDPOS is non-nil, find the correct position for the note in
  7817. the current entry. If not, assume that it can be inserted at point.
  7818. HOW is an indicator what kind of note should be created.
  7819. EXTRA is additional text that will be inserted into the notes buffer."
  7820. (save-restriction
  7821. (save-excursion
  7822. (when findpos
  7823. (org-back-to-heading t)
  7824. (narrow-to-region (point) (save-excursion
  7825. (outline-next-heading) (point)))
  7826. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7827. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7828. "[^\r\n]*\\)?"))
  7829. (goto-char (match-end 0))
  7830. (when (and org-log-state-notes-insert-after-drawers
  7831. (save-excursion
  7832. (forward-line) (looking-at org-drawer-regexp)))
  7833. (progn (forward-line)
  7834. (while (looking-at org-drawer-regexp)
  7835. (goto-char (match-end 0))
  7836. (re-search-forward org-property-end-re (point-max) t)
  7837. (forward-line))
  7838. (forward-line -1)))
  7839. (unless org-log-states-order-reversed
  7840. (and (= (char-after) ?\n) (forward-char 1))
  7841. (org-skip-over-state-notes)
  7842. (skip-chars-backward " \t\n\r")))
  7843. (move-marker org-log-note-marker (point))
  7844. (setq org-log-note-purpose purpose
  7845. org-log-note-state state
  7846. org-log-note-how how
  7847. org-log-note-extra extra)
  7848. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7849. (defun org-skip-over-state-notes ()
  7850. "Skip past the list of State notes in an entry."
  7851. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7852. (while (looking-at "[ \t]*- State")
  7853. (condition-case nil
  7854. (org-next-item)
  7855. (error (org-end-of-item)))))
  7856. (defun org-add-log-note (&optional purpose)
  7857. "Pop up a window for taking a note, and add this note later at point."
  7858. (remove-hook 'post-command-hook 'org-add-log-note)
  7859. (setq org-log-note-window-configuration (current-window-configuration))
  7860. (delete-other-windows)
  7861. (move-marker org-log-note-return-to (point))
  7862. (switch-to-buffer (marker-buffer org-log-note-marker))
  7863. (goto-char org-log-note-marker)
  7864. (org-switch-to-buffer-other-window "*Org Note*")
  7865. (erase-buffer)
  7866. (if (memq org-log-note-how '(time state))
  7867. (let (current-prefix-arg) (org-store-log-note))
  7868. (let ((org-inhibit-startup t)) (org-mode))
  7869. (insert (format "# Insert note for %s.
  7870. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7871. (cond
  7872. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7873. ((eq org-log-note-purpose 'done) "closed todo item")
  7874. ((eq org-log-note-purpose 'state)
  7875. (format "state change to \"%s\"" org-log-note-state))
  7876. ((eq org-log-note-purpose 'note)
  7877. "this entry")
  7878. (t (error "This should not happen")))))
  7879. (if org-log-note-extra (insert org-log-note-extra))
  7880. (org-set-local 'org-finish-function 'org-store-log-note)))
  7881. (defvar org-note-abort nil) ; dynamically scoped
  7882. (defun org-store-log-note ()
  7883. "Finish taking a log note, and insert it to where it belongs."
  7884. (let ((txt (buffer-string))
  7885. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7886. lines ind)
  7887. (kill-buffer (current-buffer))
  7888. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7889. (setq txt (replace-match "" t t txt)))
  7890. (if (string-match "\\s-+\\'" txt)
  7891. (setq txt (replace-match "" t t txt)))
  7892. (setq lines (org-split-string txt "\n"))
  7893. (when (and note (string-match "\\S-" note))
  7894. (setq note
  7895. (org-replace-escapes
  7896. note
  7897. (list (cons "%u" (user-login-name))
  7898. (cons "%U" user-full-name)
  7899. (cons "%t" (format-time-string
  7900. (org-time-stamp-format 'long 'inactive)
  7901. (current-time)))
  7902. (cons "%s" (if org-log-note-state
  7903. (concat "\"" org-log-note-state "\"")
  7904. "")))))
  7905. (if lines (setq note (concat note " \\\\")))
  7906. (push note lines))
  7907. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7908. (when lines
  7909. (save-excursion
  7910. (set-buffer (marker-buffer org-log-note-marker))
  7911. (save-excursion
  7912. (goto-char org-log-note-marker)
  7913. (move-marker org-log-note-marker nil)
  7914. (end-of-line 1)
  7915. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7916. (indent-relative nil)
  7917. (insert "- " (pop lines))
  7918. (org-indent-line-function)
  7919. (beginning-of-line 1)
  7920. (looking-at "[ \t]*")
  7921. (setq ind (concat (match-string 0) " "))
  7922. (end-of-line 1)
  7923. (while lines (insert "\n" ind (pop lines)))))))
  7924. (set-window-configuration org-log-note-window-configuration)
  7925. (with-current-buffer (marker-buffer org-log-note-return-to)
  7926. (goto-char org-log-note-return-to))
  7927. (move-marker org-log-note-return-to nil)
  7928. (and org-log-post-message (message "%s" org-log-post-message)))
  7929. (defun org-sparse-tree (&optional arg)
  7930. "Create a sparse tree, prompt for the details.
  7931. This command can create sparse trees. You first need to select the type
  7932. of match used to create the tree:
  7933. t Show entries with a specific TODO keyword.
  7934. T Show entries selected by a tags match.
  7935. p Enter a property name and its value (both with completion on existing
  7936. names/values) and show entries with that property.
  7937. r Show entries matching a regular expression
  7938. d Show deadlines due within `org-deadline-warning-days'."
  7939. (interactive "P")
  7940. (let (ans kwd value)
  7941. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7942. (setq ans (read-char-exclusive))
  7943. (cond
  7944. ((equal ans ?d)
  7945. (call-interactively 'org-check-deadlines))
  7946. ((equal ans ?b)
  7947. (call-interactively 'org-check-before-date))
  7948. ((equal ans ?t)
  7949. (org-show-todo-tree '(4)))
  7950. ((equal ans ?T)
  7951. (call-interactively 'org-tags-sparse-tree))
  7952. ((member ans '(?p ?P))
  7953. (setq kwd (org-ido-completing-read "Property: "
  7954. (mapcar 'list (org-buffer-property-keys))))
  7955. (setq value (org-ido-completing-read "Value: "
  7956. (mapcar 'list (org-property-values kwd))))
  7957. (unless (string-match "\\`{.*}\\'" value)
  7958. (setq value (concat "\"" value "\"")))
  7959. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7960. ((member ans '(?r ?R ?/))
  7961. (call-interactively 'org-occur))
  7962. (t (error "No such sparse tree command \"%c\"" ans)))))
  7963. (defvar org-occur-highlights nil
  7964. "List of overlays used for occur matches.")
  7965. (make-variable-buffer-local 'org-occur-highlights)
  7966. (defvar org-occur-parameters nil
  7967. "Parameters of the active org-occur calls.
  7968. This is a list, each call to org-occur pushes as cons cell,
  7969. containing the regular expression and the callback, onto the list.
  7970. The list can contain several entries if `org-occur' has been called
  7971. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7972. will only contain one set of parameters. When the highlights are
  7973. removed (for example with `C-c C-c', or with the next edit (depending
  7974. on `org-remove-highlights-with-change'), this variable is emptied
  7975. as well.")
  7976. (make-variable-buffer-local 'org-occur-parameters)
  7977. (defun org-occur (regexp &optional keep-previous callback)
  7978. "Make a compact tree which shows all matches of REGEXP.
  7979. The tree will show the lines where the regexp matches, and all higher
  7980. headlines above the match. It will also show the heading after the match,
  7981. to make sure editing the matching entry is easy.
  7982. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7983. call to `org-occur' will be kept, to allow stacking of calls to this
  7984. command.
  7985. If CALLBACK is non-nil, it is a function which is called to confirm
  7986. that the match should indeed be shown."
  7987. (interactive "sRegexp: \nP")
  7988. (unless keep-previous
  7989. (org-remove-occur-highlights nil nil t))
  7990. (push (cons regexp callback) org-occur-parameters)
  7991. (let ((cnt 0))
  7992. (save-excursion
  7993. (goto-char (point-min))
  7994. (if (or (not keep-previous) ; do not want to keep
  7995. (not org-occur-highlights)) ; no previous matches
  7996. ;; hide everything
  7997. (org-overview))
  7998. (while (re-search-forward regexp nil t)
  7999. (when (or (not callback)
  8000. (save-match-data (funcall callback)))
  8001. (setq cnt (1+ cnt))
  8002. (when org-highlight-sparse-tree-matches
  8003. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8004. (org-show-context 'occur-tree))))
  8005. (when org-remove-highlights-with-change
  8006. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8007. nil 'local))
  8008. (unless org-sparse-tree-open-archived-trees
  8009. (org-hide-archived-subtrees (point-min) (point-max)))
  8010. (run-hooks 'org-occur-hook)
  8011. (if (interactive-p)
  8012. (message "%d match(es) for regexp %s" cnt regexp))
  8013. cnt))
  8014. (defun org-show-context (&optional key)
  8015. "Make sure point and context and visible.
  8016. How much context is shown depends upon the variables
  8017. `org-show-hierarchy-above', `org-show-following-heading'. and
  8018. `org-show-siblings'."
  8019. (let ((heading-p (org-on-heading-p t))
  8020. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8021. (following-p (org-get-alist-option org-show-following-heading key))
  8022. (entry-p (org-get-alist-option org-show-entry-below key))
  8023. (siblings-p (org-get-alist-option org-show-siblings key)))
  8024. (catch 'exit
  8025. ;; Show heading or entry text
  8026. (if (and heading-p (not entry-p))
  8027. (org-flag-heading nil) ; only show the heading
  8028. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8029. (org-show-hidden-entry))) ; show entire entry
  8030. (when following-p
  8031. ;; Show next sibling, or heading below text
  8032. (save-excursion
  8033. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8034. (org-flag-heading nil))))
  8035. (when siblings-p (org-show-siblings))
  8036. (when hierarchy-p
  8037. ;; show all higher headings, possibly with siblings
  8038. (save-excursion
  8039. (while (and (condition-case nil
  8040. (progn (org-up-heading-all 1) t)
  8041. (error nil))
  8042. (not (bobp)))
  8043. (org-flag-heading nil)
  8044. (when siblings-p (org-show-siblings))))))))
  8045. (defun org-reveal (&optional siblings)
  8046. "Show current entry, hierarchy above it, and the following headline.
  8047. This can be used to show a consistent set of context around locations
  8048. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8049. not t for the search context.
  8050. With optional argument SIBLINGS, on each level of the hierarchy all
  8051. siblings are shown. This repairs the tree structure to what it would
  8052. look like when opened with hierarchical calls to `org-cycle'."
  8053. (interactive "P")
  8054. (let ((org-show-hierarchy-above t)
  8055. (org-show-following-heading t)
  8056. (org-show-siblings (if siblings t org-show-siblings)))
  8057. (org-show-context nil)))
  8058. (defun org-highlight-new-match (beg end)
  8059. "Highlight from BEG to END and mark the highlight is an occur headline."
  8060. (let ((ov (org-make-overlay beg end)))
  8061. (org-overlay-put ov 'face 'secondary-selection)
  8062. (push ov org-occur-highlights)))
  8063. (defun org-remove-occur-highlights (&optional beg end noremove)
  8064. "Remove the occur highlights from the buffer.
  8065. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8066. from the `before-change-functions' in the current buffer."
  8067. (interactive)
  8068. (unless org-inhibit-highlight-removal
  8069. (mapc 'org-delete-overlay org-occur-highlights)
  8070. (setq org-occur-highlights nil)
  8071. (setq org-occur-parameters nil)
  8072. (unless noremove
  8073. (remove-hook 'before-change-functions
  8074. 'org-remove-occur-highlights 'local))))
  8075. ;;;; Priorities
  8076. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8077. "Regular expression matching the priority indicator.")
  8078. (defvar org-remove-priority-next-time nil)
  8079. (defun org-priority-up ()
  8080. "Increase the priority of the current item."
  8081. (interactive)
  8082. (org-priority 'up))
  8083. (defun org-priority-down ()
  8084. "Decrease the priority of the current item."
  8085. (interactive)
  8086. (org-priority 'down))
  8087. (defun org-priority (&optional action)
  8088. "Change the priority of an item by ARG.
  8089. ACTION can be `set', `up', `down', or a character."
  8090. (interactive)
  8091. (setq action (or action 'set))
  8092. (let (current new news have remove)
  8093. (save-excursion
  8094. (org-back-to-heading)
  8095. (if (looking-at org-priority-regexp)
  8096. (setq current (string-to-char (match-string 2))
  8097. have t)
  8098. (setq current org-default-priority))
  8099. (cond
  8100. ((or (eq action 'set)
  8101. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8102. (if (not (eq action 'set))
  8103. (setq new action)
  8104. (message "Priority %c-%c, SPC to remove: "
  8105. org-highest-priority org-lowest-priority)
  8106. (setq new (read-char-exclusive)))
  8107. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8108. (= (upcase org-lowest-priority) org-lowest-priority))
  8109. (setq new (upcase new)))
  8110. (cond ((equal new ?\ ) (setq remove t))
  8111. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8112. (error "Priority must be between `%c' and `%c'"
  8113. org-highest-priority org-lowest-priority))))
  8114. ((eq action 'up)
  8115. (if (and (not have) (eq last-command this-command))
  8116. (setq new org-lowest-priority)
  8117. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8118. org-default-priority (1- current)))))
  8119. ((eq action 'down)
  8120. (if (and (not have) (eq last-command this-command))
  8121. (setq new org-highest-priority)
  8122. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8123. org-default-priority (1+ current)))))
  8124. (t (error "Invalid action")))
  8125. (if (or (< (upcase new) org-highest-priority)
  8126. (> (upcase new) org-lowest-priority))
  8127. (setq remove t))
  8128. (setq news (format "%c" new))
  8129. (if have
  8130. (if remove
  8131. (replace-match "" t t nil 1)
  8132. (replace-match news t t nil 2))
  8133. (if remove
  8134. (error "No priority cookie found in line")
  8135. (looking-at org-todo-line-regexp)
  8136. (if (match-end 2)
  8137. (progn
  8138. (goto-char (match-end 2))
  8139. (insert " [#" news "]"))
  8140. (goto-char (match-beginning 3))
  8141. (insert "[#" news "] ")))))
  8142. (org-preserve-lc (org-set-tags nil 'align))
  8143. (if remove
  8144. (message "Priority removed")
  8145. (message "Priority of current item set to %s" news))))
  8146. (defun org-get-priority (s)
  8147. "Find priority cookie and return priority."
  8148. (save-match-data
  8149. (if (not (string-match org-priority-regexp s))
  8150. (* 1000 (- org-lowest-priority org-default-priority))
  8151. (* 1000 (- org-lowest-priority
  8152. (string-to-char (match-string 2 s)))))))
  8153. ;;;; Tags
  8154. (defvar org-agenda-archives-mode)
  8155. (defun org-scan-tags (action matcher &optional todo-only)
  8156. "Scan headline tags with inheritance and produce output ACTION.
  8157. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8158. or `agenda' to produce an entry list for an agenda view. It can also be
  8159. a Lisp form or a function that should be called at each matched headline, in
  8160. this case the return value is a list of all return values from these calls.
  8161. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8162. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8163. only lines with a TODO keyword are included in the output."
  8164. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8165. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8166. (org-re
  8167. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8168. (props (list 'face 'default
  8169. 'done-face 'org-done
  8170. 'undone-face 'default
  8171. 'mouse-face 'highlight
  8172. 'org-not-done-regexp org-not-done-regexp
  8173. 'org-todo-regexp org-todo-regexp
  8174. 'keymap org-agenda-keymap
  8175. 'help-echo
  8176. (format "mouse-2 or RET jump to org file %s"
  8177. (abbreviate-file-name
  8178. (or (buffer-file-name (buffer-base-buffer))
  8179. (buffer-name (buffer-base-buffer)))))))
  8180. (case-fold-search nil)
  8181. lspos tags tags-list
  8182. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8183. (llast 0) rtn rtn1 level category i txt
  8184. todo marker entry priority)
  8185. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8186. (setq action (list 'lambda nil action)))
  8187. (save-excursion
  8188. (goto-char (point-min))
  8189. (when (eq action 'sparse-tree)
  8190. (org-overview)
  8191. (org-remove-occur-highlights))
  8192. (while (re-search-forward re nil t)
  8193. (catch :skip
  8194. (setq todo (if (match-end 1) (match-string 2))
  8195. tags (if (match-end 4) (match-string 4)))
  8196. (goto-char (setq lspos (1+ (match-beginning 0))))
  8197. (setq level (org-reduced-level (funcall outline-level))
  8198. category (org-get-category))
  8199. (setq i llast llast level)
  8200. ;; remove tag lists from same and sublevels
  8201. (while (>= i level)
  8202. (when (setq entry (assoc i tags-alist))
  8203. (setq tags-alist (delete entry tags-alist)))
  8204. (setq i (1- i)))
  8205. ;; add the next tags
  8206. (when tags
  8207. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8208. tags-alist
  8209. (cons (cons level tags) tags-alist)))
  8210. ;; compile tags for current headline
  8211. (setq tags-list
  8212. (if org-use-tag-inheritance
  8213. (apply 'append (mapcar 'cdr tags-alist))
  8214. tags))
  8215. (when (and tags org-use-tag-inheritance
  8216. (not (eq t org-use-tag-inheritance)))
  8217. ;; selective inheritance, remove uninherited ones
  8218. (setcdr (car tags-alist)
  8219. (org-remove-uniherited-tags (cdar tags-alist))))
  8220. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8221. (let ((case-fold-search t)) (eval matcher))
  8222. (or
  8223. (not (member org-archive-tag tags-list))
  8224. ;; we have an archive tag, should we use this anyway?
  8225. (or (not org-agenda-skip-archived-trees)
  8226. (and (eq action 'agenda) org-agenda-archives-mode))))
  8227. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8228. ;; select this headline
  8229. (cond
  8230. ((eq action 'sparse-tree)
  8231. (and org-highlight-sparse-tree-matches
  8232. (org-get-heading) (match-end 0)
  8233. (org-highlight-new-match
  8234. (match-beginning 0) (match-beginning 1)))
  8235. (org-show-context 'tags-tree))
  8236. ((eq action 'agenda)
  8237. (setq txt (org-format-agenda-item
  8238. ""
  8239. (concat
  8240. (if org-tags-match-list-sublevels
  8241. (make-string (1- level) ?.) "")
  8242. (org-get-heading))
  8243. category tags-list)
  8244. priority (org-get-priority txt))
  8245. (goto-char lspos)
  8246. (setq marker (org-agenda-new-marker))
  8247. (org-add-props txt props
  8248. 'org-marker marker 'org-hd-marker marker 'org-category category
  8249. 'priority priority 'type "tagsmatch")
  8250. (push txt rtn))
  8251. ((functionp action)
  8252. (save-excursion
  8253. (setq rtn1 (funcall action))
  8254. (push rtn1 rtn))
  8255. (goto-char (point-at-eol)))
  8256. (t (error "Invalid action")))
  8257. ;; if we are to skip sublevels, jump to end of subtree
  8258. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8259. (when (and (eq action 'sparse-tree)
  8260. (not org-sparse-tree-open-archived-trees))
  8261. (org-hide-archived-subtrees (point-min) (point-max)))
  8262. (nreverse rtn)))
  8263. (defun org-remove-uniherited-tags (tags)
  8264. "Remove all tags that are not inherited from the list TAGS."
  8265. (cond
  8266. ((eq org-use-tag-inheritance t) tags)
  8267. ((not org-use-tag-inheritance) nil)
  8268. ((stringp org-use-tag-inheritance)
  8269. (delq nil (mapcar
  8270. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8271. tags)))
  8272. ((listp org-use-tag-inheritance)
  8273. (delq nil (mapcar
  8274. (lambda (x) (if (member x org-use-tag-inheritance) x nil))
  8275. tags)))))
  8276. (defvar todo-only) ;; dynamically scoped
  8277. (defun org-tags-sparse-tree (&optional todo-only match)
  8278. "Create a sparse tree according to tags string MATCH.
  8279. MATCH can contain positive and negative selection of tags, like
  8280. \"+WORK+URGENT-WITHBOSS\".
  8281. If optional argument TODO_ONLY is non-nil, only select lines that are
  8282. also TODO lines."
  8283. (interactive "P")
  8284. (org-prepare-agenda-buffers (list (current-buffer)))
  8285. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8286. (defvar org-cached-props nil)
  8287. (defun org-cached-entry-get (pom property)
  8288. (if (or (eq t org-use-property-inheritance)
  8289. (and (stringp org-use-property-inheritance)
  8290. (string-match org-use-property-inheritance property))
  8291. (and (listp org-use-property-inheritance)
  8292. (member property org-use-property-inheritance)))
  8293. ;; Caching is not possible, check it directly
  8294. (org-entry-get pom property 'inherit)
  8295. ;; Get all properties, so that we can do complicated checks easily
  8296. (cdr (assoc property (or org-cached-props
  8297. (setq org-cached-props
  8298. (org-entry-properties pom)))))))
  8299. (defun org-global-tags-completion-table (&optional files)
  8300. "Return the list of all tags in all agenda buffer/files."
  8301. (save-excursion
  8302. (org-uniquify
  8303. (delq nil
  8304. (apply 'append
  8305. (mapcar
  8306. (lambda (file)
  8307. (set-buffer (find-file-noselect file))
  8308. (append (org-get-buffer-tags)
  8309. (mapcar (lambda (x) (if (stringp (car-safe x))
  8310. (list (car-safe x)) nil))
  8311. org-tag-alist)))
  8312. (if (and files (car files))
  8313. files
  8314. (org-agenda-files))))))))
  8315. (defun org-make-tags-matcher (match)
  8316. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8317. ;; todo-only is scoped dynamically into this function, and the function
  8318. ;; may change it it the matcher asksk for it.
  8319. (unless match
  8320. ;; Get a new match request, with completion
  8321. (let ((org-last-tags-completion-table
  8322. (org-global-tags-completion-table)))
  8323. (setq match (org-ido-completing-read
  8324. "Match: " 'org-tags-completion-function nil nil nil
  8325. 'org-tags-history))))
  8326. ;; Parse the string and create a lisp form
  8327. (let ((match0 match)
  8328. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8329. minus tag mm
  8330. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8331. orterms term orlist re-p str-p level-p level-op time-p
  8332. prop-p pn pv po cat-p gv rest)
  8333. (if (string-match "/+" match)
  8334. ;; match contains also a todo-matching request
  8335. (progn
  8336. (setq tagsmatch (substring match 0 (match-beginning 0))
  8337. todomatch (substring match (match-end 0)))
  8338. (if (string-match "^!" todomatch)
  8339. (setq todo-only t todomatch (substring todomatch 1)))
  8340. (if (string-match "^\\s-*$" todomatch)
  8341. (setq todomatch nil)))
  8342. ;; only matching tags
  8343. (setq tagsmatch match todomatch nil))
  8344. ;; Make the tags matcher
  8345. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8346. (setq tagsmatcher t)
  8347. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8348. (while (setq term (pop orterms))
  8349. (while (and (equal (substring term -1) "\\") orterms)
  8350. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8351. (while (string-match re term)
  8352. (setq rest (substring term (match-end 0))
  8353. minus (and (match-end 1)
  8354. (equal (match-string 1 term) "-"))
  8355. tag (match-string 2 term)
  8356. re-p (equal (string-to-char tag) ?{)
  8357. level-p (match-end 4)
  8358. prop-p (match-end 5)
  8359. mm (cond
  8360. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8361. (level-p
  8362. (setq level-op (org-op-to-function (match-string 3 term)))
  8363. `(,level-op level ,(string-to-number
  8364. (match-string 4 term))))
  8365. (prop-p
  8366. (setq pn (match-string 5 term)
  8367. po (match-string 6 term)
  8368. pv (match-string 7 term)
  8369. cat-p (equal pn "CATEGORY")
  8370. re-p (equal (string-to-char pv) ?{)
  8371. str-p (equal (string-to-char pv) ?\")
  8372. time-p (save-match-data
  8373. (string-match "^\"[[<].*[]>]\"$" pv))
  8374. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8375. (if time-p (setq pv (org-matcher-time pv)))
  8376. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8377. (cond
  8378. ((equal pn "CATEGORY")
  8379. (setq gv '(get-text-property (point) 'org-category)))
  8380. ((equal pn "TODO")
  8381. (setq gv 'todo))
  8382. (t
  8383. (setq gv `(org-cached-entry-get nil ,pn))))
  8384. (if re-p
  8385. (if (eq po 'org<>)
  8386. `(not (string-match ,pv (or ,gv "")))
  8387. `(string-match ,pv (or ,gv "")))
  8388. (if str-p
  8389. `(,po (or ,gv "") ,pv)
  8390. `(,po (string-to-number (or ,gv ""))
  8391. ,(string-to-number pv) ))))
  8392. (t `(member ,(downcase tag) tags-list)))
  8393. mm (if minus (list 'not mm) mm)
  8394. term rest)
  8395. (push mm tagsmatcher))
  8396. (push (if (> (length tagsmatcher) 1)
  8397. (cons 'and tagsmatcher)
  8398. (car tagsmatcher))
  8399. orlist)
  8400. (setq tagsmatcher nil))
  8401. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8402. (setq tagsmatcher
  8403. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8404. ;; Make the todo matcher
  8405. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8406. (setq todomatcher t)
  8407. (setq orterms (org-split-string todomatch "|") orlist nil)
  8408. (while (setq term (pop orterms))
  8409. (while (string-match re term)
  8410. (setq minus (and (match-end 1)
  8411. (equal (match-string 1 term) "-"))
  8412. kwd (match-string 2 term)
  8413. re-p (equal (string-to-char kwd) ?{)
  8414. term (substring term (match-end 0))
  8415. mm (if re-p
  8416. `(string-match ,(substring kwd 1 -1) todo)
  8417. (list 'equal 'todo kwd))
  8418. mm (if minus (list 'not mm) mm))
  8419. (push mm todomatcher))
  8420. (push (if (> (length todomatcher) 1)
  8421. (cons 'and todomatcher)
  8422. (car todomatcher))
  8423. orlist)
  8424. (setq todomatcher nil))
  8425. (setq todomatcher (if (> (length orlist) 1)
  8426. (cons 'or orlist) (car orlist))))
  8427. ;; Return the string and lisp forms of the matcher
  8428. (setq matcher (if todomatcher
  8429. (list 'and tagsmatcher todomatcher)
  8430. tagsmatcher))
  8431. (cons match0 matcher)))
  8432. (defun org-op-to-function (op &optional stringp)
  8433. "Turn an operator into the appropriate function."
  8434. (setq op
  8435. (cond
  8436. ((equal op "<" ) '(< string< org-time<))
  8437. ((equal op ">" ) '(> org-string> org-time>))
  8438. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8439. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8440. ((member op '("=" "==")) '(= string= org-time=))
  8441. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8442. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8443. (defun org<> (a b) (not (= a b)))
  8444. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8445. (defun org-string>= (a b) (not (string< a b)))
  8446. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8447. (defun org-string<> (a b) (not (string= a b)))
  8448. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8449. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8450. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8451. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8452. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8453. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8454. (defun org-2ft (s)
  8455. "Convert S to a floating point time.
  8456. If S is already a number, just return it. If it is a string, parse
  8457. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8458. (cond
  8459. ((numberp s) s)
  8460. ((stringp s)
  8461. (condition-case nil
  8462. (float-time (apply 'encode-time (org-parse-time-string s)))
  8463. (error 0.)))
  8464. (t 0.)))
  8465. (defun org-time-today ()
  8466. "Time in seconds today at 0:00.
  8467. Returns the float number of seconds since the beginning of the
  8468. epoch to the beginning of today (00:00)."
  8469. (float-time (apply 'encode-time
  8470. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8471. (defun org-matcher-time (s)
  8472. (cond
  8473. ((string= s "<now>") (float-time))
  8474. ((string= s "<today>") (org-time-today))
  8475. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8476. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8477. (t (org-2ft s))))
  8478. (defun org-match-any-p (re list)
  8479. "Does re match any element of list?"
  8480. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8481. (delq nil list))
  8482. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8483. (defvar org-tags-overlay (org-make-overlay 1 1))
  8484. (org-detach-overlay org-tags-overlay)
  8485. (defun org-get-local-tags-at (&optional pos)
  8486. "Get a list of tags defined in the current headline."
  8487. (org-get-tags-at pos 'local))
  8488. (defun org-get-local-tags ()
  8489. "Get a list of tags defined in the current headline."
  8490. (org-get-tags-at nil 'local))
  8491. (defun org-get-tags-at (&optional pos local)
  8492. "Get a list of all headline tags applicable at POS.
  8493. POS defaults to point. If tags are inherited, the list contains
  8494. the targets in the same sequence as the headlines appear, i.e.
  8495. the tags of the current headline come last.
  8496. When LOCAL is non-nil, only return tags from the current headline,
  8497. ignore inherited ones."
  8498. (interactive)
  8499. (let (tags ltags lastpos parent)
  8500. (save-excursion
  8501. (save-restriction
  8502. (widen)
  8503. (goto-char (or pos (point)))
  8504. (save-match-data
  8505. (catch 'done
  8506. (condition-case nil
  8507. (progn
  8508. (org-back-to-heading t)
  8509. (while (not (equal lastpos (point)))
  8510. (setq lastpos (point))
  8511. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8512. (setq ltags (org-split-string
  8513. (org-match-string-no-properties 1) ":"))
  8514. (setq tags (append
  8515. (if parent
  8516. (org-remove-uniherited-tags ltags)
  8517. ltags)
  8518. tags)))
  8519. (or org-use-tag-inheritance (throw 'done t))
  8520. (if local (throw 'done t))
  8521. (org-up-heading-all 1)
  8522. (setq parent t)))
  8523. (error nil)))))
  8524. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8525. (defun org-toggle-tag (tag &optional onoff)
  8526. "Toggle the tag TAG for the current line.
  8527. If ONOFF is `on' or `off', don't toggle but set to this state."
  8528. (unless (org-on-heading-p t) (error "Not on headling"))
  8529. (let (res current)
  8530. (save-excursion
  8531. (beginning-of-line)
  8532. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8533. (point-at-eol) t)
  8534. (progn
  8535. (setq current (match-string 1))
  8536. (replace-match ""))
  8537. (setq current ""))
  8538. (setq current (nreverse (org-split-string current ":")))
  8539. (cond
  8540. ((eq onoff 'on)
  8541. (setq res t)
  8542. (or (member tag current) (push tag current)))
  8543. ((eq onoff 'off)
  8544. (or (not (member tag current)) (setq current (delete tag current))))
  8545. (t (if (member tag current)
  8546. (setq current (delete tag current))
  8547. (setq res t)
  8548. (push tag current))))
  8549. (end-of-line 1)
  8550. (if current
  8551. (progn
  8552. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8553. (org-set-tags nil t))
  8554. (delete-horizontal-space))
  8555. (run-hooks 'org-after-tags-change-hook))
  8556. res))
  8557. (defun org-align-tags-here (to-col)
  8558. ;; Assumes that this is a headline
  8559. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8560. (beginning-of-line 1)
  8561. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8562. (< pos (match-beginning 2)))
  8563. (progn
  8564. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8565. (goto-char (match-beginning 1))
  8566. (insert " ")
  8567. (delete-region (point) (1+ (match-beginning 2)))
  8568. (setq ncol (max (1+ (current-column))
  8569. (1+ col)
  8570. (if (> to-col 0)
  8571. to-col
  8572. (- (abs to-col) tags-l))))
  8573. (setq p (point))
  8574. (insert (make-string (- ncol (current-column)) ?\ ))
  8575. (setq ncol (current-column))
  8576. (when indent-tabs-mode (tabify p (point-at-eol)))
  8577. (org-move-to-column (min ncol col) t))
  8578. (goto-char pos))))
  8579. (defun org-set-tags-command (&optional arg just-align)
  8580. "Call the set-tags command for the current entry."
  8581. (interactive "P")
  8582. (if (org-on-heading-p)
  8583. (org-set-tags arg just-align)
  8584. (save-excursion
  8585. (org-back-to-heading t)
  8586. (org-set-tags arg just-align))))
  8587. (defun org-set-tags (&optional arg just-align)
  8588. "Set the tags for the current headline.
  8589. With prefix ARG, realign all tags in headings in the current buffer."
  8590. (interactive "P")
  8591. (let* ((re (concat "^" outline-regexp))
  8592. (current (org-get-tags-string))
  8593. (col (current-column))
  8594. (org-setting-tags t)
  8595. table current-tags inherited-tags ; computed below when needed
  8596. tags p0 c0 c1 rpl)
  8597. (if arg
  8598. (save-excursion
  8599. (goto-char (point-min))
  8600. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8601. (while (re-search-forward re nil t)
  8602. (org-set-tags nil t)
  8603. (end-of-line 1)))
  8604. (message "All tags realigned to column %d" org-tags-column))
  8605. (if just-align
  8606. (setq tags current)
  8607. ;; Get a new set of tags from the user
  8608. (save-excursion
  8609. (setq table (or org-tag-alist (org-get-buffer-tags))
  8610. org-last-tags-completion-table table
  8611. current-tags (org-split-string current ":")
  8612. inherited-tags (nreverse
  8613. (nthcdr (length current-tags)
  8614. (nreverse (org-get-tags-at))))
  8615. tags
  8616. (if (or (eq t org-use-fast-tag-selection)
  8617. (and org-use-fast-tag-selection
  8618. (delq nil (mapcar 'cdr table))))
  8619. (org-fast-tag-selection
  8620. current-tags inherited-tags table
  8621. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8622. (let ((org-add-colon-after-tag-completion t))
  8623. (org-trim
  8624. (org-without-partial-completion
  8625. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8626. nil nil current 'org-tags-history)))))))
  8627. (while (string-match "[-+&]+" tags)
  8628. ;; No boolean logic, just a list
  8629. (setq tags (replace-match ":" t t tags))))
  8630. (if (string-match "\\`[\t ]*\\'" tags)
  8631. (setq tags "")
  8632. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8633. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8634. ;; Insert new tags at the correct column
  8635. (beginning-of-line 1)
  8636. (cond
  8637. ((and (equal current "") (equal tags "")))
  8638. ((re-search-forward
  8639. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8640. (point-at-eol) t)
  8641. (if (equal tags "")
  8642. (setq rpl "")
  8643. (goto-char (match-beginning 0))
  8644. (setq c0 (current-column) p0 (point)
  8645. c1 (max (1+ c0) (if (> org-tags-column 0)
  8646. org-tags-column
  8647. (- (- org-tags-column) (length tags))))
  8648. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8649. (replace-match rpl t t)
  8650. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8651. tags)
  8652. (t (error "Tags alignment failed")))
  8653. (org-move-to-column col)
  8654. (unless just-align
  8655. (run-hooks 'org-after-tags-change-hook)))))
  8656. (defun org-change-tag-in-region (beg end tag off)
  8657. "Add or remove TAG for each entry in the region.
  8658. This works in the agenda, and also in an org-mode buffer."
  8659. (interactive
  8660. (list (region-beginning) (region-end)
  8661. (let ((org-last-tags-completion-table
  8662. (if (org-mode-p)
  8663. (org-get-buffer-tags)
  8664. (org-global-tags-completion-table))))
  8665. (org-ido-completing-read
  8666. "Tag: " 'org-tags-completion-function nil nil nil
  8667. 'org-tags-history))
  8668. (progn
  8669. (message "[s]et or [r]emove? ")
  8670. (equal (read-char-exclusive) ?r))))
  8671. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8672. (let ((agendap (equal major-mode 'org-agenda-mode))
  8673. l1 l2 m buf pos newhead (cnt 0))
  8674. (goto-char end)
  8675. (setq l2 (1- (org-current-line)))
  8676. (goto-char beg)
  8677. (setq l1 (org-current-line))
  8678. (loop for l from l1 to l2 do
  8679. (goto-line l)
  8680. (setq m (get-text-property (point) 'org-hd-marker))
  8681. (when (or (and (org-mode-p) (org-on-heading-p))
  8682. (and agendap m))
  8683. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8684. pos (if agendap m (point)))
  8685. (with-current-buffer buf
  8686. (save-excursion
  8687. (save-restriction
  8688. (goto-char pos)
  8689. (setq cnt (1+ cnt))
  8690. (org-toggle-tag tag (if off 'off 'on))
  8691. (setq newhead (org-get-heading)))))
  8692. (and agendap (org-agenda-change-all-lines newhead m))))
  8693. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8694. (defun org-tags-completion-function (string predicate &optional flag)
  8695. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8696. (confirm (lambda (x) (stringp (car x)))))
  8697. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8698. (setq s1 (match-string 1 string)
  8699. s2 (match-string 2 string))
  8700. (setq s1 "" s2 string))
  8701. (cond
  8702. ((eq flag nil)
  8703. ;; try completion
  8704. (setq rtn (try-completion s2 ctable confirm))
  8705. (if (stringp rtn)
  8706. (setq rtn
  8707. (concat s1 s2 (substring rtn (length s2))
  8708. (if (and org-add-colon-after-tag-completion
  8709. (assoc rtn ctable))
  8710. ":" ""))))
  8711. rtn)
  8712. ((eq flag t)
  8713. ;; all-completions
  8714. (all-completions s2 ctable confirm)
  8715. )
  8716. ((eq flag 'lambda)
  8717. ;; exact match?
  8718. (assoc s2 ctable)))
  8719. ))
  8720. (defun org-fast-tag-insert (kwd tags face &optional end)
  8721. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8722. (insert (format "%-12s" (concat kwd ":"))
  8723. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8724. (or end "")))
  8725. (defun org-fast-tag-show-exit (flag)
  8726. (save-excursion
  8727. (goto-line 3)
  8728. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8729. (replace-match ""))
  8730. (when flag
  8731. (end-of-line 1)
  8732. (org-move-to-column (- (window-width) 19) t)
  8733. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8734. (defun org-set-current-tags-overlay (current prefix)
  8735. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8736. (if (featurep 'xemacs)
  8737. (org-overlay-display org-tags-overlay (concat prefix s)
  8738. 'secondary-selection)
  8739. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8740. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8741. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8742. "Fast tag selection with single keys.
  8743. CURRENT is the current list of tags in the headline, INHERITED is the
  8744. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8745. possibly with grouping information. TODO-TABLE is a similar table with
  8746. TODO keywords, should these have keys assigned to them.
  8747. If the keys are nil, a-z are automatically assigned.
  8748. Returns the new tags string, or nil to not change the current settings."
  8749. (let* ((fulltable (append table todo-table))
  8750. (maxlen (apply 'max (mapcar
  8751. (lambda (x)
  8752. (if (stringp (car x)) (string-width (car x)) 0))
  8753. fulltable)))
  8754. (buf (current-buffer))
  8755. (expert (eq org-fast-tag-selection-single-key 'expert))
  8756. (buffer-tags nil)
  8757. (fwidth (+ maxlen 3 1 3))
  8758. (ncol (/ (- (window-width) 4) fwidth))
  8759. (i-face 'org-done)
  8760. (c-face 'org-todo)
  8761. tg cnt e c char c1 c2 ntable tbl rtn
  8762. ov-start ov-end ov-prefix
  8763. (exit-after-next org-fast-tag-selection-single-key)
  8764. (done-keywords org-done-keywords)
  8765. groups ingroup)
  8766. (save-excursion
  8767. (beginning-of-line 1)
  8768. (if (looking-at
  8769. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8770. (setq ov-start (match-beginning 1)
  8771. ov-end (match-end 1)
  8772. ov-prefix "")
  8773. (setq ov-start (1- (point-at-eol))
  8774. ov-end (1+ ov-start))
  8775. (skip-chars-forward "^\n\r")
  8776. (setq ov-prefix
  8777. (concat
  8778. (buffer-substring (1- (point)) (point))
  8779. (if (> (current-column) org-tags-column)
  8780. " "
  8781. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8782. (org-move-overlay org-tags-overlay ov-start ov-end)
  8783. (save-window-excursion
  8784. (if expert
  8785. (set-buffer (get-buffer-create " *Org tags*"))
  8786. (delete-other-windows)
  8787. (split-window-vertically)
  8788. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8789. (erase-buffer)
  8790. (org-set-local 'org-done-keywords done-keywords)
  8791. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8792. (org-fast-tag-insert "Current" current c-face "\n\n")
  8793. (org-fast-tag-show-exit exit-after-next)
  8794. (org-set-current-tags-overlay current ov-prefix)
  8795. (setq tbl fulltable char ?a cnt 0)
  8796. (while (setq e (pop tbl))
  8797. (cond
  8798. ((equal e '(:startgroup))
  8799. (push '() groups) (setq ingroup t)
  8800. (when (not (= cnt 0))
  8801. (setq cnt 0)
  8802. (insert "\n"))
  8803. (insert "{ "))
  8804. ((equal e '(:endgroup))
  8805. (setq ingroup nil cnt 0)
  8806. (insert "}\n"))
  8807. (t
  8808. (setq tg (car e) c2 nil)
  8809. (if (cdr e)
  8810. (setq c (cdr e))
  8811. ;; automatically assign a character.
  8812. (setq c1 (string-to-char
  8813. (downcase (substring
  8814. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8815. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8816. (while (or (rassoc char ntable) (rassoc char table))
  8817. (setq char (1+ char)))
  8818. (setq c2 c1))
  8819. (setq c (or c2 char)))
  8820. (if ingroup (push tg (car groups)))
  8821. (setq tg (org-add-props tg nil 'face
  8822. (cond
  8823. ((not (assoc tg table))
  8824. (org-get-todo-face tg))
  8825. ((member tg current) c-face)
  8826. ((member tg inherited) i-face)
  8827. (t nil))))
  8828. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8829. (insert "[" c "] " tg (make-string
  8830. (- fwidth 4 (length tg)) ?\ ))
  8831. (push (cons tg c) ntable)
  8832. (when (= (setq cnt (1+ cnt)) ncol)
  8833. (insert "\n")
  8834. (if ingroup (insert " "))
  8835. (setq cnt 0)))))
  8836. (setq ntable (nreverse ntable))
  8837. (insert "\n")
  8838. (goto-char (point-min))
  8839. (if (not expert) (org-fit-window-to-buffer))
  8840. (setq rtn
  8841. (catch 'exit
  8842. (while t
  8843. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8844. (if groups " [!] no groups" " [!]groups")
  8845. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8846. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8847. (cond
  8848. ((= c ?\r) (throw 'exit t))
  8849. ((= c ?!)
  8850. (setq groups (not groups))
  8851. (goto-char (point-min))
  8852. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8853. ((= c ?\C-c)
  8854. (if (not expert)
  8855. (org-fast-tag-show-exit
  8856. (setq exit-after-next (not exit-after-next)))
  8857. (setq expert nil)
  8858. (delete-other-windows)
  8859. (split-window-vertically)
  8860. (org-switch-to-buffer-other-window " *Org tags*")
  8861. (org-fit-window-to-buffer)))
  8862. ((or (= c ?\C-g)
  8863. (and (= c ?q) (not (rassoc c ntable))))
  8864. (org-detach-overlay org-tags-overlay)
  8865. (setq quit-flag t))
  8866. ((= c ?\ )
  8867. (setq current nil)
  8868. (if exit-after-next (setq exit-after-next 'now)))
  8869. ((= c ?\t)
  8870. (condition-case nil
  8871. (setq tg (org-ido-completing-read
  8872. "Tag: "
  8873. (or buffer-tags
  8874. (with-current-buffer buf
  8875. (org-get-buffer-tags)))))
  8876. (quit (setq tg "")))
  8877. (when (string-match "\\S-" tg)
  8878. (add-to-list 'buffer-tags (list tg))
  8879. (if (member tg current)
  8880. (setq current (delete tg current))
  8881. (push tg current)))
  8882. (if exit-after-next (setq exit-after-next 'now)))
  8883. ((setq e (rassoc c todo-table) tg (car e))
  8884. (with-current-buffer buf
  8885. (save-excursion (org-todo tg)))
  8886. (if exit-after-next (setq exit-after-next 'now)))
  8887. ((setq e (rassoc c ntable) tg (car e))
  8888. (if (member tg current)
  8889. (setq current (delete tg current))
  8890. (loop for g in groups do
  8891. (if (member tg g)
  8892. (mapc (lambda (x)
  8893. (setq current (delete x current)))
  8894. g)))
  8895. (push tg current))
  8896. (if exit-after-next (setq exit-after-next 'now))))
  8897. ;; Create a sorted list
  8898. (setq current
  8899. (sort current
  8900. (lambda (a b)
  8901. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8902. (if (eq exit-after-next 'now) (throw 'exit t))
  8903. (goto-char (point-min))
  8904. (beginning-of-line 2)
  8905. (delete-region (point) (point-at-eol))
  8906. (org-fast-tag-insert "Current" current c-face)
  8907. (org-set-current-tags-overlay current ov-prefix)
  8908. (while (re-search-forward
  8909. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8910. (setq tg (match-string 1))
  8911. (add-text-properties
  8912. (match-beginning 1) (match-end 1)
  8913. (list 'face
  8914. (cond
  8915. ((member tg current) c-face)
  8916. ((member tg inherited) i-face)
  8917. (t (get-text-property (match-beginning 1) 'face))))))
  8918. (goto-char (point-min)))))
  8919. (org-detach-overlay org-tags-overlay)
  8920. (if rtn
  8921. (mapconcat 'identity current ":")
  8922. nil))))
  8923. (defun org-get-tags-string ()
  8924. "Get the TAGS string in the current headline."
  8925. (unless (org-on-heading-p t)
  8926. (error "Not on a heading"))
  8927. (save-excursion
  8928. (beginning-of-line 1)
  8929. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8930. (org-match-string-no-properties 1)
  8931. "")))
  8932. (defun org-get-tags ()
  8933. "Get the list of tags specified in the current headline."
  8934. (org-split-string (org-get-tags-string) ":"))
  8935. (defun org-get-buffer-tags ()
  8936. "Get a table of all tags used in the buffer, for completion."
  8937. (let (tags)
  8938. (save-excursion
  8939. (goto-char (point-min))
  8940. (while (re-search-forward
  8941. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8942. (when (equal (char-after (point-at-bol 0)) ?*)
  8943. (mapc (lambda (x) (add-to-list 'tags x))
  8944. (org-split-string (org-match-string-no-properties 1) ":")))))
  8945. (mapcar 'list tags)))
  8946. ;;;; The mapping API
  8947. ;;;###autoload
  8948. (defun org-map-entries (func &optional match scope &rest skip)
  8949. "Call FUNC at each headline selected by MATCH in SCOPE.
  8950. FUNC is a function or a lisp form. The function will be called without
  8951. arguments, with the cursor positioned at the beginning of the headline.
  8952. The return values of all calls to the function will be collected and
  8953. returned as a list.
  8954. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  8955. Only headlines that are matched by this query will be considered during
  8956. the iteration. When MATCH is nil or t, all headlines will be
  8957. visited by the iteration.
  8958. SCOPE determines the scope of this command. It can be any of:
  8959. nil The current buffer, respecting the restriction if any
  8960. tree The subtree started with the entry at point
  8961. file The current buffer, without restriction
  8962. file-with-archives
  8963. The current buffer, and any archives associated with it
  8964. agenda All agenda files
  8965. agenda-with-archives
  8966. All agenda files with any archive files associated with them
  8967. \(file1 file2 ...)
  8968. If this is a list, all files in the list will be scanned
  8969. The remaining args are treated as settings for the skipping facilities of
  8970. the scanner. The following items can be given here:
  8971. archive skip trees with the archive tag.
  8972. comment skip trees with the COMMENT keyword
  8973. function or Emacs Lisp form:
  8974. will be used as value for `org-agenda-skip-function', so whenever
  8975. the the function returns t, FUNC will not be called for that
  8976. entry and search will continue from the point where the
  8977. function leaves it."
  8978. (let* ((org-agenda-archives-mode nil) ; just to make sure
  8979. (org-agenda-skip-archived-trees (memq 'archive skip))
  8980. (org-agenda-skip-comment-trees (memq 'comment skip))
  8981. (org-agenda-skip-function
  8982. (car (org-delete-all '(comment archive) skip)))
  8983. (org-tags-match-list-sublevels t)
  8984. matcher pos file
  8985. org-todo-keywords-for-agenda
  8986. org-done-keywords-for-agenda
  8987. org-todo-keyword-alist-for-agenda
  8988. org-tag-alist-for-agenda)
  8989. (cond
  8990. ((eq match t) (setq matcher t))
  8991. ((eq match nil) (setq matcher t))
  8992. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  8993. (when (eq scope 'tree)
  8994. (org-back-to-heading t)
  8995. (org-narrow-to-subtree)
  8996. (setq scope nil))
  8997. (if (not scope)
  8998. (progn
  8999. (org-prepare-agenda-buffers
  9000. (list (buffer-file-name (current-buffer))))
  9001. (org-scan-tags func matcher))
  9002. ;; Get the right scope
  9003. (setq pos (point))
  9004. (cond
  9005. ((and scope (listp scope) (symbolp (car scope)))
  9006. (setq scope (eval scope)))
  9007. ((eq scope 'agenda)
  9008. (setq scope (org-agenda-files t)))
  9009. ((eq scope 'agenda-with-archives)
  9010. (setq scope (org-agenda-files t))
  9011. (setq scope (org-add-archive-files scope)))
  9012. ((eq scope 'file)
  9013. (setq scope (list (buffer-file-name))))
  9014. ((eq scope 'file-with-archives)
  9015. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9016. (org-prepare-agenda-buffers scope)
  9017. (while (setq file (pop scope))
  9018. (with-current-buffer (org-find-base-buffer-visiting file)
  9019. (save-excursion
  9020. (save-restriction
  9021. (widen)
  9022. (goto-char (point-min))
  9023. (org-scan-tags func matcher))))))))
  9024. ;;;; Properties
  9025. ;;; Setting and retrieving properties
  9026. (defconst org-special-properties
  9027. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9028. "TIMESTAMP" "TIMESTAMP_IA")
  9029. "The special properties valid in Org-mode.
  9030. These are properties that are not defined in the property drawer,
  9031. but in some other way.")
  9032. (defconst org-default-properties
  9033. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9034. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9035. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9036. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9037. "Some properties that are used by Org-mode for various purposes.
  9038. Being in this list makes sure that they are offered for completion.")
  9039. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9040. "Regular expression matching the first line of a property drawer.")
  9041. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9042. "Regular expression matching the first line of a property drawer.")
  9043. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9044. "Regular expression matching the first line of a property drawer.")
  9045. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9046. "Regular expression matching the first line of a property drawer.")
  9047. (defconst org-property-drawer-re
  9048. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9049. org-property-end-re "\\)\n?")
  9050. "Matches an entire property drawer.")
  9051. (defconst org-clock-drawer-re
  9052. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9053. org-property-end-re "\\)\n?")
  9054. "Matches an entire clock drawer.")
  9055. (defun org-property-action ()
  9056. "Do an action on properties."
  9057. (interactive)
  9058. (let (c)
  9059. (org-at-property-p)
  9060. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9061. (setq c (read-char-exclusive))
  9062. (cond
  9063. ((equal c ?s)
  9064. (call-interactively 'org-set-property))
  9065. ((equal c ?d)
  9066. (call-interactively 'org-delete-property))
  9067. ((equal c ?D)
  9068. (call-interactively 'org-delete-property-globally))
  9069. ((equal c ?c)
  9070. (call-interactively 'org-compute-property-at-point))
  9071. (t (error "No such property action %c" c)))))
  9072. (defun org-at-property-p ()
  9073. "Is the cursor in a property line?"
  9074. ;; FIXME: Does not check if we are actually in the drawer.
  9075. ;; FIXME: also returns true on any drawers.....
  9076. ;; This is used by C-c C-c for property action.
  9077. (save-excursion
  9078. (beginning-of-line 1)
  9079. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9080. (defun org-get-property-block (&optional beg end force)
  9081. "Return the (beg . end) range of the body of the property drawer.
  9082. BEG and END can be beginning and end of subtree, if not given
  9083. they will be found.
  9084. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9085. (catch 'exit
  9086. (save-excursion
  9087. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9088. (end (or end (progn (outline-next-heading) (point)))))
  9089. (goto-char beg)
  9090. (if (re-search-forward org-property-start-re end t)
  9091. (setq beg (1+ (match-end 0)))
  9092. (if force
  9093. (save-excursion
  9094. (org-insert-property-drawer)
  9095. (setq end (progn (outline-next-heading) (point))))
  9096. (throw 'exit nil))
  9097. (goto-char beg)
  9098. (if (re-search-forward org-property-start-re end t)
  9099. (setq beg (1+ (match-end 0)))))
  9100. (if (re-search-forward org-property-end-re end t)
  9101. (setq end (match-beginning 0))
  9102. (or force (throw 'exit nil))
  9103. (goto-char beg)
  9104. (setq end beg)
  9105. (org-indent-line-function)
  9106. (insert ":END:\n"))
  9107. (cons beg end)))))
  9108. (defun org-entry-properties (&optional pom which)
  9109. "Get all properties of the entry at point-or-marker POM.
  9110. This includes the TODO keyword, the tags, time strings for deadline,
  9111. scheduled, and clocking, and any additional properties defined in the
  9112. entry. The return value is an alist, keys may occur multiple times
  9113. if the property key was used several times.
  9114. POM may also be nil, in which case the current entry is used.
  9115. If WHICH is nil or `all', get all properties. If WHICH is
  9116. `special' or `standard', only get that subclass."
  9117. (setq which (or which 'all))
  9118. (org-with-point-at pom
  9119. (let ((clockstr (substring org-clock-string 0 -1))
  9120. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9121. beg end range props sum-props key value string clocksum)
  9122. (save-excursion
  9123. (when (condition-case nil (org-back-to-heading t) (error nil))
  9124. (setq beg (point))
  9125. (setq sum-props (get-text-property (point) 'org-summaries))
  9126. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9127. (outline-next-heading)
  9128. (setq end (point))
  9129. (when (memq which '(all special))
  9130. ;; Get the special properties, like TODO and tags
  9131. (goto-char beg)
  9132. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9133. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9134. (when (looking-at org-priority-regexp)
  9135. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9136. (when (and (setq value (org-get-tags-string))
  9137. (string-match "\\S-" value))
  9138. (push (cons "TAGS" value) props))
  9139. (when (setq value (org-get-tags-at))
  9140. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9141. props))
  9142. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9143. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9144. string (if (equal key clockstr)
  9145. (org-no-properties
  9146. (org-trim
  9147. (buffer-substring
  9148. (match-beginning 3) (goto-char (point-at-eol)))))
  9149. (substring (org-match-string-no-properties 3) 1 -1)))
  9150. (unless key
  9151. (if (= (char-after (match-beginning 3)) ?\[)
  9152. (setq key "TIMESTAMP_IA")
  9153. (setq key "TIMESTAMP")))
  9154. (when (or (equal key clockstr) (not (assoc key props)))
  9155. (push (cons key string) props)))
  9156. )
  9157. (when (memq which '(all standard))
  9158. ;; Get the standard properties, like :PORP: ...
  9159. (setq range (org-get-property-block beg end))
  9160. (when range
  9161. (goto-char (car range))
  9162. (while (re-search-forward
  9163. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9164. (cdr range) t)
  9165. (setq key (org-match-string-no-properties 1)
  9166. value (org-trim (or (org-match-string-no-properties 2) "")))
  9167. (unless (member key excluded)
  9168. (push (cons key (or value "")) props)))))
  9169. (if clocksum
  9170. (push (cons "CLOCKSUM"
  9171. (org-columns-number-to-string (/ (float clocksum) 60.)
  9172. 'add_times))
  9173. props))
  9174. (unless (assoc "CATEGORY" props)
  9175. (setq value (or (org-get-category)
  9176. (progn (org-refresh-category-properties)
  9177. (org-get-category))))
  9178. (push (cons "CATEGORY" value) props))
  9179. (append sum-props (nreverse props)))))))
  9180. (defun org-entry-get (pom property &optional inherit)
  9181. "Get value of PROPERTY for entry at point-or-marker POM.
  9182. If INHERIT is non-nil and the entry does not have the property,
  9183. then also check higher levels of the hierarchy.
  9184. If INHERIT is the symbol `selective', use inheritance only if the setting
  9185. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9186. If the property is present but empty, the return value is the empty string.
  9187. If the property is not present at all, nil is returned."
  9188. (org-with-point-at pom
  9189. (if (and inherit (if (eq inherit 'selective)
  9190. (org-property-inherit-p property)
  9191. t))
  9192. (org-entry-get-with-inheritance property)
  9193. (if (member property org-special-properties)
  9194. ;; We need a special property. Use brute force, get all properties.
  9195. (cdr (assoc property (org-entry-properties nil 'special)))
  9196. (let ((range (org-get-property-block)))
  9197. (if (and range
  9198. (goto-char (car range))
  9199. (re-search-forward
  9200. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9201. (cdr range) t))
  9202. ;; Found the property, return it.
  9203. (if (match-end 1)
  9204. (org-match-string-no-properties 1)
  9205. "")))))))
  9206. (defun org-property-or-variable-value (var &optional inherit)
  9207. "Check if there is a property fixing the value of VAR.
  9208. If yes, return this value. If not, return the current value of the variable."
  9209. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9210. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9211. (read prop)
  9212. (symbol-value var))))
  9213. (defun org-entry-delete (pom property)
  9214. "Delete the property PROPERTY from entry at point-or-marker POM."
  9215. (org-with-point-at pom
  9216. (if (member property org-special-properties)
  9217. nil ; cannot delete these properties.
  9218. (let ((range (org-get-property-block)))
  9219. (if (and range
  9220. (goto-char (car range))
  9221. (re-search-forward
  9222. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9223. (cdr range) t))
  9224. (progn
  9225. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9226. t)
  9227. nil)))))
  9228. ;; Multi-values properties are properties that contain multiple values
  9229. ;; These values are assumed to be single words, separated by whitespace.
  9230. (defun org-entry-add-to-multivalued-property (pom property value)
  9231. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9232. (let* ((old (org-entry-get pom property))
  9233. (values (and old (org-split-string old "[ \t]"))))
  9234. (setq value (org-entry-protect-space value))
  9235. (unless (member value values)
  9236. (setq values (cons value values))
  9237. (org-entry-put pom property
  9238. (mapconcat 'identity values " ")))))
  9239. (defun org-entry-remove-from-multivalued-property (pom property value)
  9240. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9241. (let* ((old (org-entry-get pom property))
  9242. (values (and old (org-split-string old "[ \t]"))))
  9243. (setq value (org-entry-protect-space value))
  9244. (when (member value values)
  9245. (setq values (delete value values))
  9246. (org-entry-put pom property
  9247. (mapconcat 'identity values " ")))))
  9248. (defun org-entry-member-in-multivalued-property (pom property value)
  9249. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9250. (let* ((old (org-entry-get pom property))
  9251. (values (and old (org-split-string old "[ \t]"))))
  9252. (setq value (org-entry-protect-space value))
  9253. (member value values)))
  9254. (defun org-entry-get-multivalued-property (pom property)
  9255. "Return a list of values in a multivalued property."
  9256. (let* ((value (org-entry-get pom property))
  9257. (values (and value (org-split-string value "[ \t]"))))
  9258. (mapcar 'org-entry-restore-space values)))
  9259. (defun org-entry-put-multivalued-property (pom property &rest values)
  9260. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9261. VALUES should be a list of strings. Spaces will be protected."
  9262. (org-entry-put pom property
  9263. (mapconcat 'org-entry-protect-space values " "))
  9264. (let* ((value (org-entry-get pom property))
  9265. (values (and value (org-split-string value "[ \t]"))))
  9266. (mapcar 'org-entry-restore-space values)))
  9267. (defun org-entry-protect-space (s)
  9268. "Protect spaces and newline in string S."
  9269. (while (string-match " " s)
  9270. (setq s (replace-match "%20" t t s)))
  9271. (while (string-match "\n" s)
  9272. (setq s (replace-match "%0A" t t s)))
  9273. s)
  9274. (defun org-entry-restore-space (s)
  9275. "Restore spaces and newline in string S."
  9276. (while (string-match "%20" s)
  9277. (setq s (replace-match " " t t s)))
  9278. (while (string-match "%0A" s)
  9279. (setq s (replace-match "\n" t t s)))
  9280. s)
  9281. (defvar org-entry-property-inherited-from (make-marker)
  9282. "Marker pointing to the entry from where a proerty was inherited.
  9283. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9284. location of the entry where the inheriance search matched. If there was
  9285. no match, the marker will point nowhere.
  9286. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9287. is set.")
  9288. (defun org-entry-get-with-inheritance (property)
  9289. "Get entry property, and search higher levels if not present."
  9290. (move-marker org-entry-property-inherited-from nil)
  9291. (let (tmp)
  9292. (save-excursion
  9293. (save-restriction
  9294. (widen)
  9295. (catch 'ex
  9296. (while t
  9297. (when (setq tmp (org-entry-get nil property))
  9298. (org-back-to-heading t)
  9299. (move-marker org-entry-property-inherited-from (point))
  9300. (throw 'ex tmp))
  9301. (or (org-up-heading-safe) (throw 'ex nil)))))
  9302. (or tmp
  9303. (cdr (assoc property org-file-properties))
  9304. (cdr (assoc property org-global-properties))
  9305. (cdr (assoc property org-global-properties-fixed))))))
  9306. (defun org-entry-put (pom property value)
  9307. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9308. (org-with-point-at pom
  9309. (org-back-to-heading t)
  9310. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9311. range)
  9312. (cond
  9313. ((equal property "TODO")
  9314. (when (and (stringp value) (string-match "\\S-" value)
  9315. (not (member value org-todo-keywords-1)))
  9316. (error "\"%s\" is not a valid TODO state" value))
  9317. (if (or (not value)
  9318. (not (string-match "\\S-" value)))
  9319. (setq value 'none))
  9320. (org-todo value)
  9321. (org-set-tags nil 'align))
  9322. ((equal property "PRIORITY")
  9323. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9324. (string-to-char value) ?\ ))
  9325. (org-set-tags nil 'align))
  9326. ((equal property "SCHEDULED")
  9327. (if (re-search-forward org-scheduled-time-regexp end t)
  9328. (cond
  9329. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9330. ((eq value 'later) (org-timestamp-change 1 'day))
  9331. (t (call-interactively 'org-schedule)))
  9332. (call-interactively 'org-schedule)))
  9333. ((equal property "DEADLINE")
  9334. (if (re-search-forward org-deadline-time-regexp end t)
  9335. (cond
  9336. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9337. ((eq value 'later) (org-timestamp-change 1 'day))
  9338. (t (call-interactively 'org-deadline)))
  9339. (call-interactively 'org-deadline)))
  9340. ((member property org-special-properties)
  9341. (error "The %s property can not yet be set with `org-entry-put'"
  9342. property))
  9343. (t ; a non-special property
  9344. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9345. (setq range (org-get-property-block beg end 'force))
  9346. (goto-char (car range))
  9347. (if (re-search-forward
  9348. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9349. (progn
  9350. (delete-region (match-beginning 1) (match-end 1))
  9351. (goto-char (match-beginning 1)))
  9352. (goto-char (cdr range))
  9353. (insert "\n")
  9354. (backward-char 1)
  9355. (org-indent-line-function)
  9356. (insert ":" property ":"))
  9357. (and value (insert " " value))
  9358. (org-indent-line-function)))))))
  9359. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9360. "Get all property keys in the current buffer.
  9361. With INCLUDE-SPECIALS, also list the special properties that relect things
  9362. like tags and TODO state.
  9363. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9364. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9365. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9366. formats in the current buffer."
  9367. (let (rtn range cfmt cols s p)
  9368. (save-excursion
  9369. (save-restriction
  9370. (widen)
  9371. (goto-char (point-min))
  9372. (while (re-search-forward org-property-start-re nil t)
  9373. (setq range (org-get-property-block))
  9374. (goto-char (car range))
  9375. (while (re-search-forward
  9376. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9377. (cdr range) t)
  9378. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9379. (outline-next-heading))))
  9380. (when include-specials
  9381. (setq rtn (append org-special-properties rtn)))
  9382. (when include-defaults
  9383. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9384. (when include-columns
  9385. (save-excursion
  9386. (save-restriction
  9387. (widen)
  9388. (goto-char (point-min))
  9389. (while (re-search-forward
  9390. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9391. nil t)
  9392. (setq cfmt (match-string 2) s 0)
  9393. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9394. cfmt s)
  9395. (setq s (match-end 0)
  9396. p (match-string 1 cfmt))
  9397. (unless (or (equal p "ITEM")
  9398. (member p org-special-properties))
  9399. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9400. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9401. (defun org-property-values (key)
  9402. "Return a list of all values of property KEY."
  9403. (save-excursion
  9404. (save-restriction
  9405. (widen)
  9406. (goto-char (point-min))
  9407. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9408. values)
  9409. (while (re-search-forward re nil t)
  9410. (add-to-list 'values (org-trim (match-string 1))))
  9411. (delete "" values)))))
  9412. (defun org-insert-property-drawer ()
  9413. "Insert a property drawer into the current entry."
  9414. (interactive)
  9415. (org-back-to-heading t)
  9416. (looking-at outline-regexp)
  9417. (let ((indent (- (match-end 0)(match-beginning 0)))
  9418. (beg (point))
  9419. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9420. end hiddenp)
  9421. (outline-next-heading)
  9422. (setq end (point))
  9423. (goto-char beg)
  9424. (while (re-search-forward re end t))
  9425. (setq hiddenp (org-invisible-p))
  9426. (end-of-line 1)
  9427. (and (equal (char-after) ?\n) (forward-char 1))
  9428. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9429. (beginning-of-line 2))
  9430. (org-skip-over-state-notes)
  9431. (skip-chars-backward " \t\n\r")
  9432. (if (eq (char-before) ?*) (forward-char 1))
  9433. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9434. (beginning-of-line 0)
  9435. (org-indent-to-column indent)
  9436. (beginning-of-line 2)
  9437. (org-indent-to-column indent)
  9438. (beginning-of-line 0)
  9439. (if hiddenp
  9440. (save-excursion
  9441. (org-back-to-heading t)
  9442. (hide-entry))
  9443. (org-flag-drawer t))))
  9444. (defun org-set-property (property value)
  9445. "In the current entry, set PROPERTY to VALUE.
  9446. When called interactively, this will prompt for a property name, offering
  9447. completion on existing and default properties. And then it will prompt
  9448. for a value, offering competion either on allowed values (via an inherited
  9449. xxx_ALL property) or on existing values in other instances of this property
  9450. in the current file."
  9451. (interactive
  9452. (let* ((completion-ignore-case t)
  9453. (keys (org-buffer-property-keys nil t t))
  9454. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9455. (prop (if (member prop0 keys)
  9456. prop0
  9457. (or (cdr (assoc (downcase prop0)
  9458. (mapcar (lambda (x) (cons (downcase x) x))
  9459. keys)))
  9460. prop0)))
  9461. (cur (org-entry-get nil prop))
  9462. (allowed (org-property-get-allowed-values nil prop 'table))
  9463. (existing (mapcar 'list (org-property-values prop)))
  9464. (val (if allowed
  9465. (org-completing-read "Value: " allowed nil 'req-match)
  9466. (org-completing-read
  9467. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9468. (concat "[" cur "]") "")
  9469. ": ")
  9470. existing nil nil "" nil cur))))
  9471. (list prop (if (equal val "") cur val))))
  9472. (unless (equal (org-entry-get nil property) value)
  9473. (org-entry-put nil property value)))
  9474. (defun org-delete-property (property)
  9475. "In the current entry, delete PROPERTY."
  9476. (interactive
  9477. (let* ((completion-ignore-case t)
  9478. (prop (org-ido-completing-read
  9479. "Property: " (org-entry-properties nil 'standard))))
  9480. (list prop)))
  9481. (message "Property %s %s" property
  9482. (if (org-entry-delete nil property)
  9483. "deleted"
  9484. "was not present in the entry")))
  9485. (defun org-delete-property-globally (property)
  9486. "Remove PROPERTY globally, from all entries."
  9487. (interactive
  9488. (let* ((completion-ignore-case t)
  9489. (prop (org-ido-completing-read
  9490. "Globally remove property: "
  9491. (mapcar 'list (org-buffer-property-keys)))))
  9492. (list prop)))
  9493. (save-excursion
  9494. (save-restriction
  9495. (widen)
  9496. (goto-char (point-min))
  9497. (let ((cnt 0))
  9498. (while (re-search-forward
  9499. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9500. nil t)
  9501. (setq cnt (1+ cnt))
  9502. (replace-match ""))
  9503. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9504. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9505. (defun org-compute-property-at-point ()
  9506. "Compute the property at point.
  9507. This looks for an enclosing column format, extracts the operator and
  9508. then applies it to the proerty in the column format's scope."
  9509. (interactive)
  9510. (unless (org-at-property-p)
  9511. (error "Not at a property"))
  9512. (let ((prop (org-match-string-no-properties 2)))
  9513. (org-columns-get-format-and-top-level)
  9514. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9515. (error "No operator defined for property %s" prop))
  9516. (org-columns-compute prop)))
  9517. (defun org-property-get-allowed-values (pom property &optional table)
  9518. "Get allowed values for the property PROPERTY.
  9519. When TABLE is non-nil, return an alist that can directly be used for
  9520. completion."
  9521. (let (vals)
  9522. (cond
  9523. ((equal property "TODO")
  9524. (setq vals (org-with-point-at pom
  9525. (append org-todo-keywords-1 '("")))))
  9526. ((equal property "PRIORITY")
  9527. (let ((n org-lowest-priority))
  9528. (while (>= n org-highest-priority)
  9529. (push (char-to-string n) vals)
  9530. (setq n (1- n)))))
  9531. ((member property org-special-properties))
  9532. (t
  9533. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9534. (when (and vals (string-match "\\S-" vals))
  9535. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9536. (setq vals (mapcar (lambda (x)
  9537. (cond ((stringp x) x)
  9538. ((numberp x) (number-to-string x))
  9539. ((symbolp x) (symbol-name x))
  9540. (t "???")))
  9541. vals)))))
  9542. (if table (mapcar 'list vals) vals)))
  9543. (defun org-property-previous-allowed-value (&optional previous)
  9544. "Switch to the next allowed value for this property."
  9545. (interactive)
  9546. (org-property-next-allowed-value t))
  9547. (defun org-property-next-allowed-value (&optional previous)
  9548. "Switch to the next allowed value for this property."
  9549. (interactive)
  9550. (unless (org-at-property-p)
  9551. (error "Not at a property"))
  9552. (let* ((key (match-string 2))
  9553. (value (match-string 3))
  9554. (allowed (or (org-property-get-allowed-values (point) key)
  9555. (and (member value '("[ ]" "[-]" "[X]"))
  9556. '("[ ]" "[X]"))))
  9557. nval)
  9558. (unless allowed
  9559. (error "Allowed values for this property have not been defined"))
  9560. (if previous (setq allowed (reverse allowed)))
  9561. (if (member value allowed)
  9562. (setq nval (car (cdr (member value allowed)))))
  9563. (setq nval (or nval (car allowed)))
  9564. (if (equal nval value)
  9565. (error "Only one allowed value for this property"))
  9566. (org-at-property-p)
  9567. (replace-match (concat " :" key ": " nval) t t)
  9568. (org-indent-line-function)
  9569. (beginning-of-line 1)
  9570. (skip-chars-forward " \t")))
  9571. (defun org-find-entry-with-id (ident)
  9572. "Locate the entry that contains the ID property with exact value IDENT.
  9573. IDENT can be a string, a symbol or a number, this function will search for
  9574. the string representation of it.
  9575. Return the position where this entry starts, or nil if there is no such entry."
  9576. (let ((id (cond
  9577. ((stringp ident) ident)
  9578. ((symbol-name ident) (symbol-name ident))
  9579. ((numberp ident) (number-to-string ident))
  9580. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9581. (case-fold-search nil))
  9582. (save-excursion
  9583. (save-restriction
  9584. (widen)
  9585. (goto-char (point-min))
  9586. (when (re-search-forward
  9587. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9588. nil t)
  9589. (org-back-to-heading)
  9590. (point))))))
  9591. ;;;; Timestamps
  9592. (defvar org-last-changed-timestamp nil)
  9593. (defvar org-last-inserted-timestamp nil
  9594. "The last time stamp inserted with `org-insert-time-stamp'.")
  9595. (defvar org-time-was-given) ; dynamically scoped parameter
  9596. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9597. (defvar org-ts-what) ; dynamically scoped parameter
  9598. (defun org-time-stamp (arg &optional inactive)
  9599. "Prompt for a date/time and insert a time stamp.
  9600. If the user specifies a time like HH:MM, or if this command is called
  9601. with a prefix argument, the time stamp will contain date and time.
  9602. Otherwise, only the date will be included. All parts of a date not
  9603. specified by the user will be filled in from the current date/time.
  9604. So if you press just return without typing anything, the time stamp
  9605. will represent the current date/time. If there is already a timestamp
  9606. at the cursor, it will be modified."
  9607. (interactive "P")
  9608. (let* ((ts nil)
  9609. (default-time
  9610. ;; Default time is either today, or, when entering a range,
  9611. ;; the range start.
  9612. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9613. (save-excursion
  9614. (re-search-backward
  9615. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9616. (- (point) 20) t)))
  9617. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9618. (current-time)))
  9619. (default-input (and ts (org-get-compact-tod ts)))
  9620. org-time-was-given org-end-time-was-given time)
  9621. (cond
  9622. ((and (org-at-timestamp-p t)
  9623. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9624. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9625. (insert "--")
  9626. (setq time (let ((this-command this-command))
  9627. (org-read-date arg 'totime nil nil
  9628. default-time default-input)))
  9629. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9630. ((org-at-timestamp-p t)
  9631. (setq time (let ((this-command this-command))
  9632. (org-read-date arg 'totime nil nil default-time default-input)))
  9633. (when (org-at-timestamp-p t) ; just to get the match data
  9634. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9635. (replace-match "")
  9636. (setq org-last-changed-timestamp
  9637. (org-insert-time-stamp
  9638. time (or org-time-was-given arg)
  9639. inactive nil nil (list org-end-time-was-given))))
  9640. (message "Timestamp updated"))
  9641. (t
  9642. (setq time (let ((this-command this-command))
  9643. (org-read-date arg 'totime nil nil default-time default-input)))
  9644. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9645. nil nil (list org-end-time-was-given))))))
  9646. ;; FIXME: can we use this for something else, like computing time differences?
  9647. (defun org-get-compact-tod (s)
  9648. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9649. (let* ((t1 (match-string 1 s))
  9650. (h1 (string-to-number (match-string 2 s)))
  9651. (m1 (string-to-number (match-string 3 s)))
  9652. (t2 (and (match-end 4) (match-string 5 s)))
  9653. (h2 (and t2 (string-to-number (match-string 6 s))))
  9654. (m2 (and t2 (string-to-number (match-string 7 s))))
  9655. dh dm)
  9656. (if (not t2)
  9657. t1
  9658. (setq dh (- h2 h1) dm (- m2 m1))
  9659. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9660. (concat t1 "+" (number-to-string dh)
  9661. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9662. (defun org-time-stamp-inactive (&optional arg)
  9663. "Insert an inactive time stamp.
  9664. An inactive time stamp is enclosed in square brackets instead of angle
  9665. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9666. does not link to the calendar and cannot be changed with the S-cursor keys.
  9667. So these are more for recording a certain time/date."
  9668. (interactive "P")
  9669. (org-time-stamp arg 'inactive))
  9670. (defvar org-date-ovl (org-make-overlay 1 1))
  9671. (org-overlay-put org-date-ovl 'face 'org-warning)
  9672. (org-detach-overlay org-date-ovl)
  9673. (defvar org-ans1) ; dynamically scoped parameter
  9674. (defvar org-ans2) ; dynamically scoped parameter
  9675. (defvar org-plain-time-of-day-regexp) ; defined below
  9676. (defvar org-overriding-default-time nil) ; dynamically scoped
  9677. (defvar org-read-date-overlay nil)
  9678. (defvar org-dcst nil) ; dynamically scoped
  9679. (defun org-read-date (&optional with-time to-time from-string prompt
  9680. default-time default-input)
  9681. "Read a date, possibly a time, and make things smooth for the user.
  9682. The prompt will suggest to enter an ISO date, but you can also enter anything
  9683. which will at least partially be understood by `parse-time-string'.
  9684. Unrecognized parts of the date will default to the current day, month, year,
  9685. hour and minute. If this command is called to replace a timestamp at point,
  9686. of to enter the second timestamp of a range, the default time is taken from the
  9687. existing stamp. For example,
  9688. 3-2-5 --> 2003-02-05
  9689. feb 15 --> currentyear-02-15
  9690. sep 12 9 --> 2009-09-12
  9691. 12:45 --> today 12:45
  9692. 22 sept 0:34 --> currentyear-09-22 0:34
  9693. 12 --> currentyear-currentmonth-12
  9694. Fri --> nearest Friday (today or later)
  9695. etc.
  9696. Furthermore you can specify a relative date by giving, as the *first* thing
  9697. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9698. change in days weeks, months, years.
  9699. With a single plus or minus, the date is relative to today. With a double
  9700. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9701. +4d --> four days from today
  9702. +4 --> same as above
  9703. +2w --> two weeks from today
  9704. ++5 --> five days from default date
  9705. The function understands only English month and weekday abbreviations,
  9706. but this can be configured with the variables `parse-time-months' and
  9707. `parse-time-weekdays'.
  9708. While prompting, a calendar is popped up - you can also select the
  9709. date with the mouse (button 1). The calendar shows a period of three
  9710. months. To scroll it to other months, use the keys `>' and `<'.
  9711. If you don't like the calendar, turn it off with
  9712. \(setq org-read-date-popup-calendar nil)
  9713. With optional argument TO-TIME, the date will immediately be converted
  9714. to an internal time.
  9715. With an optional argument WITH-TIME, the prompt will suggest to also
  9716. insert a time. Note that when WITH-TIME is not set, you can still
  9717. enter a time, and this function will inform the calling routine about
  9718. this change. The calling routine may then choose to change the format
  9719. used to insert the time stamp into the buffer to include the time.
  9720. With optional argument FROM-STRING, read from this string instead from
  9721. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9722. the time/date that is used for everything that is not specified by the
  9723. user."
  9724. (require 'parse-time)
  9725. (let* ((org-time-stamp-rounding-minutes
  9726. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9727. (org-dcst org-display-custom-times)
  9728. (ct (org-current-time))
  9729. (def (or org-overriding-default-time default-time ct))
  9730. (defdecode (decode-time def))
  9731. (dummy (progn
  9732. (when (< (nth 2 defdecode) org-extend-today-until)
  9733. (setcar (nthcdr 2 defdecode) -1)
  9734. (setcar (nthcdr 1 defdecode) 59)
  9735. (setq def (apply 'encode-time defdecode)
  9736. defdecode (decode-time def)))))
  9737. (calendar-move-hook nil)
  9738. (calendar-view-diary-initially-flag nil)
  9739. (view-diary-entries-initially nil)
  9740. (calendar-view-holidays-initially-flag nil)
  9741. (view-calendar-holidays-initially nil)
  9742. (timestr (format-time-string
  9743. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9744. (prompt (concat (if prompt (concat prompt " ") "")
  9745. (format "Date+time [%s]: " timestr)))
  9746. ans (org-ans0 "") org-ans1 org-ans2 final)
  9747. (cond
  9748. (from-string (setq ans from-string))
  9749. (org-read-date-popup-calendar
  9750. (save-excursion
  9751. (save-window-excursion
  9752. (calendar)
  9753. (calendar-forward-day (- (time-to-days def)
  9754. (calendar-absolute-from-gregorian
  9755. (calendar-current-date))))
  9756. (org-eval-in-calendar nil t)
  9757. (let* ((old-map (current-local-map))
  9758. (map (copy-keymap calendar-mode-map))
  9759. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9760. (org-defkey map (kbd "RET") 'org-calendar-select)
  9761. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9762. 'org-calendar-select-mouse)
  9763. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9764. 'org-calendar-select-mouse)
  9765. (org-defkey minibuffer-local-map [(meta shift left)]
  9766. (lambda () (interactive)
  9767. (org-eval-in-calendar '(calendar-backward-month 1))))
  9768. (org-defkey minibuffer-local-map [(meta shift right)]
  9769. (lambda () (interactive)
  9770. (org-eval-in-calendar '(calendar-forward-month 1))))
  9771. (org-defkey minibuffer-local-map [(meta shift up)]
  9772. (lambda () (interactive)
  9773. (org-eval-in-calendar '(calendar-backward-year 1))))
  9774. (org-defkey minibuffer-local-map [(meta shift down)]
  9775. (lambda () (interactive)
  9776. (org-eval-in-calendar '(calendar-forward-year 1))))
  9777. (org-defkey minibuffer-local-map [(shift up)]
  9778. (lambda () (interactive)
  9779. (org-eval-in-calendar '(calendar-backward-week 1))))
  9780. (org-defkey minibuffer-local-map [(shift down)]
  9781. (lambda () (interactive)
  9782. (org-eval-in-calendar '(calendar-forward-week 1))))
  9783. (org-defkey minibuffer-local-map [(shift left)]
  9784. (lambda () (interactive)
  9785. (org-eval-in-calendar '(calendar-backward-day 1))))
  9786. (org-defkey minibuffer-local-map [(shift right)]
  9787. (lambda () (interactive)
  9788. (org-eval-in-calendar '(calendar-forward-day 1))))
  9789. (org-defkey minibuffer-local-map ">"
  9790. (lambda () (interactive)
  9791. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9792. (org-defkey minibuffer-local-map "<"
  9793. (lambda () (interactive)
  9794. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9795. (unwind-protect
  9796. (progn
  9797. (use-local-map map)
  9798. (add-hook 'post-command-hook 'org-read-date-display)
  9799. (setq org-ans0 (read-string prompt default-input nil nil))
  9800. ;; org-ans0: from prompt
  9801. ;; org-ans1: from mouse click
  9802. ;; org-ans2: from calendar motion
  9803. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9804. (remove-hook 'post-command-hook 'org-read-date-display)
  9805. (use-local-map old-map)
  9806. (when org-read-date-overlay
  9807. (org-delete-overlay org-read-date-overlay)
  9808. (setq org-read-date-overlay nil)))))))
  9809. (t ; Naked prompt only
  9810. (unwind-protect
  9811. (setq ans (read-string prompt default-input nil timestr))
  9812. (when org-read-date-overlay
  9813. (org-delete-overlay org-read-date-overlay)
  9814. (setq org-read-date-overlay nil)))))
  9815. (setq final (org-read-date-analyze ans def defdecode))
  9816. (if to-time
  9817. (apply 'encode-time final)
  9818. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9819. (format "%04d-%02d-%02d %02d:%02d"
  9820. (nth 5 final) (nth 4 final) (nth 3 final)
  9821. (nth 2 final) (nth 1 final))
  9822. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9823. (defvar def)
  9824. (defvar defdecode)
  9825. (defvar with-time)
  9826. (defun org-read-date-display ()
  9827. "Display the currrent date prompt interpretation in the minibuffer."
  9828. (when org-read-date-display-live
  9829. (when org-read-date-overlay
  9830. (org-delete-overlay org-read-date-overlay))
  9831. (let ((p (point)))
  9832. (end-of-line 1)
  9833. (while (not (equal (buffer-substring
  9834. (max (point-min) (- (point) 4)) (point))
  9835. " "))
  9836. (insert " "))
  9837. (goto-char p))
  9838. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9839. " " (or org-ans1 org-ans2)))
  9840. (org-end-time-was-given nil)
  9841. (f (org-read-date-analyze ans def defdecode))
  9842. (fmts (if org-dcst
  9843. org-time-stamp-custom-formats
  9844. org-time-stamp-formats))
  9845. (fmt (if (or with-time
  9846. (and (boundp 'org-time-was-given) org-time-was-given))
  9847. (cdr fmts)
  9848. (car fmts)))
  9849. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9850. (when (and org-end-time-was-given
  9851. (string-match org-plain-time-of-day-regexp txt))
  9852. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9853. org-end-time-was-given
  9854. (substring txt (match-end 0)))))
  9855. (setq org-read-date-overlay
  9856. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9857. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9858. (defun org-read-date-analyze (ans def defdecode)
  9859. "Analyze the combined answer of the date prompt."
  9860. ;; FIXME: cleanup and comment
  9861. (let (delta deltan deltaw deltadef year month day
  9862. hour minute second wday pm h2 m2 tl wday1
  9863. iso-year iso-weekday iso-week iso-year iso-date)
  9864. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9865. (setq ans "+0"))
  9866. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9867. (setq ans (replace-match "" t t ans)
  9868. deltan (car delta)
  9869. deltaw (nth 1 delta)
  9870. deltadef (nth 2 delta)))
  9871. ;; Check if there is an iso week date in there
  9872. ;; If yes, sore the info and ostpone interpreting it until the rest
  9873. ;; of the parsing is done
  9874. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9875. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9876. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9877. iso-week (string-to-number (match-string 2 ans)))
  9878. (setq ans (replace-match "" t t ans)))
  9879. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9880. (when (string-match
  9881. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9882. (setq year (if (match-end 2)
  9883. (string-to-number (match-string 2 ans))
  9884. (string-to-number (format-time-string "%Y")))
  9885. month (string-to-number (match-string 3 ans))
  9886. day (string-to-number (match-string 4 ans)))
  9887. (if (< year 100) (setq year (+ 2000 year)))
  9888. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9889. t nil ans)))
  9890. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9891. ;; If there is a time with am/pm, and *no* time without it, we convert
  9892. ;; so that matching will be successful.
  9893. (loop for i from 1 to 2 do ; twice, for end time as well
  9894. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9895. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9896. (setq hour (string-to-number (match-string 1 ans))
  9897. minute (if (match-end 3)
  9898. (string-to-number (match-string 3 ans))
  9899. 0)
  9900. pm (equal ?p
  9901. (string-to-char (downcase (match-string 4 ans)))))
  9902. (if (and (= hour 12) (not pm))
  9903. (setq hour 0)
  9904. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9905. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9906. t t ans))))
  9907. ;; Check if a time range is given as a duration
  9908. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9909. (setq hour (string-to-number (match-string 1 ans))
  9910. h2 (+ hour (string-to-number (match-string 3 ans)))
  9911. minute (string-to-number (match-string 2 ans))
  9912. m2 (+ minute (if (match-end 5) (string-to-number
  9913. (match-string 5 ans))0)))
  9914. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9915. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9916. t t ans)))
  9917. ;; Check if there is a time range
  9918. (when (boundp 'org-end-time-was-given)
  9919. (setq org-time-was-given nil)
  9920. (when (and (string-match org-plain-time-of-day-regexp ans)
  9921. (match-end 8))
  9922. (setq org-end-time-was-given (match-string 8 ans))
  9923. (setq ans (concat (substring ans 0 (match-beginning 7))
  9924. (substring ans (match-end 7))))))
  9925. (setq tl (parse-time-string ans)
  9926. day (or (nth 3 tl) (nth 3 defdecode))
  9927. month (or (nth 4 tl)
  9928. (if (and org-read-date-prefer-future
  9929. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9930. (1+ (nth 4 defdecode))
  9931. (nth 4 defdecode)))
  9932. year (or (nth 5 tl)
  9933. (if (and org-read-date-prefer-future
  9934. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9935. (1+ (nth 5 defdecode))
  9936. (nth 5 defdecode)))
  9937. hour (or (nth 2 tl) (nth 2 defdecode))
  9938. minute (or (nth 1 tl) (nth 1 defdecode))
  9939. second (or (nth 0 tl) 0)
  9940. wday (nth 6 tl))
  9941. ;; Special date definitions below
  9942. (cond
  9943. (iso-week
  9944. ;; There was an iso week
  9945. (setq year (or iso-year year)
  9946. day (or iso-weekday wday 1)
  9947. wday nil ; to make sure that the trigger below does not match
  9948. iso-date (calendar-gregorian-from-absolute
  9949. (calendar-absolute-from-iso
  9950. (list iso-week day year))))
  9951. ; FIXME: Should we also push ISO weeks into the future?
  9952. ; (when (and org-read-date-prefer-future
  9953. ; (not iso-year)
  9954. ; (< (calendar-absolute-from-gregorian iso-date)
  9955. ; (time-to-days (current-time))))
  9956. ; (setq year (1+ year)
  9957. ; iso-date (calendar-gregorian-from-absolute
  9958. ; (calendar-absolute-from-iso
  9959. ; (list iso-week day year)))))
  9960. (setq month (car iso-date)
  9961. year (nth 2 iso-date)
  9962. day (nth 1 iso-date)))
  9963. (deltan
  9964. (unless deltadef
  9965. (let ((now (decode-time (current-time))))
  9966. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9967. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9968. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9969. ((equal deltaw "m") (setq month (+ month deltan)))
  9970. ((equal deltaw "y") (setq year (+ year deltan)))))
  9971. ((and wday (not (nth 3 tl)))
  9972. ;; Weekday was given, but no day, so pick that day in the week
  9973. ;; on or after the derived date.
  9974. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9975. (unless (equal wday wday1)
  9976. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9977. (if (and (boundp 'org-time-was-given)
  9978. (nth 2 tl))
  9979. (setq org-time-was-given t))
  9980. (if (< year 100) (setq year (+ 2000 year)))
  9981. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9982. (list second minute hour day month year)))
  9983. (defvar parse-time-weekdays)
  9984. (defun org-read-date-get-relative (s today default)
  9985. "Check string S for special relative date string.
  9986. TODAY and DEFAULT are internal times, for today and for a default.
  9987. Return shift list (N what def-flag)
  9988. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9989. N is the number of WHATs to shift.
  9990. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9991. the DEFAULT date rather than TODAY."
  9992. (when (and
  9993. (string-match
  9994. (concat
  9995. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9996. "\\([0-9]+\\)?"
  9997. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9998. "\\([ \t]\\|$\\)") s)
  9999. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10000. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10001. (string-to-char (substring (match-string 1 s) -1))
  10002. ?+))
  10003. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10004. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10005. (what (if (match-end 3) (match-string 3 s) "d"))
  10006. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10007. (date (if rel default today))
  10008. (wday (nth 6 (decode-time date)))
  10009. delta)
  10010. (if wday1
  10011. (progn
  10012. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10013. (if (= dir ?-) (setq delta (- delta 7)))
  10014. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10015. (list delta "d" rel))
  10016. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10017. (defun org-eval-in-calendar (form &optional keepdate)
  10018. "Eval FORM in the calendar window and return to current window.
  10019. Also, store the cursor date in variable org-ans2."
  10020. (let ((sw (selected-window)))
  10021. (select-window (get-buffer-window "*Calendar*"))
  10022. (eval form)
  10023. (when (and (not keepdate) (calendar-cursor-to-date))
  10024. (let* ((date (calendar-cursor-to-date))
  10025. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10026. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10027. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10028. (select-window sw)))
  10029. (defun org-calendar-select ()
  10030. "Return to `org-read-date' with the date currently selected.
  10031. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10032. (interactive)
  10033. (when (calendar-cursor-to-date)
  10034. (let* ((date (calendar-cursor-to-date))
  10035. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10036. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10037. (if (active-minibuffer-window) (exit-minibuffer))))
  10038. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10039. "Insert a date stamp for the date given by the internal TIME.
  10040. WITH-HM means, use the stamp format that includes the time of the day.
  10041. INACTIVE means use square brackets instead of angular ones, so that the
  10042. stamp will not contribute to the agenda.
  10043. PRE and POST are optional strings to be inserted before and after the
  10044. stamp.
  10045. The command returns the inserted time stamp."
  10046. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10047. stamp)
  10048. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10049. (insert-before-markers (or pre ""))
  10050. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10051. (when (listp extra)
  10052. (setq extra (car extra))
  10053. (if (and (stringp extra)
  10054. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10055. (setq extra (format "-%02d:%02d"
  10056. (string-to-number (match-string 1 extra))
  10057. (string-to-number (match-string 2 extra))))
  10058. (setq extra nil)))
  10059. (when extra
  10060. (backward-char 1)
  10061. (insert-before-markers extra)
  10062. (forward-char 1))
  10063. (insert-before-markers (or post ""))
  10064. (setq org-last-inserted-timestamp stamp)))
  10065. (defun org-toggle-time-stamp-overlays ()
  10066. "Toggle the use of custom time stamp formats."
  10067. (interactive)
  10068. (setq org-display-custom-times (not org-display-custom-times))
  10069. (unless org-display-custom-times
  10070. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10071. (while (setq p (next-single-property-change p 'display))
  10072. (if (and (get-text-property p 'display)
  10073. (eq (get-text-property p 'face) 'org-date))
  10074. (remove-text-properties
  10075. p (setq p (next-single-property-change p 'display))
  10076. '(display t))))
  10077. (set-buffer-modified-p bmp)))
  10078. (if (featurep 'xemacs)
  10079. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10080. (org-restart-font-lock)
  10081. (setq org-table-may-need-update t)
  10082. (if org-display-custom-times
  10083. (message "Time stamps are overlayed with custom format")
  10084. (message "Time stamp overlays removed")))
  10085. (defun org-display-custom-time (beg end)
  10086. "Overlay modified time stamp format over timestamp between BEG and END."
  10087. (let* ((ts (buffer-substring beg end))
  10088. t1 w1 with-hm tf time str w2 (off 0))
  10089. (save-match-data
  10090. (setq t1 (org-parse-time-string ts t))
  10091. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10092. (setq off (- (match-end 0) (match-beginning 0)))))
  10093. (setq end (- end off))
  10094. (setq w1 (- end beg)
  10095. with-hm (and (nth 1 t1) (nth 2 t1))
  10096. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10097. time (org-fix-decoded-time t1)
  10098. str (org-add-props
  10099. (format-time-string
  10100. (substring tf 1 -1) (apply 'encode-time time))
  10101. nil 'mouse-face 'highlight)
  10102. w2 (length str))
  10103. (if (not (= w2 w1))
  10104. (add-text-properties (1+ beg) (+ 2 beg)
  10105. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10106. (if (featurep 'xemacs)
  10107. (progn
  10108. (put-text-property beg end 'invisible t)
  10109. (put-text-property beg end 'end-glyph (make-glyph str)))
  10110. (put-text-property beg end 'display str))))
  10111. (defun org-translate-time (string)
  10112. "Translate all timestamps in STRING to custom format.
  10113. But do this only if the variable `org-display-custom-times' is set."
  10114. (when org-display-custom-times
  10115. (save-match-data
  10116. (let* ((start 0)
  10117. (re org-ts-regexp-both)
  10118. t1 with-hm inactive tf time str beg end)
  10119. (while (setq start (string-match re string start))
  10120. (setq beg (match-beginning 0)
  10121. end (match-end 0)
  10122. t1 (save-match-data
  10123. (org-parse-time-string (substring string beg end) t))
  10124. with-hm (and (nth 1 t1) (nth 2 t1))
  10125. inactive (equal (substring string beg (1+ beg)) "[")
  10126. tf (funcall (if with-hm 'cdr 'car)
  10127. org-time-stamp-custom-formats)
  10128. time (org-fix-decoded-time t1)
  10129. str (format-time-string
  10130. (concat
  10131. (if inactive "[" "<") (substring tf 1 -1)
  10132. (if inactive "]" ">"))
  10133. (apply 'encode-time time))
  10134. string (replace-match str t t string)
  10135. start (+ start (length str)))))))
  10136. string)
  10137. (defun org-fix-decoded-time (time)
  10138. "Set 0 instead of nil for the first 6 elements of time.
  10139. Don't touch the rest."
  10140. (let ((n 0))
  10141. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10142. (defun org-days-to-time (timestamp-string)
  10143. "Difference between TIMESTAMP-STRING and now in days."
  10144. (- (time-to-days (org-time-string-to-time timestamp-string))
  10145. (time-to-days (current-time))))
  10146. (defun org-deadline-close (timestamp-string &optional ndays)
  10147. "Is the time in TIMESTAMP-STRING close to the current date?"
  10148. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10149. (and (< (org-days-to-time timestamp-string) ndays)
  10150. (not (org-entry-is-done-p))))
  10151. (defun org-get-wdays (ts)
  10152. "Get the deadline lead time appropriate for timestring TS."
  10153. (cond
  10154. ((<= org-deadline-warning-days 0)
  10155. ;; 0 or negative, enforce this value no matter what
  10156. (- org-deadline-warning-days))
  10157. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10158. ;; lead time is specified.
  10159. (floor (* (string-to-number (match-string 1 ts))
  10160. (cdr (assoc (match-string 2 ts)
  10161. '(("d" . 1) ("w" . 7)
  10162. ("m" . 30.4) ("y" . 365.25)))))))
  10163. ;; go for the default.
  10164. (t org-deadline-warning-days)))
  10165. (defun org-calendar-select-mouse (ev)
  10166. "Return to `org-read-date' with the date currently selected.
  10167. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10168. (interactive "e")
  10169. (mouse-set-point ev)
  10170. (when (calendar-cursor-to-date)
  10171. (let* ((date (calendar-cursor-to-date))
  10172. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10173. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10174. (if (active-minibuffer-window) (exit-minibuffer))))
  10175. (defun org-check-deadlines (ndays)
  10176. "Check if there are any deadlines due or past due.
  10177. A deadline is considered due if it happens within `org-deadline-warning-days'
  10178. days from today's date. If the deadline appears in an entry marked DONE,
  10179. it is not shown. The prefix arg NDAYS can be used to test that many
  10180. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10181. (interactive "P")
  10182. (let* ((org-warn-days
  10183. (cond
  10184. ((equal ndays '(4)) 100000)
  10185. (ndays (prefix-numeric-value ndays))
  10186. (t (abs org-deadline-warning-days))))
  10187. (case-fold-search nil)
  10188. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10189. (callback
  10190. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10191. (message "%d deadlines past-due or due within %d days"
  10192. (org-occur regexp nil callback)
  10193. org-warn-days)))
  10194. (defun org-check-before-date (date)
  10195. "Check if there are deadlines or scheduled entries before DATE."
  10196. (interactive (list (org-read-date)))
  10197. (let ((case-fold-search nil)
  10198. (regexp (concat "\\<\\(" org-deadline-string
  10199. "\\|" org-scheduled-string
  10200. "\\) *<\\([^>]+\\)>"))
  10201. (callback
  10202. (lambda () (time-less-p
  10203. (org-time-string-to-time (match-string 2))
  10204. (org-time-string-to-time date)))))
  10205. (message "%d entries before %s"
  10206. (org-occur regexp nil callback) date)))
  10207. (defun org-evaluate-time-range (&optional to-buffer)
  10208. "Evaluate a time range by computing the difference between start and end.
  10209. Normally the result is just printed in the echo area, but with prefix arg
  10210. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10211. If the time range is actually in a table, the result is inserted into the
  10212. next column.
  10213. For time difference computation, a year is assumed to be exactly 365
  10214. days in order to avoid rounding problems."
  10215. (interactive "P")
  10216. (or
  10217. (org-clock-update-time-maybe)
  10218. (save-excursion
  10219. (unless (org-at-date-range-p t)
  10220. (goto-char (point-at-bol))
  10221. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10222. (if (not (org-at-date-range-p t))
  10223. (error "Not at a time-stamp range, and none found in current line")))
  10224. (let* ((ts1 (match-string 1))
  10225. (ts2 (match-string 2))
  10226. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10227. (match-end (match-end 0))
  10228. (time1 (org-time-string-to-time ts1))
  10229. (time2 (org-time-string-to-time ts2))
  10230. (t1 (time-to-seconds time1))
  10231. (t2 (time-to-seconds time2))
  10232. (diff (abs (- t2 t1)))
  10233. (negative (< (- t2 t1) 0))
  10234. ;; (ys (floor (* 365 24 60 60)))
  10235. (ds (* 24 60 60))
  10236. (hs (* 60 60))
  10237. (fy "%dy %dd %02d:%02d")
  10238. (fy1 "%dy %dd")
  10239. (fd "%dd %02d:%02d")
  10240. (fd1 "%dd")
  10241. (fh "%02d:%02d")
  10242. y d h m align)
  10243. (if havetime
  10244. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10245. y 0
  10246. d (floor (/ diff ds)) diff (mod diff ds)
  10247. h (floor (/ diff hs)) diff (mod diff hs)
  10248. m (floor (/ diff 60)))
  10249. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10250. y 0
  10251. d (floor (+ (/ diff ds) 0.5))
  10252. h 0 m 0))
  10253. (if (not to-buffer)
  10254. (message "%s" (org-make-tdiff-string y d h m))
  10255. (if (org-at-table-p)
  10256. (progn
  10257. (goto-char match-end)
  10258. (setq align t)
  10259. (and (looking-at " *|") (goto-char (match-end 0))))
  10260. (goto-char match-end))
  10261. (if (looking-at
  10262. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10263. (replace-match ""))
  10264. (if negative (insert " -"))
  10265. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10266. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10267. (insert " " (format fh h m))))
  10268. (if align (org-table-align))
  10269. (message "Time difference inserted")))))
  10270. (defun org-make-tdiff-string (y d h m)
  10271. (let ((fmt "")
  10272. (l nil))
  10273. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10274. l (push y l)))
  10275. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10276. l (push d l)))
  10277. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10278. l (push h l)))
  10279. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10280. l (push m l)))
  10281. (apply 'format fmt (nreverse l))))
  10282. (defun org-time-string-to-time (s)
  10283. (apply 'encode-time (org-parse-time-string s)))
  10284. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10285. "Convert a time stamp to an absolute day number.
  10286. If there is a specifyer for a cyclic time stamp, get the closest date to
  10287. DAYNR.
  10288. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10289. (cond
  10290. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10291. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10292. daynr
  10293. (+ daynr 1000)))
  10294. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10295. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10296. (time-to-days (current-time))) (match-string 0 s)
  10297. prefer show-all))
  10298. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10299. (defun org-days-to-iso-week (days)
  10300. "Return the iso week number."
  10301. (require 'cal-iso)
  10302. (car (calendar-iso-from-absolute days)))
  10303. (defun org-small-year-to-year (year)
  10304. "Convert 2-digit years into 4-digit years.
  10305. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10306. The year 2000 cannot be abbreviated. Any year lager than 99
  10307. is retrned unchanged."
  10308. (if (< year 38)
  10309. (setq year (+ 2000 year))
  10310. (if (< year 100)
  10311. (setq year (+ 1900 year))))
  10312. year)
  10313. (defun org-time-from-absolute (d)
  10314. "Return the time corresponding to date D.
  10315. D may be an absolute day number, or a calendar-type list (month day year)."
  10316. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10317. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10318. (defun org-calendar-holiday ()
  10319. "List of holidays, for Diary display in Org-mode."
  10320. (require 'holidays)
  10321. (let ((hl (funcall
  10322. (if (fboundp 'calendar-check-holidays)
  10323. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10324. (if hl (mapconcat 'identity hl "; "))))
  10325. (defun org-diary-sexp-entry (sexp entry date)
  10326. "Process a SEXP diary ENTRY for DATE."
  10327. (require 'diary-lib)
  10328. (let ((result (if calendar-debug-sexp
  10329. (let ((stack-trace-on-error t))
  10330. (eval (car (read-from-string sexp))))
  10331. (condition-case nil
  10332. (eval (car (read-from-string sexp)))
  10333. (error
  10334. (beep)
  10335. (message "Bad sexp at line %d in %s: %s"
  10336. (org-current-line)
  10337. (buffer-file-name) sexp)
  10338. (sleep-for 2))))))
  10339. (cond ((stringp result) result)
  10340. ((and (consp result)
  10341. (stringp (cdr result))) (cdr result))
  10342. (result entry)
  10343. (t nil))))
  10344. (defun org-diary-to-ical-string (frombuf)
  10345. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10346. This uses the icalendar.el library."
  10347. (let* ((tmpdir (if (featurep 'xemacs)
  10348. (temp-directory)
  10349. temporary-file-directory))
  10350. (tmpfile (make-temp-name
  10351. (expand-file-name "orgics" tmpdir)))
  10352. buf rtn b e)
  10353. (save-excursion
  10354. (set-buffer frombuf)
  10355. (icalendar-export-region (point-min) (point-max) tmpfile)
  10356. (setq buf (find-buffer-visiting tmpfile))
  10357. (set-buffer buf)
  10358. (goto-char (point-min))
  10359. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10360. (setq b (match-beginning 0)))
  10361. (goto-char (point-max))
  10362. (if (re-search-backward "^END:VEVENT" nil t)
  10363. (setq e (match-end 0)))
  10364. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10365. (kill-buffer buf)
  10366. (delete-file tmpfile)
  10367. rtn))
  10368. (defun org-closest-date (start current change prefer show-all)
  10369. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10370. When PREFER is `past' return a date that is either CURRENT or past.
  10371. When PREFER is `future', return a date that is either CURRENT or future.
  10372. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10373. ;; Make the proper lists from the dates
  10374. (catch 'exit
  10375. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10376. dn dw sday cday n1 n2
  10377. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10378. (setq start (org-date-to-gregorian start)
  10379. current (org-date-to-gregorian
  10380. (if show-all
  10381. current
  10382. (time-to-days (current-time))))
  10383. sday (calendar-absolute-from-gregorian start)
  10384. cday (calendar-absolute-from-gregorian current))
  10385. (if (<= cday sday) (throw 'exit sday))
  10386. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10387. (setq dn (string-to-number (match-string 1 change))
  10388. dw (cdr (assoc (match-string 2 change) a1)))
  10389. (error "Invalid change specifyer: %s" change))
  10390. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10391. (cond
  10392. ((eq dw 'day)
  10393. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10394. n2 (+ n1 dn)))
  10395. ((eq dw 'year)
  10396. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10397. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10398. (setq date1 (list m d y1)
  10399. n1 (calendar-absolute-from-gregorian date1)
  10400. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10401. n2 (calendar-absolute-from-gregorian date2)))
  10402. ((eq dw 'month)
  10403. ;; approx number of month between the two dates
  10404. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10405. ;; How often does dn fit in there?
  10406. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10407. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10408. m (+ m nm)
  10409. ny (floor (/ m 12))
  10410. y (+ y ny)
  10411. m (- m (* ny 12)))
  10412. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10413. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10414. (setq m2 (+ m dn) y2 y)
  10415. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10416. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10417. (while (<= n2 cday)
  10418. (setq n1 n2 m m2 y y2)
  10419. (setq m2 (+ m dn) y2 y)
  10420. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10421. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10422. (if show-all
  10423. (cond
  10424. ((eq prefer 'past) n1)
  10425. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10426. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10427. (cond
  10428. ((eq prefer 'past) n1)
  10429. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10430. (t (if (= cday n1) n1 n2)))))))
  10431. (defun org-date-to-gregorian (date)
  10432. "Turn any specification of DATE into a gregorian date for the calendar."
  10433. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10434. ((and (listp date) (= (length date) 3)) date)
  10435. ((stringp date)
  10436. (setq date (org-parse-time-string date))
  10437. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10438. ((listp date)
  10439. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10440. (defun org-parse-time-string (s &optional nodefault)
  10441. "Parse the standard Org-mode time string.
  10442. This should be a lot faster than the normal `parse-time-string'.
  10443. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10444. hour and minute fields will be nil if not given."
  10445. (if (string-match org-ts-regexp0 s)
  10446. (list 0
  10447. (if (or (match-beginning 8) (not nodefault))
  10448. (string-to-number (or (match-string 8 s) "0")))
  10449. (if (or (match-beginning 7) (not nodefault))
  10450. (string-to-number (or (match-string 7 s) "0")))
  10451. (string-to-number (match-string 4 s))
  10452. (string-to-number (match-string 3 s))
  10453. (string-to-number (match-string 2 s))
  10454. nil nil nil)
  10455. (make-list 9 0)))
  10456. (defun org-timestamp-up (&optional arg)
  10457. "Increase the date item at the cursor by one.
  10458. If the cursor is on the year, change the year. If it is on the month or
  10459. the day, change that.
  10460. With prefix ARG, change by that many units."
  10461. (interactive "p")
  10462. (org-timestamp-change (prefix-numeric-value arg)))
  10463. (defun org-timestamp-down (&optional arg)
  10464. "Decrease the date item at the cursor by one.
  10465. If the cursor is on the year, change the year. If it is on the month or
  10466. the day, change that.
  10467. With prefix ARG, change by that many units."
  10468. (interactive "p")
  10469. (org-timestamp-change (- (prefix-numeric-value arg))))
  10470. (defun org-timestamp-up-day (&optional arg)
  10471. "Increase the date in the time stamp by one day.
  10472. With prefix ARG, change that many days."
  10473. (interactive "p")
  10474. (if (and (not (org-at-timestamp-p t))
  10475. (org-on-heading-p))
  10476. (org-todo 'up)
  10477. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10478. (defun org-timestamp-down-day (&optional arg)
  10479. "Decrease the date in the time stamp by one day.
  10480. With prefix ARG, change that many days."
  10481. (interactive "p")
  10482. (if (and (not (org-at-timestamp-p t))
  10483. (org-on-heading-p))
  10484. (org-todo 'down)
  10485. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10486. (defun org-at-timestamp-p (&optional inactive-ok)
  10487. "Determine if the cursor is in or at a timestamp."
  10488. (interactive)
  10489. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10490. (pos (point))
  10491. (ans (or (looking-at tsr)
  10492. (save-excursion
  10493. (skip-chars-backward "^[<\n\r\t")
  10494. (if (> (point) (point-min)) (backward-char 1))
  10495. (and (looking-at tsr)
  10496. (> (- (match-end 0) pos) -1))))))
  10497. (and ans
  10498. (boundp 'org-ts-what)
  10499. (setq org-ts-what
  10500. (cond
  10501. ((= pos (match-beginning 0)) 'bracket)
  10502. ((= pos (1- (match-end 0))) 'bracket)
  10503. ((org-pos-in-match-range pos 2) 'year)
  10504. ((org-pos-in-match-range pos 3) 'month)
  10505. ((org-pos-in-match-range pos 7) 'hour)
  10506. ((org-pos-in-match-range pos 8) 'minute)
  10507. ((or (org-pos-in-match-range pos 4)
  10508. (org-pos-in-match-range pos 5)) 'day)
  10509. ((and (> pos (or (match-end 8) (match-end 5)))
  10510. (< pos (match-end 0)))
  10511. (- pos (or (match-end 8) (match-end 5))))
  10512. (t 'day))))
  10513. ans))
  10514. (defun org-toggle-timestamp-type ()
  10515. "Toggle the type (<active> or [inactive]) of a time stamp."
  10516. (interactive)
  10517. (when (org-at-timestamp-p t)
  10518. (let ((beg (match-beginning 0)) (end (match-end 0))
  10519. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10520. (save-excursion
  10521. (goto-char beg)
  10522. (while (re-search-forward "[][<>]" end t)
  10523. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10524. t t)))
  10525. (message "Timestamp is now %sactive"
  10526. (if (equal (char-after beg) ?<) "" "in")))))
  10527. (defun org-timestamp-change (n &optional what)
  10528. "Change the date in the time stamp at point.
  10529. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10530. `year', `minute', `second'. If WHAT is not given, the cursor position
  10531. in the timestamp determines what will be changed."
  10532. (let ((pos (point))
  10533. with-hm inactive
  10534. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10535. org-ts-what
  10536. extra rem
  10537. ts time time0)
  10538. (if (not (org-at-timestamp-p t))
  10539. (error "Not at a timestamp"))
  10540. (if (and (not what) (eq org-ts-what 'bracket))
  10541. (org-toggle-timestamp-type)
  10542. (if (and (not what) (not (eq org-ts-what 'day))
  10543. org-display-custom-times
  10544. (get-text-property (point) 'display)
  10545. (not (get-text-property (1- (point)) 'display)))
  10546. (setq org-ts-what 'day))
  10547. (setq org-ts-what (or what org-ts-what)
  10548. inactive (= (char-after (match-beginning 0)) ?\[)
  10549. ts (match-string 0))
  10550. (replace-match "")
  10551. (if (string-match
  10552. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10553. ts)
  10554. (setq extra (match-string 1 ts)))
  10555. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10556. (setq with-hm t))
  10557. (setq time0 (org-parse-time-string ts))
  10558. (when (and (eq org-ts-what 'minute)
  10559. (eq current-prefix-arg nil))
  10560. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10561. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10562. (setcar (cdr time0) (+ (nth 1 time0)
  10563. (if (> n 0) (- rem) (- dm rem))))))
  10564. (setq time
  10565. (encode-time (or (car time0) 0)
  10566. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10567. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10568. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10569. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10570. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10571. (nthcdr 6 time0)))
  10572. (when (integerp org-ts-what)
  10573. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10574. (if (eq what 'calendar)
  10575. (let ((cal-date (org-get-date-from-calendar)))
  10576. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10577. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10578. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10579. (setcar time0 (or (car time0) 0))
  10580. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10581. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10582. (setq time (apply 'encode-time time0))))
  10583. (setq org-last-changed-timestamp
  10584. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10585. (org-clock-update-time-maybe)
  10586. (goto-char pos)
  10587. ;; Try to recenter the calendar window, if any
  10588. (if (and org-calendar-follow-timestamp-change
  10589. (get-buffer-window "*Calendar*" t)
  10590. (memq org-ts-what '(day month year)))
  10591. (org-recenter-calendar (time-to-days time))))))
  10592. (defun org-modify-ts-extra (s pos n dm)
  10593. "Change the different parts of the lead-time and repeat fields in timestamp."
  10594. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10595. ng h m new rem)
  10596. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10597. (cond
  10598. ((or (org-pos-in-match-range pos 2)
  10599. (org-pos-in-match-range pos 3))
  10600. (setq m (string-to-number (match-string 3 s))
  10601. h (string-to-number (match-string 2 s)))
  10602. (if (org-pos-in-match-range pos 2)
  10603. (setq h (+ h n))
  10604. (setq n (* dm (org-no-warnings (signum n))))
  10605. (when (not (= 0 (setq rem (% m dm))))
  10606. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10607. (setq m (+ m n)))
  10608. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10609. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10610. (setq h (min 24 (max 0 h)))
  10611. (setq ng 1 new (format "-%02d:%02d" h m)))
  10612. ((org-pos-in-match-range pos 6)
  10613. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10614. ((org-pos-in-match-range pos 5)
  10615. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10616. ((org-pos-in-match-range pos 9)
  10617. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10618. ((org-pos-in-match-range pos 8)
  10619. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10620. (when ng
  10621. (setq s (concat
  10622. (substring s 0 (match-beginning ng))
  10623. new
  10624. (substring s (match-end ng))))))
  10625. s))
  10626. (defun org-recenter-calendar (date)
  10627. "If the calendar is visible, recenter it to DATE."
  10628. (let* ((win (selected-window))
  10629. (cwin (get-buffer-window "*Calendar*" t))
  10630. (calendar-move-hook nil))
  10631. (when cwin
  10632. (select-window cwin)
  10633. (calendar-goto-date (if (listp date) date
  10634. (calendar-gregorian-from-absolute date)))
  10635. (select-window win))))
  10636. (defun org-goto-calendar (&optional arg)
  10637. "Go to the Emacs calendar at the current date.
  10638. If there is a time stamp in the current line, go to that date.
  10639. A prefix ARG can be used to force the current date."
  10640. (interactive "P")
  10641. (let ((tsr org-ts-regexp) diff
  10642. (calendar-move-hook nil)
  10643. (calendar-view-holidays-initially-flag nil)
  10644. (view-calendar-holidays-initially nil)
  10645. (calendar-view-diary-initially-flag nil)
  10646. (view-diary-entries-initially nil))
  10647. (if (or (org-at-timestamp-p)
  10648. (save-excursion
  10649. (beginning-of-line 1)
  10650. (looking-at (concat ".*" tsr))))
  10651. (let ((d1 (time-to-days (current-time)))
  10652. (d2 (time-to-days
  10653. (org-time-string-to-time (match-string 1)))))
  10654. (setq diff (- d2 d1))))
  10655. (calendar)
  10656. (calendar-goto-today)
  10657. (if (and diff (not arg)) (calendar-forward-day diff))))
  10658. (defun org-get-date-from-calendar ()
  10659. "Return a list (month day year) of date at point in calendar."
  10660. (with-current-buffer "*Calendar*"
  10661. (save-match-data
  10662. (calendar-cursor-to-date))))
  10663. (defun org-date-from-calendar ()
  10664. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10665. If there is already a time stamp at the cursor position, update it."
  10666. (interactive)
  10667. (if (org-at-timestamp-p t)
  10668. (org-timestamp-change 0 'calendar)
  10669. (let ((cal-date (org-get-date-from-calendar)))
  10670. (org-insert-time-stamp
  10671. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10672. (defun org-minutes-to-hh:mm-string (m)
  10673. "Compute H:MM from a number of minutes."
  10674. (let ((h (/ m 60)))
  10675. (setq m (- m (* 60 h)))
  10676. (format org-time-clocksum-format h m)))
  10677. (defun org-hh:mm-string-to-minutes (s)
  10678. "Convert a string H:MM to a number of minutes."
  10679. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10680. (+ (* (string-to-number (match-string 1 s)) 60)
  10681. (string-to-number (match-string 2 s)))
  10682. 0))
  10683. ;;;; Agenda files
  10684. ;;;###autoload
  10685. (defun org-iswitchb (&optional arg)
  10686. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10687. With a prefix argument, restrict available to files.
  10688. With two prefix arguments, restrict available buffers to agenda files.
  10689. Due to some yet unresolved reason, the global function
  10690. `iswitchb-mode' needs to be active for this function to work."
  10691. (interactive "P")
  10692. (require 'iswitchb)
  10693. (let ((enabled iswitchb-mode) blist)
  10694. (or enabled (iswitchb-mode 1))
  10695. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10696. ((equal arg '(16)) (org-buffer-list 'agenda))
  10697. (t (org-buffer-list))))
  10698. (unwind-protect
  10699. (let ((iswitchb-make-buflist-hook
  10700. (lambda ()
  10701. (setq iswitchb-temp-buflist
  10702. (mapcar 'buffer-name blist)))))
  10703. (switch-to-buffer
  10704. (iswitchb-read-buffer
  10705. "Switch-to: " nil t))
  10706. (or enabled (iswitchb-mode -1))))))
  10707. (defun org-buffer-list (&optional predicate exclude-tmp)
  10708. "Return a list of Org buffers.
  10709. PREDICATE can be `export', `files' or `agenda'.
  10710. export restrict the list to Export buffers.
  10711. files restrict the list to buffers visiting Org files.
  10712. agenda restrict the list to buffers visiting agenda files.
  10713. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10714. (let* ((bfn nil)
  10715. (agenda-files (and (eq predicate 'agenda)
  10716. (mapcar 'file-truename (org-agenda-files t))))
  10717. (filter
  10718. (cond
  10719. ((eq predicate 'files)
  10720. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10721. ((eq predicate 'export)
  10722. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10723. ((eq predicate 'agenda)
  10724. (lambda (b)
  10725. (with-current-buffer b
  10726. (and (eq major-mode 'org-mode)
  10727. (setq bfn (buffer-file-name b))
  10728. (member (file-truename bfn) agenda-files)))))
  10729. (t (lambda (b) (with-current-buffer b
  10730. (or (eq major-mode 'org-mode)
  10731. (string-match "\*Org .*Export"
  10732. (buffer-name b)))))))))
  10733. (delq nil
  10734. (mapcar
  10735. (lambda(b)
  10736. (if (and (funcall filter b)
  10737. (or (not exclude-tmp)
  10738. (not (string-match "tmp" (buffer-name b)))))
  10739. b
  10740. nil))
  10741. (buffer-list)))))
  10742. (defun org-agenda-files (&optional unrestricted archives)
  10743. "Get the list of agenda files.
  10744. Optional UNRESTRICTED means return the full list even if a restriction
  10745. is currently in place.
  10746. When ARCHIVES is t, include all archive files hat are really being
  10747. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10748. `org-agenda-archives-mode' is t."
  10749. (let ((files
  10750. (cond
  10751. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10752. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10753. ((listp org-agenda-files) org-agenda-files)
  10754. (t (error "Invalid value of `org-agenda-files'")))))
  10755. (setq files (apply 'append
  10756. (mapcar (lambda (f)
  10757. (if (file-directory-p f)
  10758. (directory-files
  10759. f t org-agenda-file-regexp)
  10760. (list f)))
  10761. files)))
  10762. (when org-agenda-skip-unavailable-files
  10763. (setq files (delq nil
  10764. (mapcar (function
  10765. (lambda (file)
  10766. (and (file-readable-p file) file)))
  10767. files))))
  10768. (when (or (eq archives t)
  10769. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10770. (setq files (org-add-archive-files files)))
  10771. files))
  10772. (defun org-edit-agenda-file-list ()
  10773. "Edit the list of agenda files.
  10774. Depending on setup, this either uses customize to edit the variable
  10775. `org-agenda-files', or it visits the file that is holding the list. In the
  10776. latter case, the buffer is set up in a way that saving it automatically kills
  10777. the buffer and restores the previous window configuration."
  10778. (interactive)
  10779. (if (stringp org-agenda-files)
  10780. (let ((cw (current-window-configuration)))
  10781. (find-file org-agenda-files)
  10782. (org-set-local 'org-window-configuration cw)
  10783. (org-add-hook 'after-save-hook
  10784. (lambda ()
  10785. (set-window-configuration
  10786. (prog1 org-window-configuration
  10787. (kill-buffer (current-buffer))))
  10788. (org-install-agenda-files-menu)
  10789. (message "New agenda file list installed"))
  10790. nil 'local)
  10791. (message "%s" (substitute-command-keys
  10792. "Edit list and finish with \\[save-buffer]")))
  10793. (customize-variable 'org-agenda-files)))
  10794. (defun org-store-new-agenda-file-list (list)
  10795. "Set new value for the agenda file list and save it correcly."
  10796. (if (stringp org-agenda-files)
  10797. (let ((f org-agenda-files) b)
  10798. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10799. (with-temp-file f
  10800. (insert (mapconcat 'identity list "\n") "\n")))
  10801. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10802. (setq org-agenda-files list)
  10803. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10804. (defun org-read-agenda-file-list ()
  10805. "Read the list of agenda files from a file."
  10806. (when (file-directory-p org-agenda-files)
  10807. (error "`org-agenda-files' cannot be a single directory"))
  10808. (when (stringp org-agenda-files)
  10809. (with-temp-buffer
  10810. (insert-file-contents org-agenda-files)
  10811. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10812. ;;;###autoload
  10813. (defun org-cycle-agenda-files ()
  10814. "Cycle through the files in `org-agenda-files'.
  10815. If the current buffer visits an agenda file, find the next one in the list.
  10816. If the current buffer does not, find the first agenda file."
  10817. (interactive)
  10818. (let* ((fs (org-agenda-files t))
  10819. (files (append fs (list (car fs))))
  10820. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10821. file)
  10822. (unless files (error "No agenda files"))
  10823. (catch 'exit
  10824. (while (setq file (pop files))
  10825. (if (equal (file-truename file) tcf)
  10826. (when (car files)
  10827. (find-file (car files))
  10828. (throw 'exit t))))
  10829. (find-file (car fs)))
  10830. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10831. (defun org-agenda-file-to-front (&optional to-end)
  10832. "Move/add the current file to the top of the agenda file list.
  10833. If the file is not present in the list, it is added to the front. If it is
  10834. present, it is moved there. With optional argument TO-END, add/move to the
  10835. end of the list."
  10836. (interactive "P")
  10837. (let ((org-agenda-skip-unavailable-files nil)
  10838. (file-alist (mapcar (lambda (x)
  10839. (cons (file-truename x) x))
  10840. (org-agenda-files t)))
  10841. (ctf (file-truename buffer-file-name))
  10842. x had)
  10843. (setq x (assoc ctf file-alist) had x)
  10844. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10845. (if to-end
  10846. (setq file-alist (append (delq x file-alist) (list x)))
  10847. (setq file-alist (cons x (delq x file-alist))))
  10848. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10849. (org-install-agenda-files-menu)
  10850. (message "File %s to %s of agenda file list"
  10851. (if had "moved" "added") (if to-end "end" "front"))))
  10852. (defun org-remove-file (&optional file)
  10853. "Remove current file from the list of files in variable `org-agenda-files'.
  10854. These are the files which are being checked for agenda entries.
  10855. Optional argument FILE means, use this file instead of the current."
  10856. (interactive)
  10857. (let* ((org-agenda-skip-unavailable-files nil)
  10858. (file (or file buffer-file-name))
  10859. (true-file (file-truename file))
  10860. (afile (abbreviate-file-name file))
  10861. (files (delq nil (mapcar
  10862. (lambda (x)
  10863. (if (equal true-file
  10864. (file-truename x))
  10865. nil x))
  10866. (org-agenda-files t)))))
  10867. (if (not (= (length files) (length (org-agenda-files t))))
  10868. (progn
  10869. (org-store-new-agenda-file-list files)
  10870. (org-install-agenda-files-menu)
  10871. (message "Removed file: %s" afile))
  10872. (message "File was not in list: %s (not removed)" afile))))
  10873. (defun org-file-menu-entry (file)
  10874. (vector file (list 'find-file file) t))
  10875. (defun org-check-agenda-file (file)
  10876. "Make sure FILE exists. If not, ask user what to do."
  10877. (when (not (file-exists-p file))
  10878. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10879. (abbreviate-file-name file))
  10880. (let ((r (downcase (read-char-exclusive))))
  10881. (cond
  10882. ((equal r ?r)
  10883. (org-remove-file file)
  10884. (throw 'nextfile t))
  10885. (t (error "Abort"))))))
  10886. (defun org-get-agenda-file-buffer (file)
  10887. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10888. it to the list of buffers which might be released later."
  10889. (let ((buf (org-find-base-buffer-visiting file)))
  10890. (if buf
  10891. buf ; just return it
  10892. ;; Make a new buffer and remember it
  10893. (setq buf (find-file-noselect file))
  10894. (if buf (push buf org-agenda-new-buffers))
  10895. buf)))
  10896. (defun org-release-buffers (blist)
  10897. "Release all buffers in list, asking the user for confirmation when needed.
  10898. When a buffer is unmodified, it is just killed. When modified, it is saved
  10899. \(if the user agrees) and then killed."
  10900. (let (buf file)
  10901. (while (setq buf (pop blist))
  10902. (setq file (buffer-file-name buf))
  10903. (when (and (buffer-modified-p buf)
  10904. file
  10905. (y-or-n-p (format "Save file %s? " file)))
  10906. (with-current-buffer buf (save-buffer)))
  10907. (kill-buffer buf))))
  10908. (defun org-prepare-agenda-buffers (files)
  10909. "Create buffers for all agenda files, protect archived trees and comments."
  10910. (interactive)
  10911. (let ((pa '(:org-archived t))
  10912. (pc '(:org-comment t))
  10913. (pall '(:org-archived t :org-comment t))
  10914. (inhibit-read-only t)
  10915. (rea (concat ":" org-archive-tag ":"))
  10916. bmp file re)
  10917. (save-excursion
  10918. (save-restriction
  10919. (while (setq file (pop files))
  10920. (if (bufferp file)
  10921. (set-buffer file)
  10922. (org-check-agenda-file file)
  10923. (set-buffer (org-get-agenda-file-buffer file)))
  10924. (widen)
  10925. (setq bmp (buffer-modified-p))
  10926. (org-refresh-category-properties)
  10927. (setq org-todo-keywords-for-agenda
  10928. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10929. (setq org-done-keywords-for-agenda
  10930. (append org-done-keywords-for-agenda org-done-keywords))
  10931. (setq org-todo-keyword-alist-for-agenda
  10932. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  10933. (setq org-tag-alist-for-agenda
  10934. (append org-tag-alist-for-agenda org-tag-alist))
  10935. (save-excursion
  10936. (remove-text-properties (point-min) (point-max) pall)
  10937. (when org-agenda-skip-archived-trees
  10938. (goto-char (point-min))
  10939. (while (re-search-forward rea nil t)
  10940. (if (org-on-heading-p t)
  10941. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10942. (goto-char (point-min))
  10943. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10944. (while (re-search-forward re nil t)
  10945. (add-text-properties
  10946. (match-beginning 0) (org-end-of-subtree t) pc)))
  10947. (set-buffer-modified-p bmp))))
  10948. (setq org-todo-keyword-alist-for-agenda
  10949. (org-uniquify org-todo-keyword-alist-for-agenda)
  10950. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  10951. ;;;; Embedded LaTeX
  10952. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10953. "Keymap for the minor `org-cdlatex-mode'.")
  10954. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10955. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10956. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10957. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10958. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10959. (defvar org-cdlatex-texmathp-advice-is-done nil
  10960. "Flag remembering if we have applied the advice to texmathp already.")
  10961. (define-minor-mode org-cdlatex-mode
  10962. "Toggle the minor `org-cdlatex-mode'.
  10963. This mode supports entering LaTeX environment and math in LaTeX fragments
  10964. in Org-mode.
  10965. \\{org-cdlatex-mode-map}"
  10966. nil " OCDL" nil
  10967. (when org-cdlatex-mode (require 'cdlatex))
  10968. (unless org-cdlatex-texmathp-advice-is-done
  10969. (setq org-cdlatex-texmathp-advice-is-done t)
  10970. (defadvice texmathp (around org-math-always-on activate)
  10971. "Always return t in org-mode buffers.
  10972. This is because we want to insert math symbols without dollars even outside
  10973. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10974. en embedded LaTeX fragement, let texmathp do its job.
  10975. \\[org-cdlatex-mode-map]"
  10976. (interactive)
  10977. (let (p)
  10978. (cond
  10979. ((not (org-mode-p)) ad-do-it)
  10980. ((eq this-command 'cdlatex-math-symbol)
  10981. (setq ad-return-value t
  10982. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10983. (t
  10984. (let ((p (org-inside-LaTeX-fragment-p)))
  10985. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10986. (setq ad-return-value t
  10987. texmathp-why '("Org-mode embedded math" . 0))
  10988. (if p ad-do-it)))))))))
  10989. (defun turn-on-org-cdlatex ()
  10990. "Unconditionally turn on `org-cdlatex-mode'."
  10991. (org-cdlatex-mode 1))
  10992. (defun org-inside-LaTeX-fragment-p ()
  10993. "Test if point is inside a LaTeX fragment.
  10994. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10995. sequence appearing also before point.
  10996. Even though the matchers for math are configurable, this function assumes
  10997. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10998. delimiters are skipped when they have been removed by customization.
  10999. The return value is nil, or a cons cell with the delimiter and
  11000. and the position of this delimiter.
  11001. This function does a reasonably good job, but can locally be fooled by
  11002. for example currency specifications. For example it will assume being in
  11003. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11004. fragments that are properly closed, but during editing, we have to live
  11005. with the uncertainty caused by missing closing delimiters. This function
  11006. looks only before point, not after."
  11007. (catch 'exit
  11008. (let ((pos (point))
  11009. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11010. (lim (progn
  11011. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11012. (point)))
  11013. dd-on str (start 0) m re)
  11014. (goto-char pos)
  11015. (when dodollar
  11016. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11017. re (nth 1 (assoc "$" org-latex-regexps)))
  11018. (while (string-match re str start)
  11019. (cond
  11020. ((= (match-end 0) (length str))
  11021. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11022. ((= (match-end 0) (- (length str) 5))
  11023. (throw 'exit nil))
  11024. (t (setq start (match-end 0))))))
  11025. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11026. (goto-char pos)
  11027. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11028. (and (match-beginning 2) (throw 'exit nil))
  11029. ;; count $$
  11030. (while (re-search-backward "\\$\\$" lim t)
  11031. (setq dd-on (not dd-on)))
  11032. (goto-char pos)
  11033. (if dd-on (cons "$$" m))))))
  11034. (defun org-try-cdlatex-tab ()
  11035. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11036. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11037. - inside a LaTeX fragment, or
  11038. - after the first word in a line, where an abbreviation expansion could
  11039. insert a LaTeX environment."
  11040. (when org-cdlatex-mode
  11041. (cond
  11042. ((save-excursion
  11043. (skip-chars-backward "a-zA-Z0-9*")
  11044. (skip-chars-backward " \t")
  11045. (bolp))
  11046. (cdlatex-tab) t)
  11047. ((org-inside-LaTeX-fragment-p)
  11048. (cdlatex-tab) t)
  11049. (t nil))))
  11050. (defun org-cdlatex-underscore-caret (&optional arg)
  11051. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11052. Revert to the normal definition outside of these fragments."
  11053. (interactive "P")
  11054. (if (org-inside-LaTeX-fragment-p)
  11055. (call-interactively 'cdlatex-sub-superscript)
  11056. (let (org-cdlatex-mode)
  11057. (call-interactively (key-binding (vector last-input-event))))))
  11058. (defun org-cdlatex-math-modify (&optional arg)
  11059. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11060. Revert to the normal definition outside of these fragments."
  11061. (interactive "P")
  11062. (if (org-inside-LaTeX-fragment-p)
  11063. (call-interactively 'cdlatex-math-modify)
  11064. (let (org-cdlatex-mode)
  11065. (call-interactively (key-binding (vector last-input-event))))))
  11066. (defvar org-latex-fragment-image-overlays nil
  11067. "List of overlays carrying the images of latex fragments.")
  11068. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11069. (defun org-remove-latex-fragment-image-overlays ()
  11070. "Remove all overlays with LaTeX fragment images in current buffer."
  11071. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11072. (setq org-latex-fragment-image-overlays nil))
  11073. (defun org-preview-latex-fragment (&optional subtree)
  11074. "Preview the LaTeX fragment at point, or all locally or globally.
  11075. If the cursor is in a LaTeX fragment, create the image and overlay
  11076. it over the source code. If there is no fragment at point, display
  11077. all fragments in the current text, from one headline to the next. With
  11078. prefix SUBTREE, display all fragments in the current subtree. With a
  11079. double prefix `C-u C-u', or when the cursor is before the first headline,
  11080. display all fragments in the buffer.
  11081. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11082. (interactive "P")
  11083. (org-remove-latex-fragment-image-overlays)
  11084. (save-excursion
  11085. (save-restriction
  11086. (let (beg end at msg)
  11087. (cond
  11088. ((or (equal subtree '(16))
  11089. (not (save-excursion
  11090. (re-search-backward (concat "^" outline-regexp) nil t))))
  11091. (setq beg (point-min) end (point-max)
  11092. msg "Creating images for buffer...%s"))
  11093. ((equal subtree '(4))
  11094. (org-back-to-heading)
  11095. (setq beg (point) end (org-end-of-subtree t)
  11096. msg "Creating images for subtree...%s"))
  11097. (t
  11098. (if (setq at (org-inside-LaTeX-fragment-p))
  11099. (goto-char (max (point-min) (- (cdr at) 2)))
  11100. (org-back-to-heading))
  11101. (setq beg (point) end (progn (outline-next-heading) (point))
  11102. msg (if at "Creating image...%s"
  11103. "Creating images for entry...%s"))))
  11104. (message msg "")
  11105. (narrow-to-region beg end)
  11106. (goto-char beg)
  11107. (org-format-latex
  11108. (concat "ltxpng/" (file-name-sans-extension
  11109. (file-name-nondirectory
  11110. buffer-file-name)))
  11111. default-directory 'overlays msg at 'forbuffer)
  11112. (message msg "done. Use `C-c C-c' to remove images.")))))
  11113. (defvar org-latex-regexps
  11114. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11115. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11116. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11117. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11118. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11119. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11120. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11121. "Regular expressions for matching embedded LaTeX.")
  11122. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11123. "Replace LaTeX fragments with links to an image, and produce images."
  11124. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11125. (let* ((prefixnodir (file-name-nondirectory prefix))
  11126. (absprefix (expand-file-name prefix dir))
  11127. (todir (file-name-directory absprefix))
  11128. (opt org-format-latex-options)
  11129. (matchers (plist-get opt :matchers))
  11130. (re-list org-latex-regexps)
  11131. (cnt 0) txt link beg end re e checkdir
  11132. m n block linkfile movefile ov)
  11133. ;; Check if there are old images files with this prefix, and remove them
  11134. (when (file-directory-p todir)
  11135. (mapc 'delete-file
  11136. (directory-files
  11137. todir 'full
  11138. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11139. ;; Check the different regular expressions
  11140. (while (setq e (pop re-list))
  11141. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11142. block (if (nth 3 e) "\n\n" ""))
  11143. (when (member m matchers)
  11144. (goto-char (point-min))
  11145. (while (re-search-forward re nil t)
  11146. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11147. (setq txt (match-string n)
  11148. beg (match-beginning n) end (match-end n)
  11149. cnt (1+ cnt)
  11150. linkfile (format "%s_%04d.png" prefix cnt)
  11151. movefile (format "%s_%04d.png" absprefix cnt)
  11152. link (concat block "[[file:" linkfile "]]" block))
  11153. (if msg (message msg cnt))
  11154. (goto-char beg)
  11155. (unless checkdir ; make sure the directory exists
  11156. (setq checkdir t)
  11157. (or (file-directory-p todir) (make-directory todir)))
  11158. (org-create-formula-image
  11159. txt movefile opt forbuffer)
  11160. (if overlays
  11161. (progn
  11162. (setq ov (org-make-overlay beg end))
  11163. (if (featurep 'xemacs)
  11164. (progn
  11165. (org-overlay-put ov 'invisible t)
  11166. (org-overlay-put
  11167. ov 'end-glyph
  11168. (make-glyph (vector 'png :file movefile))))
  11169. (org-overlay-put
  11170. ov 'display
  11171. (list 'image :type 'png :file movefile :ascent 'center)))
  11172. (push ov org-latex-fragment-image-overlays)
  11173. (goto-char end))
  11174. (delete-region beg end)
  11175. (insert link))))))))
  11176. ;; This function borrows from Ganesh Swami's latex2png.el
  11177. (defun org-create-formula-image (string tofile options buffer)
  11178. (let* ((tmpdir (if (featurep 'xemacs)
  11179. (temp-directory)
  11180. temporary-file-directory))
  11181. (texfilebase (make-temp-name
  11182. (expand-file-name "orgtex" tmpdir)))
  11183. (texfile (concat texfilebase ".tex"))
  11184. (dvifile (concat texfilebase ".dvi"))
  11185. (pngfile (concat texfilebase ".png"))
  11186. (fnh (if (featurep 'xemacs)
  11187. (font-height (get-face-font 'default))
  11188. (face-attribute 'default :height nil)))
  11189. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11190. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11191. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11192. "Black"))
  11193. (bg (or (plist-get options (if buffer :background :html-background))
  11194. "Transparent")))
  11195. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11196. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11197. (with-temp-file texfile
  11198. (insert org-format-latex-header
  11199. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11200. (let ((dir default-directory))
  11201. (condition-case nil
  11202. (progn
  11203. (cd tmpdir)
  11204. (call-process "latex" nil nil nil texfile))
  11205. (error nil))
  11206. (cd dir))
  11207. (if (not (file-exists-p dvifile))
  11208. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11209. (condition-case nil
  11210. (call-process "dvipng" nil nil nil
  11211. "-E" "-fg" fg "-bg" bg
  11212. "-D" dpi
  11213. ;;"-x" scale "-y" scale
  11214. "-T" "tight"
  11215. "-o" pngfile
  11216. dvifile)
  11217. (error nil))
  11218. (if (not (file-exists-p pngfile))
  11219. (progn (message "Failed to create png file from %s" texfile) nil)
  11220. ;; Use the requested file name and clean up
  11221. (copy-file pngfile tofile 'replace)
  11222. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11223. (delete-file (concat texfilebase e)))
  11224. pngfile))))
  11225. (defun org-dvipng-color (attr)
  11226. "Return an rgb color specification for dvipng."
  11227. (apply 'format "rgb %s %s %s"
  11228. (mapcar 'org-normalize-color
  11229. (color-values (face-attribute 'default attr nil)))))
  11230. (defun org-normalize-color (value)
  11231. "Return string to be used as color value for an RGB component."
  11232. (format "%g" (/ value 65535.0)))
  11233. ;;;; Key bindings
  11234. ;; Make `C-c C-x' a prefix key
  11235. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11236. ;; TAB key with modifiers
  11237. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11238. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11239. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11240. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11241. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11242. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11243. ;; The following line is necessary under Suse GNU/Linux
  11244. (unless (featurep 'xemacs)
  11245. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11246. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11247. (define-key org-mode-map [backtab] 'org-shifttab)
  11248. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11249. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11250. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11251. ;; Cursor keys with modifiers
  11252. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11253. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11254. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11255. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11256. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11257. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11258. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11259. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11260. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11261. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11262. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11263. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11264. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11265. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11266. ;;; Extra keys for tty access.
  11267. ;; We only set them when really needed because otherwise the
  11268. ;; menus don't show the simple keys
  11269. (when (or org-use-extra-keys
  11270. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11271. (not window-system))
  11272. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11273. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11274. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11275. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11276. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11277. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11278. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11279. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11280. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11281. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11282. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11283. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11284. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11285. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11286. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11287. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11288. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11289. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11290. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11291. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11292. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11293. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11294. ;; All the other keys
  11295. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11296. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11297. (if (boundp 'narrow-map)
  11298. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11299. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11300. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11301. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11302. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11303. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11304. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11305. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11306. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11307. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11308. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11309. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11310. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11311. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11312. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11313. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11314. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11315. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11316. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11317. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11318. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11319. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11320. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11321. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11322. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11323. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11324. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11325. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11326. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11327. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11328. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11329. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11330. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11331. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11332. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11333. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11334. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11335. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11336. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11337. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11338. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11339. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11340. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11341. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11342. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11343. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11344. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11345. (org-defkey org-mode-map "\C-m" 'org-return)
  11346. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11347. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11348. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11349. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11350. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11351. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11352. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11353. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11354. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11355. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11356. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11357. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11358. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11359. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11360. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11361. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11362. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11363. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11364. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11365. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11366. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11367. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11368. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11369. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11370. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11371. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11372. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11373. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11374. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11375. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11376. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11377. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11378. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11379. (when (featurep 'xemacs)
  11380. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11381. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11382. (defun org-self-insert-command (N)
  11383. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11384. If the cursor is in a table looking at whitespace, the whitespace is
  11385. overwritten, and the table is not marked as requiring realignment."
  11386. (interactive "p")
  11387. (if (and (org-table-p)
  11388. (progn
  11389. ;; check if we blank the field, and if that triggers align
  11390. (and (featurep 'org-table) org-table-auto-blank-field
  11391. (member last-command
  11392. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11393. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11394. ;; got extra space, this field does not determine column width
  11395. (let (org-table-may-need-update) (org-table-blank-field))
  11396. ;; no extra space, this field may determine column width
  11397. (org-table-blank-field)))
  11398. t)
  11399. (eq N 1)
  11400. (looking-at "[^|\n]* |"))
  11401. (let (org-table-may-need-update)
  11402. (goto-char (1- (match-end 0)))
  11403. (delete-backward-char 1)
  11404. (goto-char (match-beginning 0))
  11405. (self-insert-command N))
  11406. (setq org-table-may-need-update t)
  11407. (self-insert-command N)
  11408. (org-fix-tags-on-the-fly)))
  11409. (defun org-fix-tags-on-the-fly ()
  11410. (when (and (equal (char-after (point-at-bol)) ?*)
  11411. (org-on-heading-p))
  11412. (org-align-tags-here org-tags-column)))
  11413. (defun org-delete-backward-char (N)
  11414. "Like `delete-backward-char', insert whitespace at field end in tables.
  11415. When deleting backwards, in tables this function will insert whitespace in
  11416. front of the next \"|\" separator, to keep the table aligned. The table will
  11417. still be marked for re-alignment if the field did fill the entire column,
  11418. because, in this case the deletion might narrow the column."
  11419. (interactive "p")
  11420. (if (and (org-table-p)
  11421. (eq N 1)
  11422. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11423. (looking-at ".*?|"))
  11424. (let ((pos (point))
  11425. (noalign (looking-at "[^|\n\r]* |"))
  11426. (c org-table-may-need-update))
  11427. (backward-delete-char N)
  11428. (skip-chars-forward "^|")
  11429. (insert " ")
  11430. (goto-char (1- pos))
  11431. ;; noalign: if there were two spaces at the end, this field
  11432. ;; does not determine the width of the column.
  11433. (if noalign (setq org-table-may-need-update c)))
  11434. (backward-delete-char N)
  11435. (org-fix-tags-on-the-fly)))
  11436. (defun org-delete-char (N)
  11437. "Like `delete-char', but insert whitespace at field end in tables.
  11438. When deleting characters, in tables this function will insert whitespace in
  11439. front of the next \"|\" separator, to keep the table aligned. The table will
  11440. still be marked for re-alignment if the field did fill the entire column,
  11441. because, in this case the deletion might narrow the column."
  11442. (interactive "p")
  11443. (if (and (org-table-p)
  11444. (not (bolp))
  11445. (not (= (char-after) ?|))
  11446. (eq N 1))
  11447. (if (looking-at ".*?|")
  11448. (let ((pos (point))
  11449. (noalign (looking-at "[^|\n\r]* |"))
  11450. (c org-table-may-need-update))
  11451. (replace-match (concat
  11452. (substring (match-string 0) 1 -1)
  11453. " |"))
  11454. (goto-char pos)
  11455. ;; noalign: if there were two spaces at the end, this field
  11456. ;; does not determine the width of the column.
  11457. (if noalign (setq org-table-may-need-update c)))
  11458. (delete-char N))
  11459. (delete-char N)
  11460. (org-fix-tags-on-the-fly)))
  11461. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11462. (put 'org-self-insert-command 'delete-selection t)
  11463. (put 'orgtbl-self-insert-command 'delete-selection t)
  11464. (put 'org-delete-char 'delete-selection 'supersede)
  11465. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11466. ;; Make `flyspell-mode' delay after some commands
  11467. (put 'org-self-insert-command 'flyspell-delayed t)
  11468. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11469. (put 'org-delete-char 'flyspell-delayed t)
  11470. (put 'org-delete-backward-char 'flyspell-delayed t)
  11471. ;; Make pabbrev-mode expand after org-mode commands
  11472. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11473. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11474. ;; How to do this: Measure non-white length of current string
  11475. ;; If equal to column width, we should realign.
  11476. (defun org-remap (map &rest commands)
  11477. "In MAP, remap the functions given in COMMANDS.
  11478. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11479. (let (new old)
  11480. (while commands
  11481. (setq old (pop commands) new (pop commands))
  11482. (if (fboundp 'command-remapping)
  11483. (org-defkey map (vector 'remap old) new)
  11484. (substitute-key-definition old new map global-map)))))
  11485. (when (eq org-enable-table-editor 'optimized)
  11486. ;; If the user wants maximum table support, we need to hijack
  11487. ;; some standard editing functions
  11488. (org-remap org-mode-map
  11489. 'self-insert-command 'org-self-insert-command
  11490. 'delete-char 'org-delete-char
  11491. 'delete-backward-char 'org-delete-backward-char)
  11492. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11493. (defun org-shiftcursor-error ()
  11494. "Throw an error because Shift-Cursor command was applied in wrong context."
  11495. (error "This command is active in special context like tables, headlines or timestamps"))
  11496. (defun org-shifttab (&optional arg)
  11497. "Global visibility cycling or move to previous table field.
  11498. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11499. on context.
  11500. See the individual commands for more information."
  11501. (interactive "P")
  11502. (cond
  11503. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11504. ((integerp arg)
  11505. (message "Content view to level: %d" arg)
  11506. (org-content (prefix-numeric-value arg))
  11507. (setq org-cycle-global-status 'overview))
  11508. (t (call-interactively 'org-global-cycle))))
  11509. (defun org-shiftmetaleft ()
  11510. "Promote subtree or delete table column.
  11511. Calls `org-promote-subtree', `org-outdent-item',
  11512. or `org-table-delete-column', depending on context.
  11513. See the individual commands for more information."
  11514. (interactive)
  11515. (cond
  11516. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11517. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11518. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11519. (t (org-shiftcursor-error))))
  11520. (defun org-shiftmetaright ()
  11521. "Demote subtree or insert table column.
  11522. Calls `org-demote-subtree', `org-indent-item',
  11523. or `org-table-insert-column', depending on context.
  11524. See the individual commands for more information."
  11525. (interactive)
  11526. (cond
  11527. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11528. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11529. ((org-at-item-p) (call-interactively 'org-indent-item))
  11530. (t (org-shiftcursor-error))))
  11531. (defun org-shiftmetaup (&optional arg)
  11532. "Move subtree up or kill table row.
  11533. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11534. `org-move-item-up' depending on context. See the individual commands
  11535. for more information."
  11536. (interactive "P")
  11537. (cond
  11538. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11539. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11540. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11541. (t (org-shiftcursor-error))))
  11542. (defun org-shiftmetadown (&optional arg)
  11543. "Move subtree down or insert table row.
  11544. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11545. `org-move-item-down', depending on context. See the individual
  11546. commands for more information."
  11547. (interactive "P")
  11548. (cond
  11549. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11550. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11551. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11552. (t (org-shiftcursor-error))))
  11553. (defun org-metaleft (&optional arg)
  11554. "Promote heading or move table column to left.
  11555. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11556. With no specific context, calls the Emacs default `backward-word'.
  11557. See the individual commands for more information."
  11558. (interactive "P")
  11559. (cond
  11560. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11561. ((or (org-on-heading-p) (org-region-active-p))
  11562. (call-interactively 'org-do-promote))
  11563. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11564. (t (call-interactively 'backward-word))))
  11565. (defun org-metaright (&optional arg)
  11566. "Demote subtree or move table column to right.
  11567. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11568. With no specific context, calls the Emacs default `forward-word'.
  11569. See the individual commands for more information."
  11570. (interactive "P")
  11571. (cond
  11572. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11573. ((or (org-on-heading-p) (org-region-active-p))
  11574. (call-interactively 'org-do-demote))
  11575. ((org-at-item-p) (call-interactively 'org-indent-item))
  11576. (t (call-interactively 'forward-word))))
  11577. (defun org-metaup (&optional arg)
  11578. "Move subtree up or move table row up.
  11579. Calls `org-move-subtree-up' or `org-table-move-row' or
  11580. `org-move-item-up', depending on context. See the individual commands
  11581. for more information."
  11582. (interactive "P")
  11583. (cond
  11584. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11585. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11586. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11587. (t (transpose-lines 1) (beginning-of-line -1))))
  11588. (defun org-metadown (&optional arg)
  11589. "Move subtree down or move table row down.
  11590. Calls `org-move-subtree-down' or `org-table-move-row' or
  11591. `org-move-item-down', depending on context. See the individual
  11592. commands for more information."
  11593. (interactive "P")
  11594. (cond
  11595. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11596. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11597. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11598. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11599. (defun org-shiftup (&optional arg)
  11600. "Increase item in timestamp or increase priority of current headline.
  11601. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11602. depending on context. See the individual commands for more information."
  11603. (interactive "P")
  11604. (cond
  11605. ((org-at-timestamp-p t)
  11606. (call-interactively (if org-edit-timestamp-down-means-later
  11607. 'org-timestamp-down 'org-timestamp-up)))
  11608. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11609. ((org-at-item-p) (call-interactively 'org-previous-item))
  11610. ((org-clocktable-try-shift 'up arg))
  11611. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11612. (defun org-shiftdown (&optional arg)
  11613. "Decrease item in timestamp or decrease priority of current headline.
  11614. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11615. depending on context. See the individual commands for more information."
  11616. (interactive "P")
  11617. (cond
  11618. ((org-at-timestamp-p t)
  11619. (call-interactively (if org-edit-timestamp-down-means-later
  11620. 'org-timestamp-up 'org-timestamp-down)))
  11621. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11622. ((org-clocktable-try-shift 'down arg))
  11623. (t (call-interactively 'org-next-item))))
  11624. (defun org-shiftright (&optional arg)
  11625. "Cycle the thing at point or in the current line, depending on context.
  11626. Depending on context, this does one of the following:
  11627. - switch a timestamp at point one day into the future
  11628. - on a headline, switch to the next TODO keyword.
  11629. - on an item, switch entire list to the next bullet type
  11630. - on a property line, switch to the next allowed value
  11631. - on a clocktable definition line, move time block into the future"
  11632. (interactive "P")
  11633. (cond
  11634. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11635. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11636. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11637. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11638. ((org-clocktable-try-shift 'right arg))
  11639. (t (org-shiftcursor-error))))
  11640. (defun org-shiftleft (&optional arg)
  11641. "Cycle the thing at point or in the current line, depending on context.
  11642. Depending on context, this does one of the following:
  11643. - switch a timestamp at point one day into the past
  11644. - on a headline, switch to the previous TODO keyword.
  11645. - on an item, switch entire list to the previous bullet type
  11646. - on a property line, switch to the previous allowed value
  11647. - on a clocktable definition line, move time block into the past"
  11648. (interactive "P")
  11649. (cond
  11650. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11651. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11652. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11653. ((org-at-property-p)
  11654. (call-interactively 'org-property-previous-allowed-value))
  11655. ((org-clocktable-try-shift 'left arg))
  11656. (t (org-shiftcursor-error))))
  11657. (defun org-shiftcontrolright ()
  11658. "Switch to next TODO set."
  11659. (interactive)
  11660. (cond
  11661. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11662. (t (org-shiftcursor-error))))
  11663. (defun org-shiftcontrolleft ()
  11664. "Switch to previous TODO set."
  11665. (interactive)
  11666. (cond
  11667. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11668. (t (org-shiftcursor-error))))
  11669. (defun org-ctrl-c-ret ()
  11670. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11671. (interactive)
  11672. (cond
  11673. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11674. (t (call-interactively 'org-insert-heading))))
  11675. (defun org-copy-special ()
  11676. "Copy region in table or copy current subtree.
  11677. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11678. See the individual commands for more information."
  11679. (interactive)
  11680. (call-interactively
  11681. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11682. (defun org-cut-special ()
  11683. "Cut region in table or cut current subtree.
  11684. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11685. See the individual commands for more information."
  11686. (interactive)
  11687. (call-interactively
  11688. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11689. (defun org-paste-special (arg)
  11690. "Paste rectangular region into table, or past subtree relative to level.
  11691. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11692. See the individual commands for more information."
  11693. (interactive "P")
  11694. (if (org-at-table-p)
  11695. (org-table-paste-rectangle)
  11696. (org-paste-subtree arg)))
  11697. (defun org-edit-special ()
  11698. "Call a special editor for the stuff at point.
  11699. When at a table, call the formula editor with `org-table-edit-formulas'.
  11700. When at the first line of an src example, call `org-edit-src-code'.
  11701. When in an #+include line, visit the include file. Otherwise call
  11702. `ffap' to visit the file at point."
  11703. (interactive)
  11704. (cond
  11705. ((org-at-table-p)
  11706. (call-interactively 'org-table-edit-formulas))
  11707. ((save-excursion
  11708. (beginning-of-line 1)
  11709. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11710. (find-file (org-trim (match-string 1))))
  11711. ((org-edit-src-code))
  11712. ((org-edit-fixed-width-region))
  11713. (t (call-interactively 'ffap))))
  11714. (defun org-ctrl-c-ctrl-c (&optional arg)
  11715. "Set tags in headline, or update according to changed information at point.
  11716. This command does many different things, depending on context:
  11717. - If the cursor is in a headline, prompt for tags and insert them
  11718. into the current line, aligned to `org-tags-column'. When called
  11719. with prefix arg, realign all tags in the current buffer.
  11720. - If the cursor is in one of the special #+KEYWORD lines, this
  11721. triggers scanning the buffer for these lines and updating the
  11722. information.
  11723. - If the cursor is inside a table, realign the table. This command
  11724. works even if the automatic table editor has been turned off.
  11725. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11726. the entire table.
  11727. - If the cursor is a the beginning of a dynamic block, update it.
  11728. - If the cursor is inside a table created by the table.el package,
  11729. activate that table.
  11730. - If the current buffer is a remember buffer, close note and file
  11731. it. A prefix argument of 1 files to the default location
  11732. without further interaction. A prefix argument of 2 files to
  11733. the currently clocking task.
  11734. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11735. links in this buffer.
  11736. - If the cursor is on a numbered item in a plain list, renumber the
  11737. ordered list.
  11738. - If the cursor is on a checkbox, toggle it."
  11739. (interactive "P")
  11740. (let ((org-enable-table-editor t))
  11741. (cond
  11742. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11743. org-occur-highlights
  11744. org-latex-fragment-image-overlays)
  11745. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11746. (org-remove-occur-highlights)
  11747. (org-remove-latex-fragment-image-overlays)
  11748. (message "Temporary highlights/overlays removed from current buffer"))
  11749. ((and (local-variable-p 'org-finish-function (current-buffer))
  11750. (fboundp org-finish-function))
  11751. (funcall org-finish-function))
  11752. ((org-at-property-p)
  11753. (call-interactively 'org-property-action))
  11754. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11755. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11756. ((org-at-table.el-p)
  11757. (require 'table)
  11758. (beginning-of-line 1)
  11759. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11760. (call-interactively 'table-recognize-table))
  11761. ((org-at-table-p)
  11762. (org-table-maybe-eval-formula)
  11763. (if arg
  11764. (call-interactively 'org-table-recalculate)
  11765. (org-table-maybe-recalculate-line))
  11766. (call-interactively 'org-table-align))
  11767. ((org-at-item-checkbox-p)
  11768. (call-interactively 'org-toggle-checkbox))
  11769. ((org-at-item-p)
  11770. (call-interactively 'org-maybe-renumber-ordered-list))
  11771. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11772. ;; Dynamic block
  11773. (beginning-of-line 1)
  11774. (save-excursion (org-update-dblock)))
  11775. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11776. (cond
  11777. ((equal (match-string 1) "TBLFM")
  11778. ;; Recalculate the table before this line
  11779. (save-excursion
  11780. (beginning-of-line 1)
  11781. (skip-chars-backward " \r\n\t")
  11782. (if (org-at-table-p)
  11783. (org-call-with-arg 'org-table-recalculate t))))
  11784. (t
  11785. ; (org-set-regexps-and-options)
  11786. ; (org-restart-font-lock)
  11787. (let ((org-inhibit-startup t)) (org-mode-restart))
  11788. (message "Local setup has been refreshed"))))
  11789. (t (error "C-c C-c can do nothing useful at this location.")))))
  11790. (defun org-mode-restart ()
  11791. "Restart Org-mode, to scan again for special lines.
  11792. Also updates the keyword regular expressions."
  11793. (interactive)
  11794. (org-mode)
  11795. (message "Org-mode restarted"))
  11796. (defun org-kill-note-or-show-branches ()
  11797. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11798. (interactive)
  11799. (if (not org-finish-function)
  11800. (call-interactively 'show-branches)
  11801. (let ((org-note-abort t))
  11802. (funcall org-finish-function))))
  11803. (defun org-return (&optional indent)
  11804. "Goto next table row or insert a newline.
  11805. Calls `org-table-next-row' or `newline', depending on context.
  11806. See the individual commands for more information."
  11807. (interactive)
  11808. (cond
  11809. ((bobp) (if indent (newline-and-indent) (newline)))
  11810. ((and (org-at-heading-p)
  11811. (looking-at
  11812. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11813. (org-show-entry)
  11814. (end-of-line 1)
  11815. (newline))
  11816. ((org-at-table-p)
  11817. (org-table-justify-field-maybe)
  11818. (call-interactively 'org-table-next-row))
  11819. (t (if indent (newline-and-indent) (newline)))))
  11820. (defun org-return-indent ()
  11821. "Goto next table row or insert a newline and indent.
  11822. Calls `org-table-next-row' or `newline-and-indent', depending on
  11823. context. See the individual commands for more information."
  11824. (interactive)
  11825. (org-return t))
  11826. (defun org-ctrl-c-star ()
  11827. "Compute table, or change heading status of lines.
  11828. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11829. depending on context. This will also turn a plain list item or a normal
  11830. line into a subheading."
  11831. (interactive)
  11832. (cond
  11833. ((org-at-table-p)
  11834. (call-interactively 'org-table-recalculate))
  11835. ((org-region-active-p)
  11836. ;; Convert all lines in region to list items
  11837. (call-interactively 'org-toggle-region-headings))
  11838. ((org-on-heading-p)
  11839. (org-toggle-region-headings (point-at-bol)
  11840. (min (1+ (point-at-eol)) (point-max))))
  11841. ((org-at-item-p)
  11842. ;; Convert to heading
  11843. (let ((level (save-match-data
  11844. (save-excursion
  11845. (condition-case nil
  11846. (progn
  11847. (org-back-to-heading t)
  11848. (funcall outline-level))
  11849. (error 0))))))
  11850. (replace-match
  11851. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11852. (t (org-toggle-region-headings (point-at-bol)
  11853. (min (1+ (point-at-eol)) (point-max))))))
  11854. (defun org-ctrl-c-minus ()
  11855. "Insert separator line in table or modify bullet status of line.
  11856. Also turns a plain line or a region of lines into list items.
  11857. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11858. `org-cycle-list-bullet', depending on context."
  11859. (interactive)
  11860. (cond
  11861. ((org-at-table-p)
  11862. (call-interactively 'org-table-insert-hline))
  11863. ((org-on-heading-p)
  11864. ;; Convert to item
  11865. (save-excursion
  11866. (beginning-of-line 1)
  11867. (if (looking-at "\\*+ ")
  11868. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11869. ((org-region-active-p)
  11870. ;; Convert all lines in region to list items
  11871. (call-interactively 'org-toggle-region-items))
  11872. ((org-in-item-p)
  11873. (call-interactively 'org-cycle-list-bullet))
  11874. (t (org-toggle-region-items (point-at-bol)
  11875. (min (1+ (point-at-eol)) (point-max))))))
  11876. (defun org-toggle-region-items (beg end)
  11877. "Convert all lines in region to list items.
  11878. If the first line is already an item, convert all list items in the region
  11879. to normal lines."
  11880. (interactive "r")
  11881. (let (l2 l)
  11882. (save-excursion
  11883. (goto-char end)
  11884. (setq l2 (org-current-line))
  11885. (goto-char beg)
  11886. (beginning-of-line 1)
  11887. (setq l (1- (org-current-line)))
  11888. (if (org-at-item-p)
  11889. ;; We already have items, de-itemize
  11890. (while (< (setq l (1+ l)) l2)
  11891. (when (org-at-item-p)
  11892. (goto-char (match-beginning 2))
  11893. (delete-region (match-beginning 2) (match-end 2))
  11894. (and (looking-at "[ \t]+") (replace-match "")))
  11895. (beginning-of-line 2))
  11896. (while (< (setq l (1+ l)) l2)
  11897. (unless (org-at-item-p)
  11898. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11899. (replace-match "\\1- \\2")))
  11900. (beginning-of-line 2))))))
  11901. (defun org-toggle-region-headings (beg end)
  11902. "Convert all lines in region to list items.
  11903. If the first line is already an item, convert all list items in the region
  11904. to normal lines."
  11905. (interactive "r")
  11906. (let (l2 l)
  11907. (save-excursion
  11908. (goto-char end)
  11909. (setq l2 (org-current-line))
  11910. (goto-char beg)
  11911. (beginning-of-line 1)
  11912. (setq l (1- (org-current-line)))
  11913. (if (org-on-heading-p)
  11914. ;; We already have headlines, de-star them
  11915. (while (< (setq l (1+ l)) l2)
  11916. (when (org-on-heading-p t)
  11917. (and (looking-at outline-regexp) (replace-match "")))
  11918. (beginning-of-line 2))
  11919. (let* ((stars (save-excursion
  11920. (re-search-backward org-complex-heading-regexp nil t)
  11921. (or (match-string 1) "*")))
  11922. (add-stars (if org-odd-levels-only "**" "*"))
  11923. (rpl (concat stars add-stars " \\2")))
  11924. (while (< (setq l (1+ l)) l2)
  11925. (unless (org-on-heading-p)
  11926. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11927. (replace-match rpl)))
  11928. (beginning-of-line 2)))))))
  11929. (defun org-meta-return (&optional arg)
  11930. "Insert a new heading or wrap a region in a table.
  11931. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11932. See the individual commands for more information."
  11933. (interactive "P")
  11934. (cond
  11935. ((org-at-table-p)
  11936. (call-interactively 'org-table-wrap-region))
  11937. (t (call-interactively 'org-insert-heading))))
  11938. ;;; Menu entries
  11939. ;; Define the Org-mode menus
  11940. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11941. '("Tbl"
  11942. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11943. ["Next Field" org-cycle (org-at-table-p)]
  11944. ["Previous Field" org-shifttab (org-at-table-p)]
  11945. ["Next Row" org-return (org-at-table-p)]
  11946. "--"
  11947. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11948. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11949. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11950. "--"
  11951. ("Column"
  11952. ["Move Column Left" org-metaleft (org-at-table-p)]
  11953. ["Move Column Right" org-metaright (org-at-table-p)]
  11954. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11955. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11956. ("Row"
  11957. ["Move Row Up" org-metaup (org-at-table-p)]
  11958. ["Move Row Down" org-metadown (org-at-table-p)]
  11959. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11960. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11961. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11962. "--"
  11963. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11964. ("Rectangle"
  11965. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11966. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11967. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11968. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11969. "--"
  11970. ("Calculate"
  11971. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11972. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11973. ["Edit Formulas" org-edit-special (org-at-table-p)]
  11974. "--"
  11975. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11976. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11977. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11978. "--"
  11979. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11980. "--"
  11981. ["Sum Column/Rectangle" org-table-sum
  11982. (or (org-at-table-p) (org-region-active-p))]
  11983. ["Which Column?" org-table-current-column (org-at-table-p)])
  11984. ["Debug Formulas"
  11985. org-table-toggle-formula-debugger
  11986. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11987. ["Show Col/Row Numbers"
  11988. org-table-toggle-coordinate-overlays
  11989. :style toggle
  11990. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11991. "--"
  11992. ["Create" org-table-create (and (not (org-at-table-p))
  11993. org-enable-table-editor)]
  11994. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11995. ["Import from File" org-table-import (not (org-at-table-p))]
  11996. ["Export to File" org-table-export (org-at-table-p)]
  11997. "--"
  11998. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11999. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12000. '("Org"
  12001. ("Show/Hide"
  12002. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12003. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12004. ["Sparse Tree..." org-sparse-tree t]
  12005. ["Reveal Context" org-reveal t]
  12006. ["Show All" show-all t]
  12007. "--"
  12008. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12009. "--"
  12010. ["New Heading" org-insert-heading t]
  12011. ("Navigate Headings"
  12012. ["Up" outline-up-heading t]
  12013. ["Next" outline-next-visible-heading t]
  12014. ["Previous" outline-previous-visible-heading t]
  12015. ["Next Same Level" outline-forward-same-level t]
  12016. ["Previous Same Level" outline-backward-same-level t]
  12017. "--"
  12018. ["Jump" org-goto t])
  12019. ("Edit Structure"
  12020. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12021. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12022. "--"
  12023. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12024. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12025. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12026. "--"
  12027. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12028. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12029. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12030. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12031. "--"
  12032. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12033. "--"
  12034. ["Convert to odd levels" org-convert-to-odd-levels t]
  12035. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12036. ("Editing"
  12037. ["Emphasis..." org-emphasize t]
  12038. ["Edit Source Example" org-edit-special t])
  12039. ("Archive"
  12040. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12041. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12042. ; :active t :keys "C-u C-c C-x C-a"]
  12043. ["Sparse trees open ARCHIVE trees"
  12044. (setq org-sparse-tree-open-archived-trees
  12045. (not org-sparse-tree-open-archived-trees))
  12046. :style toggle :selected org-sparse-tree-open-archived-trees]
  12047. ["Cycling opens ARCHIVE trees"
  12048. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12049. :style toggle :selected org-cycle-open-archived-trees]
  12050. "--"
  12051. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12052. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12053. ; ["Check and Move Children" (org-archive-subtree '(4))
  12054. ; :active t :keys "C-u C-c C-x C-s"]
  12055. )
  12056. "--"
  12057. ("TODO Lists"
  12058. ["TODO/DONE/-" org-todo t]
  12059. ("Select keyword"
  12060. ["Next keyword" org-shiftright (org-on-heading-p)]
  12061. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12062. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12063. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12064. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12065. ["Show TODO Tree" org-show-todo-tree t]
  12066. ["Global TODO list" org-todo-list t]
  12067. "--"
  12068. ["Set Priority" org-priority t]
  12069. ["Priority Up" org-shiftup t]
  12070. ["Priority Down" org-shiftdown t])
  12071. ("TAGS and Properties"
  12072. ["Set Tags" 'org-set-tags-command t]
  12073. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12074. "--"
  12075. ["Set property" 'org-set-property t]
  12076. ["Column view of properties" org-columns t]
  12077. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12078. ("Dates and Scheduling"
  12079. ["Timestamp" org-time-stamp t]
  12080. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12081. ("Change Date"
  12082. ["1 Day Later" org-shiftright t]
  12083. ["1 Day Earlier" org-shiftleft t]
  12084. ["1 ... Later" org-shiftup t]
  12085. ["1 ... Earlier" org-shiftdown t])
  12086. ["Compute Time Range" org-evaluate-time-range t]
  12087. ["Schedule Item" org-schedule t]
  12088. ["Deadline" org-deadline t]
  12089. "--"
  12090. ["Custom time format" org-toggle-time-stamp-overlays
  12091. :style radio :selected org-display-custom-times]
  12092. "--"
  12093. ["Goto Calendar" org-goto-calendar t]
  12094. ["Date from Calendar" org-date-from-calendar t])
  12095. ("Logging work"
  12096. ["Clock in" org-clock-in t]
  12097. ["Clock out" org-clock-out t]
  12098. ["Clock cancel" org-clock-cancel t]
  12099. ["Goto running clock" org-clock-goto t]
  12100. ["Display times" org-clock-display t]
  12101. ["Create clock table" org-clock-report t]
  12102. "--"
  12103. ["Record DONE time"
  12104. (progn (setq org-log-done (not org-log-done))
  12105. (message "Switching to %s will %s record a timestamp"
  12106. (car org-done-keywords)
  12107. (if org-log-done "automatically" "not")))
  12108. :style toggle :selected org-log-done])
  12109. "--"
  12110. ["Agenda Command..." org-agenda t]
  12111. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12112. ("File List for Agenda")
  12113. ("Special views current file"
  12114. ["TODO Tree" org-show-todo-tree t]
  12115. ["Check Deadlines" org-check-deadlines t]
  12116. ["Timeline" org-timeline t]
  12117. ["Tags Tree" org-tags-sparse-tree t])
  12118. "--"
  12119. ("Hyperlinks"
  12120. ["Store Link (Global)" org-store-link t]
  12121. ["Insert Link" org-insert-link t]
  12122. ["Follow Link" org-open-at-point t]
  12123. "--"
  12124. ["Next link" org-next-link t]
  12125. ["Previous link" org-previous-link t]
  12126. "--"
  12127. ["Descriptive Links"
  12128. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12129. :style radio
  12130. :selected (member '(org-link) buffer-invisibility-spec)]
  12131. ["Literal Links"
  12132. (progn
  12133. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12134. :style radio
  12135. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12136. "--"
  12137. ["Export/Publish..." org-export t]
  12138. ("LaTeX"
  12139. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12140. :selected org-cdlatex-mode]
  12141. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12142. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12143. ["Modify math symbol" org-cdlatex-math-modify
  12144. (org-inside-LaTeX-fragment-p)]
  12145. ["Export LaTeX fragments as images"
  12146. (if (featurep 'org-exp)
  12147. (setq org-export-with-LaTeX-fragments
  12148. (not org-export-with-LaTeX-fragments))
  12149. (require 'org-exp))
  12150. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12151. org-export-with-LaTeX-fragments)])
  12152. "--"
  12153. ("Documentation"
  12154. ["Show Version" org-version t]
  12155. ["Info Documentation" org-info t])
  12156. ("Customize"
  12157. ["Browse Org Group" org-customize t]
  12158. "--"
  12159. ["Expand This Menu" org-create-customize-menu
  12160. (fboundp 'customize-menu-create)])
  12161. "--"
  12162. ["Refresh setup" org-mode-restart t]
  12163. ))
  12164. (defun org-info (&optional node)
  12165. "Read documentation for Org-mode in the info system.
  12166. With optional NODE, go directly to that node."
  12167. (interactive)
  12168. (info (format "(org)%s" (or node ""))))
  12169. (defun org-install-agenda-files-menu ()
  12170. (let ((bl (buffer-list)))
  12171. (save-excursion
  12172. (while bl
  12173. (set-buffer (pop bl))
  12174. (if (org-mode-p) (setq bl nil)))
  12175. (when (org-mode-p)
  12176. (easy-menu-change
  12177. '("Org") "File List for Agenda"
  12178. (append
  12179. (list
  12180. ["Edit File List" (org-edit-agenda-file-list) t]
  12181. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12182. ["Remove Current File from List" org-remove-file t]
  12183. ["Cycle through agenda files" org-cycle-agenda-files t]
  12184. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12185. "--")
  12186. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12187. ;;;; Documentation
  12188. ;;;###autoload
  12189. (defun org-require-autoloaded-modules ()
  12190. (interactive)
  12191. (mapc 'require
  12192. '(org-agenda org-archive org-clock org-colview
  12193. org-exp org-id org-export-latex org-publish
  12194. org-remember org-table)))
  12195. ;;;###autoload
  12196. (defun org-customize ()
  12197. "Call the customize function with org as argument."
  12198. (interactive)
  12199. (org-load-modules-maybe)
  12200. (org-require-autoloaded-modules)
  12201. (customize-browse 'org))
  12202. (defun org-create-customize-menu ()
  12203. "Create a full customization menu for Org-mode, insert it into the menu."
  12204. (interactive)
  12205. (org-load-modules-maybe)
  12206. (org-require-autoloaded-modules)
  12207. (if (fboundp 'customize-menu-create)
  12208. (progn
  12209. (easy-menu-change
  12210. '("Org") "Customize"
  12211. `(["Browse Org group" org-customize t]
  12212. "--"
  12213. ,(customize-menu-create 'org)
  12214. ["Set" Custom-set t]
  12215. ["Save" Custom-save t]
  12216. ["Reset to Current" Custom-reset-current t]
  12217. ["Reset to Saved" Custom-reset-saved t]
  12218. ["Reset to Standard Settings" Custom-reset-standard t]))
  12219. (message "\"Org\"-menu now contains full customization menu"))
  12220. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12221. ;;;; Miscellaneous stuff
  12222. ;;; Generally useful functions
  12223. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12224. "Display the given MESSAGE as a warning."
  12225. (if (fboundp 'display-warning)
  12226. (display-warning 'org message
  12227. (if (featurep 'xemacs)
  12228. 'warning
  12229. :warning))
  12230. (let ((buf (get-buffer-create "*Org warnings*")))
  12231. (with-current-buffer buf
  12232. (goto-char (point-max))
  12233. (insert "Warning (Org): " message)
  12234. (unless (bolp)
  12235. (newline)))
  12236. (display-buffer buf)
  12237. (sit-for 0))))
  12238. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12239. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12240. (if (and marker (marker-buffer marker)
  12241. (buffer-live-p (marker-buffer marker)))
  12242. (progn
  12243. (switch-to-buffer (marker-buffer marker))
  12244. (if (or (> marker (point-max)) (< marker (point-min)))
  12245. (widen))
  12246. (goto-char marker))
  12247. (if bookmark
  12248. (bookmark-jump bookmark)
  12249. (error "Cannot find location"))))
  12250. (defun org-quote-csv-field (s)
  12251. "Quote field for inclusion in CSV material."
  12252. (if (string-match "[\",]" s)
  12253. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12254. s))
  12255. (defun org-plist-delete (plist property)
  12256. "Delete PROPERTY from PLIST.
  12257. This is in contrast to merely setting it to 0."
  12258. (let (p)
  12259. (while plist
  12260. (if (not (eq property (car plist)))
  12261. (setq p (plist-put p (car plist) (nth 1 plist))))
  12262. (setq plist (cddr plist)))
  12263. p))
  12264. (defun org-force-self-insert (N)
  12265. "Needed to enforce self-insert under remapping."
  12266. (interactive "p")
  12267. (self-insert-command N))
  12268. (defun org-string-width (s)
  12269. "Compute width of string, ignoring invisible characters.
  12270. This ignores character with invisibility property `org-link', and also
  12271. characters with property `org-cwidth', because these will become invisible
  12272. upon the next fontification round."
  12273. (let (b l)
  12274. (when (or (eq t buffer-invisibility-spec)
  12275. (assq 'org-link buffer-invisibility-spec))
  12276. (while (setq b (text-property-any 0 (length s)
  12277. 'invisible 'org-link s))
  12278. (setq s (concat (substring s 0 b)
  12279. (substring s (or (next-single-property-change
  12280. b 'invisible s) (length s)))))))
  12281. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12282. (setq s (concat (substring s 0 b)
  12283. (substring s (or (next-single-property-change
  12284. b 'org-cwidth s) (length s))))))
  12285. (setq l (string-width s) b -1)
  12286. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12287. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12288. l))
  12289. (defun org-get-indentation (&optional line)
  12290. "Get the indentation of the current line, interpreting tabs.
  12291. When LINE is given, assume it represents a line and compute its indentation."
  12292. (if line
  12293. (if (string-match "^ *" (org-remove-tabs line))
  12294. (match-end 0))
  12295. (save-excursion
  12296. (beginning-of-line 1)
  12297. (skip-chars-forward " \t")
  12298. (current-column))))
  12299. (defun org-remove-tabs (s &optional width)
  12300. "Replace tabulators in S with spaces.
  12301. Assumes that s is a single line, starting in column 0."
  12302. (setq width (or width tab-width))
  12303. (while (string-match "\t" s)
  12304. (setq s (replace-match
  12305. (make-string
  12306. (- (* width (/ (+ (match-beginning 0) width) width))
  12307. (match-beginning 0)) ?\ )
  12308. t t s)))
  12309. s)
  12310. (defun org-fix-indentation (line ind)
  12311. "Fix indentation in LINE.
  12312. IND is a cons cell with target and minimum indentation.
  12313. If the current indenation in LINE is smaller than the minimum,
  12314. leave it alone. If it is larger than ind, set it to the target."
  12315. (let* ((l (org-remove-tabs line))
  12316. (i (org-get-indentation l))
  12317. (i1 (car ind)) (i2 (cdr ind)))
  12318. (if (>= i i2) (setq l (substring line i2)))
  12319. (if (> i1 0)
  12320. (concat (make-string i1 ?\ ) l)
  12321. l)))
  12322. (defun org-base-buffer (buffer)
  12323. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12324. (if (not buffer)
  12325. buffer
  12326. (or (buffer-base-buffer buffer)
  12327. buffer)))
  12328. (defun org-trim (s)
  12329. "Remove whitespace at beginning and end of string."
  12330. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12331. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12332. s)
  12333. (defun org-wrap (string &optional width lines)
  12334. "Wrap string to either a number of lines, or a width in characters.
  12335. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12336. that costs. If there is a word longer than WIDTH, the text is actually
  12337. wrapped to the length of that word.
  12338. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12339. many lines, whatever width that takes.
  12340. The return value is a list of lines, without newlines at the end."
  12341. (let* ((words (org-split-string string "[ \t\n]+"))
  12342. (maxword (apply 'max (mapcar 'org-string-width words)))
  12343. w ll)
  12344. (cond (width
  12345. (org-do-wrap words (max maxword width)))
  12346. (lines
  12347. (setq w maxword)
  12348. (setq ll (org-do-wrap words maxword))
  12349. (if (<= (length ll) lines)
  12350. ll
  12351. (setq ll words)
  12352. (while (> (length ll) lines)
  12353. (setq w (1+ w))
  12354. (setq ll (org-do-wrap words w)))
  12355. ll))
  12356. (t (error "Cannot wrap this")))))
  12357. (defun org-do-wrap (words width)
  12358. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12359. (let (lines line)
  12360. (while words
  12361. (setq line (pop words))
  12362. (while (and words (< (+ (length line) (length (car words))) width))
  12363. (setq line (concat line " " (pop words))))
  12364. (setq lines (push line lines)))
  12365. (nreverse lines)))
  12366. (defun org-split-string (string &optional separators)
  12367. "Splits STRING into substrings at SEPARATORS.
  12368. No empty strings are returned if there are matches at the beginning
  12369. and end of string."
  12370. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12371. (start 0)
  12372. notfirst
  12373. (list nil))
  12374. (while (and (string-match rexp string
  12375. (if (and notfirst
  12376. (= start (match-beginning 0))
  12377. (< start (length string)))
  12378. (1+ start) start))
  12379. (< (match-beginning 0) (length string)))
  12380. (setq notfirst t)
  12381. (or (eq (match-beginning 0) 0)
  12382. (and (eq (match-beginning 0) (match-end 0))
  12383. (eq (match-beginning 0) start))
  12384. (setq list
  12385. (cons (substring string start (match-beginning 0))
  12386. list)))
  12387. (setq start (match-end 0)))
  12388. (or (eq start (length string))
  12389. (setq list
  12390. (cons (substring string start)
  12391. list)))
  12392. (nreverse list)))
  12393. (defun org-context ()
  12394. "Return a list of contexts of the current cursor position.
  12395. If several contexts apply, all are returned.
  12396. Each context entry is a list with a symbol naming the context, and
  12397. two positions indicating start and end of the context. Possible
  12398. contexts are:
  12399. :headline anywhere in a headline
  12400. :headline-stars on the leading stars in a headline
  12401. :todo-keyword on a TODO keyword (including DONE) in a headline
  12402. :tags on the TAGS in a headline
  12403. :priority on the priority cookie in a headline
  12404. :item on the first line of a plain list item
  12405. :item-bullet on the bullet/number of a plain list item
  12406. :checkbox on the checkbox in a plain list item
  12407. :table in an org-mode table
  12408. :table-special on a special filed in a table
  12409. :table-table in a table.el table
  12410. :link on a hyperlink
  12411. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12412. :target on a <<target>>
  12413. :radio-target on a <<<radio-target>>>
  12414. :latex-fragment on a LaTeX fragment
  12415. :latex-preview on a LaTeX fragment with overlayed preview image
  12416. This function expects the position to be visible because it uses font-lock
  12417. faces as a help to recognize the following contexts: :table-special, :link,
  12418. and :keyword."
  12419. (let* ((f (get-text-property (point) 'face))
  12420. (faces (if (listp f) f (list f)))
  12421. (p (point)) clist o)
  12422. ;; First the large context
  12423. (cond
  12424. ((org-on-heading-p t)
  12425. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12426. (when (progn
  12427. (beginning-of-line 1)
  12428. (looking-at org-todo-line-tags-regexp))
  12429. (push (org-point-in-group p 1 :headline-stars) clist)
  12430. (push (org-point-in-group p 2 :todo-keyword) clist)
  12431. (push (org-point-in-group p 4 :tags) clist))
  12432. (goto-char p)
  12433. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12434. (if (looking-at "\\[#[A-Z0-9]\\]")
  12435. (push (org-point-in-group p 0 :priority) clist)))
  12436. ((org-at-item-p)
  12437. (push (org-point-in-group p 2 :item-bullet) clist)
  12438. (push (list :item (point-at-bol)
  12439. (save-excursion (org-end-of-item) (point)))
  12440. clist)
  12441. (and (org-at-item-checkbox-p)
  12442. (push (org-point-in-group p 0 :checkbox) clist)))
  12443. ((org-at-table-p)
  12444. (push (list :table (org-table-begin) (org-table-end)) clist)
  12445. (if (memq 'org-formula faces)
  12446. (push (list :table-special
  12447. (previous-single-property-change p 'face)
  12448. (next-single-property-change p 'face)) clist)))
  12449. ((org-at-table-p 'any)
  12450. (push (list :table-table) clist)))
  12451. (goto-char p)
  12452. ;; Now the small context
  12453. (cond
  12454. ((org-at-timestamp-p)
  12455. (push (org-point-in-group p 0 :timestamp) clist))
  12456. ((memq 'org-link faces)
  12457. (push (list :link
  12458. (previous-single-property-change p 'face)
  12459. (next-single-property-change p 'face)) clist))
  12460. ((memq 'org-special-keyword faces)
  12461. (push (list :keyword
  12462. (previous-single-property-change p 'face)
  12463. (next-single-property-change p 'face)) clist))
  12464. ((org-on-target-p)
  12465. (push (org-point-in-group p 0 :target) clist)
  12466. (goto-char (1- (match-beginning 0)))
  12467. (if (looking-at org-radio-target-regexp)
  12468. (push (org-point-in-group p 0 :radio-target) clist))
  12469. (goto-char p))
  12470. ((setq o (car (delq nil
  12471. (mapcar
  12472. (lambda (x)
  12473. (if (memq x org-latex-fragment-image-overlays) x))
  12474. (org-overlays-at (point))))))
  12475. (push (list :latex-fragment
  12476. (org-overlay-start o) (org-overlay-end o)) clist)
  12477. (push (list :latex-preview
  12478. (org-overlay-start o) (org-overlay-end o)) clist))
  12479. ((org-inside-LaTeX-fragment-p)
  12480. ;; FIXME: positions wrong.
  12481. (push (list :latex-fragment (point) (point)) clist)))
  12482. (setq clist (nreverse (delq nil clist)))
  12483. clist))
  12484. ;; FIXME: Compare with at-regexp-p Do we need both?
  12485. (defun org-in-regexp (re &optional nlines visually)
  12486. "Check if point is inside a match of regexp.
  12487. Normally only the current line is checked, but you can include NLINES extra
  12488. lines both before and after point into the search.
  12489. If VISUALLY is set, require that the cursor is not after the match but
  12490. really on, so that the block visually is on the match."
  12491. (catch 'exit
  12492. (let ((pos (point))
  12493. (eol (point-at-eol (+ 1 (or nlines 0))))
  12494. (inc (if visually 1 0)))
  12495. (save-excursion
  12496. (beginning-of-line (- 1 (or nlines 0)))
  12497. (while (re-search-forward re eol t)
  12498. (if (and (<= (match-beginning 0) pos)
  12499. (>= (+ inc (match-end 0)) pos))
  12500. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12501. (defun org-at-regexp-p (regexp)
  12502. "Is point inside a match of REGEXP in the current line?"
  12503. (catch 'exit
  12504. (save-excursion
  12505. (let ((pos (point)) (end (point-at-eol)))
  12506. (beginning-of-line 1)
  12507. (while (re-search-forward regexp end t)
  12508. (if (and (<= (match-beginning 0) pos)
  12509. (>= (match-end 0) pos))
  12510. (throw 'exit t)))
  12511. nil))))
  12512. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12513. "Call `multi-occur' with buffers for all agenda files."
  12514. (interactive "sOrg-files matching: \np")
  12515. (let* ((files (org-agenda-files))
  12516. (tnames (mapcar 'file-truename files))
  12517. (extra org-agenda-text-search-extra-files)
  12518. f)
  12519. (when (eq (car extra) 'agenda-archives)
  12520. (setq extra (cdr extra))
  12521. (setq files (org-add-archive-files files)))
  12522. (while (setq f (pop extra))
  12523. (unless (member (file-truename f) tnames)
  12524. (add-to-list 'files f 'append)
  12525. (add-to-list 'tnames (file-truename f) 'append)))
  12526. (multi-occur
  12527. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12528. regexp)))
  12529. (if (boundp 'occur-mode-find-occurrence-hook)
  12530. ;; Emacs 23
  12531. (add-hook 'occur-mode-find-occurrence-hook
  12532. (lambda ()
  12533. (when (org-mode-p)
  12534. (org-reveal))))
  12535. ;; Emacs 22
  12536. (defadvice occur-mode-goto-occurrence
  12537. (after org-occur-reveal activate)
  12538. (and (org-mode-p) (org-reveal)))
  12539. (defadvice occur-mode-goto-occurrence-other-window
  12540. (after org-occur-reveal activate)
  12541. (and (org-mode-p) (org-reveal)))
  12542. (defadvice occur-mode-display-occurrence
  12543. (after org-occur-reveal activate)
  12544. (when (org-mode-p)
  12545. (let ((pos (occur-mode-find-occurrence)))
  12546. (with-current-buffer (marker-buffer pos)
  12547. (save-excursion
  12548. (goto-char pos)
  12549. (org-reveal)))))))
  12550. (defun org-uniquify (list)
  12551. "Remove duplicate elements from LIST."
  12552. (let (res)
  12553. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12554. res))
  12555. (defun org-delete-all (elts list)
  12556. "Remove all elements in ELTS from LIST."
  12557. (while elts
  12558. (setq list (delete (pop elts) list)))
  12559. list)
  12560. (defun org-back-over-empty-lines ()
  12561. "Move backwards over witespace, to the beginning of the first empty line.
  12562. Returns the number of empty lines passed."
  12563. (let ((pos (point)))
  12564. (skip-chars-backward " \t\n\r")
  12565. (beginning-of-line 2)
  12566. (goto-char (min (point) pos))
  12567. (count-lines (point) pos)))
  12568. (defun org-skip-whitespace ()
  12569. (skip-chars-forward " \t\n\r"))
  12570. (defun org-point-in-group (point group &optional context)
  12571. "Check if POINT is in match-group GROUP.
  12572. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12573. match. If the match group does ot exist or point is not inside it,
  12574. return nil."
  12575. (and (match-beginning group)
  12576. (>= point (match-beginning group))
  12577. (<= point (match-end group))
  12578. (if context
  12579. (list context (match-beginning group) (match-end group))
  12580. t)))
  12581. (defun org-switch-to-buffer-other-window (&rest args)
  12582. "Switch to buffer in a second window on the current frame.
  12583. In particular, do not allow pop-up frames."
  12584. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12585. special-display-function)
  12586. (apply 'switch-to-buffer-other-window args)))
  12587. (defun org-combine-plists (&rest plists)
  12588. "Create a single property list from all plists in PLISTS.
  12589. The process starts by copying the first list, and then setting properties
  12590. from the other lists. Settings in the last list are the most significant
  12591. ones and overrule settings in the other lists."
  12592. (let ((rtn (copy-sequence (pop plists)))
  12593. p v ls)
  12594. (while plists
  12595. (setq ls (pop plists))
  12596. (while ls
  12597. (setq p (pop ls) v (pop ls))
  12598. (setq rtn (plist-put rtn p v))))
  12599. rtn))
  12600. (defun org-move-line-down (arg)
  12601. "Move the current line down. With prefix argument, move it past ARG lines."
  12602. (interactive "p")
  12603. (let ((col (current-column))
  12604. beg end pos)
  12605. (beginning-of-line 1) (setq beg (point))
  12606. (beginning-of-line 2) (setq end (point))
  12607. (beginning-of-line (+ 1 arg))
  12608. (setq pos (move-marker (make-marker) (point)))
  12609. (insert (delete-and-extract-region beg end))
  12610. (goto-char pos)
  12611. (org-move-to-column col)))
  12612. (defun org-move-line-up (arg)
  12613. "Move the current line up. With prefix argument, move it past ARG lines."
  12614. (interactive "p")
  12615. (let ((col (current-column))
  12616. beg end pos)
  12617. (beginning-of-line 1) (setq beg (point))
  12618. (beginning-of-line 2) (setq end (point))
  12619. (beginning-of-line (- arg))
  12620. (setq pos (move-marker (make-marker) (point)))
  12621. (insert (delete-and-extract-region beg end))
  12622. (goto-char pos)
  12623. (org-move-to-column col)))
  12624. (defun org-replace-escapes (string table)
  12625. "Replace %-escapes in STRING with values in TABLE.
  12626. TABLE is an association list with keys like \"%a\" and string values.
  12627. The sequences in STRING may contain normal field width and padding information,
  12628. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12629. so values can contain further %-escapes if they are define later in TABLE."
  12630. (let ((case-fold-search nil)
  12631. e re rpl)
  12632. (while (setq e (pop table))
  12633. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12634. (while (string-match re string)
  12635. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12636. (cdr e)))
  12637. (setq string (replace-match rpl t t string))))
  12638. string))
  12639. (defun org-sublist (list start end)
  12640. "Return a section of LIST, from START to END.
  12641. Counting starts at 1."
  12642. (let (rtn (c start))
  12643. (setq list (nthcdr (1- start) list))
  12644. (while (and list (<= c end))
  12645. (push (pop list) rtn)
  12646. (setq c (1+ c)))
  12647. (nreverse rtn)))
  12648. (defun org-find-base-buffer-visiting (file)
  12649. "Like `find-buffer-visiting' but alway return the base buffer and
  12650. not an indirect buffer."
  12651. (let ((buf (find-buffer-visiting file)))
  12652. (if buf
  12653. (or (buffer-base-buffer buf) buf)
  12654. nil)))
  12655. (defun org-image-file-name-regexp ()
  12656. "Return regexp matching the file names of images."
  12657. (if (fboundp 'image-file-name-regexp)
  12658. (image-file-name-regexp)
  12659. (let ((image-file-name-extensions
  12660. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12661. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12662. (concat "\\."
  12663. (regexp-opt (nconc (mapcar 'upcase
  12664. image-file-name-extensions)
  12665. image-file-name-extensions)
  12666. t)
  12667. "\\'"))))
  12668. (defun org-file-image-p (file)
  12669. "Return non-nil if FILE is an image."
  12670. (save-match-data
  12671. (string-match (org-image-file-name-regexp) file)))
  12672. (defun org-get-cursor-date ()
  12673. "Return the date at cursor in as a time.
  12674. This works in the calendar and in the agenda, anywhere else it just
  12675. returns the current time."
  12676. (let (date day defd)
  12677. (cond
  12678. ((eq major-mode 'calendar-mode)
  12679. (setq date (calendar-cursor-to-date)
  12680. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12681. ((eq major-mode 'org-agenda-mode)
  12682. (setq day (get-text-property (point) 'day))
  12683. (if day
  12684. (setq date (calendar-gregorian-from-absolute day)
  12685. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12686. (nth 2 date))))))
  12687. (or defd (current-time))))
  12688. (defvar org-agenda-action-marker (make-marker)
  12689. "Marker pointing to the entry for the next agenda action.")
  12690. (defun org-mark-entry-for-agenda-action ()
  12691. "Mark the current entry as target of an agenda action.
  12692. Agenda actions are actions executed from the agenda with the key `k',
  12693. which make use of the date at the cursor."
  12694. (interactive)
  12695. (move-marker org-agenda-action-marker
  12696. (save-excursion (org-back-to-heading t) (point))
  12697. (current-buffer))
  12698. (message
  12699. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12700. ;;; Paragraph filling stuff.
  12701. ;; We want this to be just right, so use the full arsenal.
  12702. (defun org-indent-line-function ()
  12703. "Indent line like previous, but further if previous was headline or item."
  12704. (interactive)
  12705. (let* ((pos (point))
  12706. (itemp (org-at-item-p))
  12707. column bpos bcol tpos tcol bullet btype bullet-type)
  12708. ;; Find the previous relevant line
  12709. (beginning-of-line 1)
  12710. (cond
  12711. ((looking-at "#") (setq column 0))
  12712. ((looking-at "\\*+ ") (setq column 0))
  12713. (t
  12714. (beginning-of-line 0)
  12715. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12716. (beginning-of-line 0))
  12717. (cond
  12718. ((looking-at "\\*+[ \t]+")
  12719. (if (not org-adapt-indentation)
  12720. (setq column 0)
  12721. (goto-char (match-end 0))
  12722. (setq column (current-column))))
  12723. ((org-in-item-p)
  12724. (org-beginning-of-item)
  12725. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12726. (setq bpos (match-beginning 1) tpos (match-end 0)
  12727. bcol (progn (goto-char bpos) (current-column))
  12728. tcol (progn (goto-char tpos) (current-column))
  12729. bullet (match-string 1)
  12730. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12731. (if (> tcol (+ bcol org-description-max-indent))
  12732. (setq tcol (+ bcol 5)))
  12733. (if (not itemp)
  12734. (setq column tcol)
  12735. (goto-char pos)
  12736. (beginning-of-line 1)
  12737. (if (looking-at "\\S-")
  12738. (progn
  12739. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12740. (setq bullet (match-string 1)
  12741. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12742. (setq column (if (equal btype bullet-type) bcol tcol)))
  12743. (setq column (org-get-indentation)))))
  12744. (t (setq column (org-get-indentation))))))
  12745. (goto-char pos)
  12746. (if (<= (current-column) (current-indentation))
  12747. (org-indent-line-to column)
  12748. (save-excursion (org-indent-line-to column)))
  12749. (setq column (current-column))
  12750. (beginning-of-line 1)
  12751. (if (looking-at
  12752. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12753. (replace-match (concat "\\1" (format org-property-format
  12754. (match-string 2) (match-string 3)))
  12755. t nil))
  12756. (org-move-to-column column)))
  12757. (defun org-set-autofill-regexps ()
  12758. (interactive)
  12759. ;; In the paragraph separator we include headlines, because filling
  12760. ;; text in a line directly attached to a headline would otherwise
  12761. ;; fill the headline as well.
  12762. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12763. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12764. ;; The paragraph starter includes hand-formatted lists.
  12765. (org-set-local 'paragraph-start
  12766. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12767. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12768. ;; But only if the user has not turned off tables or fixed-width regions
  12769. (org-set-local
  12770. 'auto-fill-inhibit-regexp
  12771. (concat "\\*+ \\|#\\+"
  12772. "\\|[ \t]*" org-keyword-time-regexp
  12773. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12774. (concat
  12775. "\\|[ \t]*["
  12776. (if org-enable-table-editor "|" "")
  12777. (if org-enable-fixed-width-editor ":" "")
  12778. "]"))))
  12779. ;; We use our own fill-paragraph function, to make sure that tables
  12780. ;; and fixed-width regions are not wrapped. That function will pass
  12781. ;; through to `fill-paragraph' when appropriate.
  12782. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12783. ; Adaptive filling: To get full control, first make sure that
  12784. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12785. (org-set-local 'adaptive-fill-regexp "\000")
  12786. (org-set-local 'adaptive-fill-function
  12787. 'org-adaptive-fill-function)
  12788. (org-set-local
  12789. 'align-mode-rules-list
  12790. '((org-in-buffer-settings
  12791. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12792. (modes . '(org-mode))))))
  12793. (defun org-fill-paragraph (&optional justify)
  12794. "Re-align a table, pass through to fill-paragraph if no table."
  12795. (let ((table-p (org-at-table-p))
  12796. (table.el-p (org-at-table.el-p)))
  12797. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12798. (save-excursion (goto-char (point-at-bol))
  12799. (looking-at outline-regexp)))
  12800. t) ; skip headlines
  12801. (table.el-p t) ; skip table.el tables
  12802. (table-p (org-table-align) t) ; align org-mode tables
  12803. (t nil)))) ; call paragraph-fill
  12804. ;; For reference, this is the default value of adaptive-fill-regexp
  12805. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12806. (defun org-adaptive-fill-function ()
  12807. "Return a fill prefix for org-mode files.
  12808. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12809. work correctly."
  12810. (cond ((looking-at "#[ \t]+")
  12811. (match-string 0))
  12812. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12813. (save-excursion
  12814. (if (> (match-end 1) (+ (match-beginning 1)
  12815. org-description-max-indent))
  12816. (goto-char (+ (match-beginning 1) 5))
  12817. (goto-char (match-end 0)))
  12818. (make-string (current-column) ?\ )))
  12819. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  12820. (save-excursion
  12821. (goto-char (match-end 0))
  12822. (make-string (current-column) ?\ )))
  12823. (t nil)))
  12824. ;;; Other stuff.
  12825. (defun org-toggle-fixed-width-section (arg)
  12826. "Toggle the fixed-width export.
  12827. If there is no active region, the QUOTE keyword at the current headline is
  12828. inserted or removed. When present, it causes the text between this headline
  12829. and the next to be exported as fixed-width text, and unmodified.
  12830. If there is an active region, this command adds or removes a colon as the
  12831. first character of this line. If the first character of a line is a colon,
  12832. this line is also exported in fixed-width font."
  12833. (interactive "P")
  12834. (let* ((cc 0)
  12835. (regionp (org-region-active-p))
  12836. (beg (if regionp (region-beginning) (point)))
  12837. (end (if regionp (region-end)))
  12838. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12839. (case-fold-search nil)
  12840. (re "[ \t]*\\(:\\)")
  12841. off)
  12842. (if regionp
  12843. (save-excursion
  12844. (goto-char beg)
  12845. (setq cc (current-column))
  12846. (beginning-of-line 1)
  12847. (setq off (looking-at re))
  12848. (while (> nlines 0)
  12849. (setq nlines (1- nlines))
  12850. (beginning-of-line 1)
  12851. (cond
  12852. (arg
  12853. (org-move-to-column cc t)
  12854. (insert ":\n")
  12855. (forward-line -1))
  12856. ((and off (looking-at re))
  12857. (replace-match "" t t nil 1))
  12858. ((not off) (org-move-to-column cc t) (insert ":")))
  12859. (forward-line 1)))
  12860. (save-excursion
  12861. (org-back-to-heading)
  12862. (if (looking-at (concat outline-regexp
  12863. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12864. (replace-match "" t t nil 1)
  12865. (if (looking-at outline-regexp)
  12866. (progn
  12867. (goto-char (match-end 0))
  12868. (insert org-quote-string " "))))))))
  12869. ;;;; Functions extending outline functionality
  12870. (defun org-beginning-of-line (&optional arg)
  12871. "Go to the beginning of the current line. If that is invisible, continue
  12872. to a visible line beginning. This makes the function of C-a more intuitive.
  12873. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12874. first attempt, and only move to after the tags when the cursor is already
  12875. beyond the end of the headline."
  12876. (interactive "P")
  12877. (let ((pos (point)) refpos)
  12878. (beginning-of-line 1)
  12879. (if (bobp)
  12880. nil
  12881. (backward-char 1)
  12882. (if (org-invisible-p)
  12883. (while (and (not (bobp)) (org-invisible-p))
  12884. (backward-char 1)
  12885. (beginning-of-line 1))
  12886. (forward-char 1)))
  12887. (when org-special-ctrl-a/e
  12888. (cond
  12889. ((and (looking-at org-complex-heading-regexp)
  12890. (= (char-after (match-end 1)) ?\ ))
  12891. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12892. (point-at-eol)))
  12893. (goto-char
  12894. (if (eq org-special-ctrl-a/e t)
  12895. (cond ((> pos refpos) refpos)
  12896. ((= pos (point)) refpos)
  12897. (t (point)))
  12898. (cond ((> pos (point)) (point))
  12899. ((not (eq last-command this-command)) (point))
  12900. (t refpos)))))
  12901. ((org-at-item-p)
  12902. (goto-char
  12903. (if (eq org-special-ctrl-a/e t)
  12904. (cond ((> pos (match-end 4)) (match-end 4))
  12905. ((= pos (point)) (match-end 4))
  12906. (t (point)))
  12907. (cond ((> pos (point)) (point))
  12908. ((not (eq last-command this-command)) (point))
  12909. (t (match-end 4))))))))
  12910. (org-no-warnings
  12911. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12912. (defun org-end-of-line (&optional arg)
  12913. "Go to the end of the line.
  12914. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12915. first attempt, and only move to after the tags when the cursor is already
  12916. beyond the end of the headline."
  12917. (interactive "P")
  12918. (if (or (not org-special-ctrl-a/e)
  12919. (not (org-on-heading-p)))
  12920. (end-of-line arg)
  12921. (let ((pos (point)))
  12922. (beginning-of-line 1)
  12923. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12924. (if (eq org-special-ctrl-a/e t)
  12925. (if (or (< pos (match-beginning 1))
  12926. (= pos (match-end 0)))
  12927. (goto-char (match-beginning 1))
  12928. (goto-char (match-end 0)))
  12929. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12930. (goto-char (match-end 0))
  12931. (goto-char (match-beginning 1))))
  12932. (end-of-line arg))))
  12933. (org-no-warnings
  12934. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  12935. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12936. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12937. (defun org-kill-line (&optional arg)
  12938. "Kill line, to tags or end of line."
  12939. (interactive "P")
  12940. (cond
  12941. ((or (not org-special-ctrl-k)
  12942. (bolp)
  12943. (not (org-on-heading-p)))
  12944. (call-interactively 'kill-line))
  12945. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12946. (kill-region (point) (match-beginning 1))
  12947. (org-set-tags nil t))
  12948. (t (kill-region (point) (point-at-eol)))))
  12949. (define-key org-mode-map "\C-k" 'org-kill-line)
  12950. (defun org-yank (&optional arg)
  12951. "Yank. If the kill is a subtree, treat it specially.
  12952. This command will look at the current kill and check if is a single
  12953. subtree, or a series of subtrees[1]. If it passes the test, and if the
  12954. cursor is at the beginning of a line or after the stars of a currently
  12955. empty headline, then the yank is handeled specially. How exactly depends
  12956. on the value of the following variables, both set by default.
  12957. org-yank-folded-subtrees
  12958. When set, the subree(s) will be folded after insertion, but only
  12959. if doing so would now swallow text after the yanked text.
  12960. org-yank-adjusted-subtrees
  12961. When set, the subtree will be promoted or demoted in order to
  12962. fit into the local outline tree structure, which means that the level
  12963. will be adjusted so that it becomes the smaller one of the two
  12964. *visible* surrounding headings.
  12965. Any prefix to this command will cause `yank' to be called directly with
  12966. no special treatment. In particular, a simple `C-u' prefix will just
  12967. plainly yank the text as it is.
  12968. \[1] Basically, the test checks if the first non-white line is a heading
  12969. and if there are no other headings with fewer stars."
  12970. (interactive "P")
  12971. (setq this-command 'yank)
  12972. (if arg
  12973. (call-interactively 'yank)
  12974. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  12975. (and (org-kill-is-subtree-p)
  12976. (or (bolp)
  12977. (and (looking-at "[ \t]*$")
  12978. (string-match
  12979. "\\`\\*+\\'"
  12980. (buffer-substring (point-at-bol) (point)))))))
  12981. swallowp)
  12982. (cond
  12983. ((and subtreep org-yank-folded-subtrees)
  12984. (let ((beg (point))
  12985. end)
  12986. (if (and subtreep org-yank-adjusted-subtrees)
  12987. (org-paste-subtree nil nil 'for-yank)
  12988. (call-interactively 'yank))
  12989. (setq end (point))
  12990. (goto-char beg)
  12991. (when (and (bolp) subtreep
  12992. (not (setq swallowp
  12993. (org-yank-folding-would-swallow-text beg end))))
  12994. (or (looking-at outline-regexp)
  12995. (re-search-forward (concat "^" outline-regexp) end t))
  12996. (while (and (< (point) end) (looking-at outline-regexp))
  12997. (hide-subtree)
  12998. (org-cycle-show-empty-lines 'folded)
  12999. (condition-case nil
  13000. (outline-forward-same-level 1)
  13001. (error (goto-char end)))))
  13002. (when swallowp
  13003. (message
  13004. "Yanked text not folded because that would swallow text"))
  13005. (goto-char end)
  13006. (skip-chars-forward " \t\n\r")
  13007. (beginning-of-line 1)
  13008. (push-mark beg 'nomsg)))
  13009. ((and subtreep org-yank-adjusted-subtrees)
  13010. (let ((beg (point-at-bol)))
  13011. (org-paste-subtree nil nil 'for-yank)
  13012. (push-mark beg 'nomsg)))
  13013. (t
  13014. (call-interactively 'yank))))))
  13015. (defun org-yank-folding-would-swallow-text (beg end)
  13016. "Would hide-subtree at BEG swallow any text after END?"
  13017. (let (level)
  13018. (save-excursion
  13019. (goto-char beg)
  13020. (when (or (looking-at outline-regexp)
  13021. (re-search-forward (concat "^" outline-regexp) end t))
  13022. (setq level (org-outline-level)))
  13023. (goto-char end)
  13024. (skip-chars-forward " \t\r\n\v\f")
  13025. (if (or (eobp)
  13026. (and (bolp) (looking-at org-outline-regexp)
  13027. (<= (org-outline-level) level)))
  13028. nil ; Nothing would be swallowed
  13029. t)))) ; something would swallow
  13030. (define-key org-mode-map "\C-y" 'org-yank)
  13031. (defun org-invisible-p ()
  13032. "Check if point is at a character currently not visible."
  13033. ;; Early versions of noutline don't have `outline-invisible-p'.
  13034. (if (fboundp 'outline-invisible-p)
  13035. (outline-invisible-p)
  13036. (get-char-property (point) 'invisible)))
  13037. (defun org-invisible-p2 ()
  13038. "Check if point is at a character currently not visible."
  13039. (save-excursion
  13040. (if (and (eolp) (not (bobp))) (backward-char 1))
  13041. ;; Early versions of noutline don't have `outline-invisible-p'.
  13042. (if (fboundp 'outline-invisible-p)
  13043. (outline-invisible-p)
  13044. (get-char-property (point) 'invisible))))
  13045. (defun org-back-to-heading (&optional invisible-ok)
  13046. "Call `outline-back-to-heading', but provide a better error message."
  13047. (condition-case nil
  13048. (outline-back-to-heading invisible-ok)
  13049. (error (error "Before first headline at position %d in buffer %s"
  13050. (point) (current-buffer)))))
  13051. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13052. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13053. (defun org-at-heading-or-item-p ()
  13054. (or (org-on-heading-p) (org-at-item-p)))
  13055. (defun org-on-target-p ()
  13056. (or (org-in-regexp org-radio-target-regexp)
  13057. (org-in-regexp org-target-regexp)))
  13058. (defun org-up-heading-all (arg)
  13059. "Move to the heading line of which the present line is a subheading.
  13060. This function considers both visible and invisible heading lines.
  13061. With argument, move up ARG levels."
  13062. (if (fboundp 'outline-up-heading-all)
  13063. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13064. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13065. (defun org-up-heading-safe ()
  13066. "Move to the heading line of which the present line is a subheading.
  13067. This version will not throw an error. It will return the level of the
  13068. headline found, or nil if no higher level is found."
  13069. (let ((pos (point)) start-level level
  13070. (re (concat "^" outline-regexp)))
  13071. (catch 'exit
  13072. (org-back-to-heading t)
  13073. (setq start-level (funcall outline-level))
  13074. (if (equal start-level 1) (throw 'exit nil))
  13075. (while (re-search-backward re nil t)
  13076. (setq level (funcall outline-level))
  13077. (if (< level start-level) (throw 'exit level)))
  13078. nil)))
  13079. (defun org-first-sibling-p ()
  13080. "Is this heading the first child of its parents?"
  13081. (interactive)
  13082. (let ((re (concat "^" outline-regexp))
  13083. level l)
  13084. (unless (org-at-heading-p t)
  13085. (error "Not at a heading"))
  13086. (setq level (funcall outline-level))
  13087. (save-excursion
  13088. (if (not (re-search-backward re nil t))
  13089. t
  13090. (setq l (funcall outline-level))
  13091. (< l level)))))
  13092. (defun org-goto-sibling (&optional previous)
  13093. "Goto the next sibling, even if it is invisible.
  13094. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13095. when a sibling was found. When none is found, return nil and don't
  13096. move point."
  13097. (let ((fun (if previous 're-search-backward 're-search-forward))
  13098. (pos (point))
  13099. (re (concat "^" outline-regexp))
  13100. level l)
  13101. (when (condition-case nil (org-back-to-heading t) (error nil))
  13102. (setq level (funcall outline-level))
  13103. (catch 'exit
  13104. (or previous (forward-char 1))
  13105. (while (funcall fun re nil t)
  13106. (setq l (funcall outline-level))
  13107. (when (< l level) (goto-char pos) (throw 'exit nil))
  13108. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13109. (goto-char pos)
  13110. nil))))
  13111. (defun org-show-siblings ()
  13112. "Show all siblings of the current headline."
  13113. (save-excursion
  13114. (while (org-goto-sibling) (org-flag-heading nil)))
  13115. (save-excursion
  13116. (while (org-goto-sibling 'previous)
  13117. (org-flag-heading nil))))
  13118. (defun org-show-hidden-entry ()
  13119. "Show an entry where even the heading is hidden."
  13120. (save-excursion
  13121. (org-show-entry)))
  13122. (defun org-flag-heading (flag &optional entry)
  13123. "Flag the current heading. FLAG non-nil means make invisible.
  13124. When ENTRY is non-nil, show the entire entry."
  13125. (save-excursion
  13126. (org-back-to-heading t)
  13127. ;; Check if we should show the entire entry
  13128. (if entry
  13129. (progn
  13130. (org-show-entry)
  13131. (save-excursion
  13132. (and (outline-next-heading)
  13133. (org-flag-heading nil))))
  13134. (outline-flag-region (max (point-min) (1- (point)))
  13135. (save-excursion (outline-end-of-heading) (point))
  13136. flag))))
  13137. (defun org-forward-same-level (arg)
  13138. "Move forward to the ARG'th subheading at same level as this one.
  13139. Stop at the first and last subheadings of a superior heading.
  13140. This is like outline-forward-same-level, but invisible headings are ok."
  13141. (interactive "p")
  13142. (org-back-to-heading t)
  13143. (while (> arg 0)
  13144. (let ((point-to-move-to (save-excursion
  13145. (org-get-next-sibling))))
  13146. (if point-to-move-to
  13147. (progn
  13148. (goto-char point-to-move-to)
  13149. (setq arg (1- arg)))
  13150. (progn
  13151. (setq arg 0)
  13152. (error "No following same-level heading"))))))
  13153. (defun org-get-next-sibling ()
  13154. "Move to next heading of the same level, and return point.
  13155. If there is no such heading, return nil.
  13156. This is like outline-next-sibling, but invisible headings are ok."
  13157. (let ((level (funcall outline-level)))
  13158. (outline-next-heading)
  13159. (while (and (not (eobp)) (> (funcall outline-level) level))
  13160. (outline-next-heading))
  13161. (if (or (eobp) (< (funcall outline-level) level))
  13162. nil
  13163. (point))))
  13164. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13165. ;; This is an exact copy of the original function, but it uses
  13166. ;; `org-back-to-heading', to make it work also in invisible
  13167. ;; trees. And is uses an invisible-OK argument.
  13168. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13169. (org-back-to-heading invisible-OK)
  13170. (let ((first t)
  13171. (level (funcall outline-level)))
  13172. (while (and (not (eobp))
  13173. (or first (> (funcall outline-level) level)))
  13174. (setq first nil)
  13175. (outline-next-heading))
  13176. (unless to-heading
  13177. (if (memq (preceding-char) '(?\n ?\^M))
  13178. (progn
  13179. ;; Go to end of line before heading
  13180. (forward-char -1)
  13181. (if (memq (preceding-char) '(?\n ?\^M))
  13182. ;; leave blank line before heading
  13183. (forward-char -1))))))
  13184. (point))
  13185. (defun org-show-subtree ()
  13186. "Show everything after this heading at deeper levels."
  13187. (outline-flag-region
  13188. (point)
  13189. (save-excursion
  13190. (outline-end-of-subtree) (outline-next-heading) (point))
  13191. nil))
  13192. (defun org-show-entry ()
  13193. "Show the body directly following this heading.
  13194. Show the heading too, if it is currently invisible."
  13195. (interactive)
  13196. (save-excursion
  13197. (condition-case nil
  13198. (progn
  13199. (org-back-to-heading t)
  13200. (outline-flag-region
  13201. (max (point-min) (1- (point)))
  13202. (save-excursion
  13203. (re-search-forward
  13204. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13205. (or (match-beginning 1) (point-max)))
  13206. nil))
  13207. (error nil))))
  13208. (defun org-make-options-regexp (kwds)
  13209. "Make a regular expression for keyword lines."
  13210. (concat
  13211. "^"
  13212. "#?[ \t]*\\+\\("
  13213. (mapconcat 'regexp-quote kwds "\\|")
  13214. "\\):[ \t]*"
  13215. "\\(.+\\)"))
  13216. ;; Make isearch reveal the necessary context
  13217. (defun org-isearch-end ()
  13218. "Reveal context after isearch exits."
  13219. (when isearch-success ; only if search was successful
  13220. (if (featurep 'xemacs)
  13221. ;; Under XEmacs, the hook is run in the correct place,
  13222. ;; we directly show the context.
  13223. (org-show-context 'isearch)
  13224. ;; In Emacs the hook runs *before* restoring the overlays.
  13225. ;; So we have to use a one-time post-command-hook to do this.
  13226. ;; (Emacs 22 has a special variable, see function `org-mode')
  13227. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13228. isearch-mode-end-hook-quit)
  13229. ;; Only when the isearch was not quitted.
  13230. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13231. 'append 'local)))))
  13232. (defun org-isearch-post-command ()
  13233. "Remove self from hook, and show context."
  13234. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13235. (org-show-context 'isearch))
  13236. ;;;; Integration with and fixes for other packages
  13237. ;;; Imenu support
  13238. (defvar org-imenu-markers nil
  13239. "All markers currently used by Imenu.")
  13240. (make-variable-buffer-local 'org-imenu-markers)
  13241. (defun org-imenu-new-marker (&optional pos)
  13242. "Return a new marker for use by Imenu, and remember the marker."
  13243. (let ((m (make-marker)))
  13244. (move-marker m (or pos (point)))
  13245. (push m org-imenu-markers)
  13246. m))
  13247. (defun org-imenu-get-tree ()
  13248. "Produce the index for Imenu."
  13249. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13250. (setq org-imenu-markers nil)
  13251. (let* ((n org-imenu-depth)
  13252. (re (concat "^" outline-regexp))
  13253. (subs (make-vector (1+ n) nil))
  13254. (last-level 0)
  13255. m tree level head)
  13256. (save-excursion
  13257. (save-restriction
  13258. (widen)
  13259. (goto-char (point-max))
  13260. (while (re-search-backward re nil t)
  13261. (setq level (org-reduced-level (funcall outline-level)))
  13262. (when (<= level n)
  13263. (looking-at org-complex-heading-regexp)
  13264. (setq head (org-link-display-format
  13265. (org-match-string-no-properties 4))
  13266. m (org-imenu-new-marker))
  13267. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13268. (if (>= level last-level)
  13269. (push (cons head m) (aref subs level))
  13270. (push (cons head (aref subs (1+ level))) (aref subs level))
  13271. (loop for i from (1+ level) to n do (aset subs i nil)))
  13272. (setq last-level level)))))
  13273. (aref subs 1)))
  13274. (eval-after-load "imenu"
  13275. '(progn
  13276. (add-hook 'imenu-after-jump-hook
  13277. (lambda ()
  13278. (if (eq major-mode 'org-mode)
  13279. (org-show-context 'org-goto))))))
  13280. (defun org-link-display-format (link)
  13281. "Replace a link with either the description, or the link target
  13282. if no description is present"
  13283. (save-match-data
  13284. (if (string-match org-bracket-link-analytic-regexp link)
  13285. (replace-match (or (match-string 5 link)
  13286. (concat (match-string 1 link)
  13287. (match-string 3 link)))
  13288. nil nil link)
  13289. link)))
  13290. ;; Speedbar support
  13291. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13292. "Overlay marking the agenda restriction line in speedbar.")
  13293. (org-overlay-put org-speedbar-restriction-lock-overlay
  13294. 'face 'org-agenda-restriction-lock)
  13295. (org-overlay-put org-speedbar-restriction-lock-overlay
  13296. 'help-echo "Agendas are currently limited to this item.")
  13297. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13298. (defun org-speedbar-set-agenda-restriction ()
  13299. "Restrict future agenda commands to the location at point in speedbar.
  13300. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13301. (interactive)
  13302. (require 'org-agenda)
  13303. (let (p m tp np dir txt w)
  13304. (cond
  13305. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13306. 'org-imenu t))
  13307. (setq m (get-text-property p 'org-imenu-marker))
  13308. (save-excursion
  13309. (save-restriction
  13310. (set-buffer (marker-buffer m))
  13311. (goto-char m)
  13312. (org-agenda-set-restriction-lock 'subtree))))
  13313. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13314. 'speedbar-function 'speedbar-find-file))
  13315. (setq tp (previous-single-property-change
  13316. (1+ p) 'speedbar-function)
  13317. np (next-single-property-change
  13318. tp 'speedbar-function)
  13319. dir (speedbar-line-directory)
  13320. txt (buffer-substring-no-properties (or tp (point-min))
  13321. (or np (point-max))))
  13322. (save-excursion
  13323. (save-restriction
  13324. (set-buffer (find-file-noselect
  13325. (let ((default-directory dir))
  13326. (expand-file-name txt))))
  13327. (unless (org-mode-p)
  13328. (error "Cannot restrict to non-Org-mode file"))
  13329. (org-agenda-set-restriction-lock 'file))))
  13330. (t (error "Don't know how to restrict Org-mode's agenda")))
  13331. (org-move-overlay org-speedbar-restriction-lock-overlay
  13332. (point-at-bol) (point-at-eol))
  13333. (setq current-prefix-arg nil)
  13334. (org-agenda-maybe-redo)))
  13335. (eval-after-load "speedbar"
  13336. '(progn
  13337. (speedbar-add-supported-extension ".org")
  13338. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13339. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13340. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13341. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13342. (add-hook 'speedbar-visiting-tag-hook
  13343. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13344. ;;; Fixes and Hacks for problems with other packages
  13345. ;; Make flyspell not check words in links, to not mess up our keymap
  13346. (defun org-mode-flyspell-verify ()
  13347. "Don't let flyspell put overlays at active buttons."
  13348. (not (get-text-property (point) 'keymap)))
  13349. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13350. (eval-after-load "bookmark"
  13351. '(if (boundp 'bookmark-after-jump-hook)
  13352. ;; We can use the hook
  13353. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13354. ;; Hook not available, use advice
  13355. (defadvice bookmark-jump (after org-make-visible activate)
  13356. "Make the position visible."
  13357. (org-bookmark-jump-unhide))))
  13358. ;; Make sure saveplace show the location if it was hidden
  13359. (eval-after-load "saveplace"
  13360. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13361. "Make the position visible."
  13362. (org-bookmark-jump-unhide)))
  13363. (defun org-bookmark-jump-unhide ()
  13364. "Unhide the current position, to show the bookmark location."
  13365. (and (org-mode-p)
  13366. (or (org-invisible-p)
  13367. (save-excursion (goto-char (max (point-min) (1- (point))))
  13368. (org-invisible-p)))
  13369. (org-show-context 'bookmark-jump)))
  13370. ;; Make session.el ignore our circular variable
  13371. (eval-after-load "session"
  13372. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13373. ;;;; Experimental code
  13374. (defun org-closed-in-range ()
  13375. "Sparse tree of items closed in a certain time range.
  13376. Still experimental, may disappear in the future."
  13377. (interactive)
  13378. ;; Get the time interval from the user.
  13379. (let* ((time1 (time-to-seconds
  13380. (org-read-date nil 'to-time nil "Starting date: ")))
  13381. (time2 (time-to-seconds
  13382. (org-read-date nil 'to-time nil "End date:")))
  13383. ;; callback function
  13384. (callback (lambda ()
  13385. (let ((time
  13386. (time-to-seconds
  13387. (apply 'encode-time
  13388. (org-parse-time-string
  13389. (match-string 1))))))
  13390. ;; check if time in interval
  13391. (and (>= time time1) (<= time time2))))))
  13392. ;; make tree, check each match with the callback
  13393. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13394. ;;;; Finish up
  13395. (provide 'org)
  13396. (run-hooks 'org-load-hook)
  13397. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13398. ;;; org.el ends here