org.el 584 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.23b
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.23b"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  163. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  164. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  165. (const :tag "C eval: Include command output as text" org-eval)
  166. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  167. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  168. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  169. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  170. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  171. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  172. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  173. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  174. (const :tag "C R: Computation using the R language" org-R)
  175. (const :tag "C registry: A registry for Org links" org-registry)
  176. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  177. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  178. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  179. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  180. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  181. (defcustom org-support-shift-select nil
  182. "Non-nil means, make shift-cursor commands select text when possible.
  183. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  184. selecting a region, or enlarge thusly regions started in this way.
  185. In Org-mode, in special contexts, these same keys are used for other
  186. purposes, important enough to compete with shift selection. Org tries
  187. to balance these needs by supporting `shift-select-mode' outside these
  188. special contexts, under control of this variable.
  189. The default of this variable is nil, to avoid confusing behavior. Shifted
  190. cursor keys will then execute Org commands in the following contexts:
  191. - on a headline, changing TODO state (left/right) and priority (up/down)
  192. - on a time stamp, changing the time
  193. - in a plain list item, changing the bullet type
  194. - in a property definition line, switching between allowed values
  195. - in the BEGIN line of a clock table (changing the time block).
  196. Outside these contexts, the commands will throw an error.
  197. When this variable is t and the cursor is not in a special context,
  198. Org-mode will support shift-selection for making and enlarging regions.
  199. To make this more effective, the bullet cycling will no longer happen
  200. anywhere in an item line, but only if the cursor is exactly on the bullet.
  201. If you set this variable to the symbol `always', then the keys
  202. will not be special in headlines, property lines, and item lines, to make
  203. shift selection work there as well. If this is what you want, you can
  204. use the following alternative commands: `C-c C-t' and `C-c ,' to
  205. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  206. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  207. edited by hand or in column view.
  208. However, when the cursor is on a timestamp, shift-cursor commands
  209. will still edit the time stamp - this is just too good to give up.
  210. XEmacs user should have this variable set to nil, because shift-select-mode
  211. is Emacs 23 only."
  212. :group 'org
  213. :type '(choice
  214. (const :tag "Never" nil)
  215. (const :tag "When outside special context" t)
  216. (const :tag "Everywhere except timestamps" always)))
  217. (defgroup org-startup nil
  218. "Options concerning startup of Org-mode."
  219. :tag "Org Startup"
  220. :group 'org)
  221. (defcustom org-startup-folded t
  222. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  223. This can also be configured on a per-file basis by adding one of
  224. the following lines anywhere in the buffer:
  225. #+STARTUP: fold
  226. #+STARTUP: nofold
  227. #+STARTUP: content"
  228. :group 'org-startup
  229. :type '(choice
  230. (const :tag "nofold: show all" nil)
  231. (const :tag "fold: overview" t)
  232. (const :tag "content: all headlines" content)))
  233. (defcustom org-startup-truncated t
  234. "Non-nil means, entering Org-mode will set `truncate-lines'.
  235. This is useful since some lines containing links can be very long and
  236. uninteresting. Also tables look terrible when wrapped."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (defcustom org-startup-align-all-tables nil
  240. "Non-nil means, align all tables when visiting a file.
  241. This is useful when the column width in tables is forced with <N> cookies
  242. in table fields. Such tables will look correct only after the first re-align.
  243. This can also be configured on a per-file basis by adding one of
  244. the following lines anywhere in the buffer:
  245. #+STARTUP: align
  246. #+STARTUP: noalign"
  247. :group 'org-startup
  248. :type 'boolean)
  249. (defcustom org-insert-mode-line-in-empty-file nil
  250. "Non-nil means insert the first line setting Org-mode in empty files.
  251. When the function `org-mode' is called interactively in an empty file, this
  252. normally means that the file name does not automatically trigger Org-mode.
  253. To ensure that the file will always be in Org-mode in the future, a
  254. line enforcing Org-mode will be inserted into the buffer, if this option
  255. has been set."
  256. :group 'org-startup
  257. :type 'boolean)
  258. (defcustom org-replace-disputed-keys nil
  259. "Non-nil means use alternative key bindings for some keys.
  260. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  261. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  262. If you want to use Org-mode together with one of these other modes,
  263. or more generally if you would like to move some Org-mode commands to
  264. other keys, set this variable and configure the keys with the variable
  265. `org-disputed-keys'.
  266. This option is only relevant at load-time of Org-mode, and must be set
  267. *before* org.el is loaded. Changing it requires a restart of Emacs to
  268. become effective."
  269. :group 'org-startup
  270. :type 'boolean)
  271. (defcustom org-use-extra-keys nil
  272. "Non-nil means use extra key sequence definitions for certain
  273. commands. This happens automatically if you run XEmacs or if
  274. window-system is nil. This variable lets you do the same
  275. manually. You must set it before loading org.
  276. Example: on Carbon Emacs 22 running graphically, with an external
  277. keyboard on a Powerbook, the default way of setting M-left might
  278. not work for either Alt or ESC. Setting this variable will make
  279. it work for ESC."
  280. :group 'org-startup
  281. :type 'boolean)
  282. (if (fboundp 'defvaralias)
  283. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  284. (defcustom org-disputed-keys
  285. '(([(shift up)] . [(meta p)])
  286. ([(shift down)] . [(meta n)])
  287. ([(shift left)] . [(meta -)])
  288. ([(shift right)] . [(meta +)])
  289. ([(control shift right)] . [(meta shift +)])
  290. ([(control shift left)] . [(meta shift -)]))
  291. "Keys for which Org-mode and other modes compete.
  292. This is an alist, cars are the default keys, second element specifies
  293. the alternative to use when `org-replace-disputed-keys' is t.
  294. Keys can be specified in any syntax supported by `define-key'.
  295. The value of this option takes effect only at Org-mode's startup,
  296. therefore you'll have to restart Emacs to apply it after changing."
  297. :group 'org-startup
  298. :type 'alist)
  299. (defun org-key (key)
  300. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  301. Or return the original if not disputed."
  302. (if org-replace-disputed-keys
  303. (let* ((nkey (key-description key))
  304. (x (org-find-if (lambda (x)
  305. (equal (key-description (car x)) nkey))
  306. org-disputed-keys)))
  307. (if x (cdr x) key))
  308. key))
  309. (defun org-find-if (predicate seq)
  310. (catch 'exit
  311. (while seq
  312. (if (funcall predicate (car seq))
  313. (throw 'exit (car seq))
  314. (pop seq)))))
  315. (defun org-defkey (keymap key def)
  316. "Define a key, possibly translated, as returned by `org-key'."
  317. (define-key keymap (org-key key) def))
  318. (defcustom org-ellipsis nil
  319. "The ellipsis to use in the Org-mode outline.
  320. When nil, just use the standard three dots. When a string, use that instead,
  321. When a face, use the standard 3 dots, but with the specified face.
  322. The change affects only Org-mode (which will then use its own display table).
  323. Changing this requires executing `M-x org-mode' in a buffer to become
  324. effective."
  325. :group 'org-startup
  326. :type '(choice (const :tag "Default" nil)
  327. (face :tag "Face" :value org-warning)
  328. (string :tag "String" :value "...#")))
  329. (defvar org-display-table nil
  330. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  331. (defgroup org-keywords nil
  332. "Keywords in Org-mode."
  333. :tag "Org Keywords"
  334. :group 'org)
  335. (defcustom org-deadline-string "DEADLINE:"
  336. "String to mark deadline entries.
  337. A deadline is this string, followed by a time stamp. Should be a word,
  338. terminated by a colon. You can insert a schedule keyword and
  339. a timestamp with \\[org-deadline].
  340. Changes become only effective after restarting Emacs."
  341. :group 'org-keywords
  342. :type 'string)
  343. (defcustom org-scheduled-string "SCHEDULED:"
  344. "String to mark scheduled TODO entries.
  345. A schedule is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-schedule].
  348. Changes become only effective after restarting Emacs."
  349. :group 'org-keywords
  350. :type 'string)
  351. (defcustom org-closed-string "CLOSED:"
  352. "String used as the prefix for timestamps logging closing a TODO entry."
  353. :group 'org-keywords
  354. :type 'string)
  355. (defcustom org-clock-string "CLOCK:"
  356. "String used as prefix for timestamps clocking work hours on an item."
  357. :group 'org-keywords
  358. :type 'string)
  359. (defcustom org-comment-string "COMMENT"
  360. "Entries starting with this keyword will never be exported.
  361. An entry can be toggled between COMMENT and normal with
  362. \\[org-toggle-comment].
  363. Changes become only effective after restarting Emacs."
  364. :group 'org-keywords
  365. :type 'string)
  366. (defcustom org-quote-string "QUOTE"
  367. "Entries starting with this keyword will be exported in fixed-width font.
  368. Quoting applies only to the text in the entry following the headline, and does
  369. not extend beyond the next headline, even if that is lower level.
  370. An entry can be toggled between QUOTE and normal with
  371. \\[org-toggle-fixed-width-section]."
  372. :group 'org-keywords
  373. :type 'string)
  374. (defconst org-repeat-re
  375. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  376. "Regular expression for specifying repeated events.
  377. After a match, group 1 contains the repeat expression.")
  378. (defgroup org-structure nil
  379. "Options concerning the general structure of Org-mode files."
  380. :tag "Org Structure"
  381. :group 'org)
  382. (defgroup org-reveal-location nil
  383. "Options about how to make context of a location visible."
  384. :tag "Org Reveal Location"
  385. :group 'org-structure)
  386. (defconst org-context-choice
  387. '(choice
  388. (const :tag "Always" t)
  389. (const :tag "Never" nil)
  390. (repeat :greedy t :tag "Individual contexts"
  391. (cons
  392. (choice :tag "Context"
  393. (const agenda)
  394. (const org-goto)
  395. (const occur-tree)
  396. (const tags-tree)
  397. (const link-search)
  398. (const mark-goto)
  399. (const bookmark-jump)
  400. (const isearch)
  401. (const default))
  402. (boolean))))
  403. "Contexts for the reveal options.")
  404. (defcustom org-show-hierarchy-above '((default . t))
  405. "Non-nil means, show full hierarchy when revealing a location.
  406. Org-mode often shows locations in an org-mode file which might have
  407. been invisible before. When this is set, the hierarchy of headings
  408. above the exposed location is shown.
  409. Turning this off for example for sparse trees makes them very compact.
  410. Instead of t, this can also be an alist specifying this option for different
  411. contexts. Valid contexts are
  412. agenda when exposing an entry from the agenda
  413. org-goto when using the command `org-goto' on key C-c C-j
  414. occur-tree when using the command `org-occur' on key C-c /
  415. tags-tree when constructing a sparse tree based on tags matches
  416. link-search when exposing search matches associated with a link
  417. mark-goto when exposing the jump goal of a mark
  418. bookmark-jump when exposing a bookmark location
  419. isearch when exiting from an incremental search
  420. default default for all contexts not set explicitly"
  421. :group 'org-reveal-location
  422. :type org-context-choice)
  423. (defcustom org-show-following-heading '((default . nil))
  424. "Non-nil means, show following heading when revealing a location.
  425. Org-mode often shows locations in an org-mode file which might have
  426. been invisible before. When this is set, the heading following the
  427. match is shown.
  428. Turning this off for example for sparse trees makes them very compact,
  429. but makes it harder to edit the location of the match. In such a case,
  430. use the command \\[org-reveal] to show more context.
  431. Instead of t, this can also be an alist specifying this option for different
  432. contexts. See `org-show-hierarchy-above' for valid contexts."
  433. :group 'org-reveal-location
  434. :type org-context-choice)
  435. (defcustom org-show-siblings '((default . nil) (isearch t))
  436. "Non-nil means, show all sibling heading when revealing a location.
  437. Org-mode often shows locations in an org-mode file which might have
  438. been invisible before. When this is set, the sibling of the current entry
  439. heading are all made visible. If `org-show-hierarchy-above' is t,
  440. the same happens on each level of the hierarchy above the current entry.
  441. By default this is on for the isearch context, off for all other contexts.
  442. Turning this off for example for sparse trees makes them very compact,
  443. but makes it harder to edit the location of the match. In such a case,
  444. use the command \\[org-reveal] to show more context.
  445. Instead of t, this can also be an alist specifying this option for different
  446. contexts. See `org-show-hierarchy-above' for valid contexts."
  447. :group 'org-reveal-location
  448. :type org-context-choice)
  449. (defcustom org-show-entry-below '((default . nil))
  450. "Non-nil means, show the entry below a headline when revealing a location.
  451. Org-mode often shows locations in an org-mode file which might have
  452. been invisible before. When this is set, the text below the headline that is
  453. exposed is also shown.
  454. By default this is off for all contexts.
  455. Instead of t, this can also be an alist specifying this option for different
  456. contexts. See `org-show-hierarchy-above' for valid contexts."
  457. :group 'org-reveal-location
  458. :type org-context-choice)
  459. (defcustom org-indirect-buffer-display 'other-window
  460. "How should indirect tree buffers be displayed?
  461. This applies to indirect buffers created with the commands
  462. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  463. Valid values are:
  464. current-window Display in the current window
  465. other-window Just display in another window.
  466. dedicated-frame Create one new frame, and re-use it each time.
  467. new-frame Make a new frame each time. Note that in this case
  468. previously-made indirect buffers are kept, and you need to
  469. kill these buffers yourself."
  470. :group 'org-structure
  471. :group 'org-agenda-windows
  472. :type '(choice
  473. (const :tag "In current window" current-window)
  474. (const :tag "In current frame, other window" other-window)
  475. (const :tag "Each time a new frame" new-frame)
  476. (const :tag "One dedicated frame" dedicated-frame)))
  477. (defgroup org-cycle nil
  478. "Options concerning visibility cycling in Org-mode."
  479. :tag "Org Cycle"
  480. :group 'org-structure)
  481. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  482. "Names of drawers. Drawers are not opened by cycling on the headline above.
  483. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  484. this:
  485. :DRAWERNAME:
  486. .....
  487. :END:
  488. The drawer \"PROPERTIES\" is special for capturing properties through
  489. the property API.
  490. Drawers can be defined on the per-file basis with a line like:
  491. #+DRAWERS: HIDDEN STATE PROPERTIES"
  492. :group 'org-structure
  493. :type '(repeat (string :tag "Drawer Name")))
  494. (defcustom org-cycle-global-at-bob nil
  495. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  496. This makes it possible to do global cycling without having to use S-TAB or
  497. C-u TAB. For this special case to work, the first line of the buffer
  498. must not be a headline - it may be empty or some other text. When used in
  499. this way, `org-cycle-hook' is disables temporarily, to make sure the
  500. cursor stays at the beginning of the buffer.
  501. When this option is nil, don't do anything special at the beginning
  502. of the buffer."
  503. :group 'org-cycle
  504. :type 'boolean)
  505. (defcustom org-cycle-emulate-tab t
  506. "Where should `org-cycle' emulate TAB.
  507. nil Never
  508. white Only in completely white lines
  509. whitestart Only at the beginning of lines, before the first non-white char
  510. t Everywhere except in headlines
  511. exc-hl-bol Everywhere except at the start of a headline
  512. If TAB is used in a place where it does not emulate TAB, the current subtree
  513. visibility is cycled."
  514. :group 'org-cycle
  515. :type '(choice (const :tag "Never" nil)
  516. (const :tag "Only in completely white lines" white)
  517. (const :tag "Before first char in a line" whitestart)
  518. (const :tag "Everywhere except in headlines" t)
  519. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  520. ))
  521. (defcustom org-cycle-separator-lines 2
  522. "Number of empty lines needed to keep an empty line between collapsed trees.
  523. If you leave an empty line between the end of a subtree and the following
  524. headline, this empty line is hidden when the subtree is folded.
  525. Org-mode will leave (exactly) one empty line visible if the number of
  526. empty lines is equal or larger to the number given in this variable.
  527. So the default 2 means, at least 2 empty lines after the end of a subtree
  528. are needed to produce free space between a collapsed subtree and the
  529. following headline.
  530. Special case: when 0, never leave empty lines in collapsed view."
  531. :group 'org-cycle
  532. :type 'integer)
  533. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  534. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  535. org-cycle-hide-drawers
  536. org-cycle-show-empty-lines
  537. org-optimize-window-after-visibility-change)
  538. "Hook that is run after `org-cycle' has changed the buffer visibility.
  539. The function(s) in this hook must accept a single argument which indicates
  540. the new state that was set by the most recent `org-cycle' command. The
  541. argument is a symbol. After a global state change, it can have the values
  542. `overview', `content', or `all'. After a local state change, it can have
  543. the values `folded', `children', or `subtree'."
  544. :group 'org-cycle
  545. :type 'hook)
  546. (defgroup org-edit-structure nil
  547. "Options concerning structure editing in Org-mode."
  548. :tag "Org Edit Structure"
  549. :group 'org-structure)
  550. (defcustom org-odd-levels-only nil
  551. "Non-nil means, skip even levels and only use odd levels for the outline.
  552. This has the effect that two stars are being added/taken away in
  553. promotion/demotion commands. It also influences how levels are
  554. handled by the exporters.
  555. Changing it requires restart of `font-lock-mode' to become effective
  556. for fontification also in regions already fontified.
  557. You may also set this on a per-file basis by adding one of the following
  558. lines to the buffer:
  559. #+STARTUP: odd
  560. #+STARTUP: oddeven"
  561. :group 'org-edit-structure
  562. :group 'org-font-lock
  563. :type 'boolean)
  564. (defcustom org-adapt-indentation t
  565. "Non-nil means, adapt indentation when promoting and demoting.
  566. When this is set and the *entire* text in an entry is indented, the
  567. indentation is increased by one space in a demotion command, and
  568. decreased by one in a promotion command. If any line in the entry
  569. body starts at column 0, indentation is not changed at all."
  570. :group 'org-edit-structure
  571. :type 'boolean)
  572. (defcustom org-special-ctrl-a/e nil
  573. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  574. When t, `C-a' will bring back the cursor to the beginning of the
  575. headline text, i.e. after the stars and after a possible TODO keyword.
  576. In an item, this will be the position after the bullet.
  577. When the cursor is already at that position, another `C-a' will bring
  578. it to the beginning of the line.
  579. `C-e' will jump to the end of the headline, ignoring the presence of tags
  580. in the headline. A second `C-e' will then jump to the true end of the
  581. line, after any tags.
  582. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  583. and only a directly following, identical keypress will bring the cursor
  584. to the special positions.
  585. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  586. set separately."
  587. :group 'org-edit-structure
  588. :type '(choice
  589. (const :tag "off" nil)
  590. (const :tag "after stars/bullet and before tags first" t)
  591. (const :tag "true line boundary first" reversed)
  592. (cons :tag "Set C-a and C-e separately"
  593. (choice :tag "Special C-a"
  594. (const :tag "off" nil)
  595. (const :tag "after stars/bullet first" t)
  596. (const :tag "before stars/bullet first" reversed))
  597. (choice :tag "Special C-e"
  598. (const :tag "off" nil)
  599. (const :tag "before tags first" t)
  600. (const :tag "after tags first" reversed)))))
  601. (if (fboundp 'defvaralias)
  602. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  603. (defcustom org-special-ctrl-k nil
  604. "Non-nil means `C-k' will behave specially in headlines.
  605. When nil, `C-k' will call the default `kill-line' command.
  606. When t, the following will happen while the cursor is in the headline:
  607. - When the cursor is at the beginning of a headline, kill the entire
  608. line and possible the folded subtree below the line.
  609. - When in the middle of the headline text, kill the headline up to the tags.
  610. - When after the headline text, kill the tags."
  611. :group 'org-edit-structure
  612. :type 'boolean)
  613. (defcustom org-yank-folded-subtrees t
  614. "Non-nil means, when yanking subtrees, fold them.
  615. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  616. it starts with a heading and all other headings in it are either children
  617. or siblings, then fold all the subtrees. However, do this only if no
  618. text after the yank would be swallowed into a folded tree by this action."
  619. :group 'org-edit-structure
  620. :type 'boolean)
  621. (defcustom org-yank-adjusted-subtrees nil
  622. "Non-nil means, when yanking subtrees, adjust the level.
  623. With this setting, `org-paste-subtree' is used to insert the subtree, see
  624. this function for details."
  625. :group 'org-edit-structure
  626. :type 'boolean)
  627. (defcustom org-M-RET-may-split-line '((default . t))
  628. "Non-nil means, M-RET will split the line at the cursor position.
  629. When nil, it will go to the end of the line before making a
  630. new line.
  631. You may also set this option in a different way for different
  632. contexts. Valid contexts are:
  633. headline when creating a new headline
  634. item when creating a new item
  635. table in a table field
  636. default the value to be used for all contexts not explicitly
  637. customized"
  638. :group 'org-structure
  639. :group 'org-table
  640. :type '(choice
  641. (const :tag "Always" t)
  642. (const :tag "Never" nil)
  643. (repeat :greedy t :tag "Individual contexts"
  644. (cons
  645. (choice :tag "Context"
  646. (const headline)
  647. (const item)
  648. (const table)
  649. (const default))
  650. (boolean)))))
  651. (defcustom org-insert-heading-respect-content nil
  652. "Non-nil means, insert new headings after the current subtree.
  653. When nil, the new heading is created directly after the current line.
  654. The commands \\[org-insert-heading-respect-content] and
  655. \\[org-insert-todo-heading-respect-content] turn this variable on
  656. for the duration of the command."
  657. :group 'org-structure
  658. :type 'boolean)
  659. (defcustom org-blank-before-new-entry '((heading . auto)
  660. (plain-list-item . auto))
  661. "Should `org-insert-heading' leave a blank line before new heading/item?
  662. The value is an alist, with `heading' and `plain-list-item' as car,
  663. and a boolean flag as cdr."
  664. :group 'org-edit-structure
  665. :type '(list
  666. (cons (const heading)
  667. (choice (const :tag "Never" nil)
  668. (const :tag "Always" t)
  669. (const :tag "Auto" auto)))
  670. (cons (const plain-list-item)
  671. (choice (const :tag "Never" nil)
  672. (const :tag "Always" t)
  673. (const :tag "Auto" auto)))))
  674. (defcustom org-insert-heading-hook nil
  675. "Hook being run after inserting a new heading."
  676. :group 'org-edit-structure
  677. :type 'hook)
  678. (defcustom org-enable-fixed-width-editor t
  679. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  680. This currently only means, they are never auto-wrapped.
  681. When nil, such lines will be treated like ordinary lines.
  682. See also the QUOTE keyword."
  683. :group 'org-edit-structure
  684. :type 'boolean)
  685. (defcustom org-edit-src-region-extra nil
  686. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  687. For examples see the function `org-edit-src-find-region-and-lang'.
  688. The regular expression identifying the begin marker should end with a newline,
  689. and the regexp marking the end line should start with a newline, to make sure
  690. there are kept outside the narrowed region."
  691. :group 'org-edit-structure
  692. :type '(repeat
  693. (list
  694. (regexp :tag "begin regexp")
  695. (regexp :tag "end regexp")
  696. (choice :tag "language"
  697. (string :tag "specify")
  698. (integer :tag "from match group")
  699. (const :tag "from `lang' element")
  700. (const :tag "from `style' element")))))
  701. (defcustom org-coderef-label-format "(ref:%s)"
  702. "The default coderef format.
  703. This format string will be used to search for coderef labels in literal
  704. examples (EXAMPLE and SRC blocks). The format can be overwritten
  705. an individual literal example with the -f option, like
  706. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  707. ...
  708. #+END_SRC
  709. If you want to use this for HTML export, make sure that the format does
  710. not introduce special font-locking, and avoid the HTML special
  711. characters `<', `>', and `&'. The reason for this restriction is that
  712. the labels are searched for only after htmlize has done its job."
  713. :group 'org-edit-structure ; FIXME this is not in the right group
  714. :type 'string)
  715. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  716. "The mode that should be used to edit fixed-width regions.
  717. These are the regions where each line starts with a colon."
  718. :group 'org-edit-structure
  719. :type '(choice
  720. (const artist-mode)
  721. (const picture-mode)
  722. (const fundamental-mode)
  723. (function :tag "Other (specify)")))
  724. (defcustom org-goto-auto-isearch t
  725. "Non-nil means, typing characters in org-goto starts incremental search."
  726. :group 'org-edit-structure
  727. :type 'boolean)
  728. (defgroup org-sparse-trees nil
  729. "Options concerning sparse trees in Org-mode."
  730. :tag "Org Sparse Trees"
  731. :group 'org-structure)
  732. (defcustom org-highlight-sparse-tree-matches t
  733. "Non-nil means, highlight all matches that define a sparse tree.
  734. The highlights will automatically disappear the next time the buffer is
  735. changed by an edit command."
  736. :group 'org-sparse-trees
  737. :type 'boolean)
  738. (defcustom org-remove-highlights-with-change t
  739. "Non-nil means, any change to the buffer will remove temporary highlights.
  740. Such highlights are created by `org-occur' and `org-clock-display'.
  741. When nil, `C-c C-c needs to be used to get rid of the highlights.
  742. The highlights created by `org-preview-latex-fragment' always need
  743. `C-c C-c' to be removed."
  744. :group 'org-sparse-trees
  745. :group 'org-time
  746. :type 'boolean)
  747. (defcustom org-occur-hook '(org-first-headline-recenter)
  748. "Hook that is run after `org-occur' has constructed a sparse tree.
  749. This can be used to recenter the window to show as much of the structure
  750. as possible."
  751. :group 'org-sparse-trees
  752. :type 'hook)
  753. (defgroup org-imenu-and-speedbar nil
  754. "Options concerning imenu and speedbar in Org-mode."
  755. :tag "Org Imenu and Speedbar"
  756. :group 'org-structure)
  757. (defcustom org-imenu-depth 2
  758. "The maximum level for Imenu access to Org-mode headlines.
  759. This also applied for speedbar access."
  760. :group 'org-imenu-and-speedbar
  761. :type 'number)
  762. (defgroup org-table nil
  763. "Options concerning tables in Org-mode."
  764. :tag "Org Table"
  765. :group 'org)
  766. (defcustom org-enable-table-editor 'optimized
  767. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  768. When nil, such lines will be treated like ordinary lines.
  769. When equal to the symbol `optimized', the table editor will be optimized to
  770. do the following:
  771. - Automatic overwrite mode in front of whitespace in table fields.
  772. This makes the structure of the table stay in tact as long as the edited
  773. field does not exceed the column width.
  774. - Minimize the number of realigns. Normally, the table is aligned each time
  775. TAB or RET are pressed to move to another field. With optimization this
  776. happens only if changes to a field might have changed the column width.
  777. Optimization requires replacing the functions `self-insert-command',
  778. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  779. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  780. very good at guessing when a re-align will be necessary, but you can always
  781. force one with \\[org-ctrl-c-ctrl-c].
  782. If you would like to use the optimized version in Org-mode, but the
  783. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  784. This variable can be used to turn on and off the table editor during a session,
  785. but in order to toggle optimization, a restart is required.
  786. See also the variable `org-table-auto-blank-field'."
  787. :group 'org-table
  788. :type '(choice
  789. (const :tag "off" nil)
  790. (const :tag "on" t)
  791. (const :tag "on, optimized" optimized)))
  792. (defcustom org-self-insert-cluster-for-undo t
  793. "Non-nil means cluster self-insert commands for undo when possible.
  794. If this is set, then, like in the Emacs command loop, 20 consequtive
  795. characters will be undone together.
  796. This is configurable, because there is some impact on typing performance."
  797. :group 'org-table
  798. :type 'boolean)
  799. (defcustom org-table-tab-recognizes-table.el t
  800. "Non-nil means, TAB will automatically notice a table.el table.
  801. When it sees such a table, it moves point into it and - if necessary -
  802. calls `table-recognize-table'."
  803. :group 'org-table-editing
  804. :type 'boolean)
  805. (defgroup org-link nil
  806. "Options concerning links in Org-mode."
  807. :tag "Org Link"
  808. :group 'org)
  809. (defvar org-link-abbrev-alist-local nil
  810. "Buffer-local version of `org-link-abbrev-alist', which see.
  811. The value of this is taken from the #+LINK lines.")
  812. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  813. (defcustom org-link-abbrev-alist nil
  814. "Alist of link abbreviations.
  815. The car of each element is a string, to be replaced at the start of a link.
  816. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  817. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  818. [[linkkey:tag][description]]
  819. The 'linkkey' must be a word word, starting with a letter, followed
  820. by letters, numbers, '-' or '_'.
  821. If REPLACE is a string, the tag will simply be appended to create the link.
  822. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  823. the placeholder \"%h\" will cause a url-encoded version of the tag to
  824. be inserted at that point (see the function `url-hexify-string').
  825. REPLACE may also be a function that will be called with the tag as the
  826. only argument to create the link, which should be returned as a string.
  827. See the manual for examples."
  828. :group 'org-link
  829. :type '(repeat
  830. (cons
  831. (string :tag "Protocol")
  832. (choice
  833. (string :tag "Format")
  834. (function)))))
  835. (defcustom org-descriptive-links t
  836. "Non-nil means, hide link part and only show description of bracket links.
  837. Bracket links are like [[link][description]]. This variable sets the initial
  838. state in new org-mode buffers. The setting can then be toggled on a
  839. per-buffer basis from the Org->Hyperlinks menu."
  840. :group 'org-link
  841. :type 'boolean)
  842. (defcustom org-link-file-path-type 'adaptive
  843. "How the path name in file links should be stored.
  844. Valid values are:
  845. relative Relative to the current directory, i.e. the directory of the file
  846. into which the link is being inserted.
  847. absolute Absolute path, if possible with ~ for home directory.
  848. noabbrev Absolute path, no abbreviation of home directory.
  849. adaptive Use relative path for files in the current directory and sub-
  850. directories of it. For other files, use an absolute path."
  851. :group 'org-link
  852. :type '(choice
  853. (const relative)
  854. (const absolute)
  855. (const noabbrev)
  856. (const adaptive)))
  857. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  858. "Types of links that should be activated in Org-mode files.
  859. This is a list of symbols, each leading to the activation of a certain link
  860. type. In principle, it does not hurt to turn on most link types - there may
  861. be a small gain when turning off unused link types. The types are:
  862. bracket The recommended [[link][description]] or [[link]] links with hiding.
  863. angular Links in angular brackets that may contain whitespace like
  864. <bbdb:Carsten Dominik>.
  865. plain Plain links in normal text, no whitespace, like http://google.com.
  866. radio Text that is matched by a radio target, see manual for details.
  867. tag Tag settings in a headline (link to tag search).
  868. date Time stamps (link to calendar).
  869. footnote Footnote labels.
  870. Changing this variable requires a restart of Emacs to become effective."
  871. :group 'org-link
  872. :type '(set :greedy t
  873. (const :tag "Double bracket links (new style)" bracket)
  874. (const :tag "Angular bracket links (old style)" angular)
  875. (const :tag "Plain text links" plain)
  876. (const :tag "Radio target matches" radio)
  877. (const :tag "Tags" tag)
  878. (const :tag "Timestamps" date)
  879. (const :tag "Footnotes" footnote)))
  880. (defcustom org-make-link-description-function nil
  881. "Function to use to generate link descriptions from links. If
  882. nil the link location will be used. This function must take two
  883. parameters; the first is the link and the second the description
  884. org-insert-link has generated, and should return the description
  885. to use."
  886. :group 'org-link
  887. :type 'function)
  888. (defgroup org-link-store nil
  889. "Options concerning storing links in Org-mode."
  890. :tag "Org Store Link"
  891. :group 'org-link)
  892. (defcustom org-email-link-description-format "Email %c: %.30s"
  893. "Format of the description part of a link to an email or usenet message.
  894. The following %-escapes will be replaced by corresponding information:
  895. %F full \"From\" field
  896. %f name, taken from \"From\" field, address if no name
  897. %T full \"To\" field
  898. %t first name in \"To\" field, address if no name
  899. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  900. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  901. %s subject
  902. %m message-id.
  903. You may use normal field width specification between the % and the letter.
  904. This is for example useful to limit the length of the subject.
  905. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  906. :group 'org-link-store
  907. :type 'string)
  908. (defcustom org-from-is-user-regexp
  909. (let (r1 r2)
  910. (when (and user-mail-address (not (string= user-mail-address "")))
  911. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  912. (when (and user-full-name (not (string= user-full-name "")))
  913. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  914. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  915. "Regexp matched against the \"From:\" header of an email or usenet message.
  916. It should match if the message is from the user him/herself."
  917. :group 'org-link-store
  918. :type 'regexp)
  919. (defcustom org-link-to-org-use-id 'create-if-interactive
  920. "Non-nil means, storing a link to an Org file will use entry IDs.
  921. Note that before this variable is even considered, org-id must be loaded,
  922. to please customize `org-modules' and turn it on.
  923. The variable can have the following values:
  924. t Create an ID if needed to make a link to the current entry.
  925. create-if-interactive
  926. If `org-store-link' is called directly (interactively, as a user
  927. command), do create an ID to support the link. But when doing the
  928. job for remember, only use the ID if it already exists. The
  929. purpose of this setting is to avoid proliferation of unwanted
  930. IDs, just because you happen to be in an Org file when you
  931. call `org-remember' that automatically and preemptively
  932. creates a link. If you do want to get an ID link in a remember
  933. template to an entry not having an ID, create it first by
  934. explicitly creating a link to it, using `C-c C-l' first.
  935. use-existing
  936. Use existing ID, do not create one.
  937. nil Never use an ID to make a link, instead link using a text search for
  938. the headline text."
  939. :group 'org-link-store
  940. :type '(choice
  941. (const :tag "Create ID to make link" t)
  942. (const :tag "Create if string link interactively"
  943. 'create-if-interactive)
  944. (const :tag "Only use existing" 'use-existing)
  945. (const :tag "Do not use ID to create link" nil)))
  946. (defcustom org-context-in-file-links t
  947. "Non-nil means, file links from `org-store-link' contain context.
  948. A search string will be added to the file name with :: as separator and
  949. used to find the context when the link is activated by the command
  950. `org-open-at-point'.
  951. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  952. negates this setting for the duration of the command."
  953. :group 'org-link-store
  954. :type 'boolean)
  955. (defcustom org-keep-stored-link-after-insertion nil
  956. "Non-nil means, keep link in list for entire session.
  957. The command `org-store-link' adds a link pointing to the current
  958. location to an internal list. These links accumulate during a session.
  959. The command `org-insert-link' can be used to insert links into any
  960. Org-mode file (offering completion for all stored links). When this
  961. option is nil, every link which has been inserted once using \\[org-insert-link]
  962. will be removed from the list, to make completing the unused links
  963. more efficient."
  964. :group 'org-link-store
  965. :type 'boolean)
  966. (defgroup org-link-follow nil
  967. "Options concerning following links in Org-mode."
  968. :tag "Org Follow Link"
  969. :group 'org-link)
  970. (defcustom org-link-translation-function nil
  971. "Function to translate links with different syntax to Org syntax.
  972. This can be used to translate links created for example by the Planner
  973. or emacs-wiki packages to Org syntax.
  974. The function must accept two parameters, a TYPE containing the link
  975. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  976. which is everything after the link protocol. It should return a cons
  977. with possibly modified values of type and path.
  978. Org contains a function for this, so if you set this variable to
  979. `org-translate-link-from-planner', you should be able follow many
  980. links created by planner."
  981. :group 'org-link-follow
  982. :type 'function)
  983. (defcustom org-follow-link-hook nil
  984. "Hook that is run after a link has been followed."
  985. :group 'org-link-follow
  986. :type 'hook)
  987. (defcustom org-tab-follows-link nil
  988. "Non-nil means, on links TAB will follow the link.
  989. Needs to be set before org.el is loaded.
  990. This really should not be used, it does not make sense, and the
  991. implementation is bad."
  992. :group 'org-link-follow
  993. :type 'boolean)
  994. (defcustom org-return-follows-link nil
  995. "Non-nil means, on links RET will follow the link.
  996. Needs to be set before org.el is loaded."
  997. :group 'org-link-follow
  998. :type 'boolean)
  999. (defcustom org-mouse-1-follows-link
  1000. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1001. "Non-nil means, mouse-1 on a link will follow the link.
  1002. A longer mouse click will still set point. Does not work on XEmacs.
  1003. Needs to be set before org.el is loaded."
  1004. :group 'org-link-follow
  1005. :type 'boolean)
  1006. (defcustom org-mark-ring-length 4
  1007. "Number of different positions to be recorded in the ring
  1008. Changing this requires a restart of Emacs to work correctly."
  1009. :group 'org-link-follow
  1010. :type 'integer)
  1011. (defcustom org-link-frame-setup
  1012. '((vm . vm-visit-folder-other-frame)
  1013. (gnus . gnus-other-frame)
  1014. (file . find-file-other-window))
  1015. "Setup the frame configuration for following links.
  1016. When following a link with Emacs, it may often be useful to display
  1017. this link in another window or frame. This variable can be used to
  1018. set this up for the different types of links.
  1019. For VM, use any of
  1020. `vm-visit-folder'
  1021. `vm-visit-folder-other-frame'
  1022. For Gnus, use any of
  1023. `gnus'
  1024. `gnus-other-frame'
  1025. `org-gnus-no-new-news'
  1026. For FILE, use any of
  1027. `find-file'
  1028. `find-file-other-window'
  1029. `find-file-other-frame'
  1030. For the calendar, use the variable `calendar-setup'.
  1031. For BBDB, it is currently only possible to display the matches in
  1032. another window."
  1033. :group 'org-link-follow
  1034. :type '(list
  1035. (cons (const vm)
  1036. (choice
  1037. (const vm-visit-folder)
  1038. (const vm-visit-folder-other-window)
  1039. (const vm-visit-folder-other-frame)))
  1040. (cons (const gnus)
  1041. (choice
  1042. (const gnus)
  1043. (const gnus-other-frame)
  1044. (const org-gnus-no-new-news)))
  1045. (cons (const file)
  1046. (choice
  1047. (const find-file)
  1048. (const find-file-other-window)
  1049. (const find-file-other-frame)))))
  1050. (defcustom org-display-internal-link-with-indirect-buffer nil
  1051. "Non-nil means, use indirect buffer to display infile links.
  1052. Activating internal links (from one location in a file to another location
  1053. in the same file) normally just jumps to the location. When the link is
  1054. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1055. another window. When this option is set, the other window actually displays
  1056. an indirect buffer clone of the current buffer, to avoid any visibility
  1057. changes to the current buffer."
  1058. :group 'org-link-follow
  1059. :type 'boolean)
  1060. (defcustom org-open-non-existing-files nil
  1061. "Non-nil means, `org-open-file' will open non-existing files.
  1062. When nil, an error will be generated."
  1063. :group 'org-link-follow
  1064. :type 'boolean)
  1065. (defcustom org-open-directory-means-index-dot-org nil
  1066. "Non-nil means, a link to a directory really means to index.org.
  1067. When nil, following a directory link will run dired or open a finder/explorer
  1068. window on that directory."
  1069. :group 'org-link-follow
  1070. :type 'boolean)
  1071. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1072. "Function and arguments to call for following mailto links.
  1073. This is a list with the first element being a lisp function, and the
  1074. remaining elements being arguments to the function. In string arguments,
  1075. %a will be replaced by the address, and %s will be replaced by the subject
  1076. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1077. :group 'org-link-follow
  1078. :type '(choice
  1079. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1080. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1081. (const :tag "message-mail" (message-mail "%a" "%s"))
  1082. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1083. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1084. "Non-nil means, ask for confirmation before executing shell links.
  1085. Shell links can be dangerous: just think about a link
  1086. [[shell:rm -rf ~/*][Google Search]]
  1087. This link would show up in your Org-mode document as \"Google Search\",
  1088. but really it would remove your entire home directory.
  1089. Therefore we advise against setting this variable to nil.
  1090. Just change it to `y-or-n-p' of you want to confirm with a
  1091. single keystroke rather than having to type \"yes\"."
  1092. :group 'org-link-follow
  1093. :type '(choice
  1094. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1095. (const :tag "with y-or-n (faster)" y-or-n-p)
  1096. (const :tag "no confirmation (dangerous)" nil)))
  1097. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1098. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1099. Elisp links can be dangerous: just think about a link
  1100. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1101. This link would show up in your Org-mode document as \"Google Search\",
  1102. but really it would remove your entire home directory.
  1103. Therefore we advise against setting this variable to nil.
  1104. Just change it to `y-or-n-p' of you want to confirm with a
  1105. single keystroke rather than having to type \"yes\"."
  1106. :group 'org-link-follow
  1107. :type '(choice
  1108. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1109. (const :tag "with y-or-n (faster)" y-or-n-p)
  1110. (const :tag "no confirmation (dangerous)" nil)))
  1111. (defconst org-file-apps-defaults-gnu
  1112. '((remote . emacs)
  1113. (system . mailcap)
  1114. (t . mailcap))
  1115. "Default file applications on a UNIX or GNU/Linux system.
  1116. See `org-file-apps'.")
  1117. (defconst org-file-apps-defaults-macosx
  1118. '((remote . emacs)
  1119. (t . "open %s")
  1120. (system . "open %s")
  1121. ("ps.gz" . "gv %s")
  1122. ("eps.gz" . "gv %s")
  1123. ("dvi" . "xdvi %s")
  1124. ("fig" . "xfig %s"))
  1125. "Default file applications on a MacOS X system.
  1126. The system \"open\" is known as a default, but we use X11 applications
  1127. for some files for which the OS does not have a good default.
  1128. See `org-file-apps'.")
  1129. (defconst org-file-apps-defaults-windowsnt
  1130. (list
  1131. '(remote . emacs)
  1132. (cons t
  1133. (list (if (featurep 'xemacs)
  1134. 'mswindows-shell-execute
  1135. 'w32-shell-execute)
  1136. "open" 'file))
  1137. (cons 'system
  1138. (list (if (featurep 'xemacs)
  1139. 'mswindows-shell-execute
  1140. 'w32-shell-execute)
  1141. "open" 'file)))
  1142. "Default file applications on a Windows NT system.
  1143. The system \"open\" is used for most files.
  1144. See `org-file-apps'.")
  1145. (defcustom org-file-apps
  1146. '(
  1147. (auto-mode . emacs)
  1148. ("\\.x?html?\\'" . default)
  1149. ("\\.pdf\\'" . default)
  1150. )
  1151. "External applications for opening `file:path' items in a document.
  1152. Org-mode uses system defaults for different file types, but
  1153. you can use this variable to set the application for a given file
  1154. extension. The entries in this list are cons cells where the car identifies
  1155. files and the cdr the corresponding command. Possible values for the
  1156. file identifier are
  1157. \"regex\" Regular expression matched against the file name. For backward
  1158. compatibility, this can also be a string with only alphanumeric
  1159. characters, which is then interpreted as an extension.
  1160. `directory' Matches a directory
  1161. `remote' Matches a remote file, accessible through tramp or efs.
  1162. Remote files most likely should be visited through Emacs
  1163. because external applications cannot handle such paths.
  1164. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1165. so all files Emacs knows how to handle. Using this with
  1166. command `emacs' will open most files in Emacs. Beware that this
  1167. will also open html files inside Emacs, unless you add
  1168. (\"html\" . default) to the list as well.
  1169. t Default for files not matched by any of the other options.
  1170. `system' The system command to open files, like `open' on Windows
  1171. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1172. that will be selected if you call `C-c C-o' with a double
  1173. `C-u C-u' prefix.
  1174. Possible values for the command are:
  1175. `emacs' The file will be visited by the current Emacs process.
  1176. `default' Use the default application for this file type, which is the
  1177. association for t in the list, most likely in the system-specific
  1178. part.
  1179. This can be used to overrule an unwanted setting in the
  1180. system-specific variable.
  1181. `system' Use the system command for opening files, like \"open\".
  1182. This command is specified by the entry whose car is `system'.
  1183. Most likely, the system-specific version of this variable
  1184. does define this command, but you can overrule/replace it
  1185. here.
  1186. string A command to be executed by a shell; %s will be replaced
  1187. by the path to the file.
  1188. sexp A Lisp form which will be evaluated. The file path will
  1189. be available in the Lisp variable `file'.
  1190. For more examples, see the system specific constants
  1191. `org-file-apps-defaults-macosx'
  1192. `org-file-apps-defaults-windowsnt'
  1193. `org-file-apps-defaults-gnu'."
  1194. :group 'org-link-follow
  1195. :type '(repeat
  1196. (cons (choice :value ""
  1197. (string :tag "Extension")
  1198. (const :tag "System command to open files" system)
  1199. (const :tag "Default for unrecognized files" t)
  1200. (const :tag "Remote file" remote)
  1201. (const :tag "Links to a directory" directory)
  1202. (const :tag "Any files that have Emacs modes"
  1203. auto-mode))
  1204. (choice :value ""
  1205. (const :tag "Visit with Emacs" emacs)
  1206. (const :tag "Use default" default)
  1207. (const :tag "Use the system command" system)
  1208. (string :tag "Command")
  1209. (sexp :tag "Lisp form")))))
  1210. (defgroup org-refile nil
  1211. "Options concerning refiling entries in Org-mode."
  1212. :tag "Org Refile"
  1213. :group 'org)
  1214. (defcustom org-directory "~/org"
  1215. "Directory with org files.
  1216. This is just a default location to look for Org files. There is no need
  1217. at all to put your files into this directory. It is only used in the
  1218. following situations:
  1219. 1. When a remember template specifies a target file that is not an
  1220. absolute path. The path will then be interpreted relative to
  1221. `org-directory'
  1222. 2. When a remember note is filed away in an interactive way (when exiting the
  1223. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1224. with `org-directory' as the default path."
  1225. :group 'org-refile
  1226. :group 'org-remember
  1227. :type 'directory)
  1228. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1229. "Default target for storing notes.
  1230. Used by the hooks for remember.el. This can be a string, or nil to mean
  1231. the value of `remember-data-file'.
  1232. You can set this on a per-template basis with the variable
  1233. `org-remember-templates'."
  1234. :group 'org-refile
  1235. :group 'org-remember
  1236. :type '(choice
  1237. (const :tag "Default from remember-data-file" nil)
  1238. file))
  1239. (defcustom org-goto-interface 'outline
  1240. "The default interface to be used for `org-goto'.
  1241. Allowed values are:
  1242. outline The interface shows an outline of the relevant file
  1243. and the correct heading is found by moving through
  1244. the outline or by searching with incremental search.
  1245. outline-path-completion Headlines in the current buffer are offered via
  1246. completion. This is the interface also used by
  1247. the refile command."
  1248. :group 'org-refile
  1249. :type '(choice
  1250. (const :tag "Outline" outline)
  1251. (const :tag "Outline-path-completion" outline-path-completion)))
  1252. (defcustom org-goto-max-level 5
  1253. "Maximum level to be considered when running org-goto with refile interface."
  1254. :group 'org-refile
  1255. :type 'number)
  1256. (defcustom org-reverse-note-order nil
  1257. "Non-nil means, store new notes at the beginning of a file or entry.
  1258. When nil, new notes will be filed to the end of a file or entry.
  1259. This can also be a list with cons cells of regular expressions that
  1260. are matched against file names, and values."
  1261. :group 'org-remember
  1262. :group 'org-refile
  1263. :type '(choice
  1264. (const :tag "Reverse always" t)
  1265. (const :tag "Reverse never" nil)
  1266. (repeat :tag "By file name regexp"
  1267. (cons regexp boolean))))
  1268. (defcustom org-refile-targets nil
  1269. "Targets for refiling entries with \\[org-refile].
  1270. This is list of cons cells. Each cell contains:
  1271. - a specification of the files to be considered, either a list of files,
  1272. or a symbol whose function or variable value will be used to retrieve
  1273. a file name or a list of file names. If you use `org-agenda-files' for
  1274. that, all agenda files will be scanned for targets. Nil means, consider
  1275. headings in the current buffer.
  1276. - A specification of how to find candidate refile targets. This may be
  1277. any of:
  1278. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1279. This tag has to be present in all target headlines, inheritance will
  1280. not be considered.
  1281. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1282. todo keyword.
  1283. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1284. headlines that are refiling targets.
  1285. - a cons cell (:level . N). Any headline of level N is considered a target.
  1286. Note that, when `org-odd-levels-only' is set, level corresponds to
  1287. order in hierarchy, not to the number of stars.
  1288. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1289. Note that, when `org-odd-levels-only' is set, level corresponds to
  1290. order in hierarchy, not to the number of stars.
  1291. When this variable is nil, all top-level headlines in the current buffer
  1292. are used, equivalent to the value `((nil . (:level . 1))'."
  1293. :group 'org-refile
  1294. :type '(repeat
  1295. (cons
  1296. (choice :value org-agenda-files
  1297. (const :tag "All agenda files" org-agenda-files)
  1298. (const :tag "Current buffer" nil)
  1299. (function) (variable) (file))
  1300. (choice :tag "Identify target headline by"
  1301. (cons :tag "Specific tag" (const :value :tag) (string))
  1302. (cons :tag "TODO keyword" (const :value :todo) (string))
  1303. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1304. (cons :tag "Level number" (const :value :level) (integer))
  1305. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1306. (defcustom org-refile-use-outline-path nil
  1307. "Non-nil means, provide refile targets as paths.
  1308. So a level 3 headline will be available as level1/level2/level3.
  1309. When the value is `file', also include the file name (without directory)
  1310. into the path. When `full-file-path', include the full file path."
  1311. :group 'org-refile
  1312. :type '(choice
  1313. (const :tag "Not" nil)
  1314. (const :tag "Yes" t)
  1315. (const :tag "Start with file name" file)
  1316. (const :tag "Start with full file path" full-file-path)))
  1317. (defcustom org-outline-path-complete-in-steps t
  1318. "Non-nil means, complete the outline path in hierarchical steps.
  1319. When Org-mode uses the refile interface to select an outline path
  1320. \(see variable `org-refile-use-outline-path'), the completion of
  1321. the path can be done is a single go, or if can be done in steps down
  1322. the headline hierarchy. Going in steps is probably the best if you
  1323. do not use a special completion package like `ido' or `icicles'.
  1324. However, when using these packages, going in one step can be very
  1325. fast, while still showing the whole path to the entry."
  1326. :group 'org-refile
  1327. :type 'boolean)
  1328. (defgroup org-todo nil
  1329. "Options concerning TODO items in Org-mode."
  1330. :tag "Org TODO"
  1331. :group 'org)
  1332. (defgroup org-progress nil
  1333. "Options concerning Progress logging in Org-mode."
  1334. :tag "Org Progress"
  1335. :group 'org-time)
  1336. (defvar org-todo-interpretation-widgets
  1337. '(
  1338. (:tag "Sequence (cycling hits every state)" sequence)
  1339. (:tag "Type (cycling directly to DONE)" type))
  1340. "The available interpretation symbols for customizing
  1341. `org-todo-keywords'.
  1342. Interested libraries should add to this list.")
  1343. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1344. "List of TODO entry keyword sequences and their interpretation.
  1345. \\<org-mode-map>This is a list of sequences.
  1346. Each sequence starts with a symbol, either `sequence' or `type',
  1347. indicating if the keywords should be interpreted as a sequence of
  1348. action steps, or as different types of TODO items. The first
  1349. keywords are states requiring action - these states will select a headline
  1350. for inclusion into the global TODO list Org-mode produces. If one of
  1351. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1352. signify that no further action is necessary. If \"|\" is not found,
  1353. the last keyword is treated as the only DONE state of the sequence.
  1354. The command \\[org-todo] cycles an entry through these states, and one
  1355. additional state where no keyword is present. For details about this
  1356. cycling, see the manual.
  1357. TODO keywords and interpretation can also be set on a per-file basis with
  1358. the special #+SEQ_TODO and #+TYP_TODO lines.
  1359. Each keyword can optionally specify a character for fast state selection
  1360. \(in combination with the variable `org-use-fast-todo-selection')
  1361. and specifiers for state change logging, using the same syntax
  1362. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1363. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1364. indicates to record a time stamp each time this state is selected.
  1365. Each keyword may also specify if a timestamp or a note should be
  1366. recorded when entering or leaving the state, by adding additional
  1367. characters in the parenthesis after the keyword. This looks like this:
  1368. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1369. record only the time of the state change. With X and Y being either
  1370. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1371. Y when leaving the state if and only if the *target* state does not
  1372. define X. You may omit any of the fast-selection key or X or /Y,
  1373. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1374. For backward compatibility, this variable may also be just a list
  1375. of keywords - in this case the interpretation (sequence or type) will be
  1376. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1377. :group 'org-todo
  1378. :group 'org-keywords
  1379. :type '(choice
  1380. (repeat :tag "Old syntax, just keywords"
  1381. (string :tag "Keyword"))
  1382. (repeat :tag "New syntax"
  1383. (cons
  1384. (choice
  1385. :tag "Interpretation"
  1386. ;;Quick and dirty way to see
  1387. ;;`org-todo-interpretations'. This takes the
  1388. ;;place of item arguments
  1389. :convert-widget
  1390. (lambda (widget)
  1391. (widget-put widget
  1392. :args (mapcar
  1393. #'(lambda (x)
  1394. (widget-convert
  1395. (cons 'const x)))
  1396. org-todo-interpretation-widgets))
  1397. widget))
  1398. (repeat
  1399. (string :tag "Keyword"))))))
  1400. (defvar org-todo-keywords-1 nil
  1401. "All TODO and DONE keywords active in a buffer.")
  1402. (make-variable-buffer-local 'org-todo-keywords-1)
  1403. (defvar org-todo-keywords-for-agenda nil)
  1404. (defvar org-done-keywords-for-agenda nil)
  1405. (defvar org-todo-keyword-alist-for-agenda nil)
  1406. (defvar org-tag-alist-for-agenda nil)
  1407. (defvar org-agenda-contributing-files nil)
  1408. (defvar org-not-done-keywords nil)
  1409. (make-variable-buffer-local 'org-not-done-keywords)
  1410. (defvar org-done-keywords nil)
  1411. (make-variable-buffer-local 'org-done-keywords)
  1412. (defvar org-todo-heads nil)
  1413. (make-variable-buffer-local 'org-todo-heads)
  1414. (defvar org-todo-sets nil)
  1415. (make-variable-buffer-local 'org-todo-sets)
  1416. (defvar org-todo-log-states nil)
  1417. (make-variable-buffer-local 'org-todo-log-states)
  1418. (defvar org-todo-kwd-alist nil)
  1419. (make-variable-buffer-local 'org-todo-kwd-alist)
  1420. (defvar org-todo-key-alist nil)
  1421. (make-variable-buffer-local 'org-todo-key-alist)
  1422. (defvar org-todo-key-trigger nil)
  1423. (make-variable-buffer-local 'org-todo-key-trigger)
  1424. (defcustom org-todo-interpretation 'sequence
  1425. "Controls how TODO keywords are interpreted.
  1426. This variable is in principle obsolete and is only used for
  1427. backward compatibility, if the interpretation of todo keywords is
  1428. not given already in `org-todo-keywords'. See that variable for
  1429. more information."
  1430. :group 'org-todo
  1431. :group 'org-keywords
  1432. :type '(choice (const sequence)
  1433. (const type)))
  1434. (defcustom org-use-fast-todo-selection t
  1435. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1436. This variable describes if and under what circumstances the cycling
  1437. mechanism for TODO keywords will be replaced by a single-key, direct
  1438. selection scheme.
  1439. When nil, fast selection is never used.
  1440. When the symbol `prefix', it will be used when `org-todo' is called with
  1441. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1442. in an agenda buffer.
  1443. When t, fast selection is used by default. In this case, the prefix
  1444. argument forces cycling instead.
  1445. In all cases, the special interface is only used if access keys have actually
  1446. been assigned by the user, i.e. if keywords in the configuration are followed
  1447. by a letter in parenthesis, like TODO(t)."
  1448. :group 'org-todo
  1449. :type '(choice
  1450. (const :tag "Never" nil)
  1451. (const :tag "By default" t)
  1452. (const :tag "Only with C-u C-c C-t" prefix)))
  1453. (defcustom org-provide-todo-statistics t
  1454. "Non-nil means, update todo statistics after insert and toggle.
  1455. When this is set, todo statistics is updated in the parent of the current
  1456. entry each time a todo state is changed."
  1457. :group 'org-todo
  1458. :type 'boolean)
  1459. (defcustom org-after-todo-state-change-hook nil
  1460. "Hook which is run after the state of a TODO item was changed.
  1461. The new state (a string with a TODO keyword, or nil) is available in the
  1462. Lisp variable `state'."
  1463. :group 'org-todo
  1464. :type 'hook)
  1465. (defvar org-blocker-hook nil
  1466. "Hook for functions that are allowed to block a state change.
  1467. Each function gets as its single argument a property list, see
  1468. `org-trigger-hook' for more information about this list.
  1469. If any of the functions in this hook returns nil, the state change
  1470. is blocked.")
  1471. (defvar org-trigger-hook nil
  1472. "Hook for functions that are triggered by a state change.
  1473. Each function gets as its single argument a property list with at least
  1474. the following elements:
  1475. (:type type-of-change :position pos-at-entry-start
  1476. :from old-state :to new-state)
  1477. Depending on the type, more properties may be present.
  1478. This mechanism is currently implemented for:
  1479. TODO state changes
  1480. ------------------
  1481. :type todo-state-change
  1482. :from previous state (keyword as a string), or nil, or a symbol
  1483. 'todo' or 'done', to indicate the general type of state.
  1484. :to new state, like in :from")
  1485. (defcustom org-enforce-todo-dependencies nil
  1486. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1487. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1488. be blocked if any prior sibling is not yet done.
  1489. This variable needs to be set before org.el is loaded, and you need to
  1490. restart Emacs after a change to make the change effective. The only way
  1491. to change is while Emacs is running is through the customize interface."
  1492. :set (lambda (var val)
  1493. (set var val)
  1494. (if val
  1495. (add-hook 'org-blocker-hook
  1496. 'org-block-todo-from-children-or-siblings)
  1497. (remove-hook 'org-blocker-hook
  1498. 'org-block-todo-from-children-or-siblings)))
  1499. :group 'org-todo
  1500. :type 'boolean)
  1501. (defcustom org-enforce-todo-checkbox-dependencies nil
  1502. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1503. When this is nil, checkboxes have no influence on switching TODO states.
  1504. When non-nil, you first need to check off all check boxes before the TODO
  1505. entry can be switched to DONE.
  1506. This variable needs to be set before org.el is loaded, and you need to
  1507. restart Emacs after a change to make the change effective. The only way
  1508. to change is while Emacs is running is through the customize interface."
  1509. :set (lambda (var val)
  1510. (set var val)
  1511. (if val
  1512. (add-hook 'org-blocker-hook
  1513. 'org-block-todo-from-checkboxes)
  1514. (remove-hook 'org-blocker-hook
  1515. 'org-block-todo-from-checkboxes)))
  1516. :group 'org-todo
  1517. :type 'boolean)
  1518. (defcustom org-todo-state-tags-triggers nil
  1519. "Tag changes that should be triggered by TODO state changes.
  1520. This is a list. Each entry is
  1521. (state-change (tag . flag) .......)
  1522. State-change can be a string with a state, and empty string to indicate the
  1523. state that has no TODO keyword, or it can be one of the symbols `todo'
  1524. or `done', meaning any not-done or done state, respectively."
  1525. :group 'org-todo
  1526. :group 'org-tags
  1527. :type '(repeat
  1528. (cons (choice :tag "When changing to"
  1529. (const :tag "Not-done state" todo)
  1530. (const :tag "Done state" done)
  1531. (string :tag "State"))
  1532. (repeat
  1533. (cons :tag "Tag action"
  1534. (string :tag "Tag")
  1535. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1536. (defcustom org-log-done nil
  1537. "Information to record when a task moves to the DONE state.
  1538. Possible values are:
  1539. nil Don't add anything, just change the keyword
  1540. time Add a time stamp to the task
  1541. note Prompt a closing note and add it with template `org-log-note-headings'
  1542. This option can also be set with on a per-file-basis with
  1543. #+STARTUP: nologdone
  1544. #+STARTUP: logdone
  1545. #+STARTUP: lognotedone
  1546. You can have local logging settings for a subtree by setting the LOGGING
  1547. property to one or more of these keywords."
  1548. :group 'org-todo
  1549. :group 'org-progress
  1550. :type '(choice
  1551. (const :tag "No logging" nil)
  1552. (const :tag "Record CLOSED timestamp" time)
  1553. (const :tag "Record CLOSED timestamp with closing note." note)))
  1554. ;; Normalize old uses of org-log-done.
  1555. (cond
  1556. ((eq org-log-done t) (setq org-log-done 'time))
  1557. ((and (listp org-log-done) (memq 'done org-log-done))
  1558. (setq org-log-done 'note)))
  1559. (defcustom org-log-note-clock-out nil
  1560. "Non-nil means, record a note when clocking out of an item.
  1561. This can also be configured on a per-file basis by adding one of
  1562. the following lines anywhere in the buffer:
  1563. #+STARTUP: lognoteclock-out
  1564. #+STARTUP: nolognoteclock-out"
  1565. :group 'org-todo
  1566. :group 'org-progress
  1567. :type 'boolean)
  1568. (defcustom org-log-done-with-time t
  1569. "Non-nil means, the CLOSED time stamp will contain date and time.
  1570. When nil, only the date will be recorded."
  1571. :group 'org-progress
  1572. :type 'boolean)
  1573. (defcustom org-log-note-headings
  1574. '((done . "CLOSING NOTE %t")
  1575. (state . "State %-12s from %-12S %t")
  1576. (note . "Note taken on %t")
  1577. (clock-out . ""))
  1578. "Headings for notes added to entries.
  1579. The value is an alist, with the car being a symbol indicating the note
  1580. context, and the cdr is the heading to be used. The heading may also be the
  1581. empty string.
  1582. %t in the heading will be replaced by a time stamp.
  1583. %s will be replaced by the new TODO state, in double quotes.
  1584. %S will be replaced by the old TODO state, in double quotes.
  1585. %u will be replaced by the user name.
  1586. %U will be replaced by the full user name."
  1587. :group 'org-todo
  1588. :group 'org-progress
  1589. :type '(list :greedy t
  1590. (cons (const :tag "Heading when closing an item" done) string)
  1591. (cons (const :tag
  1592. "Heading when changing todo state (todo sequence only)"
  1593. state) string)
  1594. (cons (const :tag "Heading when just taking a note" note) string)
  1595. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1596. (unless (assq 'note org-log-note-headings)
  1597. (push '(note . "%t") org-log-note-headings))
  1598. (defcustom org-log-into-drawer nil
  1599. "Non-nil means, insert state change notes and time stamps into a drawer.
  1600. When nil, state changes notes will be inserted after the headline and
  1601. any scheduling and clock lines, but not inside a drawer.
  1602. The value of this variable should be the name of the drawer to use.
  1603. LOGBOOK is proposed at the default drawer for this purpose, you can
  1604. also set this to a string to define the drawer of your choice.
  1605. A value of t is also allowed, representing \"LOGBOOK\".
  1606. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1607. will be ognored."
  1608. :group 'org-todo
  1609. :group 'org-progress
  1610. :type '(choice
  1611. (const :tag "Not into a drawer" nil)
  1612. (const :tag "LOGBOOK" t)
  1613. (string :tag "Other")))
  1614. (if (fboundp 'defvaralias)
  1615. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1616. (defcustom org-log-state-notes-insert-after-drawers nil
  1617. "Non-nil means, insert state change notes after any drawers in entry.
  1618. Only the drawers that *immediately* follow the headline and the
  1619. deadline/scheduled line are skipped.
  1620. When nil, insert notes right after the heading and perhaps the line
  1621. with deadline/scheduling if present.
  1622. This variable will have no effect if `org-log-into-drawer' is
  1623. set."
  1624. :group 'org-todo
  1625. :group 'org-progress
  1626. :type 'boolean)
  1627. (defcustom org-log-states-order-reversed t
  1628. "Non-nil means, the latest state change note will be directly after heading.
  1629. When nil, the notes will be orderer according to time."
  1630. :group 'org-todo
  1631. :group 'org-progress
  1632. :type 'boolean)
  1633. (defcustom org-log-repeat 'time
  1634. "Non-nil means, record moving through the DONE state when triggering repeat.
  1635. An auto-repeating tasks is immediately switched back to TODO when marked
  1636. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1637. the TODO keyword definition, or recording a closing note by setting
  1638. `org-log-done', there will be no record of the task moving through DONE.
  1639. This variable forces taking a note anyway. Possible values are:
  1640. nil Don't force a record
  1641. time Record a time stamp
  1642. note Record a note
  1643. This option can also be set with on a per-file-basis with
  1644. #+STARTUP: logrepeat
  1645. #+STARTUP: lognoterepeat
  1646. #+STARTUP: nologrepeat
  1647. You can have local logging settings for a subtree by setting the LOGGING
  1648. property to one or more of these keywords."
  1649. :group 'org-todo
  1650. :group 'org-progress
  1651. :type '(choice
  1652. (const :tag "Don't force a record" nil)
  1653. (const :tag "Force recording the DONE state" time)
  1654. (const :tag "Force recording a note with the DONE state" note)))
  1655. (defgroup org-priorities nil
  1656. "Priorities in Org-mode."
  1657. :tag "Org Priorities"
  1658. :group 'org-todo)
  1659. (defcustom org-highest-priority ?A
  1660. "The highest priority of TODO items. A character like ?A, ?B etc.
  1661. Must have a smaller ASCII number than `org-lowest-priority'."
  1662. :group 'org-priorities
  1663. :type 'character)
  1664. (defcustom org-lowest-priority ?C
  1665. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1666. Must have a larger ASCII number than `org-highest-priority'."
  1667. :group 'org-priorities
  1668. :type 'character)
  1669. (defcustom org-default-priority ?B
  1670. "The default priority of TODO items.
  1671. This is the priority an item get if no explicit priority is given."
  1672. :group 'org-priorities
  1673. :type 'character)
  1674. (defcustom org-priority-start-cycle-with-default t
  1675. "Non-nil means, start with default priority when starting to cycle.
  1676. When this is nil, the first step in the cycle will be (depending on the
  1677. command used) one higher or lower that the default priority."
  1678. :group 'org-priorities
  1679. :type 'boolean)
  1680. (defgroup org-time nil
  1681. "Options concerning time stamps and deadlines in Org-mode."
  1682. :tag "Org Time"
  1683. :group 'org)
  1684. (defcustom org-insert-labeled-timestamps-at-point nil
  1685. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1686. When nil, these labeled time stamps are forces into the second line of an
  1687. entry, just after the headline. When scheduling from the global TODO list,
  1688. the time stamp will always be forced into the second line."
  1689. :group 'org-time
  1690. :type 'boolean)
  1691. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1692. "Formats for `format-time-string' which are used for time stamps.
  1693. It is not recommended to change this constant.")
  1694. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1695. "Number of minutes to round time stamps to.
  1696. These are two values, the first applies when first creating a time stamp.
  1697. The second applies when changing it with the commands `S-up' and `S-down'.
  1698. When changing the time stamp, this means that it will change in steps
  1699. of N minutes, as given by the second value.
  1700. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1701. numbers should be factors of 60, so for example 5, 10, 15.
  1702. When this is larger than 1, you can still force an exact time-stamp by using
  1703. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1704. and by using a prefix arg to `S-up/down' to specify the exact number
  1705. of minutes to shift."
  1706. :group 'org-time
  1707. :get '(lambda (var) ; Make sure all entries have 5 elements
  1708. (if (integerp (default-value var))
  1709. (list (default-value var) 5)
  1710. (default-value var)))
  1711. :type '(list
  1712. (integer :tag "when inserting times")
  1713. (integer :tag "when modifying times")))
  1714. ;; Normalize old customizations of this variable.
  1715. (when (integerp org-time-stamp-rounding-minutes)
  1716. (setq org-time-stamp-rounding-minutes
  1717. (list org-time-stamp-rounding-minutes
  1718. org-time-stamp-rounding-minutes)))
  1719. (defcustom org-display-custom-times nil
  1720. "Non-nil means, overlay custom formats over all time stamps.
  1721. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1722. To turn this on on a per-file basis, insert anywhere in the file:
  1723. #+STARTUP: customtime"
  1724. :group 'org-time
  1725. :set 'set-default
  1726. :type 'sexp)
  1727. (make-variable-buffer-local 'org-display-custom-times)
  1728. (defcustom org-time-stamp-custom-formats
  1729. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1730. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1731. These are overlayed over the default ISO format if the variable
  1732. `org-display-custom-times' is set. Time like %H:%M should be at the
  1733. end of the second format."
  1734. :group 'org-time
  1735. :type 'sexp)
  1736. (defun org-time-stamp-format (&optional long inactive)
  1737. "Get the right format for a time string."
  1738. (let ((f (if long (cdr org-time-stamp-formats)
  1739. (car org-time-stamp-formats))))
  1740. (if inactive
  1741. (concat "[" (substring f 1 -1) "]")
  1742. f)))
  1743. (defcustom org-time-clocksum-format "%d:%02d"
  1744. "The format string used when creating CLOCKSUM lines, or when
  1745. org-mode generates a time duration."
  1746. :group 'org-time
  1747. :type 'string)
  1748. (defcustom org-deadline-warning-days 14
  1749. "No. of days before expiration during which a deadline becomes active.
  1750. This variable governs the display in sparse trees and in the agenda.
  1751. When 0 or negative, it means use this number (the absolute value of it)
  1752. even if a deadline has a different individual lead time specified."
  1753. :group 'org-time
  1754. :group 'org-agenda-daily/weekly
  1755. :type 'number)
  1756. (defcustom org-read-date-prefer-future t
  1757. "Non-nil means, assume future for incomplete date input from user.
  1758. This affects the following situations:
  1759. 1. The user gives a day, but no month.
  1760. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1761. read this as the third of *next* month. However, if you enter \"17\",
  1762. it will be considered as *this* month.
  1763. 2. The user gives a month but not a year.
  1764. For example, if it is april and you enter \"feb 2\", this will be read
  1765. as feb 2, *next* year. \"May 5\", however, will be this year.
  1766. Currently this does not work for ISO week specifications.
  1767. When this option is nil, the current month and year will always be used
  1768. as defaults."
  1769. :group 'org-time
  1770. :type 'boolean)
  1771. (defcustom org-read-date-display-live t
  1772. "Non-nil means, display current interpretation of date prompt live.
  1773. This display will be in an overlay, in the minibuffer."
  1774. :group 'org-time
  1775. :type 'boolean)
  1776. (defcustom org-read-date-popup-calendar t
  1777. "Non-nil means, pop up a calendar when prompting for a date.
  1778. In the calendar, the date can be selected with mouse-1. However, the
  1779. minibuffer will also be active, and you can simply enter the date as well.
  1780. When nil, only the minibuffer will be available."
  1781. :group 'org-time
  1782. :type 'boolean)
  1783. (if (fboundp 'defvaralias)
  1784. (defvaralias 'org-popup-calendar-for-date-prompt
  1785. 'org-read-date-popup-calendar))
  1786. (defcustom org-extend-today-until 0
  1787. "The hour when your day really ends. Must be an integer.
  1788. This has influence for the following applications:
  1789. - When switching the agenda to \"today\". It it is still earlier than
  1790. the time given here, the day recognized as TODAY is actually yesterday.
  1791. - When a date is read from the user and it is still before the time given
  1792. here, the current date and time will be assumed to be yesterday, 23:59.
  1793. Also, timestamps inserted in remember templates follow this rule.
  1794. IMPORTANT: This is a feature whose implementation is and likely will
  1795. remain incomplete. Really, it is only here because past midnight seems to
  1796. be the favorite working time of John Wiegley :-)"
  1797. :group 'org-time
  1798. :type 'number)
  1799. (defcustom org-edit-timestamp-down-means-later nil
  1800. "Non-nil means, S-down will increase the time in a time stamp.
  1801. When nil, S-up will increase."
  1802. :group 'org-time
  1803. :type 'boolean)
  1804. (defcustom org-calendar-follow-timestamp-change t
  1805. "Non-nil means, make the calendar window follow timestamp changes.
  1806. When a timestamp is modified and the calendar window is visible, it will be
  1807. moved to the new date."
  1808. :group 'org-time
  1809. :type 'boolean)
  1810. (defgroup org-tags nil
  1811. "Options concerning tags in Org-mode."
  1812. :tag "Org Tags"
  1813. :group 'org)
  1814. (defcustom org-tag-alist nil
  1815. "List of tags allowed in Org-mode files.
  1816. When this list is nil, Org-mode will base TAG input on what is already in the
  1817. buffer.
  1818. The value of this variable is an alist, the car of each entry must be a
  1819. keyword as a string, the cdr may be a character that is used to select
  1820. that tag through the fast-tag-selection interface.
  1821. See the manual for details."
  1822. :group 'org-tags
  1823. :type '(repeat
  1824. (choice
  1825. (cons (string :tag "Tag name")
  1826. (character :tag "Access char"))
  1827. (const :tag "Start radio group" (:startgroup))
  1828. (const :tag "End radio group" (:endgroup))
  1829. (const :tag "New line" (:newline)))))
  1830. (defvar org-file-tags nil
  1831. "List of tags that can be inherited by all entries in the file.
  1832. The tags will be inherited if the variable `org-use-tag-inheritance'
  1833. says they should be.
  1834. This variable is populated from #+TAG lines.")
  1835. (defcustom org-use-fast-tag-selection 'auto
  1836. "Non-nil means, use fast tag selection scheme.
  1837. This is a special interface to select and deselect tags with single keys.
  1838. When nil, fast selection is never used.
  1839. When the symbol `auto', fast selection is used if and only if selection
  1840. characters for tags have been configured, either through the variable
  1841. `org-tag-alist' or through a #+TAGS line in the buffer.
  1842. When t, fast selection is always used and selection keys are assigned
  1843. automatically if necessary."
  1844. :group 'org-tags
  1845. :type '(choice
  1846. (const :tag "Always" t)
  1847. (const :tag "Never" nil)
  1848. (const :tag "When selection characters are configured" 'auto)))
  1849. (defcustom org-fast-tag-selection-single-key nil
  1850. "Non-nil means, fast tag selection exits after first change.
  1851. When nil, you have to press RET to exit it.
  1852. During fast tag selection, you can toggle this flag with `C-c'.
  1853. This variable can also have the value `expert'. In this case, the window
  1854. displaying the tags menu is not even shown, until you press C-c again."
  1855. :group 'org-tags
  1856. :type '(choice
  1857. (const :tag "No" nil)
  1858. (const :tag "Yes" t)
  1859. (const :tag "Expert" expert)))
  1860. (defvar org-fast-tag-selection-include-todo nil
  1861. "Non-nil means, fast tags selection interface will also offer TODO states.
  1862. This is an undocumented feature, you should not rely on it.")
  1863. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1864. "The column to which tags should be indented in a headline.
  1865. If this number is positive, it specifies the column. If it is negative,
  1866. it means that the tags should be flushright to that column. For example,
  1867. -80 works well for a normal 80 character screen."
  1868. :group 'org-tags
  1869. :type 'integer)
  1870. (defcustom org-auto-align-tags t
  1871. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1872. These operations change the length of a headline and therefore shift
  1873. the tags around. With this options turned on, after each such operation
  1874. the tags are again aligned to `org-tags-column'."
  1875. :group 'org-tags
  1876. :type 'boolean)
  1877. (defcustom org-use-tag-inheritance t
  1878. "Non-nil means, tags in levels apply also for sublevels.
  1879. When nil, only the tags directly given in a specific line apply there.
  1880. This may also be a list of tags that should be inherited, or a regexp that
  1881. matches tags that should be inherited. Additional control is possible
  1882. with the variable `org-tags-exclude-from-inheritance' which gives an
  1883. explicit list of tags to be excluded from inheritance., even if the value of
  1884. `org-use-tag-inheritance' would select it for inheritance.
  1885. If this option is t, a match early-on in a tree can lead to a large
  1886. number of matches in the subtree when constructing the agenda or creating
  1887. a sparse tree. If you only want to see the first match in a tree during
  1888. a search, check out the variable `org-tags-match-list-sublevels'."
  1889. :group 'org-tags
  1890. :type '(choice
  1891. (const :tag "Not" nil)
  1892. (const :tag "Always" t)
  1893. (repeat :tag "Specific tags" (string :tag "Tag"))
  1894. (regexp :tag "Tags matched by regexp")))
  1895. (defcustom org-tags-exclude-from-inheritance nil
  1896. "List of tags that should never be inherited.
  1897. This is a way to exclude a few tags from inheritance. For way to do
  1898. the opposite, to actively allow inheritance for selected tags,
  1899. see the variable `org-use-tag-inheritance'."
  1900. :group 'org-tags
  1901. :type '(repeat (string :tag "Tag")))
  1902. (defun org-tag-inherit-p (tag)
  1903. "Check if TAG is one that should be inherited."
  1904. (cond
  1905. ((member tag org-tags-exclude-from-inheritance) nil)
  1906. ((eq org-use-tag-inheritance t) t)
  1907. ((not org-use-tag-inheritance) nil)
  1908. ((stringp org-use-tag-inheritance)
  1909. (string-match org-use-tag-inheritance tag))
  1910. ((listp org-use-tag-inheritance)
  1911. (member tag org-use-tag-inheritance))
  1912. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1913. (defcustom org-tags-match-list-sublevels t
  1914. "Non-nil means list also sublevels of headlines matching tag search.
  1915. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1916. the sublevels of a headline matching a tag search often also match
  1917. the same search. Listing all of them can create very long lists.
  1918. Setting this variable to nil causes subtrees of a match to be skipped.
  1919. This option is off by default, because inheritance in on. If you turn
  1920. inheritance off, you very likely want to turn this option on.
  1921. As a special case, if the tag search is restricted to TODO items, the
  1922. value of this variable is ignored and sublevels are always checked, to
  1923. make sure all corresponding TODO items find their way into the list.
  1924. This variable is semi-obsolete and probably should always be true. It
  1925. is better to limit inheritance to certain tags using the variables
  1926. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1927. :group 'org-tags
  1928. :type 'boolean)
  1929. (defvar org-tags-history nil
  1930. "History of minibuffer reads for tags.")
  1931. (defvar org-last-tags-completion-table nil
  1932. "The last used completion table for tags.")
  1933. (defvar org-after-tags-change-hook nil
  1934. "Hook that is run after the tags in a line have changed.")
  1935. (defgroup org-properties nil
  1936. "Options concerning properties in Org-mode."
  1937. :tag "Org Properties"
  1938. :group 'org)
  1939. (defcustom org-property-format "%-10s %s"
  1940. "How property key/value pairs should be formatted by `indent-line'.
  1941. When `indent-line' hits a property definition, it will format the line
  1942. according to this format, mainly to make sure that the values are
  1943. lined-up with respect to each other."
  1944. :group 'org-properties
  1945. :type 'string)
  1946. (defcustom org-use-property-inheritance nil
  1947. "Non-nil means, properties apply also for sublevels.
  1948. This setting is chiefly used during property searches. Turning it on can
  1949. cause significant overhead when doing a search, which is why it is not
  1950. on by default.
  1951. When nil, only the properties directly given in the current entry count.
  1952. When t, every property is inherited. The value may also be a list of
  1953. properties that should have inheritance, or a regular expression matching
  1954. properties that should be inherited.
  1955. However, note that some special properties use inheritance under special
  1956. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1957. and the properties ending in \"_ALL\" when they are used as descriptor
  1958. for valid values of a property.
  1959. Note for programmers:
  1960. When querying an entry with `org-entry-get', you can control if inheritance
  1961. should be used. By default, `org-entry-get' looks only at the local
  1962. properties. You can request inheritance by setting the inherit argument
  1963. to t (to force inheritance) or to `selective' (to respect the setting
  1964. in this variable)."
  1965. :group 'org-properties
  1966. :type '(choice
  1967. (const :tag "Not" nil)
  1968. (const :tag "Always" t)
  1969. (repeat :tag "Specific properties" (string :tag "Property"))
  1970. (regexp :tag "Properties matched by regexp")))
  1971. (defun org-property-inherit-p (property)
  1972. "Check if PROPERTY is one that should be inherited."
  1973. (cond
  1974. ((eq org-use-property-inheritance t) t)
  1975. ((not org-use-property-inheritance) nil)
  1976. ((stringp org-use-property-inheritance)
  1977. (string-match org-use-property-inheritance property))
  1978. ((listp org-use-property-inheritance)
  1979. (member property org-use-property-inheritance))
  1980. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1981. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1982. "The default column format, if no other format has been defined.
  1983. This variable can be set on the per-file basis by inserting a line
  1984. #+COLUMNS: %25ITEM ....."
  1985. :group 'org-properties
  1986. :type 'string)
  1987. (defcustom org-columns-ellipses ".."
  1988. "The ellipses to be used when a field in column view is truncated.
  1989. When this is the empty string, as many characters as possible are shown,
  1990. but then there will be no visual indication that the field has been truncated.
  1991. When this is a string of length N, the last N characters of a truncated
  1992. field are replaced by this string. If the column is narrower than the
  1993. ellipses string, only part of the ellipses string will be shown."
  1994. :group 'org-properties
  1995. :type 'string)
  1996. (defcustom org-columns-modify-value-for-display-function nil
  1997. "Function that modifies values for display in column view.
  1998. For example, it can be used to cut out a certain part from a time stamp.
  1999. The function must take 2 arguments:
  2000. column-title The title of the column (*not* the property name)
  2001. value The value that should be modified.
  2002. The function should return the value that should be displayed,
  2003. or nil if the normal value should be used."
  2004. :group 'org-properties
  2005. :type 'function)
  2006. (defcustom org-effort-property "Effort"
  2007. "The property that is being used to keep track of effort estimates.
  2008. Effort estimates given in this property need to have the format H:MM."
  2009. :group 'org-properties
  2010. :group 'org-progress
  2011. :type '(string :tag "Property"))
  2012. (defconst org-global-properties-fixed
  2013. '(("VISIBILITY_ALL" . "folded children content all"))
  2014. "List of property/value pairs that can be inherited by any entry.
  2015. These are fixed values, for the preset properties.")
  2016. (defcustom org-global-properties nil
  2017. "List of property/value pairs that can be inherited by any entry.
  2018. You can set buffer-local values for the same purpose in the variable
  2019. `org-file-properties' this by adding lines like
  2020. #+PROPERTY: NAME VALUE"
  2021. :group 'org-properties
  2022. :type '(repeat
  2023. (cons (string :tag "Property")
  2024. (string :tag "Value"))))
  2025. (defvar org-file-properties nil
  2026. "List of property/value pairs that can be inherited by any entry.
  2027. Valid for the current buffer.
  2028. This variable is populated from #+PROPERTY lines.")
  2029. (make-variable-buffer-local 'org-file-properties)
  2030. (defgroup org-agenda nil
  2031. "Options concerning agenda views in Org-mode."
  2032. :tag "Org Agenda"
  2033. :group 'org)
  2034. (defvar org-category nil
  2035. "Variable used by org files to set a category for agenda display.
  2036. Such files should use a file variable to set it, for example
  2037. # -*- mode: org; org-category: \"ELisp\"
  2038. or contain a special line
  2039. #+CATEGORY: ELisp
  2040. If the file does not specify a category, then file's base name
  2041. is used instead.")
  2042. (make-variable-buffer-local 'org-category)
  2043. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2044. (defcustom org-agenda-files nil
  2045. "The files to be used for agenda display.
  2046. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2047. \\[org-remove-file]. You can also use customize to edit the list.
  2048. If an entry is a directory, all files in that directory that are matched by
  2049. `org-agenda-file-regexp' will be part of the file list.
  2050. If the value of the variable is not a list but a single file name, then
  2051. the list of agenda files is actually stored and maintained in that file, one
  2052. agenda file per line."
  2053. :group 'org-agenda
  2054. :type '(choice
  2055. (repeat :tag "List of files and directories" file)
  2056. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2057. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2058. "Regular expression to match files for `org-agenda-files'.
  2059. If any element in the list in that variable contains a directory instead
  2060. of a normal file, all files in that directory that are matched by this
  2061. regular expression will be included."
  2062. :group 'org-agenda
  2063. :type 'regexp)
  2064. (defcustom org-agenda-text-search-extra-files nil
  2065. "List of extra files to be searched by text search commands.
  2066. These files will be search in addition to the agenda files by the
  2067. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2068. Note that these files will only be searched for text search commands,
  2069. not for the other agenda views like todo lists, tag searches or the weekly
  2070. agenda. This variable is intended to list notes and possibly archive files
  2071. that should also be searched by these two commands.
  2072. In fact, if the first element in the list is the symbol `agenda-archives',
  2073. than all archive files of all agenda files will be added to the search
  2074. scope."
  2075. :group 'org-agenda
  2076. :type '(set :greedy t
  2077. (const :tag "Agenda Archives" agenda-archives)
  2078. (repeat :inline t (file))))
  2079. (if (fboundp 'defvaralias)
  2080. (defvaralias 'org-agenda-multi-occur-extra-files
  2081. 'org-agenda-text-search-extra-files))
  2082. (defcustom org-agenda-skip-unavailable-files nil
  2083. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2084. A nil value means to remove them, after a query, from the list."
  2085. :group 'org-agenda
  2086. :type 'boolean)
  2087. (defcustom org-calendar-to-agenda-key [?c]
  2088. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2089. The command `org-calendar-goto-agenda' will be bound to this key. The
  2090. default is the character `c' because then `c' can be used to switch back and
  2091. forth between agenda and calendar."
  2092. :group 'org-agenda
  2093. :type 'sexp)
  2094. (defcustom org-calendar-agenda-action-key [?k]
  2095. "The key to be installed in `calendar-mode-map' for agenda-action.
  2096. The command `org-agenda-action' will be bound to this key. The
  2097. default is the character `k' because we use the same key in the agenda."
  2098. :group 'org-agenda
  2099. :type 'sexp)
  2100. (eval-after-load "calendar"
  2101. '(progn
  2102. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2103. 'org-calendar-goto-agenda)
  2104. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2105. 'org-agenda-action)))
  2106. (defgroup org-latex nil
  2107. "Options for embedding LaTeX code into Org-mode."
  2108. :tag "Org LaTeX"
  2109. :group 'org)
  2110. (defcustom org-format-latex-options
  2111. '(:foreground default :background default :scale 1.0
  2112. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2113. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2114. "Options for creating images from LaTeX fragments.
  2115. This is a property list with the following properties:
  2116. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2117. `default' means use the foreground of the default face.
  2118. :background the background color, or \"Transparent\".
  2119. `default' means use the background of the default face.
  2120. :scale a scaling factor for the size of the images.
  2121. :html-foreground, :html-background, :html-scale
  2122. the same numbers for HTML export.
  2123. :matchers a list indicating which matchers should be used to
  2124. find LaTeX fragments. Valid members of this list are:
  2125. \"begin\" find environments
  2126. \"$1\" find single characters surrounded by $.$
  2127. \"$\" find math expressions surrounded by $...$
  2128. \"$$\" find math expressions surrounded by $$....$$
  2129. \"\\(\" find math expressions surrounded by \\(...\\)
  2130. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2131. :group 'org-latex
  2132. :type 'plist)
  2133. (defcustom org-format-latex-header "\\documentclass{article}
  2134. \\usepackage{fullpage} % do not remove
  2135. \\usepackage{amssymb}
  2136. \\usepackage[usenames]{color}
  2137. \\usepackage{amsmath}
  2138. \\usepackage{latexsym}
  2139. \\usepackage[mathscr]{eucal}
  2140. \\pagestyle{empty} % do not remove"
  2141. "The document header used for processing LaTeX fragments."
  2142. :group 'org-latex
  2143. :type 'string)
  2144. (defgroup org-font-lock nil
  2145. "Font-lock settings for highlighting in Org-mode."
  2146. :tag "Org Font Lock"
  2147. :group 'org)
  2148. (defcustom org-level-color-stars-only nil
  2149. "Non-nil means fontify only the stars in each headline.
  2150. When nil, the entire headline is fontified.
  2151. Changing it requires restart of `font-lock-mode' to become effective
  2152. also in regions already fontified."
  2153. :group 'org-font-lock
  2154. :type 'boolean)
  2155. (defcustom org-hide-leading-stars nil
  2156. "Non-nil means, hide the first N-1 stars in a headline.
  2157. This works by using the face `org-hide' for these stars. This
  2158. face is white for a light background, and black for a dark
  2159. background. You may have to customize the face `org-hide' to
  2160. make this work.
  2161. Changing it requires restart of `font-lock-mode' to become effective
  2162. also in regions already fontified.
  2163. You may also set this on a per-file basis by adding one of the following
  2164. lines to the buffer:
  2165. #+STARTUP: hidestars
  2166. #+STARTUP: showstars"
  2167. :group 'org-font-lock
  2168. :type 'boolean)
  2169. (defcustom org-fontify-done-headline nil
  2170. "Non-nil means, change the face of a headline if it is marked DONE.
  2171. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2172. When this is non-nil, the headline after the keyword is set to the
  2173. `org-headline-done' as an additional indication."
  2174. :group 'org-font-lock
  2175. :type 'boolean)
  2176. (defcustom org-fontify-emphasized-text t
  2177. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2178. Changing this variable requires a restart of Emacs to take effect."
  2179. :group 'org-font-lock
  2180. :type 'boolean)
  2181. (defcustom org-highlight-latex-fragments-and-specials nil
  2182. "Non-nil means, fontify what is treated specially by the exporters."
  2183. :group 'org-font-lock
  2184. :type 'boolean)
  2185. (defcustom org-hide-emphasis-markers nil
  2186. "Non-nil mean font-lock should hide the emphasis marker characters."
  2187. :group 'org-font-lock
  2188. :type 'boolean)
  2189. (defvar org-emph-re nil
  2190. "Regular expression for matching emphasis.")
  2191. (defvar org-verbatim-re nil
  2192. "Regular expression for matching verbatim text.")
  2193. (defvar org-emphasis-regexp-components) ; defined just below
  2194. (defvar org-emphasis-alist) ; defined just below
  2195. (defun org-set-emph-re (var val)
  2196. "Set variable and compute the emphasis regular expression."
  2197. (set var val)
  2198. (when (and (boundp 'org-emphasis-alist)
  2199. (boundp 'org-emphasis-regexp-components)
  2200. org-emphasis-alist org-emphasis-regexp-components)
  2201. (let* ((e org-emphasis-regexp-components)
  2202. (pre (car e))
  2203. (post (nth 1 e))
  2204. (border (nth 2 e))
  2205. (body (nth 3 e))
  2206. (nl (nth 4 e))
  2207. (body1 (concat body "*?"))
  2208. (markers (mapconcat 'car org-emphasis-alist ""))
  2209. (vmarkers (mapconcat
  2210. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2211. org-emphasis-alist "")))
  2212. ;; make sure special characters appear at the right position in the class
  2213. (if (string-match "\\^" markers)
  2214. (setq markers (concat (replace-match "" t t markers) "^")))
  2215. (if (string-match "-" markers)
  2216. (setq markers (concat (replace-match "" t t markers) "-")))
  2217. (if (string-match "\\^" vmarkers)
  2218. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2219. (if (string-match "-" vmarkers)
  2220. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2221. (if (> nl 0)
  2222. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2223. (int-to-string nl) "\\}")))
  2224. ;; Make the regexp
  2225. (setq org-emph-re
  2226. (concat "\\([" pre "]\\|^\\)"
  2227. "\\("
  2228. "\\([" markers "]\\)"
  2229. "\\("
  2230. "[^" border "]\\|"
  2231. "[^" border "]"
  2232. body1
  2233. "[^" border "]"
  2234. "\\)"
  2235. "\\3\\)"
  2236. "\\([" post "]\\|$\\)"))
  2237. (setq org-verbatim-re
  2238. (concat "\\([" pre "]\\|^\\)"
  2239. "\\("
  2240. "\\([" vmarkers "]\\)"
  2241. "\\("
  2242. "[^" border "]\\|"
  2243. "[^" border "]"
  2244. body1
  2245. "[^" border "]"
  2246. "\\)"
  2247. "\\3\\)"
  2248. "\\([" post "]\\|$\\)")))))
  2249. (defcustom org-emphasis-regexp-components
  2250. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2251. "Components used to build the regular expression for emphasis.
  2252. This is a list with 6 entries. Terminology: In an emphasis string
  2253. like \" *strong word* \", we call the initial space PREMATCH, the final
  2254. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2255. and \"trong wor\" is the body. The different components in this variable
  2256. specify what is allowed/forbidden in each part:
  2257. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2258. post Chars allowed as postmatch. End of line will be allowed too.
  2259. border The chars *forbidden* as border characters.
  2260. body-regexp A regexp like \".\" to match a body character. Don't use
  2261. non-shy groups here, and don't allow newline here.
  2262. newline The maximum number of newlines allowed in an emphasis exp.
  2263. Use customize to modify this, or restart Emacs after changing it."
  2264. :group 'org-font-lock
  2265. :set 'org-set-emph-re
  2266. :type '(list
  2267. (sexp :tag "Allowed chars in pre ")
  2268. (sexp :tag "Allowed chars in post ")
  2269. (sexp :tag "Forbidden chars in border ")
  2270. (sexp :tag "Regexp for body ")
  2271. (integer :tag "number of newlines allowed")
  2272. (option (boolean :tag "Please ignore this button"))))
  2273. (defcustom org-emphasis-alist
  2274. `(("*" bold "<b>" "</b>")
  2275. ("/" italic "<i>" "</i>")
  2276. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2277. ("=" org-code "<code>" "</code>" verbatim)
  2278. ("~" org-verbatim "<code>" "</code>" verbatim)
  2279. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2280. "<del>" "</del>")
  2281. )
  2282. "Special syntax for emphasized text.
  2283. Text starting and ending with a special character will be emphasized, for
  2284. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2285. characters, the face to be used by font-lock for highlighting in Org-mode
  2286. Emacs buffers, and the HTML tags to be used for this.
  2287. Use customize to modify this, or restart Emacs after changing it."
  2288. :group 'org-font-lock
  2289. :set 'org-set-emph-re
  2290. :type '(repeat
  2291. (list
  2292. (string :tag "Marker character")
  2293. (choice
  2294. (face :tag "Font-lock-face")
  2295. (plist :tag "Face property list"))
  2296. (string :tag "HTML start tag")
  2297. (string :tag "HTML end tag")
  2298. (option (const verbatim)))))
  2299. ;;; Miscellaneous options
  2300. (defgroup org-completion nil
  2301. "Completion in Org-mode."
  2302. :tag "Org Completion"
  2303. :group 'org)
  2304. (defcustom org-completion-use-ido nil
  2305. "Non-nil means, use ido completion wherever possible.
  2306. Note that `ido-mode' must be active for this variable to be relevant.
  2307. If you decide to turn this variable on, you might well want to turn off
  2308. `org-outline-path-complete-in-steps'."
  2309. :group 'org-completion
  2310. :type 'boolean)
  2311. (defcustom org-completion-fallback-command 'hippie-expand
  2312. "The expansion command called by \\[org-complete] in normal context.
  2313. Normal means, no org-mode-specific context."
  2314. :group 'org-completion
  2315. :type 'function)
  2316. ;;; Functions and variables from ther packages
  2317. ;; Declared here to avoid compiler warnings
  2318. ;; XEmacs only
  2319. (defvar outline-mode-menu-heading)
  2320. (defvar outline-mode-menu-show)
  2321. (defvar outline-mode-menu-hide)
  2322. (defvar zmacs-regions) ; XEmacs regions
  2323. ;; Emacs only
  2324. (defvar mark-active)
  2325. ;; Various packages
  2326. (declare-function find-library-name "find-func" (library))
  2327. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2328. (declare-function calendar-forward-day "cal-move" (arg))
  2329. (declare-function calendar-goto-date "cal-move" (date))
  2330. (declare-function calendar-goto-today "cal-move" ())
  2331. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2332. (defvar calc-embedded-close-formula)
  2333. (defvar calc-embedded-open-formula)
  2334. (declare-function cdlatex-tab "ext:cdlatex" ())
  2335. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2336. (defvar font-lock-unfontify-region-function)
  2337. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2338. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2339. (defvar iswitchb-temp-buflist)
  2340. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2341. (defvar org-agenda-tags-todo-honor-ignore-options)
  2342. (declare-function org-agenda-skip "org-agenda" ())
  2343. (declare-function org-format-agenda-item "org-agenda"
  2344. (extra txt &optional category tags dotime noprefix remove-re))
  2345. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2346. (declare-function org-agenda-change-all-lines "org-agenda"
  2347. (newhead hdmarker &optional fixface just-this))
  2348. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2349. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2350. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2351. (beg end))
  2352. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2353. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2354. "org-agenda" (&optional end))
  2355. (declare-function parse-time-string "parse-time" (string))
  2356. (declare-function remember "remember" (&optional initial))
  2357. (declare-function remember-buffer-desc "remember" ())
  2358. (declare-function remember-finalize "remember" ())
  2359. (defvar remember-save-after-remembering)
  2360. (defvar remember-data-file)
  2361. (defvar remember-register)
  2362. (defvar remember-buffer)
  2363. (defvar remember-handler-functions)
  2364. (defvar remember-annotation-functions)
  2365. (defvar texmathp-why)
  2366. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2367. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2368. (defvar w3m-current-url)
  2369. (defvar w3m-current-title)
  2370. (defvar org-latex-regexps)
  2371. ;;; Autoload and prepare some org modules
  2372. ;; Some table stuff that needs to be defined here, because it is used
  2373. ;; by the functions setting up org-mode or checking for table context.
  2374. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2375. "Detects an org-type or table-type table.")
  2376. (defconst org-table-line-regexp "^[ \t]*|"
  2377. "Detects an org-type table line.")
  2378. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2379. "Detects an org-type table line.")
  2380. (defconst org-table-hline-regexp "^[ \t]*|-"
  2381. "Detects an org-type table hline.")
  2382. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2383. "Detects a table-type table hline.")
  2384. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2385. "Searching from within a table (any type) this finds the first line
  2386. outside the table.")
  2387. ;; Autoload the functions in org-table.el that are needed by functions here.
  2388. (eval-and-compile
  2389. (org-autoload "org-table"
  2390. '(org-table-align org-table-begin org-table-blank-field
  2391. org-table-convert org-table-convert-region org-table-copy-down
  2392. org-table-copy-region org-table-create
  2393. org-table-create-or-convert-from-region
  2394. org-table-create-with-table.el org-table-current-dline
  2395. org-table-cut-region org-table-delete-column org-table-edit-field
  2396. org-table-edit-formulas org-table-end org-table-eval-formula
  2397. org-table-export org-table-field-info
  2398. org-table-get-stored-formulas org-table-goto-column
  2399. org-table-hline-and-move org-table-import org-table-insert-column
  2400. org-table-insert-hline org-table-insert-row org-table-iterate
  2401. org-table-justify-field-maybe org-table-kill-row
  2402. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2403. org-table-move-column org-table-move-column-left
  2404. org-table-move-column-right org-table-move-row
  2405. org-table-move-row-down org-table-move-row-up
  2406. org-table-next-field org-table-next-row org-table-paste-rectangle
  2407. org-table-previous-field org-table-recalculate
  2408. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2409. org-table-toggle-coordinate-overlays
  2410. org-table-toggle-formula-debugger org-table-wrap-region
  2411. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2412. (defun org-at-table-p (&optional table-type)
  2413. "Return t if the cursor is inside an org-type table.
  2414. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2415. (if org-enable-table-editor
  2416. (save-excursion
  2417. (beginning-of-line 1)
  2418. (looking-at (if table-type org-table-any-line-regexp
  2419. org-table-line-regexp)))
  2420. nil))
  2421. (defsubst org-table-p () (org-at-table-p))
  2422. (defun org-at-table.el-p ()
  2423. "Return t if and only if we are at a table.el table."
  2424. (and (org-at-table-p 'any)
  2425. (save-excursion
  2426. (goto-char (org-table-begin 'any))
  2427. (looking-at org-table1-hline-regexp))))
  2428. (defun org-table-recognize-table.el ()
  2429. "If there is a table.el table nearby, recognize it and move into it."
  2430. (if org-table-tab-recognizes-table.el
  2431. (if (org-at-table.el-p)
  2432. (progn
  2433. (beginning-of-line 1)
  2434. (if (looking-at org-table-dataline-regexp)
  2435. nil
  2436. (if (looking-at org-table1-hline-regexp)
  2437. (progn
  2438. (beginning-of-line 2)
  2439. (if (looking-at org-table-any-border-regexp)
  2440. (beginning-of-line -1)))))
  2441. (if (re-search-forward "|" (org-table-end t) t)
  2442. (progn
  2443. (require 'table)
  2444. (if (table--at-cell-p (point))
  2445. t
  2446. (message "recognizing table.el table...")
  2447. (table-recognize-table)
  2448. (message "recognizing table.el table...done")))
  2449. (error "This should not happen..."))
  2450. t)
  2451. nil)
  2452. nil))
  2453. (defun org-at-table-hline-p ()
  2454. "Return t if the cursor is inside a hline in a table."
  2455. (if org-enable-table-editor
  2456. (save-excursion
  2457. (beginning-of-line 1)
  2458. (looking-at org-table-hline-regexp))
  2459. nil))
  2460. (defvar org-table-clean-did-remove-column nil)
  2461. (defun org-table-map-tables (function)
  2462. "Apply FUNCTION to the start of all tables in the buffer."
  2463. (save-excursion
  2464. (save-restriction
  2465. (widen)
  2466. (goto-char (point-min))
  2467. (while (re-search-forward org-table-any-line-regexp nil t)
  2468. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2469. (beginning-of-line 1)
  2470. (if (looking-at org-table-line-regexp)
  2471. (save-excursion (funcall function)))
  2472. (re-search-forward org-table-any-border-regexp nil 1))))
  2473. (message "Mapping tables: done"))
  2474. ;; Declare and autoload functions from org-exp.el
  2475. (declare-function org-default-export-plist "org-exp")
  2476. (declare-function org-infile-export-plist "org-exp")
  2477. (declare-function org-get-current-options "org-exp")
  2478. (eval-and-compile
  2479. (org-autoload "org-exp"
  2480. '(org-export org-export-as-ascii org-export-visible
  2481. org-insert-export-options-template org-export-as-html-and-open
  2482. org-export-as-html-batch org-export-as-html-to-buffer
  2483. org-replace-region-by-html org-export-region-as-html
  2484. org-export-as-html org-export-icalendar-this-file
  2485. org-export-icalendar-all-agenda-files
  2486. org-table-clean-before-export
  2487. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2488. ;; Declare and autoload functions from org-agenda.el
  2489. (eval-and-compile
  2490. (org-autoload "org-agenda"
  2491. '(org-agenda org-agenda-list org-search-view
  2492. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2493. org-diary org-agenda-to-appt
  2494. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2495. ;; Autoload org-remember
  2496. (eval-and-compile
  2497. (org-autoload "org-remember"
  2498. '(org-remember-insinuate org-remember-annotation
  2499. org-remember-apply-template org-remember org-remember-handler)))
  2500. ;; Autoload org-clock.el
  2501. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2502. (beg end))
  2503. (declare-function org-clock-update-mode-line "org-clock" ())
  2504. (defvar org-clock-start-time)
  2505. (defvar org-clock-marker (make-marker)
  2506. "Marker recording the last clock-in.")
  2507. (eval-and-compile
  2508. (org-autoload
  2509. "org-clock"
  2510. '(org-clock-in org-clock-out org-clock-cancel
  2511. org-clock-goto org-clock-sum org-clock-display
  2512. org-clock-remove-overlays org-clock-report
  2513. org-clocktable-shift org-dblock-write:clocktable
  2514. org-get-clocktable)))
  2515. (defun org-clock-update-time-maybe ()
  2516. "If this is a CLOCK line, update it and return t.
  2517. Otherwise, return nil."
  2518. (interactive)
  2519. (save-excursion
  2520. (beginning-of-line 1)
  2521. (skip-chars-forward " \t")
  2522. (when (looking-at org-clock-string)
  2523. (let ((re (concat "[ \t]*" org-clock-string
  2524. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2525. "\\([ \t]*=>.*\\)?\\)?"))
  2526. ts te h m s neg)
  2527. (cond
  2528. ((not (looking-at re))
  2529. nil)
  2530. ((not (match-end 2))
  2531. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2532. (> org-clock-marker (point))
  2533. (<= org-clock-marker (point-at-eol)))
  2534. ;; The clock is running here
  2535. (setq org-clock-start-time
  2536. (apply 'encode-time
  2537. (org-parse-time-string (match-string 1))))
  2538. (org-clock-update-mode-line)))
  2539. (t
  2540. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2541. (end-of-line 1)
  2542. (setq ts (match-string 1)
  2543. te (match-string 3))
  2544. (setq s (- (time-to-seconds
  2545. (apply 'encode-time (org-parse-time-string te)))
  2546. (time-to-seconds
  2547. (apply 'encode-time (org-parse-time-string ts))))
  2548. neg (< s 0)
  2549. s (abs s)
  2550. h (floor (/ s 3600))
  2551. s (- s (* 3600 h))
  2552. m (floor (/ s 60))
  2553. s (- s (* 60 s)))
  2554. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2555. t))))))
  2556. (defun org-check-running-clock ()
  2557. "Check if the current buffer contains the running clock.
  2558. If yes, offer to stop it and to save the buffer with the changes."
  2559. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2560. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2561. (buffer-name))))
  2562. (org-clock-out)
  2563. (when (y-or-n-p "Save changed buffer?")
  2564. (save-buffer))))
  2565. (defun org-clocktable-try-shift (dir n)
  2566. "Check if this line starts a clock table, if yes, shift the time block."
  2567. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2568. (org-clocktable-shift dir n)))
  2569. ;; Autoload org-timer.el
  2570. ;(declare-function org-timer "org-timer")
  2571. (eval-and-compile
  2572. (org-autoload
  2573. "org-timer"
  2574. '(org-timer-start org-timer org-timer-item
  2575. org-timer-change-times-in-region)))
  2576. ;; Autoload archiving code
  2577. ;; The stuff that is needed for cycling and tags has to be defined here.
  2578. (defgroup org-archive nil
  2579. "Options concerning archiving in Org-mode."
  2580. :tag "Org Archive"
  2581. :group 'org-structure)
  2582. (defcustom org-archive-location "%s_archive::"
  2583. "The location where subtrees should be archived.
  2584. The value of this variable is a string, consisting of two parts,
  2585. separated by a double-colon. The first part is a filename and
  2586. the second part is a headline.
  2587. When the filename is omitted, archiving happens in the same file.
  2588. %s in the filename will be replaced by the current file
  2589. name (without the directory part). Archiving to a different file
  2590. is useful to keep archived entries from contributing to the
  2591. Org-mode Agenda.
  2592. The archived entries will be filed as subtrees of the specified
  2593. headline. When the headline is omitted, the subtrees are simply
  2594. filed away at the end of the file, as top-level entries. Also in
  2595. the heading you can use %s to represent the file name, this can be
  2596. useful when using the same archive for a number of different files.
  2597. Here are a few examples:
  2598. \"%s_archive::\"
  2599. If the current file is Projects.org, archive in file
  2600. Projects.org_archive, as top-level trees. This is the default.
  2601. \"::* Archived Tasks\"
  2602. Archive in the current file, under the top-level headline
  2603. \"* Archived Tasks\".
  2604. \"~/org/archive.org::\"
  2605. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2606. \"~/org/archive.org::From %s\"
  2607. Archive in file ~/org/archive.org (absolute path), und headlines
  2608. \"From FILENAME\" where file name is the current file name.
  2609. \"basement::** Finished Tasks\"
  2610. Archive in file ./basement (relative path), as level 3 trees
  2611. below the level 2 heading \"** Finished Tasks\".
  2612. You may set this option on a per-file basis by adding to the buffer a
  2613. line like
  2614. #+ARCHIVE: basement::** Finished Tasks
  2615. You may also define it locally for a subtree by setting an ARCHIVE property
  2616. in the entry. If such a property is found in an entry, or anywhere up
  2617. the hierarchy, it will be used."
  2618. :group 'org-archive
  2619. :type 'string)
  2620. (defcustom org-archive-tag "ARCHIVE"
  2621. "The tag that marks a subtree as archived.
  2622. An archived subtree does not open during visibility cycling, and does
  2623. not contribute to the agenda listings.
  2624. After changing this, font-lock must be restarted in the relevant buffers to
  2625. get the proper fontification."
  2626. :group 'org-archive
  2627. :group 'org-keywords
  2628. :type 'string)
  2629. (defcustom org-agenda-skip-archived-trees t
  2630. "Non-nil means, the agenda will skip any items located in archived trees.
  2631. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2632. variable is no longer recommended, you should leave it at the value t.
  2633. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2634. :group 'org-archive
  2635. :group 'org-agenda-skip
  2636. :type 'boolean)
  2637. (defcustom org-cycle-open-archived-trees nil
  2638. "Non-nil means, `org-cycle' will open archived trees.
  2639. An archived tree is a tree marked with the tag ARCHIVE.
  2640. When nil, archived trees will stay folded. You can still open them with
  2641. normal outline commands like `show-all', but not with the cycling commands."
  2642. :group 'org-archive
  2643. :group 'org-cycle
  2644. :type 'boolean)
  2645. (defcustom org-sparse-tree-open-archived-trees nil
  2646. "Non-nil means sparse tree construction shows matches in archived trees.
  2647. When nil, matches in these trees are highlighted, but the trees are kept in
  2648. collapsed state."
  2649. :group 'org-archive
  2650. :group 'org-sparse-trees
  2651. :type 'boolean)
  2652. (defun org-cycle-hide-archived-subtrees (state)
  2653. "Re-hide all archived subtrees after a visibility state change."
  2654. (when (and (not org-cycle-open-archived-trees)
  2655. (not (memq state '(overview folded))))
  2656. (save-excursion
  2657. (let* ((globalp (memq state '(contents all)))
  2658. (beg (if globalp (point-min) (point)))
  2659. (end (if globalp (point-max) (org-end-of-subtree t))))
  2660. (org-hide-archived-subtrees beg end)
  2661. (goto-char beg)
  2662. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2663. (message "%s" (substitute-command-keys
  2664. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2665. (defun org-force-cycle-archived ()
  2666. "Cycle subtree even if it is archived."
  2667. (interactive)
  2668. (setq this-command 'org-cycle)
  2669. (let ((org-cycle-open-archived-trees t))
  2670. (call-interactively 'org-cycle)))
  2671. (defun org-hide-archived-subtrees (beg end)
  2672. "Re-hide all archived subtrees after a visibility state change."
  2673. (save-excursion
  2674. (let* ((re (concat ":" org-archive-tag ":")))
  2675. (goto-char beg)
  2676. (while (re-search-forward re end t)
  2677. (and (org-on-heading-p) (hide-subtree))
  2678. (org-end-of-subtree t)))))
  2679. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2680. (eval-and-compile
  2681. (org-autoload "org-archive"
  2682. '(org-add-archive-files org-archive-subtree
  2683. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2684. ;; Autoload Column View Code
  2685. (declare-function org-columns-number-to-string "org-colview")
  2686. (declare-function org-columns-get-format-and-top-level "org-colview")
  2687. (declare-function org-columns-compute "org-colview")
  2688. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2689. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2690. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2691. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2692. ;; Autoload ID code
  2693. (declare-function org-id-store-link "org-id")
  2694. (declare-function org-id-locations-load "org-id")
  2695. (declare-function org-id-locations-save "org-id")
  2696. (defvar org-id-track-globally)
  2697. (org-autoload "org-id"
  2698. '(org-id-get-create org-id-new org-id-copy org-id-get
  2699. org-id-get-with-outline-path-completion
  2700. org-id-get-with-outline-drilling
  2701. org-id-goto org-id-find org-id-store-link))
  2702. ;; Autoload Plotting Code
  2703. (org-autoload "org-plot"
  2704. '(org-plot/gnuplot))
  2705. ;;; Variables for pre-computed regular expressions, all buffer local
  2706. (defvar org-drawer-regexp nil
  2707. "Matches first line of a hidden block.")
  2708. (make-variable-buffer-local 'org-drawer-regexp)
  2709. (defvar org-todo-regexp nil
  2710. "Matches any of the TODO state keywords.")
  2711. (make-variable-buffer-local 'org-todo-regexp)
  2712. (defvar org-not-done-regexp nil
  2713. "Matches any of the TODO state keywords except the last one.")
  2714. (make-variable-buffer-local 'org-not-done-regexp)
  2715. (defvar org-todo-line-regexp nil
  2716. "Matches a headline and puts TODO state into group 2 if present.")
  2717. (make-variable-buffer-local 'org-todo-line-regexp)
  2718. (defvar org-complex-heading-regexp nil
  2719. "Matches a headline and puts everything into groups:
  2720. group 1: the stars
  2721. group 2: The todo keyword, maybe
  2722. group 3: Priority cookie
  2723. group 4: True headline
  2724. group 5: Tags")
  2725. (make-variable-buffer-local 'org-complex-heading-regexp)
  2726. (defvar org-todo-line-tags-regexp nil
  2727. "Matches a headline and puts TODO state into group 2 if present.
  2728. Also put tags into group 4 if tags are present.")
  2729. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2730. (defvar org-nl-done-regexp nil
  2731. "Matches newline followed by a headline with the DONE keyword.")
  2732. (make-variable-buffer-local 'org-nl-done-regexp)
  2733. (defvar org-looking-at-done-regexp nil
  2734. "Matches the DONE keyword a point.")
  2735. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2736. (defvar org-ds-keyword-length 12
  2737. "Maximum length of the Deadline and SCHEDULED keywords.")
  2738. (make-variable-buffer-local 'org-ds-keyword-length)
  2739. (defvar org-deadline-regexp nil
  2740. "Matches the DEADLINE keyword.")
  2741. (make-variable-buffer-local 'org-deadline-regexp)
  2742. (defvar org-deadline-time-regexp nil
  2743. "Matches the DEADLINE keyword together with a time stamp.")
  2744. (make-variable-buffer-local 'org-deadline-time-regexp)
  2745. (defvar org-deadline-line-regexp nil
  2746. "Matches the DEADLINE keyword and the rest of the line.")
  2747. (make-variable-buffer-local 'org-deadline-line-regexp)
  2748. (defvar org-scheduled-regexp nil
  2749. "Matches the SCHEDULED keyword.")
  2750. (make-variable-buffer-local 'org-scheduled-regexp)
  2751. (defvar org-scheduled-time-regexp nil
  2752. "Matches the SCHEDULED keyword together with a time stamp.")
  2753. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2754. (defvar org-closed-time-regexp nil
  2755. "Matches the CLOSED keyword together with a time stamp.")
  2756. (make-variable-buffer-local 'org-closed-time-regexp)
  2757. (defvar org-keyword-time-regexp nil
  2758. "Matches any of the 4 keywords, together with the time stamp.")
  2759. (make-variable-buffer-local 'org-keyword-time-regexp)
  2760. (defvar org-keyword-time-not-clock-regexp nil
  2761. "Matches any of the 3 keywords, together with the time stamp.")
  2762. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2763. (defvar org-maybe-keyword-time-regexp nil
  2764. "Matches a timestamp, possibly preceeded by a keyword.")
  2765. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2766. (defvar org-planning-or-clock-line-re nil
  2767. "Matches a line with planning or clock info.")
  2768. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2769. (defconst org-plain-time-of-day-regexp
  2770. (concat
  2771. "\\(\\<[012]?[0-9]"
  2772. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2773. "\\(--?"
  2774. "\\(\\<[012]?[0-9]"
  2775. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2776. "\\)?")
  2777. "Regular expression to match a plain time or time range.
  2778. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2779. groups carry important information:
  2780. 0 the full match
  2781. 1 the first time, range or not
  2782. 8 the second time, if it is a range.")
  2783. (defconst org-plain-time-extension-regexp
  2784. (concat
  2785. "\\(\\<[012]?[0-9]"
  2786. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2787. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2788. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2789. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2790. groups carry important information:
  2791. 0 the full match
  2792. 7 hours of duration
  2793. 9 minutes of duration")
  2794. (defconst org-stamp-time-of-day-regexp
  2795. (concat
  2796. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2797. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2798. "\\(--?"
  2799. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2800. "Regular expression to match a timestamp time or time range.
  2801. After a match, the following groups carry important information:
  2802. 0 the full match
  2803. 1 date plus weekday, for backreferencing to make sure both times on same day
  2804. 2 the first time, range or not
  2805. 4 the second time, if it is a range.")
  2806. (defconst org-startup-options
  2807. '(("fold" org-startup-folded t)
  2808. ("overview" org-startup-folded t)
  2809. ("nofold" org-startup-folded nil)
  2810. ("showall" org-startup-folded nil)
  2811. ("content" org-startup-folded content)
  2812. ("hidestars" org-hide-leading-stars t)
  2813. ("showstars" org-hide-leading-stars nil)
  2814. ("odd" org-odd-levels-only t)
  2815. ("oddeven" org-odd-levels-only nil)
  2816. ("align" org-startup-align-all-tables t)
  2817. ("noalign" org-startup-align-all-tables nil)
  2818. ("customtime" org-display-custom-times t)
  2819. ("logdone" org-log-done time)
  2820. ("lognotedone" org-log-done note)
  2821. ("nologdone" org-log-done nil)
  2822. ("lognoteclock-out" org-log-note-clock-out t)
  2823. ("nolognoteclock-out" org-log-note-clock-out nil)
  2824. ("logrepeat" org-log-repeat state)
  2825. ("lognoterepeat" org-log-repeat note)
  2826. ("nologrepeat" org-log-repeat nil)
  2827. ("fninline" org-footnote-define-inline t)
  2828. ("nofninline" org-footnote-define-inline nil)
  2829. ("fnlocal" org-footnote-section nil)
  2830. ("fnauto" org-footnote-auto-label t)
  2831. ("fnprompt" org-footnote-auto-label nil)
  2832. ("fnconfirm" org-footnote-auto-label confirm)
  2833. ("fnplain" org-footnote-auto-label plain)
  2834. ("constcgs" constants-unit-system cgs)
  2835. ("constSI" constants-unit-system SI))
  2836. "Variable associated with STARTUP options for org-mode.
  2837. Each element is a list of three items: The startup options as written
  2838. in the #+STARTUP line, the corresponding variable, and the value to
  2839. set this variable to if the option is found. An optional forth element PUSH
  2840. means to push this value onto the list in the variable.")
  2841. (defun org-set-regexps-and-options ()
  2842. "Precompute regular expressions for current buffer."
  2843. (when (org-mode-p)
  2844. (org-set-local 'org-todo-kwd-alist nil)
  2845. (org-set-local 'org-todo-key-alist nil)
  2846. (org-set-local 'org-todo-key-trigger nil)
  2847. (org-set-local 'org-todo-keywords-1 nil)
  2848. (org-set-local 'org-done-keywords nil)
  2849. (org-set-local 'org-todo-heads nil)
  2850. (org-set-local 'org-todo-sets nil)
  2851. (org-set-local 'org-todo-log-states nil)
  2852. (org-set-local 'org-file-properties nil)
  2853. (org-set-local 'org-file-tags nil)
  2854. (let ((re (org-make-options-regexp
  2855. '("CATEGORY" "TODO" "COLUMNS"
  2856. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2857. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2858. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2859. (splitre "[ \t]+")
  2860. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2861. tail sep kws1 prio props ftags drawers
  2862. ext-setup-or-nil setup-contents (start 0))
  2863. (save-excursion
  2864. (save-restriction
  2865. (widen)
  2866. (goto-char (point-min))
  2867. (while (or (and ext-setup-or-nil
  2868. (string-match re ext-setup-or-nil start)
  2869. (setq start (match-end 0)))
  2870. (and (setq ext-setup-or-nil nil start 0)
  2871. (re-search-forward re nil t)))
  2872. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2873. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2874. (cond
  2875. ((equal key "CATEGORY")
  2876. (if (string-match "[ \t]+$" value)
  2877. (setq value (replace-match "" t t value)))
  2878. (setq cat value))
  2879. ((member key '("SEQ_TODO" "TODO"))
  2880. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2881. ((equal key "TYP_TODO")
  2882. (push (cons 'type (org-split-string value splitre)) kwds))
  2883. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2884. ;; general TODO-like setup
  2885. (push (cons (intern (downcase (match-string 1 key)))
  2886. (org-split-string value splitre)) kwds))
  2887. ((equal key "TAGS")
  2888. (setq tags (append tags (if tags '("\\n") nil)
  2889. (org-split-string value splitre))))
  2890. ((equal key "COLUMNS")
  2891. (org-set-local 'org-columns-default-format value))
  2892. ((equal key "LINK")
  2893. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2894. (push (cons (match-string 1 value)
  2895. (org-trim (match-string 2 value)))
  2896. links)))
  2897. ((equal key "PRIORITIES")
  2898. (setq prio (org-split-string value " +")))
  2899. ((equal key "PROPERTY")
  2900. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2901. (push (cons (match-string 1 value) (match-string 2 value))
  2902. props)))
  2903. ((equal key "FILETAGS")
  2904. (when (string-match "\\S-" value)
  2905. (setq ftags
  2906. (append
  2907. ftags
  2908. (apply 'append
  2909. (mapcar (lambda (x) (org-split-string x ":"))
  2910. (org-split-string value)))))))
  2911. ((equal key "DRAWERS")
  2912. (setq drawers (org-split-string value splitre)))
  2913. ((equal key "CONSTANTS")
  2914. (setq const (append const (org-split-string value splitre))))
  2915. ((equal key "STARTUP")
  2916. (let ((opts (org-split-string value splitre))
  2917. l var val)
  2918. (while (setq l (pop opts))
  2919. (when (setq l (assoc l org-startup-options))
  2920. (setq var (nth 1 l) val (nth 2 l))
  2921. (if (not (nth 3 l))
  2922. (set (make-local-variable var) val)
  2923. (if (not (listp (symbol-value var)))
  2924. (set (make-local-variable var) nil))
  2925. (set (make-local-variable var) (symbol-value var))
  2926. (add-to-list var val))))))
  2927. ((equal key "ARCHIVE")
  2928. (string-match " *$" value)
  2929. (setq arch (replace-match "" t t value))
  2930. (remove-text-properties 0 (length arch)
  2931. '(face t fontified t) arch))
  2932. ((equal key "SETUPFILE")
  2933. (setq setup-contents (org-file-contents
  2934. (expand-file-name
  2935. (org-remove-double-quotes value))
  2936. 'noerror))
  2937. (if (not ext-setup-or-nil)
  2938. (setq ext-setup-or-nil setup-contents start 0)
  2939. (setq ext-setup-or-nil
  2940. (concat (substring ext-setup-or-nil 0 start)
  2941. "\n" setup-contents "\n"
  2942. (substring ext-setup-or-nil start)))))
  2943. ))))
  2944. (when cat
  2945. (org-set-local 'org-category (intern cat))
  2946. (push (cons "CATEGORY" cat) props))
  2947. (when prio
  2948. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2949. (setq prio (mapcar 'string-to-char prio))
  2950. (org-set-local 'org-highest-priority (nth 0 prio))
  2951. (org-set-local 'org-lowest-priority (nth 1 prio))
  2952. (org-set-local 'org-default-priority (nth 2 prio)))
  2953. (and props (org-set-local 'org-file-properties (nreverse props)))
  2954. (and ftags (org-set-local 'org-file-tags ftags))
  2955. (and drawers (org-set-local 'org-drawers drawers))
  2956. (and arch (org-set-local 'org-archive-location arch))
  2957. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2958. ;; Process the TODO keywords
  2959. (unless kwds
  2960. ;; Use the global values as if they had been given locally.
  2961. (setq kwds (default-value 'org-todo-keywords))
  2962. (if (stringp (car kwds))
  2963. (setq kwds (list (cons org-todo-interpretation
  2964. (default-value 'org-todo-keywords)))))
  2965. (setq kwds (reverse kwds)))
  2966. (setq kwds (nreverse kwds))
  2967. (let (inter kws kw)
  2968. (while (setq kws (pop kwds))
  2969. (let ((kws (or
  2970. (run-hook-with-args-until-success
  2971. 'org-todo-setup-filter-hook kws)
  2972. kws)))
  2973. (setq inter (pop kws) sep (member "|" kws)
  2974. kws0 (delete "|" (copy-sequence kws))
  2975. kwsa nil
  2976. kws1 (mapcar
  2977. (lambda (x)
  2978. ;; 1 2
  2979. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2980. (progn
  2981. (setq kw (match-string 1 x)
  2982. key (and (match-end 2) (match-string 2 x))
  2983. log (org-extract-log-state-settings x))
  2984. (push (cons kw (and key (string-to-char key))) kwsa)
  2985. (and log (push log org-todo-log-states))
  2986. kw)
  2987. (error "Invalid TODO keyword %s" x)))
  2988. kws0)
  2989. kwsa (if kwsa (append '((:startgroup))
  2990. (nreverse kwsa)
  2991. '((:endgroup))))
  2992. hw (car kws1)
  2993. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2994. tail (list inter hw (car dws) (org-last dws))))
  2995. (add-to-list 'org-todo-heads hw 'append)
  2996. (push kws1 org-todo-sets)
  2997. (setq org-done-keywords (append org-done-keywords dws nil))
  2998. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2999. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3000. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3001. (setq org-todo-sets (nreverse org-todo-sets)
  3002. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3003. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3004. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3005. ;; Process the constants
  3006. (when const
  3007. (let (e cst)
  3008. (while (setq e (pop const))
  3009. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3010. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3011. (setq org-table-formula-constants-local cst)))
  3012. ;; Process the tags.
  3013. (when tags
  3014. (let (e tgs)
  3015. (while (setq e (pop tags))
  3016. (cond
  3017. ((equal e "{") (push '(:startgroup) tgs))
  3018. ((equal e "}") (push '(:endgroup) tgs))
  3019. ((equal e "\\n") (push '(:newline) tgs))
  3020. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3021. (push (cons (match-string 1 e)
  3022. (string-to-char (match-string 2 e)))
  3023. tgs))
  3024. (t (push (list e) tgs))))
  3025. (org-set-local 'org-tag-alist nil)
  3026. (while (setq e (pop tgs))
  3027. (or (and (stringp (car e))
  3028. (assoc (car e) org-tag-alist))
  3029. (push e org-tag-alist)))))
  3030. ;; Compute the regular expressions and other local variables
  3031. (if (not org-done-keywords)
  3032. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3033. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3034. (length org-scheduled-string)
  3035. (length org-clock-string)
  3036. (length org-closed-string)))
  3037. org-drawer-regexp
  3038. (concat "^[ \t]*:\\("
  3039. (mapconcat 'regexp-quote org-drawers "\\|")
  3040. "\\):[ \t]*$")
  3041. org-not-done-keywords
  3042. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3043. org-todo-regexp
  3044. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3045. "\\|") "\\)\\>")
  3046. org-not-done-regexp
  3047. (concat "\\<\\("
  3048. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3049. "\\)\\>")
  3050. org-todo-line-regexp
  3051. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3052. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3053. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3054. org-complex-heading-regexp
  3055. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3056. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3057. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3058. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3059. org-nl-done-regexp
  3060. (concat "\n\\*+[ \t]+"
  3061. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3062. "\\)" "\\>")
  3063. org-todo-line-tags-regexp
  3064. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3065. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3066. (org-re
  3067. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3068. org-looking-at-done-regexp
  3069. (concat "^" "\\(?:"
  3070. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3071. "\\>")
  3072. org-deadline-regexp (concat "\\<" org-deadline-string)
  3073. org-deadline-time-regexp
  3074. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3075. org-deadline-line-regexp
  3076. (concat "\\<\\(" org-deadline-string "\\).*")
  3077. org-scheduled-regexp
  3078. (concat "\\<" org-scheduled-string)
  3079. org-scheduled-time-regexp
  3080. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3081. org-closed-time-regexp
  3082. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3083. org-keyword-time-regexp
  3084. (concat "\\<\\(" org-scheduled-string
  3085. "\\|" org-deadline-string
  3086. "\\|" org-closed-string
  3087. "\\|" org-clock-string "\\)"
  3088. " *[[<]\\([^]>]+\\)[]>]")
  3089. org-keyword-time-not-clock-regexp
  3090. (concat "\\<\\(" org-scheduled-string
  3091. "\\|" org-deadline-string
  3092. "\\|" org-closed-string
  3093. "\\)"
  3094. " *[[<]\\([^]>]+\\)[]>]")
  3095. org-maybe-keyword-time-regexp
  3096. (concat "\\(\\<\\(" org-scheduled-string
  3097. "\\|" org-deadline-string
  3098. "\\|" org-closed-string
  3099. "\\|" org-clock-string "\\)\\)?"
  3100. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3101. org-planning-or-clock-line-re
  3102. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3103. "\\|" org-deadline-string
  3104. "\\|" org-closed-string "\\|" org-clock-string
  3105. "\\)\\>\\)")
  3106. )
  3107. (org-compute-latex-and-specials-regexp)
  3108. (org-set-font-lock-defaults))))
  3109. (defun org-file-contents (file &optional noerror)
  3110. "Return the contents of FILE, as a string."
  3111. (if (or (not file)
  3112. (not (file-readable-p file)))
  3113. (if noerror
  3114. (progn
  3115. (message "Cannot read file %s" file)
  3116. (ding) (sit-for 2)
  3117. "")
  3118. (error "Cannot read file %s" file))
  3119. (with-temp-buffer
  3120. (insert-file-contents file)
  3121. (buffer-string))))
  3122. (defun org-extract-log-state-settings (x)
  3123. "Extract the log state setting from a TODO keyword string.
  3124. This will extract info from a string like \"WAIT(w@/!)\"."
  3125. (let (kw key log1 log2)
  3126. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3127. (setq kw (match-string 1 x)
  3128. key (and (match-end 2) (match-string 2 x))
  3129. log1 (and (match-end 3) (match-string 3 x))
  3130. log2 (and (match-end 4) (match-string 4 x)))
  3131. (and (or log1 log2)
  3132. (list kw
  3133. (and log1 (if (equal log1 "!") 'time 'note))
  3134. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3135. (defun org-remove-keyword-keys (list)
  3136. "Remove a pair of parenthesis at the end of each string in LIST."
  3137. (mapcar (lambda (x)
  3138. (if (string-match "(.*)$" x)
  3139. (substring x 0 (match-beginning 0))
  3140. x))
  3141. list))
  3142. ;; FIXME: this could be done much better, using second characters etc.
  3143. (defun org-assign-fast-keys (alist)
  3144. "Assign fast keys to a keyword-key alist.
  3145. Respect keys that are already there."
  3146. (let (new e k c c1 c2 (char ?a))
  3147. (while (setq e (pop alist))
  3148. (cond
  3149. ((equal e '(:startgroup)) (push e new))
  3150. ((equal e '(:endgroup)) (push e new))
  3151. ((equal e '(:newline)) (push e new))
  3152. (t
  3153. (setq k (car e) c2 nil)
  3154. (if (cdr e)
  3155. (setq c (cdr e))
  3156. ;; automatically assign a character.
  3157. (setq c1 (string-to-char
  3158. (downcase (substring
  3159. k (if (= (string-to-char k) ?@) 1 0)))))
  3160. (if (or (rassoc c1 new) (rassoc c1 alist))
  3161. (while (or (rassoc char new) (rassoc char alist))
  3162. (setq char (1+ char)))
  3163. (setq c2 c1))
  3164. (setq c (or c2 char)))
  3165. (push (cons k c) new))))
  3166. (nreverse new)))
  3167. ;;; Some variables used in various places
  3168. (defvar org-window-configuration nil
  3169. "Used in various places to store a window configuration.")
  3170. (defvar org-finish-function nil
  3171. "Function to be called when `C-c C-c' is used.
  3172. This is for getting out of special buffers like remember.")
  3173. ;; FIXME: Occasionally check by commenting these, to make sure
  3174. ;; no other functions uses these, forgetting to let-bind them.
  3175. (defvar entry)
  3176. (defvar state)
  3177. (defvar last-state)
  3178. (defvar date)
  3179. (defvar description)
  3180. ;; Defined somewhere in this file, but used before definition.
  3181. (defvar org-html-entities)
  3182. (defvar org-struct-menu)
  3183. (defvar org-org-menu)
  3184. (defvar org-tbl-menu)
  3185. (defvar org-agenda-keymap)
  3186. ;;;; Define the Org-mode
  3187. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3188. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
  3189. ;; We use a before-change function to check if a table might need
  3190. ;; an update.
  3191. (defvar org-table-may-need-update t
  3192. "Indicates that a table might need an update.
  3193. This variable is set by `org-before-change-function'.
  3194. `org-table-align' sets it back to nil.")
  3195. (defun org-before-change-function (beg end)
  3196. "Every change indicates that a table might need an update."
  3197. (setq org-table-may-need-update t))
  3198. (defvar org-mode-map)
  3199. (defvar org-mode-hook nil
  3200. "Mode hook for Org-mode, run after the mode was turned on.")
  3201. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3202. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3203. (defvar org-table-buffer-is-an nil)
  3204. (defconst org-outline-regexp "\\*+ ")
  3205. ;;;###autoload
  3206. (define-derived-mode org-mode outline-mode "Org"
  3207. "Outline-based notes management and organizer, alias
  3208. \"Carsten's outline-mode for keeping track of everything.\"
  3209. Org-mode develops organizational tasks around a NOTES file which
  3210. contains information about projects as plain text. Org-mode is
  3211. implemented on top of outline-mode, which is ideal to keep the content
  3212. of large files well structured. It supports ToDo items, deadlines and
  3213. time stamps, which magically appear in the diary listing of the Emacs
  3214. calendar. Tables are easily created with a built-in table editor.
  3215. Plain text URL-like links connect to websites, emails (VM), Usenet
  3216. messages (Gnus), BBDB entries, and any files related to the project.
  3217. For printing and sharing of notes, an Org-mode file (or a part of it)
  3218. can be exported as a structured ASCII or HTML file.
  3219. The following commands are available:
  3220. \\{org-mode-map}"
  3221. ;; Get rid of Outline menus, they are not needed
  3222. ;; Need to do this here because define-derived-mode sets up
  3223. ;; the keymap so late. Still, it is a waste to call this each time
  3224. ;; we switch another buffer into org-mode.
  3225. (if (featurep 'xemacs)
  3226. (when (boundp 'outline-mode-menu-heading)
  3227. ;; Assume this is Greg's port, it used easymenu
  3228. (easy-menu-remove outline-mode-menu-heading)
  3229. (easy-menu-remove outline-mode-menu-show)
  3230. (easy-menu-remove outline-mode-menu-hide))
  3231. (define-key org-mode-map [menu-bar headings] 'undefined)
  3232. (define-key org-mode-map [menu-bar hide] 'undefined)
  3233. (define-key org-mode-map [menu-bar show] 'undefined))
  3234. (org-load-modules-maybe)
  3235. (easy-menu-add org-org-menu)
  3236. (easy-menu-add org-tbl-menu)
  3237. (org-install-agenda-files-menu)
  3238. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3239. (org-add-to-invisibility-spec '(org-cwidth))
  3240. (when (featurep 'xemacs)
  3241. (org-set-local 'line-move-ignore-invisible t))
  3242. (org-set-local 'outline-regexp org-outline-regexp)
  3243. (org-set-local 'outline-level 'org-outline-level)
  3244. (when (and org-ellipsis
  3245. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3246. (fboundp 'make-glyph-code))
  3247. (unless org-display-table
  3248. (setq org-display-table (make-display-table)))
  3249. (set-display-table-slot
  3250. org-display-table 4
  3251. (vconcat (mapcar
  3252. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3253. org-ellipsis)))
  3254. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3255. (setq buffer-display-table org-display-table))
  3256. (org-set-regexps-and-options)
  3257. (when (and org-tag-faces (not org-tags-special-faces-re))
  3258. ;; tag faces set outside customize.... force initialization.
  3259. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3260. ;; Calc embedded
  3261. (org-set-local 'calc-embedded-open-mode "# ")
  3262. (modify-syntax-entry ?# "<")
  3263. (modify-syntax-entry ?@ "w")
  3264. (if org-startup-truncated (setq truncate-lines t))
  3265. (org-set-local 'font-lock-unfontify-region-function
  3266. 'org-unfontify-region)
  3267. ;; Activate before-change-function
  3268. (org-set-local 'org-table-may-need-update t)
  3269. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3270. 'local)
  3271. ;; Check for running clock before killing a buffer
  3272. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3273. ;; Paragraphs and auto-filling
  3274. (org-set-autofill-regexps)
  3275. (setq indent-line-function 'org-indent-line-function)
  3276. (org-update-radio-target-regexp)
  3277. ;; Make sure dependence stuff works reliably, even for users who set it
  3278. ;; too late :-(
  3279. (if org-enforce-todo-dependencies
  3280. (add-hook 'org-blocker-hook
  3281. 'org-block-todo-from-children-or-siblings)
  3282. (remove-hook 'org-blocker-hook
  3283. 'org-block-todo-from-children-or-siblings))
  3284. (if org-enforce-todo-checkbox-dependencies
  3285. (add-hook 'org-blocker-hook
  3286. 'org-block-todo-from-checkboxes)
  3287. (remove-hook 'org-blocker-hook
  3288. 'org-block-todo-from-checkboxes))
  3289. ;; Comment characters
  3290. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3291. (org-set-local 'comment-padding " ")
  3292. ;; Align options lines
  3293. (org-set-local
  3294. 'align-mode-rules-list
  3295. '((org-in-buffer-settings
  3296. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3297. (modes . '(org-mode)))))
  3298. ;; Imenu
  3299. (org-set-local 'imenu-create-index-function
  3300. 'org-imenu-get-tree)
  3301. ;; Make isearch reveal context
  3302. (if (or (featurep 'xemacs)
  3303. (not (boundp 'outline-isearch-open-invisible-function)))
  3304. ;; Emacs 21 and XEmacs make use of the hook
  3305. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3306. ;; Emacs 22 deals with this through a special variable
  3307. (org-set-local 'outline-isearch-open-invisible-function
  3308. (lambda (&rest ignore) (org-show-context 'isearch))))
  3309. ;; If empty file that did not turn on org-mode automatically, make it to.
  3310. (if (and org-insert-mode-line-in-empty-file
  3311. (interactive-p)
  3312. (= (point-min) (point-max)))
  3313. (insert "# -*- mode: org -*-\n\n"))
  3314. (unless org-inhibit-startup
  3315. (when org-startup-align-all-tables
  3316. (let ((bmp (buffer-modified-p)))
  3317. (org-table-map-tables 'org-table-align)
  3318. (set-buffer-modified-p bmp)))
  3319. (org-set-startup-visibility)))
  3320. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3321. (defun org-current-time ()
  3322. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3323. (if (> (car org-time-stamp-rounding-minutes) 1)
  3324. (let ((r (car org-time-stamp-rounding-minutes))
  3325. (time (decode-time)))
  3326. (apply 'encode-time
  3327. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3328. (nthcdr 2 time))))
  3329. (current-time)))
  3330. ;;;; Font-Lock stuff, including the activators
  3331. (defvar org-mouse-map (make-sparse-keymap))
  3332. (org-defkey org-mouse-map
  3333. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3334. (org-defkey org-mouse-map
  3335. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3336. (when org-mouse-1-follows-link
  3337. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3338. (when org-tab-follows-link
  3339. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3340. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3341. (require 'font-lock)
  3342. (defconst org-non-link-chars "]\t\n\r<>")
  3343. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3344. "shell" "elisp"))
  3345. (defvar org-link-types-re nil
  3346. "Matches a link that has a url-like prefix like \"http:\"")
  3347. (defvar org-link-re-with-space nil
  3348. "Matches a link with spaces, optional angular brackets around it.")
  3349. (defvar org-link-re-with-space2 nil
  3350. "Matches a link with spaces, optional angular brackets around it.")
  3351. (defvar org-link-re-with-space3 nil
  3352. "Matches a link with spaces, only for internal part in bracket links.")
  3353. (defvar org-angle-link-re nil
  3354. "Matches link with angular brackets, spaces are allowed.")
  3355. (defvar org-plain-link-re nil
  3356. "Matches plain link, without spaces.")
  3357. (defvar org-bracket-link-regexp nil
  3358. "Matches a link in double brackets.")
  3359. (defvar org-bracket-link-analytic-regexp nil
  3360. "Regular expression used to analyze links.
  3361. Here is what the match groups contain after a match:
  3362. 1: http:
  3363. 2: http
  3364. 3: path
  3365. 4: [desc]
  3366. 5: desc")
  3367. (defvar org-bracket-link-analytic-regexp++ nil
  3368. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3369. (defvar org-any-link-re nil
  3370. "Regular expression matching any link.")
  3371. (defun org-make-link-regexps ()
  3372. "Update the link regular expressions.
  3373. This should be called after the variable `org-link-types' has changed."
  3374. (setq org-link-types-re
  3375. (concat
  3376. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3377. org-link-re-with-space
  3378. (concat
  3379. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3380. "\\([^" org-non-link-chars " ]"
  3381. "[^" org-non-link-chars "]*"
  3382. "[^" org-non-link-chars " ]\\)>?")
  3383. org-link-re-with-space2
  3384. (concat
  3385. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3386. "\\([^" org-non-link-chars " ]"
  3387. "[^\t\n\r]*"
  3388. "[^" org-non-link-chars " ]\\)>?")
  3389. org-link-re-with-space3
  3390. (concat
  3391. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3392. "\\([^" org-non-link-chars " ]"
  3393. "[^\t\n\r]*\\)")
  3394. org-angle-link-re
  3395. (concat
  3396. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3397. "\\([^" org-non-link-chars " ]"
  3398. "[^" org-non-link-chars "]*"
  3399. "\\)>")
  3400. org-plain-link-re
  3401. (concat
  3402. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3403. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3404. org-bracket-link-regexp
  3405. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3406. org-bracket-link-analytic-regexp
  3407. (concat
  3408. "\\[\\["
  3409. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3410. "\\([^]]+\\)"
  3411. "\\]"
  3412. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3413. "\\]")
  3414. org-bracket-link-analytic-regexp++
  3415. (concat
  3416. "\\[\\["
  3417. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3418. "\\([^]]+\\)"
  3419. "\\]"
  3420. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3421. "\\]")
  3422. org-any-link-re
  3423. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3424. org-angle-link-re "\\)\\|\\("
  3425. org-plain-link-re "\\)")))
  3426. (org-make-link-regexps)
  3427. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3428. "Regular expression for fast time stamp matching.")
  3429. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3430. "Regular expression for fast time stamp matching.")
  3431. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3432. "Regular expression matching time strings for analysis.
  3433. This one does not require the space after the date, so it can be used
  3434. on a string that terminates immediately after the date.")
  3435. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3436. "Regular expression matching time strings for analysis.")
  3437. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3438. "Regular expression matching time stamps, with groups.")
  3439. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3440. "Regular expression matching time stamps (also [..]), with groups.")
  3441. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3442. "Regular expression matching a time stamp range.")
  3443. (defconst org-tr-regexp-both
  3444. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3445. "Regular expression matching a time stamp range.")
  3446. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3447. org-ts-regexp "\\)?")
  3448. "Regular expression matching a time stamp or time stamp range.")
  3449. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3450. org-ts-regexp-both "\\)?")
  3451. "Regular expression matching a time stamp or time stamp range.
  3452. The time stamps may be either active or inactive.")
  3453. (defvar org-emph-face nil)
  3454. (defun org-do-emphasis-faces (limit)
  3455. "Run through the buffer and add overlays to links."
  3456. (let (rtn)
  3457. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3458. (if (not (= (char-after (match-beginning 3))
  3459. (char-after (match-beginning 4))))
  3460. (progn
  3461. (setq rtn t)
  3462. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3463. 'face
  3464. (nth 1 (assoc (match-string 3)
  3465. org-emphasis-alist)))
  3466. (add-text-properties (match-beginning 2) (match-end 2)
  3467. '(font-lock-multiline t))
  3468. (when org-hide-emphasis-markers
  3469. (add-text-properties (match-end 4) (match-beginning 5)
  3470. '(invisible org-link))
  3471. (add-text-properties (match-beginning 3) (match-end 3)
  3472. '(invisible org-link)))))
  3473. (backward-char 1))
  3474. rtn))
  3475. (defun org-emphasize (&optional char)
  3476. "Insert or change an emphasis, i.e. a font like bold or italic.
  3477. If there is an active region, change that region to a new emphasis.
  3478. If there is no region, just insert the marker characters and position
  3479. the cursor between them.
  3480. CHAR should be either the marker character, or the first character of the
  3481. HTML tag associated with that emphasis. If CHAR is a space, the means
  3482. to remove the emphasis of the selected region.
  3483. If char is not given (for example in an interactive call) it
  3484. will be prompted for."
  3485. (interactive)
  3486. (let ((eal org-emphasis-alist) e det
  3487. (erc org-emphasis-regexp-components)
  3488. (prompt "")
  3489. (string "") beg end move tag c s)
  3490. (if (org-region-active-p)
  3491. (setq beg (region-beginning) end (region-end)
  3492. string (buffer-substring beg end))
  3493. (setq move t))
  3494. (while (setq e (pop eal))
  3495. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3496. c (aref tag 0))
  3497. (push (cons c (string-to-char (car e))) det)
  3498. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3499. (substring tag 1)))))
  3500. (setq det (nreverse det))
  3501. (unless char
  3502. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3503. (setq char (read-char-exclusive)))
  3504. (setq char (or (cdr (assoc char det)) char))
  3505. (if (equal char ?\ )
  3506. (setq s "" move nil)
  3507. (unless (assoc (char-to-string char) org-emphasis-alist)
  3508. (error "No such emphasis marker: \"%c\"" char))
  3509. (setq s (char-to-string char)))
  3510. (while (and (> (length string) 1)
  3511. (equal (substring string 0 1) (substring string -1))
  3512. (assoc (substring string 0 1) org-emphasis-alist))
  3513. (setq string (substring string 1 -1)))
  3514. (setq string (concat s string s))
  3515. (if beg (delete-region beg end))
  3516. (unless (or (bolp)
  3517. (string-match (concat "[" (nth 0 erc) "\n]")
  3518. (char-to-string (char-before (point)))))
  3519. (insert " "))
  3520. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3521. (char-to-string (char-after (point))))
  3522. (insert " ") (backward-char 1))
  3523. (insert string)
  3524. (and move (backward-char 1))))
  3525. (defconst org-nonsticky-props
  3526. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3527. (defun org-activate-plain-links (limit)
  3528. "Run through the buffer and add overlays to links."
  3529. (catch 'exit
  3530. (let (f)
  3531. (while (re-search-forward org-plain-link-re limit t)
  3532. (setq f (get-text-property (match-beginning 0) 'face))
  3533. (if (or (eq f 'org-tag)
  3534. (and (listp f) (memq 'org-tag f)))
  3535. nil
  3536. (add-text-properties (match-beginning 0) (match-end 0)
  3537. (list 'mouse-face 'highlight
  3538. 'rear-nonsticky org-nonsticky-props
  3539. 'keymap org-mouse-map
  3540. ))
  3541. (throw 'exit t))))))
  3542. (defun org-activate-code (limit)
  3543. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3544. (progn
  3545. (remove-text-properties (match-beginning 0) (match-end 0)
  3546. '(display t invisible t intangible t))
  3547. t)))
  3548. (defun org-activate-angle-links (limit)
  3549. "Run through the buffer and add overlays to links."
  3550. (if (re-search-forward org-angle-link-re limit t)
  3551. (progn
  3552. (add-text-properties (match-beginning 0) (match-end 0)
  3553. (list 'mouse-face 'highlight
  3554. 'rear-nonsticky org-nonsticky-props
  3555. 'keymap org-mouse-map
  3556. ))
  3557. t)))
  3558. (defun org-activate-footnote-links (limit)
  3559. "Run through the buffer and add overlays to links."
  3560. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3561. limit t)
  3562. (progn
  3563. (add-text-properties (match-beginning 2) (match-end 2)
  3564. (list 'mouse-face 'highlight
  3565. 'rear-nonsticky org-nonsticky-props
  3566. 'keymap org-mouse-map
  3567. 'help-echo
  3568. (if (= (point-at-bol) (match-beginning 2))
  3569. "Footnote definition"
  3570. "Footnote reference")
  3571. ))
  3572. t)))
  3573. (defun org-activate-bracket-links (limit)
  3574. "Run through the buffer and add overlays to bracketed links."
  3575. (if (re-search-forward org-bracket-link-regexp limit t)
  3576. (let* ((help (concat "LINK: "
  3577. (org-match-string-no-properties 1)))
  3578. ;; FIXME: above we should remove the escapes.
  3579. ;; but that requires another match, protecting match data,
  3580. ;; a lot of overhead for font-lock.
  3581. (ip (org-maybe-intangible
  3582. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3583. 'keymap org-mouse-map 'mouse-face 'highlight
  3584. 'font-lock-multiline t 'help-echo help)))
  3585. (vp (list 'rear-nonsticky org-nonsticky-props
  3586. 'keymap org-mouse-map 'mouse-face 'highlight
  3587. ' font-lock-multiline t 'help-echo help)))
  3588. ;; We need to remove the invisible property here. Table narrowing
  3589. ;; may have made some of this invisible.
  3590. (remove-text-properties (match-beginning 0) (match-end 0)
  3591. '(invisible nil))
  3592. (if (match-end 3)
  3593. (progn
  3594. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3595. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3596. (add-text-properties (match-end 3) (match-end 0) ip))
  3597. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3598. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3599. (add-text-properties (match-end 1) (match-end 0) ip))
  3600. t)))
  3601. (defun org-activate-dates (limit)
  3602. "Run through the buffer and add overlays to dates."
  3603. (if (re-search-forward org-tsr-regexp-both limit t)
  3604. (progn
  3605. (add-text-properties (match-beginning 0) (match-end 0)
  3606. (list 'mouse-face 'highlight
  3607. 'rear-nonsticky org-nonsticky-props
  3608. 'keymap org-mouse-map))
  3609. (when org-display-custom-times
  3610. (if (match-end 3)
  3611. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3612. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3613. t)))
  3614. (defvar org-target-link-regexp nil
  3615. "Regular expression matching radio targets in plain text.")
  3616. (make-variable-buffer-local 'org-target-link-regexp)
  3617. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3618. "Regular expression matching a link target.")
  3619. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3620. "Regular expression matching a radio target.")
  3621. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3622. "Regular expression matching any target.")
  3623. (defun org-activate-target-links (limit)
  3624. "Run through the buffer and add overlays to target matches."
  3625. (when org-target-link-regexp
  3626. (let ((case-fold-search t))
  3627. (if (re-search-forward org-target-link-regexp limit t)
  3628. (progn
  3629. (add-text-properties (match-beginning 0) (match-end 0)
  3630. (list 'mouse-face 'highlight
  3631. 'rear-nonsticky org-nonsticky-props
  3632. 'keymap org-mouse-map
  3633. 'help-echo "Radio target link"
  3634. 'org-linked-text t))
  3635. t)))))
  3636. (defun org-update-radio-target-regexp ()
  3637. "Find all radio targets in this file and update the regular expression."
  3638. (interactive)
  3639. (when (memq 'radio org-activate-links)
  3640. (setq org-target-link-regexp
  3641. (org-make-target-link-regexp (org-all-targets 'radio)))
  3642. (org-restart-font-lock)))
  3643. (defun org-hide-wide-columns (limit)
  3644. (let (s e)
  3645. (setq s (text-property-any (point) (or limit (point-max))
  3646. 'org-cwidth t))
  3647. (when s
  3648. (setq e (next-single-property-change s 'org-cwidth))
  3649. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3650. (goto-char e)
  3651. t)))
  3652. (defvar org-latex-and-specials-regexp nil
  3653. "Regular expression for highlighting export special stuff.")
  3654. (defvar org-match-substring-regexp)
  3655. (defvar org-match-substring-with-braces-regexp)
  3656. (defvar org-export-html-special-string-regexps)
  3657. (defun org-compute-latex-and-specials-regexp ()
  3658. "Compute regular expression for stuff treated specially by exporters."
  3659. (if (not org-highlight-latex-fragments-and-specials)
  3660. (org-set-local 'org-latex-and-specials-regexp nil)
  3661. (require 'org-exp)
  3662. (let*
  3663. ((matchers (plist-get org-format-latex-options :matchers))
  3664. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3665. org-latex-regexps)))
  3666. (options (org-combine-plists (org-default-export-plist)
  3667. (org-infile-export-plist)))
  3668. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3669. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3670. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3671. (org-export-html-expand (plist-get options :expand-quoted-html))
  3672. (org-export-with-special-strings (plist-get options :special-strings))
  3673. (re-sub
  3674. (cond
  3675. ((equal org-export-with-sub-superscripts '{})
  3676. (list org-match-substring-with-braces-regexp))
  3677. (org-export-with-sub-superscripts
  3678. (list org-match-substring-regexp))
  3679. (t nil)))
  3680. (re-latex
  3681. (if org-export-with-LaTeX-fragments
  3682. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3683. (re-macros
  3684. (if org-export-with-TeX-macros
  3685. (list (concat "\\\\"
  3686. (regexp-opt
  3687. (append (mapcar 'car org-html-entities)
  3688. (if (boundp 'org-latex-entities)
  3689. org-latex-entities nil))
  3690. 'words))) ; FIXME
  3691. ))
  3692. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3693. (re-special (if org-export-with-special-strings
  3694. (mapcar (lambda (x) (car x))
  3695. org-export-html-special-string-regexps)))
  3696. (re-rest
  3697. (delq nil
  3698. (list
  3699. (if org-export-html-expand "@<[^>\n]+>")
  3700. ))))
  3701. (org-set-local
  3702. 'org-latex-and-specials-regexp
  3703. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3704. re-rest) "\\|")))))
  3705. (defun org-do-latex-and-special-faces (limit)
  3706. "Run through the buffer and add overlays to links."
  3707. (when org-latex-and-specials-regexp
  3708. (let (rtn d)
  3709. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3710. limit t))
  3711. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3712. 'face))
  3713. '(org-code org-verbatim underline)))
  3714. (progn
  3715. (setq rtn t
  3716. d (cond ((member (char-after (1+ (match-beginning 0)))
  3717. '(?_ ?^)) 1)
  3718. (t 0)))
  3719. (font-lock-prepend-text-property
  3720. (+ d (match-beginning 0)) (match-end 0)
  3721. 'face 'org-latex-and-export-specials)
  3722. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3723. '(font-lock-multiline t)))))
  3724. rtn)))
  3725. (defun org-restart-font-lock ()
  3726. "Restart font-lock-mode, to force refontification."
  3727. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3728. (font-lock-mode -1)
  3729. (font-lock-mode 1)))
  3730. (defun org-all-targets (&optional radio)
  3731. "Return a list of all targets in this file.
  3732. With optional argument RADIO, only find radio targets."
  3733. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3734. rtn)
  3735. (save-excursion
  3736. (goto-char (point-min))
  3737. (while (re-search-forward re nil t)
  3738. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3739. rtn)))
  3740. (defun org-make-target-link-regexp (targets)
  3741. "Make regular expression matching all strings in TARGETS.
  3742. The regular expression finds the targets also if there is a line break
  3743. between words."
  3744. (and targets
  3745. (concat
  3746. "\\<\\("
  3747. (mapconcat
  3748. (lambda (x)
  3749. (while (string-match " +" x)
  3750. (setq x (replace-match "\\s-+" t t x)))
  3751. x)
  3752. targets
  3753. "\\|")
  3754. "\\)\\>")))
  3755. (defun org-activate-tags (limit)
  3756. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3757. (progn
  3758. (add-text-properties (match-beginning 1) (match-end 1)
  3759. (list 'mouse-face 'highlight
  3760. 'rear-nonsticky org-nonsticky-props
  3761. 'keymap org-mouse-map))
  3762. t)))
  3763. (defun org-outline-level ()
  3764. (save-excursion
  3765. (looking-at outline-regexp)
  3766. (if (match-beginning 1)
  3767. (+ (org-get-string-indentation (match-string 1)) 1000)
  3768. (1- (- (match-end 0) (match-beginning 0))))))
  3769. (defvar org-font-lock-keywords nil)
  3770. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3771. "Regular expression matching a property line.")
  3772. (defvar org-font-lock-hook nil
  3773. "Functions to be called for special font lock stuff.")
  3774. (defun org-font-lock-hook (limit)
  3775. (run-hook-with-args 'org-font-lock-hook limit))
  3776. (defun org-set-font-lock-defaults ()
  3777. (let* ((em org-fontify-emphasized-text)
  3778. (lk org-activate-links)
  3779. (org-font-lock-extra-keywords
  3780. (list
  3781. ;; Call the hook
  3782. '(org-font-lock-hook)
  3783. ;; Headlines
  3784. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3785. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3786. ;; Table lines
  3787. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3788. (1 'org-table t))
  3789. ;; Table internals
  3790. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3791. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3792. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3793. ;; Drawers
  3794. (list org-drawer-regexp '(0 'org-special-keyword t))
  3795. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3796. ;; Properties
  3797. (list org-property-re
  3798. '(1 'org-special-keyword t)
  3799. '(3 'org-property-value t))
  3800. (if org-format-transports-properties-p
  3801. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3802. ;; Links
  3803. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3804. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3805. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3806. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3807. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3808. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3809. (if (memq 'footnote lk) '(org-activate-footnote-links
  3810. (2 'org-footnote t)))
  3811. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3812. '(org-hide-wide-columns (0 nil append))
  3813. ;; TODO lines
  3814. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3815. '(1 (org-get-todo-face 1) t))
  3816. ;; DONE
  3817. (if org-fontify-done-headline
  3818. (list (concat "^[*]+ +\\<\\("
  3819. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3820. "\\)\\(.*\\)")
  3821. '(2 'org-headline-done t))
  3822. nil)
  3823. ;; Priorities
  3824. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3825. ;; Tags
  3826. '(org-font-lock-add-tag-faces)
  3827. ;; Special keywords
  3828. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3829. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3830. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3831. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3832. ;; Emphasis
  3833. (if em
  3834. (if (featurep 'xemacs)
  3835. '(org-do-emphasis-faces (0 nil append))
  3836. '(org-do-emphasis-faces)))
  3837. ;; Checkboxes
  3838. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3839. 2 'bold prepend)
  3840. (if org-provide-checkbox-statistics
  3841. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3842. (0 (org-get-checkbox-statistics-face) t)))
  3843. ;; Description list items
  3844. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3845. 2 'bold prepend)
  3846. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3847. '(1 'org-archived prepend))
  3848. ;; Specials
  3849. '(org-do-latex-and-special-faces)
  3850. ;; Code
  3851. '(org-activate-code (1 'org-code t))
  3852. ;; COMMENT
  3853. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3854. "\\|" org-quote-string "\\)\\>")
  3855. '(1 'org-special-keyword t))
  3856. '("^#.*" (0 'font-lock-comment-face t))
  3857. )))
  3858. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3859. ;; Now set the full font-lock-keywords
  3860. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3861. (org-set-local 'font-lock-defaults
  3862. '(org-font-lock-keywords t nil nil backward-paragraph))
  3863. (kill-local-variable 'font-lock-keywords) nil))
  3864. (defvar org-m nil)
  3865. (defvar org-l nil)
  3866. (defvar org-f nil)
  3867. (defun org-get-level-face (n)
  3868. "Get the right face for match N in font-lock matching of headlines."
  3869. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3870. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3871. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3872. (cond
  3873. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3874. ((eq n 2) org-f)
  3875. (t (if org-level-color-stars-only nil org-f))))
  3876. (defun org-get-todo-face (kwd)
  3877. "Get the right face for a TODO keyword KWD.
  3878. If KWD is a number, get the corresponding match group."
  3879. (if (numberp kwd) (setq kwd (match-string kwd)))
  3880. (or (cdr (assoc kwd org-todo-keyword-faces))
  3881. (and (member kwd org-done-keywords) 'org-done)
  3882. 'org-todo))
  3883. (defun org-font-lock-add-tag-faces (limit)
  3884. "Add the special tag faces."
  3885. (when (and org-tag-faces org-tags-special-faces-re)
  3886. (while (re-search-forward org-tags-special-faces-re limit t)
  3887. (add-text-properties (match-beginning 1) (match-end 1)
  3888. (list 'face (org-get-tag-face 1)
  3889. 'font-lock-fontified t))
  3890. (backward-char 1))))
  3891. (defun org-get-tag-face (kwd)
  3892. "Get the right face for a TODO keyword KWD.
  3893. If KWD is a number, get the corresponding match group."
  3894. (if (numberp kwd) (setq kwd (match-string kwd)))
  3895. (or (cdr (assoc kwd org-tag-faces))
  3896. 'org-tag))
  3897. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3898. "Remove fontification and activation overlays from links."
  3899. (font-lock-default-unfontify-region beg end)
  3900. (let* ((buffer-undo-list t)
  3901. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3902. (inhibit-modification-hooks t)
  3903. deactivate-mark buffer-file-name buffer-file-truename)
  3904. (remove-text-properties beg end
  3905. '(mouse-face t keymap t org-linked-text t
  3906. invisible t intangible t))))
  3907. ;;;; Visibility cycling, including org-goto and indirect buffer
  3908. ;;; Cycling
  3909. (defvar org-cycle-global-status nil)
  3910. (make-variable-buffer-local 'org-cycle-global-status)
  3911. (defvar org-cycle-subtree-status nil)
  3912. (make-variable-buffer-local 'org-cycle-subtree-status)
  3913. ;;;###autoload
  3914. (defun org-cycle (&optional arg)
  3915. "Visibility cycling for Org-mode.
  3916. - When this function is called with a prefix argument, rotate the entire
  3917. buffer through 3 states (global cycling)
  3918. 1. OVERVIEW: Show only top-level headlines.
  3919. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3920. 3. SHOW ALL: Show everything.
  3921. When called with two C-u C-u prefixes, switch to the startup visibility,
  3922. determined by the variable `org-startup-folded', and by any VISIBILITY
  3923. properties in the buffer.
  3924. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3925. including drawers.
  3926. - When point is at the beginning of a headline, rotate the subtree started
  3927. by this line through 3 different states (local cycling)
  3928. 1. FOLDED: Only the main headline is shown.
  3929. 2. CHILDREN: The main headline and the direct children are shown.
  3930. From this state, you can move to one of the children
  3931. and zoom in further.
  3932. 3. SUBTREE: Show the entire subtree, including body text.
  3933. - When there is a numeric prefix, go up to a heading with level ARG, do
  3934. a `show-subtree' and return to the previous cursor position. If ARG
  3935. is negative, go up that many levels.
  3936. - When point is not at the beginning of a headline, execute the global
  3937. binding for TAB, which is re-indenting the line. See the option
  3938. `org-cycle-emulate-tab' for details.
  3939. - Special case: if point is at the beginning of the buffer and there is
  3940. no headline in line 1, this function will act as if called with prefix arg.
  3941. But only if also the variable `org-cycle-global-at-bob' is t."
  3942. (interactive "P")
  3943. (org-load-modules-maybe)
  3944. (let* ((outline-regexp
  3945. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3946. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3947. outline-regexp))
  3948. (bob-special (and org-cycle-global-at-bob (bobp)
  3949. (not (looking-at outline-regexp))))
  3950. (org-cycle-hook
  3951. (if bob-special
  3952. (delq 'org-optimize-window-after-visibility-change
  3953. (copy-sequence org-cycle-hook))
  3954. org-cycle-hook))
  3955. (pos (point)))
  3956. (if (or bob-special (equal arg '(4)))
  3957. ;; special case: use global cycling
  3958. (setq arg t))
  3959. (cond
  3960. ((equal arg '(16))
  3961. (org-set-startup-visibility)
  3962. (message "Startup visibility, plus VISIBILITY properties"))
  3963. ((equal arg '(64))
  3964. (show-all)
  3965. (message "Entire buffer visible, including drawers"))
  3966. ((org-at-table-p 'any)
  3967. ;; Enter the table or move to the next field in the table
  3968. (or (org-table-recognize-table.el)
  3969. (progn
  3970. (if arg (org-table-edit-field t)
  3971. (org-table-justify-field-maybe)
  3972. (call-interactively 'org-table-next-field)))))
  3973. ((eq arg t) ;; Global cycling
  3974. (cond
  3975. ((and (eq last-command this-command)
  3976. (eq org-cycle-global-status 'overview))
  3977. ;; We just created the overview - now do table of contents
  3978. ;; This can be slow in very large buffers, so indicate action
  3979. (message "CONTENTS...")
  3980. (org-content)
  3981. (message "CONTENTS...done")
  3982. (setq org-cycle-global-status 'contents)
  3983. (run-hook-with-args 'org-cycle-hook 'contents))
  3984. ((and (eq last-command this-command)
  3985. (eq org-cycle-global-status 'contents))
  3986. ;; We just showed the table of contents - now show everything
  3987. (show-all)
  3988. (message "SHOW ALL")
  3989. (setq org-cycle-global-status 'all)
  3990. (run-hook-with-args 'org-cycle-hook 'all))
  3991. (t
  3992. ;; Default action: go to overview
  3993. (org-overview)
  3994. (message "OVERVIEW")
  3995. (setq org-cycle-global-status 'overview)
  3996. (run-hook-with-args 'org-cycle-hook 'overview))))
  3997. ((and org-drawers org-drawer-regexp
  3998. (save-excursion
  3999. (beginning-of-line 1)
  4000. (looking-at org-drawer-regexp)))
  4001. ;; Toggle block visibility
  4002. (org-flag-drawer
  4003. (not (get-char-property (match-end 0) 'invisible))))
  4004. ((integerp arg)
  4005. ;; Show-subtree, ARG levels up from here.
  4006. (save-excursion
  4007. (org-back-to-heading)
  4008. (outline-up-heading (if (< arg 0) (- arg)
  4009. (- (funcall outline-level) arg)))
  4010. (org-show-subtree)))
  4011. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4012. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4013. ;; At a heading: rotate between three different views
  4014. (org-back-to-heading)
  4015. (let ((goal-column 0) eoh eol eos)
  4016. ;; First, some boundaries
  4017. (save-excursion
  4018. (org-back-to-heading)
  4019. (save-excursion
  4020. (beginning-of-line 2)
  4021. (while (and (not (eobp)) ;; this is like `next-line'
  4022. (get-char-property (1- (point)) 'invisible))
  4023. (beginning-of-line 2)) (setq eol (point)))
  4024. (outline-end-of-heading) (setq eoh (point))
  4025. (org-end-of-subtree t)
  4026. (unless (eobp)
  4027. (skip-chars-forward " \t\n")
  4028. (beginning-of-line 1) ; in case this is an item
  4029. )
  4030. (setq eos (1- (point))))
  4031. ;; Find out what to do next and set `this-command'
  4032. (cond
  4033. ((= eos eoh)
  4034. ;; Nothing is hidden behind this heading
  4035. (message "EMPTY ENTRY")
  4036. (setq org-cycle-subtree-status nil)
  4037. (save-excursion
  4038. (goto-char eos)
  4039. (outline-next-heading)
  4040. (if (org-invisible-p) (org-flag-heading nil))))
  4041. ((or (>= eol eos)
  4042. (not (string-match "\\S-" (buffer-substring eol eos))))
  4043. ;; Entire subtree is hidden in one line: open it
  4044. (org-show-entry)
  4045. (show-children)
  4046. (message "CHILDREN")
  4047. (save-excursion
  4048. (goto-char eos)
  4049. (outline-next-heading)
  4050. (if (org-invisible-p) (org-flag-heading nil)))
  4051. (setq org-cycle-subtree-status 'children)
  4052. (run-hook-with-args 'org-cycle-hook 'children))
  4053. ((and (eq last-command this-command)
  4054. (eq org-cycle-subtree-status 'children))
  4055. ;; We just showed the children, now show everything.
  4056. (org-show-subtree)
  4057. (message "SUBTREE")
  4058. (setq org-cycle-subtree-status 'subtree)
  4059. (run-hook-with-args 'org-cycle-hook 'subtree))
  4060. (t
  4061. ;; Default action: hide the subtree.
  4062. (hide-subtree)
  4063. (message "FOLDED")
  4064. (setq org-cycle-subtree-status 'folded)
  4065. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4066. ;; TAB emulation and template completion
  4067. (buffer-read-only (org-back-to-heading))
  4068. ((org-try-structure-completion))
  4069. ((org-try-cdlatex-tab))
  4070. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4071. (or (not (bolp))
  4072. (not (looking-at outline-regexp))))
  4073. (call-interactively (global-key-binding "\t")))
  4074. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4075. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4076. (or (and (eq org-cycle-emulate-tab 'white)
  4077. (= (match-end 0) (point-at-eol)))
  4078. (and (eq org-cycle-emulate-tab 'whitestart)
  4079. (>= (match-end 0) pos))))
  4080. t
  4081. (eq org-cycle-emulate-tab t))
  4082. (call-interactively (global-key-binding "\t")))
  4083. (t (save-excursion
  4084. (org-back-to-heading)
  4085. (org-cycle))))))
  4086. ;;;###autoload
  4087. (defun org-global-cycle (&optional arg)
  4088. "Cycle the global visibility. For details see `org-cycle'.
  4089. With C-u prefix arg, switch to startup visibility.
  4090. With a numeric prefix, show all headlines up to that level."
  4091. (interactive "P")
  4092. (let ((org-cycle-include-plain-lists
  4093. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4094. (cond
  4095. ((integerp arg)
  4096. (show-all)
  4097. (hide-sublevels arg)
  4098. (setq org-cycle-global-status 'contents))
  4099. ((equal arg '(4))
  4100. (org-set-startup-visibility)
  4101. (message "Startup visibility, plus VISIBILITY properties."))
  4102. (t
  4103. (org-cycle '(4))))))
  4104. (defun org-set-startup-visibility ()
  4105. "Set the visibility required by startup options and properties."
  4106. (cond
  4107. ((eq org-startup-folded t)
  4108. (org-cycle '(4)))
  4109. ((eq org-startup-folded 'content)
  4110. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4111. (org-cycle '(4)) (org-cycle '(4)))))
  4112. (org-set-visibility-according-to-property 'no-cleanup)
  4113. (org-cycle-hide-archived-subtrees 'all)
  4114. (org-cycle-hide-drawers 'all)
  4115. (org-cycle-show-empty-lines 'all))
  4116. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4117. "Switch subtree visibilities according to :VISIBILITY: property."
  4118. (interactive)
  4119. (let (org-show-entry-below state)
  4120. (save-excursion
  4121. (goto-char (point-min))
  4122. (while (re-search-forward
  4123. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4124. nil t)
  4125. (setq state (match-string 1))
  4126. (save-excursion
  4127. (org-back-to-heading t)
  4128. (hide-subtree)
  4129. (org-reveal)
  4130. (cond
  4131. ((equal state '("fold" "folded"))
  4132. (hide-subtree))
  4133. ((equal state "children")
  4134. (org-show-hidden-entry)
  4135. (show-children))
  4136. ((equal state "content")
  4137. (save-excursion
  4138. (save-restriction
  4139. (org-narrow-to-subtree)
  4140. (org-content))))
  4141. ((member state '("all" "showall"))
  4142. (show-subtree)))))
  4143. (unless no-cleanup
  4144. (org-cycle-hide-archived-subtrees 'all)
  4145. (org-cycle-hide-drawers 'all)
  4146. (org-cycle-show-empty-lines 'all)))))
  4147. (defun org-overview ()
  4148. "Switch to overview mode, showing only top-level headlines.
  4149. Really, this shows all headlines with level equal or greater than the level
  4150. of the first headline in the buffer. This is important, because if the
  4151. first headline is not level one, then (hide-sublevels 1) gives confusing
  4152. results."
  4153. (interactive)
  4154. (let ((level (save-excursion
  4155. (goto-char (point-min))
  4156. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4157. (progn
  4158. (goto-char (match-beginning 0))
  4159. (funcall outline-level))))))
  4160. (and level (hide-sublevels level))))
  4161. (defun org-content (&optional arg)
  4162. "Show all headlines in the buffer, like a table of contents.
  4163. With numerical argument N, show content up to level N."
  4164. (interactive "P")
  4165. (save-excursion
  4166. ;; Visit all headings and show their offspring
  4167. (and (integerp arg) (org-overview))
  4168. (goto-char (point-max))
  4169. (catch 'exit
  4170. (while (and (progn (condition-case nil
  4171. (outline-previous-visible-heading 1)
  4172. (error (goto-char (point-min))))
  4173. t)
  4174. (looking-at outline-regexp))
  4175. (if (integerp arg)
  4176. (show-children (1- arg))
  4177. (show-branches))
  4178. (if (bobp) (throw 'exit nil))))))
  4179. (defun org-optimize-window-after-visibility-change (state)
  4180. "Adjust the window after a change in outline visibility.
  4181. This function is the default value of the hook `org-cycle-hook'."
  4182. (when (get-buffer-window (current-buffer))
  4183. (cond
  4184. ((eq state 'content) nil)
  4185. ((eq state 'all) nil)
  4186. ((eq state 'folded) nil)
  4187. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4188. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4189. (defun org-compact-display-after-subtree-move ()
  4190. "Show a compacter version of the tree of the entry's parent."
  4191. (save-excursion
  4192. (if (org-up-heading-safe)
  4193. (progn
  4194. (hide-subtree)
  4195. (show-entry)
  4196. (show-children)
  4197. (org-cycle-show-empty-lines 'children)
  4198. (org-cycle-hide-drawers 'children))
  4199. (org-overview))))
  4200. (defun org-cycle-show-empty-lines (state)
  4201. "Show empty lines above all visible headlines.
  4202. The region to be covered depends on STATE when called through
  4203. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4204. entire buffer covered. Note that an empty line is only shown if there
  4205. are at least `org-cycle-separator-lines' empty lines before the headline."
  4206. (when (> org-cycle-separator-lines 0)
  4207. (save-excursion
  4208. (let* ((n org-cycle-separator-lines)
  4209. (re (cond
  4210. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4211. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4212. (t (let ((ns (number-to-string (- n 2))))
  4213. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4214. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4215. beg end)
  4216. (cond
  4217. ((memq state '(overview contents t))
  4218. (setq beg (point-min) end (point-max)))
  4219. ((memq state '(children folded))
  4220. (setq beg (point) end (progn (org-end-of-subtree t t)
  4221. (beginning-of-line 2)
  4222. (point)))))
  4223. (when beg
  4224. (goto-char beg)
  4225. (while (re-search-forward re end t)
  4226. (if (not (get-char-property (match-end 1) 'invisible))
  4227. (outline-flag-region
  4228. (match-beginning 1) (match-end 1) nil)))))))
  4229. ;; Never hide empty lines at the end of the file.
  4230. (save-excursion
  4231. (goto-char (point-max))
  4232. (outline-previous-heading)
  4233. (outline-end-of-heading)
  4234. (if (and (looking-at "[ \t\n]+")
  4235. (= (match-end 0) (point-max)))
  4236. (outline-flag-region (point) (match-end 0) nil))))
  4237. (defun org-show-empty-lines-in-parent ()
  4238. "Move to the parent and re-show empty lines before visible headlines."
  4239. (save-excursion
  4240. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4241. (org-cycle-show-empty-lines context))))
  4242. (defun org-cycle-hide-drawers (state)
  4243. "Re-hide all drawers after a visibility state change."
  4244. (when (and (org-mode-p)
  4245. (not (memq state '(overview folded))))
  4246. (save-excursion
  4247. (let* ((globalp (memq state '(contents all)))
  4248. (beg (if globalp (point-min) (point)))
  4249. (end (if globalp (point-max) (org-end-of-subtree t))))
  4250. (goto-char beg)
  4251. (while (re-search-forward org-drawer-regexp end t)
  4252. (org-flag-drawer t))))))
  4253. (defun org-flag-drawer (flag)
  4254. (save-excursion
  4255. (beginning-of-line 1)
  4256. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4257. (let ((b (match-end 0))
  4258. (outline-regexp org-outline-regexp))
  4259. (if (re-search-forward
  4260. "^[ \t]*:END:"
  4261. (save-excursion (outline-next-heading) (point)) t)
  4262. (outline-flag-region b (point-at-eol) flag)
  4263. (error ":END: line missing"))))))
  4264. (defun org-subtree-end-visible-p ()
  4265. "Is the end of the current subtree visible?"
  4266. (pos-visible-in-window-p
  4267. (save-excursion (org-end-of-subtree t) (point))))
  4268. (defun org-first-headline-recenter (&optional N)
  4269. "Move cursor to the first headline and recenter the headline.
  4270. Optional argument N means, put the headline into the Nth line of the window."
  4271. (goto-char (point-min))
  4272. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4273. (beginning-of-line)
  4274. (recenter (prefix-numeric-value N))))
  4275. ;;; Org-goto
  4276. (defvar org-goto-window-configuration nil)
  4277. (defvar org-goto-marker nil)
  4278. (defvar org-goto-map
  4279. (let ((map (make-sparse-keymap)))
  4280. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4281. (while (setq cmd (pop cmds))
  4282. (substitute-key-definition cmd cmd map global-map)))
  4283. (suppress-keymap map)
  4284. (org-defkey map "\C-m" 'org-goto-ret)
  4285. (org-defkey map [(return)] 'org-goto-ret)
  4286. (org-defkey map [(left)] 'org-goto-left)
  4287. (org-defkey map [(right)] 'org-goto-right)
  4288. (org-defkey map [(control ?g)] 'org-goto-quit)
  4289. (org-defkey map "\C-i" 'org-cycle)
  4290. (org-defkey map [(tab)] 'org-cycle)
  4291. (org-defkey map [(down)] 'outline-next-visible-heading)
  4292. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4293. (if org-goto-auto-isearch
  4294. (if (fboundp 'define-key-after)
  4295. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4296. nil)
  4297. (org-defkey map "q" 'org-goto-quit)
  4298. (org-defkey map "n" 'outline-next-visible-heading)
  4299. (org-defkey map "p" 'outline-previous-visible-heading)
  4300. (org-defkey map "f" 'outline-forward-same-level)
  4301. (org-defkey map "b" 'outline-backward-same-level)
  4302. (org-defkey map "u" 'outline-up-heading))
  4303. (org-defkey map "/" 'org-occur)
  4304. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4305. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4306. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4307. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4308. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4309. map))
  4310. (defconst org-goto-help
  4311. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4312. RET=jump to location [Q]uit and return to previous location
  4313. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4314. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4315. ;; FIXME: Docstring doe not mention both interfaces
  4316. (defun org-goto (&optional alternative-interface)
  4317. "Look up a different location in the current file, keeping current visibility.
  4318. When you want look-up or go to a different location in a document, the
  4319. fastest way is often to fold the entire buffer and then dive into the tree.
  4320. This method has the disadvantage, that the previous location will be folded,
  4321. which may not be what you want.
  4322. This command works around this by showing a copy of the current buffer
  4323. in an indirect buffer, in overview mode. You can dive into the tree in
  4324. that copy, use org-occur and incremental search to find a location.
  4325. When pressing RET or `Q', the command returns to the original buffer in
  4326. which the visibility is still unchanged. After RET is will also jump to
  4327. the location selected in the indirect buffer and expose the
  4328. the headline hierarchy above."
  4329. (interactive "P")
  4330. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4331. (org-refile-use-outline-path t)
  4332. (interface
  4333. (if (not alternative-interface)
  4334. org-goto-interface
  4335. (if (eq org-goto-interface 'outline)
  4336. 'outline-path-completion
  4337. 'outline)))
  4338. (org-goto-start-pos (point))
  4339. (selected-point
  4340. (if (eq interface 'outline)
  4341. (car (org-get-location (current-buffer) org-goto-help))
  4342. (nth 3 (org-refile-get-location "Goto: ")))))
  4343. (if selected-point
  4344. (progn
  4345. (org-mark-ring-push org-goto-start-pos)
  4346. (goto-char selected-point)
  4347. (if (or (org-invisible-p) (org-invisible-p2))
  4348. (org-show-context 'org-goto)))
  4349. (message "Quit"))))
  4350. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4351. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4352. (defvar org-goto-local-auto-isearch-map) ; defined below
  4353. (defun org-get-location (buf help)
  4354. "Let the user select a location in the Org-mode buffer BUF.
  4355. This function uses a recursive edit. It returns the selected position
  4356. or nil."
  4357. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4358. (isearch-hide-immediately nil)
  4359. (isearch-search-fun-function
  4360. (lambda () 'org-goto-local-search-headings))
  4361. (org-goto-selected-point org-goto-exit-command))
  4362. (save-excursion
  4363. (save-window-excursion
  4364. (delete-other-windows)
  4365. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4366. (switch-to-buffer
  4367. (condition-case nil
  4368. (make-indirect-buffer (current-buffer) "*org-goto*")
  4369. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4370. (with-output-to-temp-buffer "*Help*"
  4371. (princ help))
  4372. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4373. (setq buffer-read-only nil)
  4374. (let ((org-startup-truncated t)
  4375. (org-startup-folded nil)
  4376. (org-startup-align-all-tables nil))
  4377. (org-mode)
  4378. (org-overview))
  4379. (setq buffer-read-only t)
  4380. (if (and (boundp 'org-goto-start-pos)
  4381. (integer-or-marker-p org-goto-start-pos))
  4382. (let ((org-show-hierarchy-above t)
  4383. (org-show-siblings t)
  4384. (org-show-following-heading t))
  4385. (goto-char org-goto-start-pos)
  4386. (and (org-invisible-p) (org-show-context)))
  4387. (goto-char (point-min)))
  4388. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4389. (message "Select location and press RET")
  4390. (use-local-map org-goto-map)
  4391. (recursive-edit)
  4392. ))
  4393. (kill-buffer "*org-goto*")
  4394. (cons org-goto-selected-point org-goto-exit-command)))
  4395. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4396. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4397. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4398. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4399. (defun org-goto-local-search-headings (string bound noerror)
  4400. "Search and make sure that any matches are in headlines."
  4401. (catch 'return
  4402. (while (if isearch-forward
  4403. (search-forward string bound noerror)
  4404. (search-backward string bound noerror))
  4405. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4406. (and (member :headline context)
  4407. (not (member :tags context))))
  4408. (throw 'return (point))))))
  4409. (defun org-goto-local-auto-isearch ()
  4410. "Start isearch."
  4411. (interactive)
  4412. (goto-char (point-min))
  4413. (let ((keys (this-command-keys)))
  4414. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4415. (isearch-mode t)
  4416. (isearch-process-search-char (string-to-char keys)))))
  4417. (defun org-goto-ret (&optional arg)
  4418. "Finish `org-goto' by going to the new location."
  4419. (interactive "P")
  4420. (setq org-goto-selected-point (point)
  4421. org-goto-exit-command 'return)
  4422. (throw 'exit nil))
  4423. (defun org-goto-left ()
  4424. "Finish `org-goto' by going to the new location."
  4425. (interactive)
  4426. (if (org-on-heading-p)
  4427. (progn
  4428. (beginning-of-line 1)
  4429. (setq org-goto-selected-point (point)
  4430. org-goto-exit-command 'left)
  4431. (throw 'exit nil))
  4432. (error "Not on a heading")))
  4433. (defun org-goto-right ()
  4434. "Finish `org-goto' by going to the new location."
  4435. (interactive)
  4436. (if (org-on-heading-p)
  4437. (progn
  4438. (setq org-goto-selected-point (point)
  4439. org-goto-exit-command 'right)
  4440. (throw 'exit nil))
  4441. (error "Not on a heading")))
  4442. (defun org-goto-quit ()
  4443. "Finish `org-goto' without cursor motion."
  4444. (interactive)
  4445. (setq org-goto-selected-point nil)
  4446. (setq org-goto-exit-command 'quit)
  4447. (throw 'exit nil))
  4448. ;;; Indirect buffer display of subtrees
  4449. (defvar org-indirect-dedicated-frame nil
  4450. "This is the frame being used for indirect tree display.")
  4451. (defvar org-last-indirect-buffer nil)
  4452. (defun org-tree-to-indirect-buffer (&optional arg)
  4453. "Create indirect buffer and narrow it to current subtree.
  4454. With numerical prefix ARG, go up to this level and then take that tree.
  4455. If ARG is negative, go up that many levels.
  4456. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4457. indirect buffer previously made with this command, to avoid proliferation of
  4458. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4459. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4460. is kept so that you can work with several indirect buffers at the same time.
  4461. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4462. requests that a new frame be made for the new buffer, so that the dedicated
  4463. frame is not changed."
  4464. (interactive "P")
  4465. (let ((cbuf (current-buffer))
  4466. (cwin (selected-window))
  4467. (pos (point))
  4468. beg end level heading ibuf)
  4469. (save-excursion
  4470. (org-back-to-heading t)
  4471. (when (numberp arg)
  4472. (setq level (org-outline-level))
  4473. (if (< arg 0) (setq arg (+ level arg)))
  4474. (while (> (setq level (org-outline-level)) arg)
  4475. (outline-up-heading 1 t)))
  4476. (setq beg (point)
  4477. heading (org-get-heading))
  4478. (org-end-of-subtree t) (setq end (point)))
  4479. (if (and (buffer-live-p org-last-indirect-buffer)
  4480. (not (eq org-indirect-buffer-display 'new-frame))
  4481. (not arg))
  4482. (kill-buffer org-last-indirect-buffer))
  4483. (setq ibuf (org-get-indirect-buffer cbuf)
  4484. org-last-indirect-buffer ibuf)
  4485. (cond
  4486. ((or (eq org-indirect-buffer-display 'new-frame)
  4487. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4488. (select-frame (make-frame))
  4489. (delete-other-windows)
  4490. (switch-to-buffer ibuf)
  4491. (org-set-frame-title heading))
  4492. ((eq org-indirect-buffer-display 'dedicated-frame)
  4493. (raise-frame
  4494. (select-frame (or (and org-indirect-dedicated-frame
  4495. (frame-live-p org-indirect-dedicated-frame)
  4496. org-indirect-dedicated-frame)
  4497. (setq org-indirect-dedicated-frame (make-frame)))))
  4498. (delete-other-windows)
  4499. (switch-to-buffer ibuf)
  4500. (org-set-frame-title (concat "Indirect: " heading)))
  4501. ((eq org-indirect-buffer-display 'current-window)
  4502. (switch-to-buffer ibuf))
  4503. ((eq org-indirect-buffer-display 'other-window)
  4504. (pop-to-buffer ibuf))
  4505. (t (error "Invalid value.")))
  4506. (if (featurep 'xemacs)
  4507. (save-excursion (org-mode) (turn-on-font-lock)))
  4508. (narrow-to-region beg end)
  4509. (show-all)
  4510. (goto-char pos)
  4511. (and (window-live-p cwin) (select-window cwin))))
  4512. (defun org-get-indirect-buffer (&optional buffer)
  4513. (setq buffer (or buffer (current-buffer)))
  4514. (let ((n 1) (base (buffer-name buffer)) bname)
  4515. (while (buffer-live-p
  4516. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4517. (setq n (1+ n)))
  4518. (condition-case nil
  4519. (make-indirect-buffer buffer bname 'clone)
  4520. (error (make-indirect-buffer buffer bname)))))
  4521. (defun org-set-frame-title (title)
  4522. "Set the title of the current frame to the string TITLE."
  4523. ;; FIXME: how to name a single frame in XEmacs???
  4524. (unless (featurep 'xemacs)
  4525. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4526. ;;;; Structure editing
  4527. ;;; Inserting headlines
  4528. (defun org-previous-line-empty-p ()
  4529. (save-excursion
  4530. (and (not (bobp))
  4531. (or (beginning-of-line 0) t)
  4532. (save-match-data
  4533. (looking-at "[ \t]*$")))))
  4534. (defun org-insert-heading (&optional force-heading)
  4535. "Insert a new heading or item with same depth at point.
  4536. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4537. If point is at the beginning of a headline, insert a sibling before the
  4538. current headline. If point is not at the beginning, do not split the line,
  4539. but create the new headline after the current line."
  4540. (interactive "P")
  4541. (if (= (buffer-size) 0)
  4542. (insert "\n* ")
  4543. (when (or force-heading (not (org-insert-item)))
  4544. (let* ((empty-line-p nil)
  4545. (head (save-excursion
  4546. (condition-case nil
  4547. (progn
  4548. (org-back-to-heading)
  4549. (setq empty-line-p (org-previous-line-empty-p))
  4550. (match-string 0))
  4551. (error "*"))))
  4552. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4553. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4554. pos hide-previous previous-pos)
  4555. (cond
  4556. ((and (org-on-heading-p) (bolp)
  4557. (or (bobp)
  4558. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4559. ;; insert before the current line
  4560. (open-line (if blank 2 1)))
  4561. ((and (bolp)
  4562. (or (bobp)
  4563. (save-excursion
  4564. (backward-char 1) (not (org-invisible-p)))))
  4565. ;; insert right here
  4566. nil)
  4567. (t
  4568. ;; somewhere in the line
  4569. (save-excursion
  4570. (setq previous-pos (point-at-bol))
  4571. (end-of-line)
  4572. (setq hide-previous (org-invisible-p)))
  4573. (and org-insert-heading-respect-content (org-show-subtree))
  4574. (let ((split
  4575. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4576. (save-excursion
  4577. (let ((p (point)))
  4578. (goto-char (point-at-bol))
  4579. (and (looking-at org-complex-heading-regexp)
  4580. (> p (match-beginning 4)))))))
  4581. tags pos)
  4582. (cond
  4583. (org-insert-heading-respect-content
  4584. (org-end-of-subtree nil t)
  4585. (or (bolp) (newline))
  4586. (or (org-previous-line-empty-p)
  4587. (and blank (newline)))
  4588. (open-line 1))
  4589. ((org-on-heading-p)
  4590. (when hide-previous
  4591. (show-children)
  4592. (org-show-entry))
  4593. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4594. (setq tags (and (match-end 2) (match-string 2)))
  4595. (and (match-end 1)
  4596. (delete-region (match-beginning 1) (match-end 1)))
  4597. (setq pos (point-at-bol))
  4598. (or split (end-of-line 1))
  4599. (delete-horizontal-space)
  4600. (newline (if blank 2 1))
  4601. (when tags
  4602. (save-excursion
  4603. (goto-char pos)
  4604. (end-of-line 1)
  4605. (insert " " tags)
  4606. (org-set-tags nil 'align))))
  4607. (t
  4608. (or split (end-of-line 1))
  4609. (newline (if blank 2 1)))))))
  4610. (insert head) (just-one-space)
  4611. (setq pos (point))
  4612. (end-of-line 1)
  4613. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4614. (when (and org-insert-heading-respect-content hide-previous)
  4615. (save-excursion
  4616. (goto-char previous-pos)
  4617. (hide-subtree)))
  4618. (run-hooks 'org-insert-heading-hook)))))
  4619. (defun org-get-heading (&optional no-tags)
  4620. "Return the heading of the current entry, without the stars."
  4621. (save-excursion
  4622. (org-back-to-heading t)
  4623. (if (looking-at
  4624. (if no-tags
  4625. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4626. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4627. (match-string 1) "")))
  4628. (defun org-heading-components ()
  4629. "Return the components of the current heading.
  4630. This is a list with the following elements:
  4631. - the level as an integer
  4632. - the reduced level, different if `org-odd-levels-only' is set.
  4633. - the TODO keyword, or nil
  4634. - the priority character, like ?A, or nil if no priority is given
  4635. - the headline text itself, or the tags string if no headline text
  4636. - the tags string, or nil."
  4637. (save-excursion
  4638. (org-back-to-heading t)
  4639. (if (looking-at org-complex-heading-regexp)
  4640. (list (length (match-string 1))
  4641. (org-reduced-level (length (match-string 1)))
  4642. (org-match-string-no-properties 2)
  4643. (and (match-end 3) (aref (match-string 3) 2))
  4644. (org-match-string-no-properties 4)
  4645. (org-match-string-no-properties 5)))))
  4646. (defun org-insert-heading-after-current ()
  4647. "Insert a new heading with same level as current, after current subtree."
  4648. (interactive)
  4649. (org-back-to-heading)
  4650. (org-insert-heading)
  4651. (org-move-subtree-down)
  4652. (end-of-line 1))
  4653. (defun org-insert-heading-respect-content ()
  4654. (interactive)
  4655. (let ((org-insert-heading-respect-content t))
  4656. (org-insert-heading t)))
  4657. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4658. (interactive "P")
  4659. (let ((org-insert-heading-respect-content t))
  4660. (org-insert-todo-heading force-state t)))
  4661. (defun org-insert-todo-heading (arg &optional force-heading)
  4662. "Insert a new heading with the same level and TODO state as current heading.
  4663. If the heading has no TODO state, or if the state is DONE, use the first
  4664. state (TODO by default). Also with prefix arg, force first state."
  4665. (interactive "P")
  4666. (when (or force-heading (not (org-insert-item 'checkbox)))
  4667. (org-insert-heading force-heading)
  4668. (save-excursion
  4669. (org-back-to-heading)
  4670. (outline-previous-heading)
  4671. (looking-at org-todo-line-regexp))
  4672. (let*
  4673. ((new-mark-x
  4674. (if (or arg
  4675. (not (match-beginning 2))
  4676. (member (match-string 2) org-done-keywords))
  4677. (car org-todo-keywords-1)
  4678. (match-string 2)))
  4679. (new-mark
  4680. (or
  4681. (run-hook-with-args-until-success
  4682. 'org-todo-get-default-hook new-mark-x nil)
  4683. new-mark-x)))
  4684. (insert new-mark " "))
  4685. (when org-provide-todo-statistics
  4686. (org-update-parent-todo-statistics))))
  4687. (defun org-insert-subheading (arg)
  4688. "Insert a new subheading and demote it.
  4689. Works for outline headings and for plain lists alike."
  4690. (interactive "P")
  4691. (org-insert-heading arg)
  4692. (cond
  4693. ((org-on-heading-p) (org-do-demote))
  4694. ((org-at-item-p) (org-indent-item 1))))
  4695. (defun org-insert-todo-subheading (arg)
  4696. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4697. Works for outline headings and for plain lists alike."
  4698. (interactive "P")
  4699. (org-insert-todo-heading arg)
  4700. (cond
  4701. ((org-on-heading-p) (org-do-demote))
  4702. ((org-at-item-p) (org-indent-item 1))))
  4703. ;;; Promotion and Demotion
  4704. (defun org-promote-subtree ()
  4705. "Promote the entire subtree.
  4706. See also `org-promote'."
  4707. (interactive)
  4708. (save-excursion
  4709. (org-map-tree 'org-promote))
  4710. (org-fix-position-after-promote))
  4711. (defun org-demote-subtree ()
  4712. "Demote the entire subtree. See `org-demote'.
  4713. See also `org-promote'."
  4714. (interactive)
  4715. (save-excursion
  4716. (org-map-tree 'org-demote))
  4717. (org-fix-position-after-promote))
  4718. (defun org-do-promote ()
  4719. "Promote the current heading higher up the tree.
  4720. If the region is active in `transient-mark-mode', promote all headings
  4721. in the region."
  4722. (interactive)
  4723. (save-excursion
  4724. (if (org-region-active-p)
  4725. (org-map-region 'org-promote (region-beginning) (region-end))
  4726. (org-promote)))
  4727. (org-fix-position-after-promote))
  4728. (defun org-do-demote ()
  4729. "Demote the current heading lower down the tree.
  4730. If the region is active in `transient-mark-mode', demote all headings
  4731. in the region."
  4732. (interactive)
  4733. (save-excursion
  4734. (if (org-region-active-p)
  4735. (org-map-region 'org-demote (region-beginning) (region-end))
  4736. (org-demote)))
  4737. (org-fix-position-after-promote))
  4738. (defun org-fix-position-after-promote ()
  4739. "Make sure that after pro/demotion cursor position is right."
  4740. (let ((pos (point)))
  4741. (when (save-excursion
  4742. (beginning-of-line 1)
  4743. (looking-at org-todo-line-regexp)
  4744. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4745. (cond ((eobp) (insert " "))
  4746. ((eolp) (insert " "))
  4747. ((equal (char-after) ?\ ) (forward-char 1))))))
  4748. (defun org-reduced-level (l)
  4749. "Compute the effective level of a heading.
  4750. This takes into account the setting of `org-odd-levels-only'."
  4751. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4752. (defun org-get-valid-level (level &optional change)
  4753. "Rectify a level change under the influence of `org-odd-levels-only'
  4754. LEVEL is a current level, CHANGE is by how much the level should be
  4755. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4756. even level numbers will become the next higher odd number."
  4757. (if org-odd-levels-only
  4758. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4759. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4760. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4761. (max 1 (+ level change))))
  4762. (if (boundp 'define-obsolete-function-alias)
  4763. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4764. (define-obsolete-function-alias 'org-get-legal-level
  4765. 'org-get-valid-level)
  4766. (define-obsolete-function-alias 'org-get-legal-level
  4767. 'org-get-valid-level "23.1")))
  4768. (defun org-promote ()
  4769. "Promote the current heading higher up the tree.
  4770. If the region is active in `transient-mark-mode', promote all headings
  4771. in the region."
  4772. (org-back-to-heading t)
  4773. (let* ((level (save-match-data (funcall outline-level)))
  4774. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4775. (diff (abs (- level (length up-head) -1))))
  4776. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4777. (replace-match up-head nil t)
  4778. ;; Fixup tag positioning
  4779. (and org-auto-align-tags (org-set-tags nil t))
  4780. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4781. (defun org-demote ()
  4782. "Demote the current heading lower down the tree.
  4783. If the region is active in `transient-mark-mode', demote all headings
  4784. in the region."
  4785. (org-back-to-heading t)
  4786. (let* ((level (save-match-data (funcall outline-level)))
  4787. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4788. (diff (abs (- level (length down-head) -1))))
  4789. (replace-match down-head nil t)
  4790. ;; Fixup tag positioning
  4791. (and org-auto-align-tags (org-set-tags nil t))
  4792. (if org-adapt-indentation (org-fixup-indentation diff))))
  4793. (defun org-map-tree (fun)
  4794. "Call FUN for every heading underneath the current one."
  4795. (org-back-to-heading)
  4796. (let ((level (funcall outline-level)))
  4797. (save-excursion
  4798. (funcall fun)
  4799. (while (and (progn
  4800. (outline-next-heading)
  4801. (> (funcall outline-level) level))
  4802. (not (eobp)))
  4803. (funcall fun)))))
  4804. (defun org-map-region (fun beg end)
  4805. "Call FUN for every heading between BEG and END."
  4806. (let ((org-ignore-region t))
  4807. (save-excursion
  4808. (setq end (copy-marker end))
  4809. (goto-char beg)
  4810. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4811. (< (point) end))
  4812. (funcall fun))
  4813. (while (and (progn
  4814. (outline-next-heading)
  4815. (< (point) end))
  4816. (not (eobp)))
  4817. (funcall fun)))))
  4818. (defun org-fixup-indentation (diff)
  4819. "Change the indentation in the current entry by DIFF
  4820. However, if any line in the current entry has no indentation, or if it
  4821. would end up with no indentation after the change, nothing at all is done."
  4822. (save-excursion
  4823. (let ((end (save-excursion (outline-next-heading)
  4824. (point-marker)))
  4825. (prohibit (if (> diff 0)
  4826. "^\\S-"
  4827. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4828. col)
  4829. (unless (save-excursion (end-of-line 1)
  4830. (re-search-forward prohibit end t))
  4831. (while (and (< (point) end)
  4832. (re-search-forward "^[ \t]+" end t))
  4833. (goto-char (match-end 0))
  4834. (setq col (current-column))
  4835. (if (< diff 0) (replace-match ""))
  4836. (org-indent-to-column (+ diff col))))
  4837. (move-marker end nil))))
  4838. (defun org-convert-to-odd-levels ()
  4839. "Convert an org-mode file with all levels allowed to one with odd levels.
  4840. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4841. level 5 etc."
  4842. (interactive)
  4843. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4844. (let ((org-odd-levels-only nil) n)
  4845. (save-excursion
  4846. (goto-char (point-min))
  4847. (while (re-search-forward "^\\*\\*+ " nil t)
  4848. (setq n (- (length (match-string 0)) 2))
  4849. (while (>= (setq n (1- n)) 0)
  4850. (org-demote))
  4851. (end-of-line 1))))))
  4852. (defun org-convert-to-oddeven-levels ()
  4853. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4854. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4855. section with an even level, conversion would destroy the structure of the file. An error
  4856. is signaled in this case."
  4857. (interactive)
  4858. (goto-char (point-min))
  4859. ;; First check if there are no even levels
  4860. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4861. (org-show-context t)
  4862. (error "Not all levels are odd in this file. Conversion not possible."))
  4863. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4864. (let ((org-odd-levels-only nil) n)
  4865. (save-excursion
  4866. (goto-char (point-min))
  4867. (while (re-search-forward "^\\*\\*+ " nil t)
  4868. (setq n (/ (1- (length (match-string 0))) 2))
  4869. (while (>= (setq n (1- n)) 0)
  4870. (org-promote))
  4871. (end-of-line 1))))))
  4872. (defun org-tr-level (n)
  4873. "Make N odd if required."
  4874. (if org-odd-levels-only (1+ (/ n 2)) n))
  4875. ;;; Vertical tree motion, cutting and pasting of subtrees
  4876. (defun org-move-subtree-up (&optional arg)
  4877. "Move the current subtree up past ARG headlines of the same level."
  4878. (interactive "p")
  4879. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4880. (defun org-move-subtree-down (&optional arg)
  4881. "Move the current subtree down past ARG headlines of the same level."
  4882. (interactive "p")
  4883. (setq arg (prefix-numeric-value arg))
  4884. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4885. 'outline-get-last-sibling))
  4886. (ins-point (make-marker))
  4887. (cnt (abs arg))
  4888. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4889. ;; Select the tree
  4890. (org-back-to-heading)
  4891. (setq beg0 (point))
  4892. (save-excursion
  4893. (setq ne-beg (org-back-over-empty-lines))
  4894. (setq beg (point)))
  4895. (save-match-data
  4896. (save-excursion (outline-end-of-heading)
  4897. (setq folded (org-invisible-p)))
  4898. (outline-end-of-subtree))
  4899. (outline-next-heading)
  4900. (setq ne-end (org-back-over-empty-lines))
  4901. (setq end (point))
  4902. (goto-char beg0)
  4903. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4904. ;; include less whitespace
  4905. (save-excursion
  4906. (goto-char beg)
  4907. (forward-line (- ne-beg ne-end))
  4908. (setq beg (point))))
  4909. ;; Find insertion point, with error handling
  4910. (while (> cnt 0)
  4911. (or (and (funcall movfunc) (looking-at outline-regexp))
  4912. (progn (goto-char beg0)
  4913. (error "Cannot move past superior level or buffer limit")))
  4914. (setq cnt (1- cnt)))
  4915. (if (> arg 0)
  4916. ;; Moving forward - still need to move over subtree
  4917. (progn (org-end-of-subtree t t)
  4918. (save-excursion
  4919. (org-back-over-empty-lines)
  4920. (or (bolp) (newline)))))
  4921. (setq ne-ins (org-back-over-empty-lines))
  4922. (move-marker ins-point (point))
  4923. (setq txt (buffer-substring beg end))
  4924. (org-save-markers-in-region beg end)
  4925. (delete-region beg end)
  4926. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4927. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4928. (let ((bbb (point)))
  4929. (insert-before-markers txt)
  4930. (org-reinstall-markers-in-region bbb)
  4931. (move-marker ins-point bbb))
  4932. (or (bolp) (insert "\n"))
  4933. (setq ins-end (point))
  4934. (goto-char ins-point)
  4935. (org-skip-whitespace)
  4936. (when (and (< arg 0)
  4937. (org-first-sibling-p)
  4938. (> ne-ins ne-beg))
  4939. ;; Move whitespace back to beginning
  4940. (save-excursion
  4941. (goto-char ins-end)
  4942. (let ((kill-whole-line t))
  4943. (kill-line (- ne-ins ne-beg)) (point)))
  4944. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4945. (move-marker ins-point nil)
  4946. (org-compact-display-after-subtree-move)
  4947. (org-show-empty-lines-in-parent)
  4948. (unless folded
  4949. (org-show-entry)
  4950. (show-children)
  4951. (org-cycle-hide-drawers 'children))))
  4952. (defvar org-subtree-clip ""
  4953. "Clipboard for cut and paste of subtrees.
  4954. This is actually only a copy of the kill, because we use the normal kill
  4955. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4956. (defvar org-subtree-clip-folded nil
  4957. "Was the last copied subtree folded?
  4958. This is used to fold the tree back after pasting.")
  4959. (defun org-cut-subtree (&optional n)
  4960. "Cut the current subtree into the clipboard.
  4961. With prefix arg N, cut this many sequential subtrees.
  4962. This is a short-hand for marking the subtree and then cutting it."
  4963. (interactive "p")
  4964. (org-copy-subtree n 'cut))
  4965. (defun org-copy-subtree (&optional n cut force-store-markers)
  4966. "Cut the current subtree into the clipboard.
  4967. With prefix arg N, cut this many sequential subtrees.
  4968. This is a short-hand for marking the subtree and then copying it.
  4969. If CUT is non-nil, actually cut the subtree.
  4970. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4971. of some markers in the region, even if CUT is non-nil. This is
  4972. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4973. (interactive "p")
  4974. (let (beg end folded (beg0 (point)))
  4975. (if (interactive-p)
  4976. (org-back-to-heading nil) ; take what looks like a subtree
  4977. (org-back-to-heading t)) ; take what is really there
  4978. (org-back-over-empty-lines)
  4979. (setq beg (point))
  4980. (skip-chars-forward " \t\r\n")
  4981. (save-match-data
  4982. (save-excursion (outline-end-of-heading)
  4983. (setq folded (org-invisible-p)))
  4984. (condition-case nil
  4985. (outline-forward-same-level (1- n))
  4986. (error nil))
  4987. (org-end-of-subtree t t))
  4988. (org-back-over-empty-lines)
  4989. (setq end (point))
  4990. (goto-char beg0)
  4991. (when (> end beg)
  4992. (setq org-subtree-clip-folded folded)
  4993. (when (or cut force-store-markers)
  4994. (org-save-markers-in-region beg end))
  4995. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4996. (setq org-subtree-clip (current-kill 0))
  4997. (message "%s: Subtree(s) with %d characters"
  4998. (if cut "Cut" "Copied")
  4999. (length org-subtree-clip)))))
  5000. (defun org-paste-subtree (&optional level tree for-yank)
  5001. "Paste the clipboard as a subtree, with modification of headline level.
  5002. The entire subtree is promoted or demoted in order to match a new headline
  5003. level.
  5004. If the cursor is at the beginning of a headline, the same level as
  5005. that headline is used to paste the tree
  5006. If not, the new level is derived from the *visible* headings
  5007. before and after the insertion point, and taken to be the inferior headline
  5008. level of the two. So if the previous visible heading is level 3 and the
  5009. next is level 4 (or vice versa), level 4 will be used for insertion.
  5010. This makes sure that the subtree remains an independent subtree and does
  5011. not swallow low level entries.
  5012. You can also force a different level, either by using a numeric prefix
  5013. argument, or by inserting the heading marker by hand. For example, if the
  5014. cursor is after \"*****\", then the tree will be shifted to level 5.
  5015. If optional TREE is given, use this text instead of the kill ring.
  5016. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5017. move back over whitespace before inserting, and move point to the end of
  5018. the inserted text when done."
  5019. (interactive "P")
  5020. (unless (org-kill-is-subtree-p tree)
  5021. (error "%s"
  5022. (substitute-command-keys
  5023. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5024. (let* ((visp (not (org-invisible-p)))
  5025. (txt (or tree (and kill-ring (current-kill 0))))
  5026. (^re (concat "^\\(" outline-regexp "\\)"))
  5027. (re (concat "\\(" outline-regexp "\\)"))
  5028. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5029. (old-level (if (string-match ^re txt)
  5030. (- (match-end 0) (match-beginning 0) 1)
  5031. -1))
  5032. (force-level (cond (level (prefix-numeric-value level))
  5033. ((and (looking-at "[ \t]*$")
  5034. (string-match
  5035. ^re_ (buffer-substring
  5036. (point-at-bol) (point))))
  5037. (- (match-end 1) (match-beginning 1)))
  5038. ((and (bolp)
  5039. (looking-at org-outline-regexp))
  5040. (- (match-end 0) (point) 1))
  5041. (t nil)))
  5042. (previous-level (save-excursion
  5043. (condition-case nil
  5044. (progn
  5045. (outline-previous-visible-heading 1)
  5046. (if (looking-at re)
  5047. (- (match-end 0) (match-beginning 0) 1)
  5048. 1))
  5049. (error 1))))
  5050. (next-level (save-excursion
  5051. (condition-case nil
  5052. (progn
  5053. (or (looking-at outline-regexp)
  5054. (outline-next-visible-heading 1))
  5055. (if (looking-at re)
  5056. (- (match-end 0) (match-beginning 0) 1)
  5057. 1))
  5058. (error 1))))
  5059. (new-level (or force-level (max previous-level next-level)))
  5060. (shift (if (or (= old-level -1)
  5061. (= new-level -1)
  5062. (= old-level new-level))
  5063. 0
  5064. (- new-level old-level)))
  5065. (delta (if (> shift 0) -1 1))
  5066. (func (if (> shift 0) 'org-demote 'org-promote))
  5067. (org-odd-levels-only nil)
  5068. beg end newend)
  5069. ;; Remove the forced level indicator
  5070. (if force-level
  5071. (delete-region (point-at-bol) (point)))
  5072. ;; Paste
  5073. (beginning-of-line 1)
  5074. (unless for-yank (org-back-over-empty-lines))
  5075. (setq beg (point))
  5076. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5077. (insert-before-markers txt)
  5078. (unless (string-match "\n\\'" txt) (insert "\n"))
  5079. (setq newend (point))
  5080. (org-reinstall-markers-in-region beg)
  5081. (setq end (point))
  5082. (goto-char beg)
  5083. (skip-chars-forward " \t\n\r")
  5084. (setq beg (point))
  5085. (if (and (org-invisible-p) visp)
  5086. (save-excursion (outline-show-heading)))
  5087. ;; Shift if necessary
  5088. (unless (= shift 0)
  5089. (save-restriction
  5090. (narrow-to-region beg end)
  5091. (while (not (= shift 0))
  5092. (org-map-region func (point-min) (point-max))
  5093. (setq shift (+ delta shift)))
  5094. (goto-char (point-min))
  5095. (setq newend (point-max))))
  5096. (when (or (interactive-p) for-yank)
  5097. (message "Clipboard pasted as level %d subtree" new-level))
  5098. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5099. kill-ring
  5100. (eq org-subtree-clip (current-kill 0))
  5101. org-subtree-clip-folded)
  5102. ;; The tree was folded before it was killed/copied
  5103. (hide-subtree))
  5104. (and for-yank (goto-char newend))))
  5105. (defun org-kill-is-subtree-p (&optional txt)
  5106. "Check if the current kill is an outline subtree, or a set of trees.
  5107. Returns nil if kill does not start with a headline, or if the first
  5108. headline level is not the largest headline level in the tree.
  5109. So this will actually accept several entries of equal levels as well,
  5110. which is OK for `org-paste-subtree'.
  5111. If optional TXT is given, check this string instead of the current kill."
  5112. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5113. (start-level (and kill
  5114. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5115. org-outline-regexp "\\)")
  5116. kill)
  5117. (- (match-end 2) (match-beginning 2) 1)))
  5118. (re (concat "^" org-outline-regexp))
  5119. (start (1+ (or (match-beginning 2) -1))))
  5120. (if (not start-level)
  5121. (progn
  5122. nil) ;; does not even start with a heading
  5123. (catch 'exit
  5124. (while (setq start (string-match re kill (1+ start)))
  5125. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5126. (throw 'exit nil)))
  5127. t))))
  5128. (defvar org-markers-to-move nil
  5129. "Markers that should be moved with a cut-and-paste operation.
  5130. Those markers are stored together with their positions relative to
  5131. the start of the region.")
  5132. (defun org-save-markers-in-region (beg end)
  5133. "Check markers in region.
  5134. If these markers are between BEG and END, record their position relative
  5135. to BEG, so that after moving the block of text, we can put the markers back
  5136. into place.
  5137. This function gets called just before an entry or tree gets cut from the
  5138. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5139. called immediately, to move the markers with the entries."
  5140. (setq org-markers-to-move nil)
  5141. (when (featurep 'org-clock)
  5142. (org-clock-save-markers-for-cut-and-paste beg end))
  5143. (when (featurep 'org-agenda)
  5144. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5145. (defun org-check-and-save-marker (marker beg end)
  5146. "Check if MARKER is between BEG and END.
  5147. If yes, remember the marker and the distance to BEG."
  5148. (when (and (marker-buffer marker)
  5149. (equal (marker-buffer marker) (current-buffer)))
  5150. (if (and (>= marker beg) (< marker end))
  5151. (push (cons marker (- marker beg)) org-markers-to-move))))
  5152. (defun org-reinstall-markers-in-region (beg)
  5153. "Move all remembered markers to their position relative to BEG."
  5154. (mapc (lambda (x)
  5155. (move-marker (car x) (+ beg (cdr x))))
  5156. org-markers-to-move)
  5157. (setq org-markers-to-move nil))
  5158. (defun org-narrow-to-subtree ()
  5159. "Narrow buffer to the current subtree."
  5160. (interactive)
  5161. (save-excursion
  5162. (save-match-data
  5163. (narrow-to-region
  5164. (progn (org-back-to-heading) (point))
  5165. (progn (org-end-of-subtree t) (point))))))
  5166. ;;; Outline Sorting
  5167. (defun org-sort (with-case)
  5168. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5169. Optional argument WITH-CASE means sort case-sensitively.
  5170. With a double prefix argument, also remove duplicate entries."
  5171. (interactive "P")
  5172. (if (org-at-table-p)
  5173. (org-call-with-arg 'org-table-sort-lines with-case)
  5174. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5175. (defun org-sort-remove-invisible (s)
  5176. (remove-text-properties 0 (length s) org-rm-props s)
  5177. (while (string-match org-bracket-link-regexp s)
  5178. (setq s (replace-match (if (match-end 2)
  5179. (match-string 3 s)
  5180. (match-string 1 s)) t t s)))
  5181. s)
  5182. (defvar org-priority-regexp) ; defined later in the file
  5183. (defun org-sort-entries-or-items
  5184. (&optional with-case sorting-type getkey-func compare-func property)
  5185. "Sort entries on a certain level of an outline tree.
  5186. If there is an active region, the entries in the region are sorted.
  5187. Else, if the cursor is before the first entry, sort the top-level items.
  5188. Else, the children of the entry at point are sorted.
  5189. Sorting can be alphabetically, numerically, and by date/time as given by
  5190. the first time stamp in the entry. The command prompts for the sorting
  5191. type unless it has been given to the function through the SORTING-TYPE
  5192. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5193. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5194. called with point at the beginning of the record. It must return either
  5195. a string or a number that should serve as the sorting key for that record.
  5196. Comparing entries ignores case by default. However, with an optional argument
  5197. WITH-CASE, the sorting considers case as well."
  5198. (interactive "P")
  5199. (let ((case-func (if with-case 'identity 'downcase))
  5200. start beg end stars re re2
  5201. txt what tmp plain-list-p)
  5202. ;; Find beginning and end of region to sort
  5203. (cond
  5204. ((org-region-active-p)
  5205. ;; we will sort the region
  5206. (setq end (region-end)
  5207. what "region")
  5208. (goto-char (region-beginning))
  5209. (if (not (org-on-heading-p)) (outline-next-heading))
  5210. (setq start (point)))
  5211. ((org-at-item-p)
  5212. ;; we will sort this plain list
  5213. (org-beginning-of-item-list) (setq start (point))
  5214. (org-end-of-item-list) (setq end (point))
  5215. (goto-char start)
  5216. (setq plain-list-p t
  5217. what "plain list"))
  5218. ((or (org-on-heading-p)
  5219. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5220. ;; we will sort the children of the current headline
  5221. (org-back-to-heading)
  5222. (setq start (point)
  5223. end (progn (org-end-of-subtree t t)
  5224. (org-back-over-empty-lines)
  5225. (point))
  5226. what "children")
  5227. (goto-char start)
  5228. (show-subtree)
  5229. (outline-next-heading))
  5230. (t
  5231. ;; we will sort the top-level entries in this file
  5232. (goto-char (point-min))
  5233. (or (org-on-heading-p) (outline-next-heading))
  5234. (setq start (point) end (point-max) what "top-level")
  5235. (goto-char start)
  5236. (show-all)))
  5237. (setq beg (point))
  5238. (if (>= beg end) (error "Nothing to sort"))
  5239. (unless plain-list-p
  5240. (looking-at "\\(\\*+\\)")
  5241. (setq stars (match-string 1)
  5242. re (concat "^" (regexp-quote stars) " +")
  5243. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5244. txt (buffer-substring beg end))
  5245. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5246. (if (and (not (equal stars "*")) (string-match re2 txt))
  5247. (error "Region to sort contains a level above the first entry")))
  5248. (unless sorting-type
  5249. (message
  5250. (if plain-list-p
  5251. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5252. "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:")
  5253. what)
  5254. (setq sorting-type (read-char-exclusive))
  5255. (and (= (downcase sorting-type) ?f)
  5256. (setq getkey-func
  5257. (org-ido-completing-read "Sort using function: "
  5258. obarray 'fboundp t nil nil))
  5259. (setq getkey-func (intern getkey-func)))
  5260. (and (= (downcase sorting-type) ?r)
  5261. (setq property
  5262. (org-ido-completing-read "Property: "
  5263. (mapcar 'list (org-buffer-property-keys t))
  5264. nil t))))
  5265. (message "Sorting entries...")
  5266. (save-restriction
  5267. (narrow-to-region start end)
  5268. (let ((dcst (downcase sorting-type))
  5269. (now (current-time)))
  5270. (sort-subr
  5271. (/= dcst sorting-type)
  5272. ;; This function moves to the beginning character of the "record" to
  5273. ;; be sorted.
  5274. (if plain-list-p
  5275. (lambda nil
  5276. (if (org-at-item-p) t (goto-char (point-max))))
  5277. (lambda nil
  5278. (if (re-search-forward re nil t)
  5279. (goto-char (match-beginning 0))
  5280. (goto-char (point-max)))))
  5281. ;; This function moves to the last character of the "record" being
  5282. ;; sorted.
  5283. (if plain-list-p
  5284. 'org-end-of-item
  5285. (lambda nil
  5286. (save-match-data
  5287. (condition-case nil
  5288. (outline-forward-same-level 1)
  5289. (error
  5290. (goto-char (point-max)))))))
  5291. ;; This function returns the value that gets sorted against.
  5292. (if plain-list-p
  5293. (lambda nil
  5294. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5295. (cond
  5296. ((= dcst ?n)
  5297. (string-to-number (buffer-substring (match-end 0)
  5298. (point-at-eol))))
  5299. ((= dcst ?a)
  5300. (buffer-substring (match-end 0) (point-at-eol)))
  5301. ((= dcst ?t)
  5302. (if (re-search-forward org-ts-regexp
  5303. (point-at-eol) t)
  5304. (org-time-string-to-time (match-string 0))
  5305. now))
  5306. ((= dcst ?f)
  5307. (if getkey-func
  5308. (progn
  5309. (setq tmp (funcall getkey-func))
  5310. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5311. tmp)
  5312. (error "Invalid key function `%s'" getkey-func)))
  5313. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5314. (lambda nil
  5315. (cond
  5316. ((= dcst ?n)
  5317. (if (looking-at org-complex-heading-regexp)
  5318. (string-to-number (match-string 4))
  5319. nil))
  5320. ((= dcst ?a)
  5321. (if (looking-at org-complex-heading-regexp)
  5322. (funcall case-func (match-string 4))
  5323. nil))
  5324. ((= dcst ?t)
  5325. (if (re-search-forward org-ts-regexp
  5326. (save-excursion
  5327. (forward-line 2)
  5328. (point)) t)
  5329. (org-time-string-to-time (match-string 0))
  5330. now))
  5331. ((= dcst ?p)
  5332. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5333. (string-to-char (match-string 2))
  5334. org-default-priority))
  5335. ((= dcst ?r)
  5336. (or (org-entry-get nil property) ""))
  5337. ((= dcst ?o)
  5338. (if (looking-at org-complex-heading-regexp)
  5339. (- 9999 (length (member (match-string 2)
  5340. org-todo-keywords-1)))))
  5341. ((= dcst ?f)
  5342. (if getkey-func
  5343. (progn
  5344. (setq tmp (funcall getkey-func))
  5345. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5346. tmp)
  5347. (error "Invalid key function `%s'" getkey-func)))
  5348. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5349. nil
  5350. (cond
  5351. ((= dcst ?a) 'string<)
  5352. ((= dcst ?t) 'time-less-p)
  5353. ((= dcst ?f) compare-func)
  5354. (t nil)))))
  5355. (message "Sorting entries...done")))
  5356. (defun org-do-sort (table what &optional with-case sorting-type)
  5357. "Sort TABLE of WHAT according to SORTING-TYPE.
  5358. The user will be prompted for the SORTING-TYPE if the call to this
  5359. function does not specify it. WHAT is only for the prompt, to indicate
  5360. what is being sorted. The sorting key will be extracted from
  5361. the car of the elements of the table.
  5362. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5363. (unless sorting-type
  5364. (message
  5365. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5366. what)
  5367. (setq sorting-type (read-char-exclusive)))
  5368. (let ((dcst (downcase sorting-type))
  5369. extractfun comparefun)
  5370. ;; Define the appropriate functions
  5371. (cond
  5372. ((= dcst ?n)
  5373. (setq extractfun 'string-to-number
  5374. comparefun (if (= dcst sorting-type) '< '>)))
  5375. ((= dcst ?a)
  5376. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5377. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5378. comparefun (if (= dcst sorting-type)
  5379. 'string<
  5380. (lambda (a b) (and (not (string< a b))
  5381. (not (string= a b)))))))
  5382. ((= dcst ?t)
  5383. (setq extractfun
  5384. (lambda (x)
  5385. (if (or (string-match org-ts-regexp x)
  5386. (string-match org-ts-regexp-both x))
  5387. (time-to-seconds
  5388. (org-time-string-to-time (match-string 0 x)))
  5389. 0))
  5390. comparefun (if (= dcst sorting-type) '< '>)))
  5391. (t (error "Invalid sorting type `%c'" sorting-type)))
  5392. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5393. table)
  5394. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5395. ;;; Editing source examples
  5396. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5397. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5398. (defvar org-edit-src-force-single-line nil)
  5399. (defvar org-edit-src-from-org-mode nil)
  5400. (defvar org-edit-src-picture nil)
  5401. (define-minor-mode org-exit-edit-mode
  5402. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5403. (defun org-edit-src-code ()
  5404. "Edit the source code example at point.
  5405. An indirect buffer is created, and that buffer is then narrowed to the
  5406. example at point and switched to the correct language mode. When done,
  5407. exit by killing the buffer with \\[org-edit-src-exit]."
  5408. (interactive)
  5409. (let ((line (org-current-line))
  5410. (case-fold-search t)
  5411. (msg (substitute-command-keys
  5412. "Edit, then exit with C-c ' (C-c and single quote)"))
  5413. (info (org-edit-src-find-region-and-lang))
  5414. (org-mode-p (eq major-mode 'org-mode))
  5415. beg end lang lang-f single lfmt)
  5416. (if (not info)
  5417. nil
  5418. (setq beg (nth 0 info)
  5419. end (nth 1 info)
  5420. lang (nth 2 info)
  5421. single (nth 3 info)
  5422. lfmt (nth 4 info)
  5423. lang-f (intern (concat lang "-mode")))
  5424. (unless (functionp lang-f)
  5425. (error "No such language mode: %s" lang-f))
  5426. (goto-line line)
  5427. (if (get-buffer "*Org Edit Src Example*")
  5428. (kill-buffer "*Org Edit Src Example*"))
  5429. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5430. "*Org Edit Src Example*"))
  5431. (narrow-to-region beg end)
  5432. (remove-text-properties beg end '(display nil invisible nil
  5433. intangible nil))
  5434. (let ((org-inhibit-startup t))
  5435. (funcall lang-f))
  5436. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5437. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5438. (when lfmt
  5439. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5440. (when org-mode-p
  5441. (goto-char (point-min))
  5442. (while (re-search-forward "^," nil t)
  5443. (replace-match "")))
  5444. (goto-line line)
  5445. (org-exit-edit-mode)
  5446. (org-set-local 'header-line-format msg)
  5447. (message "%s" msg)
  5448. t)))
  5449. (defun org-edit-fixed-width-region ()
  5450. "Edit the fixed-width ascii drawing at point.
  5451. This must be a region where each line starts with a colon followed by
  5452. a space character.
  5453. An indirect buffer is created, and that buffer is then narrowed to the
  5454. example at point and switched to artist-mode. When done,
  5455. exit by killing the buffer with \\[org-edit-src-exit]."
  5456. (interactive)
  5457. (let ((line (org-current-line))
  5458. (case-fold-search t)
  5459. (msg (substitute-command-keys
  5460. "Edit, then exit with C-c ' (C-c and single quote)"))
  5461. (org-mode-p (eq major-mode 'org-mode))
  5462. beg end)
  5463. (beginning-of-line 1)
  5464. (if (looking-at "[ \t]*[^:\n \t]")
  5465. nil
  5466. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5467. (setq beg (point) end beg)
  5468. (save-excursion
  5469. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5470. (setq beg (point-at-bol 2))
  5471. (setq beg (point))))
  5472. (save-excursion
  5473. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5474. (setq end (1- (match-beginning 0)))
  5475. (setq end (point))))
  5476. (goto-line line))
  5477. (if (get-buffer "*Org Edit Picture*")
  5478. (kill-buffer "*Org Edit Picture*"))
  5479. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5480. "*Org Edit Picture*"))
  5481. (narrow-to-region beg end)
  5482. (remove-text-properties beg end '(display nil invisible nil
  5483. intangible nil))
  5484. (when (fboundp 'font-lock-unfontify-region)
  5485. (font-lock-unfontify-region (point-min) (point-max)))
  5486. (cond
  5487. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5488. (fundamental-mode)
  5489. (artist-mode 1))
  5490. (t (funcall org-edit-fixed-width-region-mode)))
  5491. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5492. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5493. (set (make-local-variable 'org-edit-src-picture) t)
  5494. (goto-char (point-min))
  5495. (while (re-search-forward "^[ \t]*: ?" nil t)
  5496. (replace-match ""))
  5497. (goto-line line)
  5498. (org-exit-edit-mode)
  5499. (org-set-local 'header-line-format msg)
  5500. (message "%s" msg)
  5501. t)))
  5502. (defun org-edit-src-find-region-and-lang ()
  5503. "Find the region and language for a local edit.
  5504. Return a list with beginning and end of the region, a string representing
  5505. the language, a switch telling of the content should be in a single line."
  5506. (let ((re-list
  5507. (append
  5508. org-edit-src-region-extra
  5509. '(
  5510. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5511. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5512. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5513. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5514. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5515. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5516. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5517. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5518. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5519. ("^#\\+html:" "\n" "html" single-line)
  5520. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5521. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5522. ("^#\\+latex:" "\n" "latex" single-line)
  5523. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5524. ("^#\\+ascii:" "\n" "ascii" single-line)
  5525. )))
  5526. (pos (point))
  5527. re1 re2 single beg end lang lfmt match-re1)
  5528. (catch 'exit
  5529. (while (setq entry (pop re-list))
  5530. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5531. single (nth 3 entry))
  5532. (save-excursion
  5533. (if (or (looking-at re1)
  5534. (re-search-backward re1 nil t))
  5535. (progn
  5536. (setq match-re1 (match-string 0))
  5537. (setq beg (match-end 0)
  5538. lang (org-edit-src-get-lang lang)
  5539. lfmt (org-edit-src-get-label-format match-re1))
  5540. (if (and (re-search-forward re2 nil t)
  5541. (>= (match-end 0) pos))
  5542. (throw 'exit (list beg (match-beginning 0)
  5543. lang single lfmt))))
  5544. (if (or (looking-at re2)
  5545. (re-search-forward re2 nil t))
  5546. (progn
  5547. (setq end (match-beginning 0))
  5548. (if (and (re-search-backward re1 nil t)
  5549. (<= (match-beginning 0) pos))
  5550. (progn
  5551. (setq lfmt (org-edit-src-get-label-format
  5552. (match-string 0)))
  5553. (throw 'exit
  5554. (list (match-end 0) end
  5555. (org-edit-src-get-lang lang)
  5556. single lfmt))))))))))))
  5557. (defun org-edit-src-get-lang (lang)
  5558. "Extract the src language."
  5559. (let ((m (match-string 0)))
  5560. (cond
  5561. ((stringp lang) lang)
  5562. ((integerp lang) (match-string lang))
  5563. ((and (eq lang 'lang)
  5564. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5565. (match-string 1 m))
  5566. ((and (eq lang 'style)
  5567. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5568. (match-string 1 m))
  5569. (t "fundamental"))))
  5570. (defun org-edit-src-get-label-format (s)
  5571. "Extract the label format."
  5572. (save-match-data
  5573. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5574. (match-string 1 s))))
  5575. (defun org-edit-src-exit ()
  5576. "Exit special edit and protect problematic lines."
  5577. (interactive)
  5578. (unless (buffer-base-buffer (current-buffer))
  5579. (error "This is not an indirect buffer, something is wrong..."))
  5580. (unless (> (point-min) 1)
  5581. (error "This buffer is not narrowed, something is wrong..."))
  5582. (goto-char (point-min))
  5583. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5584. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5585. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5586. (goto-char (point-min))
  5587. (while (re-search-forward "\n" nil t)
  5588. (replace-match " "))
  5589. (goto-char (point-min))
  5590. (if (looking-at "\\s-*") (replace-match " "))
  5591. (if (re-search-forward "\\s-+\\'" nil t)
  5592. (replace-match "")))
  5593. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5594. (goto-char (point-min))
  5595. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5596. (replace-match ",\\1"))
  5597. (when font-lock-mode
  5598. (font-lock-unfontify-region (point-min) (point-max)))
  5599. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5600. (when (org-bound-and-true-p org-edit-src-picture)
  5601. (untabify (point-min) (point-max))
  5602. (goto-char (point-min))
  5603. (while (re-search-forward "^" nil t)
  5604. (replace-match ": "))
  5605. (when font-lock-mode
  5606. (font-lock-unfontify-region (point-min) (point-max)))
  5607. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5608. (kill-buffer (current-buffer))
  5609. (and (org-mode-p) (org-restart-font-lock)))
  5610. ;;; The orgstruct minor mode
  5611. ;; Define a minor mode which can be used in other modes in order to
  5612. ;; integrate the org-mode structure editing commands.
  5613. ;; This is really a hack, because the org-mode structure commands use
  5614. ;; keys which normally belong to the major mode. Here is how it
  5615. ;; works: The minor mode defines all the keys necessary to operate the
  5616. ;; structure commands, but wraps the commands into a function which
  5617. ;; tests if the cursor is currently at a headline or a plain list
  5618. ;; item. If that is the case, the structure command is used,
  5619. ;; temporarily setting many Org-mode variables like regular
  5620. ;; expressions for filling etc. However, when any of those keys is
  5621. ;; used at a different location, function uses `key-binding' to look
  5622. ;; up if the key has an associated command in another currently active
  5623. ;; keymap (minor modes, major mode, global), and executes that
  5624. ;; command. There might be problems if any of the keys is otherwise
  5625. ;; used as a prefix key.
  5626. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5627. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5628. ;; addresses this by checking explicitly for both bindings.
  5629. (defvar orgstruct-mode-map (make-sparse-keymap)
  5630. "Keymap for the minor `orgstruct-mode'.")
  5631. (defvar org-local-vars nil
  5632. "List of local variables, for use by `orgstruct-mode'")
  5633. ;;;###autoload
  5634. (define-minor-mode orgstruct-mode
  5635. "Toggle the minor more `orgstruct-mode'.
  5636. This mode is for using Org-mode structure commands in other modes.
  5637. The following key behave as if Org-mode was active, if the cursor
  5638. is on a headline, or on a plain list item (both in the definition
  5639. of Org-mode).
  5640. M-up Move entry/item up
  5641. M-down Move entry/item down
  5642. M-left Promote
  5643. M-right Demote
  5644. M-S-up Move entry/item up
  5645. M-S-down Move entry/item down
  5646. M-S-left Promote subtree
  5647. M-S-right Demote subtree
  5648. M-q Fill paragraph and items like in Org-mode
  5649. C-c ^ Sort entries
  5650. C-c - Cycle list bullet
  5651. TAB Cycle item visibility
  5652. M-RET Insert new heading/item
  5653. S-M-RET Insert new TODO heading / Checkbox item
  5654. C-c C-c Set tags / toggle checkbox"
  5655. nil " OrgStruct" nil
  5656. (org-load-modules-maybe)
  5657. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5658. ;;;###autoload
  5659. (defun turn-on-orgstruct ()
  5660. "Unconditionally turn on `orgstruct-mode'."
  5661. (orgstruct-mode 1))
  5662. (defun orgstruct++-mode (&optional arg)
  5663. "Toggle `orgstruct-mode', the enhanced version of it.
  5664. In addition to setting orgstruct-mode, this also exports all indentation and
  5665. autofilling variables from org-mode into the buffer. It will also
  5666. recognize item context in multiline items.
  5667. Note that turning off orgstruct-mode will *not* remove the
  5668. indentation/paragraph settings. This can only be done by refreshing the
  5669. major mode, for example with \[normal-mode]."
  5670. (interactive "P")
  5671. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5672. (if (< arg 1)
  5673. (orgstruct-mode -1)
  5674. (orgstruct-mode 1)
  5675. (let (var val)
  5676. (mapc
  5677. (lambda (x)
  5678. (when (string-match
  5679. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5680. (symbol-name (car x)))
  5681. (setq var (car x) val (nth 1 x))
  5682. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5683. org-local-vars)
  5684. (org-set-local 'orgstruct-is-++ t))))
  5685. (defvar orgstruct-is-++ nil
  5686. "Is orgstruct-mode in ++ version in the current-buffer?")
  5687. (make-variable-buffer-local 'orgstruct-is-++)
  5688. ;;;###autoload
  5689. (defun turn-on-orgstruct++ ()
  5690. "Unconditionally turn on `orgstruct++-mode'."
  5691. (orgstruct++-mode 1))
  5692. (defun orgstruct-error ()
  5693. "Error when there is no default binding for a structure key."
  5694. (interactive)
  5695. (error "This key has no function outside structure elements"))
  5696. (defun orgstruct-setup ()
  5697. "Setup orgstruct keymaps."
  5698. (let ((nfunc 0)
  5699. (bindings
  5700. (list
  5701. '([(meta up)] org-metaup)
  5702. '([(meta down)] org-metadown)
  5703. '([(meta left)] org-metaleft)
  5704. '([(meta right)] org-metaright)
  5705. '([(meta shift up)] org-shiftmetaup)
  5706. '([(meta shift down)] org-shiftmetadown)
  5707. '([(meta shift left)] org-shiftmetaleft)
  5708. '([(meta shift right)] org-shiftmetaright)
  5709. '([(shift up)] org-shiftup)
  5710. '([(shift down)] org-shiftdown)
  5711. '([(shift left)] org-shiftleft)
  5712. '([(shift right)] org-shiftright)
  5713. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5714. '("\M-q" fill-paragraph)
  5715. '("\C-c^" org-sort)
  5716. '("\C-c-" org-cycle-list-bullet)))
  5717. elt key fun cmd)
  5718. (while (setq elt (pop bindings))
  5719. (setq nfunc (1+ nfunc))
  5720. (setq key (org-key (car elt))
  5721. fun (nth 1 elt)
  5722. cmd (orgstruct-make-binding fun nfunc key))
  5723. (org-defkey orgstruct-mode-map key cmd))
  5724. ;; Special treatment needed for TAB and RET
  5725. (org-defkey orgstruct-mode-map [(tab)]
  5726. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5727. (org-defkey orgstruct-mode-map "\C-i"
  5728. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5729. (org-defkey orgstruct-mode-map "\M-\C-m"
  5730. (orgstruct-make-binding 'org-insert-heading 105
  5731. "\M-\C-m" [(meta return)]))
  5732. (org-defkey orgstruct-mode-map [(meta return)]
  5733. (orgstruct-make-binding 'org-insert-heading 106
  5734. [(meta return)] "\M-\C-m"))
  5735. (org-defkey orgstruct-mode-map [(shift meta return)]
  5736. (orgstruct-make-binding 'org-insert-todo-heading 107
  5737. [(meta return)] "\M-\C-m"))
  5738. (unless org-local-vars
  5739. (setq org-local-vars (org-get-local-variables)))
  5740. t))
  5741. (defun orgstruct-make-binding (fun n &rest keys)
  5742. "Create a function for binding in the structure minor mode.
  5743. FUN is the command to call inside a table. N is used to create a unique
  5744. command name. KEYS are keys that should be checked in for a command
  5745. to execute outside of tables."
  5746. (eval
  5747. (list 'defun
  5748. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5749. '(arg)
  5750. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5751. "Outside of structure, run the binding of `"
  5752. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5753. "'.")
  5754. '(interactive "p")
  5755. (list 'if
  5756. `(org-context-p 'headline 'item
  5757. (and orgstruct-is-++
  5758. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5759. 'item-body))
  5760. (list 'org-run-like-in-org-mode (list 'quote fun))
  5761. (list 'let '(orgstruct-mode)
  5762. (list 'call-interactively
  5763. (append '(or)
  5764. (mapcar (lambda (k)
  5765. (list 'key-binding k))
  5766. keys)
  5767. '('orgstruct-error))))))))
  5768. (defun org-context-p (&rest contexts)
  5769. "Check if local context is any of CONTEXTS.
  5770. Possible values in the list of contexts are `table', `headline', and `item'."
  5771. (let ((pos (point)))
  5772. (goto-char (point-at-bol))
  5773. (prog1 (or (and (memq 'table contexts)
  5774. (looking-at "[ \t]*|"))
  5775. (and (memq 'headline contexts)
  5776. ;;????????? (looking-at "\\*+"))
  5777. (looking-at outline-regexp))
  5778. (and (memq 'item contexts)
  5779. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5780. (and (memq 'item-body contexts)
  5781. (org-in-item-p)))
  5782. (goto-char pos))))
  5783. (defun org-get-local-variables ()
  5784. "Return a list of all local variables in an org-mode buffer."
  5785. (let (varlist)
  5786. (with-current-buffer (get-buffer-create "*Org tmp*")
  5787. (erase-buffer)
  5788. (org-mode)
  5789. (setq varlist (buffer-local-variables)))
  5790. (kill-buffer "*Org tmp*")
  5791. (delq nil
  5792. (mapcar
  5793. (lambda (x)
  5794. (setq x
  5795. (if (symbolp x)
  5796. (list x)
  5797. (list (car x) (list 'quote (cdr x)))))
  5798. (if (string-match
  5799. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5800. (symbol-name (car x)))
  5801. x nil))
  5802. varlist))))
  5803. ;;;###autoload
  5804. (defun org-run-like-in-org-mode (cmd)
  5805. (org-load-modules-maybe)
  5806. (unless org-local-vars
  5807. (setq org-local-vars (org-get-local-variables)))
  5808. (eval (list 'let org-local-vars
  5809. (list 'call-interactively (list 'quote cmd)))))
  5810. ;;;; Archiving
  5811. (defun org-get-category (&optional pos)
  5812. "Get the category applying to position POS."
  5813. (get-text-property (or pos (point)) 'org-category))
  5814. (defun org-refresh-category-properties ()
  5815. "Refresh category text properties in the buffer."
  5816. (let ((def-cat (cond
  5817. ((null org-category)
  5818. (if buffer-file-name
  5819. (file-name-sans-extension
  5820. (file-name-nondirectory buffer-file-name))
  5821. "???"))
  5822. ((symbolp org-category) (symbol-name org-category))
  5823. (t org-category)))
  5824. beg end cat pos optionp)
  5825. (org-unmodified
  5826. (save-excursion
  5827. (save-restriction
  5828. (widen)
  5829. (goto-char (point-min))
  5830. (put-text-property (point) (point-max) 'org-category def-cat)
  5831. (while (re-search-forward
  5832. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5833. (setq pos (match-end 0)
  5834. optionp (equal (char-after (match-beginning 0)) ?#)
  5835. cat (org-trim (match-string 2)))
  5836. (if optionp
  5837. (setq beg (point-at-bol) end (point-max))
  5838. (org-back-to-heading t)
  5839. (setq beg (point) end (org-end-of-subtree t t)))
  5840. (put-text-property beg end 'org-category cat)
  5841. (goto-char pos)))))))
  5842. ;;;; Link Stuff
  5843. ;;; Link abbreviations
  5844. (defun org-link-expand-abbrev (link)
  5845. "Apply replacements as defined in `org-link-abbrev-alist."
  5846. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5847. (let* ((key (match-string 1 link))
  5848. (as (or (assoc key org-link-abbrev-alist-local)
  5849. (assoc key org-link-abbrev-alist)))
  5850. (tag (and (match-end 2) (match-string 3 link)))
  5851. rpl)
  5852. (if (not as)
  5853. link
  5854. (setq rpl (cdr as))
  5855. (cond
  5856. ((symbolp rpl) (funcall rpl tag))
  5857. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5858. ((string-match "%h" rpl)
  5859. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5860. (t (concat rpl tag)))))
  5861. link))
  5862. ;;; Storing and inserting links
  5863. (defvar org-insert-link-history nil
  5864. "Minibuffer history for links inserted with `org-insert-link'.")
  5865. (defvar org-stored-links nil
  5866. "Contains the links stored with `org-store-link'.")
  5867. (defvar org-store-link-plist nil
  5868. "Plist with info about the most recently link created with `org-store-link'.")
  5869. (defvar org-link-protocols nil
  5870. "Link protocols added to Org-mode using `org-add-link-type'.")
  5871. (defvar org-store-link-functions nil
  5872. "List of functions that are called to create and store a link.
  5873. Each function will be called in turn until one returns a non-nil
  5874. value. Each function should check if it is responsible for creating
  5875. this link (for example by looking at the major mode).
  5876. If not, it must exit and return nil.
  5877. If yes, it should return a non-nil value after a calling
  5878. `org-store-link-props' with a list of properties and values.
  5879. Special properties are:
  5880. :type The link prefix. like \"http\". This must be given.
  5881. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5882. This is obligatory as well.
  5883. :description Optional default description for the second pair
  5884. of brackets in an Org-mode link. The user can still change
  5885. this when inserting this link into an Org-mode buffer.
  5886. In addition to these, any additional properties can be specified
  5887. and then used in remember templates.")
  5888. (defun org-add-link-type (type &optional follow export)
  5889. "Add TYPE to the list of `org-link-types'.
  5890. Re-compute all regular expressions depending on `org-link-types'
  5891. FOLLOW and EXPORT are two functions.
  5892. FOLLOW should take the link path as the single argument and do whatever
  5893. is necessary to follow the link, for example find a file or display
  5894. a mail message.
  5895. EXPORT should format the link path for export to one of the export formats.
  5896. It should be a function accepting three arguments:
  5897. path the path of the link, the text after the prefix (like \"http:\")
  5898. desc the description of the link, if any, nil if there was no description
  5899. format the export format, a symbol like `html' or `latex'.
  5900. The function may use the FORMAT information to return different values
  5901. depending on the format. The return value will be put literally into
  5902. the exported file.
  5903. Org-mode has a built-in default for exporting links. If you are happy with
  5904. this default, there is no need to define an export function for the link
  5905. type. For a simple example of an export function, see `org-bbdb.el'."
  5906. (add-to-list 'org-link-types type t)
  5907. (org-make-link-regexps)
  5908. (if (assoc type org-link-protocols)
  5909. (setcdr (assoc type org-link-protocols) (list follow export))
  5910. (push (list type follow export) org-link-protocols)))
  5911. ;;;###autoload
  5912. (defun org-store-link (arg)
  5913. "\\<org-mode-map>Store an org-link to the current location.
  5914. This link is added to `org-stored-links' and can later be inserted
  5915. into an org-buffer with \\[org-insert-link].
  5916. For some link types, a prefix arg is interpreted:
  5917. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5918. For file links, arg negates `org-context-in-file-links'."
  5919. (interactive "P")
  5920. (org-load-modules-maybe)
  5921. (setq org-store-link-plist nil) ; reset
  5922. (let (link cpltxt desc description search txt)
  5923. (cond
  5924. ((run-hook-with-args-until-success 'org-store-link-functions)
  5925. (setq link (plist-get org-store-link-plist :link)
  5926. desc (or (plist-get org-store-link-plist :description) link)))
  5927. ((equal (buffer-name) "*Org Edit Src Example*")
  5928. (let (label gc)
  5929. (while (or (not label)
  5930. (save-excursion
  5931. (save-restriction
  5932. (widen)
  5933. (goto-char (point-min))
  5934. (re-search-forward
  5935. (regexp-quote (format org-coderef-label-format label))
  5936. nil t))))
  5937. (when label (message "Label exists already") (sit-for 2))
  5938. (setq label (read-string "Code line label: " label)))
  5939. (end-of-line 1)
  5940. (setq link (format org-coderef-label-format label))
  5941. (setq gc (- 79 (length link)))
  5942. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5943. (insert link)
  5944. (setq link (concat "(" label ")") desc nil)))
  5945. ((eq major-mode 'calendar-mode)
  5946. (let ((cd (calendar-cursor-to-date)))
  5947. (setq link
  5948. (format-time-string
  5949. (car org-time-stamp-formats)
  5950. (apply 'encode-time
  5951. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5952. nil nil nil))))
  5953. (org-store-link-props :type "calendar" :date cd)))
  5954. ((eq major-mode 'w3-mode)
  5955. (setq cpltxt (url-view-url t)
  5956. link (org-make-link cpltxt))
  5957. (org-store-link-props :type "w3" :url (url-view-url t)))
  5958. ((eq major-mode 'w3m-mode)
  5959. (setq cpltxt (or w3m-current-title w3m-current-url)
  5960. link (org-make-link w3m-current-url))
  5961. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5962. ((setq search (run-hook-with-args-until-success
  5963. 'org-create-file-search-functions))
  5964. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5965. "::" search))
  5966. (setq cpltxt (or description link)))
  5967. ((eq major-mode 'image-mode)
  5968. (setq cpltxt (concat "file:"
  5969. (abbreviate-file-name buffer-file-name))
  5970. link (org-make-link cpltxt))
  5971. (org-store-link-props :type "image" :file buffer-file-name))
  5972. ((eq major-mode 'dired-mode)
  5973. ;; link to the file in the current line
  5974. (setq cpltxt (concat "file:"
  5975. (abbreviate-file-name
  5976. (expand-file-name
  5977. (dired-get-filename nil t))))
  5978. link (org-make-link cpltxt)))
  5979. ((and buffer-file-name (org-mode-p))
  5980. (cond
  5981. ((org-in-regexp "<<\\(.*?\\)>>")
  5982. (setq cpltxt
  5983. (concat "file:"
  5984. (abbreviate-file-name buffer-file-name)
  5985. "::" (match-string 1))
  5986. link (org-make-link cpltxt)))
  5987. ((and (featurep 'org-id)
  5988. (or (eq org-link-to-org-use-id t)
  5989. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5990. (interactive-p))
  5991. (and org-link-to-org-use-id
  5992. (condition-case nil
  5993. (org-entry-get nil "ID")
  5994. (error nil)))))
  5995. ;; We can make a link using the ID.
  5996. (setq link (condition-case nil
  5997. (prog1 (org-id-store-link)
  5998. (setq desc (plist-get org-store-link-plist
  5999. :description)))
  6000. (error
  6001. ;; probably before first headline, link to file only
  6002. (concat "file:"
  6003. (abbreviate-file-name buffer-file-name))))))
  6004. (t
  6005. ;; Just link to current headline
  6006. (setq cpltxt (concat "file:"
  6007. (abbreviate-file-name buffer-file-name)))
  6008. ;; Add a context search string
  6009. (when (org-xor org-context-in-file-links arg)
  6010. (setq txt (cond
  6011. ((org-on-heading-p) nil)
  6012. ((org-region-active-p)
  6013. (buffer-substring (region-beginning) (region-end)))
  6014. (t nil)))
  6015. (when (or (null txt) (string-match "\\S-" txt))
  6016. (setq cpltxt
  6017. (concat cpltxt "::"
  6018. (condition-case nil
  6019. (org-make-org-heading-search-string txt)
  6020. (error "")))
  6021. desc "NONE")))
  6022. (if (string-match "::\\'" cpltxt)
  6023. (setq cpltxt (substring cpltxt 0 -2)))
  6024. (setq link (org-make-link cpltxt)))))
  6025. ((buffer-file-name (buffer-base-buffer))
  6026. ;; Just link to this file here.
  6027. (setq cpltxt (concat "file:"
  6028. (abbreviate-file-name
  6029. (buffer-file-name (buffer-base-buffer)))))
  6030. ;; Add a context string
  6031. (when (org-xor org-context-in-file-links arg)
  6032. (setq txt (if (org-region-active-p)
  6033. (buffer-substring (region-beginning) (region-end))
  6034. (buffer-substring (point-at-bol) (point-at-eol))))
  6035. ;; Only use search option if there is some text.
  6036. (when (string-match "\\S-" txt)
  6037. (setq cpltxt
  6038. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6039. desc "NONE")))
  6040. (setq link (org-make-link cpltxt)))
  6041. ((interactive-p)
  6042. (error "Cannot link to a buffer which is not visiting a file"))
  6043. (t (setq link nil)))
  6044. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6045. (setq link (or link cpltxt)
  6046. desc (or desc cpltxt))
  6047. (if (equal desc "NONE") (setq desc nil))
  6048. (if (and (interactive-p) link)
  6049. (progn
  6050. (setq org-stored-links
  6051. (cons (list link desc) org-stored-links))
  6052. (message "Stored: %s" (or desc link)))
  6053. (and link (org-make-link-string link desc)))))
  6054. (defun org-store-link-props (&rest plist)
  6055. "Store link properties, extract names and addresses."
  6056. (let (x adr)
  6057. (when (setq x (plist-get plist :from))
  6058. (setq adr (mail-extract-address-components x))
  6059. (setq plist (plist-put plist :fromname (car adr)))
  6060. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6061. (when (setq x (plist-get plist :to))
  6062. (setq adr (mail-extract-address-components x))
  6063. (setq plist (plist-put plist :toname (car adr)))
  6064. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6065. (let ((from (plist-get plist :from))
  6066. (to (plist-get plist :to)))
  6067. (when (and from to org-from-is-user-regexp)
  6068. (setq plist
  6069. (plist-put plist :fromto
  6070. (if (string-match org-from-is-user-regexp from)
  6071. (concat "to %t")
  6072. (concat "from %f"))))))
  6073. (setq org-store-link-plist plist))
  6074. (defun org-add-link-props (&rest plist)
  6075. "Add these properties to the link property list."
  6076. (let (key value)
  6077. (while plist
  6078. (setq key (pop plist) value (pop plist))
  6079. (setq org-store-link-plist
  6080. (plist-put org-store-link-plist key value)))))
  6081. (defun org-email-link-description (&optional fmt)
  6082. "Return the description part of an email link.
  6083. This takes information from `org-store-link-plist' and formats it
  6084. according to FMT (default from `org-email-link-description-format')."
  6085. (setq fmt (or fmt org-email-link-description-format))
  6086. (let* ((p org-store-link-plist)
  6087. (to (plist-get p :toaddress))
  6088. (from (plist-get p :fromaddress))
  6089. (table
  6090. (list
  6091. (cons "%c" (plist-get p :fromto))
  6092. (cons "%F" (plist-get p :from))
  6093. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6094. (cons "%T" (plist-get p :to))
  6095. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6096. (cons "%s" (plist-get p :subject))
  6097. (cons "%m" (plist-get p :message-id)))))
  6098. (when (string-match "%c" fmt)
  6099. ;; Check if the user wrote this message
  6100. (if (and org-from-is-user-regexp from to
  6101. (save-match-data (string-match org-from-is-user-regexp from)))
  6102. (setq fmt (replace-match "to %t" t t fmt))
  6103. (setq fmt (replace-match "from %f" t t fmt))))
  6104. (org-replace-escapes fmt table)))
  6105. (defun org-make-org-heading-search-string (&optional string heading)
  6106. "Make search string for STRING or current headline."
  6107. (interactive)
  6108. (let ((s (or string (org-get-heading))))
  6109. (unless (and string (not heading))
  6110. ;; We are using a headline, clean up garbage in there.
  6111. (if (string-match org-todo-regexp s)
  6112. (setq s (replace-match "" t t s)))
  6113. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6114. (setq s (replace-match "" t t s)))
  6115. (setq s (org-trim s))
  6116. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6117. org-comment-string "\\)") s)
  6118. (setq s (replace-match "" t t s)))
  6119. (while (string-match org-ts-regexp s)
  6120. (setq s (replace-match "" t t s))))
  6121. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6122. (setq s (replace-match " " t t s)))
  6123. (or string (setq s (concat "*" s))) ; Add * for headlines
  6124. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6125. (defun org-make-link (&rest strings)
  6126. "Concatenate STRINGS."
  6127. (apply 'concat strings))
  6128. (defun org-make-link-string (link &optional description)
  6129. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6130. (unless (string-match "\\S-" link)
  6131. (error "Empty link"))
  6132. (when (stringp description)
  6133. ;; Remove brackets from the description, they are fatal.
  6134. (while (string-match "\\[" description)
  6135. (setq description (replace-match "{" t t description)))
  6136. (while (string-match "\\]" description)
  6137. (setq description (replace-match "}" t t description))))
  6138. (when (equal (org-link-escape link) description)
  6139. ;; No description needed, it is identical
  6140. (setq description nil))
  6141. (when (and (not description)
  6142. (not (equal link (org-link-escape link))))
  6143. (setq description (org-extract-attributes link)))
  6144. (concat "[[" (org-link-escape link) "]"
  6145. (if description (concat "[" description "]") "")
  6146. "]"))
  6147. (defconst org-link-escape-chars
  6148. '((?\ . "%20")
  6149. (?\[ . "%5B")
  6150. (?\] . "%5D")
  6151. (?\340 . "%E0") ; `a
  6152. (?\342 . "%E2") ; ^a
  6153. (?\347 . "%E7") ; ,c
  6154. (?\350 . "%E8") ; `e
  6155. (?\351 . "%E9") ; 'e
  6156. (?\352 . "%EA") ; ^e
  6157. (?\356 . "%EE") ; ^i
  6158. (?\364 . "%F4") ; ^o
  6159. (?\371 . "%F9") ; `u
  6160. (?\373 . "%FB") ; ^u
  6161. (?\; . "%3B")
  6162. (?? . "%3F")
  6163. (?= . "%3D")
  6164. (?+ . "%2B")
  6165. )
  6166. "Association list of escapes for some characters problematic in links.
  6167. This is the list that is used for internal purposes.")
  6168. (defconst org-link-escape-chars-browser
  6169. '((?\ . "%20")) ; 32 for the SPC char
  6170. "Association list of escapes for some characters problematic in links.
  6171. This is the list that is used before handing over to the browser.")
  6172. (defun org-link-escape (text &optional table)
  6173. "Escape characters in TEXT that are problematic for links."
  6174. (setq table (or table org-link-escape-chars))
  6175. (when text
  6176. (let ((re (mapconcat (lambda (x) (regexp-quote
  6177. (char-to-string (car x))))
  6178. table "\\|")))
  6179. (while (string-match re text)
  6180. (setq text
  6181. (replace-match
  6182. (cdr (assoc (string-to-char (match-string 0 text))
  6183. table))
  6184. t t text)))
  6185. text)))
  6186. (defun org-link-unescape (text &optional table)
  6187. "Reverse the action of `org-link-escape'."
  6188. (setq table (or table org-link-escape-chars))
  6189. (when text
  6190. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6191. table "\\|")))
  6192. (while (string-match re text)
  6193. (setq text
  6194. (replace-match
  6195. (char-to-string (car (rassoc (match-string 0 text) table)))
  6196. t t text)))
  6197. text)))
  6198. (defun org-xor (a b)
  6199. "Exclusive or."
  6200. (if a (not b) b))
  6201. (defun org-fixup-message-id-for-http (s)
  6202. "Replace special characters in a message id, so it can be used in an http query."
  6203. (while (string-match "<" s)
  6204. (setq s (replace-match "%3C" t t s)))
  6205. (while (string-match ">" s)
  6206. (setq s (replace-match "%3E" t t s)))
  6207. (while (string-match "@" s)
  6208. (setq s (replace-match "%40" t t s)))
  6209. s)
  6210. ;;;###autoload
  6211. (defun org-insert-link-global ()
  6212. "Insert a link like Org-mode does.
  6213. This command can be called in any mode to insert a link in Org-mode syntax."
  6214. (interactive)
  6215. (org-load-modules-maybe)
  6216. (org-run-like-in-org-mode 'org-insert-link))
  6217. (defun org-insert-link (&optional complete-file link-location)
  6218. "Insert a link. At the prompt, enter the link.
  6219. Completion can be used to insert any of the link protocol prefixes like
  6220. http or ftp in use.
  6221. The history can be used to select a link previously stored with
  6222. `org-store-link'. When the empty string is entered (i.e. if you just
  6223. press RET at the prompt), the link defaults to the most recently
  6224. stored link. As SPC triggers completion in the minibuffer, you need to
  6225. use M-SPC or C-q SPC to force the insertion of a space character.
  6226. You will also be prompted for a description, and if one is given, it will
  6227. be displayed in the buffer instead of the link.
  6228. If there is already a link at point, this command will allow you to edit link
  6229. and description parts.
  6230. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6231. be selected using completion. The path to the file will be relative to the
  6232. current directory if the file is in the current directory or a subdirectory.
  6233. Otherwise, the link will be the absolute path as completed in the minibuffer
  6234. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6235. option `org-link-file-path-type'.
  6236. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6237. the current directory or below.
  6238. With three \\[universal-argument] prefixes, negate the meaning of
  6239. `org-keep-stored-link-after-insertion'.
  6240. If `org-make-link-description-function' is non-nil, this function will be
  6241. called with the link target, and the result will be the default
  6242. link description.
  6243. If the LINK-LOCATION parameter is non-nil, this value will be
  6244. used as the link location instead of reading one interactively."
  6245. (interactive "P")
  6246. (let* ((wcf (current-window-configuration))
  6247. (region (if (org-region-active-p)
  6248. (buffer-substring (region-beginning) (region-end))))
  6249. (remove (and region (list (region-beginning) (region-end))))
  6250. (desc region)
  6251. tmphist ; byte-compile incorrectly complains about this
  6252. (link link-location)
  6253. entry file)
  6254. (cond
  6255. (link-location) ; specified by arg, just use it.
  6256. ((org-in-regexp org-bracket-link-regexp 1)
  6257. ;; We do have a link at point, and we are going to edit it.
  6258. (setq remove (list (match-beginning 0) (match-end 0)))
  6259. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6260. (setq link (read-string "Link: "
  6261. (org-link-unescape
  6262. (org-match-string-no-properties 1)))))
  6263. ((or (org-in-regexp org-angle-link-re)
  6264. (org-in-regexp org-plain-link-re))
  6265. ;; Convert to bracket link
  6266. (setq remove (list (match-beginning 0) (match-end 0))
  6267. link (read-string "Link: "
  6268. (org-remove-angle-brackets (match-string 0)))))
  6269. ((member complete-file '((4) (16)))
  6270. ;; Completing read for file names.
  6271. (setq file (read-file-name "File: "))
  6272. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6273. (pwd1 (file-name-as-directory (abbreviate-file-name
  6274. (expand-file-name ".")))))
  6275. (cond
  6276. ((equal complete-file '(16))
  6277. (setq link (org-make-link
  6278. "file:"
  6279. (abbreviate-file-name (expand-file-name file)))))
  6280. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6281. (setq link (org-make-link "file:" (match-string 1 file))))
  6282. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6283. (expand-file-name file))
  6284. (setq link (org-make-link
  6285. "file:" (match-string 1 (expand-file-name file)))))
  6286. (t (setq link (org-make-link "file:" file))))))
  6287. (t
  6288. ;; Read link, with completion for stored links.
  6289. (with-output-to-temp-buffer "*Org Links*"
  6290. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6291. (when org-stored-links
  6292. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6293. (princ (mapconcat
  6294. (lambda (x)
  6295. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6296. (reverse org-stored-links) "\n"))))
  6297. (let ((cw (selected-window)))
  6298. (select-window (get-buffer-window "*Org Links*"))
  6299. (setq truncate-lines t)
  6300. (org-fit-window-to-buffer)
  6301. (select-window cw))
  6302. ;; Fake a link history, containing the stored links.
  6303. (setq tmphist (append (mapcar 'car org-stored-links)
  6304. org-insert-link-history))
  6305. (unwind-protect
  6306. (setq link
  6307. (let ((org-completion-use-ido nil))
  6308. (org-completing-read
  6309. "Link: "
  6310. (append
  6311. (mapcar (lambda (x) (list (concat (car x) ":")))
  6312. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6313. (mapcar (lambda (x) (list (concat x ":")))
  6314. org-link-types))
  6315. nil nil nil
  6316. 'tmphist
  6317. (or (car (car org-stored-links))))))
  6318. (set-window-configuration wcf)
  6319. (kill-buffer "*Org Links*"))
  6320. (setq entry (assoc link org-stored-links))
  6321. (or entry (push link org-insert-link-history))
  6322. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6323. (not org-keep-stored-link-after-insertion))
  6324. (setq org-stored-links (delq (assoc link org-stored-links)
  6325. org-stored-links)))
  6326. (setq desc (or desc (nth 1 entry)))))
  6327. (if (string-match org-plain-link-re link)
  6328. ;; URL-like link, normalize the use of angular brackets.
  6329. (setq link (org-make-link (org-remove-angle-brackets link))))
  6330. ;; Check if we are linking to the current file with a search option
  6331. ;; If yes, simplify the link by using only the search option.
  6332. (when (and buffer-file-name
  6333. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6334. (let* ((path (match-string 1 link))
  6335. (case-fold-search nil)
  6336. (search (match-string 2 link)))
  6337. (save-match-data
  6338. (if (equal (file-truename buffer-file-name) (file-truename path))
  6339. ;; We are linking to this same file, with a search option
  6340. (setq link search)))))
  6341. ;; Check if we can/should use a relative path. If yes, simplify the link
  6342. (when (string-match "^file:\\(.*\\)" link)
  6343. (let* ((path (match-string 1 link))
  6344. (origpath path)
  6345. (case-fold-search nil))
  6346. (cond
  6347. ((or (eq org-link-file-path-type 'absolute)
  6348. (equal complete-file '(16)))
  6349. (setq path (abbreviate-file-name (expand-file-name path))))
  6350. ((eq org-link-file-path-type 'noabbrev)
  6351. (setq path (expand-file-name path)))
  6352. ((eq org-link-file-path-type 'relative)
  6353. (setq path (file-relative-name path)))
  6354. (t
  6355. (save-match-data
  6356. (if (string-match (concat "^" (regexp-quote
  6357. (file-name-as-directory
  6358. (expand-file-name "."))))
  6359. (expand-file-name path))
  6360. ;; We are linking a file with relative path name.
  6361. (setq path (substring (expand-file-name path)
  6362. (match-end 0)))
  6363. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6364. (setq link (concat "file:" path))
  6365. (if (equal desc origpath)
  6366. (setq desc path))))
  6367. (if org-make-link-description-function
  6368. (setq desc (funcall org-make-link-description-function link desc)))
  6369. (setq desc (read-string "Description: " desc))
  6370. (unless (string-match "\\S-" desc) (setq desc nil))
  6371. (if remove (apply 'delete-region remove))
  6372. (insert (org-make-link-string link desc))))
  6373. (defun org-completing-read (&rest args)
  6374. "Completing-read with SPACE being a normal character."
  6375. (let ((minibuffer-local-completion-map
  6376. (copy-keymap minibuffer-local-completion-map)))
  6377. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6378. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6379. (apply 'org-ido-completing-read args)))
  6380. (defun org-completing-read-no-ido (&rest args)
  6381. (let (org-completion-use-ido)
  6382. (apply 'org-completing-read args)))
  6383. (defun org-ido-completing-read (&rest args)
  6384. "Completing-read using `ido-mode' speedups if available"
  6385. (if (and org-completion-use-ido
  6386. (fboundp 'ido-completing-read)
  6387. (boundp 'ido-mode) ido-mode
  6388. (listp (second args)))
  6389. (let ((ido-enter-matching-directory nil))
  6390. (apply 'ido-completing-read (concat (car args))
  6391. (mapcar (lambda (x) (car x)) (nth 1 args))
  6392. (cddr args)))
  6393. (apply 'completing-read args)))
  6394. (defun org-extract-attributes (s)
  6395. "Extract the attributes cookie from a string and set as text property."
  6396. (let (a attr (start 0) key value)
  6397. (save-match-data
  6398. (when (string-match "{{\\([^}]+\\)}}$" s)
  6399. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6400. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6401. (setq key (match-string 1 a) value (match-string 2 a)
  6402. start (match-end 0)
  6403. attr (plist-put attr (intern key) value))))
  6404. (org-add-props s nil 'org-attr attr))
  6405. s))
  6406. (defun org-attributes-to-string (plist)
  6407. "Format a property list into an HTML attribute list."
  6408. (let ((s "") key value)
  6409. (while plist
  6410. (setq key (pop plist) value (pop plist))
  6411. (and value
  6412. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6413. s))
  6414. ;;; Opening/following a link
  6415. (defvar org-link-search-failed nil)
  6416. (defun org-next-link ()
  6417. "Move forward to the next link.
  6418. If the link is in hidden text, expose it."
  6419. (interactive)
  6420. (when (and org-link-search-failed (eq this-command last-command))
  6421. (goto-char (point-min))
  6422. (message "Link search wrapped back to beginning of buffer"))
  6423. (setq org-link-search-failed nil)
  6424. (let* ((pos (point))
  6425. (ct (org-context))
  6426. (a (assoc :link ct)))
  6427. (if a (goto-char (nth 2 a)))
  6428. (if (re-search-forward org-any-link-re nil t)
  6429. (progn
  6430. (goto-char (match-beginning 0))
  6431. (if (org-invisible-p) (org-show-context)))
  6432. (goto-char pos)
  6433. (setq org-link-search-failed t)
  6434. (error "No further link found"))))
  6435. (defun org-previous-link ()
  6436. "Move backward to the previous link.
  6437. If the link is in hidden text, expose it."
  6438. (interactive)
  6439. (when (and org-link-search-failed (eq this-command last-command))
  6440. (goto-char (point-max))
  6441. (message "Link search wrapped back to end of buffer"))
  6442. (setq org-link-search-failed nil)
  6443. (let* ((pos (point))
  6444. (ct (org-context))
  6445. (a (assoc :link ct)))
  6446. (if a (goto-char (nth 1 a)))
  6447. (if (re-search-backward org-any-link-re nil t)
  6448. (progn
  6449. (goto-char (match-beginning 0))
  6450. (if (org-invisible-p) (org-show-context)))
  6451. (goto-char pos)
  6452. (setq org-link-search-failed t)
  6453. (error "No further link found"))))
  6454. (defun org-translate-link (s)
  6455. "Translate a link string if a translation function has been defined."
  6456. (if (and org-link-translation-function
  6457. (fboundp org-link-translation-function)
  6458. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6459. (progn
  6460. (setq s (funcall org-link-translation-function
  6461. (match-string 1) (match-string 2)))
  6462. (concat (car s) ":" (cdr s)))
  6463. s))
  6464. (defun org-translate-link-from-planner (type path)
  6465. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6466. This is still an experimental function, your mileage may vary."
  6467. (cond
  6468. ((member type '("http" "https" "news" "ftp"))
  6469. ;; standard Internet links are the same.
  6470. nil)
  6471. ((and (equal type "irc") (string-match "^//" path))
  6472. ;; Planner has two / at the beginning of an irc link, we have 1.
  6473. ;; We should have zero, actually....
  6474. (setq path (substring path 1)))
  6475. ((and (equal type "lisp") (string-match "^/" path))
  6476. ;; Planner has a slash, we do not.
  6477. (setq type "elisp" path (substring path 1)))
  6478. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6479. ;; A typical message link. Planner has the id after the fina slash,
  6480. ;; we separate it with a hash mark
  6481. (setq path (concat (match-string 1 path) "#"
  6482. (org-remove-angle-brackets (match-string 2 path)))))
  6483. )
  6484. (cons type path))
  6485. (defun org-find-file-at-mouse (ev)
  6486. "Open file link or URL at mouse."
  6487. (interactive "e")
  6488. (mouse-set-point ev)
  6489. (org-open-at-point 'in-emacs))
  6490. (defun org-open-at-mouse (ev)
  6491. "Open file link or URL at mouse."
  6492. (interactive "e")
  6493. (mouse-set-point ev)
  6494. (if (eq major-mode 'org-agenda-mode)
  6495. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6496. (org-open-at-point))
  6497. (defvar org-window-config-before-follow-link nil
  6498. "The window configuration before following a link.
  6499. This is saved in case the need arises to restore it.")
  6500. (defvar org-open-link-marker (make-marker)
  6501. "Marker pointing to the location where `org-open-at-point; was called.")
  6502. ;;;###autoload
  6503. (defun org-open-at-point-global ()
  6504. "Follow a link like Org-mode does.
  6505. This command can be called in any mode to follow a link that has
  6506. Org-mode syntax."
  6507. (interactive)
  6508. (org-run-like-in-org-mode 'org-open-at-point))
  6509. ;;;###autoload
  6510. (defun org-open-link-from-string (s &optional arg)
  6511. "Open a link in the string S, as if it was in Org-mode."
  6512. (interactive "sLink: \nP")
  6513. (with-temp-buffer
  6514. (let ((org-inhibit-startup t))
  6515. (org-mode)
  6516. (insert s)
  6517. (goto-char (point-min))
  6518. (org-open-at-point arg))))
  6519. (defun org-open-at-point (&optional in-emacs)
  6520. "Open link at or after point.
  6521. If there is no link at point, this function will search forward up to
  6522. the end of the current subtree.
  6523. Normally, files will be opened by an appropriate application. If the
  6524. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6525. With a double prefix argument, try to open outside of Emacs, in the
  6526. application the system uses for this file type."
  6527. (interactive "P")
  6528. (org-load-modules-maybe)
  6529. (move-marker org-open-link-marker (point))
  6530. (setq org-window-config-before-follow-link (current-window-configuration))
  6531. (org-remove-occur-highlights nil nil t)
  6532. (cond
  6533. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6534. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6535. (org-footnote-action))
  6536. (t
  6537. (let (type path link line search (pos (point)))
  6538. (catch 'match
  6539. (save-excursion
  6540. (skip-chars-forward "^]\n\r")
  6541. (when (org-in-regexp org-bracket-link-regexp)
  6542. (setq link (org-extract-attributes
  6543. (org-link-unescape (org-match-string-no-properties 1))))
  6544. (while (string-match " *\n *" link)
  6545. (setq link (replace-match " " t t link)))
  6546. (setq link (org-link-expand-abbrev link))
  6547. (cond
  6548. ((or (file-name-absolute-p link)
  6549. (string-match "^\\.\\.?/" link))
  6550. (setq type "file" path link))
  6551. ((string-match org-link-re-with-space3 link)
  6552. (setq type (match-string 1 link) path (match-string 2 link)))
  6553. (t (setq type "thisfile" path link)))
  6554. (throw 'match t)))
  6555. (when (get-text-property (point) 'org-linked-text)
  6556. (setq type "thisfile"
  6557. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6558. (1+ (point)) (point))
  6559. path (buffer-substring
  6560. (previous-single-property-change pos 'org-linked-text)
  6561. (next-single-property-change pos 'org-linked-text)))
  6562. (throw 'match t))
  6563. (save-excursion
  6564. (when (or (org-in-regexp org-angle-link-re)
  6565. (org-in-regexp org-plain-link-re))
  6566. (setq type (match-string 1) path (match-string 2))
  6567. (throw 'match t)))
  6568. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6569. (setq type "tree-match"
  6570. path (match-string 1))
  6571. (throw 'match t))
  6572. (save-excursion
  6573. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6574. (setq type "tags"
  6575. path (match-string 1))
  6576. (while (string-match ":" path)
  6577. (setq path (replace-match "+" t t path)))
  6578. (throw 'match t))))
  6579. (unless path
  6580. (error "No link found"))
  6581. ;; Remove any trailing spaces in path
  6582. (if (string-match " +\\'" path)
  6583. (setq path (replace-match "" t t path)))
  6584. (if (and org-link-translation-function
  6585. (fboundp org-link-translation-function))
  6586. ;; Check if we need to translate the link
  6587. (let ((tmp (funcall org-link-translation-function type path)))
  6588. (setq type (car tmp) path (cdr tmp))))
  6589. (cond
  6590. ((assoc type org-link-protocols)
  6591. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6592. ((equal type "mailto")
  6593. (let ((cmd (car org-link-mailto-program))
  6594. (args (cdr org-link-mailto-program)) args1
  6595. (address path) (subject "") a)
  6596. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6597. (setq address (match-string 1 path)
  6598. subject (org-link-escape (match-string 2 path))))
  6599. (while args
  6600. (cond
  6601. ((not (stringp (car args))) (push (pop args) args1))
  6602. (t (setq a (pop args))
  6603. (if (string-match "%a" a)
  6604. (setq a (replace-match address t t a)))
  6605. (if (string-match "%s" a)
  6606. (setq a (replace-match subject t t a)))
  6607. (push a args1))))
  6608. (apply cmd (nreverse args1))))
  6609. ((member type '("http" "https" "ftp" "news"))
  6610. (browse-url (concat type ":" (org-link-escape
  6611. path org-link-escape-chars-browser))))
  6612. ((member type '("message"))
  6613. (browse-url (concat type ":" path)))
  6614. ((string= type "tags")
  6615. (org-tags-view in-emacs path))
  6616. ((string= type "thisfile")
  6617. (if in-emacs
  6618. (switch-to-buffer-other-window
  6619. (org-get-buffer-for-internal-link (current-buffer)))
  6620. (org-mark-ring-push))
  6621. (let ((cmd `(org-link-search
  6622. ,path
  6623. ,(cond ((equal in-emacs '(4)) 'occur)
  6624. ((equal in-emacs '(16)) 'org-occur)
  6625. (t nil))
  6626. ,pos)))
  6627. (condition-case nil (eval cmd)
  6628. (error (progn (widen) (eval cmd))))))
  6629. ((string= type "tree-match")
  6630. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6631. ((string= type "file")
  6632. (if (string-match "::\\([0-9]+\\)\\'" path)
  6633. (setq line (string-to-number (match-string 1 path))
  6634. path (substring path 0 (match-beginning 0)))
  6635. (if (string-match "::\\(.+\\)\\'" path)
  6636. (setq search (match-string 1 path)
  6637. path (substring path 0 (match-beginning 0)))))
  6638. (if (string-match "[*?{]" (file-name-nondirectory path))
  6639. (dired path)
  6640. (org-open-file path in-emacs line search)))
  6641. ((string= type "news")
  6642. (require 'org-gnus)
  6643. (org-gnus-follow-link path))
  6644. ((string= type "shell")
  6645. (let ((cmd path))
  6646. (if (or (not org-confirm-shell-link-function)
  6647. (funcall org-confirm-shell-link-function
  6648. (format "Execute \"%s\" in shell? "
  6649. (org-add-props cmd nil
  6650. 'face 'org-warning))))
  6651. (progn
  6652. (message "Executing %s" cmd)
  6653. (shell-command cmd))
  6654. (error "Abort"))))
  6655. ((string= type "elisp")
  6656. (let ((cmd path))
  6657. (if (or (not org-confirm-elisp-link-function)
  6658. (funcall org-confirm-elisp-link-function
  6659. (format "Execute \"%s\" as elisp? "
  6660. (org-add-props cmd nil
  6661. 'face 'org-warning))))
  6662. (message "%s => %s" cmd
  6663. (if (equal (string-to-char cmd) ?\()
  6664. (eval (read cmd))
  6665. (call-interactively (read cmd))))
  6666. (error "Abort"))))
  6667. (t
  6668. (browse-url-at-point))))))
  6669. (move-marker org-open-link-marker nil)
  6670. (run-hook-with-args 'org-follow-link-hook))
  6671. ;;;; Time estimates
  6672. (defun org-get-effort (&optional pom)
  6673. "Get the effort estimate for the current entry."
  6674. (org-entry-get pom org-effort-property))
  6675. ;;; File search
  6676. (defvar org-create-file-search-functions nil
  6677. "List of functions to construct the right search string for a file link.
  6678. These functions are called in turn with point at the location to
  6679. which the link should point.
  6680. A function in the hook should first test if it would like to
  6681. handle this file type, for example by checking the major-mode or
  6682. the file extension. If it decides not to handle this file, it
  6683. should just return nil to give other functions a chance. If it
  6684. does handle the file, it must return the search string to be used
  6685. when following the link. The search string will be part of the
  6686. file link, given after a double colon, and `org-open-at-point'
  6687. will automatically search for it. If special measures must be
  6688. taken to make the search successful, another function should be
  6689. added to the companion hook `org-execute-file-search-functions',
  6690. which see.
  6691. A function in this hook may also use `setq' to set the variable
  6692. `description' to provide a suggestion for the descriptive text to
  6693. be used for this link when it gets inserted into an Org-mode
  6694. buffer with \\[org-insert-link].")
  6695. (defvar org-execute-file-search-functions nil
  6696. "List of functions to execute a file search triggered by a link.
  6697. Functions added to this hook must accept a single argument, the
  6698. search string that was part of the file link, the part after the
  6699. double colon. The function must first check if it would like to
  6700. handle this search, for example by checking the major-mode or the
  6701. file extension. If it decides not to handle this search, it
  6702. should just return nil to give other functions a chance. If it
  6703. does handle the search, it must return a non-nil value to keep
  6704. other functions from trying.
  6705. Each function can access the current prefix argument through the
  6706. variable `current-prefix-argument'. Note that a single prefix is
  6707. used to force opening a link in Emacs, so it may be good to only
  6708. use a numeric or double prefix to guide the search function.
  6709. In case this is needed, a function in this hook can also restore
  6710. the window configuration before `org-open-at-point' was called using:
  6711. (set-window-configuration org-window-config-before-follow-link)")
  6712. (defun org-link-search (s &optional type avoid-pos)
  6713. "Search for a link search option.
  6714. If S is surrounded by forward slashes, it is interpreted as a
  6715. regular expression. In org-mode files, this will create an `org-occur'
  6716. sparse tree. In ordinary files, `occur' will be used to list matches.
  6717. If the current buffer is in `dired-mode', grep will be used to search
  6718. in all files. If AVOID-POS is given, ignore matches near that position."
  6719. (let ((case-fold-search t)
  6720. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6721. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6722. (append '(("") (" ") ("\t") ("\n"))
  6723. org-emphasis-alist)
  6724. "\\|") "\\)"))
  6725. (pos (point))
  6726. (pre nil) (post nil)
  6727. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6728. (cond
  6729. ;; First check if there are any special
  6730. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6731. ;; Now try the builtin stuff
  6732. ((save-excursion
  6733. (goto-char (point-min))
  6734. (and
  6735. (re-search-forward
  6736. (concat "<<" (regexp-quote s0) ">>") nil t)
  6737. (setq type 'dedicated
  6738. pos (match-beginning 0))))
  6739. ;; There is an exact target for this
  6740. (goto-char pos))
  6741. ((and (string-match "^(\\(.*\\))$" s0)
  6742. (save-excursion
  6743. (goto-char (point-min))
  6744. (and
  6745. (re-search-forward
  6746. (concat "[^[]" (regexp-quote
  6747. (format org-coderef-label-format
  6748. (match-string 1 s0))))
  6749. nil t)
  6750. (setq type 'dedicated
  6751. pos (1+ (match-beginning 0))))))
  6752. ;; There is a coderef target for this
  6753. (goto-char pos))
  6754. ((string-match "^/\\(.*\\)/$" s)
  6755. ;; A regular expression
  6756. (cond
  6757. ((org-mode-p)
  6758. (org-occur (match-string 1 s)))
  6759. ;;((eq major-mode 'dired-mode)
  6760. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6761. (t (org-do-occur (match-string 1 s)))))
  6762. (t
  6763. ;; A normal search strings
  6764. (when (equal (string-to-char s) ?*)
  6765. ;; Anchor on headlines, post may include tags.
  6766. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6767. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6768. s (substring s 1)))
  6769. (remove-text-properties
  6770. 0 (length s)
  6771. '(face nil mouse-face nil keymap nil fontified nil) s)
  6772. ;; Make a series of regular expressions to find a match
  6773. (setq words (org-split-string s "[ \n\r\t]+")
  6774. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6775. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6776. "\\)" markers)
  6777. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6778. re2a (concat "[ \t\r\n]" re2a_)
  6779. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6780. re4 (concat "[^a-zA-Z_]" re4_)
  6781. re1 (concat pre re2 post)
  6782. re3 (concat pre (if pre re4_ re4) post)
  6783. re5 (concat pre ".*" re4)
  6784. re2 (concat pre re2)
  6785. re2a (concat pre (if pre re2a_ re2a))
  6786. re4 (concat pre (if pre re4_ re4))
  6787. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6788. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6789. re5 "\\)"
  6790. ))
  6791. (cond
  6792. ((eq type 'org-occur) (org-occur reall))
  6793. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6794. (t (goto-char (point-min))
  6795. (setq type 'fuzzy)
  6796. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6797. (org-search-not-self 1 re1 nil t)
  6798. (org-search-not-self 1 re2 nil t)
  6799. (org-search-not-self 1 re2a nil t)
  6800. (org-search-not-self 1 re3 nil t)
  6801. (org-search-not-self 1 re4 nil t)
  6802. (org-search-not-self 1 re5 nil t)
  6803. )
  6804. (goto-char (match-beginning 1))
  6805. (goto-char pos)
  6806. (error "No match")))))
  6807. (t
  6808. ;; Normal string-search
  6809. (goto-char (point-min))
  6810. (if (search-forward s nil t)
  6811. (goto-char (match-beginning 0))
  6812. (error "No match"))))
  6813. (and (org-mode-p) (org-show-context 'link-search))
  6814. type))
  6815. (defun org-search-not-self (group &rest args)
  6816. "Execute `re-search-forward', but only accept matches that do not
  6817. enclose the position of `org-open-link-marker'."
  6818. (let ((m org-open-link-marker))
  6819. (catch 'exit
  6820. (while (apply 're-search-forward args)
  6821. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6822. (goto-char (match-end group))
  6823. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6824. (> (match-beginning 0) (marker-position m))
  6825. (< (match-end 0) (marker-position m)))
  6826. (save-match-data
  6827. (or (not (org-in-regexp
  6828. org-bracket-link-analytic-regexp 1))
  6829. (not (match-end 4)) ; no description
  6830. (and (<= (match-beginning 4) (point))
  6831. (>= (match-end 4) (point))))))
  6832. (throw 'exit (point))))))))
  6833. (defun org-get-buffer-for-internal-link (buffer)
  6834. "Return a buffer to be used for displaying the link target of internal links."
  6835. (cond
  6836. ((not org-display-internal-link-with-indirect-buffer)
  6837. buffer)
  6838. ((string-match "(Clone)$" (buffer-name buffer))
  6839. (message "Buffer is already a clone, not making another one")
  6840. ;; we also do not modify visibility in this case
  6841. buffer)
  6842. (t ; make a new indirect buffer for displaying the link
  6843. (let* ((bn (buffer-name buffer))
  6844. (ibn (concat bn "(Clone)"))
  6845. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6846. (with-current-buffer ib (org-overview))
  6847. ib))))
  6848. (defun org-do-occur (regexp &optional cleanup)
  6849. "Call the Emacs command `occur'.
  6850. If CLEANUP is non-nil, remove the printout of the regular expression
  6851. in the *Occur* buffer. This is useful if the regex is long and not useful
  6852. to read."
  6853. (occur regexp)
  6854. (when cleanup
  6855. (let ((cwin (selected-window)) win beg end)
  6856. (when (setq win (get-buffer-window "*Occur*"))
  6857. (select-window win))
  6858. (goto-char (point-min))
  6859. (when (re-search-forward "match[a-z]+" nil t)
  6860. (setq beg (match-end 0))
  6861. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6862. (setq end (1- (match-beginning 0)))))
  6863. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6864. (goto-char (point-min))
  6865. (select-window cwin))))
  6866. ;;; The mark ring for links jumps
  6867. (defvar org-mark-ring nil
  6868. "Mark ring for positions before jumps in Org-mode.")
  6869. (defvar org-mark-ring-last-goto nil
  6870. "Last position in the mark ring used to go back.")
  6871. ;; Fill and close the ring
  6872. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6873. (loop for i from 1 to org-mark-ring-length do
  6874. (push (make-marker) org-mark-ring))
  6875. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6876. org-mark-ring)
  6877. (defun org-mark-ring-push (&optional pos buffer)
  6878. "Put the current position or POS into the mark ring and rotate it."
  6879. (interactive)
  6880. (setq pos (or pos (point)))
  6881. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6882. (move-marker (car org-mark-ring)
  6883. (or pos (point))
  6884. (or buffer (current-buffer)))
  6885. (message "%s"
  6886. (substitute-command-keys
  6887. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6888. (defun org-mark-ring-goto (&optional n)
  6889. "Jump to the previous position in the mark ring.
  6890. With prefix arg N, jump back that many stored positions. When
  6891. called several times in succession, walk through the entire ring.
  6892. Org-mode commands jumping to a different position in the current file,
  6893. or to another Org-mode file, automatically push the old position
  6894. onto the ring."
  6895. (interactive "p")
  6896. (let (p m)
  6897. (if (eq last-command this-command)
  6898. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6899. (setq p org-mark-ring))
  6900. (setq org-mark-ring-last-goto p)
  6901. (setq m (car p))
  6902. (switch-to-buffer (marker-buffer m))
  6903. (goto-char m)
  6904. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6905. (defun org-remove-angle-brackets (s)
  6906. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6907. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6908. s)
  6909. (defun org-add-angle-brackets (s)
  6910. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6911. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6912. s)
  6913. (defun org-remove-double-quotes (s)
  6914. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6915. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6916. s)
  6917. ;;; Following specific links
  6918. (defun org-follow-timestamp-link ()
  6919. (cond
  6920. ((org-at-date-range-p t)
  6921. (let ((org-agenda-start-on-weekday)
  6922. (t1 (match-string 1))
  6923. (t2 (match-string 2)))
  6924. (setq t1 (time-to-days (org-time-string-to-time t1))
  6925. t2 (time-to-days (org-time-string-to-time t2)))
  6926. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6927. ((org-at-timestamp-p t)
  6928. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6929. (substring (match-string 1) 0 10)))
  6930. 1))
  6931. (t (error "This should not happen"))))
  6932. ;;; Following file links
  6933. (defvar org-wait nil)
  6934. (defun org-open-file (path &optional in-emacs line search)
  6935. "Open the file at PATH.
  6936. First, this expands any special file name abbreviations. Then the
  6937. configuration variable `org-file-apps' is checked if it contains an
  6938. entry for this file type, and if yes, the corresponding command is launched.
  6939. If no application is found, Emacs simply visits the file.
  6940. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6941. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6942. and o use an external application to visit the file.
  6943. Optional LINE specifies a line to go to, optional SEARCH a string to
  6944. search for. If LINE or SEARCH is given, the file will always be
  6945. opened in Emacs.
  6946. If the file does not exist, an error is thrown."
  6947. (setq in-emacs (or in-emacs line search))
  6948. (let* ((file (if (equal path "")
  6949. buffer-file-name
  6950. (substitute-in-file-name (expand-file-name path))))
  6951. (apps (append org-file-apps (org-default-apps)))
  6952. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6953. (dirp (if remp nil (file-directory-p file)))
  6954. (file (if (and dirp org-open-directory-means-index-dot-org)
  6955. (concat (file-name-as-directory file) "index.org")
  6956. file))
  6957. (a-m-a-p (assq 'auto-mode apps))
  6958. (dfile (downcase file))
  6959. (old-buffer (current-buffer))
  6960. (old-pos (point))
  6961. (old-mode major-mode)
  6962. ext cmd)
  6963. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6964. (setq ext (match-string 1 dfile))
  6965. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6966. (setq ext (match-string 1 dfile))))
  6967. (cond
  6968. ((equal in-emacs '(16))
  6969. (setq cmd (cdr (assoc 'system apps))))
  6970. (in-emacs (setq cmd 'emacs))
  6971. (t
  6972. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6973. (and dirp (cdr (assoc 'directory apps)))
  6974. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6975. 'string-match)
  6976. (cdr (assoc ext apps))
  6977. (cdr (assoc t apps))))))
  6978. (when (eq cmd 'system)
  6979. (setq cmd (cdr (assoc 'system apps))))
  6980. (when (eq cmd 'default)
  6981. (setq cmd (cdr (assoc t apps))))
  6982. (when (eq cmd 'mailcap)
  6983. (require 'mailcap)
  6984. (mailcap-parse-mailcaps)
  6985. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6986. (command (mailcap-mime-info mime-type)))
  6987. (if (stringp command)
  6988. (setq cmd command)
  6989. (setq cmd 'emacs))))
  6990. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6991. (not (file-exists-p file))
  6992. (not org-open-non-existing-files))
  6993. (error "No such file: %s" file))
  6994. (cond
  6995. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6996. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6997. (while (string-match "['\"]%s['\"]" cmd)
  6998. (setq cmd (replace-match "%s" t t cmd)))
  6999. (while (string-match "%s" cmd)
  7000. (setq cmd (replace-match
  7001. (save-match-data
  7002. (shell-quote-argument
  7003. (convert-standard-filename file)))
  7004. t t cmd)))
  7005. (save-window-excursion
  7006. (start-process-shell-command cmd nil cmd)
  7007. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7008. ))
  7009. ((or (stringp cmd)
  7010. (eq cmd 'emacs))
  7011. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7012. (widen)
  7013. (if line (goto-line line)
  7014. (if search (org-link-search search))))
  7015. ((consp cmd)
  7016. (let ((file (convert-standard-filename file)))
  7017. (eval cmd)))
  7018. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7019. (and (org-mode-p) (eq old-mode 'org-mode)
  7020. (or (not (equal old-buffer (current-buffer)))
  7021. (not (equal old-pos (point))))
  7022. (org-mark-ring-push old-pos old-buffer))))
  7023. (defun org-default-apps ()
  7024. "Return the default applications for this operating system."
  7025. (cond
  7026. ((eq system-type 'darwin)
  7027. org-file-apps-defaults-macosx)
  7028. ((eq system-type 'windows-nt)
  7029. org-file-apps-defaults-windowsnt)
  7030. (t org-file-apps-defaults-gnu)))
  7031. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7032. "Convert extensions to regular expressions in the cars of LIST.
  7033. Also, weed out any non-string entries, because the return value is used
  7034. only for regexp matching.
  7035. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7036. point to the symbol `emacs', indicating that the file should
  7037. be opened in Emacs."
  7038. (append
  7039. (delq nil
  7040. (mapcar (lambda (x)
  7041. (if (not (stringp (car x)))
  7042. nil
  7043. (if (string-match "\\W" (car x))
  7044. x
  7045. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7046. list))
  7047. (if add-auto-mode
  7048. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7049. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7050. (defun org-file-remote-p (file)
  7051. "Test whether FILE specifies a location on a remote system.
  7052. Return non-nil if the location is indeed remote.
  7053. For example, the filename \"/user@host:/foo\" specifies a location
  7054. on the system \"/user@host:\"."
  7055. (cond ((fboundp 'file-remote-p)
  7056. (file-remote-p file))
  7057. ((fboundp 'tramp-handle-file-remote-p)
  7058. (tramp-handle-file-remote-p file))
  7059. ((and (boundp 'ange-ftp-name-format)
  7060. (string-match (car ange-ftp-name-format) file))
  7061. t)
  7062. (t nil)))
  7063. ;;;; Refiling
  7064. (defun org-get-org-file ()
  7065. "Read a filename, with default directory `org-directory'."
  7066. (let ((default (or org-default-notes-file remember-data-file)))
  7067. (read-file-name (format "File name [%s]: " default)
  7068. (file-name-as-directory org-directory)
  7069. default)))
  7070. (defun org-notes-order-reversed-p ()
  7071. "Check if the current file should receive notes in reversed order."
  7072. (cond
  7073. ((not org-reverse-note-order) nil)
  7074. ((eq t org-reverse-note-order) t)
  7075. ((not (listp org-reverse-note-order)) nil)
  7076. (t (catch 'exit
  7077. (let ((all org-reverse-note-order)
  7078. entry)
  7079. (while (setq entry (pop all))
  7080. (if (string-match (car entry) buffer-file-name)
  7081. (throw 'exit (cdr entry))))
  7082. nil)))))
  7083. (defvar org-refile-target-table nil
  7084. "The list of refile targets, created by `org-refile'.")
  7085. (defvar org-agenda-new-buffers nil
  7086. "Buffers created to visit agenda files.")
  7087. (defun org-get-refile-targets (&optional default-buffer)
  7088. "Produce a table with refile targets."
  7089. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7090. targets txt re files f desc descre fast-path-p level)
  7091. (message "Getting targets...")
  7092. (with-current-buffer (or default-buffer (current-buffer))
  7093. (while (setq entry (pop entries))
  7094. (setq files (car entry) desc (cdr entry))
  7095. (setq fast-path-p nil)
  7096. (cond
  7097. ((null files) (setq files (list (current-buffer))))
  7098. ((eq files 'org-agenda-files)
  7099. (setq files (org-agenda-files 'unrestricted)))
  7100. ((and (symbolp files) (fboundp files))
  7101. (setq files (funcall files)))
  7102. ((and (symbolp files) (boundp files))
  7103. (setq files (symbol-value files))))
  7104. (if (stringp files) (setq files (list files)))
  7105. (cond
  7106. ((eq (car desc) :tag)
  7107. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7108. ((eq (car desc) :todo)
  7109. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7110. ((eq (car desc) :regexp)
  7111. (setq descre (cdr desc)))
  7112. ((eq (car desc) :level)
  7113. (setq descre (concat "^\\*\\{" (number-to-string
  7114. (if org-odd-levels-only
  7115. (1- (* 2 (cdr desc)))
  7116. (cdr desc)))
  7117. "\\}[ \t]")))
  7118. ((eq (car desc) :maxlevel)
  7119. (setq fast-path-p t)
  7120. (setq descre (concat "^\\*\\{1," (number-to-string
  7121. (if org-odd-levels-only
  7122. (1- (* 2 (cdr desc)))
  7123. (cdr desc)))
  7124. "\\}[ \t]")))
  7125. (t (error "Bad refiling target description %s" desc)))
  7126. (while (setq f (pop files))
  7127. (save-excursion
  7128. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7129. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7130. (setq f (expand-file-name f))
  7131. (save-excursion
  7132. (save-restriction
  7133. (widen)
  7134. (goto-char (point-min))
  7135. (while (re-search-forward descre nil t)
  7136. (goto-char (point-at-bol))
  7137. (when (looking-at org-complex-heading-regexp)
  7138. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7139. txt (org-link-display-format (match-string 4))
  7140. re (concat "^" (regexp-quote
  7141. (buffer-substring (match-beginning 1)
  7142. (match-end 4)))))
  7143. (if (match-end 5) (setq re (concat re "[ \t]+"
  7144. (regexp-quote
  7145. (match-string 5)))))
  7146. (setq re (concat re "[ \t]*$"))
  7147. (when org-refile-use-outline-path
  7148. (setq txt (mapconcat 'org-protect-slash
  7149. (append
  7150. (if (eq org-refile-use-outline-path 'file)
  7151. (list (file-name-nondirectory
  7152. (buffer-file-name (buffer-base-buffer))))
  7153. (if (eq org-refile-use-outline-path 'full-file-path)
  7154. (list (buffer-file-name (buffer-base-buffer)))))
  7155. (org-get-outline-path fast-path-p level txt)
  7156. (list txt))
  7157. "/")))
  7158. (push (list txt f re (point)) targets))
  7159. (goto-char (point-at-eol))))))))
  7160. (message "Getting targets...done")
  7161. (nreverse targets))))
  7162. (defun org-protect-slash (s)
  7163. (while (string-match "/" s)
  7164. (setq s (replace-match "\\" t t s)))
  7165. s)
  7166. (defvar org-olpa (make-vector 20 nil))
  7167. (defun org-get-outline-path (&optional fastp level heading)
  7168. "Return the outline path to the current entry, as a list."
  7169. (if fastp
  7170. (progn
  7171. (if (> level 19)
  7172. (error "Outline path failure, more than 19 levels."))
  7173. (loop for i from level upto 19 do
  7174. (aset org-olpa i nil))
  7175. (prog1
  7176. (delq nil (append org-olpa nil))
  7177. (aset org-olpa level heading)))
  7178. (let (rtn)
  7179. (save-excursion
  7180. (while (org-up-heading-safe)
  7181. (when (looking-at org-complex-heading-regexp)
  7182. (push (org-match-string-no-properties 4) rtn)))
  7183. rtn))))
  7184. (defvar org-refile-history nil
  7185. "History for refiling operations.")
  7186. (defun org-refile (&optional goto default-buffer)
  7187. "Move the entry at point to another heading.
  7188. The list of target headings is compiled using the information in
  7189. `org-refile-targets', which see. This list is created before each use
  7190. and will therefore always be up-to-date.
  7191. At the target location, the entry is filed as a subitem of the target heading.
  7192. Depending on `org-reverse-note-order', the new subitem will either be the
  7193. first or the last subitem.
  7194. If there is an active region, all entries in that region will be moved.
  7195. However, the region must fulfil the requirement that the first heading
  7196. is the first one sets the top-level of the moved text - at most siblings
  7197. below it are allowed.
  7198. With prefix arg GOTO, the command will only visit the target location,
  7199. not actually move anything.
  7200. With a double prefix `C-u C-u', go to the location where the last refiling
  7201. operation has put the subtree."
  7202. (interactive "P")
  7203. (let* ((cbuf (current-buffer))
  7204. (regionp (org-region-active-p))
  7205. (region-start (and regionp (region-beginning)))
  7206. (region-end (and regionp (region-end)))
  7207. (region-length (and regionp (- region-end region-start)))
  7208. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7209. pos it nbuf file re level reversed)
  7210. (when regionp (goto-char region-start)
  7211. (unless (org-kill-is-subtree-p
  7212. (buffer-substring region-start region-end))
  7213. (error "The region is not a (sequence of) subtree(s)")))
  7214. (if (equal goto '(16))
  7215. (org-refile-goto-last-stored)
  7216. (when (setq it (org-refile-get-location
  7217. (if goto "Goto: " "Refile to: ") default-buffer))
  7218. (setq file (nth 1 it)
  7219. re (nth 2 it)
  7220. pos (nth 3 it))
  7221. (if (and (equal (buffer-file-name) file)
  7222. (if regionp
  7223. (and (>= pos region-start)
  7224. (<= pos region-end))
  7225. (and (>= pos (point))
  7226. (< pos (save-excursion
  7227. (org-end-of-subtree t t))))))
  7228. (error "Cannot refile to position inside the tree or region"))
  7229. (setq nbuf (or (find-buffer-visiting file)
  7230. (find-file-noselect file)))
  7231. (if goto
  7232. (progn
  7233. (switch-to-buffer nbuf)
  7234. (goto-char pos)
  7235. (org-show-context 'org-goto))
  7236. (if regionp
  7237. (progn
  7238. (kill-new (buffer-substring region-start region-end))
  7239. (org-save-markers-in-region region-start region-end))
  7240. (org-copy-subtree 1 nil t))
  7241. (save-excursion
  7242. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7243. (find-file-noselect file))))
  7244. (setq reversed (org-notes-order-reversed-p))
  7245. (save-excursion
  7246. (save-restriction
  7247. (widen)
  7248. (goto-char pos)
  7249. (looking-at outline-regexp)
  7250. (setq level (org-get-valid-level (funcall outline-level) 1))
  7251. (goto-char
  7252. (if reversed
  7253. (or (outline-next-heading) (point-max))
  7254. (or (save-excursion (outline-get-next-sibling))
  7255. (org-end-of-subtree t t)
  7256. (point-max))))
  7257. (if (not (bolp)) (newline))
  7258. (bookmark-set "org-refile-last-stored")
  7259. (org-paste-subtree level))))
  7260. (if regionp
  7261. (delete-region (point) (+ (point) region-length))
  7262. (org-cut-subtree))
  7263. (setq org-markers-to-move nil)
  7264. (message "Refiled to \"%s\"" (car it)))))))
  7265. (defun org-refile-goto-last-stored ()
  7266. "Go to the location where the last refile was stored."
  7267. (interactive)
  7268. (bookmark-jump "org-refile-last-stored")
  7269. (message "This is the location of the last refile"))
  7270. (defun org-refile-get-location (&optional prompt default-buffer)
  7271. "Prompt the user for a refile location, using PROMPT."
  7272. (let ((org-refile-targets org-refile-targets)
  7273. (org-refile-use-outline-path org-refile-use-outline-path))
  7274. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7275. (unless org-refile-target-table
  7276. (error "No refile targets"))
  7277. (let* ((cbuf (current-buffer))
  7278. (partial-completion-mode nil)
  7279. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7280. (cfunc (if (and org-refile-use-outline-path
  7281. org-outline-path-complete-in-steps)
  7282. 'org-olpath-completing-read
  7283. 'org-ido-completing-read))
  7284. (extra (if org-refile-use-outline-path "/" ""))
  7285. (filename (and cfn (expand-file-name cfn)))
  7286. (tbl (mapcar
  7287. (lambda (x)
  7288. (if (not (equal filename (nth 1 x)))
  7289. (cons (concat (car x) extra " ("
  7290. (file-name-nondirectory (nth 1 x)) ")")
  7291. (cdr x))
  7292. (cons (concat (car x) extra) (cdr x))))
  7293. org-refile-target-table))
  7294. (completion-ignore-case t))
  7295. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7296. tbl)))
  7297. (defun org-olpath-completing-read (prompt collection &rest args)
  7298. "Read an outline path like a file name."
  7299. (let ((thetable collection)
  7300. (org-completion-use-ido nil)) ; does not work with ido.
  7301. (apply
  7302. 'org-ido-completing-read prompt
  7303. (lambda (string predicate &optional flag)
  7304. (let (rtn r f (l (length string)))
  7305. (cond
  7306. ((eq flag nil)
  7307. ;; try completion
  7308. (try-completion string thetable))
  7309. ((eq flag t)
  7310. ;; all-completions
  7311. (setq rtn (all-completions string thetable predicate))
  7312. (mapcar
  7313. (lambda (x)
  7314. (setq r (substring x l))
  7315. (if (string-match " ([^)]*)$" x)
  7316. (setq f (match-string 0 x))
  7317. (setq f ""))
  7318. (if (string-match "/" r)
  7319. (concat string (substring r 0 (match-end 0)) f)
  7320. x))
  7321. rtn))
  7322. ((eq flag 'lambda)
  7323. ;; exact match?
  7324. (assoc string thetable)))
  7325. ))
  7326. args)))
  7327. ;;;; Dynamic blocks
  7328. (defun org-find-dblock (name)
  7329. "Find the first dynamic block with name NAME in the buffer.
  7330. If not found, stay at current position and return nil."
  7331. (let (pos)
  7332. (save-excursion
  7333. (goto-char (point-min))
  7334. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7335. nil t)
  7336. (match-beginning 0))))
  7337. (if pos (goto-char pos))
  7338. pos))
  7339. (defconst org-dblock-start-re
  7340. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7341. "Matches the startline of a dynamic block, with parameters.")
  7342. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7343. "Matches the end of a dynamic block.")
  7344. (defun org-create-dblock (plist)
  7345. "Create a dynamic block section, with parameters taken from PLIST.
  7346. PLIST must contain a :name entry which is used as name of the block."
  7347. (unless (bolp) (newline))
  7348. (let ((name (plist-get plist :name)))
  7349. (insert "#+BEGIN: " name)
  7350. (while plist
  7351. (if (eq (car plist) :name)
  7352. (setq plist (cddr plist))
  7353. (insert " " (prin1-to-string (pop plist)))))
  7354. (insert "\n\n#+END:\n")
  7355. (beginning-of-line -2)))
  7356. (defun org-prepare-dblock ()
  7357. "Prepare dynamic block for refresh.
  7358. This empties the block, puts the cursor at the insert position and returns
  7359. the property list including an extra property :name with the block name."
  7360. (unless (looking-at org-dblock-start-re)
  7361. (error "Not at a dynamic block"))
  7362. (let* ((begdel (1+ (match-end 0)))
  7363. (name (org-no-properties (match-string 1)))
  7364. (params (append (list :name name)
  7365. (read (concat "(" (match-string 3) ")")))))
  7366. (unless (re-search-forward org-dblock-end-re nil t)
  7367. (error "Dynamic block not terminated"))
  7368. (setq params
  7369. (append params
  7370. (list :content (buffer-substring
  7371. begdel (match-beginning 0)))))
  7372. (delete-region begdel (match-beginning 0))
  7373. (goto-char begdel)
  7374. (open-line 1)
  7375. params))
  7376. (defun org-map-dblocks (&optional command)
  7377. "Apply COMMAND to all dynamic blocks in the current buffer.
  7378. If COMMAND is not given, use `org-update-dblock'."
  7379. (let ((cmd (or command 'org-update-dblock))
  7380. pos)
  7381. (save-excursion
  7382. (goto-char (point-min))
  7383. (while (re-search-forward org-dblock-start-re nil t)
  7384. (goto-char (setq pos (match-beginning 0)))
  7385. (condition-case nil
  7386. (funcall cmd)
  7387. (error (message "Error during update of dynamic block")))
  7388. (goto-char pos)
  7389. (unless (re-search-forward org-dblock-end-re nil t)
  7390. (error "Dynamic block not terminated"))))))
  7391. (defun org-dblock-update (&optional arg)
  7392. "User command for updating dynamic blocks.
  7393. Update the dynamic block at point. With prefix ARG, update all dynamic
  7394. blocks in the buffer."
  7395. (interactive "P")
  7396. (if arg
  7397. (org-update-all-dblocks)
  7398. (or (looking-at org-dblock-start-re)
  7399. (org-beginning-of-dblock))
  7400. (org-update-dblock)))
  7401. (defun org-update-dblock ()
  7402. "Update the dynamic block at point
  7403. This means to empty the block, parse for parameters and then call
  7404. the correct writing function."
  7405. (save-window-excursion
  7406. (let* ((pos (point))
  7407. (line (org-current-line))
  7408. (params (org-prepare-dblock))
  7409. (name (plist-get params :name))
  7410. (cmd (intern (concat "org-dblock-write:" name))))
  7411. (message "Updating dynamic block `%s' at line %d..." name line)
  7412. (funcall cmd params)
  7413. (message "Updating dynamic block `%s' at line %d...done" name line)
  7414. (goto-char pos))))
  7415. (defun org-beginning-of-dblock ()
  7416. "Find the beginning of the dynamic block at point.
  7417. Error if there is no such block at point."
  7418. (let ((pos (point))
  7419. beg)
  7420. (end-of-line 1)
  7421. (if (and (re-search-backward org-dblock-start-re nil t)
  7422. (setq beg (match-beginning 0))
  7423. (re-search-forward org-dblock-end-re nil t)
  7424. (> (match-end 0) pos))
  7425. (goto-char beg)
  7426. (goto-char pos)
  7427. (error "Not in a dynamic block"))))
  7428. (defun org-update-all-dblocks ()
  7429. "Update all dynamic blocks in the buffer.
  7430. This function can be used in a hook."
  7431. (when (org-mode-p)
  7432. (org-map-dblocks 'org-update-dblock)))
  7433. ;;;; Completion
  7434. (defconst org-additional-option-like-keywords
  7435. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7436. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7437. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7438. "BEGIN_QUOTE" "END_QUOTE"
  7439. "BEGIN_VERSE" "END_VERSE"
  7440. "BEGIN_SRC" "END_SRC"
  7441. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7442. (defcustom org-structure-template-alist
  7443. '(
  7444. ("s" "#+begin_src ?\n\n#+end_src"
  7445. "<src lang=\"?\">\n\n</src>")
  7446. ("e" "#+begin_example\n?\n#+end_example"
  7447. "<example>\n?\n</example>")
  7448. ("q" "#+begin_quote\n?\n#+end_quote"
  7449. "<quote>\n?\n</quote>")
  7450. ("v" "#+begin_verse\n?\n#+end_verse"
  7451. "<verse>\n?\n/verse>")
  7452. ("l" "#+begin_latex\n?\n#+end_latex"
  7453. "<literal style=\"latex\">\n?\n</literal>")
  7454. ("L" "#+latex: "
  7455. "<literal style=\"latex\">?</literal>")
  7456. ("h" "#+begin_html\n?\n#+end_html"
  7457. "<literal style=\"html\">\n?\n</literal>")
  7458. ("H" "#+html: "
  7459. "<literal style=\"html\">?</literal>")
  7460. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7461. ("A" "#+ascii: ")
  7462. ("i" "#+include %file ?"
  7463. "<include file=%file markup=\"?\">")
  7464. )
  7465. "Structure completion elements.
  7466. This is a list of abbreviation keys and values. The value gets inserted
  7467. it you type @samp{.} followed by the key and then the completion key,
  7468. usually `M-TAB'. %file will be replaced by a file name after prompting
  7469. for the file using completion.
  7470. There are two templates for each key, the first uses the original Org syntax,
  7471. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7472. the default when the /org-mtags.el/ module has been loaded. See also the
  7473. variable `org-mtags-prefer-muse-templates'.
  7474. This is an experimental feature, it is undecided if it is going to stay in."
  7475. :group 'org-completion
  7476. :type '(repeat
  7477. (string :tag "Key")
  7478. (string :tag "Template")
  7479. (string :tag "Muse Template")))
  7480. (defun org-try-structure-completion ()
  7481. "Try to complete a structure template before point.
  7482. This looks for strings like \"<e\" on an otherwise empty line and
  7483. expands them."
  7484. (let ((l (buffer-substring (point-at-bol) (point)))
  7485. a)
  7486. (when (and (looking-at "[ \t]*$")
  7487. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7488. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7489. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7490. (match-beginning 1)) a)
  7491. t)))
  7492. (defun org-complete-expand-structure-template (start cell)
  7493. "Expand a structure template."
  7494. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7495. (rpl (nth (if musep 2 1) cell)))
  7496. (delete-region start (point))
  7497. (when (string-match "\\`#\\+" rpl)
  7498. (cond
  7499. ((bolp))
  7500. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7501. (delete-region (point-at-bol) (point)))
  7502. (t (newline))))
  7503. (setq start (point))
  7504. (if (string-match "%file" rpl)
  7505. (setq rpl (replace-match
  7506. (concat
  7507. "\""
  7508. (save-match-data
  7509. (abbreviate-file-name (read-file-name "Include file: ")))
  7510. "\"")
  7511. t t rpl)))
  7512. (insert rpl)
  7513. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7514. (defun org-complete (&optional arg)
  7515. "Perform completion on word at point.
  7516. At the beginning of a headline, this completes TODO keywords as given in
  7517. `org-todo-keywords'.
  7518. If the current word is preceded by a backslash, completes the TeX symbols
  7519. that are supported for HTML support.
  7520. If the current word is preceded by \"#+\", completes special words for
  7521. setting file options.
  7522. In the line after \"#+STARTUP:, complete valid keywords.\"
  7523. At all other locations, this simply calls the value of
  7524. `org-completion-fallback-command'."
  7525. (interactive "P")
  7526. (org-without-partial-completion
  7527. (catch 'exit
  7528. (let* ((a nil)
  7529. (end (point))
  7530. (beg1 (save-excursion
  7531. (skip-chars-backward (org-re "[:alnum:]_@"))
  7532. (point)))
  7533. (beg (save-excursion
  7534. (skip-chars-backward "a-zA-Z0-9_:$")
  7535. (point)))
  7536. (confirm (lambda (x) (stringp (car x))))
  7537. (searchhead (equal (char-before beg) ?*))
  7538. (struct
  7539. (when (and (member (char-before beg1) '(?. ?<))
  7540. (setq a (assoc (buffer-substring beg1 (point))
  7541. org-structure-template-alist)))
  7542. (org-complete-expand-structure-template (1- beg1) a)
  7543. (throw 'exit t)))
  7544. (tag (and (equal (char-before beg1) ?:)
  7545. (equal (char-after (point-at-bol)) ?*)))
  7546. (prop (and (equal (char-before beg1) ?:)
  7547. (not (equal (char-after (point-at-bol)) ?*))))
  7548. (texp (equal (char-before beg) ?\\))
  7549. (link (equal (char-before beg) ?\[))
  7550. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7551. beg)
  7552. "#+"))
  7553. (startup (string-match "^#\\+STARTUP:.*"
  7554. (buffer-substring (point-at-bol) (point))))
  7555. (completion-ignore-case opt)
  7556. (type nil)
  7557. (tbl nil)
  7558. (table (cond
  7559. (opt
  7560. (setq type :opt)
  7561. (require 'org-exp)
  7562. (append
  7563. (mapcar
  7564. (lambda (x)
  7565. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7566. (cons (match-string 2 x) (match-string 1 x)))
  7567. (org-split-string (org-get-current-options) "\n"))
  7568. (mapcar 'list org-additional-option-like-keywords)))
  7569. (startup
  7570. (setq type :startup)
  7571. org-startup-options)
  7572. (link (append org-link-abbrev-alist-local
  7573. org-link-abbrev-alist))
  7574. (texp
  7575. (setq type :tex)
  7576. org-html-entities)
  7577. ((string-match "\\`\\*+[ \t]+\\'"
  7578. (buffer-substring (point-at-bol) beg))
  7579. (setq type :todo)
  7580. (mapcar 'list org-todo-keywords-1))
  7581. (searchhead
  7582. (setq type :searchhead)
  7583. (save-excursion
  7584. (goto-char (point-min))
  7585. (while (re-search-forward org-todo-line-regexp nil t)
  7586. (push (list
  7587. (org-make-org-heading-search-string
  7588. (match-string 3) t))
  7589. tbl)))
  7590. tbl)
  7591. (tag (setq type :tag beg beg1)
  7592. (or org-tag-alist (org-get-buffer-tags)))
  7593. (prop (setq type :prop beg beg1)
  7594. (mapcar 'list (org-buffer-property-keys nil t t)))
  7595. (t (progn
  7596. (call-interactively org-completion-fallback-command)
  7597. (throw 'exit nil)))))
  7598. (pattern (buffer-substring-no-properties beg end))
  7599. (completion (try-completion pattern table confirm)))
  7600. (cond ((eq completion t)
  7601. (if (not (assoc (upcase pattern) table))
  7602. (message "Already complete")
  7603. (if (and (equal type :opt)
  7604. (not (member (car (assoc (upcase pattern) table))
  7605. org-additional-option-like-keywords)))
  7606. (insert (substring (cdr (assoc (upcase pattern) table))
  7607. (length pattern)))
  7608. (if (memq type '(:tag :prop)) (insert ":")))))
  7609. ((null completion)
  7610. (message "Can't find completion for \"%s\"" pattern)
  7611. (ding))
  7612. ((not (string= pattern completion))
  7613. (delete-region beg end)
  7614. (if (string-match " +$" completion)
  7615. (setq completion (replace-match "" t t completion)))
  7616. (insert completion)
  7617. (if (get-buffer-window "*Completions*")
  7618. (delete-window (get-buffer-window "*Completions*")))
  7619. (if (assoc completion table)
  7620. (if (eq type :todo) (insert " ")
  7621. (if (memq type '(:tag :prop)) (insert ":"))))
  7622. (if (and (equal type :opt) (assoc completion table))
  7623. (message "%s" (substitute-command-keys
  7624. "Press \\[org-complete] again to insert example settings"))))
  7625. (t
  7626. (message "Making completion list...")
  7627. (let ((list (sort (all-completions pattern table confirm)
  7628. 'string<)))
  7629. (with-output-to-temp-buffer "*Completions*"
  7630. (condition-case nil
  7631. ;; Protection needed for XEmacs and emacs 21
  7632. (display-completion-list list pattern)
  7633. (error (display-completion-list list)))))
  7634. (message "Making completion list...%s" "done")))))))
  7635. ;;;; TODO, DEADLINE, Comments
  7636. (defun org-toggle-comment ()
  7637. "Change the COMMENT state of an entry."
  7638. (interactive)
  7639. (save-excursion
  7640. (org-back-to-heading)
  7641. (let (case-fold-search)
  7642. (if (looking-at (concat outline-regexp
  7643. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7644. (replace-match "" t t nil 1)
  7645. (if (looking-at outline-regexp)
  7646. (progn
  7647. (goto-char (match-end 0))
  7648. (insert org-comment-string " ")))))))
  7649. (defvar org-last-todo-state-is-todo nil
  7650. "This is non-nil when the last TODO state change led to a TODO state.
  7651. If the last change removed the TODO tag or switched to DONE, then
  7652. this is nil.")
  7653. (defvar org-setting-tags nil) ; dynamically skipped
  7654. (defun org-parse-local-options (string var)
  7655. "Parse STRING for startup setting relevant for variable VAR."
  7656. (let ((rtn (symbol-value var))
  7657. e opts)
  7658. (save-match-data
  7659. (if (or (not string) (not (string-match "\\S-" string)))
  7660. rtn
  7661. (setq opts (delq nil (mapcar (lambda (x)
  7662. (setq e (assoc x org-startup-options))
  7663. (if (eq (nth 1 e) var) e nil))
  7664. (org-split-string string "[ \t]+"))))
  7665. (if (not opts)
  7666. rtn
  7667. (setq rtn nil)
  7668. (while (setq e (pop opts))
  7669. (if (not (nth 3 e))
  7670. (setq rtn (nth 2 e))
  7671. (if (not (listp rtn)) (setq rtn nil))
  7672. (push (nth 2 e) rtn)))
  7673. rtn)))))
  7674. (defvar org-todo-setup-filter-hook nil
  7675. "Hook for functions that pre-filter todo specs.
  7676. Each function takes a todo spec and returns either `nil' or the spec
  7677. transformed into canonical form." )
  7678. (defvar org-todo-get-default-hook nil
  7679. "Hook for functions that get a default item for todo.
  7680. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7681. `nil' or a string to be used for the todo mark." )
  7682. (defvar org-agenda-headline-snapshot-before-repeat)
  7683. (defun org-todo (&optional arg)
  7684. "Change the TODO state of an item.
  7685. The state of an item is given by a keyword at the start of the heading,
  7686. like
  7687. *** TODO Write paper
  7688. *** DONE Call mom
  7689. The different keywords are specified in the variable `org-todo-keywords'.
  7690. By default the available states are \"TODO\" and \"DONE\".
  7691. So for this example: when the item starts with TODO, it is changed to DONE.
  7692. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7693. DONE are present, add TODO at the beginning of the heading.
  7694. With C-u prefix arg, use completion to determine the new state.
  7695. With numeric prefix arg, switch to that state.
  7696. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7697. With a tripple C-u prefix, circumvent any state blocking.
  7698. For calling through lisp, arg is also interpreted in the following way:
  7699. 'none -> empty state
  7700. \"\"(empty string) -> switch to empty state
  7701. 'done -> switch to DONE
  7702. 'nextset -> switch to the next set of keywords
  7703. 'previousset -> switch to the previous set of keywords
  7704. \"WAITING\" -> switch to the specified keyword, but only if it
  7705. really is a member of `org-todo-keywords'."
  7706. (interactive "P")
  7707. (if (equal arg '(16)) (setq arg 'nextset))
  7708. (let ((org-blocker-hook org-blocker-hook))
  7709. (when (equal arg '(64))
  7710. (setq arg nil org-blocker-hook nil))
  7711. (save-excursion
  7712. (catch 'exit
  7713. (org-back-to-heading)
  7714. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7715. (or (looking-at (concat " +" org-todo-regexp " *"))
  7716. (looking-at " *"))
  7717. (let* ((match-data (match-data))
  7718. (startpos (point-at-bol))
  7719. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7720. (org-log-done org-log-done)
  7721. (org-log-repeat org-log-repeat)
  7722. (org-todo-log-states org-todo-log-states)
  7723. (this (match-string 1))
  7724. (hl-pos (match-beginning 0))
  7725. (head (org-get-todo-sequence-head this))
  7726. (ass (assoc head org-todo-kwd-alist))
  7727. (interpret (nth 1 ass))
  7728. (done-word (nth 3 ass))
  7729. (final-done-word (nth 4 ass))
  7730. (last-state (or this ""))
  7731. (completion-ignore-case t)
  7732. (member (member this org-todo-keywords-1))
  7733. (tail (cdr member))
  7734. (state (cond
  7735. ((and org-todo-key-trigger
  7736. (or (and (equal arg '(4))
  7737. (eq org-use-fast-todo-selection 'prefix))
  7738. (and (not arg) org-use-fast-todo-selection
  7739. (not (eq org-use-fast-todo-selection
  7740. 'prefix)))))
  7741. ;; Use fast selection
  7742. (org-fast-todo-selection))
  7743. ((and (equal arg '(4))
  7744. (or (not org-use-fast-todo-selection)
  7745. (not org-todo-key-trigger)))
  7746. ;; Read a state with completion
  7747. (org-ido-completing-read
  7748. "State: " (mapcar (lambda(x) (list x))
  7749. org-todo-keywords-1)
  7750. nil t))
  7751. ((eq arg 'right)
  7752. (if this
  7753. (if tail (car tail) nil)
  7754. (car org-todo-keywords-1)))
  7755. ((eq arg 'left)
  7756. (if (equal member org-todo-keywords-1)
  7757. nil
  7758. (if this
  7759. (nth (- (length org-todo-keywords-1)
  7760. (length tail) 2)
  7761. org-todo-keywords-1)
  7762. (org-last org-todo-keywords-1))))
  7763. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7764. (setq arg nil))) ; hack to fall back to cycling
  7765. (arg
  7766. ;; user or caller requests a specific state
  7767. (cond
  7768. ((equal arg "") nil)
  7769. ((eq arg 'none) nil)
  7770. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7771. ((eq arg 'nextset)
  7772. (or (car (cdr (member head org-todo-heads)))
  7773. (car org-todo-heads)))
  7774. ((eq arg 'previousset)
  7775. (let ((org-todo-heads (reverse org-todo-heads)))
  7776. (or (car (cdr (member head org-todo-heads)))
  7777. (car org-todo-heads))))
  7778. ((car (member arg org-todo-keywords-1)))
  7779. ((nth (1- (prefix-numeric-value arg))
  7780. org-todo-keywords-1))))
  7781. ((null member) (or head (car org-todo-keywords-1)))
  7782. ((equal this final-done-word) nil) ;; -> make empty
  7783. ((null tail) nil) ;; -> first entry
  7784. ((memq interpret '(type priority))
  7785. (if (eq this-command last-command)
  7786. (car tail)
  7787. (if (> (length tail) 0)
  7788. (or done-word (car org-done-keywords))
  7789. nil)))
  7790. (t
  7791. (car tail))))
  7792. (state (or
  7793. (run-hook-with-args-until-success
  7794. 'org-todo-get-default-hook state last-state)
  7795. state))
  7796. (next (if state (concat " " state " ") " "))
  7797. (change-plist (list :type 'todo-state-change :from this :to state
  7798. :position startpos))
  7799. dolog now-done-p)
  7800. (when org-blocker-hook
  7801. (setq org-last-todo-state-is-todo
  7802. (not (member this org-done-keywords)))
  7803. (unless (save-excursion
  7804. (save-match-data
  7805. (run-hook-with-args-until-failure
  7806. 'org-blocker-hook change-plist)))
  7807. (if (interactive-p)
  7808. (error "TODO state change from %s to %s blocked" this state)
  7809. ;; fail silently
  7810. (message "TODO state change from %s to %s blocked" this state)
  7811. (throw 'exit nil))))
  7812. (store-match-data match-data)
  7813. (replace-match next t t)
  7814. (unless (pos-visible-in-window-p hl-pos)
  7815. (message "TODO state changed to %s" (org-trim next)))
  7816. (unless head
  7817. (setq head (org-get-todo-sequence-head state)
  7818. ass (assoc head org-todo-kwd-alist)
  7819. interpret (nth 1 ass)
  7820. done-word (nth 3 ass)
  7821. final-done-word (nth 4 ass)))
  7822. (when (memq arg '(nextset previousset))
  7823. (message "Keyword-Set %d/%d: %s"
  7824. (- (length org-todo-sets) -1
  7825. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7826. (length org-todo-sets)
  7827. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7828. (setq org-last-todo-state-is-todo
  7829. (not (member state org-done-keywords)))
  7830. (setq now-done-p (and (member state org-done-keywords)
  7831. (not (member this org-done-keywords))))
  7832. (and logging (org-local-logging logging))
  7833. (when (and (or org-todo-log-states org-log-done)
  7834. (not (memq arg '(nextset previousset))))
  7835. ;; we need to look at recording a time and note
  7836. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7837. (nth 2 (assoc this org-todo-log-states))))
  7838. (when (and state
  7839. (member state org-not-done-keywords)
  7840. (not (member this org-not-done-keywords)))
  7841. ;; This is now a todo state and was not one before
  7842. ;; If there was a CLOSED time stamp, get rid of it.
  7843. (org-add-planning-info nil nil 'closed))
  7844. (when (and now-done-p org-log-done)
  7845. ;; It is now done, and it was not done before
  7846. (org-add-planning-info 'closed (org-current-time))
  7847. (if (and (not dolog) (eq 'note org-log-done))
  7848. (org-add-log-setup 'done state this 'findpos 'note)))
  7849. (when (and state dolog)
  7850. ;; This is a non-nil state, and we need to log it
  7851. (org-add-log-setup 'state state this 'findpos dolog)))
  7852. ;; Fixup tag positioning
  7853. (org-todo-trigger-tag-changes state)
  7854. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7855. (when org-provide-todo-statistics
  7856. (org-update-parent-todo-statistics))
  7857. (run-hooks 'org-after-todo-state-change-hook)
  7858. (if (and arg (not (member state org-done-keywords)))
  7859. (setq head (org-get-todo-sequence-head state)))
  7860. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7861. ;; Do we need to trigger a repeat?
  7862. (when now-done-p
  7863. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7864. ;; This is for the agenda, take a snapshot of the headline.
  7865. (save-match-data
  7866. (setq org-agenda-headline-snapshot-before-repeat
  7867. (org-get-heading))))
  7868. (org-auto-repeat-maybe state))
  7869. ;; Fixup cursor location if close to the keyword
  7870. (if (and (outline-on-heading-p)
  7871. (not (bolp))
  7872. (save-excursion (beginning-of-line 1)
  7873. (looking-at org-todo-line-regexp))
  7874. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7875. (progn
  7876. (goto-char (or (match-end 2) (match-end 1)))
  7877. (just-one-space)))
  7878. (when org-trigger-hook
  7879. (save-excursion
  7880. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7881. (defun org-block-todo-from-children-or-siblings (change-plist)
  7882. "Block turning an entry into a TODO, using the hierarchy.
  7883. This checks whether the current task should be blocked from state
  7884. changes. Such blocking occurs when:
  7885. 1. The task has children which are not all in a completed state.
  7886. 2. A task has a parent with the property :ORDERED:, and there
  7887. are siblings prior to the current task with incomplete
  7888. status."
  7889. (catch 'dont-block
  7890. ;; If this is not a todo state change, or if this entry is already DONE,
  7891. ;; do not block
  7892. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7893. (member (plist-get change-plist :from)
  7894. (cons 'done org-done-keywords))
  7895. (member (plist-get change-plist :to)
  7896. (cons 'todo org-not-done-keywords)))
  7897. (throw 'dont-block t))
  7898. ;; If this task has children, and any are undone, it's blocked
  7899. (save-excursion
  7900. (org-back-to-heading t)
  7901. (let ((this-level (funcall outline-level)))
  7902. (outline-next-heading)
  7903. (let ((child-level (funcall outline-level)))
  7904. (while (and (not (eobp))
  7905. (> child-level this-level))
  7906. ;; this todo has children, check whether they are all
  7907. ;; completed
  7908. (if (and (not (org-entry-is-done-p))
  7909. (org-entry-is-todo-p))
  7910. (throw 'dont-block nil))
  7911. (outline-next-heading)
  7912. (setq child-level (funcall outline-level))))))
  7913. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7914. ;; any previous siblings are undone, it's blocked
  7915. (save-excursion
  7916. (org-back-to-heading t)
  7917. (when (save-excursion
  7918. (ignore-errors
  7919. (org-up-heading-all 1)
  7920. (org-entry-get (point) "ORDERED")))
  7921. (let* ((this-level (funcall outline-level))
  7922. (current-level this-level))
  7923. (while (and (not (bobp))
  7924. (= current-level this-level))
  7925. (outline-previous-heading)
  7926. (setq current-level (funcall outline-level))
  7927. (if (= current-level this-level)
  7928. ;; this todo has children, check whether they are all
  7929. ;; completed
  7930. (if (and (not (org-entry-is-done-p))
  7931. (org-entry-is-todo-p))
  7932. (throw 'dont-block nil)))))))
  7933. t)) ; don't block
  7934. (defcustom org-track-ordered-property-with-tag nil
  7935. "Should the ORDERED property also be shown as a tag?
  7936. The ORDERED property decides if an entry should require subtasks to be
  7937. completed in sequence. Since a property is not very visible, setting
  7938. this option means that toggling the ORDERED property with the command
  7939. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  7940. not relevant for the behavior, but it makes things more visible.
  7941. Note that toggling the tag with tags commands will not change the property
  7942. and therefore not influence behavior!
  7943. This can be t, meaning the tag ORDERED should be used, It can also be a
  7944. string to select a different tag for this task."
  7945. :group 'org-todo
  7946. :type '(choice
  7947. (const :tag "No tracking" nil)
  7948. (const :tag "Track with ORDERED tag" t)
  7949. (string :tag "Use other tag")))
  7950. (defun org-toggle-ordered-property ()
  7951. "Toggle the ORDERED property of the current entry.
  7952. For better visibility, you can track the value of this property with a tag.
  7953. See variable `org-track-ordered-property-with-tag'."
  7954. (interactive)
  7955. (let* ((t1 org-track-ordered-property-with-tag)
  7956. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  7957. (save-excursion
  7958. (org-back-to-heading)
  7959. (if (org-entry-get nil "ORDERED")
  7960. (progn
  7961. (org-delete-property "ORDERED")
  7962. (and tag (org-toggle-tag tag 'off))
  7963. (message "Subtasks can be completed in arbitrary order"))
  7964. (org-entry-put nil "ORDERED" "t")
  7965. (and tag (org-toggle-tag tag 'on))
  7966. (message "Subtasks must be completed in sequence")))))
  7967. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  7968. (defun org-block-todo-from-checkboxes (change-plist)
  7969. "Block turning an entry into a TODO, using checkboxes.
  7970. This checks whether the current task should be blocked from state
  7971. changes because there are uncheckd boxes in this entry."
  7972. (catch 'dont-block
  7973. ;; If this is not a todo state change, or if this entry is already DONE,
  7974. ;; do not block
  7975. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7976. (member (plist-get change-plist :from)
  7977. (cons 'done org-done-keywords))
  7978. (member (plist-get change-plist :to)
  7979. (cons 'todo org-not-done-keywords)))
  7980. (throw 'dont-block t))
  7981. ;; If this task has checkboxes that are not checked, it's blocked
  7982. (save-excursion
  7983. (org-back-to-heading t)
  7984. (let ((beg (point)) end)
  7985. (outline-next-heading)
  7986. (setq end (point))
  7987. (goto-char beg)
  7988. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7989. end t)
  7990. (progn
  7991. (if (boundp 'org-blocked-by-checkboxes)
  7992. (setq org-blocked-by-checkboxes t))
  7993. (throw 'dont-block nil)))))
  7994. t)) ; do not block
  7995. (defun org-update-parent-todo-statistics ()
  7996. "Update any statistics cookie in the parent of the current headline."
  7997. (interactive)
  7998. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7999. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8000. (catch 'exit
  8001. (save-excursion
  8002. (setq level (org-up-heading-safe))
  8003. (unless level
  8004. (throw 'exit nil))
  8005. (while (re-search-forward box-re (point-at-eol) t)
  8006. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8007. (setq is-percent (match-end 2))
  8008. (save-match-data
  8009. (unless (outline-next-heading) (throw 'exit nil))
  8010. (while (looking-at org-todo-line-regexp)
  8011. (setq kwd (match-string 2))
  8012. (and kwd (setq cnt-all (1+ cnt-all)))
  8013. (and (member kwd org-done-keywords)
  8014. (setq cnt-done (1+ cnt-done)))
  8015. (condition-case nil
  8016. (org-forward-same-level 1)
  8017. (error (end-of-line 1)))))
  8018. (replace-match
  8019. (if is-percent
  8020. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8021. (format "[%d/%d]" cnt-done cnt-all))))
  8022. (when cookie-present
  8023. (run-hook-with-args 'org-after-todo-statistics-hook
  8024. cnt-done (- cnt-all cnt-done)))))))
  8025. (defvar org-after-todo-statistics-hook nil
  8026. "Hook that is called after a TODO statistics cookie has been updated.
  8027. Each function is called with two arguments: the number of not-done entries
  8028. and the number of done entries.
  8029. For example, the following function, when added to this hook, will switch
  8030. an entry to DONE when all children are done, and back to TODO when new
  8031. entries are set to a TODO status. Note that this hook is only called
  8032. when there is a statistics cookie in the headline!
  8033. (defun org-summary-todo (n-done n-not-done)
  8034. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8035. (let (org-log-done org-log-states) ; turn off logging
  8036. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8037. ")
  8038. (defun org-todo-trigger-tag-changes (state)
  8039. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8040. (let ((l org-todo-state-tags-triggers)
  8041. changes)
  8042. (when (or (not state) (equal state ""))
  8043. (setq changes (append changes (cdr (assoc "" l)))))
  8044. (when (and (stringp state) (> (length state) 0))
  8045. (setq changes (append changes (cdr (assoc state l)))))
  8046. (when (member state org-not-done-keywords)
  8047. (setq changes (append changes (cdr (assoc 'todo l)))))
  8048. (when (member state org-done-keywords)
  8049. (setq changes (append changes (cdr (assoc 'done l)))))
  8050. (dolist (c changes)
  8051. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8052. (defun org-local-logging (value)
  8053. "Get logging settings from a property VALUE."
  8054. (let* (words w a)
  8055. ;; directly set the variables, they are already local.
  8056. (setq org-log-done nil
  8057. org-log-repeat nil
  8058. org-todo-log-states nil)
  8059. (setq words (org-split-string value))
  8060. (while (setq w (pop words))
  8061. (cond
  8062. ((setq a (assoc w org-startup-options))
  8063. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8064. (set (nth 1 a) (nth 2 a))))
  8065. ((setq a (org-extract-log-state-settings w))
  8066. (and (member (car a) org-todo-keywords-1)
  8067. (push a org-todo-log-states)))))))
  8068. (defun org-get-todo-sequence-head (kwd)
  8069. "Return the head of the TODO sequence to which KWD belongs.
  8070. If KWD is not set, check if there is a text property remembering the
  8071. right sequence."
  8072. (let (p)
  8073. (cond
  8074. ((not kwd)
  8075. (or (get-text-property (point-at-bol) 'org-todo-head)
  8076. (progn
  8077. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8078. nil (point-at-eol)))
  8079. (get-text-property p 'org-todo-head))))
  8080. ((not (member kwd org-todo-keywords-1))
  8081. (car org-todo-keywords-1))
  8082. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8083. (defun org-fast-todo-selection ()
  8084. "Fast TODO keyword selection with single keys.
  8085. Returns the new TODO keyword, or nil if no state change should occur."
  8086. (let* ((fulltable org-todo-key-alist)
  8087. (done-keywords org-done-keywords) ;; needed for the faces.
  8088. (maxlen (apply 'max (mapcar
  8089. (lambda (x)
  8090. (if (stringp (car x)) (string-width (car x)) 0))
  8091. fulltable)))
  8092. (expert nil)
  8093. (fwidth (+ maxlen 3 1 3))
  8094. (ncol (/ (- (window-width) 4) fwidth))
  8095. tg cnt e c tbl
  8096. groups ingroup)
  8097. (save-excursion
  8098. (save-window-excursion
  8099. (if expert
  8100. (set-buffer (get-buffer-create " *Org todo*"))
  8101. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8102. (erase-buffer)
  8103. (org-set-local 'org-done-keywords done-keywords)
  8104. (setq tbl fulltable cnt 0)
  8105. (while (setq e (pop tbl))
  8106. (cond
  8107. ((equal e '(:startgroup))
  8108. (push '() groups) (setq ingroup t)
  8109. (when (not (= cnt 0))
  8110. (setq cnt 0)
  8111. (insert "\n"))
  8112. (insert "{ "))
  8113. ((equal e '(:endgroup))
  8114. (setq ingroup nil cnt 0)
  8115. (insert "}\n"))
  8116. ((equal e '(:newline))
  8117. (insert "\n "))
  8118. (t
  8119. (setq tg (car e) c (cdr e))
  8120. (if ingroup (push tg (car groups)))
  8121. (setq tg (org-add-props tg nil 'face
  8122. (org-get-todo-face tg)))
  8123. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8124. (insert "[" c "] " tg (make-string
  8125. (- fwidth 4 (length tg)) ?\ ))
  8126. (when (= (setq cnt (1+ cnt)) ncol)
  8127. (insert "\n")
  8128. (if ingroup (insert " "))
  8129. (setq cnt 0)))))
  8130. (insert "\n")
  8131. (goto-char (point-min))
  8132. (if (not expert) (org-fit-window-to-buffer))
  8133. (message "[a-z..]:Set [SPC]:clear")
  8134. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8135. (cond
  8136. ((or (= c ?\C-g)
  8137. (and (= c ?q) (not (rassoc c fulltable))))
  8138. (setq quit-flag t))
  8139. ((= c ?\ ) nil)
  8140. ((setq e (rassoc c fulltable) tg (car e))
  8141. tg)
  8142. (t (setq quit-flag t)))))))
  8143. (defun org-entry-is-todo-p ()
  8144. (member (org-get-todo-state) org-not-done-keywords))
  8145. (defun org-entry-is-done-p ()
  8146. (member (org-get-todo-state) org-done-keywords))
  8147. (defun org-get-todo-state ()
  8148. (save-excursion
  8149. (org-back-to-heading t)
  8150. (and (looking-at org-todo-line-regexp)
  8151. (match-end 2)
  8152. (match-string 2))))
  8153. (defun org-at-date-range-p (&optional inactive-ok)
  8154. "Is the cursor inside a date range?"
  8155. (interactive)
  8156. (save-excursion
  8157. (catch 'exit
  8158. (let ((pos (point)))
  8159. (skip-chars-backward "^[<\r\n")
  8160. (skip-chars-backward "<[")
  8161. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8162. (>= (match-end 0) pos)
  8163. (throw 'exit t))
  8164. (skip-chars-backward "^<[\r\n")
  8165. (skip-chars-backward "<[")
  8166. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8167. (>= (match-end 0) pos)
  8168. (throw 'exit t)))
  8169. nil)))
  8170. (defun org-get-repeat ()
  8171. "Check if there is a deadline/schedule with repeater in this entry."
  8172. (save-match-data
  8173. (save-excursion
  8174. (org-back-to-heading t)
  8175. (if (re-search-forward
  8176. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8177. (match-string 1)))))
  8178. (defvar org-last-changed-timestamp)
  8179. (defvar org-last-inserted-timestamp)
  8180. (defvar org-log-post-message)
  8181. (defvar org-log-note-purpose)
  8182. (defvar org-log-note-how)
  8183. (defvar org-log-note-extra)
  8184. (defun org-auto-repeat-maybe (done-word)
  8185. "Check if the current headline contains a repeated deadline/schedule.
  8186. If yes, set TODO state back to what it was and change the base date
  8187. of repeating deadline/scheduled time stamps to new date.
  8188. This function is run automatically after each state change to a DONE state."
  8189. ;; last-state is dynamically scoped into this function
  8190. (let* ((repeat (org-get-repeat))
  8191. (aa (assoc last-state org-todo-kwd-alist))
  8192. (interpret (nth 1 aa))
  8193. (head (nth 2 aa))
  8194. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8195. (msg "Entry repeats: ")
  8196. (org-log-done nil)
  8197. (org-todo-log-states nil)
  8198. (nshiftmax 10) (nshift 0)
  8199. re type n what ts time)
  8200. (when repeat
  8201. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8202. (org-todo (if (eq interpret 'type) last-state head))
  8203. (when org-log-repeat
  8204. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8205. (memq 'org-add-log-note post-command-hook))
  8206. ;; OK, we are already setup for some record
  8207. (if (eq org-log-repeat 'note)
  8208. ;; make sure we take a note, not only a time stamp
  8209. (setq org-log-note-how 'note))
  8210. ;; Set up for taking a record
  8211. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8212. last-state
  8213. 'findpos org-log-repeat)))
  8214. (org-back-to-heading t)
  8215. (org-add-planning-info nil nil 'closed)
  8216. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8217. org-deadline-time-regexp "\\)\\|\\("
  8218. org-ts-regexp "\\)"))
  8219. (while (re-search-forward
  8220. re (save-excursion (outline-next-heading) (point)) t)
  8221. (setq type (if (match-end 1) org-scheduled-string
  8222. (if (match-end 3) org-deadline-string "Plain:"))
  8223. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8224. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8225. (setq n (string-to-number (match-string 2 ts))
  8226. what (match-string 3 ts))
  8227. (if (equal what "w") (setq n (* n 7) what "d"))
  8228. ;; Preparation, see if we need to modify the start date for the change
  8229. (when (match-end 1)
  8230. (setq time (save-match-data (org-time-string-to-time ts)))
  8231. (cond
  8232. ((equal (match-string 1 ts) ".")
  8233. ;; Shift starting date to today
  8234. (org-timestamp-change
  8235. (- (time-to-days (current-time)) (time-to-days time))
  8236. 'day))
  8237. ((equal (match-string 1 ts) "+")
  8238. (while (or (= nshift 0)
  8239. (<= (time-to-days time) (time-to-days (current-time))))
  8240. (when (= (incf nshift) nshiftmax)
  8241. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8242. (error "Abort")))
  8243. (org-timestamp-change n (cdr (assoc what whata)))
  8244. (org-at-timestamp-p t)
  8245. (setq ts (match-string 1))
  8246. (setq time (save-match-data (org-time-string-to-time ts))))
  8247. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8248. ;; rematch, so that we have everything in place for the real shift
  8249. (org-at-timestamp-p t)
  8250. (setq ts (match-string 1))
  8251. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8252. (org-timestamp-change n (cdr (assoc what whata)))
  8253. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8254. (setq org-log-post-message msg)
  8255. (message "%s" msg))))
  8256. (defun org-show-todo-tree (arg)
  8257. "Make a compact tree which shows all headlines marked with TODO.
  8258. The tree will show the lines where the regexp matches, and all higher
  8259. headlines above the match.
  8260. With a \\[universal-argument] prefix, also show the DONE entries.
  8261. With a numeric prefix N, construct a sparse tree for the Nth element
  8262. of `org-todo-keywords-1'."
  8263. (interactive "P")
  8264. (let ((case-fold-search nil)
  8265. (kwd-re
  8266. (cond ((null arg) org-not-done-regexp)
  8267. ((equal arg '(4))
  8268. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8269. (mapcar 'list org-todo-keywords-1))))
  8270. (concat "\\("
  8271. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8272. "\\)\\>")))
  8273. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8274. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8275. org-todo-keywords-1)))
  8276. (t (error "Invalid prefix argument: %s" arg)))))
  8277. (message "%d TODO entries found"
  8278. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8279. (defun org-deadline (&optional remove time)
  8280. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8281. With argument REMOVE, remove any deadline from the item.
  8282. When TIME is set, it should be an internal time specification, and the
  8283. scheduling will use the corresponding date."
  8284. (interactive "P")
  8285. (if remove
  8286. (progn
  8287. (org-remove-timestamp-with-keyword org-deadline-string)
  8288. (message "Item no longer has a deadline."))
  8289. (if (org-get-repeat)
  8290. (error "Cannot change deadline on task with repeater, please do that by hand")
  8291. (org-add-planning-info 'deadline time 'closed)
  8292. (message "Deadline on %s" org-last-inserted-timestamp))))
  8293. (defun org-schedule (&optional remove time)
  8294. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8295. With argument REMOVE, remove any scheduling date from the item.
  8296. When TIME is set, it should be an internal time specification, and the
  8297. scheduling will use the corresponding date."
  8298. (interactive "P")
  8299. (if remove
  8300. (progn
  8301. (org-remove-timestamp-with-keyword org-scheduled-string)
  8302. (message "Item is no longer scheduled."))
  8303. (if (org-get-repeat)
  8304. (error "Cannot reschedule task with repeater, please do that by hand")
  8305. (org-add-planning-info 'scheduled time 'closed)
  8306. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8307. (defun org-get-scheduled-time (pom &optional inherit)
  8308. "Get the scheduled time as a time tuple, of a format suitable
  8309. for calling org-schedule with, or if there is no scheduling,
  8310. returns nil."
  8311. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8312. (when time
  8313. (apply 'encode-time (org-parse-time-string time)))))
  8314. (defun org-get-deadline-time (pom &optional inherit)
  8315. "Get the deadine as a time tuple, of a format suitable for
  8316. calling org-deadlin with, or if there is no scheduling, returns
  8317. nil."
  8318. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8319. (when time
  8320. (apply 'encode-time (org-parse-time-string time)))))
  8321. (defun org-remove-timestamp-with-keyword (keyword)
  8322. "Remove all time stamps with KEYWORD in the current entry."
  8323. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8324. beg)
  8325. (save-excursion
  8326. (org-back-to-heading t)
  8327. (setq beg (point))
  8328. (org-end-of-subtree t t)
  8329. (while (re-search-backward re beg t)
  8330. (replace-match "")
  8331. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8332. (equal (char-before) ?\ ))
  8333. (backward-delete-char 1)
  8334. (if (string-match "^[ \t]*$" (buffer-substring
  8335. (point-at-bol) (point-at-eol)))
  8336. (delete-region (point-at-bol)
  8337. (min (point-max) (1+ (point-at-eol))))))))))
  8338. (defun org-add-planning-info (what &optional time &rest remove)
  8339. "Insert new timestamp with keyword in the line directly after the headline.
  8340. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8341. If non is given, the user is prompted for a date.
  8342. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8343. be removed."
  8344. (interactive)
  8345. (let (org-time-was-given org-end-time-was-given ts
  8346. end default-time default-input)
  8347. (catch 'exit
  8348. (when (and (not time) (memq what '(scheduled deadline)))
  8349. ;; Try to get a default date/time from existing timestamp
  8350. (save-excursion
  8351. (org-back-to-heading t)
  8352. (setq end (save-excursion (outline-next-heading) (point)))
  8353. (when (re-search-forward (if (eq what 'scheduled)
  8354. org-scheduled-time-regexp
  8355. org-deadline-time-regexp)
  8356. end t)
  8357. (setq ts (match-string 1)
  8358. default-time
  8359. (apply 'encode-time (org-parse-time-string ts))
  8360. default-input (and ts (org-get-compact-tod ts))))))
  8361. (when what
  8362. ;; If necessary, get the time from the user
  8363. (setq time (or time (org-read-date nil 'to-time nil nil
  8364. default-time default-input))))
  8365. (when (and org-insert-labeled-timestamps-at-point
  8366. (member what '(scheduled deadline)))
  8367. (insert
  8368. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8369. (org-insert-time-stamp time org-time-was-given
  8370. nil nil nil (list org-end-time-was-given))
  8371. (setq what nil))
  8372. (save-excursion
  8373. (save-restriction
  8374. (let (col list elt ts buffer-invisibility-spec)
  8375. (org-back-to-heading t)
  8376. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8377. (goto-char (match-end 1))
  8378. (setq col (current-column))
  8379. (goto-char (match-end 0))
  8380. (if (eobp) (insert "\n") (forward-char 1))
  8381. (when (and (not what)
  8382. (not (looking-at org-keyword-time-not-clock-regexp)))
  8383. ;; Nothing to add, nothing to remove...... :-)
  8384. (throw 'exit nil))
  8385. (if (and (not (looking-at outline-regexp))
  8386. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8387. "[^\r\n]*"))
  8388. (not (equal (match-string 1) org-clock-string)))
  8389. (narrow-to-region (match-beginning 0) (match-end 0))
  8390. (insert-before-markers "\n")
  8391. (backward-char 1)
  8392. (narrow-to-region (point) (point))
  8393. (and org-adapt-indentation (org-indent-to-column col)))
  8394. ;; Check if we have to remove something.
  8395. (setq list (cons what remove))
  8396. (while list
  8397. (setq elt (pop list))
  8398. (goto-char (point-min))
  8399. (when (or (and (eq elt 'scheduled)
  8400. (re-search-forward org-scheduled-time-regexp nil t))
  8401. (and (eq elt 'deadline)
  8402. (re-search-forward org-deadline-time-regexp nil t))
  8403. (and (eq elt 'closed)
  8404. (re-search-forward org-closed-time-regexp nil t)))
  8405. (replace-match "")
  8406. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8407. (if (looking-at " +") (replace-match ""))))
  8408. (goto-char (point-max))
  8409. (when what
  8410. (insert
  8411. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8412. (cond ((eq what 'scheduled) org-scheduled-string)
  8413. ((eq what 'deadline) org-deadline-string)
  8414. ((eq what 'closed) org-closed-string))
  8415. " ")
  8416. (setq ts (org-insert-time-stamp
  8417. time
  8418. (or org-time-was-given
  8419. (and (eq what 'closed) org-log-done-with-time))
  8420. (eq what 'closed)
  8421. nil nil (list org-end-time-was-given)))
  8422. (end-of-line 1))
  8423. (goto-char (point-min))
  8424. (widen)
  8425. (if (and (looking-at "[ \t]+\n")
  8426. (equal (char-before) ?\n))
  8427. (delete-region (1- (point)) (point-at-eol)))
  8428. ts))))))
  8429. (defvar org-log-note-marker (make-marker))
  8430. (defvar org-log-note-purpose nil)
  8431. (defvar org-log-note-state nil)
  8432. (defvar org-log-note-previous-state nil)
  8433. (defvar org-log-note-how nil)
  8434. (defvar org-log-note-extra nil)
  8435. (defvar org-log-note-window-configuration nil)
  8436. (defvar org-log-note-return-to (make-marker))
  8437. (defvar org-log-post-message nil
  8438. "Message to be displayed after a log note has been stored.
  8439. The auto-repeater uses this.")
  8440. (defun org-add-note ()
  8441. "Add a note to the current entry.
  8442. This is done in the same way as adding a state change note."
  8443. (interactive)
  8444. (org-add-log-setup 'note nil nil 'findpos nil))
  8445. (defvar org-property-end-re)
  8446. (defun org-add-log-setup (&optional purpose state prev-state
  8447. findpos how &optional extra)
  8448. "Set up the post command hook to take a note.
  8449. If this is about to TODO state change, the new state is expected in STATE.
  8450. When FINDPOS is non-nil, find the correct position for the note in
  8451. the current entry. If not, assume that it can be inserted at point.
  8452. HOW is an indicator what kind of note should be created.
  8453. EXTRA is additional text that will be inserted into the notes buffer."
  8454. (let ((drawer (cond ((stringp org-log-into-drawer)
  8455. org-log-into-drawer)
  8456. (org-log-into-drawer "LOGBOOK")
  8457. (t nil))))
  8458. (save-restriction
  8459. (save-excursion
  8460. (when findpos
  8461. (org-back-to-heading t)
  8462. (narrow-to-region (point) (save-excursion
  8463. (outline-next-heading) (point)))
  8464. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8465. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8466. "[^\r\n]*\\)?"))
  8467. (goto-char (match-end 0))
  8468. (cond
  8469. (drawer
  8470. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8471. nil t)
  8472. (progn
  8473. (goto-char (match-end 0))
  8474. (or org-log-states-order-reversed
  8475. (and (re-search-forward org-property-end-re nil t)
  8476. (goto-char (1- (match-beginning 0))))))
  8477. (insert "\n:" drawer ":\n:END:")
  8478. (beginning-of-line 0)
  8479. (org-indent-line-function)
  8480. (beginning-of-line 2)
  8481. (org-indent-line-function)
  8482. (end-of-line 0)))
  8483. ((and org-log-state-notes-insert-after-drawers
  8484. (save-excursion
  8485. (forward-line) (looking-at org-drawer-regexp)))
  8486. (forward-line)
  8487. (while (looking-at org-drawer-regexp)
  8488. (goto-char (match-end 0))
  8489. (re-search-forward org-property-end-re (point-max) t)
  8490. (forward-line))
  8491. (forward-line -1)))
  8492. (unless org-log-states-order-reversed
  8493. (and (= (char-after) ?\n) (forward-char 1))
  8494. (org-skip-over-state-notes)
  8495. (skip-chars-backward " \t\n\r")))
  8496. (move-marker org-log-note-marker (point))
  8497. (setq org-log-note-purpose purpose
  8498. org-log-note-state state
  8499. org-log-note-previous-state prev-state
  8500. org-log-note-how how
  8501. org-log-note-extra extra)
  8502. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8503. (defun org-skip-over-state-notes ()
  8504. "Skip past the list of State notes in an entry."
  8505. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8506. (while (looking-at "[ \t]*- State")
  8507. (condition-case nil
  8508. (org-next-item)
  8509. (error (org-end-of-item)))))
  8510. (defun org-add-log-note (&optional purpose)
  8511. "Pop up a window for taking a note, and add this note later at point."
  8512. (remove-hook 'post-command-hook 'org-add-log-note)
  8513. (setq org-log-note-window-configuration (current-window-configuration))
  8514. (delete-other-windows)
  8515. (move-marker org-log-note-return-to (point))
  8516. (switch-to-buffer (marker-buffer org-log-note-marker))
  8517. (goto-char org-log-note-marker)
  8518. (org-switch-to-buffer-other-window "*Org Note*")
  8519. (erase-buffer)
  8520. (if (memq org-log-note-how '(time state))
  8521. (let (current-prefix-arg) (org-store-log-note))
  8522. (let ((org-inhibit-startup t)) (org-mode))
  8523. (insert (format "# Insert note for %s.
  8524. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8525. (cond
  8526. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8527. ((eq org-log-note-purpose 'done) "closed todo item")
  8528. ((eq org-log-note-purpose 'state)
  8529. (format "state change from \"%s\" to \"%s\""
  8530. (or org-log-note-previous-state "")
  8531. (or org-log-note-state "")))
  8532. ((eq org-log-note-purpose 'note)
  8533. "this entry")
  8534. (t (error "This should not happen")))))
  8535. (if org-log-note-extra (insert org-log-note-extra))
  8536. (org-set-local 'org-finish-function 'org-store-log-note)))
  8537. (defvar org-note-abort nil) ; dynamically scoped
  8538. (defun org-store-log-note ()
  8539. "Finish taking a log note, and insert it to where it belongs."
  8540. (let ((txt (buffer-string))
  8541. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8542. lines ind)
  8543. (kill-buffer (current-buffer))
  8544. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8545. (setq txt (replace-match "" t t txt)))
  8546. (if (string-match "\\s-+\\'" txt)
  8547. (setq txt (replace-match "" t t txt)))
  8548. (setq lines (org-split-string txt "\n"))
  8549. (when (and note (string-match "\\S-" note))
  8550. (setq note
  8551. (org-replace-escapes
  8552. note
  8553. (list (cons "%u" (user-login-name))
  8554. (cons "%U" user-full-name)
  8555. (cons "%t" (format-time-string
  8556. (org-time-stamp-format 'long 'inactive)
  8557. (current-time)))
  8558. (cons "%s" (if org-log-note-state
  8559. (concat "\"" org-log-note-state "\"")
  8560. ""))
  8561. (cons "%S" (if org-log-note-previous-state
  8562. (concat "\"" org-log-note-previous-state "\"")
  8563. "\"\"")))))
  8564. (if lines (setq note (concat note " \\\\")))
  8565. (push note lines))
  8566. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8567. (when lines
  8568. (save-excursion
  8569. (set-buffer (marker-buffer org-log-note-marker))
  8570. (save-excursion
  8571. (goto-char org-log-note-marker)
  8572. (move-marker org-log-note-marker nil)
  8573. (end-of-line 1)
  8574. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8575. (insert "- " (pop lines))
  8576. (org-indent-line-function)
  8577. (beginning-of-line 1)
  8578. (looking-at "[ \t]*")
  8579. (setq ind (concat (match-string 0) " "))
  8580. (end-of-line 1)
  8581. (while lines (insert "\n" ind (pop lines)))
  8582. (message "Note stored")
  8583. (org-back-to-heading t)
  8584. (org-cycle-hide-drawers 'children)))))
  8585. (set-window-configuration org-log-note-window-configuration)
  8586. (with-current-buffer (marker-buffer org-log-note-return-to)
  8587. (goto-char org-log-note-return-to))
  8588. (move-marker org-log-note-return-to nil)
  8589. (and org-log-post-message (message "%s" org-log-post-message)))
  8590. (defun org-sparse-tree (&optional arg)
  8591. "Create a sparse tree, prompt for the details.
  8592. This command can create sparse trees. You first need to select the type
  8593. of match used to create the tree:
  8594. t Show entries with a specific TODO keyword.
  8595. T Show entries selected by a tags match.
  8596. p Enter a property name and its value (both with completion on existing
  8597. names/values) and show entries with that property.
  8598. r Show entries matching a regular expression
  8599. d Show deadlines due within `org-deadline-warning-days'."
  8600. (interactive "P")
  8601. (let (ans kwd value)
  8602. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8603. (setq ans (read-char-exclusive))
  8604. (cond
  8605. ((equal ans ?d)
  8606. (call-interactively 'org-check-deadlines))
  8607. ((equal ans ?b)
  8608. (call-interactively 'org-check-before-date))
  8609. ((equal ans ?t)
  8610. (org-show-todo-tree '(4)))
  8611. ((equal ans ?T)
  8612. (call-interactively 'org-tags-sparse-tree))
  8613. ((member ans '(?p ?P))
  8614. (setq kwd (org-ido-completing-read "Property: "
  8615. (mapcar 'list (org-buffer-property-keys))))
  8616. (setq value (org-ido-completing-read "Value: "
  8617. (mapcar 'list (org-property-values kwd))))
  8618. (unless (string-match "\\`{.*}\\'" value)
  8619. (setq value (concat "\"" value "\"")))
  8620. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8621. ((member ans '(?r ?R ?/))
  8622. (call-interactively 'org-occur))
  8623. (t (error "No such sparse tree command \"%c\"" ans)))))
  8624. (defvar org-occur-highlights nil
  8625. "List of overlays used for occur matches.")
  8626. (make-variable-buffer-local 'org-occur-highlights)
  8627. (defvar org-occur-parameters nil
  8628. "Parameters of the active org-occur calls.
  8629. This is a list, each call to org-occur pushes as cons cell,
  8630. containing the regular expression and the callback, onto the list.
  8631. The list can contain several entries if `org-occur' has been called
  8632. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8633. will only contain one set of parameters. When the highlights are
  8634. removed (for example with `C-c C-c', or with the next edit (depending
  8635. on `org-remove-highlights-with-change'), this variable is emptied
  8636. as well.")
  8637. (make-variable-buffer-local 'org-occur-parameters)
  8638. (defun org-occur (regexp &optional keep-previous callback)
  8639. "Make a compact tree which shows all matches of REGEXP.
  8640. The tree will show the lines where the regexp matches, and all higher
  8641. headlines above the match. It will also show the heading after the match,
  8642. to make sure editing the matching entry is easy.
  8643. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8644. call to `org-occur' will be kept, to allow stacking of calls to this
  8645. command.
  8646. If CALLBACK is non-nil, it is a function which is called to confirm
  8647. that the match should indeed be shown."
  8648. (interactive "sRegexp: \nP")
  8649. (unless keep-previous
  8650. (org-remove-occur-highlights nil nil t))
  8651. (push (cons regexp callback) org-occur-parameters)
  8652. (let ((cnt 0))
  8653. (save-excursion
  8654. (goto-char (point-min))
  8655. (if (or (not keep-previous) ; do not want to keep
  8656. (not org-occur-highlights)) ; no previous matches
  8657. ;; hide everything
  8658. (org-overview))
  8659. (while (re-search-forward regexp nil t)
  8660. (when (or (not callback)
  8661. (save-match-data (funcall callback)))
  8662. (setq cnt (1+ cnt))
  8663. (when org-highlight-sparse-tree-matches
  8664. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8665. (org-show-context 'occur-tree))))
  8666. (when org-remove-highlights-with-change
  8667. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8668. nil 'local))
  8669. (unless org-sparse-tree-open-archived-trees
  8670. (org-hide-archived-subtrees (point-min) (point-max)))
  8671. (run-hooks 'org-occur-hook)
  8672. (if (interactive-p)
  8673. (message "%d match(es) for regexp %s" cnt regexp))
  8674. cnt))
  8675. (defun org-show-context (&optional key)
  8676. "Make sure point and context and visible.
  8677. How much context is shown depends upon the variables
  8678. `org-show-hierarchy-above', `org-show-following-heading'. and
  8679. `org-show-siblings'."
  8680. (let ((heading-p (org-on-heading-p t))
  8681. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8682. (following-p (org-get-alist-option org-show-following-heading key))
  8683. (entry-p (org-get-alist-option org-show-entry-below key))
  8684. (siblings-p (org-get-alist-option org-show-siblings key)))
  8685. (catch 'exit
  8686. ;; Show heading or entry text
  8687. (if (and heading-p (not entry-p))
  8688. (org-flag-heading nil) ; only show the heading
  8689. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8690. (org-show-hidden-entry))) ; show entire entry
  8691. (when following-p
  8692. ;; Show next sibling, or heading below text
  8693. (save-excursion
  8694. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8695. (org-flag-heading nil))))
  8696. (when siblings-p (org-show-siblings))
  8697. (when hierarchy-p
  8698. ;; show all higher headings, possibly with siblings
  8699. (save-excursion
  8700. (while (and (condition-case nil
  8701. (progn (org-up-heading-all 1) t)
  8702. (error nil))
  8703. (not (bobp)))
  8704. (org-flag-heading nil)
  8705. (when siblings-p (org-show-siblings))))))))
  8706. (defun org-reveal (&optional siblings)
  8707. "Show current entry, hierarchy above it, and the following headline.
  8708. This can be used to show a consistent set of context around locations
  8709. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8710. not t for the search context.
  8711. With optional argument SIBLINGS, on each level of the hierarchy all
  8712. siblings are shown. This repairs the tree structure to what it would
  8713. look like when opened with hierarchical calls to `org-cycle'."
  8714. (interactive "P")
  8715. (let ((org-show-hierarchy-above t)
  8716. (org-show-following-heading t)
  8717. (org-show-siblings (if siblings t org-show-siblings)))
  8718. (org-show-context nil)))
  8719. (defun org-highlight-new-match (beg end)
  8720. "Highlight from BEG to END and mark the highlight is an occur headline."
  8721. (let ((ov (org-make-overlay beg end)))
  8722. (org-overlay-put ov 'face 'secondary-selection)
  8723. (push ov org-occur-highlights)))
  8724. (defun org-remove-occur-highlights (&optional beg end noremove)
  8725. "Remove the occur highlights from the buffer.
  8726. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8727. from the `before-change-functions' in the current buffer."
  8728. (interactive)
  8729. (unless org-inhibit-highlight-removal
  8730. (mapc 'org-delete-overlay org-occur-highlights)
  8731. (setq org-occur-highlights nil)
  8732. (setq org-occur-parameters nil)
  8733. (unless noremove
  8734. (remove-hook 'before-change-functions
  8735. 'org-remove-occur-highlights 'local))))
  8736. ;;;; Priorities
  8737. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8738. "Regular expression matching the priority indicator.")
  8739. (defvar org-remove-priority-next-time nil)
  8740. (defun org-priority-up ()
  8741. "Increase the priority of the current item."
  8742. (interactive)
  8743. (org-priority 'up))
  8744. (defun org-priority-down ()
  8745. "Decrease the priority of the current item."
  8746. (interactive)
  8747. (org-priority 'down))
  8748. (defun org-priority (&optional action)
  8749. "Change the priority of an item by ARG.
  8750. ACTION can be `set', `up', `down', or a character."
  8751. (interactive)
  8752. (setq action (or action 'set))
  8753. (let (current new news have remove)
  8754. (save-excursion
  8755. (org-back-to-heading t)
  8756. (if (looking-at org-priority-regexp)
  8757. (setq current (string-to-char (match-string 2))
  8758. have t)
  8759. (setq current org-default-priority))
  8760. (cond
  8761. ((or (eq action 'set)
  8762. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8763. (if (not (eq action 'set))
  8764. (setq new action)
  8765. (message "Priority %c-%c, SPC to remove: "
  8766. org-highest-priority org-lowest-priority)
  8767. (setq new (read-char-exclusive)))
  8768. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8769. (= (upcase org-lowest-priority) org-lowest-priority))
  8770. (setq new (upcase new)))
  8771. (cond ((equal new ?\ ) (setq remove t))
  8772. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8773. (error "Priority must be between `%c' and `%c'"
  8774. org-highest-priority org-lowest-priority))))
  8775. ((eq action 'up)
  8776. (if (and (not have) (eq last-command this-command))
  8777. (setq new org-lowest-priority)
  8778. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8779. org-default-priority (1- current)))))
  8780. ((eq action 'down)
  8781. (if (and (not have) (eq last-command this-command))
  8782. (setq new org-highest-priority)
  8783. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8784. org-default-priority (1+ current)))))
  8785. (t (error "Invalid action")))
  8786. (if (or (< (upcase new) org-highest-priority)
  8787. (> (upcase new) org-lowest-priority))
  8788. (setq remove t))
  8789. (setq news (format "%c" new))
  8790. (if have
  8791. (if remove
  8792. (replace-match "" t t nil 1)
  8793. (replace-match news t t nil 2))
  8794. (if remove
  8795. (error "No priority cookie found in line")
  8796. (looking-at org-todo-line-regexp)
  8797. (if (match-end 2)
  8798. (progn
  8799. (goto-char (match-end 2))
  8800. (insert " [#" news "]"))
  8801. (goto-char (match-beginning 3))
  8802. (insert "[#" news "] ")))))
  8803. (org-preserve-lc (org-set-tags nil 'align))
  8804. (if remove
  8805. (message "Priority removed")
  8806. (message "Priority of current item set to %s" news))))
  8807. (defun org-get-priority (s)
  8808. "Find priority cookie and return priority."
  8809. (save-match-data
  8810. (if (not (string-match org-priority-regexp s))
  8811. (* 1000 (- org-lowest-priority org-default-priority))
  8812. (* 1000 (- org-lowest-priority
  8813. (string-to-char (match-string 2 s)))))))
  8814. ;;;; Tags
  8815. (defvar org-agenda-archives-mode)
  8816. (defun org-scan-tags (action matcher &optional todo-only)
  8817. "Scan headline tags with inheritance and produce output ACTION.
  8818. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8819. or `agenda' to produce an entry list for an agenda view. It can also be
  8820. a Lisp form or a function that should be called at each matched headline, in
  8821. this case the return value is a list of all return values from these calls.
  8822. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8823. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8824. only lines with a TODO keyword are included in the output."
  8825. (require 'org-agenda)
  8826. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8827. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8828. (org-re
  8829. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8830. (props (list 'face 'default
  8831. 'done-face 'org-done
  8832. 'undone-face 'default
  8833. 'mouse-face 'highlight
  8834. 'org-not-done-regexp org-not-done-regexp
  8835. 'org-todo-regexp org-todo-regexp
  8836. 'keymap org-agenda-keymap
  8837. 'help-echo
  8838. (format "mouse-2 or RET jump to org file %s"
  8839. (abbreviate-file-name
  8840. (or (buffer-file-name (buffer-base-buffer))
  8841. (buffer-name (buffer-base-buffer)))))))
  8842. (case-fold-search nil)
  8843. lspos tags tags-list
  8844. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8845. (llast 0) rtn rtn1 level category i txt
  8846. todo marker entry priority)
  8847. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8848. (setq action (list 'lambda nil action)))
  8849. (save-excursion
  8850. (goto-char (point-min))
  8851. (when (eq action 'sparse-tree)
  8852. (org-overview)
  8853. (org-remove-occur-highlights))
  8854. (while (re-search-forward re nil t)
  8855. (catch :skip
  8856. (setq todo (if (match-end 1) (match-string 2))
  8857. tags (if (match-end 4) (match-string 4)))
  8858. (goto-char (setq lspos (1+ (match-beginning 0))))
  8859. (setq level (org-reduced-level (funcall outline-level))
  8860. category (org-get-category))
  8861. (setq i llast llast level)
  8862. ;; remove tag lists from same and sublevels
  8863. (while (>= i level)
  8864. (when (setq entry (assoc i tags-alist))
  8865. (setq tags-alist (delete entry tags-alist)))
  8866. (setq i (1- i)))
  8867. ;; add the next tags
  8868. (when tags
  8869. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8870. tags-alist
  8871. (cons (cons level tags) tags-alist)))
  8872. ;; compile tags for current headline
  8873. (setq tags-list
  8874. (if org-use-tag-inheritance
  8875. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8876. tags))
  8877. (when org-use-tag-inheritance
  8878. (setcdr (car tags-alist)
  8879. (mapcar (lambda (x)
  8880. (setq x (copy-sequence x))
  8881. (org-add-prop-inherited x))
  8882. (cdar tags-alist))))
  8883. (when (and tags org-use-tag-inheritance
  8884. (not (eq t org-use-tag-inheritance)))
  8885. ;; selective inheritance, remove uninherited ones
  8886. (setcdr (car tags-alist)
  8887. (org-remove-uniherited-tags (cdar tags-alist))))
  8888. (when (and (or (not todo-only)
  8889. (and (member todo org-not-done-keywords)
  8890. (or (not org-agenda-tags-todo-honor-ignore-options)
  8891. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8892. (let ((case-fold-search t)) (eval matcher))
  8893. (or
  8894. (not (member org-archive-tag tags-list))
  8895. ;; we have an archive tag, should we use this anyway?
  8896. (or (not org-agenda-skip-archived-trees)
  8897. (and (eq action 'agenda) org-agenda-archives-mode))))
  8898. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8899. ;; select this headline
  8900. (cond
  8901. ((eq action 'sparse-tree)
  8902. (and org-highlight-sparse-tree-matches
  8903. (org-get-heading) (match-end 0)
  8904. (org-highlight-new-match
  8905. (match-beginning 0) (match-beginning 1)))
  8906. (org-show-context 'tags-tree))
  8907. ((eq action 'agenda)
  8908. (setq txt (org-format-agenda-item
  8909. ""
  8910. (concat
  8911. (if org-tags-match-list-sublevels
  8912. (make-string (1- level) ?.) "")
  8913. (org-get-heading))
  8914. category (org-get-tags-at))
  8915. priority (org-get-priority txt))
  8916. (goto-char lspos)
  8917. (setq marker (org-agenda-new-marker))
  8918. (org-add-props txt props
  8919. 'org-marker marker 'org-hd-marker marker 'org-category category
  8920. 'todo-state todo
  8921. 'priority priority 'type "tagsmatch")
  8922. (push txt rtn))
  8923. ((functionp action)
  8924. (save-excursion
  8925. (setq rtn1 (funcall action))
  8926. (push rtn1 rtn))
  8927. (goto-char (point-at-eol)))
  8928. (t (error "Invalid action")))
  8929. ;; if we are to skip sublevels, jump to end of subtree
  8930. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8931. (when (and (eq action 'sparse-tree)
  8932. (not org-sparse-tree-open-archived-trees))
  8933. (org-hide-archived-subtrees (point-min) (point-max)))
  8934. (nreverse rtn)))
  8935. (defun org-remove-uniherited-tags (tags)
  8936. "Remove all tags that are not inherited from the list TAGS."
  8937. (cond
  8938. ((eq org-use-tag-inheritance t)
  8939. (if org-tags-exclude-from-inheritance
  8940. (org-delete-all org-tags-exclude-from-inheritance tags)
  8941. tags))
  8942. ((not org-use-tag-inheritance) nil)
  8943. ((stringp org-use-tag-inheritance)
  8944. (delq nil (mapcar
  8945. (lambda (x)
  8946. (if (and (string-match org-use-tag-inheritance x)
  8947. (not (member x org-tags-exclude-from-inheritance)))
  8948. x nil))
  8949. tags)))
  8950. ((listp org-use-tag-inheritance)
  8951. (delq nil (mapcar
  8952. (lambda (x)
  8953. (if (member x org-use-tag-inheritance) x nil))
  8954. tags)))))
  8955. (defvar todo-only) ;; dynamically scoped
  8956. (defun org-tags-sparse-tree (&optional todo-only match)
  8957. "Create a sparse tree according to tags string MATCH.
  8958. MATCH can contain positive and negative selection of tags, like
  8959. \"+WORK+URGENT-WITHBOSS\".
  8960. If optional argument TODO-ONLY is non-nil, only select lines that are
  8961. also TODO lines."
  8962. (interactive "P")
  8963. (org-prepare-agenda-buffers (list (current-buffer)))
  8964. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8965. (defvar org-cached-props nil)
  8966. (defun org-cached-entry-get (pom property)
  8967. (if (or (eq t org-use-property-inheritance)
  8968. (and (stringp org-use-property-inheritance)
  8969. (string-match org-use-property-inheritance property))
  8970. (and (listp org-use-property-inheritance)
  8971. (member property org-use-property-inheritance)))
  8972. ;; Caching is not possible, check it directly
  8973. (org-entry-get pom property 'inherit)
  8974. ;; Get all properties, so that we can do complicated checks easily
  8975. (cdr (assoc property (or org-cached-props
  8976. (setq org-cached-props
  8977. (org-entry-properties pom)))))))
  8978. (defun org-global-tags-completion-table (&optional files)
  8979. "Return the list of all tags in all agenda buffer/files."
  8980. (save-excursion
  8981. (org-uniquify
  8982. (delq nil
  8983. (apply 'append
  8984. (mapcar
  8985. (lambda (file)
  8986. (set-buffer (find-file-noselect file))
  8987. (append (org-get-buffer-tags)
  8988. (mapcar (lambda (x) (if (stringp (car-safe x))
  8989. (list (car-safe x)) nil))
  8990. org-tag-alist)))
  8991. (if (and files (car files))
  8992. files
  8993. (org-agenda-files))))))))
  8994. (defun org-make-tags-matcher (match)
  8995. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8996. ;; todo-only is scoped dynamically into this function, and the function
  8997. ;; may change it if the matcher asks for it.
  8998. (unless match
  8999. ;; Get a new match request, with completion
  9000. (let ((org-last-tags-completion-table
  9001. (org-global-tags-completion-table)))
  9002. (setq match (org-completing-read-no-ido
  9003. "Match: " 'org-tags-completion-function nil nil nil
  9004. 'org-tags-history))))
  9005. ;; Parse the string and create a lisp form
  9006. (let ((match0 match)
  9007. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9008. minus tag mm
  9009. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9010. orterms term orlist re-p str-p level-p level-op time-p
  9011. prop-p pn pv po cat-p gv rest)
  9012. (if (string-match "/+" match)
  9013. ;; match contains also a todo-matching request
  9014. (progn
  9015. (setq tagsmatch (substring match 0 (match-beginning 0))
  9016. todomatch (substring match (match-end 0)))
  9017. (if (string-match "^!" todomatch)
  9018. (setq todo-only t todomatch (substring todomatch 1)))
  9019. (if (string-match "^\\s-*$" todomatch)
  9020. (setq todomatch nil)))
  9021. ;; only matching tags
  9022. (setq tagsmatch match todomatch nil))
  9023. ;; Make the tags matcher
  9024. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9025. (setq tagsmatcher t)
  9026. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9027. (while (setq term (pop orterms))
  9028. (while (and (equal (substring term -1) "\\") orterms)
  9029. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9030. (while (string-match re term)
  9031. (setq rest (substring term (match-end 0))
  9032. minus (and (match-end 1)
  9033. (equal (match-string 1 term) "-"))
  9034. tag (match-string 2 term)
  9035. re-p (equal (string-to-char tag) ?{)
  9036. level-p (match-end 4)
  9037. prop-p (match-end 5)
  9038. mm (cond
  9039. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9040. (level-p
  9041. (setq level-op (org-op-to-function (match-string 3 term)))
  9042. `(,level-op level ,(string-to-number
  9043. (match-string 4 term))))
  9044. (prop-p
  9045. (setq pn (match-string 5 term)
  9046. po (match-string 6 term)
  9047. pv (match-string 7 term)
  9048. cat-p (equal pn "CATEGORY")
  9049. re-p (equal (string-to-char pv) ?{)
  9050. str-p (equal (string-to-char pv) ?\")
  9051. time-p (save-match-data
  9052. (string-match "^\"[[<].*[]>]\"$" pv))
  9053. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9054. (if time-p (setq pv (org-matcher-time pv)))
  9055. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9056. (cond
  9057. ((equal pn "CATEGORY")
  9058. (setq gv '(get-text-property (point) 'org-category)))
  9059. ((equal pn "TODO")
  9060. (setq gv 'todo))
  9061. (t
  9062. (setq gv `(org-cached-entry-get nil ,pn))))
  9063. (if re-p
  9064. (if (eq po 'org<>)
  9065. `(not (string-match ,pv (or ,gv "")))
  9066. `(string-match ,pv (or ,gv "")))
  9067. (if str-p
  9068. `(,po (or ,gv "") ,pv)
  9069. `(,po (string-to-number (or ,gv ""))
  9070. ,(string-to-number pv) ))))
  9071. (t `(member ,(downcase tag) tags-list)))
  9072. mm (if minus (list 'not mm) mm)
  9073. term rest)
  9074. (push mm tagsmatcher))
  9075. (push (if (> (length tagsmatcher) 1)
  9076. (cons 'and tagsmatcher)
  9077. (car tagsmatcher))
  9078. orlist)
  9079. (setq tagsmatcher nil))
  9080. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9081. (setq tagsmatcher
  9082. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9083. ;; Make the todo matcher
  9084. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9085. (setq todomatcher t)
  9086. (setq orterms (org-split-string todomatch "|") orlist nil)
  9087. (while (setq term (pop orterms))
  9088. (while (string-match re term)
  9089. (setq minus (and (match-end 1)
  9090. (equal (match-string 1 term) "-"))
  9091. kwd (match-string 2 term)
  9092. re-p (equal (string-to-char kwd) ?{)
  9093. term (substring term (match-end 0))
  9094. mm (if re-p
  9095. `(string-match ,(substring kwd 1 -1) todo)
  9096. (list 'equal 'todo kwd))
  9097. mm (if minus (list 'not mm) mm))
  9098. (push mm todomatcher))
  9099. (push (if (> (length todomatcher) 1)
  9100. (cons 'and todomatcher)
  9101. (car todomatcher))
  9102. orlist)
  9103. (setq todomatcher nil))
  9104. (setq todomatcher (if (> (length orlist) 1)
  9105. (cons 'or orlist) (car orlist))))
  9106. ;; Return the string and lisp forms of the matcher
  9107. (setq matcher (if todomatcher
  9108. (list 'and tagsmatcher todomatcher)
  9109. tagsmatcher))
  9110. (cons match0 matcher)))
  9111. (defun org-op-to-function (op &optional stringp)
  9112. "Turn an operator into the appropriate function."
  9113. (setq op
  9114. (cond
  9115. ((equal op "<" ) '(< string< org-time<))
  9116. ((equal op ">" ) '(> org-string> org-time>))
  9117. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9118. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9119. ((member op '("=" "==")) '(= string= org-time=))
  9120. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9121. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9122. (defun org<> (a b) (not (= a b)))
  9123. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9124. (defun org-string>= (a b) (not (string< a b)))
  9125. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9126. (defun org-string<> (a b) (not (string= a b)))
  9127. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9128. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9129. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9130. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9131. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9132. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9133. (defun org-2ft (s)
  9134. "Convert S to a floating point time.
  9135. If S is already a number, just return it. If it is a string, parse
  9136. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9137. (cond
  9138. ((numberp s) s)
  9139. ((stringp s)
  9140. (condition-case nil
  9141. (float-time (apply 'encode-time (org-parse-time-string s)))
  9142. (error 0.)))
  9143. (t 0.)))
  9144. (defun org-time-today ()
  9145. "Time in seconds today at 0:00.
  9146. Returns the float number of seconds since the beginning of the
  9147. epoch to the beginning of today (00:00)."
  9148. (float-time (apply 'encode-time
  9149. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9150. (defun org-matcher-time (s)
  9151. "Interpret a time comparison value."
  9152. (save-match-data
  9153. (cond
  9154. ((string= s "<now>") (float-time))
  9155. ((string= s "<today>") (org-time-today))
  9156. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9157. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9158. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9159. (+ (org-time-today)
  9160. (* (string-to-number (match-string 1 s))
  9161. (cdr (assoc (match-string 2 s)
  9162. '(("d" . 86400.0) ("w" . 604800.0)
  9163. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9164. (t (org-2ft s)))))
  9165. (defun org-match-any-p (re list)
  9166. "Does re match any element of list?"
  9167. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9168. (delq nil list))
  9169. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9170. (defvar org-tags-overlay (org-make-overlay 1 1))
  9171. (org-detach-overlay org-tags-overlay)
  9172. (defun org-get-local-tags-at (&optional pos)
  9173. "Get a list of tags defined in the current headline."
  9174. (org-get-tags-at pos 'local))
  9175. (defun org-get-local-tags ()
  9176. "Get a list of tags defined in the current headline."
  9177. (org-get-tags-at nil 'local))
  9178. (defun org-get-tags-at (&optional pos local)
  9179. "Get a list of all headline tags applicable at POS.
  9180. POS defaults to point. If tags are inherited, the list contains
  9181. the targets in the same sequence as the headlines appear, i.e.
  9182. the tags of the current headline come last.
  9183. When LOCAL is non-nil, only return tags from the current headline,
  9184. ignore inherited ones."
  9185. (interactive)
  9186. (let (tags ltags lastpos parent)
  9187. (save-excursion
  9188. (save-restriction
  9189. (widen)
  9190. (goto-char (or pos (point)))
  9191. (save-match-data
  9192. (catch 'done
  9193. (condition-case nil
  9194. (progn
  9195. (org-back-to-heading t)
  9196. (while (not (equal lastpos (point)))
  9197. (setq lastpos (point))
  9198. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9199. (setq ltags (org-split-string
  9200. (org-match-string-no-properties 1) ":"))
  9201. (when parent
  9202. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9203. (setq tags (append
  9204. (if parent
  9205. (org-remove-uniherited-tags ltags)
  9206. ltags)
  9207. tags)))
  9208. (or org-use-tag-inheritance (throw 'done t))
  9209. (if local (throw 'done t))
  9210. (org-up-heading-all 1)
  9211. (setq parent t)))
  9212. (error nil)))))
  9213. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9214. (defun org-add-prop-inherited (s)
  9215. (add-text-properties 0 (length s) '(inherited t) s)
  9216. s)
  9217. (defun org-toggle-tag (tag &optional onoff)
  9218. "Toggle the tag TAG for the current line.
  9219. If ONOFF is `on' or `off', don't toggle but set to this state."
  9220. (let (res current)
  9221. (save-excursion
  9222. (org-back-to-heading t)
  9223. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9224. (point-at-eol) t)
  9225. (progn
  9226. (setq current (match-string 1))
  9227. (replace-match ""))
  9228. (setq current ""))
  9229. (setq current (nreverse (org-split-string current ":")))
  9230. (cond
  9231. ((eq onoff 'on)
  9232. (setq res t)
  9233. (or (member tag current) (push tag current)))
  9234. ((eq onoff 'off)
  9235. (or (not (member tag current)) (setq current (delete tag current))))
  9236. (t (if (member tag current)
  9237. (setq current (delete tag current))
  9238. (setq res t)
  9239. (push tag current))))
  9240. (end-of-line 1)
  9241. (if current
  9242. (progn
  9243. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9244. (org-set-tags nil t))
  9245. (delete-horizontal-space))
  9246. (run-hooks 'org-after-tags-change-hook))
  9247. res))
  9248. (defun org-align-tags-here (to-col)
  9249. ;; Assumes that this is a headline
  9250. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9251. (beginning-of-line 1)
  9252. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9253. (< pos (match-beginning 2)))
  9254. (progn
  9255. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9256. (goto-char (match-beginning 1))
  9257. (insert " ")
  9258. (delete-region (point) (1+ (match-beginning 2)))
  9259. (setq ncol (max (1+ (current-column))
  9260. (1+ col)
  9261. (if (> to-col 0)
  9262. to-col
  9263. (- (abs to-col) tags-l))))
  9264. (setq p (point))
  9265. (insert (make-string (- ncol (current-column)) ?\ ))
  9266. (setq ncol (current-column))
  9267. (when indent-tabs-mode (tabify p (point-at-eol)))
  9268. (org-move-to-column (min ncol col) t))
  9269. (goto-char pos))))
  9270. (defun org-set-tags-command (&optional arg just-align)
  9271. "Call the set-tags command for the current entry."
  9272. (interactive "P")
  9273. (if (org-on-heading-p)
  9274. (org-set-tags arg just-align)
  9275. (save-excursion
  9276. (org-back-to-heading t)
  9277. (org-set-tags arg just-align))))
  9278. (defun org-set-tags (&optional arg just-align)
  9279. "Set the tags for the current headline.
  9280. With prefix ARG, realign all tags in headings in the current buffer."
  9281. (interactive "P")
  9282. (let* ((re (concat "^" outline-regexp))
  9283. (current (org-get-tags-string))
  9284. (col (current-column))
  9285. (org-setting-tags t)
  9286. table current-tags inherited-tags ; computed below when needed
  9287. tags p0 c0 c1 rpl)
  9288. (if arg
  9289. (save-excursion
  9290. (goto-char (point-min))
  9291. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9292. (while (re-search-forward re nil t)
  9293. (org-set-tags nil t)
  9294. (end-of-line 1)))
  9295. (message "All tags realigned to column %d" org-tags-column))
  9296. (if just-align
  9297. (setq tags current)
  9298. ;; Get a new set of tags from the user
  9299. (save-excursion
  9300. (setq table (or org-tag-alist (org-get-buffer-tags))
  9301. org-last-tags-completion-table table
  9302. current-tags (org-split-string current ":")
  9303. inherited-tags (nreverse
  9304. (nthcdr (length current-tags)
  9305. (nreverse (org-get-tags-at))))
  9306. tags
  9307. (if (or (eq t org-use-fast-tag-selection)
  9308. (and org-use-fast-tag-selection
  9309. (delq nil (mapcar 'cdr table))))
  9310. (org-fast-tag-selection
  9311. current-tags inherited-tags table
  9312. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9313. (let ((org-add-colon-after-tag-completion t))
  9314. (org-trim
  9315. (org-without-partial-completion
  9316. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9317. nil nil current 'org-tags-history)))))))
  9318. (while (string-match "[-+&]+" tags)
  9319. ;; No boolean logic, just a list
  9320. (setq tags (replace-match ":" t t tags))))
  9321. (if (string-match "\\`[\t ]*\\'" tags)
  9322. (setq tags "")
  9323. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9324. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9325. ;; Insert new tags at the correct column
  9326. (beginning-of-line 1)
  9327. (cond
  9328. ((and (equal current "") (equal tags "")))
  9329. ((re-search-forward
  9330. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9331. (point-at-eol) t)
  9332. (if (equal tags "")
  9333. (setq rpl "")
  9334. (goto-char (match-beginning 0))
  9335. (setq c0 (current-column) p0 (point)
  9336. c1 (max (1+ c0) (if (> org-tags-column 0)
  9337. org-tags-column
  9338. (- (- org-tags-column) (length tags))))
  9339. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9340. (replace-match rpl t t)
  9341. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9342. tags)
  9343. (t (error "Tags alignment failed")))
  9344. (org-move-to-column col)
  9345. (unless just-align
  9346. (run-hooks 'org-after-tags-change-hook)))))
  9347. (defun org-change-tag-in-region (beg end tag off)
  9348. "Add or remove TAG for each entry in the region.
  9349. This works in the agenda, and also in an org-mode buffer."
  9350. (interactive
  9351. (list (region-beginning) (region-end)
  9352. (let ((org-last-tags-completion-table
  9353. (if (org-mode-p)
  9354. (org-get-buffer-tags)
  9355. (org-global-tags-completion-table))))
  9356. (org-ido-completing-read
  9357. "Tag: " 'org-tags-completion-function nil nil nil
  9358. 'org-tags-history))
  9359. (progn
  9360. (message "[s]et or [r]emove? ")
  9361. (equal (read-char-exclusive) ?r))))
  9362. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9363. (let ((agendap (equal major-mode 'org-agenda-mode))
  9364. l1 l2 m buf pos newhead (cnt 0))
  9365. (goto-char end)
  9366. (setq l2 (1- (org-current-line)))
  9367. (goto-char beg)
  9368. (setq l1 (org-current-line))
  9369. (loop for l from l1 to l2 do
  9370. (goto-line l)
  9371. (setq m (get-text-property (point) 'org-hd-marker))
  9372. (when (or (and (org-mode-p) (org-on-heading-p))
  9373. (and agendap m))
  9374. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9375. pos (if agendap m (point)))
  9376. (with-current-buffer buf
  9377. (save-excursion
  9378. (save-restriction
  9379. (goto-char pos)
  9380. (setq cnt (1+ cnt))
  9381. (org-toggle-tag tag (if off 'off 'on))
  9382. (setq newhead (org-get-heading)))))
  9383. (and agendap (org-agenda-change-all-lines newhead m))))
  9384. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9385. (defun org-tags-completion-function (string predicate &optional flag)
  9386. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9387. (confirm (lambda (x) (stringp (car x)))))
  9388. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9389. (setq s1 (match-string 1 string)
  9390. s2 (match-string 2 string))
  9391. (setq s1 "" s2 string))
  9392. (cond
  9393. ((eq flag nil)
  9394. ;; try completion
  9395. (setq rtn (try-completion s2 ctable confirm))
  9396. (if (stringp rtn)
  9397. (setq rtn
  9398. (concat s1 s2 (substring rtn (length s2))
  9399. (if (and org-add-colon-after-tag-completion
  9400. (assoc rtn ctable))
  9401. ":" ""))))
  9402. rtn)
  9403. ((eq flag t)
  9404. ;; all-completions
  9405. (all-completions s2 ctable confirm)
  9406. )
  9407. ((eq flag 'lambda)
  9408. ;; exact match?
  9409. (assoc s2 ctable)))
  9410. ))
  9411. (defun org-fast-tag-insert (kwd tags face &optional end)
  9412. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9413. (insert (format "%-12s" (concat kwd ":"))
  9414. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9415. (or end "")))
  9416. (defun org-fast-tag-show-exit (flag)
  9417. (save-excursion
  9418. (goto-line 3)
  9419. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9420. (replace-match ""))
  9421. (when flag
  9422. (end-of-line 1)
  9423. (org-move-to-column (- (window-width) 19) t)
  9424. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9425. (defun org-set-current-tags-overlay (current prefix)
  9426. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9427. (if (featurep 'xemacs)
  9428. (org-overlay-display org-tags-overlay (concat prefix s)
  9429. 'secondary-selection)
  9430. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9431. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9432. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9433. "Fast tag selection with single keys.
  9434. CURRENT is the current list of tags in the headline, INHERITED is the
  9435. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9436. possibly with grouping information. TODO-TABLE is a similar table with
  9437. TODO keywords, should these have keys assigned to them.
  9438. If the keys are nil, a-z are automatically assigned.
  9439. Returns the new tags string, or nil to not change the current settings."
  9440. (let* ((fulltable (append table todo-table))
  9441. (maxlen (apply 'max (mapcar
  9442. (lambda (x)
  9443. (if (stringp (car x)) (string-width (car x)) 0))
  9444. fulltable)))
  9445. (buf (current-buffer))
  9446. (expert (eq org-fast-tag-selection-single-key 'expert))
  9447. (buffer-tags nil)
  9448. (fwidth (+ maxlen 3 1 3))
  9449. (ncol (/ (- (window-width) 4) fwidth))
  9450. (i-face 'org-done)
  9451. (c-face 'org-todo)
  9452. tg cnt e c char c1 c2 ntable tbl rtn
  9453. ov-start ov-end ov-prefix
  9454. (exit-after-next org-fast-tag-selection-single-key)
  9455. (done-keywords org-done-keywords)
  9456. groups ingroup)
  9457. (save-excursion
  9458. (beginning-of-line 1)
  9459. (if (looking-at
  9460. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9461. (setq ov-start (match-beginning 1)
  9462. ov-end (match-end 1)
  9463. ov-prefix "")
  9464. (setq ov-start (1- (point-at-eol))
  9465. ov-end (1+ ov-start))
  9466. (skip-chars-forward "^\n\r")
  9467. (setq ov-prefix
  9468. (concat
  9469. (buffer-substring (1- (point)) (point))
  9470. (if (> (current-column) org-tags-column)
  9471. " "
  9472. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9473. (org-move-overlay org-tags-overlay ov-start ov-end)
  9474. (save-window-excursion
  9475. (if expert
  9476. (set-buffer (get-buffer-create " *Org tags*"))
  9477. (delete-other-windows)
  9478. (split-window-vertically)
  9479. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9480. (erase-buffer)
  9481. (org-set-local 'org-done-keywords done-keywords)
  9482. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9483. (org-fast-tag-insert "Current" current c-face "\n\n")
  9484. (org-fast-tag-show-exit exit-after-next)
  9485. (org-set-current-tags-overlay current ov-prefix)
  9486. (setq tbl fulltable char ?a cnt 0)
  9487. (while (setq e (pop tbl))
  9488. (cond
  9489. ((equal e '(:startgroup))
  9490. (push '() groups) (setq ingroup t)
  9491. (when (not (= cnt 0))
  9492. (setq cnt 0)
  9493. (insert "\n"))
  9494. (insert "{ "))
  9495. ((equal e '(:endgroup))
  9496. (setq ingroup nil cnt 0)
  9497. (insert "}\n"))
  9498. ((equal e '(:newline))
  9499. (insert "\n "))
  9500. (t
  9501. (setq tg (car e) c2 nil)
  9502. (if (cdr e)
  9503. (setq c (cdr e))
  9504. ;; automatically assign a character.
  9505. (setq c1 (string-to-char
  9506. (downcase (substring
  9507. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9508. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9509. (while (or (rassoc char ntable) (rassoc char table))
  9510. (setq char (1+ char)))
  9511. (setq c2 c1))
  9512. (setq c (or c2 char)))
  9513. (if ingroup (push tg (car groups)))
  9514. (setq tg (org-add-props tg nil 'face
  9515. (cond
  9516. ((not (assoc tg table))
  9517. (org-get-todo-face tg))
  9518. ((member tg current) c-face)
  9519. ((member tg inherited) i-face)
  9520. (t nil))))
  9521. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9522. (insert "[" c "] " tg (make-string
  9523. (- fwidth 4 (length tg)) ?\ ))
  9524. (push (cons tg c) ntable)
  9525. (when (= (setq cnt (1+ cnt)) ncol)
  9526. (insert "\n")
  9527. (if ingroup (insert " "))
  9528. (setq cnt 0)))))
  9529. (setq ntable (nreverse ntable))
  9530. (insert "\n")
  9531. (goto-char (point-min))
  9532. (if (not expert) (org-fit-window-to-buffer))
  9533. (setq rtn
  9534. (catch 'exit
  9535. (while t
  9536. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9537. (if groups " [!] no groups" " [!]groups")
  9538. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9539. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9540. (cond
  9541. ((= c ?\r) (throw 'exit t))
  9542. ((= c ?!)
  9543. (setq groups (not groups))
  9544. (goto-char (point-min))
  9545. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9546. ((= c ?\C-c)
  9547. (if (not expert)
  9548. (org-fast-tag-show-exit
  9549. (setq exit-after-next (not exit-after-next)))
  9550. (setq expert nil)
  9551. (delete-other-windows)
  9552. (split-window-vertically)
  9553. (org-switch-to-buffer-other-window " *Org tags*")
  9554. (org-fit-window-to-buffer)))
  9555. ((or (= c ?\C-g)
  9556. (and (= c ?q) (not (rassoc c ntable))))
  9557. (org-detach-overlay org-tags-overlay)
  9558. (setq quit-flag t))
  9559. ((= c ?\ )
  9560. (setq current nil)
  9561. (if exit-after-next (setq exit-after-next 'now)))
  9562. ((= c ?\t)
  9563. (condition-case nil
  9564. (setq tg (org-ido-completing-read
  9565. "Tag: "
  9566. (or buffer-tags
  9567. (with-current-buffer buf
  9568. (org-get-buffer-tags)))))
  9569. (quit (setq tg "")))
  9570. (when (string-match "\\S-" tg)
  9571. (add-to-list 'buffer-tags (list tg))
  9572. (if (member tg current)
  9573. (setq current (delete tg current))
  9574. (push tg current)))
  9575. (if exit-after-next (setq exit-after-next 'now)))
  9576. ((setq e (rassoc c todo-table) tg (car e))
  9577. (with-current-buffer buf
  9578. (save-excursion (org-todo tg)))
  9579. (if exit-after-next (setq exit-after-next 'now)))
  9580. ((setq e (rassoc c ntable) tg (car e))
  9581. (if (member tg current)
  9582. (setq current (delete tg current))
  9583. (loop for g in groups do
  9584. (if (member tg g)
  9585. (mapc (lambda (x)
  9586. (setq current (delete x current)))
  9587. g)))
  9588. (push tg current))
  9589. (if exit-after-next (setq exit-after-next 'now))))
  9590. ;; Create a sorted list
  9591. (setq current
  9592. (sort current
  9593. (lambda (a b)
  9594. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9595. (if (eq exit-after-next 'now) (throw 'exit t))
  9596. (goto-char (point-min))
  9597. (beginning-of-line 2)
  9598. (delete-region (point) (point-at-eol))
  9599. (org-fast-tag-insert "Current" current c-face)
  9600. (org-set-current-tags-overlay current ov-prefix)
  9601. (while (re-search-forward
  9602. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9603. (setq tg (match-string 1))
  9604. (add-text-properties
  9605. (match-beginning 1) (match-end 1)
  9606. (list 'face
  9607. (cond
  9608. ((member tg current) c-face)
  9609. ((member tg inherited) i-face)
  9610. (t (get-text-property (match-beginning 1) 'face))))))
  9611. (goto-char (point-min)))))
  9612. (org-detach-overlay org-tags-overlay)
  9613. (if rtn
  9614. (mapconcat 'identity current ":")
  9615. nil))))
  9616. (defun org-get-tags-string ()
  9617. "Get the TAGS string in the current headline."
  9618. (unless (org-on-heading-p t)
  9619. (error "Not on a heading"))
  9620. (save-excursion
  9621. (beginning-of-line 1)
  9622. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9623. (org-match-string-no-properties 1)
  9624. "")))
  9625. (defun org-get-tags ()
  9626. "Get the list of tags specified in the current headline."
  9627. (org-split-string (org-get-tags-string) ":"))
  9628. (defun org-get-buffer-tags ()
  9629. "Get a table of all tags used in the buffer, for completion."
  9630. (let (tags)
  9631. (save-excursion
  9632. (goto-char (point-min))
  9633. (while (re-search-forward
  9634. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9635. (when (equal (char-after (point-at-bol 0)) ?*)
  9636. (mapc (lambda (x) (add-to-list 'tags x))
  9637. (org-split-string (org-match-string-no-properties 1) ":")))))
  9638. (mapcar 'list tags)))
  9639. ;;;; The mapping API
  9640. ;;;###autoload
  9641. (defun org-map-entries (func &optional match scope &rest skip)
  9642. "Call FUNC at each headline selected by MATCH in SCOPE.
  9643. FUNC is a function or a lisp form. The function will be called without
  9644. arguments, with the cursor positioned at the beginning of the headline.
  9645. The return values of all calls to the function will be collected and
  9646. returned as a list.
  9647. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9648. Only headlines that are matched by this query will be considered during
  9649. the iteration. When MATCH is nil or t, all headlines will be
  9650. visited by the iteration.
  9651. SCOPE determines the scope of this command. It can be any of:
  9652. nil The current buffer, respecting the restriction if any
  9653. tree The subtree started with the entry at point
  9654. file The current buffer, without restriction
  9655. file-with-archives
  9656. The current buffer, and any archives associated with it
  9657. agenda All agenda files
  9658. agenda-with-archives
  9659. All agenda files with any archive files associated with them
  9660. \(file1 file2 ...)
  9661. If this is a list, all files in the list will be scanned
  9662. The remaining args are treated as settings for the skipping facilities of
  9663. the scanner. The following items can be given here:
  9664. archive skip trees with the archive tag.
  9665. comment skip trees with the COMMENT keyword
  9666. function or Emacs Lisp form:
  9667. will be used as value for `org-agenda-skip-function', so whenever
  9668. the the function returns t, FUNC will not be called for that
  9669. entry and search will continue from the point where the
  9670. function leaves it."
  9671. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9672. (org-agenda-skip-archived-trees (memq 'archive skip))
  9673. (org-agenda-skip-comment-trees (memq 'comment skip))
  9674. (org-agenda-skip-function
  9675. (car (org-delete-all '(comment archive) skip)))
  9676. (org-tags-match-list-sublevels t)
  9677. matcher file res
  9678. org-todo-keywords-for-agenda
  9679. org-done-keywords-for-agenda
  9680. org-todo-keyword-alist-for-agenda
  9681. org-tag-alist-for-agenda)
  9682. (cond
  9683. ((eq match t) (setq matcher t))
  9684. ((eq match nil) (setq matcher t))
  9685. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9686. (save-excursion
  9687. (save-restriction
  9688. (when (eq scope 'tree)
  9689. (org-back-to-heading t)
  9690. (org-narrow-to-subtree)
  9691. (setq scope nil))
  9692. (if (not scope)
  9693. (progn
  9694. (org-prepare-agenda-buffers
  9695. (list (buffer-file-name (current-buffer))))
  9696. (setq res (org-scan-tags func matcher)))
  9697. ;; Get the right scope
  9698. (cond
  9699. ((and scope (listp scope) (symbolp (car scope)))
  9700. (setq scope (eval scope)))
  9701. ((eq scope 'agenda)
  9702. (setq scope (org-agenda-files t)))
  9703. ((eq scope 'agenda-with-archives)
  9704. (setq scope (org-agenda-files t))
  9705. (setq scope (org-add-archive-files scope)))
  9706. ((eq scope 'file)
  9707. (setq scope (list (buffer-file-name))))
  9708. ((eq scope 'file-with-archives)
  9709. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9710. (org-prepare-agenda-buffers scope)
  9711. (while (setq file (pop scope))
  9712. (with-current-buffer (org-find-base-buffer-visiting file)
  9713. (save-excursion
  9714. (save-restriction
  9715. (widen)
  9716. (goto-char (point-min))
  9717. (setq res (append res (org-scan-tags func matcher))))))))))
  9718. res))
  9719. ;;;; Properties
  9720. ;;; Setting and retrieving properties
  9721. (defconst org-special-properties
  9722. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9723. "TIMESTAMP" "TIMESTAMP_IA")
  9724. "The special properties valid in Org-mode.
  9725. These are properties that are not defined in the property drawer,
  9726. but in some other way.")
  9727. (defconst org-default-properties
  9728. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9729. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9730. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9731. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9732. "Some properties that are used by Org-mode for various purposes.
  9733. Being in this list makes sure that they are offered for completion.")
  9734. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9735. "Regular expression matching the first line of a property drawer.")
  9736. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9737. "Regular expression matching the first line of a property drawer.")
  9738. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9739. "Regular expression matching the first line of a property drawer.")
  9740. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9741. "Regular expression matching the first line of a property drawer.")
  9742. (defconst org-property-drawer-re
  9743. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9744. org-property-end-re "\\)\n?")
  9745. "Matches an entire property drawer.")
  9746. (defconst org-clock-drawer-re
  9747. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9748. org-property-end-re "\\)\n?")
  9749. "Matches an entire clock drawer.")
  9750. (defun org-property-action ()
  9751. "Do an action on properties."
  9752. (interactive)
  9753. (let (c)
  9754. (org-at-property-p)
  9755. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9756. (setq c (read-char-exclusive))
  9757. (cond
  9758. ((equal c ?s)
  9759. (call-interactively 'org-set-property))
  9760. ((equal c ?d)
  9761. (call-interactively 'org-delete-property))
  9762. ((equal c ?D)
  9763. (call-interactively 'org-delete-property-globally))
  9764. ((equal c ?c)
  9765. (call-interactively 'org-compute-property-at-point))
  9766. (t (error "No such property action %c" c)))))
  9767. (defun org-at-property-p ()
  9768. "Is the cursor in a property line?"
  9769. ;; FIXME: Does not check if we are actually in the drawer.
  9770. ;; FIXME: also returns true on any drawers.....
  9771. ;; This is used by C-c C-c for property action.
  9772. (save-excursion
  9773. (beginning-of-line 1)
  9774. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9775. (defun org-get-property-block (&optional beg end force)
  9776. "Return the (beg . end) range of the body of the property drawer.
  9777. BEG and END can be beginning and end of subtree, if not given
  9778. they will be found.
  9779. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9780. (catch 'exit
  9781. (save-excursion
  9782. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9783. (end (or end (progn (outline-next-heading) (point)))))
  9784. (goto-char beg)
  9785. (if (re-search-forward org-property-start-re end t)
  9786. (setq beg (1+ (match-end 0)))
  9787. (if force
  9788. (save-excursion
  9789. (org-insert-property-drawer)
  9790. (setq end (progn (outline-next-heading) (point))))
  9791. (throw 'exit nil))
  9792. (goto-char beg)
  9793. (if (re-search-forward org-property-start-re end t)
  9794. (setq beg (1+ (match-end 0)))))
  9795. (if (re-search-forward org-property-end-re end t)
  9796. (setq end (match-beginning 0))
  9797. (or force (throw 'exit nil))
  9798. (goto-char beg)
  9799. (setq end beg)
  9800. (org-indent-line-function)
  9801. (insert ":END:\n"))
  9802. (cons beg end)))))
  9803. (defun org-entry-properties (&optional pom which)
  9804. "Get all properties of the entry at point-or-marker POM.
  9805. This includes the TODO keyword, the tags, time strings for deadline,
  9806. scheduled, and clocking, and any additional properties defined in the
  9807. entry. The return value is an alist, keys may occur multiple times
  9808. if the property key was used several times.
  9809. POM may also be nil, in which case the current entry is used.
  9810. If WHICH is nil or `all', get all properties. If WHICH is
  9811. `special' or `standard', only get that subclass."
  9812. (setq which (or which 'all))
  9813. (org-with-point-at pom
  9814. (let ((clockstr (substring org-clock-string 0 -1))
  9815. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9816. beg end range props sum-props key value string clocksum)
  9817. (save-excursion
  9818. (when (condition-case nil
  9819. (and (org-mode-p) (org-back-to-heading t))
  9820. (error nil))
  9821. (setq beg (point))
  9822. (setq sum-props (get-text-property (point) 'org-summaries))
  9823. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9824. (outline-next-heading)
  9825. (setq end (point))
  9826. (when (memq which '(all special))
  9827. ;; Get the special properties, like TODO and tags
  9828. (goto-char beg)
  9829. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9830. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9831. (when (looking-at org-priority-regexp)
  9832. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9833. (when (and (setq value (org-get-tags-string))
  9834. (string-match "\\S-" value))
  9835. (push (cons "TAGS" value) props))
  9836. (when (setq value (org-get-tags-at))
  9837. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9838. props))
  9839. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9840. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9841. string (if (equal key clockstr)
  9842. (org-no-properties
  9843. (org-trim
  9844. (buffer-substring
  9845. (match-beginning 3) (goto-char (point-at-eol)))))
  9846. (substring (org-match-string-no-properties 3) 1 -1)))
  9847. (unless key
  9848. (if (= (char-after (match-beginning 3)) ?\[)
  9849. (setq key "TIMESTAMP_IA")
  9850. (setq key "TIMESTAMP")))
  9851. (when (or (equal key clockstr) (not (assoc key props)))
  9852. (push (cons key string) props)))
  9853. )
  9854. (when (memq which '(all standard))
  9855. ;; Get the standard properties, like :PORP: ...
  9856. (setq range (org-get-property-block beg end))
  9857. (when range
  9858. (goto-char (car range))
  9859. (while (re-search-forward
  9860. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9861. (cdr range) t)
  9862. (setq key (org-match-string-no-properties 1)
  9863. value (org-trim (or (org-match-string-no-properties 2) "")))
  9864. (unless (member key excluded)
  9865. (push (cons key (or value "")) props)))))
  9866. (if clocksum
  9867. (push (cons "CLOCKSUM"
  9868. (org-columns-number-to-string (/ (float clocksum) 60.)
  9869. 'add_times))
  9870. props))
  9871. (unless (assoc "CATEGORY" props)
  9872. (setq value (or (org-get-category)
  9873. (progn (org-refresh-category-properties)
  9874. (org-get-category))))
  9875. (push (cons "CATEGORY" value) props))
  9876. (append sum-props (nreverse props)))))))
  9877. (defun org-entry-get (pom property &optional inherit)
  9878. "Get value of PROPERTY for entry at point-or-marker POM.
  9879. If INHERIT is non-nil and the entry does not have the property,
  9880. then also check higher levels of the hierarchy.
  9881. If INHERIT is the symbol `selective', use inheritance only if the setting
  9882. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9883. If the property is present but empty, the return value is the empty string.
  9884. If the property is not present at all, nil is returned."
  9885. (org-with-point-at pom
  9886. (if (and inherit (if (eq inherit 'selective)
  9887. (org-property-inherit-p property)
  9888. t))
  9889. (org-entry-get-with-inheritance property)
  9890. (if (member property org-special-properties)
  9891. ;; We need a special property. Use brute force, get all properties.
  9892. (cdr (assoc property (org-entry-properties nil 'special)))
  9893. (let ((range (org-get-property-block)))
  9894. (if (and range
  9895. (goto-char (car range))
  9896. (re-search-forward
  9897. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9898. (cdr range) t))
  9899. ;; Found the property, return it.
  9900. (if (match-end 1)
  9901. (org-match-string-no-properties 1)
  9902. "")))))))
  9903. (defun org-property-or-variable-value (var &optional inherit)
  9904. "Check if there is a property fixing the value of VAR.
  9905. If yes, return this value. If not, return the current value of the variable."
  9906. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9907. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9908. (read prop)
  9909. (symbol-value var))))
  9910. (defun org-entry-delete (pom property)
  9911. "Delete the property PROPERTY from entry at point-or-marker POM."
  9912. (org-with-point-at pom
  9913. (if (member property org-special-properties)
  9914. nil ; cannot delete these properties.
  9915. (let ((range (org-get-property-block)))
  9916. (if (and range
  9917. (goto-char (car range))
  9918. (re-search-forward
  9919. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9920. (cdr range) t))
  9921. (progn
  9922. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9923. t)
  9924. nil)))))
  9925. ;; Multi-values properties are properties that contain multiple values
  9926. ;; These values are assumed to be single words, separated by whitespace.
  9927. (defun org-entry-add-to-multivalued-property (pom property value)
  9928. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9929. (let* ((old (org-entry-get pom property))
  9930. (values (and old (org-split-string old "[ \t]"))))
  9931. (setq value (org-entry-protect-space value))
  9932. (unless (member value values)
  9933. (setq values (cons value values))
  9934. (org-entry-put pom property
  9935. (mapconcat 'identity values " ")))))
  9936. (defun org-entry-remove-from-multivalued-property (pom property value)
  9937. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9938. (let* ((old (org-entry-get pom property))
  9939. (values (and old (org-split-string old "[ \t]"))))
  9940. (setq value (org-entry-protect-space value))
  9941. (when (member value values)
  9942. (setq values (delete value values))
  9943. (org-entry-put pom property
  9944. (mapconcat 'identity values " ")))))
  9945. (defun org-entry-member-in-multivalued-property (pom property value)
  9946. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9947. (let* ((old (org-entry-get pom property))
  9948. (values (and old (org-split-string old "[ \t]"))))
  9949. (setq value (org-entry-protect-space value))
  9950. (member value values)))
  9951. (defun org-entry-get-multivalued-property (pom property)
  9952. "Return a list of values in a multivalued property."
  9953. (let* ((value (org-entry-get pom property))
  9954. (values (and value (org-split-string value "[ \t]"))))
  9955. (mapcar 'org-entry-restore-space values)))
  9956. (defun org-entry-put-multivalued-property (pom property &rest values)
  9957. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9958. VALUES should be a list of strings. Spaces will be protected."
  9959. (org-entry-put pom property
  9960. (mapconcat 'org-entry-protect-space values " "))
  9961. (let* ((value (org-entry-get pom property))
  9962. (values (and value (org-split-string value "[ \t]"))))
  9963. (mapcar 'org-entry-restore-space values)))
  9964. (defun org-entry-protect-space (s)
  9965. "Protect spaces and newline in string S."
  9966. (while (string-match " " s)
  9967. (setq s (replace-match "%20" t t s)))
  9968. (while (string-match "\n" s)
  9969. (setq s (replace-match "%0A" t t s)))
  9970. s)
  9971. (defun org-entry-restore-space (s)
  9972. "Restore spaces and newline in string S."
  9973. (while (string-match "%20" s)
  9974. (setq s (replace-match " " t t s)))
  9975. (while (string-match "%0A" s)
  9976. (setq s (replace-match "\n" t t s)))
  9977. s)
  9978. (defvar org-entry-property-inherited-from (make-marker)
  9979. "Marker pointing to the entry from where a property was inherited.
  9980. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9981. location of the entry where the inheritance search matched. If there was
  9982. no match, the marker will point nowhere.
  9983. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9984. is set.")
  9985. (defun org-entry-get-with-inheritance (property)
  9986. "Get entry property, and search higher levels if not present."
  9987. (move-marker org-entry-property-inherited-from nil)
  9988. (let (tmp)
  9989. (save-excursion
  9990. (save-restriction
  9991. (widen)
  9992. (catch 'ex
  9993. (while t
  9994. (when (setq tmp (org-entry-get nil property))
  9995. (org-back-to-heading t)
  9996. (move-marker org-entry-property-inherited-from (point))
  9997. (throw 'ex tmp))
  9998. (or (org-up-heading-safe) (throw 'ex nil)))))
  9999. (or tmp
  10000. (cdr (assoc property org-file-properties))
  10001. (cdr (assoc property org-global-properties))
  10002. (cdr (assoc property org-global-properties-fixed))))))
  10003. (defun org-entry-put (pom property value)
  10004. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10005. (org-with-point-at pom
  10006. (org-back-to-heading t)
  10007. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10008. range)
  10009. (cond
  10010. ((equal property "TODO")
  10011. (when (and (stringp value) (string-match "\\S-" value)
  10012. (not (member value org-todo-keywords-1)))
  10013. (error "\"%s\" is not a valid TODO state" value))
  10014. (if (or (not value)
  10015. (not (string-match "\\S-" value)))
  10016. (setq value 'none))
  10017. (org-todo value)
  10018. (org-set-tags nil 'align))
  10019. ((equal property "PRIORITY")
  10020. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10021. (string-to-char value) ?\ ))
  10022. (org-set-tags nil 'align))
  10023. ((equal property "SCHEDULED")
  10024. (if (re-search-forward org-scheduled-time-regexp end t)
  10025. (cond
  10026. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10027. ((eq value 'later) (org-timestamp-change 1 'day))
  10028. (t (call-interactively 'org-schedule)))
  10029. (call-interactively 'org-schedule)))
  10030. ((equal property "DEADLINE")
  10031. (if (re-search-forward org-deadline-time-regexp end t)
  10032. (cond
  10033. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10034. ((eq value 'later) (org-timestamp-change 1 'day))
  10035. (t (call-interactively 'org-deadline)))
  10036. (call-interactively 'org-deadline)))
  10037. ((member property org-special-properties)
  10038. (error "The %s property can not yet be set with `org-entry-put'"
  10039. property))
  10040. (t ; a non-special property
  10041. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10042. (setq range (org-get-property-block beg end 'force))
  10043. (goto-char (car range))
  10044. (if (re-search-forward
  10045. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10046. (progn
  10047. (delete-region (match-beginning 1) (match-end 1))
  10048. (goto-char (match-beginning 1)))
  10049. (goto-char (cdr range))
  10050. (insert "\n")
  10051. (backward-char 1)
  10052. (org-indent-line-function)
  10053. (insert ":" property ":"))
  10054. (and value (insert " " value))
  10055. (org-indent-line-function)))))))
  10056. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10057. "Get all property keys in the current buffer.
  10058. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10059. like tags and TODO state.
  10060. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10061. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10062. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10063. formats in the current buffer."
  10064. (let (rtn range cfmt s p)
  10065. (save-excursion
  10066. (save-restriction
  10067. (widen)
  10068. (goto-char (point-min))
  10069. (while (re-search-forward org-property-start-re nil t)
  10070. (setq range (org-get-property-block))
  10071. (goto-char (car range))
  10072. (while (re-search-forward
  10073. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10074. (cdr range) t)
  10075. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10076. (outline-next-heading))))
  10077. (when include-specials
  10078. (setq rtn (append org-special-properties rtn)))
  10079. (when include-defaults
  10080. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10081. (when include-columns
  10082. (save-excursion
  10083. (save-restriction
  10084. (widen)
  10085. (goto-char (point-min))
  10086. (while (re-search-forward
  10087. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10088. nil t)
  10089. (setq cfmt (match-string 2) s 0)
  10090. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10091. cfmt s)
  10092. (setq s (match-end 0)
  10093. p (match-string 1 cfmt))
  10094. (unless (or (equal p "ITEM")
  10095. (member p org-special-properties))
  10096. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10097. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10098. (defun org-property-values (key)
  10099. "Return a list of all values of property KEY."
  10100. (save-excursion
  10101. (save-restriction
  10102. (widen)
  10103. (goto-char (point-min))
  10104. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10105. values)
  10106. (while (re-search-forward re nil t)
  10107. (add-to-list 'values (org-trim (match-string 1))))
  10108. (delete "" values)))))
  10109. (defun org-insert-property-drawer ()
  10110. "Insert a property drawer into the current entry."
  10111. (interactive)
  10112. (org-back-to-heading t)
  10113. (looking-at outline-regexp)
  10114. (let ((indent (- (match-end 0)(match-beginning 0)))
  10115. (beg (point))
  10116. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10117. end hiddenp)
  10118. (outline-next-heading)
  10119. (setq end (point))
  10120. (goto-char beg)
  10121. (while (re-search-forward re end t))
  10122. (setq hiddenp (org-invisible-p))
  10123. (end-of-line 1)
  10124. (and (equal (char-after) ?\n) (forward-char 1))
  10125. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10126. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10127. ;; just skip this line
  10128. (beginning-of-line 2)
  10129. ;; Drawer start, find the end
  10130. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10131. (beginning-of-line 1)))
  10132. (org-skip-over-state-notes)
  10133. (skip-chars-backward " \t\n\r")
  10134. (if (eq (char-before) ?*) (forward-char 1))
  10135. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10136. (beginning-of-line 0)
  10137. (org-indent-to-column indent)
  10138. (beginning-of-line 2)
  10139. (org-indent-to-column indent)
  10140. (beginning-of-line 0)
  10141. (if hiddenp
  10142. (save-excursion
  10143. (org-back-to-heading t)
  10144. (hide-entry))
  10145. (org-flag-drawer t))))
  10146. (defun org-set-property (property value)
  10147. "In the current entry, set PROPERTY to VALUE.
  10148. When called interactively, this will prompt for a property name, offering
  10149. completion on existing and default properties. And then it will prompt
  10150. for a value, offering completion either on allowed values (via an inherited
  10151. xxx_ALL property) or on existing values in other instances of this property
  10152. in the current file."
  10153. (interactive
  10154. (let* ((completion-ignore-case t)
  10155. (keys (org-buffer-property-keys nil t t))
  10156. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10157. (prop (if (member prop0 keys)
  10158. prop0
  10159. (or (cdr (assoc (downcase prop0)
  10160. (mapcar (lambda (x) (cons (downcase x) x))
  10161. keys)))
  10162. prop0)))
  10163. (cur (org-entry-get nil prop))
  10164. (allowed (org-property-get-allowed-values nil prop 'table))
  10165. (existing (mapcar 'list (org-property-values prop)))
  10166. (val (if allowed
  10167. (org-completing-read "Value: " allowed nil 'req-match)
  10168. (let (org-completion-use-ido)
  10169. (org-completing-read
  10170. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10171. (concat "[" cur "]") "")
  10172. ": ")
  10173. existing nil nil "" nil cur)))))
  10174. (list prop (if (equal val "") cur val))))
  10175. (unless (equal (org-entry-get nil property) value)
  10176. (org-entry-put nil property value)))
  10177. (defun org-delete-property (property)
  10178. "In the current entry, delete PROPERTY."
  10179. (interactive
  10180. (let* ((completion-ignore-case t)
  10181. (prop (org-ido-completing-read
  10182. "Property: " (org-entry-properties nil 'standard))))
  10183. (list prop)))
  10184. (message "Property %s %s" property
  10185. (if (org-entry-delete nil property)
  10186. "deleted"
  10187. "was not present in the entry")))
  10188. (defun org-delete-property-globally (property)
  10189. "Remove PROPERTY globally, from all entries."
  10190. (interactive
  10191. (let* ((completion-ignore-case t)
  10192. (prop (org-ido-completing-read
  10193. "Globally remove property: "
  10194. (mapcar 'list (org-buffer-property-keys)))))
  10195. (list prop)))
  10196. (save-excursion
  10197. (save-restriction
  10198. (widen)
  10199. (goto-char (point-min))
  10200. (let ((cnt 0))
  10201. (while (re-search-forward
  10202. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10203. nil t)
  10204. (setq cnt (1+ cnt))
  10205. (replace-match ""))
  10206. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10207. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10208. (defun org-compute-property-at-point ()
  10209. "Compute the property at point.
  10210. This looks for an enclosing column format, extracts the operator and
  10211. then applies it to the property in the column format's scope."
  10212. (interactive)
  10213. (unless (org-at-property-p)
  10214. (error "Not at a property"))
  10215. (let ((prop (org-match-string-no-properties 2)))
  10216. (org-columns-get-format-and-top-level)
  10217. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10218. (error "No operator defined for property %s" prop))
  10219. (org-columns-compute prop)))
  10220. (defun org-property-get-allowed-values (pom property &optional table)
  10221. "Get allowed values for the property PROPERTY.
  10222. When TABLE is non-nil, return an alist that can directly be used for
  10223. completion."
  10224. (let (vals)
  10225. (cond
  10226. ((equal property "TODO")
  10227. (setq vals (org-with-point-at pom
  10228. (append org-todo-keywords-1 '("")))))
  10229. ((equal property "PRIORITY")
  10230. (let ((n org-lowest-priority))
  10231. (while (>= n org-highest-priority)
  10232. (push (char-to-string n) vals)
  10233. (setq n (1- n)))))
  10234. ((member property org-special-properties))
  10235. (t
  10236. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10237. (when (and vals (string-match "\\S-" vals))
  10238. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10239. (setq vals (mapcar (lambda (x)
  10240. (cond ((stringp x) x)
  10241. ((numberp x) (number-to-string x))
  10242. ((symbolp x) (symbol-name x))
  10243. (t "???")))
  10244. vals)))))
  10245. (if table (mapcar 'list vals) vals)))
  10246. (defun org-property-previous-allowed-value (&optional previous)
  10247. "Switch to the next allowed value for this property."
  10248. (interactive)
  10249. (org-property-next-allowed-value t))
  10250. (defun org-property-next-allowed-value (&optional previous)
  10251. "Switch to the next allowed value for this property."
  10252. (interactive)
  10253. (unless (org-at-property-p)
  10254. (error "Not at a property"))
  10255. (let* ((key (match-string 2))
  10256. (value (match-string 3))
  10257. (allowed (or (org-property-get-allowed-values (point) key)
  10258. (and (member value '("[ ]" "[-]" "[X]"))
  10259. '("[ ]" "[X]"))))
  10260. nval)
  10261. (unless allowed
  10262. (error "Allowed values for this property have not been defined"))
  10263. (if previous (setq allowed (reverse allowed)))
  10264. (if (member value allowed)
  10265. (setq nval (car (cdr (member value allowed)))))
  10266. (setq nval (or nval (car allowed)))
  10267. (if (equal nval value)
  10268. (error "Only one allowed value for this property"))
  10269. (org-at-property-p)
  10270. (replace-match (concat " :" key ": " nval) t t)
  10271. (org-indent-line-function)
  10272. (beginning-of-line 1)
  10273. (skip-chars-forward " \t")))
  10274. (defun org-find-entry-with-id (ident)
  10275. "Locate the entry that contains the ID property with exact value IDENT.
  10276. IDENT can be a string, a symbol or a number, this function will search for
  10277. the string representation of it.
  10278. Return the position where this entry starts, or nil if there is no such entry."
  10279. (interactive "sID: ")
  10280. (let ((id (cond
  10281. ((stringp ident) ident)
  10282. ((symbol-name ident) (symbol-name ident))
  10283. ((numberp ident) (number-to-string ident))
  10284. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10285. (case-fold-search nil))
  10286. (save-excursion
  10287. (save-restriction
  10288. (widen)
  10289. (goto-char (point-min))
  10290. (when (re-search-forward
  10291. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10292. nil t)
  10293. (org-back-to-heading)
  10294. (point))))))
  10295. ;;;; Timestamps
  10296. (defvar org-last-changed-timestamp nil)
  10297. (defvar org-last-inserted-timestamp nil
  10298. "The last time stamp inserted with `org-insert-time-stamp'.")
  10299. (defvar org-time-was-given) ; dynamically scoped parameter
  10300. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10301. (defvar org-ts-what) ; dynamically scoped parameter
  10302. (defun org-time-stamp (arg &optional inactive)
  10303. "Prompt for a date/time and insert a time stamp.
  10304. If the user specifies a time like HH:MM, or if this command is called
  10305. with a prefix argument, the time stamp will contain date and time.
  10306. Otherwise, only the date will be included. All parts of a date not
  10307. specified by the user will be filled in from the current date/time.
  10308. So if you press just return without typing anything, the time stamp
  10309. will represent the current date/time. If there is already a timestamp
  10310. at the cursor, it will be modified."
  10311. (interactive "P")
  10312. (let* ((ts nil)
  10313. (default-time
  10314. ;; Default time is either today, or, when entering a range,
  10315. ;; the range start.
  10316. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10317. (save-excursion
  10318. (re-search-backward
  10319. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10320. (- (point) 20) t)))
  10321. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10322. (current-time)))
  10323. (default-input (and ts (org-get-compact-tod ts)))
  10324. org-time-was-given org-end-time-was-given time)
  10325. (cond
  10326. ((and (org-at-timestamp-p t)
  10327. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10328. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10329. (insert "--")
  10330. (setq time (let ((this-command this-command))
  10331. (org-read-date arg 'totime nil nil
  10332. default-time default-input)))
  10333. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10334. ((org-at-timestamp-p t)
  10335. (setq time (let ((this-command this-command))
  10336. (org-read-date arg 'totime nil nil default-time default-input)))
  10337. (when (org-at-timestamp-p t) ; just to get the match data
  10338. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10339. (replace-match "")
  10340. (setq org-last-changed-timestamp
  10341. (org-insert-time-stamp
  10342. time (or org-time-was-given arg)
  10343. inactive nil nil (list org-end-time-was-given))))
  10344. (message "Timestamp updated"))
  10345. (t
  10346. (setq time (let ((this-command this-command))
  10347. (org-read-date arg 'totime nil nil default-time default-input)))
  10348. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10349. nil nil (list org-end-time-was-given))))))
  10350. ;; FIXME: can we use this for something else, like computing time differences?
  10351. (defun org-get-compact-tod (s)
  10352. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10353. (let* ((t1 (match-string 1 s))
  10354. (h1 (string-to-number (match-string 2 s)))
  10355. (m1 (string-to-number (match-string 3 s)))
  10356. (t2 (and (match-end 4) (match-string 5 s)))
  10357. (h2 (and t2 (string-to-number (match-string 6 s))))
  10358. (m2 (and t2 (string-to-number (match-string 7 s))))
  10359. dh dm)
  10360. (if (not t2)
  10361. t1
  10362. (setq dh (- h2 h1) dm (- m2 m1))
  10363. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10364. (concat t1 "+" (number-to-string dh)
  10365. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10366. (defun org-time-stamp-inactive (&optional arg)
  10367. "Insert an inactive time stamp.
  10368. An inactive time stamp is enclosed in square brackets instead of angle
  10369. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10370. does not link to the calendar and cannot be changed with the S-cursor keys.
  10371. So these are more for recording a certain time/date."
  10372. (interactive "P")
  10373. (org-time-stamp arg 'inactive))
  10374. (defvar org-date-ovl (org-make-overlay 1 1))
  10375. (org-overlay-put org-date-ovl 'face 'org-warning)
  10376. (org-detach-overlay org-date-ovl)
  10377. (defvar org-ans1) ; dynamically scoped parameter
  10378. (defvar org-ans2) ; dynamically scoped parameter
  10379. (defvar org-plain-time-of-day-regexp) ; defined below
  10380. (defvar org-overriding-default-time nil) ; dynamically scoped
  10381. (defvar org-read-date-overlay nil)
  10382. (defvar org-dcst nil) ; dynamically scoped
  10383. (defun org-read-date (&optional with-time to-time from-string prompt
  10384. default-time default-input)
  10385. "Read a date, possibly a time, and make things smooth for the user.
  10386. The prompt will suggest to enter an ISO date, but you can also enter anything
  10387. which will at least partially be understood by `parse-time-string'.
  10388. Unrecognized parts of the date will default to the current day, month, year,
  10389. hour and minute. If this command is called to replace a timestamp at point,
  10390. of to enter the second timestamp of a range, the default time is taken from the
  10391. existing stamp. For example,
  10392. 3-2-5 --> 2003-02-05
  10393. feb 15 --> currentyear-02-15
  10394. sep 12 9 --> 2009-09-12
  10395. 12:45 --> today 12:45
  10396. 22 sept 0:34 --> currentyear-09-22 0:34
  10397. 12 --> currentyear-currentmonth-12
  10398. Fri --> nearest Friday (today or later)
  10399. etc.
  10400. Furthermore you can specify a relative date by giving, as the *first* thing
  10401. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10402. change in days weeks, months, years.
  10403. With a single plus or minus, the date is relative to today. With a double
  10404. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10405. +4d --> four days from today
  10406. +4 --> same as above
  10407. +2w --> two weeks from today
  10408. ++5 --> five days from default date
  10409. The function understands only English month and weekday abbreviations,
  10410. but this can be configured with the variables `parse-time-months' and
  10411. `parse-time-weekdays'.
  10412. While prompting, a calendar is popped up - you can also select the
  10413. date with the mouse (button 1). The calendar shows a period of three
  10414. months. To scroll it to other months, use the keys `>' and `<'.
  10415. If you don't like the calendar, turn it off with
  10416. \(setq org-read-date-popup-calendar nil)
  10417. With optional argument TO-TIME, the date will immediately be converted
  10418. to an internal time.
  10419. With an optional argument WITH-TIME, the prompt will suggest to also
  10420. insert a time. Note that when WITH-TIME is not set, you can still
  10421. enter a time, and this function will inform the calling routine about
  10422. this change. The calling routine may then choose to change the format
  10423. used to insert the time stamp into the buffer to include the time.
  10424. With optional argument FROM-STRING, read from this string instead from
  10425. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10426. the time/date that is used for everything that is not specified by the
  10427. user."
  10428. (require 'parse-time)
  10429. (let* ((org-time-stamp-rounding-minutes
  10430. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10431. (org-dcst org-display-custom-times)
  10432. (ct (org-current-time))
  10433. (def (or org-overriding-default-time default-time ct))
  10434. (defdecode (decode-time def))
  10435. (dummy (progn
  10436. (when (< (nth 2 defdecode) org-extend-today-until)
  10437. (setcar (nthcdr 2 defdecode) -1)
  10438. (setcar (nthcdr 1 defdecode) 59)
  10439. (setq def (apply 'encode-time defdecode)
  10440. defdecode (decode-time def)))))
  10441. (calendar-move-hook nil)
  10442. (calendar-view-diary-initially-flag nil)
  10443. (view-diary-entries-initially nil)
  10444. (calendar-view-holidays-initially-flag nil)
  10445. (view-calendar-holidays-initially nil)
  10446. (timestr (format-time-string
  10447. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10448. (prompt (concat (if prompt (concat prompt " ") "")
  10449. (format "Date+time [%s]: " timestr)))
  10450. ans (org-ans0 "") org-ans1 org-ans2 final)
  10451. (cond
  10452. (from-string (setq ans from-string))
  10453. (org-read-date-popup-calendar
  10454. (save-excursion
  10455. (save-window-excursion
  10456. (calendar)
  10457. (calendar-forward-day (- (time-to-days def)
  10458. (calendar-absolute-from-gregorian
  10459. (calendar-current-date))))
  10460. (org-eval-in-calendar nil t)
  10461. (let* ((old-map (current-local-map))
  10462. (map (copy-keymap calendar-mode-map))
  10463. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10464. (org-defkey map (kbd "RET") 'org-calendar-select)
  10465. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10466. 'org-calendar-select-mouse)
  10467. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10468. 'org-calendar-select-mouse)
  10469. (org-defkey minibuffer-local-map [(meta shift left)]
  10470. (lambda () (interactive)
  10471. (org-eval-in-calendar '(calendar-backward-month 1))))
  10472. (org-defkey minibuffer-local-map [(meta shift right)]
  10473. (lambda () (interactive)
  10474. (org-eval-in-calendar '(calendar-forward-month 1))))
  10475. (org-defkey minibuffer-local-map [(meta shift up)]
  10476. (lambda () (interactive)
  10477. (org-eval-in-calendar '(calendar-backward-year 1))))
  10478. (org-defkey minibuffer-local-map [(meta shift down)]
  10479. (lambda () (interactive)
  10480. (org-eval-in-calendar '(calendar-forward-year 1))))
  10481. (org-defkey minibuffer-local-map [(shift up)]
  10482. (lambda () (interactive)
  10483. (org-eval-in-calendar '(calendar-backward-week 1))))
  10484. (org-defkey minibuffer-local-map [(shift down)]
  10485. (lambda () (interactive)
  10486. (org-eval-in-calendar '(calendar-forward-week 1))))
  10487. (org-defkey minibuffer-local-map [(shift left)]
  10488. (lambda () (interactive)
  10489. (org-eval-in-calendar '(calendar-backward-day 1))))
  10490. (org-defkey minibuffer-local-map [(shift right)]
  10491. (lambda () (interactive)
  10492. (org-eval-in-calendar '(calendar-forward-day 1))))
  10493. (org-defkey minibuffer-local-map ">"
  10494. (lambda () (interactive)
  10495. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10496. (org-defkey minibuffer-local-map "<"
  10497. (lambda () (interactive)
  10498. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10499. (unwind-protect
  10500. (progn
  10501. (use-local-map map)
  10502. (add-hook 'post-command-hook 'org-read-date-display)
  10503. (setq org-ans0 (read-string prompt default-input nil nil))
  10504. ;; org-ans0: from prompt
  10505. ;; org-ans1: from mouse click
  10506. ;; org-ans2: from calendar motion
  10507. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10508. (remove-hook 'post-command-hook 'org-read-date-display)
  10509. (use-local-map old-map)
  10510. (when org-read-date-overlay
  10511. (org-delete-overlay org-read-date-overlay)
  10512. (setq org-read-date-overlay nil)))))))
  10513. (t ; Naked prompt only
  10514. (unwind-protect
  10515. (setq ans (read-string prompt default-input nil timestr))
  10516. (when org-read-date-overlay
  10517. (org-delete-overlay org-read-date-overlay)
  10518. (setq org-read-date-overlay nil)))))
  10519. (setq final (org-read-date-analyze ans def defdecode))
  10520. (if to-time
  10521. (apply 'encode-time final)
  10522. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10523. (format "%04d-%02d-%02d %02d:%02d"
  10524. (nth 5 final) (nth 4 final) (nth 3 final)
  10525. (nth 2 final) (nth 1 final))
  10526. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10527. (defvar def)
  10528. (defvar defdecode)
  10529. (defvar with-time)
  10530. (defun org-read-date-display ()
  10531. "Display the current date prompt interpretation in the minibuffer."
  10532. (when org-read-date-display-live
  10533. (when org-read-date-overlay
  10534. (org-delete-overlay org-read-date-overlay))
  10535. (let ((p (point)))
  10536. (end-of-line 1)
  10537. (while (not (equal (buffer-substring
  10538. (max (point-min) (- (point) 4)) (point))
  10539. " "))
  10540. (insert " "))
  10541. (goto-char p))
  10542. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10543. " " (or org-ans1 org-ans2)))
  10544. (org-end-time-was-given nil)
  10545. (f (org-read-date-analyze ans def defdecode))
  10546. (fmts (if org-dcst
  10547. org-time-stamp-custom-formats
  10548. org-time-stamp-formats))
  10549. (fmt (if (or with-time
  10550. (and (boundp 'org-time-was-given) org-time-was-given))
  10551. (cdr fmts)
  10552. (car fmts)))
  10553. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10554. (when (and org-end-time-was-given
  10555. (string-match org-plain-time-of-day-regexp txt))
  10556. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10557. org-end-time-was-given
  10558. (substring txt (match-end 0)))))
  10559. (setq org-read-date-overlay
  10560. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10561. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10562. (defun org-read-date-analyze (ans def defdecode)
  10563. "Analyse the combined answer of the date prompt."
  10564. ;; FIXME: cleanup and comment
  10565. (let (delta deltan deltaw deltadef year month day
  10566. hour minute second wday pm h2 m2 tl wday1
  10567. iso-year iso-weekday iso-week iso-year iso-date)
  10568. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10569. (setq ans "+0"))
  10570. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10571. (setq ans (replace-match "" t t ans)
  10572. deltan (car delta)
  10573. deltaw (nth 1 delta)
  10574. deltadef (nth 2 delta)))
  10575. ;; Check if there is an iso week date in there
  10576. ;; If yes, sore the info and postpone interpreting it until the rest
  10577. ;; of the parsing is done
  10578. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10579. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10580. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10581. iso-week (string-to-number (match-string 2 ans)))
  10582. (setq ans (replace-match "" t t ans)))
  10583. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10584. (when (string-match
  10585. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10586. (setq year (if (match-end 2)
  10587. (string-to-number (match-string 2 ans))
  10588. (string-to-number (format-time-string "%Y")))
  10589. month (string-to-number (match-string 3 ans))
  10590. day (string-to-number (match-string 4 ans)))
  10591. (if (< year 100) (setq year (+ 2000 year)))
  10592. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10593. t nil ans)))
  10594. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10595. ;; If there is a time with am/pm, and *no* time without it, we convert
  10596. ;; so that matching will be successful.
  10597. (loop for i from 1 to 2 do ; twice, for end time as well
  10598. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10599. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10600. (setq hour (string-to-number (match-string 1 ans))
  10601. minute (if (match-end 3)
  10602. (string-to-number (match-string 3 ans))
  10603. 0)
  10604. pm (equal ?p
  10605. (string-to-char (downcase (match-string 4 ans)))))
  10606. (if (and (= hour 12) (not pm))
  10607. (setq hour 0)
  10608. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10609. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10610. t t ans))))
  10611. ;; Check if a time range is given as a duration
  10612. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10613. (setq hour (string-to-number (match-string 1 ans))
  10614. h2 (+ hour (string-to-number (match-string 3 ans)))
  10615. minute (string-to-number (match-string 2 ans))
  10616. m2 (+ minute (if (match-end 5) (string-to-number
  10617. (match-string 5 ans))0)))
  10618. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10619. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10620. t t ans)))
  10621. ;; Check if there is a time range
  10622. (when (boundp 'org-end-time-was-given)
  10623. (setq org-time-was-given nil)
  10624. (when (and (string-match org-plain-time-of-day-regexp ans)
  10625. (match-end 8))
  10626. (setq org-end-time-was-given (match-string 8 ans))
  10627. (setq ans (concat (substring ans 0 (match-beginning 7))
  10628. (substring ans (match-end 7))))))
  10629. (setq tl (parse-time-string ans)
  10630. day (or (nth 3 tl) (nth 3 defdecode))
  10631. month (or (nth 4 tl)
  10632. (if (and org-read-date-prefer-future
  10633. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10634. (1+ (nth 4 defdecode))
  10635. (nth 4 defdecode)))
  10636. year (or (nth 5 tl)
  10637. (if (and org-read-date-prefer-future
  10638. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10639. (1+ (nth 5 defdecode))
  10640. (nth 5 defdecode)))
  10641. hour (or (nth 2 tl) (nth 2 defdecode))
  10642. minute (or (nth 1 tl) (nth 1 defdecode))
  10643. second (or (nth 0 tl) 0)
  10644. wday (nth 6 tl))
  10645. ;; Special date definitions below
  10646. (cond
  10647. (iso-week
  10648. ;; There was an iso week
  10649. (setq year (or iso-year year)
  10650. day (or iso-weekday wday 1)
  10651. wday nil ; to make sure that the trigger below does not match
  10652. iso-date (calendar-gregorian-from-absolute
  10653. (calendar-absolute-from-iso
  10654. (list iso-week day year))))
  10655. ; FIXME: Should we also push ISO weeks into the future?
  10656. ; (when (and org-read-date-prefer-future
  10657. ; (not iso-year)
  10658. ; (< (calendar-absolute-from-gregorian iso-date)
  10659. ; (time-to-days (current-time))))
  10660. ; (setq year (1+ year)
  10661. ; iso-date (calendar-gregorian-from-absolute
  10662. ; (calendar-absolute-from-iso
  10663. ; (list iso-week day year)))))
  10664. (setq month (car iso-date)
  10665. year (nth 2 iso-date)
  10666. day (nth 1 iso-date)))
  10667. (deltan
  10668. (unless deltadef
  10669. (let ((now (decode-time (current-time))))
  10670. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10671. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10672. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10673. ((equal deltaw "m") (setq month (+ month deltan)))
  10674. ((equal deltaw "y") (setq year (+ year deltan)))))
  10675. ((and wday (not (nth 3 tl)))
  10676. ;; Weekday was given, but no day, so pick that day in the week
  10677. ;; on or after the derived date.
  10678. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10679. (unless (equal wday wday1)
  10680. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10681. (if (and (boundp 'org-time-was-given)
  10682. (nth 2 tl))
  10683. (setq org-time-was-given t))
  10684. (if (< year 100) (setq year (+ 2000 year)))
  10685. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10686. (list second minute hour day month year)))
  10687. (defvar parse-time-weekdays)
  10688. (defun org-read-date-get-relative (s today default)
  10689. "Check string S for special relative date string.
  10690. TODAY and DEFAULT are internal times, for today and for a default.
  10691. Return shift list (N what def-flag)
  10692. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10693. N is the number of WHATs to shift.
  10694. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10695. the DEFAULT date rather than TODAY."
  10696. (when (and
  10697. (string-match
  10698. (concat
  10699. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10700. "\\([0-9]+\\)?"
  10701. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10702. "\\([ \t]\\|$\\)") s)
  10703. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10704. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10705. (string-to-char (substring (match-string 1 s) -1))
  10706. ?+))
  10707. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10708. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10709. (what (if (match-end 3) (match-string 3 s) "d"))
  10710. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10711. (date (if rel default today))
  10712. (wday (nth 6 (decode-time date)))
  10713. delta)
  10714. (if wday1
  10715. (progn
  10716. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10717. (if (= dir ?-) (setq delta (- delta 7)))
  10718. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10719. (list delta "d" rel))
  10720. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10721. (defun org-eval-in-calendar (form &optional keepdate)
  10722. "Eval FORM in the calendar window and return to current window.
  10723. Also, store the cursor date in variable org-ans2."
  10724. (let ((sw (selected-window)))
  10725. (select-window (get-buffer-window "*Calendar*"))
  10726. (eval form)
  10727. (when (and (not keepdate) (calendar-cursor-to-date))
  10728. (let* ((date (calendar-cursor-to-date))
  10729. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10730. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10731. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10732. (select-window sw)))
  10733. (defun org-calendar-select ()
  10734. "Return to `org-read-date' with the date currently selected.
  10735. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10736. (interactive)
  10737. (when (calendar-cursor-to-date)
  10738. (let* ((date (calendar-cursor-to-date))
  10739. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10740. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10741. (if (active-minibuffer-window) (exit-minibuffer))))
  10742. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10743. "Insert a date stamp for the date given by the internal TIME.
  10744. WITH-HM means, use the stamp format that includes the time of the day.
  10745. INACTIVE means use square brackets instead of angular ones, so that the
  10746. stamp will not contribute to the agenda.
  10747. PRE and POST are optional strings to be inserted before and after the
  10748. stamp.
  10749. The command returns the inserted time stamp."
  10750. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10751. stamp)
  10752. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10753. (insert-before-markers (or pre ""))
  10754. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10755. (when (listp extra)
  10756. (setq extra (car extra))
  10757. (if (and (stringp extra)
  10758. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10759. (setq extra (format "-%02d:%02d"
  10760. (string-to-number (match-string 1 extra))
  10761. (string-to-number (match-string 2 extra))))
  10762. (setq extra nil)))
  10763. (when extra
  10764. (backward-char 1)
  10765. (insert-before-markers extra)
  10766. (forward-char 1))
  10767. (insert-before-markers (or post ""))
  10768. (setq org-last-inserted-timestamp stamp)))
  10769. (defun org-toggle-time-stamp-overlays ()
  10770. "Toggle the use of custom time stamp formats."
  10771. (interactive)
  10772. (setq org-display-custom-times (not org-display-custom-times))
  10773. (unless org-display-custom-times
  10774. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10775. (while (setq p (next-single-property-change p 'display))
  10776. (if (and (get-text-property p 'display)
  10777. (eq (get-text-property p 'face) 'org-date))
  10778. (remove-text-properties
  10779. p (setq p (next-single-property-change p 'display))
  10780. '(display t))))
  10781. (set-buffer-modified-p bmp)))
  10782. (if (featurep 'xemacs)
  10783. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10784. (org-restart-font-lock)
  10785. (setq org-table-may-need-update t)
  10786. (if org-display-custom-times
  10787. (message "Time stamps are overlayed with custom format")
  10788. (message "Time stamp overlays removed")))
  10789. (defun org-display-custom-time (beg end)
  10790. "Overlay modified time stamp format over timestamp between BEG and END."
  10791. (let* ((ts (buffer-substring beg end))
  10792. t1 w1 with-hm tf time str w2 (off 0))
  10793. (save-match-data
  10794. (setq t1 (org-parse-time-string ts t))
  10795. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10796. (setq off (- (match-end 0) (match-beginning 0)))))
  10797. (setq end (- end off))
  10798. (setq w1 (- end beg)
  10799. with-hm (and (nth 1 t1) (nth 2 t1))
  10800. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10801. time (org-fix-decoded-time t1)
  10802. str (org-add-props
  10803. (format-time-string
  10804. (substring tf 1 -1) (apply 'encode-time time))
  10805. nil 'mouse-face 'highlight)
  10806. w2 (length str))
  10807. (if (not (= w2 w1))
  10808. (add-text-properties (1+ beg) (+ 2 beg)
  10809. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10810. (if (featurep 'xemacs)
  10811. (progn
  10812. (put-text-property beg end 'invisible t)
  10813. (put-text-property beg end 'end-glyph (make-glyph str)))
  10814. (put-text-property beg end 'display str))))
  10815. (defun org-translate-time (string)
  10816. "Translate all timestamps in STRING to custom format.
  10817. But do this only if the variable `org-display-custom-times' is set."
  10818. (when org-display-custom-times
  10819. (save-match-data
  10820. (let* ((start 0)
  10821. (re org-ts-regexp-both)
  10822. t1 with-hm inactive tf time str beg end)
  10823. (while (setq start (string-match re string start))
  10824. (setq beg (match-beginning 0)
  10825. end (match-end 0)
  10826. t1 (save-match-data
  10827. (org-parse-time-string (substring string beg end) t))
  10828. with-hm (and (nth 1 t1) (nth 2 t1))
  10829. inactive (equal (substring string beg (1+ beg)) "[")
  10830. tf (funcall (if with-hm 'cdr 'car)
  10831. org-time-stamp-custom-formats)
  10832. time (org-fix-decoded-time t1)
  10833. str (format-time-string
  10834. (concat
  10835. (if inactive "[" "<") (substring tf 1 -1)
  10836. (if inactive "]" ">"))
  10837. (apply 'encode-time time))
  10838. string (replace-match str t t string)
  10839. start (+ start (length str)))))))
  10840. string)
  10841. (defun org-fix-decoded-time (time)
  10842. "Set 0 instead of nil for the first 6 elements of time.
  10843. Don't touch the rest."
  10844. (let ((n 0))
  10845. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10846. (defun org-days-to-time (timestamp-string)
  10847. "Difference between TIMESTAMP-STRING and now in days."
  10848. (- (time-to-days (org-time-string-to-time timestamp-string))
  10849. (time-to-days (current-time))))
  10850. (defun org-deadline-close (timestamp-string &optional ndays)
  10851. "Is the time in TIMESTAMP-STRING close to the current date?"
  10852. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10853. (and (< (org-days-to-time timestamp-string) ndays)
  10854. (not (org-entry-is-done-p))))
  10855. (defun org-get-wdays (ts)
  10856. "Get the deadline lead time appropriate for timestring TS."
  10857. (cond
  10858. ((<= org-deadline-warning-days 0)
  10859. ;; 0 or negative, enforce this value no matter what
  10860. (- org-deadline-warning-days))
  10861. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  10862. ;; lead time is specified.
  10863. (floor (* (string-to-number (match-string 1 ts))
  10864. (cdr (assoc (match-string 2 ts)
  10865. '(("d" . 1) ("w" . 7)
  10866. ("m" . 30.4) ("y" . 365.25)))))))
  10867. ;; go for the default.
  10868. (t org-deadline-warning-days)))
  10869. (defun org-calendar-select-mouse (ev)
  10870. "Return to `org-read-date' with the date currently selected.
  10871. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10872. (interactive "e")
  10873. (mouse-set-point ev)
  10874. (when (calendar-cursor-to-date)
  10875. (let* ((date (calendar-cursor-to-date))
  10876. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10877. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10878. (if (active-minibuffer-window) (exit-minibuffer))))
  10879. (defun org-check-deadlines (ndays)
  10880. "Check if there are any deadlines due or past due.
  10881. A deadline is considered due if it happens within `org-deadline-warning-days'
  10882. days from today's date. If the deadline appears in an entry marked DONE,
  10883. it is not shown. The prefix arg NDAYS can be used to test that many
  10884. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10885. (interactive "P")
  10886. (let* ((org-warn-days
  10887. (cond
  10888. ((equal ndays '(4)) 100000)
  10889. (ndays (prefix-numeric-value ndays))
  10890. (t (abs org-deadline-warning-days))))
  10891. (case-fold-search nil)
  10892. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10893. (callback
  10894. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10895. (message "%d deadlines past-due or due within %d days"
  10896. (org-occur regexp nil callback)
  10897. org-warn-days)))
  10898. (defun org-check-before-date (date)
  10899. "Check if there are deadlines or scheduled entries before DATE."
  10900. (interactive (list (org-read-date)))
  10901. (let ((case-fold-search nil)
  10902. (regexp (concat "\\<\\(" org-deadline-string
  10903. "\\|" org-scheduled-string
  10904. "\\) *<\\([^>]+\\)>"))
  10905. (callback
  10906. (lambda () (time-less-p
  10907. (org-time-string-to-time (match-string 2))
  10908. (org-time-string-to-time date)))))
  10909. (message "%d entries before %s"
  10910. (org-occur regexp nil callback) date)))
  10911. (defun org-evaluate-time-range (&optional to-buffer)
  10912. "Evaluate a time range by computing the difference between start and end.
  10913. Normally the result is just printed in the echo area, but with prefix arg
  10914. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10915. If the time range is actually in a table, the result is inserted into the
  10916. next column.
  10917. For time difference computation, a year is assumed to be exactly 365
  10918. days in order to avoid rounding problems."
  10919. (interactive "P")
  10920. (or
  10921. (org-clock-update-time-maybe)
  10922. (save-excursion
  10923. (unless (org-at-date-range-p t)
  10924. (goto-char (point-at-bol))
  10925. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10926. (if (not (org-at-date-range-p t))
  10927. (error "Not at a time-stamp range, and none found in current line")))
  10928. (let* ((ts1 (match-string 1))
  10929. (ts2 (match-string 2))
  10930. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10931. (match-end (match-end 0))
  10932. (time1 (org-time-string-to-time ts1))
  10933. (time2 (org-time-string-to-time ts2))
  10934. (t1 (time-to-seconds time1))
  10935. (t2 (time-to-seconds time2))
  10936. (diff (abs (- t2 t1)))
  10937. (negative (< (- t2 t1) 0))
  10938. ;; (ys (floor (* 365 24 60 60)))
  10939. (ds (* 24 60 60))
  10940. (hs (* 60 60))
  10941. (fy "%dy %dd %02d:%02d")
  10942. (fy1 "%dy %dd")
  10943. (fd "%dd %02d:%02d")
  10944. (fd1 "%dd")
  10945. (fh "%02d:%02d")
  10946. y d h m align)
  10947. (if havetime
  10948. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10949. y 0
  10950. d (floor (/ diff ds)) diff (mod diff ds)
  10951. h (floor (/ diff hs)) diff (mod diff hs)
  10952. m (floor (/ diff 60)))
  10953. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10954. y 0
  10955. d (floor (+ (/ diff ds) 0.5))
  10956. h 0 m 0))
  10957. (if (not to-buffer)
  10958. (message "%s" (org-make-tdiff-string y d h m))
  10959. (if (org-at-table-p)
  10960. (progn
  10961. (goto-char match-end)
  10962. (setq align t)
  10963. (and (looking-at " *|") (goto-char (match-end 0))))
  10964. (goto-char match-end))
  10965. (if (looking-at
  10966. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10967. (replace-match ""))
  10968. (if negative (insert " -"))
  10969. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10970. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10971. (insert " " (format fh h m))))
  10972. (if align (org-table-align))
  10973. (message "Time difference inserted")))))
  10974. (defun org-make-tdiff-string (y d h m)
  10975. (let ((fmt "")
  10976. (l nil))
  10977. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10978. l (push y l)))
  10979. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10980. l (push d l)))
  10981. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10982. l (push h l)))
  10983. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10984. l (push m l)))
  10985. (apply 'format fmt (nreverse l))))
  10986. (defun org-time-string-to-time (s)
  10987. (apply 'encode-time (org-parse-time-string s)))
  10988. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10989. "Convert a time stamp to an absolute day number.
  10990. If there is a specifyer for a cyclic time stamp, get the closest date to
  10991. DAYNR.
  10992. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10993. (cond
  10994. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10995. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10996. daynr
  10997. (+ daynr 1000)))
  10998. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10999. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11000. (time-to-days (current-time))) (match-string 0 s)
  11001. prefer show-all))
  11002. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11003. (defun org-days-to-iso-week (days)
  11004. "Return the iso week number."
  11005. (require 'cal-iso)
  11006. (car (calendar-iso-from-absolute days)))
  11007. (defun org-small-year-to-year (year)
  11008. "Convert 2-digit years into 4-digit years.
  11009. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11010. The year 2000 cannot be abbreviated. Any year larger than 99
  11011. is returned unchanged."
  11012. (if (< year 38)
  11013. (setq year (+ 2000 year))
  11014. (if (< year 100)
  11015. (setq year (+ 1900 year))))
  11016. year)
  11017. (defun org-time-from-absolute (d)
  11018. "Return the time corresponding to date D.
  11019. D may be an absolute day number, or a calendar-type list (month day year)."
  11020. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11021. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11022. (defun org-calendar-holiday ()
  11023. "List of holidays, for Diary display in Org-mode."
  11024. (require 'holidays)
  11025. (let ((hl (funcall
  11026. (if (fboundp 'calendar-check-holidays)
  11027. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11028. (if hl (mapconcat 'identity hl "; "))))
  11029. (defun org-diary-sexp-entry (sexp entry date)
  11030. "Process a SEXP diary ENTRY for DATE."
  11031. (require 'diary-lib)
  11032. (let ((result (if calendar-debug-sexp
  11033. (let ((stack-trace-on-error t))
  11034. (eval (car (read-from-string sexp))))
  11035. (condition-case nil
  11036. (eval (car (read-from-string sexp)))
  11037. (error
  11038. (beep)
  11039. (message "Bad sexp at line %d in %s: %s"
  11040. (org-current-line)
  11041. (buffer-file-name) sexp)
  11042. (sleep-for 2))))))
  11043. (cond ((stringp result) result)
  11044. ((and (consp result)
  11045. (stringp (cdr result))) (cdr result))
  11046. (result entry)
  11047. (t nil))))
  11048. (defun org-diary-to-ical-string (frombuf)
  11049. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11050. This uses the icalendar.el library."
  11051. (let* ((tmpdir (if (featurep 'xemacs)
  11052. (temp-directory)
  11053. temporary-file-directory))
  11054. (tmpfile (make-temp-name
  11055. (expand-file-name "orgics" tmpdir)))
  11056. buf rtn b e)
  11057. (save-excursion
  11058. (set-buffer frombuf)
  11059. (icalendar-export-region (point-min) (point-max) tmpfile)
  11060. (setq buf (find-buffer-visiting tmpfile))
  11061. (set-buffer buf)
  11062. (goto-char (point-min))
  11063. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11064. (setq b (match-beginning 0)))
  11065. (goto-char (point-max))
  11066. (if (re-search-backward "^END:VEVENT" nil t)
  11067. (setq e (match-end 0)))
  11068. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11069. (kill-buffer buf)
  11070. (delete-file tmpfile)
  11071. rtn))
  11072. (defun org-closest-date (start current change prefer show-all)
  11073. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11074. When PREFER is `past' return a date that is either CURRENT or past.
  11075. When PREFER is `future', return a date that is either CURRENT or future.
  11076. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11077. ;; Make the proper lists from the dates
  11078. (catch 'exit
  11079. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11080. dn dw sday cday n1 n2 n0
  11081. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11082. (setq start (org-date-to-gregorian start)
  11083. current (org-date-to-gregorian
  11084. (if show-all
  11085. current
  11086. (time-to-days (current-time))))
  11087. sday (calendar-absolute-from-gregorian start)
  11088. cday (calendar-absolute-from-gregorian current))
  11089. (if (<= cday sday) (throw 'exit sday))
  11090. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11091. (setq dn (string-to-number (match-string 1 change))
  11092. dw (cdr (assoc (match-string 2 change) a1)))
  11093. (error "Invalid change specifyer: %s" change))
  11094. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11095. (cond
  11096. ((eq dw 'day)
  11097. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11098. n2 (+ n1 dn)))
  11099. ((eq dw 'year)
  11100. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11101. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11102. (setq date1 (list m d y1)
  11103. n1 (calendar-absolute-from-gregorian date1)
  11104. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11105. n2 (calendar-absolute-from-gregorian date2)))
  11106. ((eq dw 'month)
  11107. ;; approx number of month between the two dates
  11108. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11109. ;; How often does dn fit in there?
  11110. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11111. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11112. m (+ m nm)
  11113. ny (floor (/ m 12))
  11114. y (+ y ny)
  11115. m (- m (* ny 12)))
  11116. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11117. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11118. (setq m2 (+ m dn) y2 y)
  11119. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11120. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11121. (while (<= n2 cday)
  11122. (setq n1 n2 m m2 y y2)
  11123. (setq m2 (+ m dn) y2 y)
  11124. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11125. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11126. ;; Make sure n1 is the earlier date
  11127. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11128. (if show-all
  11129. (cond
  11130. ((eq prefer 'past) n1)
  11131. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11132. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11133. (cond
  11134. ((eq prefer 'past) n1)
  11135. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11136. (t (if (= cday n1) n1 n2)))))))
  11137. (defun org-date-to-gregorian (date)
  11138. "Turn any specification of DATE into a gregorian date for the calendar."
  11139. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11140. ((and (listp date) (= (length date) 3)) date)
  11141. ((stringp date)
  11142. (setq date (org-parse-time-string date))
  11143. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11144. ((listp date)
  11145. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11146. (defun org-parse-time-string (s &optional nodefault)
  11147. "Parse the standard Org-mode time string.
  11148. This should be a lot faster than the normal `parse-time-string'.
  11149. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11150. hour and minute fields will be nil if not given."
  11151. (if (string-match org-ts-regexp0 s)
  11152. (list 0
  11153. (if (or (match-beginning 8) (not nodefault))
  11154. (string-to-number (or (match-string 8 s) "0")))
  11155. (if (or (match-beginning 7) (not nodefault))
  11156. (string-to-number (or (match-string 7 s) "0")))
  11157. (string-to-number (match-string 4 s))
  11158. (string-to-number (match-string 3 s))
  11159. (string-to-number (match-string 2 s))
  11160. nil nil nil)
  11161. (make-list 9 0)))
  11162. (defun org-timestamp-up (&optional arg)
  11163. "Increase the date item at the cursor by one.
  11164. If the cursor is on the year, change the year. If it is on the month or
  11165. the day, change that.
  11166. With prefix ARG, change by that many units."
  11167. (interactive "p")
  11168. (org-timestamp-change (prefix-numeric-value arg)))
  11169. (defun org-timestamp-down (&optional arg)
  11170. "Decrease the date item at the cursor by one.
  11171. If the cursor is on the year, change the year. If it is on the month or
  11172. the day, change that.
  11173. With prefix ARG, change by that many units."
  11174. (interactive "p")
  11175. (org-timestamp-change (- (prefix-numeric-value arg))))
  11176. (defun org-timestamp-up-day (&optional arg)
  11177. "Increase the date in the time stamp by one day.
  11178. With prefix ARG, change that many days."
  11179. (interactive "p")
  11180. (if (and (not (org-at-timestamp-p t))
  11181. (org-on-heading-p))
  11182. (org-todo 'up)
  11183. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11184. (defun org-timestamp-down-day (&optional arg)
  11185. "Decrease the date in the time stamp by one day.
  11186. With prefix ARG, change that many days."
  11187. (interactive "p")
  11188. (if (and (not (org-at-timestamp-p t))
  11189. (org-on-heading-p))
  11190. (org-todo 'down)
  11191. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11192. (defun org-at-timestamp-p (&optional inactive-ok)
  11193. "Determine if the cursor is in or at a timestamp."
  11194. (interactive)
  11195. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11196. (pos (point))
  11197. (ans (or (looking-at tsr)
  11198. (save-excursion
  11199. (skip-chars-backward "^[<\n\r\t")
  11200. (if (> (point) (point-min)) (backward-char 1))
  11201. (and (looking-at tsr)
  11202. (> (- (match-end 0) pos) -1))))))
  11203. (and ans
  11204. (boundp 'org-ts-what)
  11205. (setq org-ts-what
  11206. (cond
  11207. ((= pos (match-beginning 0)) 'bracket)
  11208. ((= pos (1- (match-end 0))) 'bracket)
  11209. ((org-pos-in-match-range pos 2) 'year)
  11210. ((org-pos-in-match-range pos 3) 'month)
  11211. ((org-pos-in-match-range pos 7) 'hour)
  11212. ((org-pos-in-match-range pos 8) 'minute)
  11213. ((or (org-pos-in-match-range pos 4)
  11214. (org-pos-in-match-range pos 5)) 'day)
  11215. ((and (> pos (or (match-end 8) (match-end 5)))
  11216. (< pos (match-end 0)))
  11217. (- pos (or (match-end 8) (match-end 5))))
  11218. (t 'day))))
  11219. ans))
  11220. (defun org-toggle-timestamp-type ()
  11221. "Toggle the type (<active> or [inactive]) of a time stamp."
  11222. (interactive)
  11223. (when (org-at-timestamp-p t)
  11224. (let ((beg (match-beginning 0)) (end (match-end 0))
  11225. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11226. (save-excursion
  11227. (goto-char beg)
  11228. (while (re-search-forward "[][<>]" end t)
  11229. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11230. t t)))
  11231. (message "Timestamp is now %sactive"
  11232. (if (equal (char-after beg) ?<) "" "in")))))
  11233. (defun org-timestamp-change (n &optional what)
  11234. "Change the date in the time stamp at point.
  11235. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11236. `year', `minute', `second'. If WHAT is not given, the cursor position
  11237. in the timestamp determines what will be changed."
  11238. (let ((pos (point))
  11239. with-hm inactive
  11240. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11241. org-ts-what
  11242. extra rem
  11243. ts time time0)
  11244. (if (not (org-at-timestamp-p t))
  11245. (error "Not at a timestamp"))
  11246. (if (and (not what) (eq org-ts-what 'bracket))
  11247. (org-toggle-timestamp-type)
  11248. (if (and (not what) (not (eq org-ts-what 'day))
  11249. org-display-custom-times
  11250. (get-text-property (point) 'display)
  11251. (not (get-text-property (1- (point)) 'display)))
  11252. (setq org-ts-what 'day))
  11253. (setq org-ts-what (or what org-ts-what)
  11254. inactive (= (char-after (match-beginning 0)) ?\[)
  11255. ts (match-string 0))
  11256. (replace-match "")
  11257. (if (string-match
  11258. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11259. ts)
  11260. (setq extra (match-string 1 ts)))
  11261. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11262. (setq with-hm t))
  11263. (setq time0 (org-parse-time-string ts))
  11264. (when (and (eq org-ts-what 'minute)
  11265. (eq current-prefix-arg nil))
  11266. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11267. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11268. (setcar (cdr time0) (+ (nth 1 time0)
  11269. (if (> n 0) (- rem) (- dm rem))))))
  11270. (setq time
  11271. (encode-time (or (car time0) 0)
  11272. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11273. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11274. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11275. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11276. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11277. (nthcdr 6 time0)))
  11278. (when (integerp org-ts-what)
  11279. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11280. (if (eq what 'calendar)
  11281. (let ((cal-date (org-get-date-from-calendar)))
  11282. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11283. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11284. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11285. (setcar time0 (or (car time0) 0))
  11286. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11287. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11288. (setq time (apply 'encode-time time0))))
  11289. (setq org-last-changed-timestamp
  11290. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11291. (org-clock-update-time-maybe)
  11292. (goto-char pos)
  11293. ;; Try to recenter the calendar window, if any
  11294. (if (and org-calendar-follow-timestamp-change
  11295. (get-buffer-window "*Calendar*" t)
  11296. (memq org-ts-what '(day month year)))
  11297. (org-recenter-calendar (time-to-days time))))))
  11298. (defun org-modify-ts-extra (s pos n dm)
  11299. "Change the different parts of the lead-time and repeat fields in timestamp."
  11300. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11301. ng h m new rem)
  11302. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11303. (cond
  11304. ((or (org-pos-in-match-range pos 2)
  11305. (org-pos-in-match-range pos 3))
  11306. (setq m (string-to-number (match-string 3 s))
  11307. h (string-to-number (match-string 2 s)))
  11308. (if (org-pos-in-match-range pos 2)
  11309. (setq h (+ h n))
  11310. (setq n (* dm (org-no-warnings (signum n))))
  11311. (when (not (= 0 (setq rem (% m dm))))
  11312. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11313. (setq m (+ m n)))
  11314. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11315. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11316. (setq h (min 24 (max 0 h)))
  11317. (setq ng 1 new (format "-%02d:%02d" h m)))
  11318. ((org-pos-in-match-range pos 6)
  11319. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11320. ((org-pos-in-match-range pos 5)
  11321. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11322. ((org-pos-in-match-range pos 9)
  11323. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11324. ((org-pos-in-match-range pos 8)
  11325. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11326. (when ng
  11327. (setq s (concat
  11328. (substring s 0 (match-beginning ng))
  11329. new
  11330. (substring s (match-end ng))))))
  11331. s))
  11332. (defun org-recenter-calendar (date)
  11333. "If the calendar is visible, recenter it to DATE."
  11334. (let* ((win (selected-window))
  11335. (cwin (get-buffer-window "*Calendar*" t))
  11336. (calendar-move-hook nil))
  11337. (when cwin
  11338. (select-window cwin)
  11339. (calendar-goto-date (if (listp date) date
  11340. (calendar-gregorian-from-absolute date)))
  11341. (select-window win))))
  11342. (defun org-goto-calendar (&optional arg)
  11343. "Go to the Emacs calendar at the current date.
  11344. If there is a time stamp in the current line, go to that date.
  11345. A prefix ARG can be used to force the current date."
  11346. (interactive "P")
  11347. (let ((tsr org-ts-regexp) diff
  11348. (calendar-move-hook nil)
  11349. (calendar-view-holidays-initially-flag nil)
  11350. (view-calendar-holidays-initially nil)
  11351. (calendar-view-diary-initially-flag nil)
  11352. (view-diary-entries-initially nil))
  11353. (if (or (org-at-timestamp-p)
  11354. (save-excursion
  11355. (beginning-of-line 1)
  11356. (looking-at (concat ".*" tsr))))
  11357. (let ((d1 (time-to-days (current-time)))
  11358. (d2 (time-to-days
  11359. (org-time-string-to-time (match-string 1)))))
  11360. (setq diff (- d2 d1))))
  11361. (calendar)
  11362. (calendar-goto-today)
  11363. (if (and diff (not arg)) (calendar-forward-day diff))))
  11364. (defun org-get-date-from-calendar ()
  11365. "Return a list (month day year) of date at point in calendar."
  11366. (with-current-buffer "*Calendar*"
  11367. (save-match-data
  11368. (calendar-cursor-to-date))))
  11369. (defun org-date-from-calendar ()
  11370. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11371. If there is already a time stamp at the cursor position, update it."
  11372. (interactive)
  11373. (if (org-at-timestamp-p t)
  11374. (org-timestamp-change 0 'calendar)
  11375. (let ((cal-date (org-get-date-from-calendar)))
  11376. (org-insert-time-stamp
  11377. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11378. (defun org-minutes-to-hh:mm-string (m)
  11379. "Compute H:MM from a number of minutes."
  11380. (let ((h (/ m 60)))
  11381. (setq m (- m (* 60 h)))
  11382. (format org-time-clocksum-format h m)))
  11383. (defun org-hh:mm-string-to-minutes (s)
  11384. "Convert a string H:MM to a number of minutes."
  11385. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11386. (+ (* (string-to-number (match-string 1 s)) 60)
  11387. (string-to-number (match-string 2 s)))
  11388. 0))
  11389. ;;;; Files
  11390. (defun org-save-all-org-buffers ()
  11391. "Save all Org-mode buffers without user confirmation."
  11392. (interactive)
  11393. (message "Saving all Org-mode buffers...")
  11394. (save-some-buffers t 'org-mode-p)
  11395. (when (featurep 'org-id) (org-id-locations-save))
  11396. (message "Saving all Org-mode buffers... done"))
  11397. (defun org-revert-all-org-buffers ()
  11398. "Revert all Org-mode buffers.
  11399. Prompt for confirmation when there are unsaved changes.
  11400. Be sure you know what you are doing before letting this function
  11401. overwrite your changes.
  11402. This function is useful in a setup where one tracks org files
  11403. with a version control system, to revert on one machine after pulling
  11404. changes from another. I believe the procedure must be like this:
  11405. 1. M-x org-save-all-org-buffers
  11406. 2. Pull changes from the other machine, resolve conflicts
  11407. 3. M-x org-revert-all-org-buffers"
  11408. (interactive)
  11409. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11410. (error "Abort"))
  11411. (save-excursion
  11412. (save-window-excursion
  11413. (mapc
  11414. (lambda (b)
  11415. (when (and (with-current-buffer b (org-mode-p))
  11416. (with-current-buffer b buffer-file-name))
  11417. (switch-to-buffer b)
  11418. (revert-buffer t 'no-confirm)))
  11419. (buffer-list))
  11420. (when (and (featurep 'org-id) org-id-track-globally)
  11421. (org-id-locations-load)))))
  11422. ;;;; Agenda files
  11423. ;;;###autoload
  11424. (defun org-iswitchb (&optional arg)
  11425. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11426. With a prefix argument, restrict available to files.
  11427. With two prefix arguments, restrict available buffers to agenda files.
  11428. Due to some yet unresolved reason, the global function
  11429. `iswitchb-mode' needs to be active for this function to work."
  11430. (interactive "P")
  11431. (require 'iswitchb)
  11432. (let ((enabled iswitchb-mode) blist)
  11433. (or enabled (iswitchb-mode 1))
  11434. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11435. ((equal arg '(16)) (org-buffer-list 'agenda))
  11436. (t (org-buffer-list))))
  11437. (unwind-protect
  11438. (let ((iswitchb-make-buflist-hook
  11439. (lambda ()
  11440. (setq iswitchb-temp-buflist
  11441. (mapcar 'buffer-name blist)))))
  11442. (switch-to-buffer
  11443. (iswitchb-read-buffer
  11444. "Switch-to: " nil t))
  11445. (or enabled (iswitchb-mode -1))))))
  11446. ;;;###autoload
  11447. (defun org-ido-switchb (&optional arg)
  11448. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11449. With a prefix argument, restrict available to files.
  11450. With two prefix arguments, restrict available buffers to agenda files."
  11451. (interactive "P")
  11452. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11453. ((equal arg '(16)) (org-buffer-list 'agenda))
  11454. (t (org-buffer-list)))))
  11455. (switch-to-buffer
  11456. (org-ido-completing-read "Org buffer: "
  11457. (mapcar 'buffer-name blist)
  11458. nil t))))
  11459. (defun org-buffer-list (&optional predicate exclude-tmp)
  11460. "Return a list of Org buffers.
  11461. PREDICATE can be `export', `files' or `agenda'.
  11462. export restrict the list to Export buffers.
  11463. files restrict the list to buffers visiting Org files.
  11464. agenda restrict the list to buffers visiting agenda files.
  11465. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11466. (let* ((bfn nil)
  11467. (agenda-files (and (eq predicate 'agenda)
  11468. (mapcar 'file-truename (org-agenda-files t))))
  11469. (filter
  11470. (cond
  11471. ((eq predicate 'files)
  11472. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11473. ((eq predicate 'export)
  11474. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11475. ((eq predicate 'agenda)
  11476. (lambda (b)
  11477. (with-current-buffer b
  11478. (and (eq major-mode 'org-mode)
  11479. (setq bfn (buffer-file-name b))
  11480. (member (file-truename bfn) agenda-files)))))
  11481. (t (lambda (b) (with-current-buffer b
  11482. (or (eq major-mode 'org-mode)
  11483. (string-match "\*Org .*Export"
  11484. (buffer-name b)))))))))
  11485. (delq nil
  11486. (mapcar
  11487. (lambda(b)
  11488. (if (and (funcall filter b)
  11489. (or (not exclude-tmp)
  11490. (not (string-match "tmp" (buffer-name b)))))
  11491. b
  11492. nil))
  11493. (buffer-list)))))
  11494. (defun org-agenda-files (&optional unrestricted archives)
  11495. "Get the list of agenda files.
  11496. Optional UNRESTRICTED means return the full list even if a restriction
  11497. is currently in place.
  11498. When ARCHIVES is t, include all archive files hat are really being
  11499. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11500. `org-agenda-archives-mode' is t."
  11501. (let ((files
  11502. (cond
  11503. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11504. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11505. ((listp org-agenda-files) org-agenda-files)
  11506. (t (error "Invalid value of `org-agenda-files'")))))
  11507. (setq files (apply 'append
  11508. (mapcar (lambda (f)
  11509. (if (file-directory-p f)
  11510. (directory-files
  11511. f t org-agenda-file-regexp)
  11512. (list f)))
  11513. files)))
  11514. (when org-agenda-skip-unavailable-files
  11515. (setq files (delq nil
  11516. (mapcar (function
  11517. (lambda (file)
  11518. (and (file-readable-p file) file)))
  11519. files))))
  11520. (when (or (eq archives t)
  11521. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11522. (setq files (org-add-archive-files files)))
  11523. files))
  11524. (defun org-edit-agenda-file-list ()
  11525. "Edit the list of agenda files.
  11526. Depending on setup, this either uses customize to edit the variable
  11527. `org-agenda-files', or it visits the file that is holding the list. In the
  11528. latter case, the buffer is set up in a way that saving it automatically kills
  11529. the buffer and restores the previous window configuration."
  11530. (interactive)
  11531. (if (stringp org-agenda-files)
  11532. (let ((cw (current-window-configuration)))
  11533. (find-file org-agenda-files)
  11534. (org-set-local 'org-window-configuration cw)
  11535. (org-add-hook 'after-save-hook
  11536. (lambda ()
  11537. (set-window-configuration
  11538. (prog1 org-window-configuration
  11539. (kill-buffer (current-buffer))))
  11540. (org-install-agenda-files-menu)
  11541. (message "New agenda file list installed"))
  11542. nil 'local)
  11543. (message "%s" (substitute-command-keys
  11544. "Edit list and finish with \\[save-buffer]")))
  11545. (customize-variable 'org-agenda-files)))
  11546. (defun org-store-new-agenda-file-list (list)
  11547. "Set new value for the agenda file list and save it correctly."
  11548. (if (stringp org-agenda-files)
  11549. (let ((f org-agenda-files) b)
  11550. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11551. (with-temp-file f
  11552. (insert (mapconcat 'identity list "\n") "\n")))
  11553. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11554. (setq org-agenda-files list)
  11555. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11556. (defun org-read-agenda-file-list ()
  11557. "Read the list of agenda files from a file."
  11558. (when (file-directory-p org-agenda-files)
  11559. (error "`org-agenda-files' cannot be a single directory"))
  11560. (when (stringp org-agenda-files)
  11561. (with-temp-buffer
  11562. (insert-file-contents org-agenda-files)
  11563. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11564. ;;;###autoload
  11565. (defun org-cycle-agenda-files ()
  11566. "Cycle through the files in `org-agenda-files'.
  11567. If the current buffer visits an agenda file, find the next one in the list.
  11568. If the current buffer does not, find the first agenda file."
  11569. (interactive)
  11570. (let* ((fs (org-agenda-files t))
  11571. (files (append fs (list (car fs))))
  11572. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11573. file)
  11574. (unless files (error "No agenda files"))
  11575. (catch 'exit
  11576. (while (setq file (pop files))
  11577. (if (equal (file-truename file) tcf)
  11578. (when (car files)
  11579. (find-file (car files))
  11580. (throw 'exit t))))
  11581. (find-file (car fs)))
  11582. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11583. (defun org-agenda-file-to-front (&optional to-end)
  11584. "Move/add the current file to the top of the agenda file list.
  11585. If the file is not present in the list, it is added to the front. If it is
  11586. present, it is moved there. With optional argument TO-END, add/move to the
  11587. end of the list."
  11588. (interactive "P")
  11589. (let ((org-agenda-skip-unavailable-files nil)
  11590. (file-alist (mapcar (lambda (x)
  11591. (cons (file-truename x) x))
  11592. (org-agenda-files t)))
  11593. (ctf (file-truename buffer-file-name))
  11594. x had)
  11595. (setq x (assoc ctf file-alist) had x)
  11596. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11597. (if to-end
  11598. (setq file-alist (append (delq x file-alist) (list x)))
  11599. (setq file-alist (cons x (delq x file-alist))))
  11600. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11601. (org-install-agenda-files-menu)
  11602. (message "File %s to %s of agenda file list"
  11603. (if had "moved" "added") (if to-end "end" "front"))))
  11604. (defun org-remove-file (&optional file)
  11605. "Remove current file from the list of files in variable `org-agenda-files'.
  11606. These are the files which are being checked for agenda entries.
  11607. Optional argument FILE means, use this file instead of the current."
  11608. (interactive)
  11609. (let* ((org-agenda-skip-unavailable-files nil)
  11610. (file (or file buffer-file-name))
  11611. (true-file (file-truename file))
  11612. (afile (abbreviate-file-name file))
  11613. (files (delq nil (mapcar
  11614. (lambda (x)
  11615. (if (equal true-file
  11616. (file-truename x))
  11617. nil x))
  11618. (org-agenda-files t)))))
  11619. (if (not (= (length files) (length (org-agenda-files t))))
  11620. (progn
  11621. (org-store-new-agenda-file-list files)
  11622. (org-install-agenda-files-menu)
  11623. (message "Removed file: %s" afile))
  11624. (message "File was not in list: %s (not removed)" afile))))
  11625. (defun org-file-menu-entry (file)
  11626. (vector file (list 'find-file file) t))
  11627. (defun org-check-agenda-file (file)
  11628. "Make sure FILE exists. If not, ask user what to do."
  11629. (when (not (file-exists-p file))
  11630. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11631. (abbreviate-file-name file))
  11632. (let ((r (downcase (read-char-exclusive))))
  11633. (cond
  11634. ((equal r ?r)
  11635. (org-remove-file file)
  11636. (throw 'nextfile t))
  11637. (t (error "Abort"))))))
  11638. (defun org-get-agenda-file-buffer (file)
  11639. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11640. it to the list of buffers which might be released later."
  11641. (let ((buf (org-find-base-buffer-visiting file)))
  11642. (if buf
  11643. buf ; just return it
  11644. ;; Make a new buffer and remember it
  11645. (setq buf (find-file-noselect file))
  11646. (if buf (push buf org-agenda-new-buffers))
  11647. buf)))
  11648. (defun org-release-buffers (blist)
  11649. "Release all buffers in list, asking the user for confirmation when needed.
  11650. When a buffer is unmodified, it is just killed. When modified, it is saved
  11651. \(if the user agrees) and then killed."
  11652. (let (buf file)
  11653. (while (setq buf (pop blist))
  11654. (setq file (buffer-file-name buf))
  11655. (when (and (buffer-modified-p buf)
  11656. file
  11657. (y-or-n-p (format "Save file %s? " file)))
  11658. (with-current-buffer buf (save-buffer)))
  11659. (kill-buffer buf))))
  11660. (defun org-prepare-agenda-buffers (files)
  11661. "Create buffers for all agenda files, protect archived trees and comments."
  11662. (interactive)
  11663. (let ((pa '(:org-archived t))
  11664. (pc '(:org-comment t))
  11665. (pall '(:org-archived t :org-comment t))
  11666. (inhibit-read-only t)
  11667. (rea (concat ":" org-archive-tag ":"))
  11668. bmp file re)
  11669. (save-excursion
  11670. (save-restriction
  11671. (while (setq file (pop files))
  11672. (if (bufferp file)
  11673. (set-buffer file)
  11674. (org-check-agenda-file file)
  11675. (set-buffer (org-get-agenda-file-buffer file)))
  11676. (widen)
  11677. (setq bmp (buffer-modified-p))
  11678. (org-refresh-category-properties)
  11679. (setq org-todo-keywords-for-agenda
  11680. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11681. (setq org-done-keywords-for-agenda
  11682. (append org-done-keywords-for-agenda org-done-keywords))
  11683. (setq org-todo-keyword-alist-for-agenda
  11684. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11685. (setq org-tag-alist-for-agenda
  11686. (append org-tag-alist-for-agenda org-tag-alist))
  11687. (save-excursion
  11688. (remove-text-properties (point-min) (point-max) pall)
  11689. (when org-agenda-skip-archived-trees
  11690. (goto-char (point-min))
  11691. (while (re-search-forward rea nil t)
  11692. (if (org-on-heading-p t)
  11693. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11694. (goto-char (point-min))
  11695. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11696. (while (re-search-forward re nil t)
  11697. (add-text-properties
  11698. (match-beginning 0) (org-end-of-subtree t) pc)))
  11699. (set-buffer-modified-p bmp))))
  11700. (setq org-todo-keyword-alist-for-agenda
  11701. (org-uniquify org-todo-keyword-alist-for-agenda)
  11702. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11703. ;;;; Embedded LaTeX
  11704. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11705. "Keymap for the minor `org-cdlatex-mode'.")
  11706. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11707. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11708. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11709. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11710. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11711. (defvar org-cdlatex-texmathp-advice-is-done nil
  11712. "Flag remembering if we have applied the advice to texmathp already.")
  11713. (define-minor-mode org-cdlatex-mode
  11714. "Toggle the minor `org-cdlatex-mode'.
  11715. This mode supports entering LaTeX environment and math in LaTeX fragments
  11716. in Org-mode.
  11717. \\{org-cdlatex-mode-map}"
  11718. nil " OCDL" nil
  11719. (when org-cdlatex-mode (require 'cdlatex))
  11720. (unless org-cdlatex-texmathp-advice-is-done
  11721. (setq org-cdlatex-texmathp-advice-is-done t)
  11722. (defadvice texmathp (around org-math-always-on activate)
  11723. "Always return t in org-mode buffers.
  11724. This is because we want to insert math symbols without dollars even outside
  11725. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11726. an embedded LaTeX fragment, let texmathp do its job.
  11727. \\[org-cdlatex-mode-map]"
  11728. (interactive)
  11729. (let (p)
  11730. (cond
  11731. ((not (org-mode-p)) ad-do-it)
  11732. ((eq this-command 'cdlatex-math-symbol)
  11733. (setq ad-return-value t
  11734. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11735. (t
  11736. (let ((p (org-inside-LaTeX-fragment-p)))
  11737. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11738. (setq ad-return-value t
  11739. texmathp-why '("Org-mode embedded math" . 0))
  11740. (if p ad-do-it)))))))))
  11741. (defun turn-on-org-cdlatex ()
  11742. "Unconditionally turn on `org-cdlatex-mode'."
  11743. (org-cdlatex-mode 1))
  11744. (defun org-inside-LaTeX-fragment-p ()
  11745. "Test if point is inside a LaTeX fragment.
  11746. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11747. sequence appearing also before point.
  11748. Even though the matchers for math are configurable, this function assumes
  11749. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11750. delimiters are skipped when they have been removed by customization.
  11751. The return value is nil, or a cons cell with the delimiter and
  11752. and the position of this delimiter.
  11753. This function does a reasonably good job, but can locally be fooled by
  11754. for example currency specifications. For example it will assume being in
  11755. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11756. fragments that are properly closed, but during editing, we have to live
  11757. with the uncertainty caused by missing closing delimiters. This function
  11758. looks only before point, not after."
  11759. (catch 'exit
  11760. (let ((pos (point))
  11761. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11762. (lim (progn
  11763. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11764. (point)))
  11765. dd-on str (start 0) m re)
  11766. (goto-char pos)
  11767. (when dodollar
  11768. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11769. re (nth 1 (assoc "$" org-latex-regexps)))
  11770. (while (string-match re str start)
  11771. (cond
  11772. ((= (match-end 0) (length str))
  11773. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11774. ((= (match-end 0) (- (length str) 5))
  11775. (throw 'exit nil))
  11776. (t (setq start (match-end 0))))))
  11777. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11778. (goto-char pos)
  11779. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11780. (and (match-beginning 2) (throw 'exit nil))
  11781. ;; count $$
  11782. (while (re-search-backward "\\$\\$" lim t)
  11783. (setq dd-on (not dd-on)))
  11784. (goto-char pos)
  11785. (if dd-on (cons "$$" m))))))
  11786. (defun org-try-cdlatex-tab ()
  11787. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11788. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11789. - inside a LaTeX fragment, or
  11790. - after the first word in a line, where an abbreviation expansion could
  11791. insert a LaTeX environment."
  11792. (when org-cdlatex-mode
  11793. (cond
  11794. ((save-excursion
  11795. (skip-chars-backward "a-zA-Z0-9*")
  11796. (skip-chars-backward " \t")
  11797. (bolp))
  11798. (cdlatex-tab) t)
  11799. ((org-inside-LaTeX-fragment-p)
  11800. (cdlatex-tab) t)
  11801. (t nil))))
  11802. (defun org-cdlatex-underscore-caret (&optional arg)
  11803. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11804. Revert to the normal definition outside of these fragments."
  11805. (interactive "P")
  11806. (if (org-inside-LaTeX-fragment-p)
  11807. (call-interactively 'cdlatex-sub-superscript)
  11808. (let (org-cdlatex-mode)
  11809. (call-interactively (key-binding (vector last-input-event))))))
  11810. (defun org-cdlatex-math-modify (&optional arg)
  11811. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11812. Revert to the normal definition outside of these fragments."
  11813. (interactive "P")
  11814. (if (org-inside-LaTeX-fragment-p)
  11815. (call-interactively 'cdlatex-math-modify)
  11816. (let (org-cdlatex-mode)
  11817. (call-interactively (key-binding (vector last-input-event))))))
  11818. (defvar org-latex-fragment-image-overlays nil
  11819. "List of overlays carrying the images of latex fragments.")
  11820. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11821. (defun org-remove-latex-fragment-image-overlays ()
  11822. "Remove all overlays with LaTeX fragment images in current buffer."
  11823. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11824. (setq org-latex-fragment-image-overlays nil))
  11825. (defun org-preview-latex-fragment (&optional subtree)
  11826. "Preview the LaTeX fragment at point, or all locally or globally.
  11827. If the cursor is in a LaTeX fragment, create the image and overlay
  11828. it over the source code. If there is no fragment at point, display
  11829. all fragments in the current text, from one headline to the next. With
  11830. prefix SUBTREE, display all fragments in the current subtree. With a
  11831. double prefix `C-u C-u', or when the cursor is before the first headline,
  11832. display all fragments in the buffer.
  11833. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11834. (interactive "P")
  11835. (org-remove-latex-fragment-image-overlays)
  11836. (save-excursion
  11837. (save-restriction
  11838. (let (beg end at msg)
  11839. (cond
  11840. ((or (equal subtree '(16))
  11841. (not (save-excursion
  11842. (re-search-backward (concat "^" outline-regexp) nil t))))
  11843. (setq beg (point-min) end (point-max)
  11844. msg "Creating images for buffer...%s"))
  11845. ((equal subtree '(4))
  11846. (org-back-to-heading)
  11847. (setq beg (point) end (org-end-of-subtree t)
  11848. msg "Creating images for subtree...%s"))
  11849. (t
  11850. (if (setq at (org-inside-LaTeX-fragment-p))
  11851. (goto-char (max (point-min) (- (cdr at) 2)))
  11852. (org-back-to-heading))
  11853. (setq beg (point) end (progn (outline-next-heading) (point))
  11854. msg (if at "Creating image...%s"
  11855. "Creating images for entry...%s"))))
  11856. (message msg "")
  11857. (narrow-to-region beg end)
  11858. (goto-char beg)
  11859. (org-format-latex
  11860. (concat "ltxpng/" (file-name-sans-extension
  11861. (file-name-nondirectory
  11862. buffer-file-name)))
  11863. default-directory 'overlays msg at 'forbuffer)
  11864. (message msg "done. Use `C-c C-c' to remove images.")))))
  11865. (defvar org-latex-regexps
  11866. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11867. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11868. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11869. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11870. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11871. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11872. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11873. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11874. "Regular expressions for matching embedded LaTeX.")
  11875. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11876. "Replace LaTeX fragments with links to an image, and produce images."
  11877. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11878. (let* ((prefixnodir (file-name-nondirectory prefix))
  11879. (absprefix (expand-file-name prefix dir))
  11880. (todir (file-name-directory absprefix))
  11881. (opt org-format-latex-options)
  11882. (matchers (plist-get opt :matchers))
  11883. (re-list org-latex-regexps)
  11884. (cnt 0) txt link beg end re e checkdir
  11885. m n block linkfile movefile ov)
  11886. ;; Check if there are old images files with this prefix, and remove them
  11887. (when (file-directory-p todir)
  11888. (mapc 'delete-file
  11889. (directory-files
  11890. todir 'full
  11891. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11892. ;; Check the different regular expressions
  11893. (while (setq e (pop re-list))
  11894. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11895. block (if (nth 3 e) "\n\n" ""))
  11896. (when (member m matchers)
  11897. (goto-char (point-min))
  11898. (while (re-search-forward re nil t)
  11899. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11900. (not (get-text-property (match-beginning n)
  11901. 'org-protected)))
  11902. (setq txt (match-string n)
  11903. beg (match-beginning n) end (match-end n)
  11904. cnt (1+ cnt)
  11905. linkfile (format "%s_%04d.png" prefix cnt)
  11906. movefile (format "%s_%04d.png" absprefix cnt)
  11907. link (concat block "[[file:" linkfile "]]" block))
  11908. (if msg (message msg cnt))
  11909. (goto-char beg)
  11910. (unless checkdir ; make sure the directory exists
  11911. (setq checkdir t)
  11912. (or (file-directory-p todir) (make-directory todir)))
  11913. (org-create-formula-image
  11914. txt movefile opt forbuffer)
  11915. (if overlays
  11916. (progn
  11917. (setq ov (org-make-overlay beg end))
  11918. (if (featurep 'xemacs)
  11919. (progn
  11920. (org-overlay-put ov 'invisible t)
  11921. (org-overlay-put
  11922. ov 'end-glyph
  11923. (make-glyph (vector 'png :file movefile))))
  11924. (org-overlay-put
  11925. ov 'display
  11926. (list 'image :type 'png :file movefile :ascent 'center)))
  11927. (push ov org-latex-fragment-image-overlays)
  11928. (goto-char end))
  11929. (delete-region beg end)
  11930. (insert link))))))))
  11931. ;; This function borrows from Ganesh Swami's latex2png.el
  11932. (defun org-create-formula-image (string tofile options buffer)
  11933. (let* ((tmpdir (if (featurep 'xemacs)
  11934. (temp-directory)
  11935. temporary-file-directory))
  11936. (texfilebase (make-temp-name
  11937. (expand-file-name "orgtex" tmpdir)))
  11938. (texfile (concat texfilebase ".tex"))
  11939. (dvifile (concat texfilebase ".dvi"))
  11940. (pngfile (concat texfilebase ".png"))
  11941. (fnh (if (featurep 'xemacs)
  11942. (font-height (get-face-font 'default))
  11943. (face-attribute 'default :height nil)))
  11944. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11945. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11946. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11947. "Black"))
  11948. (bg (or (plist-get options (if buffer :background :html-background))
  11949. "Transparent")))
  11950. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11951. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11952. (with-temp-file texfile
  11953. (insert org-format-latex-header
  11954. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11955. (let ((dir default-directory))
  11956. (condition-case nil
  11957. (progn
  11958. (cd tmpdir)
  11959. (call-process "latex" nil nil nil texfile))
  11960. (error nil))
  11961. (cd dir))
  11962. (if (not (file-exists-p dvifile))
  11963. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11964. (condition-case nil
  11965. (call-process "dvipng" nil nil nil
  11966. "-E" "-fg" fg "-bg" bg
  11967. "-D" dpi
  11968. ;;"-x" scale "-y" scale
  11969. "-T" "tight"
  11970. "-o" pngfile
  11971. dvifile)
  11972. (error nil))
  11973. (if (not (file-exists-p pngfile))
  11974. (progn (message "Failed to create png file from %s" texfile) nil)
  11975. ;; Use the requested file name and clean up
  11976. (copy-file pngfile tofile 'replace)
  11977. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11978. (delete-file (concat texfilebase e)))
  11979. pngfile))))
  11980. (defun org-dvipng-color (attr)
  11981. "Return an rgb color specification for dvipng."
  11982. (apply 'format "rgb %s %s %s"
  11983. (mapcar 'org-normalize-color
  11984. (color-values (face-attribute 'default attr nil)))))
  11985. (defun org-normalize-color (value)
  11986. "Return string to be used as color value for an RGB component."
  11987. (format "%g" (/ value 65535.0)))
  11988. ;;;; Key bindings
  11989. ;; Make `C-c C-x' a prefix key
  11990. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11991. ;; TAB key with modifiers
  11992. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11993. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11994. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11995. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11996. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11997. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11998. ;; The following line is necessary under Suse GNU/Linux
  11999. (unless (featurep 'xemacs)
  12000. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12001. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12002. (define-key org-mode-map [backtab] 'org-shifttab)
  12003. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12004. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12005. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12006. ;; Cursor keys with modifiers
  12007. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12008. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12009. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12010. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12011. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12012. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12013. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12014. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12015. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12016. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12017. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12018. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12019. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12020. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12021. ;;; Extra keys for tty access.
  12022. ;; We only set them when really needed because otherwise the
  12023. ;; menus don't show the simple keys
  12024. (when (or org-use-extra-keys
  12025. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12026. (not window-system))
  12027. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12028. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12029. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12030. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12031. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12032. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12033. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12034. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12035. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12036. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12037. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12038. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12039. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12040. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12041. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12042. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12043. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12044. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12045. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12046. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12047. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12048. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12049. ;; All the other keys
  12050. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12051. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12052. (if (boundp 'narrow-map)
  12053. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12054. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12055. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12056. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12057. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12058. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12059. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12060. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12061. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12062. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12063. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12064. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12065. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12066. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12067. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12068. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12069. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12070. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12071. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12072. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12073. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12074. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12075. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12076. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12077. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12078. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12079. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12080. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12081. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12082. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12083. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12084. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12085. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12086. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12087. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12088. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12089. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12090. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12091. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12092. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12093. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12094. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12095. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12096. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12097. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12098. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12099. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12100. (org-defkey org-mode-map "\C-m" 'org-return)
  12101. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12102. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12103. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12104. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12105. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12106. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12107. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12108. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12109. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12110. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12111. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12112. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12113. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12114. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12115. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12116. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12117. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12118. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12119. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12120. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12121. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12122. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12123. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12124. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12125. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12126. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12127. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12128. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12129. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12130. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12131. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12132. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12133. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12134. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12135. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12136. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12137. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12138. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12139. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12140. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12141. (when (featurep 'xemacs)
  12142. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12143. (defvar org-self-insert-command-undo-counter 0)
  12144. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12145. (defun org-self-insert-command (N)
  12146. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12147. If the cursor is in a table looking at whitespace, the whitespace is
  12148. overwritten, and the table is not marked as requiring realignment."
  12149. (interactive "p")
  12150. (if (and
  12151. (org-table-p)
  12152. (progn
  12153. ;; check if we blank the field, and if that triggers align
  12154. (and (featurep 'org-table) org-table-auto-blank-field
  12155. (member last-command
  12156. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12157. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12158. ;; got extra space, this field does not determine column width
  12159. (let (org-table-may-need-update) (org-table-blank-field))
  12160. ;; no extra space, this field may determine column width
  12161. (org-table-blank-field)))
  12162. t)
  12163. (eq N 1)
  12164. (looking-at "[^|\n]* |"))
  12165. (let (org-table-may-need-update)
  12166. (goto-char (1- (match-end 0)))
  12167. (delete-backward-char 1)
  12168. (goto-char (match-beginning 0))
  12169. (self-insert-command N))
  12170. (setq org-table-may-need-update t)
  12171. (self-insert-command N)
  12172. (org-fix-tags-on-the-fly)
  12173. (if org-self-insert-cluster-for-undo
  12174. (if (not (eq last-command 'org-self-insert-command))
  12175. (setq org-self-insert-command-undo-counter 1)
  12176. (if (>= org-self-insert-command-undo-counter 20)
  12177. (setq org-self-insert-command-undo-counter 1)
  12178. (and (> org-self-insert-command-undo-counter 0)
  12179. buffer-undo-list
  12180. (not (cadr buffer-undo-list)) ; remove nil entry
  12181. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12182. (setq org-self-insert-command-undo-counter
  12183. (1+ org-self-insert-command-undo-counter)))))))
  12184. (defun org-fix-tags-on-the-fly ()
  12185. (when (and (equal (char-after (point-at-bol)) ?*)
  12186. (org-on-heading-p))
  12187. (org-align-tags-here org-tags-column)))
  12188. (defun org-delete-backward-char (N)
  12189. "Like `delete-backward-char', insert whitespace at field end in tables.
  12190. When deleting backwards, in tables this function will insert whitespace in
  12191. front of the next \"|\" separator, to keep the table aligned. The table will
  12192. still be marked for re-alignment if the field did fill the entire column,
  12193. because, in this case the deletion might narrow the column."
  12194. (interactive "p")
  12195. (if (and (org-table-p)
  12196. (eq N 1)
  12197. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12198. (looking-at ".*?|"))
  12199. (let ((pos (point))
  12200. (noalign (looking-at "[^|\n\r]* |"))
  12201. (c org-table-may-need-update))
  12202. (backward-delete-char N)
  12203. (skip-chars-forward "^|")
  12204. (insert " ")
  12205. (goto-char (1- pos))
  12206. ;; noalign: if there were two spaces at the end, this field
  12207. ;; does not determine the width of the column.
  12208. (if noalign (setq org-table-may-need-update c)))
  12209. (backward-delete-char N)
  12210. (org-fix-tags-on-the-fly)))
  12211. (defun org-delete-char (N)
  12212. "Like `delete-char', but insert whitespace at field end in tables.
  12213. When deleting characters, in tables this function will insert whitespace in
  12214. front of the next \"|\" separator, to keep the table aligned. The table will
  12215. still be marked for re-alignment if the field did fill the entire column,
  12216. because, in this case the deletion might narrow the column."
  12217. (interactive "p")
  12218. (if (and (org-table-p)
  12219. (not (bolp))
  12220. (not (= (char-after) ?|))
  12221. (eq N 1))
  12222. (if (looking-at ".*?|")
  12223. (let ((pos (point))
  12224. (noalign (looking-at "[^|\n\r]* |"))
  12225. (c org-table-may-need-update))
  12226. (replace-match (concat
  12227. (substring (match-string 0) 1 -1)
  12228. " |"))
  12229. (goto-char pos)
  12230. ;; noalign: if there were two spaces at the end, this field
  12231. ;; does not determine the width of the column.
  12232. (if noalign (setq org-table-may-need-update c)))
  12233. (delete-char N))
  12234. (delete-char N)
  12235. (org-fix-tags-on-the-fly)))
  12236. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12237. (put 'org-self-insert-command 'delete-selection t)
  12238. (put 'orgtbl-self-insert-command 'delete-selection t)
  12239. (put 'org-delete-char 'delete-selection 'supersede)
  12240. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12241. (put 'org-yank 'delete-selection 'yank)
  12242. ;; Make `flyspell-mode' delay after some commands
  12243. (put 'org-self-insert-command 'flyspell-delayed t)
  12244. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12245. (put 'org-delete-char 'flyspell-delayed t)
  12246. (put 'org-delete-backward-char 'flyspell-delayed t)
  12247. ;; Make pabbrev-mode expand after org-mode commands
  12248. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12249. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12250. ;; How to do this: Measure non-white length of current string
  12251. ;; If equal to column width, we should realign.
  12252. (defun org-remap (map &rest commands)
  12253. "In MAP, remap the functions given in COMMANDS.
  12254. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12255. (let (new old)
  12256. (while commands
  12257. (setq old (pop commands) new (pop commands))
  12258. (if (fboundp 'command-remapping)
  12259. (org-defkey map (vector 'remap old) new)
  12260. (substitute-key-definition old new map global-map)))))
  12261. (when (eq org-enable-table-editor 'optimized)
  12262. ;; If the user wants maximum table support, we need to hijack
  12263. ;; some standard editing functions
  12264. (org-remap org-mode-map
  12265. 'self-insert-command 'org-self-insert-command
  12266. 'delete-char 'org-delete-char
  12267. 'delete-backward-char 'org-delete-backward-char)
  12268. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12269. (defvar org-ctrl-c-ctrl-c-hook nil
  12270. "Hook for functions attaching themselves to `C-c C-c'.
  12271. This can be used to add additional functionality to the C-c C-c key which
  12272. executes context-dependent commands.
  12273. Each function will be called with no arguments. The function must check
  12274. if the context is appropriate for it to act. If yes, it should do its
  12275. thing and then return a non-nil value. If the context is wrong,
  12276. just do nothing.")
  12277. (defvar org-metaleft-hook nil
  12278. "Hook for functions attaching themselves to `M-left'.
  12279. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12280. (defvar org-metaright-hook nil
  12281. "Hook for functions attaching themselves to `M-right'.
  12282. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12283. (defvar org-metaup-hook nil
  12284. "Hook for functions attaching themselves to `M-up'.
  12285. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12286. (defvar org-metadown-hook nil
  12287. "Hook for functions attaching themselves to `M-down'.
  12288. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12289. (defvar org-shiftmetaleft-hook nil
  12290. "Hook for functions attaching themselves to `M-S-left'.
  12291. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12292. (defvar org-shiftmetaright-hook nil
  12293. "Hook for functions attaching themselves to `M-S-right'.
  12294. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12295. (defvar org-shiftmetaup-hook nil
  12296. "Hook for functions attaching themselves to `M-S-up'.
  12297. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12298. (defvar org-shiftmetadown-hook nil
  12299. "Hook for functions attaching themselves to `M-S-down'.
  12300. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12301. (defvar org-metareturn-hook nil
  12302. "Hook for functions attaching themselves to `M-RET'.
  12303. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12304. (defun org-modifier-cursor-error ()
  12305. "Throw an error, a modified cursor command was applied in wrong context."
  12306. (error "This command is active in special context like tables, headlines or items"))
  12307. (defun org-shiftselect-error ()
  12308. "Throw an error because Shift-Cursor command was applied in wrong context."
  12309. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12310. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12311. (error "This command works only in special context like headlines or timestamps.")))
  12312. (defun org-call-for-shift-select (cmd)
  12313. (let ((this-command-keys-shift-translated t))
  12314. (call-interactively cmd)))
  12315. (defun org-shifttab (&optional arg)
  12316. "Global visibility cycling or move to previous table field.
  12317. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12318. on context.
  12319. See the individual commands for more information."
  12320. (interactive "P")
  12321. (cond
  12322. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12323. ((integerp arg)
  12324. (message "Content view to level: %d" arg)
  12325. (org-content (prefix-numeric-value arg))
  12326. (setq org-cycle-global-status 'overview))
  12327. (t (call-interactively 'org-global-cycle))))
  12328. (defun org-shiftmetaleft ()
  12329. "Promote subtree or delete table column.
  12330. Calls `org-promote-subtree', `org-outdent-item',
  12331. or `org-table-delete-column', depending on context.
  12332. See the individual commands for more information."
  12333. (interactive)
  12334. (cond
  12335. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12336. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12337. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12338. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12339. (t (org-modifier-cursor-error))))
  12340. (defun org-shiftmetaright ()
  12341. "Demote subtree or insert table column.
  12342. Calls `org-demote-subtree', `org-indent-item',
  12343. or `org-table-insert-column', depending on context.
  12344. See the individual commands for more information."
  12345. (interactive)
  12346. (cond
  12347. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12348. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12349. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12350. ((org-at-item-p) (call-interactively 'org-indent-item))
  12351. (t (org-modifier-cursor-error))))
  12352. (defun org-shiftmetaup (&optional arg)
  12353. "Move subtree up or kill table row.
  12354. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12355. `org-move-item-up' depending on context. See the individual commands
  12356. for more information."
  12357. (interactive "P")
  12358. (cond
  12359. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12360. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12361. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12362. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12363. (t (org-modifier-cursor-error))))
  12364. (defun org-shiftmetadown (&optional arg)
  12365. "Move subtree down or insert table row.
  12366. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12367. `org-move-item-down', depending on context. See the individual
  12368. commands for more information."
  12369. (interactive "P")
  12370. (cond
  12371. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12372. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12373. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12374. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12375. (t (org-modifier-cursor-error))))
  12376. (defun org-metaleft (&optional arg)
  12377. "Promote heading or move table column to left.
  12378. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12379. With no specific context, calls the Emacs default `backward-word'.
  12380. See the individual commands for more information."
  12381. (interactive "P")
  12382. (cond
  12383. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12384. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12385. ((or (org-on-heading-p)
  12386. (and (org-region-active-p)
  12387. (save-excursion
  12388. (goto-char (region-beginning))
  12389. (org-on-heading-p))))
  12390. (call-interactively 'org-do-promote))
  12391. ((or (org-at-item-p)
  12392. (and (org-region-active-p)
  12393. (save-excursion
  12394. (goto-char (region-beginning))
  12395. (org-at-item-p))))
  12396. (call-interactively 'org-outdent-item))
  12397. (t (call-interactively 'backward-word))))
  12398. (defun org-metaright (&optional arg)
  12399. "Demote subtree or move table column to right.
  12400. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12401. With no specific context, calls the Emacs default `forward-word'.
  12402. See the individual commands for more information."
  12403. (interactive "P")
  12404. (cond
  12405. ((run-hook-with-args-until-success 'org-metaright-hook))
  12406. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12407. ((or (org-on-heading-p)
  12408. (and (org-region-active-p)
  12409. (save-excursion
  12410. (goto-char (region-beginning))
  12411. (org-on-heading-p))))
  12412. (call-interactively 'org-do-demote))
  12413. ((or (org-at-item-p)
  12414. (and (org-region-active-p)
  12415. (save-excursion
  12416. (goto-char (region-beginning))
  12417. (org-at-item-p))))
  12418. (call-interactively 'org-indent-item))
  12419. (t (call-interactively 'forward-word))))
  12420. (defun org-metaup (&optional arg)
  12421. "Move subtree up or move table row up.
  12422. Calls `org-move-subtree-up' or `org-table-move-row' or
  12423. `org-move-item-up', depending on context. See the individual commands
  12424. for more information."
  12425. (interactive "P")
  12426. (cond
  12427. ((run-hook-with-args-until-success 'org-metaup-hook))
  12428. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12429. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12430. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12431. (t (transpose-lines 1) (beginning-of-line -1))))
  12432. (defun org-metadown (&optional arg)
  12433. "Move subtree down or move table row down.
  12434. Calls `org-move-subtree-down' or `org-table-move-row' or
  12435. `org-move-item-down', depending on context. See the individual
  12436. commands for more information."
  12437. (interactive "P")
  12438. (cond
  12439. ((run-hook-with-args-until-success 'org-metadown-hook))
  12440. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12441. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12442. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12443. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12444. (defun org-shiftup (&optional arg)
  12445. "Increase item in timestamp or increase priority of current headline.
  12446. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12447. depending on context. See the individual commands for more information."
  12448. (interactive "P")
  12449. (cond
  12450. ((and org-support-shift-select (org-region-active-p))
  12451. (org-call-for-shift-select 'previous-line))
  12452. ((org-at-timestamp-p t)
  12453. (call-interactively (if org-edit-timestamp-down-means-later
  12454. 'org-timestamp-down 'org-timestamp-up)))
  12455. ((and (not (eq org-support-shift-select 'always))
  12456. (org-on-heading-p))
  12457. (call-interactively 'org-priority-up))
  12458. ((and (not org-support-shift-select) (org-at-item-p))
  12459. (call-interactively 'org-previous-item))
  12460. ((org-clocktable-try-shift 'up arg))
  12461. (org-support-shift-select
  12462. (org-call-for-shift-select 'previous-line))
  12463. (t (org-shiftselect-error))))
  12464. (defun org-shiftdown (&optional arg)
  12465. "Decrease item in timestamp or decrease priority of current headline.
  12466. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12467. depending on context. See the individual commands for more information."
  12468. (interactive "P")
  12469. (cond
  12470. ((and org-support-shift-select (org-region-active-p))
  12471. (org-call-for-shift-select 'next-line))
  12472. ((org-at-timestamp-p t)
  12473. (call-interactively (if org-edit-timestamp-down-means-later
  12474. 'org-timestamp-up 'org-timestamp-down)))
  12475. ((and (not (eq org-support-shift-select 'always))
  12476. (org-on-heading-p))
  12477. (call-interactively 'org-priority-down))
  12478. ((and (not org-support-shift-select) (org-at-item-p))
  12479. (call-interactively 'org-next-item))
  12480. ((org-clocktable-try-shift 'down arg))
  12481. (org-support-shift-select
  12482. (org-call-for-shift-select 'next-line))
  12483. (t (org-shiftselect-error))))
  12484. (defun org-shiftright (&optional arg)
  12485. "Cycle the thing at point or in the current line, depending on context.
  12486. Depending on context, this does one of the following:
  12487. - switch a timestamp at point one day into the future
  12488. - on a headline, switch to the next TODO keyword.
  12489. - on an item, switch entire list to the next bullet type
  12490. - on a property line, switch to the next allowed value
  12491. - on a clocktable definition line, move time block into the future"
  12492. (interactive "P")
  12493. (cond
  12494. ((and org-support-shift-select (org-region-active-p))
  12495. (org-call-for-shift-select 'forward-char))
  12496. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12497. ((and (not (eq org-support-shift-select 'always))
  12498. (org-on-heading-p))
  12499. (org-call-with-arg 'org-todo 'right))
  12500. ((or (and org-support-shift-select
  12501. (not (eq org-support-shift-select 'always))
  12502. (org-at-item-bullet-p))
  12503. (and (not org-support-shift-select) (org-at-item-p)))
  12504. (org-call-with-arg 'org-cycle-list-bullet nil))
  12505. ((and (not (eq org-support-shift-select 'always))
  12506. (org-at-property-p))
  12507. (call-interactively 'org-property-next-allowed-value))
  12508. ((org-clocktable-try-shift 'right arg))
  12509. (org-support-shift-select
  12510. (org-call-for-shift-select 'forward-char))
  12511. (t (org-shiftselect-error))))
  12512. (defun org-shiftleft (&optional arg)
  12513. "Cycle the thing at point or in the current line, depending on context.
  12514. Depending on context, this does one of the following:
  12515. - switch a timestamp at point one day into the past
  12516. - on a headline, switch to the previous TODO keyword.
  12517. - on an item, switch entire list to the previous bullet type
  12518. - on a property line, switch to the previous allowed value
  12519. - on a clocktable definition line, move time block into the past"
  12520. (interactive "P")
  12521. (cond
  12522. ((and org-support-shift-select (org-region-active-p))
  12523. (org-call-for-shift-select 'backward-char))
  12524. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12525. ((and (not (eq org-support-shift-select 'always))
  12526. (org-on-heading-p))
  12527. (org-call-with-arg 'org-todo 'left))
  12528. ((or (and org-support-shift-select
  12529. (not (eq org-support-shift-select 'always))
  12530. (org-at-item-bullet-p))
  12531. (and (not org-support-shift-select) (org-at-item-p)))
  12532. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12533. ((and (not (eq org-support-shift-select 'always))
  12534. (org-at-property-p))
  12535. (call-interactively 'org-property-previous-allowed-value))
  12536. ((org-clocktable-try-shift 'left arg))
  12537. (org-support-shift-select
  12538. (org-call-for-shift-select 'backward-char))
  12539. (t (org-shiftselect-error))))
  12540. (defun org-shiftcontrolright ()
  12541. "Switch to next TODO set."
  12542. (interactive)
  12543. (cond
  12544. ((and org-support-shift-select (org-region-active-p))
  12545. (org-call-for-shift-select 'forward-word))
  12546. ((and (not (eq org-support-shift-select 'always))
  12547. (org-on-heading-p))
  12548. (org-call-with-arg 'org-todo 'nextset))
  12549. (org-support-shift-select
  12550. (org-call-for-shift-select 'forward-word))
  12551. (t (org-shiftselect-error))))
  12552. (defun org-shiftcontrolleft ()
  12553. "Switch to previous TODO set."
  12554. (interactive)
  12555. (cond
  12556. ((and org-support-shift-select (org-region-active-p))
  12557. (org-call-for-shift-select 'backward-word))
  12558. ((and (not (eq org-support-shift-select 'always))
  12559. (org-on-heading-p))
  12560. (org-call-with-arg 'org-todo 'previousset))
  12561. (org-support-shift-select
  12562. (org-call-for-shift-select 'backward-word))
  12563. (t (org-shiftselect-error))))
  12564. (defun org-ctrl-c-ret ()
  12565. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12566. (interactive)
  12567. (cond
  12568. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12569. (t (call-interactively 'org-insert-heading))))
  12570. (defun org-copy-special ()
  12571. "Copy region in table or copy current subtree.
  12572. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12573. See the individual commands for more information."
  12574. (interactive)
  12575. (call-interactively
  12576. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12577. (defun org-cut-special ()
  12578. "Cut region in table or cut current subtree.
  12579. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12580. See the individual commands for more information."
  12581. (interactive)
  12582. (call-interactively
  12583. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12584. (defun org-paste-special (arg)
  12585. "Paste rectangular region into table, or past subtree relative to level.
  12586. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12587. See the individual commands for more information."
  12588. (interactive "P")
  12589. (if (org-at-table-p)
  12590. (org-table-paste-rectangle)
  12591. (org-paste-subtree arg)))
  12592. (defun org-edit-special ()
  12593. "Call a special editor for the stuff at point.
  12594. When at a table, call the formula editor with `org-table-edit-formulas'.
  12595. When at the first line of an src example, call `org-edit-src-code'.
  12596. When in an #+include line, visit the include file. Otherwise call
  12597. `ffap' to visit the file at point."
  12598. (interactive)
  12599. (cond
  12600. ((org-at-table-p)
  12601. (call-interactively 'org-table-edit-formulas))
  12602. ((save-excursion
  12603. (beginning-of-line 1)
  12604. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12605. (find-file (org-trim (match-string 1))))
  12606. ((org-edit-src-code))
  12607. ((org-edit-fixed-width-region))
  12608. (t (call-interactively 'ffap))))
  12609. (defun org-ctrl-c-ctrl-c (&optional arg)
  12610. "Set tags in headline, or update according to changed information at point.
  12611. This command does many different things, depending on context:
  12612. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12613. this is what we do.
  12614. - If the cursor is in a headline, prompt for tags and insert them
  12615. into the current line, aligned to `org-tags-column'. When called
  12616. with prefix arg, realign all tags in the current buffer.
  12617. - If the cursor is in one of the special #+KEYWORD lines, this
  12618. triggers scanning the buffer for these lines and updating the
  12619. information.
  12620. - If the cursor is inside a table, realign the table. This command
  12621. works even if the automatic table editor has been turned off.
  12622. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12623. the entire table.
  12624. - If the cursor is at a footnote reference or definition, jump to
  12625. the corresponding definition or references, respectively.
  12626. - If the cursor is a the beginning of a dynamic block, update it.
  12627. - If the cursor is inside a table created by the table.el package,
  12628. activate that table.
  12629. - If the current buffer is a remember buffer, close note and file
  12630. it. A prefix argument of 1 files to the default location
  12631. without further interaction. A prefix argument of 2 files to
  12632. the currently clocking task.
  12633. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12634. links in this buffer.
  12635. - If the cursor is on a numbered item in a plain list, renumber the
  12636. ordered list.
  12637. - If the cursor is on a checkbox, toggle it."
  12638. (interactive "P")
  12639. (let ((org-enable-table-editor t))
  12640. (cond
  12641. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12642. org-occur-highlights
  12643. org-latex-fragment-image-overlays)
  12644. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12645. (org-remove-occur-highlights)
  12646. (org-remove-latex-fragment-image-overlays)
  12647. (message "Temporary highlights/overlays removed from current buffer"))
  12648. ((and (local-variable-p 'org-finish-function (current-buffer))
  12649. (fboundp org-finish-function))
  12650. (funcall org-finish-function))
  12651. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12652. ((org-at-property-p)
  12653. (call-interactively 'org-property-action))
  12654. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12655. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12656. ((org-at-table.el-p)
  12657. (require 'table)
  12658. (beginning-of-line 1)
  12659. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12660. (call-interactively 'table-recognize-table))
  12661. ((org-at-table-p)
  12662. (org-table-maybe-eval-formula)
  12663. (if arg
  12664. (call-interactively 'org-table-recalculate)
  12665. (org-table-maybe-recalculate-line))
  12666. (call-interactively 'org-table-align))
  12667. ((or (org-footnote-at-reference-p)
  12668. (org-footnote-at-definition-p))
  12669. (call-interactively 'org-footnote-action))
  12670. ((org-at-item-checkbox-p)
  12671. (call-interactively 'org-toggle-checkbox))
  12672. ((org-at-item-p)
  12673. (if arg
  12674. (call-interactively 'org-toggle-checkbox)
  12675. (call-interactively 'org-maybe-renumber-ordered-list)))
  12676. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12677. ;; Dynamic block
  12678. (beginning-of-line 1)
  12679. (save-excursion (org-update-dblock)))
  12680. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12681. (cond
  12682. ((equal (match-string 1) "TBLFM")
  12683. ;; Recalculate the table before this line
  12684. (save-excursion
  12685. (beginning-of-line 1)
  12686. (skip-chars-backward " \r\n\t")
  12687. (if (org-at-table-p)
  12688. (org-call-with-arg 'org-table-recalculate t))))
  12689. (t
  12690. ; (org-set-regexps-and-options)
  12691. ; (org-restart-font-lock)
  12692. (let ((org-inhibit-startup t)) (org-mode-restart))
  12693. (message "Local setup has been refreshed"))))
  12694. (t (error "C-c C-c can do nothing useful at this location.")))))
  12695. (defun org-mode-restart ()
  12696. "Restart Org-mode, to scan again for special lines.
  12697. Also updates the keyword regular expressions."
  12698. (interactive)
  12699. (org-mode)
  12700. (message "Org-mode restarted"))
  12701. (defun org-kill-note-or-show-branches ()
  12702. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12703. (interactive)
  12704. (if (not org-finish-function)
  12705. (call-interactively 'show-branches)
  12706. (let ((org-note-abort t))
  12707. (funcall org-finish-function))))
  12708. (defun org-return (&optional indent)
  12709. "Goto next table row or insert a newline.
  12710. Calls `org-table-next-row' or `newline', depending on context.
  12711. See the individual commands for more information."
  12712. (interactive)
  12713. (cond
  12714. ((bobp) (if indent (newline-and-indent) (newline)))
  12715. ((org-at-table-p)
  12716. (org-table-justify-field-maybe)
  12717. (call-interactively 'org-table-next-row))
  12718. ((and org-return-follows-link
  12719. (eq (get-text-property (point) 'face) 'org-link))
  12720. (call-interactively 'org-open-at-point))
  12721. ((and (org-at-heading-p)
  12722. (looking-at
  12723. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12724. (org-show-entry)
  12725. (end-of-line 1)
  12726. (newline))
  12727. (t (if indent (newline-and-indent) (newline)))))
  12728. (defun org-return-indent ()
  12729. "Goto next table row or insert a newline and indent.
  12730. Calls `org-table-next-row' or `newline-and-indent', depending on
  12731. context. See the individual commands for more information."
  12732. (interactive)
  12733. (org-return t))
  12734. (defun org-ctrl-c-star ()
  12735. "Compute table, or change heading status of lines.
  12736. Calls `org-table-recalculate' or `org-toggle-heading',
  12737. depending on context."
  12738. (interactive)
  12739. (cond
  12740. ((org-at-table-p)
  12741. (call-interactively 'org-table-recalculate))
  12742. (t
  12743. ;; Convert all lines in region to list items
  12744. (call-interactively 'org-toggle-heading))))
  12745. (defun org-ctrl-c-minus ()
  12746. "Insert separator line in table or modify bullet status of line.
  12747. Also turns a plain line or a region of lines into list items.
  12748. Calls `org-table-insert-hline', `org-toggle-item', or
  12749. `org-cycle-list-bullet', depending on context."
  12750. (interactive)
  12751. (cond
  12752. ((org-at-table-p)
  12753. (call-interactively 'org-table-insert-hline))
  12754. ((org-region-active-p)
  12755. (call-interactively 'org-toggle-item))
  12756. ((org-in-item-p)
  12757. (call-interactively 'org-cycle-list-bullet))
  12758. (t
  12759. (call-interactively 'org-toggle-item))))
  12760. (defun org-toggle-item ()
  12761. "Convert headings or normal lines to items, items to normal lines.
  12762. If there is no active region, only the current line is considered.
  12763. If the first line in the region is a headline, convert all headlines to items.
  12764. If the first line in the region is an item, convert all items to normal lines.
  12765. If the first line is normal text, add an item bullet to each line."
  12766. (interactive)
  12767. (let (l2 l beg end)
  12768. (if (org-region-active-p)
  12769. (setq beg (region-beginning) end (region-end))
  12770. (setq beg (point-at-bol)
  12771. end (min (1+ (point-at-eol)) (point-max))))
  12772. (save-excursion
  12773. (goto-char end)
  12774. (setq l2 (org-current-line))
  12775. (goto-char beg)
  12776. (beginning-of-line 1)
  12777. (setq l (1- (org-current-line)))
  12778. (if (org-at-item-p)
  12779. ;; We already have items, de-itemize
  12780. (while (< (setq l (1+ l)) l2)
  12781. (when (org-at-item-p)
  12782. (goto-char (match-beginning 2))
  12783. (delete-region (match-beginning 2) (match-end 2))
  12784. (and (looking-at "[ \t]+") (replace-match "")))
  12785. (beginning-of-line 2))
  12786. (if (org-on-heading-p)
  12787. ;; Headings, convert to items
  12788. (while (< (setq l (1+ l)) l2)
  12789. (if (looking-at org-outline-regexp)
  12790. (replace-match "- " t t))
  12791. (beginning-of-line 2))
  12792. ;; normal lines, turn them into items
  12793. (while (< (setq l (1+ l)) l2)
  12794. (unless (org-at-item-p)
  12795. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12796. (replace-match "\\1- \\2")))
  12797. (beginning-of-line 2)))))))
  12798. (defun org-toggle-heading (&optional nstars)
  12799. "Convert headings to normal text, or items or text to headings.
  12800. If there is no active region, only the current line is considered.
  12801. If the first line is a heading, remove the stars from all headlines
  12802. in the region.
  12803. If the first line is a plain list item, turn all plain list items into
  12804. headings.
  12805. If the first line is a normal line, turn each and every line in the region
  12806. into a heading.
  12807. When converting a line into a heading, the number of stars is chosen
  12808. such that the lines become children of the current entry. However, when
  12809. a prefix argument is given, its value determines the number of stars to add."
  12810. (interactive "P")
  12811. (let (l2 l itemp beg end)
  12812. (if (org-region-active-p)
  12813. (setq beg (region-beginning) end (region-end))
  12814. (setq beg (point-at-bol)
  12815. end (min (1+ (point-at-eol)) (point-max))))
  12816. (save-excursion
  12817. (goto-char end)
  12818. (setq l2 (org-current-line))
  12819. (goto-char beg)
  12820. (beginning-of-line 1)
  12821. (setq l (1- (org-current-line)))
  12822. (if (org-on-heading-p)
  12823. ;; We already have headlines, de-star them
  12824. (while (< (setq l (1+ l)) l2)
  12825. (when (org-on-heading-p t)
  12826. (and (looking-at outline-regexp) (replace-match "")))
  12827. (beginning-of-line 2))
  12828. (setq itemp (org-at-item-p))
  12829. (let* ((stars
  12830. (if nstars
  12831. (make-string (prefix-numeric-value current-prefix-arg)
  12832. ?*)
  12833. (save-excursion
  12834. (re-search-backward org-complex-heading-regexp nil t)
  12835. (or (match-string 1) "*"))))
  12836. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12837. (rpl (concat stars add-stars " ")))
  12838. (while (< (setq l (1+ l)) l2)
  12839. (if itemp
  12840. (and (org-at-item-p) (replace-match rpl t t))
  12841. (unless (org-on-heading-p)
  12842. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12843. (replace-match (concat rpl (match-string 2))))))
  12844. (beginning-of-line 2)))))))
  12845. (defun org-meta-return (&optional arg)
  12846. "Insert a new heading or wrap a region in a table.
  12847. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12848. See the individual commands for more information."
  12849. (interactive "P")
  12850. (cond
  12851. ((run-hook-with-args-until-success 'org-metareturn-hook))
  12852. ((org-at-table-p)
  12853. (call-interactively 'org-table-wrap-region))
  12854. (t (call-interactively 'org-insert-heading))))
  12855. ;;; Menu entries
  12856. ;; Define the Org-mode menus
  12857. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12858. '("Tbl"
  12859. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12860. ["Next Field" org-cycle (org-at-table-p)]
  12861. ["Previous Field" org-shifttab (org-at-table-p)]
  12862. ["Next Row" org-return (org-at-table-p)]
  12863. "--"
  12864. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12865. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12866. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12867. "--"
  12868. ("Column"
  12869. ["Move Column Left" org-metaleft (org-at-table-p)]
  12870. ["Move Column Right" org-metaright (org-at-table-p)]
  12871. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12872. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12873. ("Row"
  12874. ["Move Row Up" org-metaup (org-at-table-p)]
  12875. ["Move Row Down" org-metadown (org-at-table-p)]
  12876. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12877. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12878. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12879. "--"
  12880. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12881. ("Rectangle"
  12882. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12883. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12884. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12885. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12886. "--"
  12887. ("Calculate"
  12888. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12889. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12890. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12891. "--"
  12892. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12893. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12894. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12895. "--"
  12896. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12897. "--"
  12898. ["Sum Column/Rectangle" org-table-sum
  12899. (or (org-at-table-p) (org-region-active-p))]
  12900. ["Which Column?" org-table-current-column (org-at-table-p)])
  12901. ["Debug Formulas"
  12902. org-table-toggle-formula-debugger
  12903. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12904. ["Show Col/Row Numbers"
  12905. org-table-toggle-coordinate-overlays
  12906. :style toggle
  12907. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12908. "--"
  12909. ["Create" org-table-create (and (not (org-at-table-p))
  12910. org-enable-table-editor)]
  12911. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12912. ["Import from File" org-table-import (not (org-at-table-p))]
  12913. ["Export to File" org-table-export (org-at-table-p)]
  12914. "--"
  12915. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12916. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12917. '("Org"
  12918. ("Show/Hide"
  12919. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12920. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12921. ["Sparse Tree..." org-sparse-tree t]
  12922. ["Reveal Context" org-reveal t]
  12923. ["Show All" show-all t]
  12924. "--"
  12925. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12926. "--"
  12927. ["New Heading" org-insert-heading t]
  12928. ("Navigate Headings"
  12929. ["Up" outline-up-heading t]
  12930. ["Next" outline-next-visible-heading t]
  12931. ["Previous" outline-previous-visible-heading t]
  12932. ["Next Same Level" outline-forward-same-level t]
  12933. ["Previous Same Level" outline-backward-same-level t]
  12934. "--"
  12935. ["Jump" org-goto t])
  12936. ("Edit Structure"
  12937. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12938. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12939. "--"
  12940. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12941. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12942. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12943. "--"
  12944. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12945. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12946. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12947. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12948. "--"
  12949. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12950. "--"
  12951. ["Convert to odd levels" org-convert-to-odd-levels t]
  12952. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12953. ("Editing"
  12954. ["Emphasis..." org-emphasize t]
  12955. ["Edit Source Example" org-edit-special t]
  12956. "--"
  12957. ["Footnote new/jump" org-footnote-action t]
  12958. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12959. ("Archive"
  12960. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12961. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12962. ; :active t :keys "C-u C-c C-x C-a"]
  12963. ["Sparse trees open ARCHIVE trees"
  12964. (setq org-sparse-tree-open-archived-trees
  12965. (not org-sparse-tree-open-archived-trees))
  12966. :style toggle :selected org-sparse-tree-open-archived-trees]
  12967. ["Cycling opens ARCHIVE trees"
  12968. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12969. :style toggle :selected org-cycle-open-archived-trees]
  12970. "--"
  12971. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12972. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12973. ; ["Check and Move Children" (org-archive-subtree '(4))
  12974. ; :active t :keys "C-u C-c C-x C-s"]
  12975. )
  12976. "--"
  12977. ("TODO Lists"
  12978. ["TODO/DONE/-" org-todo t]
  12979. ("Select keyword"
  12980. ["Next keyword" org-shiftright (org-on-heading-p)]
  12981. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12982. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12983. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12984. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12985. ["Show TODO Tree" org-show-todo-tree t]
  12986. ["Global TODO list" org-todo-list t]
  12987. "--"
  12988. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12989. :selected org-enforce-todo-dependencies :style toggle :active t]
  12990. "Settings for tree at point"
  12991. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12992. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12993. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12994. ["Do Children parallel" org-toggle-ordered-property :style radio
  12995. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12996. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12997. "--"
  12998. ["Set Priority" org-priority t]
  12999. ["Priority Up" org-shiftup t]
  13000. ["Priority Down" org-shiftdown t])
  13001. ("TAGS and Properties"
  13002. ["Set Tags" org-set-tags-command t]
  13003. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13004. "--"
  13005. ["Set property" org-set-property t]
  13006. ["Column view of properties" org-columns t]
  13007. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13008. ("Dates and Scheduling"
  13009. ["Timestamp" org-time-stamp t]
  13010. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13011. ("Change Date"
  13012. ["1 Day Later" org-shiftright t]
  13013. ["1 Day Earlier" org-shiftleft t]
  13014. ["1 ... Later" org-shiftup t]
  13015. ["1 ... Earlier" org-shiftdown t])
  13016. ["Compute Time Range" org-evaluate-time-range t]
  13017. ["Schedule Item" org-schedule t]
  13018. ["Deadline" org-deadline t]
  13019. "--"
  13020. ["Custom time format" org-toggle-time-stamp-overlays
  13021. :style radio :selected org-display-custom-times]
  13022. "--"
  13023. ["Goto Calendar" org-goto-calendar t]
  13024. ["Date from Calendar" org-date-from-calendar t]
  13025. "--"
  13026. ["Start/Restart Timer" org-timer-start t]
  13027. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13028. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13029. ["Insert Timer String" org-timer t]
  13030. ["Insert Timer Item" org-timer-item t])
  13031. ("Logging work"
  13032. ["Clock in" org-clock-in t]
  13033. ["Clock out" org-clock-out t]
  13034. ["Clock cancel" org-clock-cancel t]
  13035. ["Goto running clock" org-clock-goto t]
  13036. ["Display times" org-clock-display t]
  13037. ["Create clock table" org-clock-report t]
  13038. "--"
  13039. ["Record DONE time"
  13040. (progn (setq org-log-done (not org-log-done))
  13041. (message "Switching to %s will %s record a timestamp"
  13042. (car org-done-keywords)
  13043. (if org-log-done "automatically" "not")))
  13044. :style toggle :selected org-log-done])
  13045. "--"
  13046. ["Agenda Command..." org-agenda t]
  13047. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13048. ("File List for Agenda")
  13049. ("Special views current file"
  13050. ["TODO Tree" org-show-todo-tree t]
  13051. ["Check Deadlines" org-check-deadlines t]
  13052. ["Timeline" org-timeline t]
  13053. ["Tags Tree" org-tags-sparse-tree t])
  13054. "--"
  13055. ("Hyperlinks"
  13056. ["Store Link (Global)" org-store-link t]
  13057. ["Insert Link" org-insert-link t]
  13058. ["Follow Link" org-open-at-point t]
  13059. "--"
  13060. ["Next link" org-next-link t]
  13061. ["Previous link" org-previous-link t]
  13062. "--"
  13063. ["Descriptive Links"
  13064. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13065. :style radio
  13066. :selected (member '(org-link) buffer-invisibility-spec)]
  13067. ["Literal Links"
  13068. (progn
  13069. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13070. :style radio
  13071. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13072. "--"
  13073. ["Export/Publish..." org-export t]
  13074. ("LaTeX"
  13075. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13076. :selected org-cdlatex-mode]
  13077. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13078. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13079. ["Modify math symbol" org-cdlatex-math-modify
  13080. (org-inside-LaTeX-fragment-p)]
  13081. ["Export LaTeX fragments as images"
  13082. (if (featurep 'org-exp)
  13083. (setq org-export-with-LaTeX-fragments
  13084. (not org-export-with-LaTeX-fragments))
  13085. (require 'org-exp))
  13086. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13087. org-export-with-LaTeX-fragments)])
  13088. "--"
  13089. ("Documentation"
  13090. ["Show Version" org-version t]
  13091. ["Info Documentation" org-info t])
  13092. ("Customize"
  13093. ["Browse Org Group" org-customize t]
  13094. "--"
  13095. ["Expand This Menu" org-create-customize-menu
  13096. (fboundp 'customize-menu-create)])
  13097. "--"
  13098. ("Refresh/Reload"
  13099. ["Refresh setup current buffer" org-mode-restart t]
  13100. ["Reload Org (after update)" org-reload t]
  13101. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13102. ))
  13103. (defun org-info (&optional node)
  13104. "Read documentation for Org-mode in the info system.
  13105. With optional NODE, go directly to that node."
  13106. (interactive)
  13107. (info (format "(org)%s" (or node ""))))
  13108. (defun org-install-agenda-files-menu ()
  13109. (let ((bl (buffer-list)))
  13110. (save-excursion
  13111. (while bl
  13112. (set-buffer (pop bl))
  13113. (if (org-mode-p) (setq bl nil)))
  13114. (when (org-mode-p)
  13115. (easy-menu-change
  13116. '("Org") "File List for Agenda"
  13117. (append
  13118. (list
  13119. ["Edit File List" (org-edit-agenda-file-list) t]
  13120. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13121. ["Remove Current File from List" org-remove-file t]
  13122. ["Cycle through agenda files" org-cycle-agenda-files t]
  13123. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13124. "--")
  13125. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13126. ;;;; Documentation
  13127. ;;;###autoload
  13128. (defun org-require-autoloaded-modules ()
  13129. (interactive)
  13130. (mapc 'require
  13131. '(org-agenda org-archive org-attach org-clock org-colview
  13132. org-exp org-id org-export-latex org-publish
  13133. org-remember org-table org-timer)))
  13134. ;;;###autoload
  13135. (defun org-reload (&optional uncompiled)
  13136. "Reload all org lisp files.
  13137. With prefix arg UNCOMPILED, load the uncompiled versions."
  13138. (interactive "P")
  13139. (require 'find-func)
  13140. (let* ((dir (file-name-directory (find-library-name "org")))
  13141. (files (directory-files dir t "\\.el\\'"))
  13142. (remove-re (concat (if (featurep 'xemacs)
  13143. "org-colview" "org-colview-xemacs")
  13144. "\\'")))
  13145. (setq files (mapcar 'file-name-sans-extension files))
  13146. (setq files (mapcar
  13147. (lambda (x) (if (string-match remove-re x) nil x))
  13148. files))
  13149. (setq files (delq nil files))
  13150. (mapc
  13151. (lambda (f)
  13152. (if (and (not uncompiled)
  13153. (file-exists-p (concat f ".elc")))
  13154. (load (concat f ".elc") nil nil t)
  13155. (load (concat f ".el") nil nil t)))
  13156. files)))
  13157. ;;;###autoload
  13158. (defun org-customize ()
  13159. "Call the customize function with org as argument."
  13160. (interactive)
  13161. (org-load-modules-maybe)
  13162. (org-require-autoloaded-modules)
  13163. (customize-browse 'org))
  13164. (defun org-create-customize-menu ()
  13165. "Create a full customization menu for Org-mode, insert it into the menu."
  13166. (interactive)
  13167. (org-load-modules-maybe)
  13168. (org-require-autoloaded-modules)
  13169. (if (fboundp 'customize-menu-create)
  13170. (progn
  13171. (easy-menu-change
  13172. '("Org") "Customize"
  13173. `(["Browse Org group" org-customize t]
  13174. "--"
  13175. ,(customize-menu-create 'org)
  13176. ["Set" Custom-set t]
  13177. ["Save" Custom-save t]
  13178. ["Reset to Current" Custom-reset-current t]
  13179. ["Reset to Saved" Custom-reset-saved t]
  13180. ["Reset to Standard Settings" Custom-reset-standard t]))
  13181. (message "\"Org\"-menu now contains full customization menu"))
  13182. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13183. ;;;; Miscellaneous stuff
  13184. ;;; Generally useful functions
  13185. (defun org-find-text-property-in-string (prop s)
  13186. "Return the first non-nil value of property PROP in string S."
  13187. (or (get-text-property 0 prop s)
  13188. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13189. prop s)))
  13190. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13191. "Display the given MESSAGE as a warning."
  13192. (if (fboundp 'display-warning)
  13193. (display-warning 'org message
  13194. (if (featurep 'xemacs)
  13195. 'warning
  13196. :warning))
  13197. (let ((buf (get-buffer-create "*Org warnings*")))
  13198. (with-current-buffer buf
  13199. (goto-char (point-max))
  13200. (insert "Warning (Org): " message)
  13201. (unless (bolp)
  13202. (newline)))
  13203. (display-buffer buf)
  13204. (sit-for 0))))
  13205. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13206. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13207. (if (and marker (marker-buffer marker)
  13208. (buffer-live-p (marker-buffer marker)))
  13209. (progn
  13210. (switch-to-buffer (marker-buffer marker))
  13211. (if (or (> marker (point-max)) (< marker (point-min)))
  13212. (widen))
  13213. (goto-char marker)
  13214. (org-show-context 'org-goto))
  13215. (if bookmark
  13216. (bookmark-jump bookmark)
  13217. (error "Cannot find location"))))
  13218. (defun org-quote-csv-field (s)
  13219. "Quote field for inclusion in CSV material."
  13220. (if (string-match "[\",]" s)
  13221. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13222. s))
  13223. (defun org-plist-delete (plist property)
  13224. "Delete PROPERTY from PLIST.
  13225. This is in contrast to merely setting it to 0."
  13226. (let (p)
  13227. (while plist
  13228. (if (not (eq property (car plist)))
  13229. (setq p (plist-put p (car plist) (nth 1 plist))))
  13230. (setq plist (cddr plist)))
  13231. p))
  13232. (defun org-force-self-insert (N)
  13233. "Needed to enforce self-insert under remapping."
  13234. (interactive "p")
  13235. (self-insert-command N))
  13236. (defun org-string-width (s)
  13237. "Compute width of string, ignoring invisible characters.
  13238. This ignores character with invisibility property `org-link', and also
  13239. characters with property `org-cwidth', because these will become invisible
  13240. upon the next fontification round."
  13241. (let (b l)
  13242. (when (or (eq t buffer-invisibility-spec)
  13243. (assq 'org-link buffer-invisibility-spec))
  13244. (while (setq b (text-property-any 0 (length s)
  13245. 'invisible 'org-link s))
  13246. (setq s (concat (substring s 0 b)
  13247. (substring s (or (next-single-property-change
  13248. b 'invisible s) (length s)))))))
  13249. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13250. (setq s (concat (substring s 0 b)
  13251. (substring s (or (next-single-property-change
  13252. b 'org-cwidth s) (length s))))))
  13253. (setq l (string-width s) b -1)
  13254. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13255. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13256. l))
  13257. (defun org-get-indentation (&optional line)
  13258. "Get the indentation of the current line, interpreting tabs.
  13259. When LINE is given, assume it represents a line and compute its indentation."
  13260. (if line
  13261. (if (string-match "^ *" (org-remove-tabs line))
  13262. (match-end 0))
  13263. (save-excursion
  13264. (beginning-of-line 1)
  13265. (skip-chars-forward " \t")
  13266. (current-column))))
  13267. (defun org-remove-tabs (s &optional width)
  13268. "Replace tabulators in S with spaces.
  13269. Assumes that s is a single line, starting in column 0."
  13270. (setq width (or width tab-width))
  13271. (while (string-match "\t" s)
  13272. (setq s (replace-match
  13273. (make-string
  13274. (- (* width (/ (+ (match-beginning 0) width) width))
  13275. (match-beginning 0)) ?\ )
  13276. t t s)))
  13277. s)
  13278. (defun org-fix-indentation (line ind)
  13279. "Fix indentation in LINE.
  13280. IND is a cons cell with target and minimum indentation.
  13281. If the current indentation in LINE is smaller than the minimum,
  13282. leave it alone. If it is larger than ind, set it to the target."
  13283. (let* ((l (org-remove-tabs line))
  13284. (i (org-get-indentation l))
  13285. (i1 (car ind)) (i2 (cdr ind)))
  13286. (if (>= i i2) (setq l (substring line i2)))
  13287. (if (> i1 0)
  13288. (concat (make-string i1 ?\ ) l)
  13289. l)))
  13290. (defun org-base-buffer (buffer)
  13291. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13292. (if (not buffer)
  13293. buffer
  13294. (or (buffer-base-buffer buffer)
  13295. buffer)))
  13296. (defun org-trim (s)
  13297. "Remove whitespace at beginning and end of string."
  13298. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13299. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13300. s)
  13301. (defun org-wrap (string &optional width lines)
  13302. "Wrap string to either a number of lines, or a width in characters.
  13303. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13304. that costs. If there is a word longer than WIDTH, the text is actually
  13305. wrapped to the length of that word.
  13306. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13307. many lines, whatever width that takes.
  13308. The return value is a list of lines, without newlines at the end."
  13309. (let* ((words (org-split-string string "[ \t\n]+"))
  13310. (maxword (apply 'max (mapcar 'org-string-width words)))
  13311. w ll)
  13312. (cond (width
  13313. (org-do-wrap words (max maxword width)))
  13314. (lines
  13315. (setq w maxword)
  13316. (setq ll (org-do-wrap words maxword))
  13317. (if (<= (length ll) lines)
  13318. ll
  13319. (setq ll words)
  13320. (while (> (length ll) lines)
  13321. (setq w (1+ w))
  13322. (setq ll (org-do-wrap words w)))
  13323. ll))
  13324. (t (error "Cannot wrap this")))))
  13325. (defun org-do-wrap (words width)
  13326. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13327. (let (lines line)
  13328. (while words
  13329. (setq line (pop words))
  13330. (while (and words (< (+ (length line) (length (car words))) width))
  13331. (setq line (concat line " " (pop words))))
  13332. (setq lines (push line lines)))
  13333. (nreverse lines)))
  13334. (defun org-split-string (string &optional separators)
  13335. "Splits STRING into substrings at SEPARATORS.
  13336. No empty strings are returned if there are matches at the beginning
  13337. and end of string."
  13338. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13339. (start 0)
  13340. notfirst
  13341. (list nil))
  13342. (while (and (string-match rexp string
  13343. (if (and notfirst
  13344. (= start (match-beginning 0))
  13345. (< start (length string)))
  13346. (1+ start) start))
  13347. (< (match-beginning 0) (length string)))
  13348. (setq notfirst t)
  13349. (or (eq (match-beginning 0) 0)
  13350. (and (eq (match-beginning 0) (match-end 0))
  13351. (eq (match-beginning 0) start))
  13352. (setq list
  13353. (cons (substring string start (match-beginning 0))
  13354. list)))
  13355. (setq start (match-end 0)))
  13356. (or (eq start (length string))
  13357. (setq list
  13358. (cons (substring string start)
  13359. list)))
  13360. (nreverse list)))
  13361. (defun org-context ()
  13362. "Return a list of contexts of the current cursor position.
  13363. If several contexts apply, all are returned.
  13364. Each context entry is a list with a symbol naming the context, and
  13365. two positions indicating start and end of the context. Possible
  13366. contexts are:
  13367. :headline anywhere in a headline
  13368. :headline-stars on the leading stars in a headline
  13369. :todo-keyword on a TODO keyword (including DONE) in a headline
  13370. :tags on the TAGS in a headline
  13371. :priority on the priority cookie in a headline
  13372. :item on the first line of a plain list item
  13373. :item-bullet on the bullet/number of a plain list item
  13374. :checkbox on the checkbox in a plain list item
  13375. :table in an org-mode table
  13376. :table-special on a special filed in a table
  13377. :table-table in a table.el table
  13378. :link on a hyperlink
  13379. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13380. :target on a <<target>>
  13381. :radio-target on a <<<radio-target>>>
  13382. :latex-fragment on a LaTeX fragment
  13383. :latex-preview on a LaTeX fragment with overlayed preview image
  13384. This function expects the position to be visible because it uses font-lock
  13385. faces as a help to recognize the following contexts: :table-special, :link,
  13386. and :keyword."
  13387. (let* ((f (get-text-property (point) 'face))
  13388. (faces (if (listp f) f (list f)))
  13389. (p (point)) clist o)
  13390. ;; First the large context
  13391. (cond
  13392. ((org-on-heading-p t)
  13393. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13394. (when (progn
  13395. (beginning-of-line 1)
  13396. (looking-at org-todo-line-tags-regexp))
  13397. (push (org-point-in-group p 1 :headline-stars) clist)
  13398. (push (org-point-in-group p 2 :todo-keyword) clist)
  13399. (push (org-point-in-group p 4 :tags) clist))
  13400. (goto-char p)
  13401. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13402. (if (looking-at "\\[#[A-Z0-9]\\]")
  13403. (push (org-point-in-group p 0 :priority) clist)))
  13404. ((org-at-item-p)
  13405. (push (org-point-in-group p 2 :item-bullet) clist)
  13406. (push (list :item (point-at-bol)
  13407. (save-excursion (org-end-of-item) (point)))
  13408. clist)
  13409. (and (org-at-item-checkbox-p)
  13410. (push (org-point-in-group p 0 :checkbox) clist)))
  13411. ((org-at-table-p)
  13412. (push (list :table (org-table-begin) (org-table-end)) clist)
  13413. (if (memq 'org-formula faces)
  13414. (push (list :table-special
  13415. (previous-single-property-change p 'face)
  13416. (next-single-property-change p 'face)) clist)))
  13417. ((org-at-table-p 'any)
  13418. (push (list :table-table) clist)))
  13419. (goto-char p)
  13420. ;; Now the small context
  13421. (cond
  13422. ((org-at-timestamp-p)
  13423. (push (org-point-in-group p 0 :timestamp) clist))
  13424. ((memq 'org-link faces)
  13425. (push (list :link
  13426. (previous-single-property-change p 'face)
  13427. (next-single-property-change p 'face)) clist))
  13428. ((memq 'org-special-keyword faces)
  13429. (push (list :keyword
  13430. (previous-single-property-change p 'face)
  13431. (next-single-property-change p 'face)) clist))
  13432. ((org-on-target-p)
  13433. (push (org-point-in-group p 0 :target) clist)
  13434. (goto-char (1- (match-beginning 0)))
  13435. (if (looking-at org-radio-target-regexp)
  13436. (push (org-point-in-group p 0 :radio-target) clist))
  13437. (goto-char p))
  13438. ((setq o (car (delq nil
  13439. (mapcar
  13440. (lambda (x)
  13441. (if (memq x org-latex-fragment-image-overlays) x))
  13442. (org-overlays-at (point))))))
  13443. (push (list :latex-fragment
  13444. (org-overlay-start o) (org-overlay-end o)) clist)
  13445. (push (list :latex-preview
  13446. (org-overlay-start o) (org-overlay-end o)) clist))
  13447. ((org-inside-LaTeX-fragment-p)
  13448. ;; FIXME: positions wrong.
  13449. (push (list :latex-fragment (point) (point)) clist)))
  13450. (setq clist (nreverse (delq nil clist)))
  13451. clist))
  13452. ;; FIXME: Compare with at-regexp-p Do we need both?
  13453. (defun org-in-regexp (re &optional nlines visually)
  13454. "Check if point is inside a match of regexp.
  13455. Normally only the current line is checked, but you can include NLINES extra
  13456. lines both before and after point into the search.
  13457. If VISUALLY is set, require that the cursor is not after the match but
  13458. really on, so that the block visually is on the match."
  13459. (catch 'exit
  13460. (let ((pos (point))
  13461. (eol (point-at-eol (+ 1 (or nlines 0))))
  13462. (inc (if visually 1 0)))
  13463. (save-excursion
  13464. (beginning-of-line (- 1 (or nlines 0)))
  13465. (while (re-search-forward re eol t)
  13466. (if (and (<= (match-beginning 0) pos)
  13467. (>= (+ inc (match-end 0)) pos))
  13468. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13469. (defun org-at-regexp-p (regexp)
  13470. "Is point inside a match of REGEXP in the current line?"
  13471. (catch 'exit
  13472. (save-excursion
  13473. (let ((pos (point)) (end (point-at-eol)))
  13474. (beginning-of-line 1)
  13475. (while (re-search-forward regexp end t)
  13476. (if (and (<= (match-beginning 0) pos)
  13477. (>= (match-end 0) pos))
  13478. (throw 'exit t)))
  13479. nil))))
  13480. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13481. "Call `multi-occur' with buffers for all agenda files."
  13482. (interactive "sOrg-files matching: \np")
  13483. (let* ((files (org-agenda-files))
  13484. (tnames (mapcar 'file-truename files))
  13485. (extra org-agenda-text-search-extra-files)
  13486. f)
  13487. (when (eq (car extra) 'agenda-archives)
  13488. (setq extra (cdr extra))
  13489. (setq files (org-add-archive-files files)))
  13490. (while (setq f (pop extra))
  13491. (unless (member (file-truename f) tnames)
  13492. (add-to-list 'files f 'append)
  13493. (add-to-list 'tnames (file-truename f) 'append)))
  13494. (multi-occur
  13495. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13496. regexp)))
  13497. (if (boundp 'occur-mode-find-occurrence-hook)
  13498. ;; Emacs 23
  13499. (add-hook 'occur-mode-find-occurrence-hook
  13500. (lambda ()
  13501. (when (org-mode-p)
  13502. (org-reveal))))
  13503. ;; Emacs 22
  13504. (defadvice occur-mode-goto-occurrence
  13505. (after org-occur-reveal activate)
  13506. (and (org-mode-p) (org-reveal)))
  13507. (defadvice occur-mode-goto-occurrence-other-window
  13508. (after org-occur-reveal activate)
  13509. (and (org-mode-p) (org-reveal)))
  13510. (defadvice occur-mode-display-occurrence
  13511. (after org-occur-reveal activate)
  13512. (when (org-mode-p)
  13513. (let ((pos (occur-mode-find-occurrence)))
  13514. (with-current-buffer (marker-buffer pos)
  13515. (save-excursion
  13516. (goto-char pos)
  13517. (org-reveal)))))))
  13518. (defun org-uniquify (list)
  13519. "Remove duplicate elements from LIST."
  13520. (let (res)
  13521. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13522. res))
  13523. (defun org-delete-all (elts list)
  13524. "Remove all elements in ELTS from LIST."
  13525. (while elts
  13526. (setq list (delete (pop elts) list)))
  13527. list)
  13528. (defun org-back-over-empty-lines ()
  13529. "Move backwards over whitespace, to the beginning of the first empty line.
  13530. Returns the number of empty lines passed."
  13531. (let ((pos (point)))
  13532. (skip-chars-backward " \t\n\r")
  13533. (beginning-of-line 2)
  13534. (goto-char (min (point) pos))
  13535. (count-lines (point) pos)))
  13536. (defun org-skip-whitespace ()
  13537. (skip-chars-forward " \t\n\r"))
  13538. (defun org-point-in-group (point group &optional context)
  13539. "Check if POINT is in match-group GROUP.
  13540. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13541. match. If the match group does ot exist or point is not inside it,
  13542. return nil."
  13543. (and (match-beginning group)
  13544. (>= point (match-beginning group))
  13545. (<= point (match-end group))
  13546. (if context
  13547. (list context (match-beginning group) (match-end group))
  13548. t)))
  13549. (defun org-switch-to-buffer-other-window (&rest args)
  13550. "Switch to buffer in a second window on the current frame.
  13551. In particular, do not allow pop-up frames."
  13552. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13553. special-display-function)
  13554. (apply 'switch-to-buffer-other-window args)))
  13555. (defun org-combine-plists (&rest plists)
  13556. "Create a single property list from all plists in PLISTS.
  13557. The process starts by copying the first list, and then setting properties
  13558. from the other lists. Settings in the last list are the most significant
  13559. ones and overrule settings in the other lists."
  13560. (let ((rtn (copy-sequence (pop plists)))
  13561. p v ls)
  13562. (while plists
  13563. (setq ls (pop plists))
  13564. (while ls
  13565. (setq p (pop ls) v (pop ls))
  13566. (setq rtn (plist-put rtn p v))))
  13567. rtn))
  13568. (defun org-move-line-down (arg)
  13569. "Move the current line down. With prefix argument, move it past ARG lines."
  13570. (interactive "p")
  13571. (let ((col (current-column))
  13572. beg end pos)
  13573. (beginning-of-line 1) (setq beg (point))
  13574. (beginning-of-line 2) (setq end (point))
  13575. (beginning-of-line (+ 1 arg))
  13576. (setq pos (move-marker (make-marker) (point)))
  13577. (insert (delete-and-extract-region beg end))
  13578. (goto-char pos)
  13579. (org-move-to-column col)))
  13580. (defun org-move-line-up (arg)
  13581. "Move the current line up. With prefix argument, move it past ARG lines."
  13582. (interactive "p")
  13583. (let ((col (current-column))
  13584. beg end pos)
  13585. (beginning-of-line 1) (setq beg (point))
  13586. (beginning-of-line 2) (setq end (point))
  13587. (beginning-of-line (- arg))
  13588. (setq pos (move-marker (make-marker) (point)))
  13589. (insert (delete-and-extract-region beg end))
  13590. (goto-char pos)
  13591. (org-move-to-column col)))
  13592. (defun org-replace-escapes (string table)
  13593. "Replace %-escapes in STRING with values in TABLE.
  13594. TABLE is an association list with keys like \"%a\" and string values.
  13595. The sequences in STRING may contain normal field width and padding information,
  13596. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13597. so values can contain further %-escapes if they are define later in TABLE."
  13598. (let ((case-fold-search nil)
  13599. e re rpl)
  13600. (while (setq e (pop table))
  13601. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13602. (while (string-match re string)
  13603. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13604. (cdr e)))
  13605. (setq string (replace-match rpl t t string))))
  13606. string))
  13607. (defun org-sublist (list start end)
  13608. "Return a section of LIST, from START to END.
  13609. Counting starts at 1."
  13610. (let (rtn (c start))
  13611. (setq list (nthcdr (1- start) list))
  13612. (while (and list (<= c end))
  13613. (push (pop list) rtn)
  13614. (setq c (1+ c)))
  13615. (nreverse rtn)))
  13616. (defun org-find-base-buffer-visiting (file)
  13617. "Like `find-buffer-visiting' but alway return the base buffer and
  13618. not an indirect buffer."
  13619. (let ((buf (find-buffer-visiting file)))
  13620. (if buf
  13621. (or (buffer-base-buffer buf) buf)
  13622. nil)))
  13623. (defun org-image-file-name-regexp (&optional extensions)
  13624. "Return regexp matching the file names of images.
  13625. If EXTENSIONS is given, only match these."
  13626. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13627. (image-file-name-regexp)
  13628. (let ((image-file-name-extensions
  13629. (or extensions
  13630. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13631. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13632. (concat "\\."
  13633. (regexp-opt (nconc (mapcar 'upcase
  13634. image-file-name-extensions)
  13635. image-file-name-extensions)
  13636. t)
  13637. "\\'"))))
  13638. (defun org-file-image-p (file &optional extensions)
  13639. "Return non-nil if FILE is an image."
  13640. (save-match-data
  13641. (string-match (org-image-file-name-regexp extensions) file)))
  13642. (defun org-get-cursor-date ()
  13643. "Return the date at cursor in as a time.
  13644. This works in the calendar and in the agenda, anywhere else it just
  13645. returns the current time."
  13646. (let (date day defd)
  13647. (cond
  13648. ((eq major-mode 'calendar-mode)
  13649. (setq date (calendar-cursor-to-date)
  13650. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13651. ((eq major-mode 'org-agenda-mode)
  13652. (setq day (get-text-property (point) 'day))
  13653. (if day
  13654. (setq date (calendar-gregorian-from-absolute day)
  13655. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13656. (nth 2 date))))))
  13657. (or defd (current-time))))
  13658. (defvar org-agenda-action-marker (make-marker)
  13659. "Marker pointing to the entry for the next agenda action.")
  13660. (defun org-mark-entry-for-agenda-action ()
  13661. "Mark the current entry as target of an agenda action.
  13662. Agenda actions are actions executed from the agenda with the key `k',
  13663. which make use of the date at the cursor."
  13664. (interactive)
  13665. (move-marker org-agenda-action-marker
  13666. (save-excursion (org-back-to-heading t) (point))
  13667. (current-buffer))
  13668. (message
  13669. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13670. ;;; Paragraph filling stuff.
  13671. ;; We want this to be just right, so use the full arsenal.
  13672. (defun org-indent-line-function ()
  13673. "Indent line like previous, but further if previous was headline or item."
  13674. (interactive)
  13675. (let* ((pos (point))
  13676. (itemp (org-at-item-p))
  13677. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13678. column bpos bcol tpos tcol bullet btype bullet-type)
  13679. ;; Find the previous relevant line
  13680. (beginning-of-line 1)
  13681. (cond
  13682. ((looking-at "#") (setq column 0))
  13683. ((looking-at "\\*+ ") (setq column 0))
  13684. ((and (looking-at "[ \t]*:END:")
  13685. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13686. (save-excursion
  13687. (goto-char (1- (match-beginning 1)))
  13688. (setq column (current-column))))
  13689. (t
  13690. (beginning-of-line 0)
  13691. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13692. (not (looking-at "[ \t]*:END:"))
  13693. (not (looking-at org-drawer-regexp)))
  13694. (beginning-of-line 0))
  13695. (cond
  13696. ((looking-at "\\*+[ \t]+")
  13697. (if (not org-adapt-indentation)
  13698. (setq column 0)
  13699. (goto-char (match-end 0))
  13700. (setq column (current-column))))
  13701. ((looking-at org-drawer-regexp)
  13702. (goto-char (1- (match-beginning 1)))
  13703. (setq column (current-column)))
  13704. ((looking-at "\\([ \t]*\\):END:")
  13705. (goto-char (match-end 1))
  13706. (setq column (current-column)))
  13707. ((org-in-item-p)
  13708. (org-beginning-of-item)
  13709. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13710. (setq bpos (match-beginning 1) tpos (match-end 0)
  13711. bcol (progn (goto-char bpos) (current-column))
  13712. tcol (progn (goto-char tpos) (current-column))
  13713. bullet (match-string 1)
  13714. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13715. (if (> tcol (+ bcol org-description-max-indent))
  13716. (setq tcol (+ bcol 5)))
  13717. (if (not itemp)
  13718. (setq column tcol)
  13719. (goto-char pos)
  13720. (beginning-of-line 1)
  13721. (if (looking-at "\\S-")
  13722. (progn
  13723. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13724. (setq bullet (match-string 1)
  13725. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13726. (setq column (if (equal btype bullet-type) bcol tcol)))
  13727. (setq column (org-get-indentation)))))
  13728. (t (setq column (org-get-indentation))))))
  13729. (goto-char pos)
  13730. (if (<= (current-column) (current-indentation))
  13731. (org-indent-line-to column)
  13732. (save-excursion (org-indent-line-to column)))
  13733. (setq column (current-column))
  13734. (beginning-of-line 1)
  13735. (if (looking-at
  13736. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13737. (replace-match (concat "\\1" (format org-property-format
  13738. (match-string 2) (match-string 3)))
  13739. t nil))
  13740. (org-move-to-column column)))
  13741. (defun org-set-autofill-regexps ()
  13742. (interactive)
  13743. ;; In the paragraph separator we include headlines, because filling
  13744. ;; text in a line directly attached to a headline would otherwise
  13745. ;; fill the headline as well.
  13746. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13747. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13748. ;; The paragraph starter includes hand-formatted lists.
  13749. (org-set-local 'paragraph-start
  13750. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13751. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13752. ;; But only if the user has not turned off tables or fixed-width regions
  13753. (org-set-local
  13754. 'auto-fill-inhibit-regexp
  13755. (concat "\\*+ \\|#\\+"
  13756. "\\|[ \t]*" org-keyword-time-regexp
  13757. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13758. (concat
  13759. "\\|[ \t]*["
  13760. (if org-enable-table-editor "|" "")
  13761. (if org-enable-fixed-width-editor ":" "")
  13762. "]"))))
  13763. ;; We use our own fill-paragraph function, to make sure that tables
  13764. ;; and fixed-width regions are not wrapped. That function will pass
  13765. ;; through to `fill-paragraph' when appropriate.
  13766. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13767. ; Adaptive filling: To get full control, first make sure that
  13768. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13769. (org-set-local 'adaptive-fill-regexp "\000")
  13770. (org-set-local 'adaptive-fill-function
  13771. 'org-adaptive-fill-function)
  13772. (org-set-local
  13773. 'align-mode-rules-list
  13774. '((org-in-buffer-settings
  13775. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13776. (modes . '(org-mode))))))
  13777. (defun org-fill-paragraph (&optional justify)
  13778. "Re-align a table, pass through to fill-paragraph if no table."
  13779. (let ((table-p (org-at-table-p))
  13780. (table.el-p (org-at-table.el-p)))
  13781. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13782. (save-excursion (goto-char (point-at-bol))
  13783. (looking-at outline-regexp)))
  13784. t) ; skip headlines
  13785. (table.el-p t) ; skip table.el tables
  13786. (table-p (org-table-align) t) ; align org-mode tables
  13787. (t nil)))) ; call paragraph-fill
  13788. ;; For reference, this is the default value of adaptive-fill-regexp
  13789. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13790. (defun org-adaptive-fill-function ()
  13791. "Return a fill prefix for org-mode files.
  13792. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13793. work correctly."
  13794. (cond ((looking-at "#[ \t]+")
  13795. (match-string 0))
  13796. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13797. (save-excursion
  13798. (if (> (match-end 1) (+ (match-beginning 1)
  13799. org-description-max-indent))
  13800. (goto-char (+ (match-beginning 1) 5))
  13801. (goto-char (match-end 0)))
  13802. (make-string (current-column) ?\ )))
  13803. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13804. (save-excursion
  13805. (goto-char (match-end 0))
  13806. (make-string (current-column) ?\ )))
  13807. (t nil)))
  13808. ;;; Other stuff.
  13809. (defun org-toggle-fixed-width-section (arg)
  13810. "Toggle the fixed-width export.
  13811. If there is no active region, the QUOTE keyword at the current headline is
  13812. inserted or removed. When present, it causes the text between this headline
  13813. and the next to be exported as fixed-width text, and unmodified.
  13814. If there is an active region, this command adds or removes a colon as the
  13815. first character of this line. If the first character of a line is a colon,
  13816. this line is also exported in fixed-width font."
  13817. (interactive "P")
  13818. (let* ((cc 0)
  13819. (regionp (org-region-active-p))
  13820. (beg (if regionp (region-beginning) (point)))
  13821. (end (if regionp (region-end)))
  13822. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13823. (case-fold-search nil)
  13824. (re "[ \t]*\\(:\\)")
  13825. off)
  13826. (if regionp
  13827. (save-excursion
  13828. (goto-char beg)
  13829. (setq cc (current-column))
  13830. (beginning-of-line 1)
  13831. (setq off (looking-at re))
  13832. (while (> nlines 0)
  13833. (setq nlines (1- nlines))
  13834. (beginning-of-line 1)
  13835. (cond
  13836. (arg
  13837. (org-move-to-column cc t)
  13838. (insert ":\n")
  13839. (forward-line -1))
  13840. ((and off (looking-at re))
  13841. (replace-match "" t t nil 1))
  13842. ((not off) (org-move-to-column cc t) (insert ":")))
  13843. (forward-line 1)))
  13844. (save-excursion
  13845. (org-back-to-heading)
  13846. (if (looking-at (concat outline-regexp
  13847. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13848. (replace-match "" t t nil 1)
  13849. (if (looking-at outline-regexp)
  13850. (progn
  13851. (goto-char (match-end 0))
  13852. (insert org-quote-string " "))))))))
  13853. ;;;; Functions extending outline functionality
  13854. (defun org-beginning-of-line (&optional arg)
  13855. "Go to the beginning of the current line. If that is invisible, continue
  13856. to a visible line beginning. This makes the function of C-a more intuitive.
  13857. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13858. first attempt, and only move to after the tags when the cursor is already
  13859. beyond the end of the headline."
  13860. (interactive "P")
  13861. (let ((pos (point))
  13862. (special (if (consp org-special-ctrl-a/e)
  13863. (car org-special-ctrl-a/e)
  13864. org-special-ctrl-a/e))
  13865. refpos)
  13866. (beginning-of-line 1)
  13867. (if (and arg (fboundp 'move-beginning-of-line))
  13868. (call-interactively 'move-beginning-of-line)
  13869. (if (bobp)
  13870. nil
  13871. (backward-char 1)
  13872. (if (org-invisible-p)
  13873. (while (and (not (bobp)) (org-invisible-p))
  13874. (backward-char 1)
  13875. (beginning-of-line 1))
  13876. (forward-char 1))))
  13877. (when special
  13878. (cond
  13879. ((and (looking-at org-complex-heading-regexp)
  13880. (= (char-after (match-end 1)) ?\ ))
  13881. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13882. (point-at-eol)))
  13883. (goto-char
  13884. (if (eq special t)
  13885. (cond ((> pos refpos) refpos)
  13886. ((= pos (point)) refpos)
  13887. (t (point)))
  13888. (cond ((> pos (point)) (point))
  13889. ((not (eq last-command this-command)) (point))
  13890. (t refpos)))))
  13891. ((org-at-item-p)
  13892. (goto-char
  13893. (if (eq special t)
  13894. (cond ((> pos (match-end 4)) (match-end 4))
  13895. ((= pos (point)) (match-end 4))
  13896. (t (point)))
  13897. (cond ((> pos (point)) (point))
  13898. ((not (eq last-command this-command)) (point))
  13899. (t (match-end 4))))))))
  13900. (org-no-warnings
  13901. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13902. (defun org-end-of-line (&optional arg)
  13903. "Go to the end of the line.
  13904. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13905. first attempt, and only move to after the tags when the cursor is already
  13906. beyond the end of the headline."
  13907. (interactive "P")
  13908. (let ((special (if (consp org-special-ctrl-a/e)
  13909. (cdr org-special-ctrl-a/e)
  13910. org-special-ctrl-a/e)))
  13911. (if (or (not special)
  13912. (not (org-on-heading-p))
  13913. arg)
  13914. (call-interactively (if (fboundp 'move-end-of-line)
  13915. 'move-end-of-line
  13916. 'end-of-line))
  13917. (let ((pos (point)))
  13918. (beginning-of-line 1)
  13919. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13920. (if (eq special t)
  13921. (if (or (< pos (match-beginning 1))
  13922. (= pos (match-end 0)))
  13923. (goto-char (match-beginning 1))
  13924. (goto-char (match-end 0)))
  13925. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13926. (goto-char (match-end 0))
  13927. (goto-char (match-beginning 1))))
  13928. (call-interactively (if (fboundp 'move-end-of-line)
  13929. 'move-end-of-line
  13930. 'end-of-line)))))
  13931. (org-no-warnings
  13932. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13933. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13934. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13935. (defun org-kill-line (&optional arg)
  13936. "Kill line, to tags or end of line."
  13937. (interactive "P")
  13938. (cond
  13939. ((or (not org-special-ctrl-k)
  13940. (bolp)
  13941. (not (org-on-heading-p)))
  13942. (call-interactively 'kill-line))
  13943. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13944. (kill-region (point) (match-beginning 1))
  13945. (org-set-tags nil t))
  13946. (t (kill-region (point) (point-at-eol)))))
  13947. (define-key org-mode-map "\C-k" 'org-kill-line)
  13948. (defun org-yank (&optional arg)
  13949. "Yank. If the kill is a subtree, treat it specially.
  13950. This command will look at the current kill and check if is a single
  13951. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13952. cursor is at the beginning of a line or after the stars of a currently
  13953. empty headline, then the yank is handled specially. How exactly depends
  13954. on the value of the following variables, both set by default.
  13955. org-yank-folded-subtrees
  13956. When set, the subtree(s) will be folded after insertion, but only
  13957. if doing so would now swallow text after the yanked text.
  13958. org-yank-adjusted-subtrees
  13959. When set, the subtree will be promoted or demoted in order to
  13960. fit into the local outline tree structure, which means that the level
  13961. will be adjusted so that it becomes the smaller one of the two
  13962. *visible* surrounding headings.
  13963. Any prefix to this command will cause `yank' to be called directly with
  13964. no special treatment. In particular, a simple `C-u' prefix will just
  13965. plainly yank the text as it is.
  13966. \[1] Basically, the test checks if the first non-white line is a heading
  13967. and if there are no other headings with fewer stars."
  13968. (interactive "P")
  13969. (setq this-command 'yank)
  13970. (if arg
  13971. (call-interactively 'yank)
  13972. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13973. (and (org-kill-is-subtree-p)
  13974. (or (bolp)
  13975. (and (looking-at "[ \t]*$")
  13976. (string-match
  13977. "\\`\\*+\\'"
  13978. (buffer-substring (point-at-bol) (point)))))))
  13979. swallowp)
  13980. (cond
  13981. ((and subtreep org-yank-folded-subtrees)
  13982. (let ((beg (point))
  13983. end)
  13984. (if (and subtreep org-yank-adjusted-subtrees)
  13985. (org-paste-subtree nil nil 'for-yank)
  13986. (call-interactively 'yank))
  13987. (setq end (point))
  13988. (goto-char beg)
  13989. (when (and (bolp) subtreep
  13990. (not (setq swallowp
  13991. (org-yank-folding-would-swallow-text beg end))))
  13992. (or (looking-at outline-regexp)
  13993. (re-search-forward (concat "^" outline-regexp) end t))
  13994. (while (and (< (point) end) (looking-at outline-regexp))
  13995. (hide-subtree)
  13996. (org-cycle-show-empty-lines 'folded)
  13997. (condition-case nil
  13998. (outline-forward-same-level 1)
  13999. (error (goto-char end)))))
  14000. (when swallowp
  14001. (message
  14002. "Yanked text not folded because that would swallow text"))
  14003. (goto-char end)
  14004. (skip-chars-forward " \t\n\r")
  14005. (beginning-of-line 1)
  14006. (push-mark beg 'nomsg)))
  14007. ((and subtreep org-yank-adjusted-subtrees)
  14008. (let ((beg (point-at-bol)))
  14009. (org-paste-subtree nil nil 'for-yank)
  14010. (push-mark beg 'nomsg)))
  14011. (t
  14012. (call-interactively 'yank))))))
  14013. (defun org-yank-folding-would-swallow-text (beg end)
  14014. "Would hide-subtree at BEG swallow any text after END?"
  14015. (let (level)
  14016. (save-excursion
  14017. (goto-char beg)
  14018. (when (or (looking-at outline-regexp)
  14019. (re-search-forward (concat "^" outline-regexp) end t))
  14020. (setq level (org-outline-level)))
  14021. (goto-char end)
  14022. (skip-chars-forward " \t\r\n\v\f")
  14023. (if (or (eobp)
  14024. (and (bolp) (looking-at org-outline-regexp)
  14025. (<= (org-outline-level) level)))
  14026. nil ; Nothing would be swallowed
  14027. t)))) ; something would swallow
  14028. (define-key org-mode-map "\C-y" 'org-yank)
  14029. (defun org-invisible-p ()
  14030. "Check if point is at a character currently not visible."
  14031. ;; Early versions of noutline don't have `outline-invisible-p'.
  14032. (if (fboundp 'outline-invisible-p)
  14033. (outline-invisible-p)
  14034. (get-char-property (point) 'invisible)))
  14035. (defun org-invisible-p2 ()
  14036. "Check if point is at a character currently not visible."
  14037. (save-excursion
  14038. (if (and (eolp) (not (bobp))) (backward-char 1))
  14039. ;; Early versions of noutline don't have `outline-invisible-p'.
  14040. (if (fboundp 'outline-invisible-p)
  14041. (outline-invisible-p)
  14042. (get-char-property (point) 'invisible))))
  14043. (defun org-back-to-heading (&optional invisible-ok)
  14044. "Call `outline-back-to-heading', but provide a better error message."
  14045. (condition-case nil
  14046. (outline-back-to-heading invisible-ok)
  14047. (error (error "Before first headline at position %d in buffer %s"
  14048. (point) (current-buffer)))))
  14049. (defun org-before-first-heading-p ()
  14050. "Before first heading?"
  14051. (save-excursion
  14052. (null (re-search-backward "^\\*+ " nil t))))
  14053. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14054. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14055. (defun org-at-heading-or-item-p ()
  14056. (or (org-on-heading-p) (org-at-item-p)))
  14057. (defun org-on-target-p ()
  14058. (or (org-in-regexp org-radio-target-regexp)
  14059. (org-in-regexp org-target-regexp)))
  14060. (defun org-up-heading-all (arg)
  14061. "Move to the heading line of which the present line is a subheading.
  14062. This function considers both visible and invisible heading lines.
  14063. With argument, move up ARG levels."
  14064. (if (fboundp 'outline-up-heading-all)
  14065. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14066. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14067. (defun org-up-heading-safe ()
  14068. "Move to the heading line of which the present line is a subheading.
  14069. This version will not throw an error. It will return the level of the
  14070. headline found, or nil if no higher level is found."
  14071. (let (start-level re)
  14072. (org-back-to-heading t)
  14073. (setq start-level (funcall outline-level))
  14074. (if (equal start-level 1)
  14075. nil
  14076. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14077. (if (re-search-backward re nil t)
  14078. (funcall outline-level)))))
  14079. (defun org-first-sibling-p ()
  14080. "Is this heading the first child of its parents?"
  14081. (interactive)
  14082. (let ((re (concat "^" outline-regexp))
  14083. level l)
  14084. (unless (org-at-heading-p t)
  14085. (error "Not at a heading"))
  14086. (setq level (funcall outline-level))
  14087. (save-excursion
  14088. (if (not (re-search-backward re nil t))
  14089. t
  14090. (setq l (funcall outline-level))
  14091. (< l level)))))
  14092. (defun org-goto-sibling (&optional previous)
  14093. "Goto the next sibling, even if it is invisible.
  14094. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14095. when a sibling was found. When none is found, return nil and don't
  14096. move point."
  14097. (let ((fun (if previous 're-search-backward 're-search-forward))
  14098. (pos (point))
  14099. (re (concat "^" outline-regexp))
  14100. level l)
  14101. (when (condition-case nil (org-back-to-heading t) (error nil))
  14102. (setq level (funcall outline-level))
  14103. (catch 'exit
  14104. (or previous (forward-char 1))
  14105. (while (funcall fun re nil t)
  14106. (setq l (funcall outline-level))
  14107. (when (< l level) (goto-char pos) (throw 'exit nil))
  14108. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14109. (goto-char pos)
  14110. nil))))
  14111. (defun org-show-siblings ()
  14112. "Show all siblings of the current headline."
  14113. (save-excursion
  14114. (while (org-goto-sibling) (org-flag-heading nil)))
  14115. (save-excursion
  14116. (while (org-goto-sibling 'previous)
  14117. (org-flag-heading nil))))
  14118. (defun org-show-hidden-entry ()
  14119. "Show an entry where even the heading is hidden."
  14120. (save-excursion
  14121. (org-show-entry)))
  14122. (defun org-flag-heading (flag &optional entry)
  14123. "Flag the current heading. FLAG non-nil means make invisible.
  14124. When ENTRY is non-nil, show the entire entry."
  14125. (save-excursion
  14126. (org-back-to-heading t)
  14127. ;; Check if we should show the entire entry
  14128. (if entry
  14129. (progn
  14130. (org-show-entry)
  14131. (save-excursion
  14132. (and (outline-next-heading)
  14133. (org-flag-heading nil))))
  14134. (outline-flag-region (max (point-min) (1- (point)))
  14135. (save-excursion (outline-end-of-heading) (point))
  14136. flag))))
  14137. (defun org-forward-same-level (arg)
  14138. "Move forward to the ARG'th subheading at same level as this one.
  14139. Stop at the first and last subheadings of a superior heading.
  14140. This is like outline-forward-same-level, but invisible headings are ok."
  14141. (interactive "p")
  14142. (org-back-to-heading t)
  14143. (while (> arg 0)
  14144. (let ((point-to-move-to (save-excursion
  14145. (org-get-next-sibling))))
  14146. (if point-to-move-to
  14147. (progn
  14148. (goto-char point-to-move-to)
  14149. (setq arg (1- arg)))
  14150. (progn
  14151. (setq arg 0)
  14152. (error "No following same-level heading"))))))
  14153. (defun org-get-next-sibling ()
  14154. "Move to next heading of the same level, and return point.
  14155. If there is no such heading, return nil.
  14156. This is like outline-next-sibling, but invisible headings are ok."
  14157. (let ((level (funcall outline-level)))
  14158. (outline-next-heading)
  14159. (while (and (not (eobp)) (> (funcall outline-level) level))
  14160. (outline-next-heading))
  14161. (if (or (eobp) (< (funcall outline-level) level))
  14162. nil
  14163. (point))))
  14164. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14165. ;; This is an exact copy of the original function, but it uses
  14166. ;; `org-back-to-heading', to make it work also in invisible
  14167. ;; trees. And is uses an invisible-OK argument.
  14168. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14169. (org-back-to-heading invisible-OK)
  14170. (let ((first t)
  14171. (level (funcall outline-level)))
  14172. (while (and (not (eobp))
  14173. (or first (> (funcall outline-level) level)))
  14174. (setq first nil)
  14175. (outline-next-heading))
  14176. (unless to-heading
  14177. (if (memq (preceding-char) '(?\n ?\^M))
  14178. (progn
  14179. ;; Go to end of line before heading
  14180. (forward-char -1)
  14181. (if (memq (preceding-char) '(?\n ?\^M))
  14182. ;; leave blank line before heading
  14183. (forward-char -1))))))
  14184. (point))
  14185. (defun org-show-subtree ()
  14186. "Show everything after this heading at deeper levels."
  14187. (outline-flag-region
  14188. (point)
  14189. (save-excursion
  14190. (outline-end-of-subtree) (outline-next-heading) (point))
  14191. nil))
  14192. (defun org-show-entry ()
  14193. "Show the body directly following this heading.
  14194. Show the heading too, if it is currently invisible."
  14195. (interactive)
  14196. (save-excursion
  14197. (condition-case nil
  14198. (progn
  14199. (org-back-to-heading t)
  14200. (outline-flag-region
  14201. (max (point-min) (1- (point)))
  14202. (save-excursion
  14203. (if (re-search-forward
  14204. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14205. (match-beginning 1)
  14206. (point-max)))
  14207. nil))
  14208. (error nil))))
  14209. (defun org-make-options-regexp (kwds &optional extra)
  14210. "Make a regular expression for keyword lines."
  14211. (concat
  14212. "^"
  14213. "#?[ \t]*\\+\\("
  14214. (mapconcat 'regexp-quote kwds "\\|")
  14215. (if extra (concat "\\|" extra))
  14216. "\\):[ \t]*"
  14217. "\\(.+\\)"))
  14218. ;; Make isearch reveal the necessary context
  14219. (defun org-isearch-end ()
  14220. "Reveal context after isearch exits."
  14221. (when isearch-success ; only if search was successful
  14222. (if (featurep 'xemacs)
  14223. ;; Under XEmacs, the hook is run in the correct place,
  14224. ;; we directly show the context.
  14225. (org-show-context 'isearch)
  14226. ;; In Emacs the hook runs *before* restoring the overlays.
  14227. ;; So we have to use a one-time post-command-hook to do this.
  14228. ;; (Emacs 22 has a special variable, see function `org-mode')
  14229. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14230. isearch-mode-end-hook-quit)
  14231. ;; Only when the isearch was not quitted.
  14232. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14233. 'append 'local)))))
  14234. (defun org-isearch-post-command ()
  14235. "Remove self from hook, and show context."
  14236. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14237. (org-show-context 'isearch))
  14238. ;;;; Integration with and fixes for other packages
  14239. ;;; Imenu support
  14240. (defvar org-imenu-markers nil
  14241. "All markers currently used by Imenu.")
  14242. (make-variable-buffer-local 'org-imenu-markers)
  14243. (defun org-imenu-new-marker (&optional pos)
  14244. "Return a new marker for use by Imenu, and remember the marker."
  14245. (let ((m (make-marker)))
  14246. (move-marker m (or pos (point)))
  14247. (push m org-imenu-markers)
  14248. m))
  14249. (defun org-imenu-get-tree ()
  14250. "Produce the index for Imenu."
  14251. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14252. (setq org-imenu-markers nil)
  14253. (let* ((n org-imenu-depth)
  14254. (re (concat "^" outline-regexp))
  14255. (subs (make-vector (1+ n) nil))
  14256. (last-level 0)
  14257. m level head)
  14258. (save-excursion
  14259. (save-restriction
  14260. (widen)
  14261. (goto-char (point-max))
  14262. (while (re-search-backward re nil t)
  14263. (setq level (org-reduced-level (funcall outline-level)))
  14264. (when (<= level n)
  14265. (looking-at org-complex-heading-regexp)
  14266. (setq head (org-link-display-format
  14267. (org-match-string-no-properties 4))
  14268. m (org-imenu-new-marker))
  14269. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14270. (if (>= level last-level)
  14271. (push (cons head m) (aref subs level))
  14272. (push (cons head (aref subs (1+ level))) (aref subs level))
  14273. (loop for i from (1+ level) to n do (aset subs i nil)))
  14274. (setq last-level level)))))
  14275. (aref subs 1)))
  14276. (eval-after-load "imenu"
  14277. '(progn
  14278. (add-hook 'imenu-after-jump-hook
  14279. (lambda ()
  14280. (if (eq major-mode 'org-mode)
  14281. (org-show-context 'org-goto))))))
  14282. (defun org-link-display-format (link)
  14283. "Replace a link with either the description, or the link target
  14284. if no description is present"
  14285. (save-match-data
  14286. (if (string-match org-bracket-link-analytic-regexp link)
  14287. (replace-match (or (match-string 5 link)
  14288. (concat (match-string 1 link)
  14289. (match-string 3 link)))
  14290. nil nil link)
  14291. link)))
  14292. ;; Speedbar support
  14293. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14294. "Overlay marking the agenda restriction line in speedbar.")
  14295. (org-overlay-put org-speedbar-restriction-lock-overlay
  14296. 'face 'org-agenda-restriction-lock)
  14297. (org-overlay-put org-speedbar-restriction-lock-overlay
  14298. 'help-echo "Agendas are currently limited to this item.")
  14299. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14300. (defun org-speedbar-set-agenda-restriction ()
  14301. "Restrict future agenda commands to the location at point in speedbar.
  14302. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14303. (interactive)
  14304. (require 'org-agenda)
  14305. (let (p m tp np dir txt)
  14306. (cond
  14307. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14308. 'org-imenu t))
  14309. (setq m (get-text-property p 'org-imenu-marker))
  14310. (save-excursion
  14311. (save-restriction
  14312. (set-buffer (marker-buffer m))
  14313. (goto-char m)
  14314. (org-agenda-set-restriction-lock 'subtree))))
  14315. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14316. 'speedbar-function 'speedbar-find-file))
  14317. (setq tp (previous-single-property-change
  14318. (1+ p) 'speedbar-function)
  14319. np (next-single-property-change
  14320. tp 'speedbar-function)
  14321. dir (speedbar-line-directory)
  14322. txt (buffer-substring-no-properties (or tp (point-min))
  14323. (or np (point-max))))
  14324. (save-excursion
  14325. (save-restriction
  14326. (set-buffer (find-file-noselect
  14327. (let ((default-directory dir))
  14328. (expand-file-name txt))))
  14329. (unless (org-mode-p)
  14330. (error "Cannot restrict to non-Org-mode file"))
  14331. (org-agenda-set-restriction-lock 'file))))
  14332. (t (error "Don't know how to restrict Org-mode's agenda")))
  14333. (org-move-overlay org-speedbar-restriction-lock-overlay
  14334. (point-at-bol) (point-at-eol))
  14335. (setq current-prefix-arg nil)
  14336. (org-agenda-maybe-redo)))
  14337. (eval-after-load "speedbar"
  14338. '(progn
  14339. (speedbar-add-supported-extension ".org")
  14340. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14341. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14342. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14343. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14344. (add-hook 'speedbar-visiting-tag-hook
  14345. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14346. ;;; Fixes and Hacks for problems with other packages
  14347. ;; Make flyspell not check words in links, to not mess up our keymap
  14348. (defun org-mode-flyspell-verify ()
  14349. "Don't let flyspell put overlays at active buttons."
  14350. (not (get-text-property (point) 'keymap)))
  14351. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14352. (eval-after-load "bookmark"
  14353. '(if (boundp 'bookmark-after-jump-hook)
  14354. ;; We can use the hook
  14355. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14356. ;; Hook not available, use advice
  14357. (defadvice bookmark-jump (after org-make-visible activate)
  14358. "Make the position visible."
  14359. (org-bookmark-jump-unhide))))
  14360. ;; Make sure saveplace show the location if it was hidden
  14361. (eval-after-load "saveplace"
  14362. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14363. "Make the position visible."
  14364. (org-bookmark-jump-unhide)))
  14365. (defun org-bookmark-jump-unhide ()
  14366. "Unhide the current position, to show the bookmark location."
  14367. (and (org-mode-p)
  14368. (or (org-invisible-p)
  14369. (save-excursion (goto-char (max (point-min) (1- (point))))
  14370. (org-invisible-p)))
  14371. (org-show-context 'bookmark-jump)))
  14372. ;; Make session.el ignore our circular variable
  14373. (eval-after-load "session"
  14374. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14375. ;;;; Experimental code
  14376. (defun org-closed-in-range ()
  14377. "Sparse tree of items closed in a certain time range.
  14378. Still experimental, may disappear in the future."
  14379. (interactive)
  14380. ;; Get the time interval from the user.
  14381. (let* ((time1 (time-to-seconds
  14382. (org-read-date nil 'to-time nil "Starting date: ")))
  14383. (time2 (time-to-seconds
  14384. (org-read-date nil 'to-time nil "End date:")))
  14385. ;; callback function
  14386. (callback (lambda ()
  14387. (let ((time
  14388. (time-to-seconds
  14389. (apply 'encode-time
  14390. (org-parse-time-string
  14391. (match-string 1))))))
  14392. ;; check if time in interval
  14393. (and (>= time time1) (<= time time2))))))
  14394. ;; make tree, check each match with the callback
  14395. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14396. ;;;; Finish up
  14397. (provide 'org)
  14398. (run-hooks 'org-load-hook)
  14399. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14400. ;;; org.el ends here