org.el 578 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553
  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.22trans
  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.22trans"
  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. :group 'org-edit-structure
  586. :type '(choice
  587. (const :tag "off" nil)
  588. (const :tag "after bullet first" t)
  589. (const :tag "border first" reversed)))
  590. (if (fboundp 'defvaralias)
  591. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  592. (defcustom org-special-ctrl-k nil
  593. "Non-nil means `C-k' will behave specially in headlines.
  594. When nil, `C-k' will call the default `kill-line' command.
  595. When t, the following will happen while the cursor is in the headline:
  596. - When the cursor is at the beginning of a headline, kill the entire
  597. line and possible the folded subtree below the line.
  598. - When in the middle of the headline text, kill the headline up to the tags.
  599. - When after the headline text, kill the tags."
  600. :group 'org-edit-structure
  601. :type 'boolean)
  602. (defcustom org-yank-folded-subtrees t
  603. "Non-nil means, when yanking subtrees, fold them.
  604. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  605. it starts with a heading and all other headings in it are either children
  606. or siblings, then fold all the subtrees. However, do this only if no
  607. text after the yank would be swallowed into a folded tree by this action."
  608. :group 'org-edit-structure
  609. :type 'boolean)
  610. (defcustom org-yank-adjusted-subtrees nil
  611. "Non-nil means, when yanking subtrees, adjust the level.
  612. With this setting, `org-paste-subtree' is used to insert the subtree, see
  613. this function for details."
  614. :group 'org-edit-structure
  615. :type 'boolean)
  616. (defcustom org-M-RET-may-split-line '((default . t))
  617. "Non-nil means, M-RET will split the line at the cursor position.
  618. When nil, it will go to the end of the line before making a
  619. new line.
  620. You may also set this option in a different way for different
  621. contexts. Valid contexts are:
  622. headline when creating a new headline
  623. item when creating a new item
  624. table in a table field
  625. default the value to be used for all contexts not explicitly
  626. customized"
  627. :group 'org-structure
  628. :group 'org-table
  629. :type '(choice
  630. (const :tag "Always" t)
  631. (const :tag "Never" nil)
  632. (repeat :greedy t :tag "Individual contexts"
  633. (cons
  634. (choice :tag "Context"
  635. (const headline)
  636. (const item)
  637. (const table)
  638. (const default))
  639. (boolean)))))
  640. (defcustom org-insert-heading-respect-content nil
  641. "Non-nil means, insert new headings after the current subtree.
  642. When nil, the new heading is created directly after the current line.
  643. The commands \\[org-insert-heading-respect-content] and
  644. \\[org-insert-todo-heading-respect-content] turn this variable on
  645. for the duration of the command."
  646. :group 'org-structure
  647. :type 'boolean)
  648. (defcustom org-blank-before-new-entry '((heading . auto)
  649. (plain-list-item . auto))
  650. "Should `org-insert-heading' leave a blank line before new heading/item?
  651. The value is an alist, with `heading' and `plain-list-item' as car,
  652. and a boolean flag as cdr."
  653. :group 'org-edit-structure
  654. :type '(list
  655. (cons (const heading)
  656. (choice (const :tag "Never" nil)
  657. (const :tag "Always" t)
  658. (const :tag "Auto" auto)))
  659. (cons (const plain-list-item)
  660. (choice (const :tag "Never" nil)
  661. (const :tag "Always" t)
  662. (const :tag "Auto" auto)))))
  663. (defcustom org-insert-heading-hook nil
  664. "Hook being run after inserting a new heading."
  665. :group 'org-edit-structure
  666. :type 'hook)
  667. (defcustom org-enable-fixed-width-editor t
  668. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  669. This currently only means, they are never auto-wrapped.
  670. When nil, such lines will be treated like ordinary lines.
  671. See also the QUOTE keyword."
  672. :group 'org-edit-structure
  673. :type 'boolean)
  674. (defcustom org-edit-src-region-extra nil
  675. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  676. For examples see the function `org-edit-src-find-region-and-lang'.
  677. The regular expression identifying the begin marker should end with a newline,
  678. and the regexp marking the end line should start with a newline, to make sure
  679. there are kept outside the narrowed region."
  680. :group 'org-edit-structure
  681. :type '(repeat
  682. (list
  683. (regexp :tag "begin regexp")
  684. (regexp :tag "end regexp")
  685. (choice :tag "language"
  686. (string :tag "specify")
  687. (integer :tag "from match group")
  688. (const :tag "from `lang' element")
  689. (const :tag "from `style' element")))))
  690. (defcustom org-coderef-label-format "(ref:%s)"
  691. "The default coderef format.
  692. This format string will be used to search for coderef labels in literal
  693. examples (EXAMPLE and SRC blocks). The format can be overwritten
  694. an individual literal example with the -f option, like
  695. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  696. ...
  697. #+END_SRC
  698. If you want to use this for HTML export, make sure that the format does
  699. not introduce special font-locking, and avoid the HTML special
  700. characters `<', `>', and `&'. The reason for this restriction is that
  701. the labels are searched for only after htmlize has done its job."
  702. :group 'org-edit-structure ; FIXME this is not in the right group
  703. :type 'string)
  704. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  705. "The mode that should be used to edit fixed-width regions.
  706. These are the regions where each line starts with a colon."
  707. :group 'org-edit-structure
  708. :type '(choice
  709. (const artist-mode)
  710. (const picture-mode)
  711. (const fundamental-mode)
  712. (function :tag "Other (specify)")))
  713. (defcustom org-goto-auto-isearch t
  714. "Non-nil means, typing characters in org-goto starts incremental search."
  715. :group 'org-edit-structure
  716. :type 'boolean)
  717. (defgroup org-sparse-trees nil
  718. "Options concerning sparse trees in Org-mode."
  719. :tag "Org Sparse Trees"
  720. :group 'org-structure)
  721. (defcustom org-highlight-sparse-tree-matches t
  722. "Non-nil means, highlight all matches that define a sparse tree.
  723. The highlights will automatically disappear the next time the buffer is
  724. changed by an edit command."
  725. :group 'org-sparse-trees
  726. :type 'boolean)
  727. (defcustom org-remove-highlights-with-change t
  728. "Non-nil means, any change to the buffer will remove temporary highlights.
  729. Such highlights are created by `org-occur' and `org-clock-display'.
  730. When nil, `C-c C-c needs to be used to get rid of the highlights.
  731. The highlights created by `org-preview-latex-fragment' always need
  732. `C-c C-c' to be removed."
  733. :group 'org-sparse-trees
  734. :group 'org-time
  735. :type 'boolean)
  736. (defcustom org-occur-hook '(org-first-headline-recenter)
  737. "Hook that is run after `org-occur' has constructed a sparse tree.
  738. This can be used to recenter the window to show as much of the structure
  739. as possible."
  740. :group 'org-sparse-trees
  741. :type 'hook)
  742. (defgroup org-imenu-and-speedbar nil
  743. "Options concerning imenu and speedbar in Org-mode."
  744. :tag "Org Imenu and Speedbar"
  745. :group 'org-structure)
  746. (defcustom org-imenu-depth 2
  747. "The maximum level for Imenu access to Org-mode headlines.
  748. This also applied for speedbar access."
  749. :group 'org-imenu-and-speedbar
  750. :type 'number)
  751. (defgroup org-table nil
  752. "Options concerning tables in Org-mode."
  753. :tag "Org Table"
  754. :group 'org)
  755. (defcustom org-enable-table-editor 'optimized
  756. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  757. When nil, such lines will be treated like ordinary lines.
  758. When equal to the symbol `optimized', the table editor will be optimized to
  759. do the following:
  760. - Automatic overwrite mode in front of whitespace in table fields.
  761. This makes the structure of the table stay in tact as long as the edited
  762. field does not exceed the column width.
  763. - Minimize the number of realigns. Normally, the table is aligned each time
  764. TAB or RET are pressed to move to another field. With optimization this
  765. happens only if changes to a field might have changed the column width.
  766. Optimization requires replacing the functions `self-insert-command',
  767. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  768. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  769. very good at guessing when a re-align will be necessary, but you can always
  770. force one with \\[org-ctrl-c-ctrl-c].
  771. If you would like to use the optimized version in Org-mode, but the
  772. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  773. This variable can be used to turn on and off the table editor during a session,
  774. but in order to toggle optimization, a restart is required.
  775. See also the variable `org-table-auto-blank-field'."
  776. :group 'org-table
  777. :type '(choice
  778. (const :tag "off" nil)
  779. (const :tag "on" t)
  780. (const :tag "on, optimized" optimized)))
  781. (defcustom org-table-tab-recognizes-table.el t
  782. "Non-nil means, TAB will automatically notice a table.el table.
  783. When it sees such a table, it moves point into it and - if necessary -
  784. calls `table-recognize-table'."
  785. :group 'org-table-editing
  786. :type 'boolean)
  787. (defgroup org-link nil
  788. "Options concerning links in Org-mode."
  789. :tag "Org Link"
  790. :group 'org)
  791. (defvar org-link-abbrev-alist-local nil
  792. "Buffer-local version of `org-link-abbrev-alist', which see.
  793. The value of this is taken from the #+LINK lines.")
  794. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  795. (defcustom org-link-abbrev-alist nil
  796. "Alist of link abbreviations.
  797. The car of each element is a string, to be replaced at the start of a link.
  798. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  799. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  800. [[linkkey:tag][description]]
  801. If REPLACE is a string, the tag will simply be appended to create the link.
  802. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  803. the placeholder \"%h\" will cause a url-encoded version of the tag to
  804. be inserted at that point (see the function `url-hexify-string').
  805. REPLACE may also be a function that will be called with the tag as the
  806. only argument to create the link, which should be returned as a string.
  807. See the manual for examples."
  808. :group 'org-link
  809. :type '(repeat
  810. (cons
  811. (string :tag "Protocol")
  812. (choice
  813. (string :tag "Format")
  814. (function)))))
  815. (defcustom org-descriptive-links t
  816. "Non-nil means, hide link part and only show description of bracket links.
  817. Bracket links are like [[link][description]]. This variable sets the initial
  818. state in new org-mode buffers. The setting can then be toggled on a
  819. per-buffer basis from the Org->Hyperlinks menu."
  820. :group 'org-link
  821. :type 'boolean)
  822. (defcustom org-link-file-path-type 'adaptive
  823. "How the path name in file links should be stored.
  824. Valid values are:
  825. relative Relative to the current directory, i.e. the directory of the file
  826. into which the link is being inserted.
  827. absolute Absolute path, if possible with ~ for home directory.
  828. noabbrev Absolute path, no abbreviation of home directory.
  829. adaptive Use relative path for files in the current directory and sub-
  830. directories of it. For other files, use an absolute path."
  831. :group 'org-link
  832. :type '(choice
  833. (const relative)
  834. (const absolute)
  835. (const noabbrev)
  836. (const adaptive)))
  837. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  838. "Types of links that should be activated in Org-mode files.
  839. This is a list of symbols, each leading to the activation of a certain link
  840. type. In principle, it does not hurt to turn on most link types - there may
  841. be a small gain when turning off unused link types. The types are:
  842. bracket The recommended [[link][description]] or [[link]] links with hiding.
  843. angular Links in angular brackets that may contain whitespace like
  844. <bbdb:Carsten Dominik>.
  845. plain Plain links in normal text, no whitespace, like http://google.com.
  846. radio Text that is matched by a radio target, see manual for details.
  847. tag Tag settings in a headline (link to tag search).
  848. date Time stamps (link to calendar).
  849. footnote Footnote labels.
  850. Changing this variable requires a restart of Emacs to become effective."
  851. :group 'org-link
  852. :type '(set :greedy t
  853. (const :tag "Double bracket links (new style)" bracket)
  854. (const :tag "Angular bracket links (old style)" angular)
  855. (const :tag "Plain text links" plain)
  856. (const :tag "Radio target matches" radio)
  857. (const :tag "Tags" tag)
  858. (const :tag "Timestamps" date)
  859. (const :tag "Footnotes" footnote)))
  860. (defcustom org-make-link-description-function nil
  861. "Function to use to generate link descriptions from links. If
  862. nil the link location will be used. This function must take two
  863. parameters; the first is the link and the second the description
  864. org-insert-link has generated, and should return the description
  865. to use."
  866. :group 'org-link
  867. :type 'function)
  868. (defgroup org-link-store nil
  869. "Options concerning storing links in Org-mode."
  870. :tag "Org Store Link"
  871. :group 'org-link)
  872. (defcustom org-email-link-description-format "Email %c: %.30s"
  873. "Format of the description part of a link to an email or usenet message.
  874. The following %-escapes will be replaced by corresponding information:
  875. %F full \"From\" field
  876. %f name, taken from \"From\" field, address if no name
  877. %T full \"To\" field
  878. %t first name in \"To\" field, address if no name
  879. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  880. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  881. %s subject
  882. %m message-id.
  883. You may use normal field width specification between the % and the letter.
  884. This is for example useful to limit the length of the subject.
  885. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  886. :group 'org-link-store
  887. :type 'string)
  888. (defcustom org-from-is-user-regexp
  889. (let (r1 r2)
  890. (when (and user-mail-address (not (string= user-mail-address "")))
  891. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  892. (when (and user-full-name (not (string= user-full-name "")))
  893. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  894. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  895. "Regexp matched against the \"From:\" header of an email or usenet message.
  896. It should match if the message is from the user him/herself."
  897. :group 'org-link-store
  898. :type 'regexp)
  899. (defcustom org-link-to-org-use-id 'create-if-interactive
  900. "Non-nil means, storing a link to an Org file will use entry IDs.
  901. Note that before this variable is even considered, org-id must be loaded,
  902. to please customize `org-modules' and turn it on.
  903. The variable can have the following values:
  904. t Create an ID if needed to make a link to the current entry.
  905. create-if-interactive
  906. If `org-store-link' is called directly (interactively, as a user
  907. command), do create an ID to support the link. But when doing the
  908. job for remember, only use the ID if it already exists. The
  909. purpose of this setting is to avoid proliferation of unwanted
  910. IDs, just because you happen to be in an Org file when you
  911. call `org-remember' that automatically and preemptively
  912. creates a link. If you do want to get an ID link in a remember
  913. template to an entry not having an ID, create it first by
  914. explicitly creating a link to it, using `C-c C-l' first.
  915. use-existing
  916. Use existing ID, do not create one.
  917. nil Never use an ID to make a link, instead link using a text search for
  918. the headline text."
  919. :group 'org-link-store
  920. :type '(choice
  921. (const :tag "Create ID to make link" t)
  922. (const :tag "Create if string link interactively"
  923. 'create-if-interactive)
  924. (const :tag "Only use existing" 'use-existing)
  925. (const :tag "Do not use ID to create link" nil)))
  926. (defcustom org-context-in-file-links t
  927. "Non-nil means, file links from `org-store-link' contain context.
  928. A search string will be added to the file name with :: as separator and
  929. used to find the context when the link is activated by the command
  930. `org-open-at-point'.
  931. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  932. negates this setting for the duration of the command."
  933. :group 'org-link-store
  934. :type 'boolean)
  935. (defcustom org-keep-stored-link-after-insertion nil
  936. "Non-nil means, keep link in list for entire session.
  937. The command `org-store-link' adds a link pointing to the current
  938. location to an internal list. These links accumulate during a session.
  939. The command `org-insert-link' can be used to insert links into any
  940. Org-mode file (offering completion for all stored links). When this
  941. option is nil, every link which has been inserted once using \\[org-insert-link]
  942. will be removed from the list, to make completing the unused links
  943. more efficient."
  944. :group 'org-link-store
  945. :type 'boolean)
  946. (defgroup org-link-follow nil
  947. "Options concerning following links in Org-mode."
  948. :tag "Org Follow Link"
  949. :group 'org-link)
  950. (defcustom org-link-translation-function nil
  951. "Function to translate links with different syntax to Org syntax.
  952. This can be used to translate links created for example by the Planner
  953. or emacs-wiki packages to Org syntax.
  954. The function must accept two parameters, a TYPE containing the link
  955. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  956. which is everything after the link protocol. It should return a cons
  957. with possibly modified values of type and path.
  958. Org contains a function for this, so if you set this variable to
  959. `org-translate-link-from-planner', you should be able follow many
  960. links created by planner."
  961. :group 'org-link-follow
  962. :type 'function)
  963. (defcustom org-follow-link-hook nil
  964. "Hook that is run after a link has been followed."
  965. :group 'org-link-follow
  966. :type 'hook)
  967. (defcustom org-tab-follows-link nil
  968. "Non-nil means, on links TAB will follow the link.
  969. Needs to be set before org.el is loaded.
  970. This really should not be used, it does not make sense, and the
  971. implementation is bad."
  972. :group 'org-link-follow
  973. :type 'boolean)
  974. (defcustom org-return-follows-link nil
  975. "Non-nil means, on links RET will follow the link.
  976. Needs to be set before org.el is loaded."
  977. :group 'org-link-follow
  978. :type 'boolean)
  979. (defcustom org-mouse-1-follows-link
  980. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  981. "Non-nil means, mouse-1 on a link will follow the link.
  982. A longer mouse click will still set point. Does not work on XEmacs.
  983. Needs to be set before org.el is loaded."
  984. :group 'org-link-follow
  985. :type 'boolean)
  986. (defcustom org-mark-ring-length 4
  987. "Number of different positions to be recorded in the ring
  988. Changing this requires a restart of Emacs to work correctly."
  989. :group 'org-link-follow
  990. :type 'integer)
  991. (defcustom org-link-frame-setup
  992. '((vm . vm-visit-folder-other-frame)
  993. (gnus . gnus-other-frame)
  994. (file . find-file-other-window))
  995. "Setup the frame configuration for following links.
  996. When following a link with Emacs, it may often be useful to display
  997. this link in another window or frame. This variable can be used to
  998. set this up for the different types of links.
  999. For VM, use any of
  1000. `vm-visit-folder'
  1001. `vm-visit-folder-other-frame'
  1002. For Gnus, use any of
  1003. `gnus'
  1004. `gnus-other-frame'
  1005. `org-gnus-no-new-news'
  1006. For FILE, use any of
  1007. `find-file'
  1008. `find-file-other-window'
  1009. `find-file-other-frame'
  1010. For the calendar, use the variable `calendar-setup'.
  1011. For BBDB, it is currently only possible to display the matches in
  1012. another window."
  1013. :group 'org-link-follow
  1014. :type '(list
  1015. (cons (const vm)
  1016. (choice
  1017. (const vm-visit-folder)
  1018. (const vm-visit-folder-other-window)
  1019. (const vm-visit-folder-other-frame)))
  1020. (cons (const gnus)
  1021. (choice
  1022. (const gnus)
  1023. (const gnus-other-frame)
  1024. (const org-gnus-no-new-news)))
  1025. (cons (const file)
  1026. (choice
  1027. (const find-file)
  1028. (const find-file-other-window)
  1029. (const find-file-other-frame)))))
  1030. (defcustom org-display-internal-link-with-indirect-buffer nil
  1031. "Non-nil means, use indirect buffer to display infile links.
  1032. Activating internal links (from one location in a file to another location
  1033. in the same file) normally just jumps to the location. When the link is
  1034. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1035. another window. When this option is set, the other window actually displays
  1036. an indirect buffer clone of the current buffer, to avoid any visibility
  1037. changes to the current buffer."
  1038. :group 'org-link-follow
  1039. :type 'boolean)
  1040. (defcustom org-open-non-existing-files nil
  1041. "Non-nil means, `org-open-file' will open non-existing files.
  1042. When nil, an error will be generated."
  1043. :group 'org-link-follow
  1044. :type 'boolean)
  1045. (defcustom org-open-directory-means-index-dot-org nil
  1046. "Non-nil means, a link to a directory really means to index.org.
  1047. When nil, following a directory link will run dired or open a finder/explorer
  1048. window on that directory."
  1049. :group 'org-link-follow
  1050. :type 'boolean)
  1051. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1052. "Function and arguments to call for following mailto links.
  1053. This is a list with the first element being a lisp function, and the
  1054. remaining elements being arguments to the function. In string arguments,
  1055. %a will be replaced by the address, and %s will be replaced by the subject
  1056. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1057. :group 'org-link-follow
  1058. :type '(choice
  1059. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1060. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1061. (const :tag "message-mail" (message-mail "%a" "%s"))
  1062. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1063. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1064. "Non-nil means, ask for confirmation before executing shell links.
  1065. Shell links can be dangerous: just think about a link
  1066. [[shell:rm -rf ~/*][Google Search]]
  1067. This link would show up in your Org-mode document as \"Google Search\",
  1068. but really it would remove your entire home directory.
  1069. Therefore we advise against setting this variable to nil.
  1070. Just change it to `y-or-n-p' of you want to confirm with a
  1071. single keystroke rather than having to type \"yes\"."
  1072. :group 'org-link-follow
  1073. :type '(choice
  1074. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1075. (const :tag "with y-or-n (faster)" y-or-n-p)
  1076. (const :tag "no confirmation (dangerous)" nil)))
  1077. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1078. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1079. Elisp links can be dangerous: just think about a link
  1080. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1081. This link would show up in your Org-mode document as \"Google Search\",
  1082. but really it would remove your entire home directory.
  1083. Therefore we advise against setting this variable to nil.
  1084. Just change it to `y-or-n-p' of you want to confirm with a
  1085. single keystroke rather than having to type \"yes\"."
  1086. :group 'org-link-follow
  1087. :type '(choice
  1088. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1089. (const :tag "with y-or-n (faster)" y-or-n-p)
  1090. (const :tag "no confirmation (dangerous)" nil)))
  1091. (defconst org-file-apps-defaults-gnu
  1092. '((remote . emacs)
  1093. (system . mailcap)
  1094. (t . mailcap))
  1095. "Default file applications on a UNIX or GNU/Linux system.
  1096. See `org-file-apps'.")
  1097. (defconst org-file-apps-defaults-macosx
  1098. '((remote . emacs)
  1099. (t . "open %s")
  1100. (system . "open %s")
  1101. ("ps.gz" . "gv %s")
  1102. ("eps.gz" . "gv %s")
  1103. ("dvi" . "xdvi %s")
  1104. ("fig" . "xfig %s"))
  1105. "Default file applications on a MacOS X system.
  1106. The system \"open\" is known as a default, but we use X11 applications
  1107. for some files for which the OS does not have a good default.
  1108. See `org-file-apps'.")
  1109. (defconst org-file-apps-defaults-windowsnt
  1110. (list
  1111. '(remote . emacs)
  1112. (cons t
  1113. (list (if (featurep 'xemacs)
  1114. 'mswindows-shell-execute
  1115. 'w32-shell-execute)
  1116. "open" 'file))
  1117. (cons 'system
  1118. (list (if (featurep 'xemacs)
  1119. 'mswindows-shell-execute
  1120. 'w32-shell-execute)
  1121. "open" 'file)))
  1122. "Default file applications on a Windows NT system.
  1123. The system \"open\" is used for most files.
  1124. See `org-file-apps'.")
  1125. (defcustom org-file-apps
  1126. '(
  1127. (auto-mode . emacs)
  1128. ("\\.x?html?\\'" . default)
  1129. ("\\.pdf\\'" . default)
  1130. )
  1131. "External applications for opening `file:path' items in a document.
  1132. Org-mode uses system defaults for different file types, but
  1133. you can use this variable to set the application for a given file
  1134. extension. The entries in this list are cons cells where the car identifies
  1135. files and the cdr the corresponding command. Possible values for the
  1136. file identifier are
  1137. \"regex\" Regular expression matched against the file name. For backward
  1138. compatibility, this can also be a string with only alphanumeric
  1139. characters, which is then interpreted as an extension.
  1140. `directory' Matches a directory
  1141. `remote' Matches a remote file, accessible through tramp or efs.
  1142. Remote files most likely should be visited through Emacs
  1143. because external applications cannot handle such paths.
  1144. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1145. so all files Emacs knows how to handle. Using this with
  1146. command `emacs' will open most files in Emacs. Beware that this
  1147. will also open html files inside Emacs, unless you add
  1148. (\"html\" . default) to the list as well.
  1149. t Default for files not matched by any of the other options.
  1150. `system' The system command to open files, like `open' on Windows
  1151. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1152. that will be selected if you call `C-c C-o' with a double
  1153. `C-u C-u' prefix.
  1154. Possible values for the command are:
  1155. `emacs' The file will be visited by the current Emacs process.
  1156. `default' Use the default application for this file type, which is the
  1157. association for t in the list, most likely in the system-specific
  1158. part.
  1159. This can be used to overrule an unwanted setting in the
  1160. system-specific variable.
  1161. `system' Use the system command for opening files, like \"open\".
  1162. This command is specified by the entry whose car is `system'.
  1163. Most likely, the system-specific version of this variable
  1164. does define this command, but you can overrule/replace it
  1165. here.
  1166. string A command to be executed by a shell; %s will be replaced
  1167. by the path to the file.
  1168. sexp A Lisp form which will be evaluated. The file path will
  1169. be available in the Lisp variable `file'.
  1170. For more examples, see the system specific constants
  1171. `org-file-apps-defaults-macosx'
  1172. `org-file-apps-defaults-windowsnt'
  1173. `org-file-apps-defaults-gnu'."
  1174. :group 'org-link-follow
  1175. :type '(repeat
  1176. (cons (choice :value ""
  1177. (string :tag "Extension")
  1178. (const :tag "System command to open files" system)
  1179. (const :tag "Default for unrecognized files" t)
  1180. (const :tag "Remote file" remote)
  1181. (const :tag "Links to a directory" directory)
  1182. (const :tag "Any files that have Emacs modes"
  1183. auto-mode))
  1184. (choice :value ""
  1185. (const :tag "Visit with Emacs" emacs)
  1186. (const :tag "Use default" default)
  1187. (const :tag "Use the system command" system)
  1188. (string :tag "Command")
  1189. (sexp :tag "Lisp form")))))
  1190. (defgroup org-refile nil
  1191. "Options concerning refiling entries in Org-mode."
  1192. :tag "Org Refile"
  1193. :group 'org)
  1194. (defcustom org-directory "~/org"
  1195. "Directory with org files.
  1196. This is just a default location to look for Org files. There is no need
  1197. at all to put your files into this directory. It is only used in the
  1198. following situations:
  1199. 1. When a remember template specifies a target file that is not an
  1200. absolute path. The path will then be interpreted relative to
  1201. `org-directory'
  1202. 2. When a remember note is filed away in an interactive way (when exiting the
  1203. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1204. with `org-directory' as the default path."
  1205. :group 'org-refile
  1206. :group 'org-remember
  1207. :type 'directory)
  1208. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1209. "Default target for storing notes.
  1210. Used by the hooks for remember.el. This can be a string, or nil to mean
  1211. the value of `remember-data-file'.
  1212. You can set this on a per-template basis with the variable
  1213. `org-remember-templates'."
  1214. :group 'org-refile
  1215. :group 'org-remember
  1216. :type '(choice
  1217. (const :tag "Default from remember-data-file" nil)
  1218. file))
  1219. (defcustom org-goto-interface 'outline
  1220. "The default interface to be used for `org-goto'.
  1221. Allowed values are:
  1222. outline The interface shows an outline of the relevant file
  1223. and the correct heading is found by moving through
  1224. the outline or by searching with incremental search.
  1225. outline-path-completion Headlines in the current buffer are offered via
  1226. completion. This is the interface also used by
  1227. the refile command."
  1228. :group 'org-refile
  1229. :type '(choice
  1230. (const :tag "Outline" outline)
  1231. (const :tag "Outline-path-completion" outline-path-completion)))
  1232. (defcustom org-goto-max-level 5
  1233. "Maximum level to be considered when running org-goto with refile interface."
  1234. :group 'org-refile
  1235. :type 'number)
  1236. (defcustom org-reverse-note-order nil
  1237. "Non-nil means, store new notes at the beginning of a file or entry.
  1238. When nil, new notes will be filed to the end of a file or entry.
  1239. This can also be a list with cons cells of regular expressions that
  1240. are matched against file names, and values."
  1241. :group 'org-remember
  1242. :group 'org-refile
  1243. :type '(choice
  1244. (const :tag "Reverse always" t)
  1245. (const :tag "Reverse never" nil)
  1246. (repeat :tag "By file name regexp"
  1247. (cons regexp boolean))))
  1248. (defcustom org-refile-targets nil
  1249. "Targets for refiling entries with \\[org-refile].
  1250. This is list of cons cells. Each cell contains:
  1251. - a specification of the files to be considered, either a list of files,
  1252. or a symbol whose function or variable value will be used to retrieve
  1253. a file name or a list of file names. If you use `org-agenda-files' for
  1254. that, all agenda files will be scanned for targets. Nil means, consider
  1255. headings in the current buffer.
  1256. - A specification of how to find candidate refile targets. This may be
  1257. any of:
  1258. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1259. This tag has to be present in all target headlines, inheritance will
  1260. not be considered.
  1261. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1262. todo keyword.
  1263. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1264. headlines that are refiling targets.
  1265. - a cons cell (:level . N). Any headline of level N is considered a target.
  1266. Note that, when `org-odd-levels-only' is set, level corresponds to
  1267. order in hierarchy, not to the number of stars.
  1268. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1269. Note that, when `org-odd-levels-only' is set, level corresponds to
  1270. order in hierarchy, not to the number of stars.
  1271. When this variable is nil, all top-level headlines in the current buffer
  1272. are used, equivalent to the value `((nil . (:level . 1))'."
  1273. :group 'org-refile
  1274. :type '(repeat
  1275. (cons
  1276. (choice :value org-agenda-files
  1277. (const :tag "All agenda files" org-agenda-files)
  1278. (const :tag "Current buffer" nil)
  1279. (function) (variable) (file))
  1280. (choice :tag "Identify target headline by"
  1281. (cons :tag "Specific tag" (const :value :tag) (string))
  1282. (cons :tag "TODO keyword" (const :value :todo) (string))
  1283. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1284. (cons :tag "Level number" (const :value :level) (integer))
  1285. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1286. (defcustom org-refile-use-outline-path nil
  1287. "Non-nil means, provide refile targets as paths.
  1288. So a level 3 headline will be available as level1/level2/level3.
  1289. When the value is `file', also include the file name (without directory)
  1290. into the path. When `full-file-path', include the full file path."
  1291. :group 'org-refile
  1292. :type '(choice
  1293. (const :tag "Not" nil)
  1294. (const :tag "Yes" t)
  1295. (const :tag "Start with file name" file)
  1296. (const :tag "Start with full file path" full-file-path)))
  1297. (defcustom org-outline-path-complete-in-steps t
  1298. "Non-nil means, complete the outline path in hierarchical steps.
  1299. When Org-mode uses the refile interface to select an outline path
  1300. \(see variable `org-refile-use-outline-path'), the completion of
  1301. the path can be done is a single go, or if can be done in steps down
  1302. the headline hierarchy. Going in steps is probably the best if you
  1303. do not use a special completion package like `ido' or `icicles'.
  1304. However, when using these packages, going in one step can be very
  1305. fast, while still showing the whole path to the entry."
  1306. :group 'org-refile
  1307. :type 'boolean)
  1308. (defgroup org-todo nil
  1309. "Options concerning TODO items in Org-mode."
  1310. :tag "Org TODO"
  1311. :group 'org)
  1312. (defgroup org-progress nil
  1313. "Options concerning Progress logging in Org-mode."
  1314. :tag "Org Progress"
  1315. :group 'org-time)
  1316. (defvar org-todo-interpretation-widgets
  1317. '(
  1318. (:tag "Sequence (cycling hits every state)" sequence)
  1319. (:tag "Type (cycling directly to DONE)" type))
  1320. "The available interpretation symbols for customizing
  1321. `org-todo-keywords'.
  1322. Interested libraries should add to this list.")
  1323. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1324. "List of TODO entry keyword sequences and their interpretation.
  1325. \\<org-mode-map>This is a list of sequences.
  1326. Each sequence starts with a symbol, either `sequence' or `type',
  1327. indicating if the keywords should be interpreted as a sequence of
  1328. action steps, or as different types of TODO items. The first
  1329. keywords are states requiring action - these states will select a headline
  1330. for inclusion into the global TODO list Org-mode produces. If one of
  1331. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1332. signify that no further action is necessary. If \"|\" is not found,
  1333. the last keyword is treated as the only DONE state of the sequence.
  1334. The command \\[org-todo] cycles an entry through these states, and one
  1335. additional state where no keyword is present. For details about this
  1336. cycling, see the manual.
  1337. TODO keywords and interpretation can also be set on a per-file basis with
  1338. the special #+SEQ_TODO and #+TYP_TODO lines.
  1339. Each keyword can optionally specify a character for fast state selection
  1340. \(in combination with the variable `org-use-fast-todo-selection')
  1341. and specifiers for state change logging, using the same syntax
  1342. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1343. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1344. indicates to record a time stamp each time this state is selected.
  1345. Each keyword may also specify if a timestamp or a note should be
  1346. recorded when entering or leaving the state, by adding additional
  1347. characters in the parenthesis after the keyword. This looks like this:
  1348. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1349. record only the time of the state change. With X and Y being either
  1350. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1351. Y when leaving the state if and only if the *target* state does not
  1352. define X. You may omit any of the fast-selection key or X or /Y,
  1353. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1354. For backward compatibility, this variable may also be just a list
  1355. of keywords - in this case the interpretation (sequence or type) will be
  1356. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1357. :group 'org-todo
  1358. :group 'org-keywords
  1359. :type '(choice
  1360. (repeat :tag "Old syntax, just keywords"
  1361. (string :tag "Keyword"))
  1362. (repeat :tag "New syntax"
  1363. (cons
  1364. (choice
  1365. :tag "Interpretation"
  1366. ;;Quick and dirty way to see
  1367. ;;`org-todo-interpretations'. This takes the
  1368. ;;place of item arguments
  1369. :convert-widget
  1370. (lambda (widget)
  1371. (widget-put widget
  1372. :args (mapcar
  1373. #'(lambda (x)
  1374. (widget-convert
  1375. (cons 'const x)))
  1376. org-todo-interpretation-widgets))
  1377. widget))
  1378. (repeat
  1379. (string :tag "Keyword"))))))
  1380. (defvar org-todo-keywords-1 nil
  1381. "All TODO and DONE keywords active in a buffer.")
  1382. (make-variable-buffer-local 'org-todo-keywords-1)
  1383. (defvar org-todo-keywords-for-agenda nil)
  1384. (defvar org-done-keywords-for-agenda nil)
  1385. (defvar org-todo-keyword-alist-for-agenda nil)
  1386. (defvar org-tag-alist-for-agenda nil)
  1387. (defvar org-agenda-contributing-files nil)
  1388. (defvar org-not-done-keywords nil)
  1389. (make-variable-buffer-local 'org-not-done-keywords)
  1390. (defvar org-done-keywords nil)
  1391. (make-variable-buffer-local 'org-done-keywords)
  1392. (defvar org-todo-heads nil)
  1393. (make-variable-buffer-local 'org-todo-heads)
  1394. (defvar org-todo-sets nil)
  1395. (make-variable-buffer-local 'org-todo-sets)
  1396. (defvar org-todo-log-states nil)
  1397. (make-variable-buffer-local 'org-todo-log-states)
  1398. (defvar org-todo-kwd-alist nil)
  1399. (make-variable-buffer-local 'org-todo-kwd-alist)
  1400. (defvar org-todo-key-alist nil)
  1401. (make-variable-buffer-local 'org-todo-key-alist)
  1402. (defvar org-todo-key-trigger nil)
  1403. (make-variable-buffer-local 'org-todo-key-trigger)
  1404. (defcustom org-todo-interpretation 'sequence
  1405. "Controls how TODO keywords are interpreted.
  1406. This variable is in principle obsolete and is only used for
  1407. backward compatibility, if the interpretation of todo keywords is
  1408. not given already in `org-todo-keywords'. See that variable for
  1409. more information."
  1410. :group 'org-todo
  1411. :group 'org-keywords
  1412. :type '(choice (const sequence)
  1413. (const type)))
  1414. (defcustom org-use-fast-todo-selection t
  1415. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1416. This variable describes if and under what circumstances the cycling
  1417. mechanism for TODO keywords will be replaced by a single-key, direct
  1418. selection scheme.
  1419. When nil, fast selection is never used.
  1420. When the symbol `prefix', it will be used when `org-todo' is called with
  1421. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1422. in an agenda buffer.
  1423. When t, fast selection is used by default. In this case, the prefix
  1424. argument forces cycling instead.
  1425. In all cases, the special interface is only used if access keys have actually
  1426. been assigned by the user, i.e. if keywords in the configuration are followed
  1427. by a letter in parenthesis, like TODO(t)."
  1428. :group 'org-todo
  1429. :type '(choice
  1430. (const :tag "Never" nil)
  1431. (const :tag "By default" t)
  1432. (const :tag "Only with C-u C-c C-t" prefix)))
  1433. (defcustom org-provide-todo-statistics t
  1434. "Non-nil means, update todo statistics after insert and toggle.
  1435. When this is set, todo statistics is updated in the parent of the current
  1436. entry each time a todo state is changed."
  1437. :group 'org-todo
  1438. :type 'boolean)
  1439. (defcustom org-after-todo-state-change-hook nil
  1440. "Hook which is run after the state of a TODO item was changed.
  1441. The new state (a string with a TODO keyword, or nil) is available in the
  1442. Lisp variable `state'."
  1443. :group 'org-todo
  1444. :type 'hook)
  1445. (defvar org-blocker-hook nil
  1446. "Hook for functions that are allowed to block a state change.
  1447. Each function gets as its single argument a property list, see
  1448. `org-trigger-hook' for more information about this list.
  1449. If any of the functions in this hook returns nil, the state change
  1450. is blocked.")
  1451. (defvar org-trigger-hook nil
  1452. "Hook for functions that are triggered by a state change.
  1453. Each function gets as its single argument a property list with at least
  1454. the following elements:
  1455. (:type type-of-change :position pos-at-entry-start
  1456. :from old-state :to new-state)
  1457. Depending on the type, more properties may be present.
  1458. This mechanism is currently implemented for:
  1459. TODO state changes
  1460. ------------------
  1461. :type todo-state-change
  1462. :from previous state (keyword as a string), or nil, or a symbol
  1463. 'todo' or 'done', to indicate the general type of state.
  1464. :to new state, like in :from")
  1465. (defcustom org-enforce-todo-dependencies nil
  1466. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1467. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1468. be blocked if any prior sibling is not yet done.
  1469. This variable needs to be set before org.el is loaded, and you need to
  1470. restart Emacs after a change to make the change effective. The only way
  1471. to change is while Emacs is running is through the customize interface."
  1472. :set (lambda (var val)
  1473. (set var val)
  1474. (if val
  1475. (add-hook 'org-blocker-hook
  1476. 'org-block-todo-from-children-or-siblings)
  1477. (remove-hook 'org-blocker-hook
  1478. 'org-block-todo-from-children-or-siblings)))
  1479. :group 'org-todo
  1480. :type 'boolean)
  1481. (defcustom org-enforce-todo-checkbox-dependencies nil
  1482. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1483. When this is nil, checkboxes have no influence on switching TODO states.
  1484. When non-nil, you first need to check off all check boxes before the TODO
  1485. entry can be switched to DONE.
  1486. This variable needs to be set before org.el is loaded, and you need to
  1487. restart Emacs after a change to make the change effective. The only way
  1488. to change is while Emacs is running is through the customize interface."
  1489. :set (lambda (var val)
  1490. (set var val)
  1491. (if val
  1492. (add-hook 'org-blocker-hook
  1493. 'org-block-todo-from-checkboxes)
  1494. (remove-hook 'org-blocker-hook
  1495. 'org-block-todo-from-checkboxes)))
  1496. :group 'org-todo
  1497. :type 'boolean)
  1498. (defcustom org-todo-state-tags-triggers nil
  1499. "Tag changes that should be triggered by TODO state changes.
  1500. This is a list. Each entry is
  1501. (state-change (tag . flag) .......)
  1502. State-change can be a string with a state, and empty string to indicate the
  1503. state that has no TODO keyword, or it can be one of the symbols `todo'
  1504. or `done', meaning any not-done or done state, respectively."
  1505. :group 'org-todo
  1506. :group 'org-tags
  1507. :type '(repeat
  1508. (cons (choice :tag "When changing to"
  1509. (const :tag "Not-done state" todo)
  1510. (const :tag "Done state" done)
  1511. (string :tag "State"))
  1512. (repeat
  1513. (cons :tag "Tag action"
  1514. (string :tag "Tag")
  1515. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1516. (defcustom org-log-done nil
  1517. "Information to record when a task moves to the DONE state.
  1518. Possible values are:
  1519. nil Don't add anything, just change the keyword
  1520. time Add a time stamp to the task
  1521. note Prompt a closing note and add it with template `org-log-note-headings'
  1522. This option can also be set with on a per-file-basis with
  1523. #+STARTUP: nologdone
  1524. #+STARTUP: logdone
  1525. #+STARTUP: lognotedone
  1526. You can have local logging settings for a subtree by setting the LOGGING
  1527. property to one or more of these keywords."
  1528. :group 'org-todo
  1529. :group 'org-progress
  1530. :type '(choice
  1531. (const :tag "No logging" nil)
  1532. (const :tag "Record CLOSED timestamp" time)
  1533. (const :tag "Record CLOSED timestamp with closing note." note)))
  1534. ;; Normalize old uses of org-log-done.
  1535. (cond
  1536. ((eq org-log-done t) (setq org-log-done 'time))
  1537. ((and (listp org-log-done) (memq 'done org-log-done))
  1538. (setq org-log-done 'note)))
  1539. (defcustom org-log-note-clock-out nil
  1540. "Non-nil means, record a note when clocking out of an item.
  1541. This can also be configured on a per-file basis by adding one of
  1542. the following lines anywhere in the buffer:
  1543. #+STARTUP: lognoteclock-out
  1544. #+STARTUP: nolognoteclock-out"
  1545. :group 'org-todo
  1546. :group 'org-progress
  1547. :type 'boolean)
  1548. (defcustom org-log-done-with-time t
  1549. "Non-nil means, the CLOSED time stamp will contain date and time.
  1550. When nil, only the date will be recorded."
  1551. :group 'org-progress
  1552. :type 'boolean)
  1553. (defcustom org-log-note-headings
  1554. '((done . "CLOSING NOTE %t")
  1555. (state . "State %-12s from %-12S %t")
  1556. (note . "Note taken on %t")
  1557. (clock-out . ""))
  1558. "Headings for notes added to entries.
  1559. The value is an alist, with the car being a symbol indicating the note
  1560. context, and the cdr is the heading to be used. The heading may also be the
  1561. empty string.
  1562. %t in the heading will be replaced by a time stamp.
  1563. %s will be replaced by the new TODO state, in double quotes.
  1564. %S will be replaced by the old TODO state, in double quotes.
  1565. %u will be replaced by the user name.
  1566. %U will be replaced by the full user name."
  1567. :group 'org-todo
  1568. :group 'org-progress
  1569. :type '(list :greedy t
  1570. (cons (const :tag "Heading when closing an item" done) string)
  1571. (cons (const :tag
  1572. "Heading when changing todo state (todo sequence only)"
  1573. state) string)
  1574. (cons (const :tag "Heading when just taking a note" note) string)
  1575. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1576. (unless (assq 'note org-log-note-headings)
  1577. (push '(note . "%t") org-log-note-headings))
  1578. (defcustom org-log-into-drawer nil
  1579. "Non-nil means, insert state change notes and time stamps into a drawer.
  1580. When nil, state changes notes will be inserted after the headline and
  1581. any scheduling and clock lines, but not inside a drawer.
  1582. The value of this variable should be the name of the drawer to use.
  1583. LOGBOOK is proposed at the default drawer for this purpose, you can
  1584. also set this to a string to define the drawer of your choice.
  1585. A value of t is also allowed, representing \"LOGBOOK\".
  1586. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1587. will be ognored."
  1588. :group 'org-todo
  1589. :group 'org-progress
  1590. :type '(choice
  1591. (const :tag "Not into a drawer" nil)
  1592. (const :tag "LOGBOOK" t)
  1593. (string :tag "Other")))
  1594. (if (fboundp 'defvaralias)
  1595. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1596. (defcustom org-log-state-notes-insert-after-drawers nil
  1597. "Non-nil means, insert state change notes after any drawers in entry.
  1598. Only the drawers that *immediately* follow the headline and the
  1599. deadline/scheduled line are skipped.
  1600. When nil, insert notes right after the heading and perhaps the line
  1601. with deadline/scheduling if present.
  1602. This variable will have no effect if `org-log-into-drawer' is
  1603. set."
  1604. :group 'org-todo
  1605. :group 'org-progress
  1606. :type 'boolean)
  1607. (defcustom org-log-states-order-reversed t
  1608. "Non-nil means, the latest state change note will be directly after heading.
  1609. When nil, the notes will be orderer according to time."
  1610. :group 'org-todo
  1611. :group 'org-progress
  1612. :type 'boolean)
  1613. (defcustom org-log-repeat 'time
  1614. "Non-nil means, record moving through the DONE state when triggering repeat.
  1615. An auto-repeating tasks is immediately switched back to TODO when marked
  1616. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1617. the TODO keyword definition, or recording a closing note by setting
  1618. `org-log-done', there will be no record of the task moving through DONE.
  1619. This variable forces taking a note anyway. Possible values are:
  1620. nil Don't force a record
  1621. time Record a time stamp
  1622. note Record a note
  1623. This option can also be set with on a per-file-basis with
  1624. #+STARTUP: logrepeat
  1625. #+STARTUP: lognoterepeat
  1626. #+STARTUP: nologrepeat
  1627. You can have local logging settings for a subtree by setting the LOGGING
  1628. property to one or more of these keywords."
  1629. :group 'org-todo
  1630. :group 'org-progress
  1631. :type '(choice
  1632. (const :tag "Don't force a record" nil)
  1633. (const :tag "Force recording the DONE state" time)
  1634. (const :tag "Force recording a note with the DONE state" note)))
  1635. (defgroup org-priorities nil
  1636. "Priorities in Org-mode."
  1637. :tag "Org Priorities"
  1638. :group 'org-todo)
  1639. (defcustom org-highest-priority ?A
  1640. "The highest priority of TODO items. A character like ?A, ?B etc.
  1641. Must have a smaller ASCII number than `org-lowest-priority'."
  1642. :group 'org-priorities
  1643. :type 'character)
  1644. (defcustom org-lowest-priority ?C
  1645. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1646. Must have a larger ASCII number than `org-highest-priority'."
  1647. :group 'org-priorities
  1648. :type 'character)
  1649. (defcustom org-default-priority ?B
  1650. "The default priority of TODO items.
  1651. This is the priority an item get if no explicit priority is given."
  1652. :group 'org-priorities
  1653. :type 'character)
  1654. (defcustom org-priority-start-cycle-with-default t
  1655. "Non-nil means, start with default priority when starting to cycle.
  1656. When this is nil, the first step in the cycle will be (depending on the
  1657. command used) one higher or lower that the default priority."
  1658. :group 'org-priorities
  1659. :type 'boolean)
  1660. (defgroup org-time nil
  1661. "Options concerning time stamps and deadlines in Org-mode."
  1662. :tag "Org Time"
  1663. :group 'org)
  1664. (defcustom org-insert-labeled-timestamps-at-point nil
  1665. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1666. When nil, these labeled time stamps are forces into the second line of an
  1667. entry, just after the headline. When scheduling from the global TODO list,
  1668. the time stamp will always be forced into the second line."
  1669. :group 'org-time
  1670. :type 'boolean)
  1671. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1672. "Formats for `format-time-string' which are used for time stamps.
  1673. It is not recommended to change this constant.")
  1674. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1675. "Number of minutes to round time stamps to.
  1676. These are two values, the first applies when first creating a time stamp.
  1677. The second applies when changing it with the commands `S-up' and `S-down'.
  1678. When changing the time stamp, this means that it will change in steps
  1679. of N minutes, as given by the second value.
  1680. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1681. numbers should be factors of 60, so for example 5, 10, 15.
  1682. When this is larger than 1, you can still force an exact time-stamp by using
  1683. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1684. and by using a prefix arg to `S-up/down' to specify the exact number
  1685. of minutes to shift."
  1686. :group 'org-time
  1687. :get '(lambda (var) ; Make sure all entries have 5 elements
  1688. (if (integerp (default-value var))
  1689. (list (default-value var) 5)
  1690. (default-value var)))
  1691. :type '(list
  1692. (integer :tag "when inserting times")
  1693. (integer :tag "when modifying times")))
  1694. ;; Normalize old customizations of this variable.
  1695. (when (integerp org-time-stamp-rounding-minutes)
  1696. (setq org-time-stamp-rounding-minutes
  1697. (list org-time-stamp-rounding-minutes
  1698. org-time-stamp-rounding-minutes)))
  1699. (defcustom org-display-custom-times nil
  1700. "Non-nil means, overlay custom formats over all time stamps.
  1701. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1702. To turn this on on a per-file basis, insert anywhere in the file:
  1703. #+STARTUP: customtime"
  1704. :group 'org-time
  1705. :set 'set-default
  1706. :type 'sexp)
  1707. (make-variable-buffer-local 'org-display-custom-times)
  1708. (defcustom org-time-stamp-custom-formats
  1709. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1710. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1711. These are overlayed over the default ISO format if the variable
  1712. `org-display-custom-times' is set. Time like %H:%M should be at the
  1713. end of the second format."
  1714. :group 'org-time
  1715. :type 'sexp)
  1716. (defun org-time-stamp-format (&optional long inactive)
  1717. "Get the right format for a time string."
  1718. (let ((f (if long (cdr org-time-stamp-formats)
  1719. (car org-time-stamp-formats))))
  1720. (if inactive
  1721. (concat "[" (substring f 1 -1) "]")
  1722. f)))
  1723. (defcustom org-time-clocksum-format "%d:%02d"
  1724. "The format string used when creating CLOCKSUM lines, or when
  1725. org-mode generates a time duration."
  1726. :group 'org-time
  1727. :type 'string)
  1728. (defcustom org-deadline-warning-days 14
  1729. "No. of days before expiration during which a deadline becomes active.
  1730. This variable governs the display in sparse trees and in the agenda.
  1731. When 0 or negative, it means use this number (the absolute value of it)
  1732. even if a deadline has a different individual lead time specified."
  1733. :group 'org-time
  1734. :group 'org-agenda-daily/weekly
  1735. :type 'number)
  1736. (defcustom org-read-date-prefer-future t
  1737. "Non-nil means, assume future for incomplete date input from user.
  1738. This affects the following situations:
  1739. 1. The user gives a day, but no month.
  1740. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1741. read this as the third of *next* month. However, if you enter \"17\",
  1742. it will be considered as *this* month.
  1743. 2. The user gives a month but not a year.
  1744. For example, if it is april and you enter \"feb 2\", this will be read
  1745. as feb 2, *next* year. \"May 5\", however, will be this year.
  1746. Currently this does not work for ISO week specifications.
  1747. When this option is nil, the current month and year will always be used
  1748. as defaults."
  1749. :group 'org-time
  1750. :type 'boolean)
  1751. (defcustom org-read-date-display-live t
  1752. "Non-nil means, display current interpretation of date prompt live.
  1753. This display will be in an overlay, in the minibuffer."
  1754. :group 'org-time
  1755. :type 'boolean)
  1756. (defcustom org-read-date-popup-calendar t
  1757. "Non-nil means, pop up a calendar when prompting for a date.
  1758. In the calendar, the date can be selected with mouse-1. However, the
  1759. minibuffer will also be active, and you can simply enter the date as well.
  1760. When nil, only the minibuffer will be available."
  1761. :group 'org-time
  1762. :type 'boolean)
  1763. (if (fboundp 'defvaralias)
  1764. (defvaralias 'org-popup-calendar-for-date-prompt
  1765. 'org-read-date-popup-calendar))
  1766. (defcustom org-extend-today-until 0
  1767. "The hour when your day really ends. Must be an integer.
  1768. This has influence for the following applications:
  1769. - When switching the agenda to \"today\". It it is still earlier than
  1770. the time given here, the day recognized as TODAY is actually yesterday.
  1771. - When a date is read from the user and it is still before the time given
  1772. here, the current date and time will be assumed to be yesterday, 23:59.
  1773. Also, timestamps inserted in remember templates follow this rule.
  1774. IMPORTANT: This is a feature whose implementation is and likely will
  1775. remain incomplete. Really, it is only here because past midnight seems to
  1776. be the favorite working time of John Wiegley :-)"
  1777. :group 'org-time
  1778. :type 'number)
  1779. (defcustom org-edit-timestamp-down-means-later nil
  1780. "Non-nil means, S-down will increase the time in a time stamp.
  1781. When nil, S-up will increase."
  1782. :group 'org-time
  1783. :type 'boolean)
  1784. (defcustom org-calendar-follow-timestamp-change t
  1785. "Non-nil means, make the calendar window follow timestamp changes.
  1786. When a timestamp is modified and the calendar window is visible, it will be
  1787. moved to the new date."
  1788. :group 'org-time
  1789. :type 'boolean)
  1790. (defgroup org-tags nil
  1791. "Options concerning tags in Org-mode."
  1792. :tag "Org Tags"
  1793. :group 'org)
  1794. (defcustom org-tag-alist nil
  1795. "List of tags allowed in Org-mode files.
  1796. When this list is nil, Org-mode will base TAG input on what is already in the
  1797. buffer.
  1798. The value of this variable is an alist, the car of each entry must be a
  1799. keyword as a string, the cdr may be a character that is used to select
  1800. that tag through the fast-tag-selection interface.
  1801. See the manual for details."
  1802. :group 'org-tags
  1803. :type '(repeat
  1804. (choice
  1805. (cons (string :tag "Tag name")
  1806. (character :tag "Access char"))
  1807. (const :tag "Start radio group" (:startgroup))
  1808. (const :tag "End radio group" (:endgroup)))))
  1809. (defvar org-file-tags nil
  1810. "List of tags that can be inherited by all entries in the file.
  1811. The tags will be inherited if the variable `org-use-tag-inheritance'
  1812. says they should be.
  1813. This variable is populated from #+TAG lines.")
  1814. (defcustom org-use-fast-tag-selection 'auto
  1815. "Non-nil means, use fast tag selection scheme.
  1816. This is a special interface to select and deselect tags with single keys.
  1817. When nil, fast selection is never used.
  1818. When the symbol `auto', fast selection is used if and only if selection
  1819. characters for tags have been configured, either through the variable
  1820. `org-tag-alist' or through a #+TAGS line in the buffer.
  1821. When t, fast selection is always used and selection keys are assigned
  1822. automatically if necessary."
  1823. :group 'org-tags
  1824. :type '(choice
  1825. (const :tag "Always" t)
  1826. (const :tag "Never" nil)
  1827. (const :tag "When selection characters are configured" 'auto)))
  1828. (defcustom org-fast-tag-selection-single-key nil
  1829. "Non-nil means, fast tag selection exits after first change.
  1830. When nil, you have to press RET to exit it.
  1831. During fast tag selection, you can toggle this flag with `C-c'.
  1832. This variable can also have the value `expert'. In this case, the window
  1833. displaying the tags menu is not even shown, until you press C-c again."
  1834. :group 'org-tags
  1835. :type '(choice
  1836. (const :tag "No" nil)
  1837. (const :tag "Yes" t)
  1838. (const :tag "Expert" expert)))
  1839. (defvar org-fast-tag-selection-include-todo nil
  1840. "Non-nil means, fast tags selection interface will also offer TODO states.
  1841. This is an undocumented feature, you should not rely on it.")
  1842. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1843. "The column to which tags should be indented in a headline.
  1844. If this number is positive, it specifies the column. If it is negative,
  1845. it means that the tags should be flushright to that column. For example,
  1846. -80 works well for a normal 80 character screen."
  1847. :group 'org-tags
  1848. :type 'integer)
  1849. (defcustom org-auto-align-tags t
  1850. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1851. These operations change the length of a headline and therefore shift
  1852. the tags around. With this options turned on, after each such operation
  1853. the tags are again aligned to `org-tags-column'."
  1854. :group 'org-tags
  1855. :type 'boolean)
  1856. (defcustom org-use-tag-inheritance t
  1857. "Non-nil means, tags in levels apply also for sublevels.
  1858. When nil, only the tags directly given in a specific line apply there.
  1859. This may also be a list of tags that should be inherited, or a regexp that
  1860. matches tags that should be inherited. Additional control is possible
  1861. with the variable `org-tags-exclude-from-inheritance' which gives an
  1862. explicit list of tags to be excluded from inheritance., even if the value of
  1863. `org-use-tag-inheritance' would select it for inheritance.
  1864. If this option is t, a match early-on in a tree can lead to a large
  1865. number of matches in the subtree when constructing the agenda or creating
  1866. a sparse tree. If you only want to see the first match in a tree during
  1867. a search, check out the variable `org-tags-match-list-sublevels'."
  1868. :group 'org-tags
  1869. :type '(choice
  1870. (const :tag "Not" nil)
  1871. (const :tag "Always" t)
  1872. (repeat :tag "Specific tags" (string :tag "Tag"))
  1873. (regexp :tag "Tags matched by regexp")))
  1874. (defcustom org-tags-exclude-from-inheritance nil
  1875. "List of tags that should never be inherited.
  1876. This is a way to exclude a few tags from inheritance. For way to do
  1877. the opposite, to actively allow inheritance for selected tags,
  1878. see the variable `org-use-tag-inheritance'."
  1879. :group 'org-tags
  1880. :type '(repeat (string :tag "Tag")))
  1881. (defun org-tag-inherit-p (tag)
  1882. "Check if TAG is one that should be inherited."
  1883. (cond
  1884. ((member tag org-tags-exclude-from-inheritance) nil)
  1885. ((eq org-use-tag-inheritance t) t)
  1886. ((not org-use-tag-inheritance) nil)
  1887. ((stringp org-use-tag-inheritance)
  1888. (string-match org-use-tag-inheritance tag))
  1889. ((listp org-use-tag-inheritance)
  1890. (member tag org-use-tag-inheritance))
  1891. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1892. (defcustom org-tags-match-list-sublevels t
  1893. "Non-nil means list also sublevels of headlines matching tag search.
  1894. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1895. the sublevels of a headline matching a tag search often also match
  1896. the same search. Listing all of them can create very long lists.
  1897. Setting this variable to nil causes subtrees of a match to be skipped.
  1898. This option is off by default, because inheritance in on. If you turn
  1899. inheritance off, you very likely want to turn this option on.
  1900. As a special case, if the tag search is restricted to TODO items, the
  1901. value of this variable is ignored and sublevels are always checked, to
  1902. make sure all corresponding TODO items find their way into the list.
  1903. This variable is semi-obsolete and probably should always be true. It
  1904. is better to limit inheritance to certain tags using the variables
  1905. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1906. :group 'org-tags
  1907. :type 'boolean)
  1908. (defvar org-tags-history nil
  1909. "History of minibuffer reads for tags.")
  1910. (defvar org-last-tags-completion-table nil
  1911. "The last used completion table for tags.")
  1912. (defvar org-after-tags-change-hook nil
  1913. "Hook that is run after the tags in a line have changed.")
  1914. (defgroup org-properties nil
  1915. "Options concerning properties in Org-mode."
  1916. :tag "Org Properties"
  1917. :group 'org)
  1918. (defcustom org-property-format "%-10s %s"
  1919. "How property key/value pairs should be formatted by `indent-line'.
  1920. When `indent-line' hits a property definition, it will format the line
  1921. according to this format, mainly to make sure that the values are
  1922. lined-up with respect to each other."
  1923. :group 'org-properties
  1924. :type 'string)
  1925. (defcustom org-use-property-inheritance nil
  1926. "Non-nil means, properties apply also for sublevels.
  1927. This setting is chiefly used during property searches. Turning it on can
  1928. cause significant overhead when doing a search, which is why it is not
  1929. on by default.
  1930. When nil, only the properties directly given in the current entry count.
  1931. When t, every property is inherited. The value may also be a list of
  1932. properties that should have inheritance, or a regular expression matching
  1933. properties that should be inherited.
  1934. However, note that some special properties use inheritance under special
  1935. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1936. and the properties ending in \"_ALL\" when they are used as descriptor
  1937. for valid values of a property.
  1938. Note for programmers:
  1939. When querying an entry with `org-entry-get', you can control if inheritance
  1940. should be used. By default, `org-entry-get' looks only at the local
  1941. properties. You can request inheritance by setting the inherit argument
  1942. to t (to force inheritance) or to `selective' (to respect the setting
  1943. in this variable)."
  1944. :group 'org-properties
  1945. :type '(choice
  1946. (const :tag "Not" nil)
  1947. (const :tag "Always" t)
  1948. (repeat :tag "Specific properties" (string :tag "Property"))
  1949. (regexp :tag "Properties matched by regexp")))
  1950. (defun org-property-inherit-p (property)
  1951. "Check if PROPERTY is one that should be inherited."
  1952. (cond
  1953. ((eq org-use-property-inheritance t) t)
  1954. ((not org-use-property-inheritance) nil)
  1955. ((stringp org-use-property-inheritance)
  1956. (string-match org-use-property-inheritance property))
  1957. ((listp org-use-property-inheritance)
  1958. (member property org-use-property-inheritance))
  1959. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1960. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1961. "The default column format, if no other format has been defined.
  1962. This variable can be set on the per-file basis by inserting a line
  1963. #+COLUMNS: %25ITEM ....."
  1964. :group 'org-properties
  1965. :type 'string)
  1966. (defcustom org-columns-ellipses ".."
  1967. "The ellipses to be used when a field in column view is truncated.
  1968. When this is the empty string, as many characters as possible are shown,
  1969. but then there will be no visual indication that the field has been truncated.
  1970. When this is a string of length N, the last N characters of a truncated
  1971. field are replaced by this string. If the column is narrower than the
  1972. ellipses string, only part of the ellipses string will be shown."
  1973. :group 'org-properties
  1974. :type 'string)
  1975. (defcustom org-columns-modify-value-for-display-function nil
  1976. "Function that modifies values for display in column view.
  1977. For example, it can be used to cut out a certain part from a time stamp.
  1978. The function must take 2 arguments:
  1979. column-title The title of the column (*not* the property name)
  1980. value The value that should be modified.
  1981. The function should return the value that should be displayed,
  1982. or nil if the normal value should be used."
  1983. :group 'org-properties
  1984. :type 'function)
  1985. (defcustom org-effort-property "Effort"
  1986. "The property that is being used to keep track of effort estimates.
  1987. Effort estimates given in this property need to have the format H:MM."
  1988. :group 'org-properties
  1989. :group 'org-progress
  1990. :type '(string :tag "Property"))
  1991. (defconst org-global-properties-fixed
  1992. '(("VISIBILITY_ALL" . "folded children content all"))
  1993. "List of property/value pairs that can be inherited by any entry.
  1994. These are fixed values, for the preset properties.")
  1995. (defcustom org-global-properties nil
  1996. "List of property/value pairs that can be inherited by any entry.
  1997. You can set buffer-local values for the same purpose in the variable
  1998. `org-file-properties' this by adding lines like
  1999. #+PROPERTY: NAME VALUE"
  2000. :group 'org-properties
  2001. :type '(repeat
  2002. (cons (string :tag "Property")
  2003. (string :tag "Value"))))
  2004. (defvar org-file-properties nil
  2005. "List of property/value pairs that can be inherited by any entry.
  2006. Valid for the current buffer.
  2007. This variable is populated from #+PROPERTY lines.")
  2008. (make-variable-buffer-local 'org-file-properties)
  2009. (defgroup org-agenda nil
  2010. "Options concerning agenda views in Org-mode."
  2011. :tag "Org Agenda"
  2012. :group 'org)
  2013. (defvar org-category nil
  2014. "Variable used by org files to set a category for agenda display.
  2015. Such files should use a file variable to set it, for example
  2016. # -*- mode: org; org-category: \"ELisp\"
  2017. or contain a special line
  2018. #+CATEGORY: ELisp
  2019. If the file does not specify a category, then file's base name
  2020. is used instead.")
  2021. (make-variable-buffer-local 'org-category)
  2022. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2023. (defcustom org-agenda-files nil
  2024. "The files to be used for agenda display.
  2025. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2026. \\[org-remove-file]. You can also use customize to edit the list.
  2027. If an entry is a directory, all files in that directory that are matched by
  2028. `org-agenda-file-regexp' will be part of the file list.
  2029. If the value of the variable is not a list but a single file name, then
  2030. the list of agenda files is actually stored and maintained in that file, one
  2031. agenda file per line."
  2032. :group 'org-agenda
  2033. :type '(choice
  2034. (repeat :tag "List of files and directories" file)
  2035. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2036. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2037. "Regular expression to match files for `org-agenda-files'.
  2038. If any element in the list in that variable contains a directory instead
  2039. of a normal file, all files in that directory that are matched by this
  2040. regular expression will be included."
  2041. :group 'org-agenda
  2042. :type 'regexp)
  2043. (defcustom org-agenda-text-search-extra-files nil
  2044. "List of extra files to be searched by text search commands.
  2045. These files will be search in addition to the agenda files by the
  2046. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2047. Note that these files will only be searched for text search commands,
  2048. not for the other agenda views like todo lists, tag searches or the weekly
  2049. agenda. This variable is intended to list notes and possibly archive files
  2050. that should also be searched by these two commands.
  2051. In fact, if the first element in the list is the symbol `agenda-archives',
  2052. than all archive files of all agenda files will be added to the search
  2053. scope."
  2054. :group 'org-agenda
  2055. :type '(set :greedy t
  2056. (const :tag "Agenda Archives" agenda-archives)
  2057. (repeat :inline t (file))))
  2058. (if (fboundp 'defvaralias)
  2059. (defvaralias 'org-agenda-multi-occur-extra-files
  2060. 'org-agenda-text-search-extra-files))
  2061. (defcustom org-agenda-skip-unavailable-files nil
  2062. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2063. A nil value means to remove them, after a query, from the list."
  2064. :group 'org-agenda
  2065. :type 'boolean)
  2066. (defcustom org-calendar-to-agenda-key [?c]
  2067. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2068. The command `org-calendar-goto-agenda' will be bound to this key. The
  2069. default is the character `c' because then `c' can be used to switch back and
  2070. forth between agenda and calendar."
  2071. :group 'org-agenda
  2072. :type 'sexp)
  2073. (defcustom org-calendar-agenda-action-key [?k]
  2074. "The key to be installed in `calendar-mode-map' for agenda-action.
  2075. The command `org-agenda-action' will be bound to this key. The
  2076. default is the character `k' because we use the same key in the agenda."
  2077. :group 'org-agenda
  2078. :type 'sexp)
  2079. (eval-after-load "calendar"
  2080. '(progn
  2081. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2082. 'org-calendar-goto-agenda)
  2083. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2084. 'org-agenda-action)))
  2085. (defgroup org-latex nil
  2086. "Options for embedding LaTeX code into Org-mode."
  2087. :tag "Org LaTeX"
  2088. :group 'org)
  2089. (defcustom org-format-latex-options
  2090. '(:foreground default :background default :scale 1.0
  2091. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2092. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2093. "Options for creating images from LaTeX fragments.
  2094. This is a property list with the following properties:
  2095. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2096. `default' means use the foreground of the default face.
  2097. :background the background color, or \"Transparent\".
  2098. `default' means use the background of the default face.
  2099. :scale a scaling factor for the size of the images.
  2100. :html-foreground, :html-background, :html-scale
  2101. the same numbers for HTML export.
  2102. :matchers a list indicating which matchers should be used to
  2103. find LaTeX fragments. Valid members of this list are:
  2104. \"begin\" find environments
  2105. \"$1\" find single characters surrounded by $.$
  2106. \"$\" find math expressions surrounded by $...$
  2107. \"$$\" find math expressions surrounded by $$....$$
  2108. \"\\(\" find math expressions surrounded by \\(...\\)
  2109. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2110. :group 'org-latex
  2111. :type 'plist)
  2112. (defcustom org-format-latex-header "\\documentclass{article}
  2113. \\usepackage{fullpage} % do not remove
  2114. \\usepackage{amssymb}
  2115. \\usepackage[usenames]{color}
  2116. \\usepackage{amsmath}
  2117. \\usepackage{latexsym}
  2118. \\usepackage[mathscr]{eucal}
  2119. \\pagestyle{empty} % do not remove"
  2120. "The document header used for processing LaTeX fragments."
  2121. :group 'org-latex
  2122. :type 'string)
  2123. (defgroup org-font-lock nil
  2124. "Font-lock settings for highlighting in Org-mode."
  2125. :tag "Org Font Lock"
  2126. :group 'org)
  2127. (defcustom org-level-color-stars-only nil
  2128. "Non-nil means fontify only the stars in each headline.
  2129. When nil, the entire headline is fontified.
  2130. Changing it requires restart of `font-lock-mode' to become effective
  2131. also in regions already fontified."
  2132. :group 'org-font-lock
  2133. :type 'boolean)
  2134. (defcustom org-hide-leading-stars nil
  2135. "Non-nil means, hide the first N-1 stars in a headline.
  2136. This works by using the face `org-hide' for these stars. This
  2137. face is white for a light background, and black for a dark
  2138. background. You may have to customize the face `org-hide' to
  2139. make this work.
  2140. Changing it requires restart of `font-lock-mode' to become effective
  2141. also in regions already fontified.
  2142. You may also set this on a per-file basis by adding one of the following
  2143. lines to the buffer:
  2144. #+STARTUP: hidestars
  2145. #+STARTUP: showstars"
  2146. :group 'org-font-lock
  2147. :type 'boolean)
  2148. (defcustom org-fontify-done-headline nil
  2149. "Non-nil means, change the face of a headline if it is marked DONE.
  2150. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2151. When this is non-nil, the headline after the keyword is set to the
  2152. `org-headline-done' as an additional indication."
  2153. :group 'org-font-lock
  2154. :type 'boolean)
  2155. (defcustom org-fontify-emphasized-text t
  2156. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2157. Changing this variable requires a restart of Emacs to take effect."
  2158. :group 'org-font-lock
  2159. :type 'boolean)
  2160. (defcustom org-highlight-latex-fragments-and-specials nil
  2161. "Non-nil means, fontify what is treated specially by the exporters."
  2162. :group 'org-font-lock
  2163. :type 'boolean)
  2164. (defcustom org-hide-emphasis-markers nil
  2165. "Non-nil mean font-lock should hide the emphasis marker characters."
  2166. :group 'org-font-lock
  2167. :type 'boolean)
  2168. (defvar org-emph-re nil
  2169. "Regular expression for matching emphasis.")
  2170. (defvar org-verbatim-re nil
  2171. "Regular expression for matching verbatim text.")
  2172. (defvar org-emphasis-regexp-components) ; defined just below
  2173. (defvar org-emphasis-alist) ; defined just below
  2174. (defun org-set-emph-re (var val)
  2175. "Set variable and compute the emphasis regular expression."
  2176. (set var val)
  2177. (when (and (boundp 'org-emphasis-alist)
  2178. (boundp 'org-emphasis-regexp-components)
  2179. org-emphasis-alist org-emphasis-regexp-components)
  2180. (let* ((e org-emphasis-regexp-components)
  2181. (pre (car e))
  2182. (post (nth 1 e))
  2183. (border (nth 2 e))
  2184. (body (nth 3 e))
  2185. (nl (nth 4 e))
  2186. (body1 (concat body "*?"))
  2187. (markers (mapconcat 'car org-emphasis-alist ""))
  2188. (vmarkers (mapconcat
  2189. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2190. org-emphasis-alist "")))
  2191. ;; make sure special characters appear at the right position in the class
  2192. (if (string-match "\\^" markers)
  2193. (setq markers (concat (replace-match "" t t markers) "^")))
  2194. (if (string-match "-" markers)
  2195. (setq markers (concat (replace-match "" t t markers) "-")))
  2196. (if (string-match "\\^" vmarkers)
  2197. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2198. (if (string-match "-" vmarkers)
  2199. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2200. (if (> nl 0)
  2201. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2202. (int-to-string nl) "\\}")))
  2203. ;; Make the regexp
  2204. (setq org-emph-re
  2205. (concat "\\([" pre "]\\|^\\)"
  2206. "\\("
  2207. "\\([" markers "]\\)"
  2208. "\\("
  2209. "[^" border "]\\|"
  2210. "[^" border "]"
  2211. body1
  2212. "[^" border "]"
  2213. "\\)"
  2214. "\\3\\)"
  2215. "\\([" post "]\\|$\\)"))
  2216. (setq org-verbatim-re
  2217. (concat "\\([" pre "]\\|^\\)"
  2218. "\\("
  2219. "\\([" vmarkers "]\\)"
  2220. "\\("
  2221. "[^" border "]\\|"
  2222. "[^" border "]"
  2223. body1
  2224. "[^" border "]"
  2225. "\\)"
  2226. "\\3\\)"
  2227. "\\([" post "]\\|$\\)")))))
  2228. (defcustom org-emphasis-regexp-components
  2229. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2230. "Components used to build the regular expression for emphasis.
  2231. This is a list with 6 entries. Terminology: In an emphasis string
  2232. like \" *strong word* \", we call the initial space PREMATCH, the final
  2233. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2234. and \"trong wor\" is the body. The different components in this variable
  2235. specify what is allowed/forbidden in each part:
  2236. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2237. post Chars allowed as postmatch. End of line will be allowed too.
  2238. border The chars *forbidden* as border characters.
  2239. body-regexp A regexp like \".\" to match a body character. Don't use
  2240. non-shy groups here, and don't allow newline here.
  2241. newline The maximum number of newlines allowed in an emphasis exp.
  2242. Use customize to modify this, or restart Emacs after changing it."
  2243. :group 'org-font-lock
  2244. :set 'org-set-emph-re
  2245. :type '(list
  2246. (sexp :tag "Allowed chars in pre ")
  2247. (sexp :tag "Allowed chars in post ")
  2248. (sexp :tag "Forbidden chars in border ")
  2249. (sexp :tag "Regexp for body ")
  2250. (integer :tag "number of newlines allowed")
  2251. (option (boolean :tag "Please ignore this button"))))
  2252. (defcustom org-emphasis-alist
  2253. `(("*" bold "<b>" "</b>")
  2254. ("/" italic "<i>" "</i>")
  2255. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2256. ("=" org-code "<code>" "</code>" verbatim)
  2257. ("~" org-verbatim "<code>" "</code>" verbatim)
  2258. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2259. "<del>" "</del>")
  2260. )
  2261. "Special syntax for emphasized text.
  2262. Text starting and ending with a special character will be emphasized, for
  2263. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2264. characters, the face to be used by font-lock for highlighting in Org-mode
  2265. Emacs buffers, and the HTML tags to be used for this.
  2266. Use customize to modify this, or restart Emacs after changing it."
  2267. :group 'org-font-lock
  2268. :set 'org-set-emph-re
  2269. :type '(repeat
  2270. (list
  2271. (string :tag "Marker character")
  2272. (choice
  2273. (face :tag "Font-lock-face")
  2274. (plist :tag "Face property list"))
  2275. (string :tag "HTML start tag")
  2276. (string :tag "HTML end tag")
  2277. (option (const verbatim)))))
  2278. ;;; Miscellaneous options
  2279. (defgroup org-completion nil
  2280. "Completion in Org-mode."
  2281. :tag "Org Completion"
  2282. :group 'org)
  2283. (defcustom org-completion-use-ido nil
  2284. "Non-nil means, use ido completion wherever possible.
  2285. Note that `ido-mode' must be active for this variable to be relevant.
  2286. If you decide to turn this variable on, you might well want to turn off
  2287. `org-outline-path-complete-in-steps'."
  2288. :group 'org-completion
  2289. :type 'boolean)
  2290. (defcustom org-completion-fallback-command 'hippie-expand
  2291. "The expansion command called by \\[org-complete] in normal context.
  2292. Normal means, no org-mode-specific context."
  2293. :group 'org-completion
  2294. :type 'function)
  2295. ;;; Functions and variables from ther packages
  2296. ;; Declared here to avoid compiler warnings
  2297. ;; XEmacs only
  2298. (defvar outline-mode-menu-heading)
  2299. (defvar outline-mode-menu-show)
  2300. (defvar outline-mode-menu-hide)
  2301. (defvar zmacs-regions) ; XEmacs regions
  2302. ;; Emacs only
  2303. (defvar mark-active)
  2304. ;; Various packages
  2305. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2306. (declare-function calendar-forward-day "cal-move" (arg))
  2307. (declare-function calendar-goto-date "cal-move" (date))
  2308. (declare-function calendar-goto-today "cal-move" ())
  2309. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2310. (defvar calc-embedded-close-formula)
  2311. (defvar calc-embedded-open-formula)
  2312. (declare-function cdlatex-tab "ext:cdlatex" ())
  2313. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2314. (defvar font-lock-unfontify-region-function)
  2315. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2316. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2317. (defvar iswitchb-temp-buflist)
  2318. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2319. (defvar org-agenda-tags-todo-honor-ignore-options)
  2320. (declare-function org-agenda-skip "org-agenda" ())
  2321. (declare-function org-format-agenda-item "org-agenda"
  2322. (extra txt &optional category tags dotime noprefix remove-re))
  2323. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2324. (declare-function org-agenda-change-all-lines "org-agenda"
  2325. (newhead hdmarker &optional fixface just-this))
  2326. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2327. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2328. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2329. (beg end))
  2330. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2331. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2332. "org-agenda" (&optional end))
  2333. (declare-function parse-time-string "parse-time" (string))
  2334. (declare-function remember "remember" (&optional initial))
  2335. (declare-function remember-buffer-desc "remember" ())
  2336. (declare-function remember-finalize "remember" ())
  2337. (defvar remember-save-after-remembering)
  2338. (defvar remember-data-file)
  2339. (defvar remember-register)
  2340. (defvar remember-buffer)
  2341. (defvar remember-handler-functions)
  2342. (defvar remember-annotation-functions)
  2343. (defvar texmathp-why)
  2344. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2345. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2346. (defvar w3m-current-url)
  2347. (defvar w3m-current-title)
  2348. (defvar org-latex-regexps)
  2349. ;;; Autoload and prepare some org modules
  2350. ;; Some table stuff that needs to be defined here, because it is used
  2351. ;; by the functions setting up org-mode or checking for table context.
  2352. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2353. "Detects an org-type or table-type table.")
  2354. (defconst org-table-line-regexp "^[ \t]*|"
  2355. "Detects an org-type table line.")
  2356. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2357. "Detects an org-type table line.")
  2358. (defconst org-table-hline-regexp "^[ \t]*|-"
  2359. "Detects an org-type table hline.")
  2360. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2361. "Detects a table-type table hline.")
  2362. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2363. "Searching from within a table (any type) this finds the first line
  2364. outside the table.")
  2365. ;; Autoload the functions in org-table.el that are needed by functions here.
  2366. (eval-and-compile
  2367. (org-autoload "org-table"
  2368. '(org-table-align org-table-begin org-table-blank-field
  2369. org-table-convert org-table-convert-region org-table-copy-down
  2370. org-table-copy-region org-table-create
  2371. org-table-create-or-convert-from-region
  2372. org-table-create-with-table.el org-table-current-dline
  2373. org-table-cut-region org-table-delete-column org-table-edit-field
  2374. org-table-edit-formulas org-table-end org-table-eval-formula
  2375. org-table-export org-table-field-info
  2376. org-table-get-stored-formulas org-table-goto-column
  2377. org-table-hline-and-move org-table-import org-table-insert-column
  2378. org-table-insert-hline org-table-insert-row org-table-iterate
  2379. org-table-justify-field-maybe org-table-kill-row
  2380. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2381. org-table-move-column org-table-move-column-left
  2382. org-table-move-column-right org-table-move-row
  2383. org-table-move-row-down org-table-move-row-up
  2384. org-table-next-field org-table-next-row org-table-paste-rectangle
  2385. org-table-previous-field org-table-recalculate
  2386. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2387. org-table-toggle-coordinate-overlays
  2388. org-table-toggle-formula-debugger org-table-wrap-region
  2389. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2390. (defun org-at-table-p (&optional table-type)
  2391. "Return t if the cursor is inside an org-type table.
  2392. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2393. (if org-enable-table-editor
  2394. (save-excursion
  2395. (beginning-of-line 1)
  2396. (looking-at (if table-type org-table-any-line-regexp
  2397. org-table-line-regexp)))
  2398. nil))
  2399. (defsubst org-table-p () (org-at-table-p))
  2400. (defun org-at-table.el-p ()
  2401. "Return t if and only if we are at a table.el table."
  2402. (and (org-at-table-p 'any)
  2403. (save-excursion
  2404. (goto-char (org-table-begin 'any))
  2405. (looking-at org-table1-hline-regexp))))
  2406. (defun org-table-recognize-table.el ()
  2407. "If there is a table.el table nearby, recognize it and move into it."
  2408. (if org-table-tab-recognizes-table.el
  2409. (if (org-at-table.el-p)
  2410. (progn
  2411. (beginning-of-line 1)
  2412. (if (looking-at org-table-dataline-regexp)
  2413. nil
  2414. (if (looking-at org-table1-hline-regexp)
  2415. (progn
  2416. (beginning-of-line 2)
  2417. (if (looking-at org-table-any-border-regexp)
  2418. (beginning-of-line -1)))))
  2419. (if (re-search-forward "|" (org-table-end t) t)
  2420. (progn
  2421. (require 'table)
  2422. (if (table--at-cell-p (point))
  2423. t
  2424. (message "recognizing table.el table...")
  2425. (table-recognize-table)
  2426. (message "recognizing table.el table...done")))
  2427. (error "This should not happen..."))
  2428. t)
  2429. nil)
  2430. nil))
  2431. (defun org-at-table-hline-p ()
  2432. "Return t if the cursor is inside a hline in a table."
  2433. (if org-enable-table-editor
  2434. (save-excursion
  2435. (beginning-of-line 1)
  2436. (looking-at org-table-hline-regexp))
  2437. nil))
  2438. (defvar org-table-clean-did-remove-column nil)
  2439. (defun org-table-map-tables (function)
  2440. "Apply FUNCTION to the start of all tables in the buffer."
  2441. (save-excursion
  2442. (save-restriction
  2443. (widen)
  2444. (goto-char (point-min))
  2445. (while (re-search-forward org-table-any-line-regexp nil t)
  2446. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2447. (beginning-of-line 1)
  2448. (if (looking-at org-table-line-regexp)
  2449. (save-excursion (funcall function)))
  2450. (re-search-forward org-table-any-border-regexp nil 1))))
  2451. (message "Mapping tables: done"))
  2452. ;; Declare and autoload functions from org-exp.el
  2453. (declare-function org-default-export-plist "org-exp")
  2454. (declare-function org-infile-export-plist "org-exp")
  2455. (declare-function org-get-current-options "org-exp")
  2456. (eval-and-compile
  2457. (org-autoload "org-exp"
  2458. '(org-export org-export-as-ascii org-export-visible
  2459. org-insert-export-options-template org-export-as-html-and-open
  2460. org-export-as-html-batch org-export-as-html-to-buffer
  2461. org-replace-region-by-html org-export-region-as-html
  2462. org-export-as-html org-export-icalendar-this-file
  2463. org-export-icalendar-all-agenda-files
  2464. org-table-clean-before-export
  2465. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2466. ;; Declare and autoload functions from org-agenda.el
  2467. (eval-and-compile
  2468. (org-autoload "org-agenda"
  2469. '(org-agenda org-agenda-list org-search-view
  2470. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2471. org-diary org-agenda-to-appt
  2472. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2473. ;; Autoload org-remember
  2474. (eval-and-compile
  2475. (org-autoload "org-remember"
  2476. '(org-remember-insinuate org-remember-annotation
  2477. org-remember-apply-template org-remember org-remember-handler)))
  2478. ;; Autoload org-clock.el
  2479. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2480. (beg end))
  2481. (declare-function org-clock-update-mode-line "org-clock" ())
  2482. (defvar org-clock-start-time)
  2483. (defvar org-clock-marker (make-marker)
  2484. "Marker recording the last clock-in.")
  2485. (eval-and-compile
  2486. (org-autoload
  2487. "org-clock"
  2488. '(org-clock-in org-clock-out org-clock-cancel
  2489. org-clock-goto org-clock-sum org-clock-display
  2490. org-clock-remove-overlays org-clock-report
  2491. org-clocktable-shift org-dblock-write:clocktable
  2492. org-get-clocktable)))
  2493. (defun org-clock-update-time-maybe ()
  2494. "If this is a CLOCK line, update it and return t.
  2495. Otherwise, return nil."
  2496. (interactive)
  2497. (save-excursion
  2498. (beginning-of-line 1)
  2499. (skip-chars-forward " \t")
  2500. (when (looking-at org-clock-string)
  2501. (let ((re (concat "[ \t]*" org-clock-string
  2502. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2503. "\\([ \t]*=>.*\\)?\\)?"))
  2504. ts te h m s neg)
  2505. (cond
  2506. ((not (looking-at re))
  2507. nil)
  2508. ((not (match-end 2))
  2509. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2510. (> org-clock-marker (point))
  2511. (<= org-clock-marker (point-at-eol)))
  2512. ;; The clock is running here
  2513. (setq org-clock-start-time
  2514. (apply 'encode-time
  2515. (org-parse-time-string (match-string 1))))
  2516. (org-clock-update-mode-line)))
  2517. (t
  2518. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2519. (end-of-line 1)
  2520. (setq ts (match-string 1)
  2521. te (match-string 3))
  2522. (setq s (- (time-to-seconds
  2523. (apply 'encode-time (org-parse-time-string te)))
  2524. (time-to-seconds
  2525. (apply 'encode-time (org-parse-time-string ts))))
  2526. neg (< s 0)
  2527. s (abs s)
  2528. h (floor (/ s 3600))
  2529. s (- s (* 3600 h))
  2530. m (floor (/ s 60))
  2531. s (- s (* 60 s)))
  2532. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2533. t))))))
  2534. (defun org-check-running-clock ()
  2535. "Check if the current buffer contains the running clock.
  2536. If yes, offer to stop it and to save the buffer with the changes."
  2537. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2538. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2539. (buffer-name))))
  2540. (org-clock-out)
  2541. (when (y-or-n-p "Save changed buffer?")
  2542. (save-buffer))))
  2543. (defun org-clocktable-try-shift (dir n)
  2544. "Check if this line starts a clock table, if yes, shift the time block."
  2545. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2546. (org-clocktable-shift dir n)))
  2547. ;; Autoload org-timer.el
  2548. ;(declare-function org-timer "org-timer")
  2549. (eval-and-compile
  2550. (org-autoload
  2551. "org-timer"
  2552. '(org-timer-start org-timer org-timer-item
  2553. org-timer-change-times-in-region)))
  2554. ;; Autoload archiving code
  2555. ;; The stuff that is needed for cycling and tags has to be defined here.
  2556. (defgroup org-archive nil
  2557. "Options concerning archiving in Org-mode."
  2558. :tag "Org Archive"
  2559. :group 'org-structure)
  2560. (defcustom org-archive-location "%s_archive::"
  2561. "The location where subtrees should be archived.
  2562. The value of this variable is a string, consisting of two parts,
  2563. separated by a double-colon. The first part is a filename and
  2564. the second part is a headline.
  2565. When the filename is omitted, archiving happens in the same file.
  2566. %s in the filename will be replaced by the current file
  2567. name (without the directory part). Archiving to a different file
  2568. is useful to keep archived entries from contributing to the
  2569. Org-mode Agenda.
  2570. The archived entries will be filed as subtrees of the specified
  2571. headline. When the headline is omitted, the subtrees are simply
  2572. filed away at the end of the file, as top-level entries. Also in
  2573. the heading you can use %s to represent the file name, this can be
  2574. useful when using the same archive for a number of different files.
  2575. Here are a few examples:
  2576. \"%s_archive::\"
  2577. If the current file is Projects.org, archive in file
  2578. Projects.org_archive, as top-level trees. This is the default.
  2579. \"::* Archived Tasks\"
  2580. Archive in the current file, under the top-level headline
  2581. \"* Archived Tasks\".
  2582. \"~/org/archive.org::\"
  2583. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2584. \"~/org/archive.org::From %s\"
  2585. Archive in file ~/org/archive.org (absolute path), und headlines
  2586. \"From FILENAME\" where file name is the current file name.
  2587. \"basement::** Finished Tasks\"
  2588. Archive in file ./basement (relative path), as level 3 trees
  2589. below the level 2 heading \"** Finished Tasks\".
  2590. You may set this option on a per-file basis by adding to the buffer a
  2591. line like
  2592. #+ARCHIVE: basement::** Finished Tasks
  2593. You may also define it locally for a subtree by setting an ARCHIVE property
  2594. in the entry. If such a property is found in an entry, or anywhere up
  2595. the hierarchy, it will be used."
  2596. :group 'org-archive
  2597. :type 'string)
  2598. (defcustom org-archive-tag "ARCHIVE"
  2599. "The tag that marks a subtree as archived.
  2600. An archived subtree does not open during visibility cycling, and does
  2601. not contribute to the agenda listings.
  2602. After changing this, font-lock must be restarted in the relevant buffers to
  2603. get the proper fontification."
  2604. :group 'org-archive
  2605. :group 'org-keywords
  2606. :type 'string)
  2607. (defcustom org-agenda-skip-archived-trees t
  2608. "Non-nil means, the agenda will skip any items located in archived trees.
  2609. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2610. variable is no longer recommended, you should leave it at the value t.
  2611. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2612. :group 'org-archive
  2613. :group 'org-agenda-skip
  2614. :type 'boolean)
  2615. (defcustom org-cycle-open-archived-trees nil
  2616. "Non-nil means, `org-cycle' will open archived trees.
  2617. An archived tree is a tree marked with the tag ARCHIVE.
  2618. When nil, archived trees will stay folded. You can still open them with
  2619. normal outline commands like `show-all', but not with the cycling commands."
  2620. :group 'org-archive
  2621. :group 'org-cycle
  2622. :type 'boolean)
  2623. (defcustom org-sparse-tree-open-archived-trees nil
  2624. "Non-nil means sparse tree construction shows matches in archived trees.
  2625. When nil, matches in these trees are highlighted, but the trees are kept in
  2626. collapsed state."
  2627. :group 'org-archive
  2628. :group 'org-sparse-trees
  2629. :type 'boolean)
  2630. (defun org-cycle-hide-archived-subtrees (state)
  2631. "Re-hide all archived subtrees after a visibility state change."
  2632. (when (and (not org-cycle-open-archived-trees)
  2633. (not (memq state '(overview folded))))
  2634. (save-excursion
  2635. (let* ((globalp (memq state '(contents all)))
  2636. (beg (if globalp (point-min) (point)))
  2637. (end (if globalp (point-max) (org-end-of-subtree t))))
  2638. (org-hide-archived-subtrees beg end)
  2639. (goto-char beg)
  2640. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2641. (message "%s" (substitute-command-keys
  2642. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2643. (defun org-force-cycle-archived ()
  2644. "Cycle subtree even if it is archived."
  2645. (interactive)
  2646. (setq this-command 'org-cycle)
  2647. (let ((org-cycle-open-archived-trees t))
  2648. (call-interactively 'org-cycle)))
  2649. (defun org-hide-archived-subtrees (beg end)
  2650. "Re-hide all archived subtrees after a visibility state change."
  2651. (save-excursion
  2652. (let* ((re (concat ":" org-archive-tag ":")))
  2653. (goto-char beg)
  2654. (while (re-search-forward re end t)
  2655. (and (org-on-heading-p) (hide-subtree))
  2656. (org-end-of-subtree t)))))
  2657. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2658. (eval-and-compile
  2659. (org-autoload "org-archive"
  2660. '(org-add-archive-files org-archive-subtree
  2661. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2662. ;; Autoload Column View Code
  2663. (declare-function org-columns-number-to-string "org-colview")
  2664. (declare-function org-columns-get-format-and-top-level "org-colview")
  2665. (declare-function org-columns-compute "org-colview")
  2666. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2667. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2668. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2669. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2670. ;; Autoload ID code
  2671. (declare-function org-id-store-link "org-id")
  2672. (declare-function org-id-locations-load "org-id")
  2673. (declare-function org-id-locations-save "org-id")
  2674. (defvar org-id-track-globally)
  2675. (org-autoload "org-id"
  2676. '(org-id-get-create org-id-new org-id-copy org-id-get
  2677. org-id-get-with-outline-path-completion
  2678. org-id-get-with-outline-drilling
  2679. org-id-goto org-id-find org-id-store-link))
  2680. ;; Autoload Plotting Code
  2681. (org-autoload "org-plot"
  2682. '(org-plot/gnuplot))
  2683. ;;; Variables for pre-computed regular expressions, all buffer local
  2684. (defvar org-drawer-regexp nil
  2685. "Matches first line of a hidden block.")
  2686. (make-variable-buffer-local 'org-drawer-regexp)
  2687. (defvar org-todo-regexp nil
  2688. "Matches any of the TODO state keywords.")
  2689. (make-variable-buffer-local 'org-todo-regexp)
  2690. (defvar org-not-done-regexp nil
  2691. "Matches any of the TODO state keywords except the last one.")
  2692. (make-variable-buffer-local 'org-not-done-regexp)
  2693. (defvar org-todo-line-regexp nil
  2694. "Matches a headline and puts TODO state into group 2 if present.")
  2695. (make-variable-buffer-local 'org-todo-line-regexp)
  2696. (defvar org-complex-heading-regexp nil
  2697. "Matches a headline and puts everything into groups:
  2698. group 1: the stars
  2699. group 2: The todo keyword, maybe
  2700. group 3: Priority cookie
  2701. group 4: True headline
  2702. group 5: Tags")
  2703. (make-variable-buffer-local 'org-complex-heading-regexp)
  2704. (defvar org-todo-line-tags-regexp nil
  2705. "Matches a headline and puts TODO state into group 2 if present.
  2706. Also put tags into group 4 if tags are present.")
  2707. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2708. (defvar org-nl-done-regexp nil
  2709. "Matches newline followed by a headline with the DONE keyword.")
  2710. (make-variable-buffer-local 'org-nl-done-regexp)
  2711. (defvar org-looking-at-done-regexp nil
  2712. "Matches the DONE keyword a point.")
  2713. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2714. (defvar org-ds-keyword-length 12
  2715. "Maximum length of the Deadline and SCHEDULED keywords.")
  2716. (make-variable-buffer-local 'org-ds-keyword-length)
  2717. (defvar org-deadline-regexp nil
  2718. "Matches the DEADLINE keyword.")
  2719. (make-variable-buffer-local 'org-deadline-regexp)
  2720. (defvar org-deadline-time-regexp nil
  2721. "Matches the DEADLINE keyword together with a time stamp.")
  2722. (make-variable-buffer-local 'org-deadline-time-regexp)
  2723. (defvar org-deadline-line-regexp nil
  2724. "Matches the DEADLINE keyword and the rest of the line.")
  2725. (make-variable-buffer-local 'org-deadline-line-regexp)
  2726. (defvar org-scheduled-regexp nil
  2727. "Matches the SCHEDULED keyword.")
  2728. (make-variable-buffer-local 'org-scheduled-regexp)
  2729. (defvar org-scheduled-time-regexp nil
  2730. "Matches the SCHEDULED keyword together with a time stamp.")
  2731. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2732. (defvar org-closed-time-regexp nil
  2733. "Matches the CLOSED keyword together with a time stamp.")
  2734. (make-variable-buffer-local 'org-closed-time-regexp)
  2735. (defvar org-keyword-time-regexp nil
  2736. "Matches any of the 4 keywords, together with the time stamp.")
  2737. (make-variable-buffer-local 'org-keyword-time-regexp)
  2738. (defvar org-keyword-time-not-clock-regexp nil
  2739. "Matches any of the 3 keywords, together with the time stamp.")
  2740. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2741. (defvar org-maybe-keyword-time-regexp nil
  2742. "Matches a timestamp, possibly preceeded by a keyword.")
  2743. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2744. (defvar org-planning-or-clock-line-re nil
  2745. "Matches a line with planning or clock info.")
  2746. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2747. (defconst org-plain-time-of-day-regexp
  2748. (concat
  2749. "\\(\\<[012]?[0-9]"
  2750. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2751. "\\(--?"
  2752. "\\(\\<[012]?[0-9]"
  2753. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2754. "\\)?")
  2755. "Regular expression to match a plain time or time range.
  2756. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2757. groups carry important information:
  2758. 0 the full match
  2759. 1 the first time, range or not
  2760. 8 the second time, if it is a range.")
  2761. (defconst org-plain-time-extension-regexp
  2762. (concat
  2763. "\\(\\<[012]?[0-9]"
  2764. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2765. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2766. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2767. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2768. groups carry important information:
  2769. 0 the full match
  2770. 7 hours of duration
  2771. 9 minutes of duration")
  2772. (defconst org-stamp-time-of-day-regexp
  2773. (concat
  2774. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2775. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2776. "\\(--?"
  2777. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2778. "Regular expression to match a timestamp time or time range.
  2779. After a match, the following groups carry important information:
  2780. 0 the full match
  2781. 1 date plus weekday, for backreferencing to make sure both times on same day
  2782. 2 the first time, range or not
  2783. 4 the second time, if it is a range.")
  2784. (defconst org-startup-options
  2785. '(("fold" org-startup-folded t)
  2786. ("overview" org-startup-folded t)
  2787. ("nofold" org-startup-folded nil)
  2788. ("showall" org-startup-folded nil)
  2789. ("content" org-startup-folded content)
  2790. ("hidestars" org-hide-leading-stars t)
  2791. ("showstars" org-hide-leading-stars nil)
  2792. ("odd" org-odd-levels-only t)
  2793. ("oddeven" org-odd-levels-only nil)
  2794. ("align" org-startup-align-all-tables t)
  2795. ("noalign" org-startup-align-all-tables nil)
  2796. ("customtime" org-display-custom-times t)
  2797. ("logdone" org-log-done time)
  2798. ("lognotedone" org-log-done note)
  2799. ("nologdone" org-log-done nil)
  2800. ("lognoteclock-out" org-log-note-clock-out t)
  2801. ("nolognoteclock-out" org-log-note-clock-out nil)
  2802. ("logrepeat" org-log-repeat state)
  2803. ("lognoterepeat" org-log-repeat note)
  2804. ("nologrepeat" org-log-repeat nil)
  2805. ("fninline" org-footnote-define-inline t)
  2806. ("nofninline" org-footnote-define-inline nil)
  2807. ("fnlocal" org-footnote-section nil)
  2808. ("fnauto" org-footnote-auto-label t)
  2809. ("fnprompt" org-footnote-auto-label nil)
  2810. ("fnconfirm" org-footnote-auto-label confirm)
  2811. ("fnplain" org-footnote-auto-label plain)
  2812. ("constcgs" constants-unit-system cgs)
  2813. ("constSI" constants-unit-system SI))
  2814. "Variable associated with STARTUP options for org-mode.
  2815. Each element is a list of three items: The startup options as written
  2816. in the #+STARTUP line, the corresponding variable, and the value to
  2817. set this variable to if the option is found. An optional forth element PUSH
  2818. means to push this value onto the list in the variable.")
  2819. (defun org-set-regexps-and-options ()
  2820. "Precompute regular expressions for current buffer."
  2821. (when (org-mode-p)
  2822. (org-set-local 'org-todo-kwd-alist nil)
  2823. (org-set-local 'org-todo-key-alist nil)
  2824. (org-set-local 'org-todo-key-trigger nil)
  2825. (org-set-local 'org-todo-keywords-1 nil)
  2826. (org-set-local 'org-done-keywords nil)
  2827. (org-set-local 'org-todo-heads nil)
  2828. (org-set-local 'org-todo-sets nil)
  2829. (org-set-local 'org-todo-log-states nil)
  2830. (org-set-local 'org-file-properties nil)
  2831. (org-set-local 'org-file-tags nil)
  2832. (let ((re (org-make-options-regexp
  2833. '("CATEGORY" "TODO" "COLUMNS"
  2834. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2835. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2836. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2837. (splitre "[ \t]+")
  2838. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2839. tail sep kws1 prio props ftags drawers
  2840. ext-setup-or-nil setup-contents (start 0))
  2841. (save-excursion
  2842. (save-restriction
  2843. (widen)
  2844. (goto-char (point-min))
  2845. (while (or (and ext-setup-or-nil
  2846. (string-match re ext-setup-or-nil start)
  2847. (setq start (match-end 0)))
  2848. (and (setq ext-setup-or-nil nil start 0)
  2849. (re-search-forward re nil t)))
  2850. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2851. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2852. (cond
  2853. ((equal key "CATEGORY")
  2854. (if (string-match "[ \t]+$" value)
  2855. (setq value (replace-match "" t t value)))
  2856. (setq cat value))
  2857. ((member key '("SEQ_TODO" "TODO"))
  2858. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2859. ((equal key "TYP_TODO")
  2860. (push (cons 'type (org-split-string value splitre)) kwds))
  2861. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2862. ;; general TODO-like setup
  2863. (push (cons (intern (downcase (match-string 1 key)))
  2864. (org-split-string value splitre)) kwds))
  2865. ((equal key "TAGS")
  2866. (setq tags (append tags (org-split-string value splitre))))
  2867. ((equal key "COLUMNS")
  2868. (org-set-local 'org-columns-default-format value))
  2869. ((equal key "LINK")
  2870. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2871. (push (cons (match-string 1 value)
  2872. (org-trim (match-string 2 value)))
  2873. links)))
  2874. ((equal key "PRIORITIES")
  2875. (setq prio (org-split-string value " +")))
  2876. ((equal key "PROPERTY")
  2877. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2878. (push (cons (match-string 1 value) (match-string 2 value))
  2879. props)))
  2880. ((equal key "FILETAGS")
  2881. (when (string-match "\\S-" value)
  2882. (setq ftags
  2883. (append
  2884. ftags
  2885. (apply 'append
  2886. (mapcar (lambda (x) (org-split-string x ":"))
  2887. (org-split-string value)))))))
  2888. ((equal key "DRAWERS")
  2889. (setq drawers (org-split-string value splitre)))
  2890. ((equal key "CONSTANTS")
  2891. (setq const (append const (org-split-string value splitre))))
  2892. ((equal key "STARTUP")
  2893. (let ((opts (org-split-string value splitre))
  2894. l var val)
  2895. (while (setq l (pop opts))
  2896. (when (setq l (assoc l org-startup-options))
  2897. (setq var (nth 1 l) val (nth 2 l))
  2898. (if (not (nth 3 l))
  2899. (set (make-local-variable var) val)
  2900. (if (not (listp (symbol-value var)))
  2901. (set (make-local-variable var) nil))
  2902. (set (make-local-variable var) (symbol-value var))
  2903. (add-to-list var val))))))
  2904. ((equal key "ARCHIVE")
  2905. (string-match " *$" value)
  2906. (setq arch (replace-match "" t t value))
  2907. (remove-text-properties 0 (length arch)
  2908. '(face t fontified t) arch))
  2909. ((equal key "SETUPFILE")
  2910. (setq setup-contents (org-file-contents
  2911. (expand-file-name
  2912. (org-remove-double-quotes value))
  2913. 'noerror))
  2914. (if (not ext-setup-or-nil)
  2915. (setq ext-setup-or-nil setup-contents start 0)
  2916. (setq ext-setup-or-nil
  2917. (concat (substring ext-setup-or-nil 0 start)
  2918. "\n" setup-contents "\n"
  2919. (substring ext-setup-or-nil start)))))
  2920. ))))
  2921. (when cat
  2922. (org-set-local 'org-category (intern cat))
  2923. (push (cons "CATEGORY" cat) props))
  2924. (when prio
  2925. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2926. (setq prio (mapcar 'string-to-char prio))
  2927. (org-set-local 'org-highest-priority (nth 0 prio))
  2928. (org-set-local 'org-lowest-priority (nth 1 prio))
  2929. (org-set-local 'org-default-priority (nth 2 prio)))
  2930. (and props (org-set-local 'org-file-properties (nreverse props)))
  2931. (and ftags (org-set-local 'org-file-tags ftags))
  2932. (and drawers (org-set-local 'org-drawers drawers))
  2933. (and arch (org-set-local 'org-archive-location arch))
  2934. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2935. ;; Process the TODO keywords
  2936. (unless kwds
  2937. ;; Use the global values as if they had been given locally.
  2938. (setq kwds (default-value 'org-todo-keywords))
  2939. (if (stringp (car kwds))
  2940. (setq kwds (list (cons org-todo-interpretation
  2941. (default-value 'org-todo-keywords)))))
  2942. (setq kwds (reverse kwds)))
  2943. (setq kwds (nreverse kwds))
  2944. (let (inter kws kw)
  2945. (while (setq kws (pop kwds))
  2946. (let ((kws (or
  2947. (run-hook-with-args-until-success
  2948. 'org-todo-setup-filter-hook kws)
  2949. kws)))
  2950. (setq inter (pop kws) sep (member "|" kws)
  2951. kws0 (delete "|" (copy-sequence kws))
  2952. kwsa nil
  2953. kws1 (mapcar
  2954. (lambda (x)
  2955. ;; 1 2
  2956. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2957. (progn
  2958. (setq kw (match-string 1 x)
  2959. key (and (match-end 2) (match-string 2 x))
  2960. log (org-extract-log-state-settings x))
  2961. (push (cons kw (and key (string-to-char key))) kwsa)
  2962. (and log (push log org-todo-log-states))
  2963. kw)
  2964. (error "Invalid TODO keyword %s" x)))
  2965. kws0)
  2966. kwsa (if kwsa (append '((:startgroup))
  2967. (nreverse kwsa)
  2968. '((:endgroup))))
  2969. hw (car kws1)
  2970. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2971. tail (list inter hw (car dws) (org-last dws))))
  2972. (add-to-list 'org-todo-heads hw 'append)
  2973. (push kws1 org-todo-sets)
  2974. (setq org-done-keywords (append org-done-keywords dws nil))
  2975. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2976. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2977. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2978. (setq org-todo-sets (nreverse org-todo-sets)
  2979. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2980. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2981. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2982. ;; Process the constants
  2983. (when const
  2984. (let (e cst)
  2985. (while (setq e (pop const))
  2986. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2987. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2988. (setq org-table-formula-constants-local cst)))
  2989. ;; Process the tags.
  2990. (when tags
  2991. (let (e tgs)
  2992. (while (setq e (pop tags))
  2993. (cond
  2994. ((equal e "{") (push '(:startgroup) tgs))
  2995. ((equal e "}") (push '(:endgroup) tgs))
  2996. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2997. (push (cons (match-string 1 e)
  2998. (string-to-char (match-string 2 e)))
  2999. tgs))
  3000. (t (push (list e) tgs))))
  3001. (org-set-local 'org-tag-alist nil)
  3002. (while (setq e (pop tgs))
  3003. (or (and (stringp (car e))
  3004. (assoc (car e) org-tag-alist))
  3005. (push e org-tag-alist)))))
  3006. ;; Compute the regular expressions and other local variables
  3007. (if (not org-done-keywords)
  3008. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3009. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3010. (length org-scheduled-string)
  3011. (length org-clock-string)
  3012. (length org-closed-string)))
  3013. org-drawer-regexp
  3014. (concat "^[ \t]*:\\("
  3015. (mapconcat 'regexp-quote org-drawers "\\|")
  3016. "\\):[ \t]*$")
  3017. org-not-done-keywords
  3018. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3019. org-todo-regexp
  3020. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3021. "\\|") "\\)\\>")
  3022. org-not-done-regexp
  3023. (concat "\\<\\("
  3024. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3025. "\\)\\>")
  3026. org-todo-line-regexp
  3027. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3028. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3029. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3030. org-complex-heading-regexp
  3031. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3032. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3033. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3034. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3035. org-nl-done-regexp
  3036. (concat "\n\\*+[ \t]+"
  3037. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3038. "\\)" "\\>")
  3039. org-todo-line-tags-regexp
  3040. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3041. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3042. (org-re
  3043. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3044. org-looking-at-done-regexp
  3045. (concat "^" "\\(?:"
  3046. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3047. "\\>")
  3048. org-deadline-regexp (concat "\\<" org-deadline-string)
  3049. org-deadline-time-regexp
  3050. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3051. org-deadline-line-regexp
  3052. (concat "\\<\\(" org-deadline-string "\\).*")
  3053. org-scheduled-regexp
  3054. (concat "\\<" org-scheduled-string)
  3055. org-scheduled-time-regexp
  3056. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3057. org-closed-time-regexp
  3058. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3059. org-keyword-time-regexp
  3060. (concat "\\<\\(" org-scheduled-string
  3061. "\\|" org-deadline-string
  3062. "\\|" org-closed-string
  3063. "\\|" org-clock-string "\\)"
  3064. " *[[<]\\([^]>]+\\)[]>]")
  3065. org-keyword-time-not-clock-regexp
  3066. (concat "\\<\\(" org-scheduled-string
  3067. "\\|" org-deadline-string
  3068. "\\|" org-closed-string
  3069. "\\)"
  3070. " *[[<]\\([^]>]+\\)[]>]")
  3071. org-maybe-keyword-time-regexp
  3072. (concat "\\(\\<\\(" org-scheduled-string
  3073. "\\|" org-deadline-string
  3074. "\\|" org-closed-string
  3075. "\\|" org-clock-string "\\)\\)?"
  3076. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3077. org-planning-or-clock-line-re
  3078. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3079. "\\|" org-deadline-string
  3080. "\\|" org-closed-string "\\|" org-clock-string
  3081. "\\)\\>\\)")
  3082. )
  3083. (org-compute-latex-and-specials-regexp)
  3084. (org-set-font-lock-defaults))))
  3085. (defun org-file-contents (file &optional noerror)
  3086. "Return the contents of FILE, as a string."
  3087. (if (or (not file)
  3088. (not (file-readable-p file)))
  3089. (if noerror
  3090. (progn
  3091. (message "Cannot read file %s" file)
  3092. (ding) (sit-for 2)
  3093. "")
  3094. (error "Cannot read file %s" file))
  3095. (with-temp-buffer
  3096. (insert-file-contents file)
  3097. (buffer-string))))
  3098. (defun org-extract-log-state-settings (x)
  3099. "Extract the log state setting from a TODO keyword string.
  3100. This will extract info from a string like \"WAIT(w@/!)\"."
  3101. (let (kw key log1 log2)
  3102. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3103. (setq kw (match-string 1 x)
  3104. key (and (match-end 2) (match-string 2 x))
  3105. log1 (and (match-end 3) (match-string 3 x))
  3106. log2 (and (match-end 4) (match-string 4 x)))
  3107. (and (or log1 log2)
  3108. (list kw
  3109. (and log1 (if (equal log1 "!") 'time 'note))
  3110. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3111. (defun org-remove-keyword-keys (list)
  3112. "Remove a pair of parenthesis at the end of each string in LIST."
  3113. (mapcar (lambda (x)
  3114. (if (string-match "(.*)$" x)
  3115. (substring x 0 (match-beginning 0))
  3116. x))
  3117. list))
  3118. ;; FIXME: this could be done much better, using second characters etc.
  3119. (defun org-assign-fast-keys (alist)
  3120. "Assign fast keys to a keyword-key alist.
  3121. Respect keys that are already there."
  3122. (let (new e k c c1 c2 (char ?a))
  3123. (while (setq e (pop alist))
  3124. (cond
  3125. ((equal e '(:startgroup)) (push e new))
  3126. ((equal e '(:endgroup)) (push e new))
  3127. (t
  3128. (setq k (car e) c2 nil)
  3129. (if (cdr e)
  3130. (setq c (cdr e))
  3131. ;; automatically assign a character.
  3132. (setq c1 (string-to-char
  3133. (downcase (substring
  3134. k (if (= (string-to-char k) ?@) 1 0)))))
  3135. (if (or (rassoc c1 new) (rassoc c1 alist))
  3136. (while (or (rassoc char new) (rassoc char alist))
  3137. (setq char (1+ char)))
  3138. (setq c2 c1))
  3139. (setq c (or c2 char)))
  3140. (push (cons k c) new))))
  3141. (nreverse new)))
  3142. ;;; Some variables used in various places
  3143. (defvar org-window-configuration nil
  3144. "Used in various places to store a window configuration.")
  3145. (defvar org-finish-function nil
  3146. "Function to be called when `C-c C-c' is used.
  3147. This is for getting out of special buffers like remember.")
  3148. ;; FIXME: Occasionally check by commenting these, to make sure
  3149. ;; no other functions uses these, forgetting to let-bind them.
  3150. (defvar entry)
  3151. (defvar state)
  3152. (defvar last-state)
  3153. (defvar date)
  3154. (defvar description)
  3155. ;; Defined somewhere in this file, but used before definition.
  3156. (defvar org-html-entities)
  3157. (defvar org-struct-menu)
  3158. (defvar org-org-menu)
  3159. (defvar org-tbl-menu)
  3160. (defvar org-agenda-keymap)
  3161. ;;;; Define the Org-mode
  3162. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3163. (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."))
  3164. ;; We use a before-change function to check if a table might need
  3165. ;; an update.
  3166. (defvar org-table-may-need-update t
  3167. "Indicates that a table might need an update.
  3168. This variable is set by `org-before-change-function'.
  3169. `org-table-align' sets it back to nil.")
  3170. (defun org-before-change-function (beg end)
  3171. "Every change indicates that a table might need an update."
  3172. (setq org-table-may-need-update t))
  3173. (defvar org-mode-map)
  3174. (defvar org-mode-hook nil
  3175. "Mode hook for Org-mode, run after the mode was turned on.")
  3176. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3177. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3178. (defvar org-table-buffer-is-an nil)
  3179. (defconst org-outline-regexp "\\*+ ")
  3180. ;;;###autoload
  3181. (define-derived-mode org-mode outline-mode "Org"
  3182. "Outline-based notes management and organizer, alias
  3183. \"Carsten's outline-mode for keeping track of everything.\"
  3184. Org-mode develops organizational tasks around a NOTES file which
  3185. contains information about projects as plain text. Org-mode is
  3186. implemented on top of outline-mode, which is ideal to keep the content
  3187. of large files well structured. It supports ToDo items, deadlines and
  3188. time stamps, which magically appear in the diary listing of the Emacs
  3189. calendar. Tables are easily created with a built-in table editor.
  3190. Plain text URL-like links connect to websites, emails (VM), Usenet
  3191. messages (Gnus), BBDB entries, and any files related to the project.
  3192. For printing and sharing of notes, an Org-mode file (or a part of it)
  3193. can be exported as a structured ASCII or HTML file.
  3194. The following commands are available:
  3195. \\{org-mode-map}"
  3196. ;; Get rid of Outline menus, they are not needed
  3197. ;; Need to do this here because define-derived-mode sets up
  3198. ;; the keymap so late. Still, it is a waste to call this each time
  3199. ;; we switch another buffer into org-mode.
  3200. (if (featurep 'xemacs)
  3201. (when (boundp 'outline-mode-menu-heading)
  3202. ;; Assume this is Greg's port, it used easymenu
  3203. (easy-menu-remove outline-mode-menu-heading)
  3204. (easy-menu-remove outline-mode-menu-show)
  3205. (easy-menu-remove outline-mode-menu-hide))
  3206. (define-key org-mode-map [menu-bar headings] 'undefined)
  3207. (define-key org-mode-map [menu-bar hide] 'undefined)
  3208. (define-key org-mode-map [menu-bar show] 'undefined))
  3209. (org-load-modules-maybe)
  3210. (easy-menu-add org-org-menu)
  3211. (easy-menu-add org-tbl-menu)
  3212. (org-install-agenda-files-menu)
  3213. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3214. (org-add-to-invisibility-spec '(org-cwidth))
  3215. (when (featurep 'xemacs)
  3216. (org-set-local 'line-move-ignore-invisible t))
  3217. (org-set-local 'outline-regexp org-outline-regexp)
  3218. (org-set-local 'outline-level 'org-outline-level)
  3219. (when (and org-ellipsis
  3220. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3221. (fboundp 'make-glyph-code))
  3222. (unless org-display-table
  3223. (setq org-display-table (make-display-table)))
  3224. (set-display-table-slot
  3225. org-display-table 4
  3226. (vconcat (mapcar
  3227. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3228. org-ellipsis)))
  3229. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3230. (setq buffer-display-table org-display-table))
  3231. (org-set-regexps-and-options)
  3232. (when (and org-tag-faces (not org-tags-special-faces-re))
  3233. ;; tag faces set outside customize.... force initialization.
  3234. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3235. ;; Calc embedded
  3236. (org-set-local 'calc-embedded-open-mode "# ")
  3237. (modify-syntax-entry ?# "<")
  3238. (modify-syntax-entry ?@ "w")
  3239. (if org-startup-truncated (setq truncate-lines t))
  3240. (org-set-local 'font-lock-unfontify-region-function
  3241. 'org-unfontify-region)
  3242. ;; Activate before-change-function
  3243. (org-set-local 'org-table-may-need-update t)
  3244. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3245. 'local)
  3246. ;; Check for running clock before killing a buffer
  3247. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3248. ;; Paragraphs and auto-filling
  3249. (org-set-autofill-regexps)
  3250. (setq indent-line-function 'org-indent-line-function)
  3251. (org-update-radio-target-regexp)
  3252. ;; Make sure dependence stuff works reliably, even for users who set it
  3253. ;; too late :-(
  3254. (if org-enforce-todo-dependencies
  3255. (add-hook 'org-blocker-hook
  3256. 'org-block-todo-from-children-or-siblings)
  3257. (remove-hook 'org-blocker-hook
  3258. 'org-block-todo-from-children-or-siblings))
  3259. (if org-enforce-todo-checkbox-dependencies
  3260. (add-hook 'org-blocker-hook
  3261. 'org-block-todo-from-checkboxes)
  3262. (remove-hook 'org-blocker-hook
  3263. 'org-block-todo-from-checkboxes))
  3264. ;; Comment characters
  3265. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3266. (org-set-local 'comment-padding " ")
  3267. ;; Align options lines
  3268. (org-set-local
  3269. 'align-mode-rules-list
  3270. '((org-in-buffer-settings
  3271. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3272. (modes . '(org-mode)))))
  3273. ;; Imenu
  3274. (org-set-local 'imenu-create-index-function
  3275. 'org-imenu-get-tree)
  3276. ;; Make isearch reveal context
  3277. (if (or (featurep 'xemacs)
  3278. (not (boundp 'outline-isearch-open-invisible-function)))
  3279. ;; Emacs 21 and XEmacs make use of the hook
  3280. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3281. ;; Emacs 22 deals with this through a special variable
  3282. (org-set-local 'outline-isearch-open-invisible-function
  3283. (lambda (&rest ignore) (org-show-context 'isearch))))
  3284. ;; If empty file that did not turn on org-mode automatically, make it to.
  3285. (if (and org-insert-mode-line-in-empty-file
  3286. (interactive-p)
  3287. (= (point-min) (point-max)))
  3288. (insert "# -*- mode: org -*-\n\n"))
  3289. (unless org-inhibit-startup
  3290. (when org-startup-align-all-tables
  3291. (let ((bmp (buffer-modified-p)))
  3292. (org-table-map-tables 'org-table-align)
  3293. (set-buffer-modified-p bmp)))
  3294. (org-set-startup-visibility)))
  3295. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3296. (defun org-current-time ()
  3297. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3298. (if (> (car org-time-stamp-rounding-minutes) 1)
  3299. (let ((r (car org-time-stamp-rounding-minutes))
  3300. (time (decode-time)))
  3301. (apply 'encode-time
  3302. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3303. (nthcdr 2 time))))
  3304. (current-time)))
  3305. ;;;; Font-Lock stuff, including the activators
  3306. (defvar org-mouse-map (make-sparse-keymap))
  3307. (org-defkey org-mouse-map
  3308. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3309. (org-defkey org-mouse-map
  3310. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3311. (when org-mouse-1-follows-link
  3312. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3313. (when org-tab-follows-link
  3314. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3315. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3316. (require 'font-lock)
  3317. (defconst org-non-link-chars "]\t\n\r<>")
  3318. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3319. "shell" "elisp"))
  3320. (defvar org-link-types-re nil
  3321. "Matches a link that has a url-like prefix like \"http:\"")
  3322. (defvar org-link-re-with-space nil
  3323. "Matches a link with spaces, optional angular brackets around it.")
  3324. (defvar org-link-re-with-space2 nil
  3325. "Matches a link with spaces, optional angular brackets around it.")
  3326. (defvar org-link-re-with-space3 nil
  3327. "Matches a link with spaces, only for internal part in bracket links.")
  3328. (defvar org-angle-link-re nil
  3329. "Matches link with angular brackets, spaces are allowed.")
  3330. (defvar org-plain-link-re nil
  3331. "Matches plain link, without spaces.")
  3332. (defvar org-bracket-link-regexp nil
  3333. "Matches a link in double brackets.")
  3334. (defvar org-bracket-link-analytic-regexp nil
  3335. "Regular expression used to analyze links.
  3336. Here is what the match groups contain after a match:
  3337. 1: http:
  3338. 2: http
  3339. 3: path
  3340. 4: [desc]
  3341. 5: desc")
  3342. (defvar org-bracket-link-analytic-regexp++ nil
  3343. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3344. (defvar org-any-link-re nil
  3345. "Regular expression matching any link.")
  3346. (defun org-make-link-regexps ()
  3347. "Update the link regular expressions.
  3348. This should be called after the variable `org-link-types' has changed."
  3349. (setq org-link-types-re
  3350. (concat
  3351. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3352. org-link-re-with-space
  3353. (concat
  3354. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3355. "\\([^" org-non-link-chars " ]"
  3356. "[^" org-non-link-chars "]*"
  3357. "[^" org-non-link-chars " ]\\)>?")
  3358. org-link-re-with-space2
  3359. (concat
  3360. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3361. "\\([^" org-non-link-chars " ]"
  3362. "[^\t\n\r]*"
  3363. "[^" org-non-link-chars " ]\\)>?")
  3364. org-link-re-with-space3
  3365. (concat
  3366. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3367. "\\([^" org-non-link-chars " ]"
  3368. "[^\t\n\r]*\\)")
  3369. org-angle-link-re
  3370. (concat
  3371. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3372. "\\([^" org-non-link-chars " ]"
  3373. "[^" org-non-link-chars "]*"
  3374. "\\)>")
  3375. org-plain-link-re
  3376. (concat
  3377. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3378. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3379. org-bracket-link-regexp
  3380. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3381. org-bracket-link-analytic-regexp
  3382. (concat
  3383. "\\[\\["
  3384. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3385. "\\([^]]+\\)"
  3386. "\\]"
  3387. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3388. "\\]")
  3389. org-bracket-link-analytic-regexp++
  3390. (concat
  3391. "\\[\\["
  3392. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3393. "\\([^]]+\\)"
  3394. "\\]"
  3395. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3396. "\\]")
  3397. org-any-link-re
  3398. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3399. org-angle-link-re "\\)\\|\\("
  3400. org-plain-link-re "\\)")))
  3401. (org-make-link-regexps)
  3402. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3403. "Regular expression for fast time stamp matching.")
  3404. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3405. "Regular expression for fast time stamp matching.")
  3406. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3407. "Regular expression matching time strings for analysis.
  3408. This one does not require the space after the date, so it can be used
  3409. on a string that terminates immediately after the date.")
  3410. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3411. "Regular expression matching time strings for analysis.")
  3412. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3413. "Regular expression matching time stamps, with groups.")
  3414. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3415. "Regular expression matching time stamps (also [..]), with groups.")
  3416. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3417. "Regular expression matching a time stamp range.")
  3418. (defconst org-tr-regexp-both
  3419. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3420. "Regular expression matching a time stamp range.")
  3421. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3422. org-ts-regexp "\\)?")
  3423. "Regular expression matching a time stamp or time stamp range.")
  3424. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3425. org-ts-regexp-both "\\)?")
  3426. "Regular expression matching a time stamp or time stamp range.
  3427. The time stamps may be either active or inactive.")
  3428. (defvar org-emph-face nil)
  3429. (defun org-do-emphasis-faces (limit)
  3430. "Run through the buffer and add overlays to links."
  3431. (let (rtn)
  3432. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3433. (if (not (= (char-after (match-beginning 3))
  3434. (char-after (match-beginning 4))))
  3435. (progn
  3436. (setq rtn t)
  3437. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3438. 'face
  3439. (nth 1 (assoc (match-string 3)
  3440. org-emphasis-alist)))
  3441. (add-text-properties (match-beginning 2) (match-end 2)
  3442. '(font-lock-multiline t))
  3443. (when org-hide-emphasis-markers
  3444. (add-text-properties (match-end 4) (match-beginning 5)
  3445. '(invisible org-link))
  3446. (add-text-properties (match-beginning 3) (match-end 3)
  3447. '(invisible org-link)))))
  3448. (backward-char 1))
  3449. rtn))
  3450. (defun org-emphasize (&optional char)
  3451. "Insert or change an emphasis, i.e. a font like bold or italic.
  3452. If there is an active region, change that region to a new emphasis.
  3453. If there is no region, just insert the marker characters and position
  3454. the cursor between them.
  3455. CHAR should be either the marker character, or the first character of the
  3456. HTML tag associated with that emphasis. If CHAR is a space, the means
  3457. to remove the emphasis of the selected region.
  3458. If char is not given (for example in an interactive call) it
  3459. will be prompted for."
  3460. (interactive)
  3461. (let ((eal org-emphasis-alist) e det
  3462. (erc org-emphasis-regexp-components)
  3463. (prompt "")
  3464. (string "") beg end move tag c s)
  3465. (if (org-region-active-p)
  3466. (setq beg (region-beginning) end (region-end)
  3467. string (buffer-substring beg end))
  3468. (setq move t))
  3469. (while (setq e (pop eal))
  3470. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3471. c (aref tag 0))
  3472. (push (cons c (string-to-char (car e))) det)
  3473. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3474. (substring tag 1)))))
  3475. (setq det (nreverse det))
  3476. (unless char
  3477. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3478. (setq char (read-char-exclusive)))
  3479. (setq char (or (cdr (assoc char det)) char))
  3480. (if (equal char ?\ )
  3481. (setq s "" move nil)
  3482. (unless (assoc (char-to-string char) org-emphasis-alist)
  3483. (error "No such emphasis marker: \"%c\"" char))
  3484. (setq s (char-to-string char)))
  3485. (while (and (> (length string) 1)
  3486. (equal (substring string 0 1) (substring string -1))
  3487. (assoc (substring string 0 1) org-emphasis-alist))
  3488. (setq string (substring string 1 -1)))
  3489. (setq string (concat s string s))
  3490. (if beg (delete-region beg end))
  3491. (unless (or (bolp)
  3492. (string-match (concat "[" (nth 0 erc) "\n]")
  3493. (char-to-string (char-before (point)))))
  3494. (insert " "))
  3495. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3496. (char-to-string (char-after (point))))
  3497. (insert " ") (backward-char 1))
  3498. (insert string)
  3499. (and move (backward-char 1))))
  3500. (defconst org-nonsticky-props
  3501. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3502. (defun org-activate-plain-links (limit)
  3503. "Run through the buffer and add overlays to links."
  3504. (catch 'exit
  3505. (let (f)
  3506. (while (re-search-forward org-plain-link-re limit t)
  3507. (setq f (get-text-property (match-beginning 0) 'face))
  3508. (if (or (eq f 'org-tag)
  3509. (and (listp f) (memq 'org-tag f)))
  3510. nil
  3511. (add-text-properties (match-beginning 0) (match-end 0)
  3512. (list 'mouse-face 'highlight
  3513. 'rear-nonsticky org-nonsticky-props
  3514. 'keymap org-mouse-map
  3515. ))
  3516. (throw 'exit t))))))
  3517. (defun org-activate-code (limit)
  3518. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3519. (progn
  3520. (remove-text-properties (match-beginning 0) (match-end 0)
  3521. '(display t invisible t intangible t))
  3522. t)))
  3523. (defun org-activate-angle-links (limit)
  3524. "Run through the buffer and add overlays to links."
  3525. (if (re-search-forward org-angle-link-re limit t)
  3526. (progn
  3527. (add-text-properties (match-beginning 0) (match-end 0)
  3528. (list 'mouse-face 'highlight
  3529. 'rear-nonsticky org-nonsticky-props
  3530. 'keymap org-mouse-map
  3531. ))
  3532. t)))
  3533. (defun org-activate-footnote-links (limit)
  3534. "Run through the buffer and add overlays to links."
  3535. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3536. limit t)
  3537. (progn
  3538. (add-text-properties (match-beginning 2) (match-end 2)
  3539. (list 'mouse-face 'highlight
  3540. 'rear-nonsticky org-nonsticky-props
  3541. 'keymap org-mouse-map
  3542. 'help-echo
  3543. (if (= (point-at-bol) (match-beginning 2))
  3544. "Footnote definition"
  3545. "Footnote reference")
  3546. ))
  3547. t)))
  3548. (defun org-activate-bracket-links (limit)
  3549. "Run through the buffer and add overlays to bracketed links."
  3550. (if (re-search-forward org-bracket-link-regexp limit t)
  3551. (let* ((help (concat "LINK: "
  3552. (org-match-string-no-properties 1)))
  3553. ;; FIXME: above we should remove the escapes.
  3554. ;; but that requires another match, protecting match data,
  3555. ;; a lot of overhead for font-lock.
  3556. (ip (org-maybe-intangible
  3557. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3558. 'keymap org-mouse-map 'mouse-face 'highlight
  3559. 'font-lock-multiline t 'help-echo help)))
  3560. (vp (list 'rear-nonsticky org-nonsticky-props
  3561. 'keymap org-mouse-map 'mouse-face 'highlight
  3562. ' font-lock-multiline t 'help-echo help)))
  3563. ;; We need to remove the invisible property here. Table narrowing
  3564. ;; may have made some of this invisible.
  3565. (remove-text-properties (match-beginning 0) (match-end 0)
  3566. '(invisible nil))
  3567. (if (match-end 3)
  3568. (progn
  3569. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3570. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3571. (add-text-properties (match-end 3) (match-end 0) ip))
  3572. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3573. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3574. (add-text-properties (match-end 1) (match-end 0) ip))
  3575. t)))
  3576. (defun org-activate-dates (limit)
  3577. "Run through the buffer and add overlays to dates."
  3578. (if (re-search-forward org-tsr-regexp-both limit t)
  3579. (progn
  3580. (add-text-properties (match-beginning 0) (match-end 0)
  3581. (list 'mouse-face 'highlight
  3582. 'rear-nonsticky org-nonsticky-props
  3583. 'keymap org-mouse-map))
  3584. (when org-display-custom-times
  3585. (if (match-end 3)
  3586. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3587. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3588. t)))
  3589. (defvar org-target-link-regexp nil
  3590. "Regular expression matching radio targets in plain text.")
  3591. (make-variable-buffer-local 'org-target-link-regexp)
  3592. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3593. "Regular expression matching a link target.")
  3594. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3595. "Regular expression matching a radio target.")
  3596. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3597. "Regular expression matching any target.")
  3598. (defun org-activate-target-links (limit)
  3599. "Run through the buffer and add overlays to target matches."
  3600. (when org-target-link-regexp
  3601. (let ((case-fold-search t))
  3602. (if (re-search-forward org-target-link-regexp limit t)
  3603. (progn
  3604. (add-text-properties (match-beginning 0) (match-end 0)
  3605. (list 'mouse-face 'highlight
  3606. 'rear-nonsticky org-nonsticky-props
  3607. 'keymap org-mouse-map
  3608. 'help-echo "Radio target link"
  3609. 'org-linked-text t))
  3610. t)))))
  3611. (defun org-update-radio-target-regexp ()
  3612. "Find all radio targets in this file and update the regular expression."
  3613. (interactive)
  3614. (when (memq 'radio org-activate-links)
  3615. (setq org-target-link-regexp
  3616. (org-make-target-link-regexp (org-all-targets 'radio)))
  3617. (org-restart-font-lock)))
  3618. (defun org-hide-wide-columns (limit)
  3619. (let (s e)
  3620. (setq s (text-property-any (point) (or limit (point-max))
  3621. 'org-cwidth t))
  3622. (when s
  3623. (setq e (next-single-property-change s 'org-cwidth))
  3624. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3625. (goto-char e)
  3626. t)))
  3627. (defvar org-latex-and-specials-regexp nil
  3628. "Regular expression for highlighting export special stuff.")
  3629. (defvar org-match-substring-regexp)
  3630. (defvar org-match-substring-with-braces-regexp)
  3631. (defvar org-export-html-special-string-regexps)
  3632. (defun org-compute-latex-and-specials-regexp ()
  3633. "Compute regular expression for stuff treated specially by exporters."
  3634. (if (not org-highlight-latex-fragments-and-specials)
  3635. (org-set-local 'org-latex-and-specials-regexp nil)
  3636. (require 'org-exp)
  3637. (let*
  3638. ((matchers (plist-get org-format-latex-options :matchers))
  3639. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3640. org-latex-regexps)))
  3641. (options (org-combine-plists (org-default-export-plist)
  3642. (org-infile-export-plist)))
  3643. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3644. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3645. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3646. (org-export-html-expand (plist-get options :expand-quoted-html))
  3647. (org-export-with-special-strings (plist-get options :special-strings))
  3648. (re-sub
  3649. (cond
  3650. ((equal org-export-with-sub-superscripts '{})
  3651. (list org-match-substring-with-braces-regexp))
  3652. (org-export-with-sub-superscripts
  3653. (list org-match-substring-regexp))
  3654. (t nil)))
  3655. (re-latex
  3656. (if org-export-with-LaTeX-fragments
  3657. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3658. (re-macros
  3659. (if org-export-with-TeX-macros
  3660. (list (concat "\\\\"
  3661. (regexp-opt
  3662. (append (mapcar 'car org-html-entities)
  3663. (if (boundp 'org-latex-entities)
  3664. org-latex-entities nil))
  3665. 'words))) ; FIXME
  3666. ))
  3667. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3668. (re-special (if org-export-with-special-strings
  3669. (mapcar (lambda (x) (car x))
  3670. org-export-html-special-string-regexps)))
  3671. (re-rest
  3672. (delq nil
  3673. (list
  3674. (if org-export-html-expand "@<[^>\n]+>")
  3675. ))))
  3676. (org-set-local
  3677. 'org-latex-and-specials-regexp
  3678. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3679. re-rest) "\\|")))))
  3680. (defun org-do-latex-and-special-faces (limit)
  3681. "Run through the buffer and add overlays to links."
  3682. (when org-latex-and-specials-regexp
  3683. (let (rtn d)
  3684. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3685. limit t))
  3686. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3687. 'face))
  3688. '(org-code org-verbatim underline)))
  3689. (progn
  3690. (setq rtn t
  3691. d (cond ((member (char-after (1+ (match-beginning 0)))
  3692. '(?_ ?^)) 1)
  3693. (t 0)))
  3694. (font-lock-prepend-text-property
  3695. (+ d (match-beginning 0)) (match-end 0)
  3696. 'face 'org-latex-and-export-specials)
  3697. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3698. '(font-lock-multiline t)))))
  3699. rtn)))
  3700. (defun org-restart-font-lock ()
  3701. "Restart font-lock-mode, to force refontification."
  3702. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3703. (font-lock-mode -1)
  3704. (font-lock-mode 1)))
  3705. (defun org-all-targets (&optional radio)
  3706. "Return a list of all targets in this file.
  3707. With optional argument RADIO, only find radio targets."
  3708. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3709. rtn)
  3710. (save-excursion
  3711. (goto-char (point-min))
  3712. (while (re-search-forward re nil t)
  3713. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3714. rtn)))
  3715. (defun org-make-target-link-regexp (targets)
  3716. "Make regular expression matching all strings in TARGETS.
  3717. The regular expression finds the targets also if there is a line break
  3718. between words."
  3719. (and targets
  3720. (concat
  3721. "\\<\\("
  3722. (mapconcat
  3723. (lambda (x)
  3724. (while (string-match " +" x)
  3725. (setq x (replace-match "\\s-+" t t x)))
  3726. x)
  3727. targets
  3728. "\\|")
  3729. "\\)\\>")))
  3730. (defun org-activate-tags (limit)
  3731. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3732. (progn
  3733. (add-text-properties (match-beginning 1) (match-end 1)
  3734. (list 'mouse-face 'highlight
  3735. 'rear-nonsticky org-nonsticky-props
  3736. 'keymap org-mouse-map))
  3737. t)))
  3738. (defun org-outline-level ()
  3739. (save-excursion
  3740. (looking-at outline-regexp)
  3741. (if (match-beginning 1)
  3742. (+ (org-get-string-indentation (match-string 1)) 1000)
  3743. (1- (- (match-end 0) (match-beginning 0))))))
  3744. (defvar org-font-lock-keywords nil)
  3745. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3746. "Regular expression matching a property line.")
  3747. (defvar org-font-lock-hook nil
  3748. "Functions to be called for special font lock stuff.")
  3749. (defun org-font-lock-hook (limit)
  3750. (run-hook-with-args 'org-font-lock-hook limit))
  3751. (defun org-set-font-lock-defaults ()
  3752. (let* ((em org-fontify-emphasized-text)
  3753. (lk org-activate-links)
  3754. (org-font-lock-extra-keywords
  3755. (list
  3756. ;; Call the hook
  3757. '(org-font-lock-hook)
  3758. ;; Headlines
  3759. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3760. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3761. ;; Table lines
  3762. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3763. (1 'org-table t))
  3764. ;; Table internals
  3765. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3766. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3767. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3768. ;; Drawers
  3769. (list org-drawer-regexp '(0 'org-special-keyword t))
  3770. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3771. ;; Properties
  3772. (list org-property-re
  3773. '(1 'org-special-keyword t)
  3774. '(3 'org-property-value t))
  3775. (if org-format-transports-properties-p
  3776. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3777. ;; Links
  3778. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3779. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3780. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3781. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3782. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3783. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3784. (if (memq 'footnote lk) '(org-activate-footnote-links
  3785. (2 'org-footnote t)))
  3786. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3787. '(org-hide-wide-columns (0 nil append))
  3788. ;; TODO lines
  3789. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3790. '(1 (org-get-todo-face 1) t))
  3791. ;; DONE
  3792. (if org-fontify-done-headline
  3793. (list (concat "^[*]+ +\\<\\("
  3794. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3795. "\\)\\(.*\\)")
  3796. '(2 'org-headline-done t))
  3797. nil)
  3798. ;; Priorities
  3799. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3800. ;; Tags
  3801. '(org-font-lock-add-tag-faces)
  3802. ;; Special keywords
  3803. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3804. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3805. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3806. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3807. ;; Emphasis
  3808. (if em
  3809. (if (featurep 'xemacs)
  3810. '(org-do-emphasis-faces (0 nil append))
  3811. '(org-do-emphasis-faces)))
  3812. ;; Checkboxes
  3813. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3814. 2 'bold prepend)
  3815. (if org-provide-checkbox-statistics
  3816. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3817. (0 (org-get-checkbox-statistics-face) t)))
  3818. ;; Description list items
  3819. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3820. 2 'bold prepend)
  3821. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3822. '(1 'org-archived prepend))
  3823. ;; Specials
  3824. '(org-do-latex-and-special-faces)
  3825. ;; Code
  3826. '(org-activate-code (1 'org-code t))
  3827. ;; COMMENT
  3828. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3829. "\\|" org-quote-string "\\)\\>")
  3830. '(1 'org-special-keyword t))
  3831. '("^#.*" (0 'font-lock-comment-face t))
  3832. )))
  3833. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3834. ;; Now set the full font-lock-keywords
  3835. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3836. (org-set-local 'font-lock-defaults
  3837. '(org-font-lock-keywords t nil nil backward-paragraph))
  3838. (kill-local-variable 'font-lock-keywords) nil))
  3839. (defvar org-m nil)
  3840. (defvar org-l nil)
  3841. (defvar org-f nil)
  3842. (defun org-get-level-face (n)
  3843. "Get the right face for match N in font-lock matching of headlines."
  3844. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3845. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3846. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3847. (cond
  3848. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3849. ((eq n 2) org-f)
  3850. (t (if org-level-color-stars-only nil org-f))))
  3851. (defun org-get-todo-face (kwd)
  3852. "Get the right face for a TODO keyword KWD.
  3853. If KWD is a number, get the corresponding match group."
  3854. (if (numberp kwd) (setq kwd (match-string kwd)))
  3855. (or (cdr (assoc kwd org-todo-keyword-faces))
  3856. (and (member kwd org-done-keywords) 'org-done)
  3857. 'org-todo))
  3858. (defun org-font-lock-add-tag-faces (limit)
  3859. "Add the special tag faces."
  3860. (when (and org-tag-faces org-tags-special-faces-re)
  3861. (while (re-search-forward org-tags-special-faces-re limit t)
  3862. (add-text-properties (match-beginning 1) (match-end 1)
  3863. (list 'face (org-get-tag-face 1)
  3864. 'font-lock-fontified t))
  3865. (backward-char 1))))
  3866. (defun org-get-tag-face (kwd)
  3867. "Get the right face for a TODO keyword KWD.
  3868. If KWD is a number, get the corresponding match group."
  3869. (if (numberp kwd) (setq kwd (match-string kwd)))
  3870. (or (cdr (assoc kwd org-tag-faces))
  3871. 'org-tag))
  3872. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3873. "Remove fontification and activation overlays from links."
  3874. (font-lock-default-unfontify-region beg end)
  3875. (let* ((buffer-undo-list t)
  3876. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3877. (inhibit-modification-hooks t)
  3878. deactivate-mark buffer-file-name buffer-file-truename)
  3879. (remove-text-properties beg end
  3880. '(mouse-face t keymap t org-linked-text t
  3881. invisible t intangible t))))
  3882. ;;;; Visibility cycling, including org-goto and indirect buffer
  3883. ;;; Cycling
  3884. (defvar org-cycle-global-status nil)
  3885. (make-variable-buffer-local 'org-cycle-global-status)
  3886. (defvar org-cycle-subtree-status nil)
  3887. (make-variable-buffer-local 'org-cycle-subtree-status)
  3888. ;;;###autoload
  3889. (defun org-cycle (&optional arg)
  3890. "Visibility cycling for Org-mode.
  3891. - When this function is called with a prefix argument, rotate the entire
  3892. buffer through 3 states (global cycling)
  3893. 1. OVERVIEW: Show only top-level headlines.
  3894. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3895. 3. SHOW ALL: Show everything.
  3896. When called with two C-u C-u prefixes, switch to the startup visibility,
  3897. determined by the variable `org-startup-folded', and by any VISIBILITY
  3898. properties in the buffer.
  3899. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3900. including drawers.
  3901. - When point is at the beginning of a headline, rotate the subtree started
  3902. by this line through 3 different states (local cycling)
  3903. 1. FOLDED: Only the main headline is shown.
  3904. 2. CHILDREN: The main headline and the direct children are shown.
  3905. From this state, you can move to one of the children
  3906. and zoom in further.
  3907. 3. SUBTREE: Show the entire subtree, including body text.
  3908. - When there is a numeric prefix, go up to a heading with level ARG, do
  3909. a `show-subtree' and return to the previous cursor position. If ARG
  3910. is negative, go up that many levels.
  3911. - When point is not at the beginning of a headline, execute the global
  3912. binding for TAB, which is re-indenting the line. See the option
  3913. `org-cycle-emulate-tab' for details.
  3914. - Special case: if point is at the beginning of the buffer and there is
  3915. no headline in line 1, this function will act as if called with prefix arg.
  3916. But only if also the variable `org-cycle-global-at-bob' is t."
  3917. (interactive "P")
  3918. (org-load-modules-maybe)
  3919. (let* ((outline-regexp
  3920. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3921. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3922. outline-regexp))
  3923. (bob-special (and org-cycle-global-at-bob (bobp)
  3924. (not (looking-at outline-regexp))))
  3925. (org-cycle-hook
  3926. (if bob-special
  3927. (delq 'org-optimize-window-after-visibility-change
  3928. (copy-sequence org-cycle-hook))
  3929. org-cycle-hook))
  3930. (pos (point)))
  3931. (if (or bob-special (equal arg '(4)))
  3932. ;; special case: use global cycling
  3933. (setq arg t))
  3934. (cond
  3935. ((equal arg '(16))
  3936. (org-set-startup-visibility)
  3937. (message "Startup visibility, plus VISIBILITY properties"))
  3938. ((equal arg '(64))
  3939. (show-all)
  3940. (message "Entire buffer visible, including drawers"))
  3941. ((org-at-table-p 'any)
  3942. ;; Enter the table or move to the next field in the table
  3943. (or (org-table-recognize-table.el)
  3944. (progn
  3945. (if arg (org-table-edit-field t)
  3946. (org-table-justify-field-maybe)
  3947. (call-interactively 'org-table-next-field)))))
  3948. ((eq arg t) ;; Global cycling
  3949. (cond
  3950. ((and (eq last-command this-command)
  3951. (eq org-cycle-global-status 'overview))
  3952. ;; We just created the overview - now do table of contents
  3953. ;; This can be slow in very large buffers, so indicate action
  3954. (message "CONTENTS...")
  3955. (org-content)
  3956. (message "CONTENTS...done")
  3957. (setq org-cycle-global-status 'contents)
  3958. (run-hook-with-args 'org-cycle-hook 'contents))
  3959. ((and (eq last-command this-command)
  3960. (eq org-cycle-global-status 'contents))
  3961. ;; We just showed the table of contents - now show everything
  3962. (show-all)
  3963. (message "SHOW ALL")
  3964. (setq org-cycle-global-status 'all)
  3965. (run-hook-with-args 'org-cycle-hook 'all))
  3966. (t
  3967. ;; Default action: go to overview
  3968. (org-overview)
  3969. (message "OVERVIEW")
  3970. (setq org-cycle-global-status 'overview)
  3971. (run-hook-with-args 'org-cycle-hook 'overview))))
  3972. ((and org-drawers org-drawer-regexp
  3973. (save-excursion
  3974. (beginning-of-line 1)
  3975. (looking-at org-drawer-regexp)))
  3976. ;; Toggle block visibility
  3977. (org-flag-drawer
  3978. (not (get-char-property (match-end 0) 'invisible))))
  3979. ((integerp arg)
  3980. ;; Show-subtree, ARG levels up from here.
  3981. (save-excursion
  3982. (org-back-to-heading)
  3983. (outline-up-heading (if (< arg 0) (- arg)
  3984. (- (funcall outline-level) arg)))
  3985. (org-show-subtree)))
  3986. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3987. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3988. ;; At a heading: rotate between three different views
  3989. (org-back-to-heading)
  3990. (let ((goal-column 0) eoh eol eos)
  3991. ;; First, some boundaries
  3992. (save-excursion
  3993. (org-back-to-heading)
  3994. (save-excursion
  3995. (beginning-of-line 2)
  3996. (while (and (not (eobp)) ;; this is like `next-line'
  3997. (get-char-property (1- (point)) 'invisible))
  3998. (beginning-of-line 2)) (setq eol (point)))
  3999. (outline-end-of-heading) (setq eoh (point))
  4000. (org-end-of-subtree t)
  4001. (unless (eobp)
  4002. (skip-chars-forward " \t\n")
  4003. (beginning-of-line 1) ; in case this is an item
  4004. )
  4005. (setq eos (1- (point))))
  4006. ;; Find out what to do next and set `this-command'
  4007. (cond
  4008. ((= eos eoh)
  4009. ;; Nothing is hidden behind this heading
  4010. (message "EMPTY ENTRY")
  4011. (setq org-cycle-subtree-status nil)
  4012. (save-excursion
  4013. (goto-char eos)
  4014. (outline-next-heading)
  4015. (if (org-invisible-p) (org-flag-heading nil))))
  4016. ((or (>= eol eos)
  4017. (not (string-match "\\S-" (buffer-substring eol eos))))
  4018. ;; Entire subtree is hidden in one line: open it
  4019. (org-show-entry)
  4020. (show-children)
  4021. (message "CHILDREN")
  4022. (save-excursion
  4023. (goto-char eos)
  4024. (outline-next-heading)
  4025. (if (org-invisible-p) (org-flag-heading nil)))
  4026. (setq org-cycle-subtree-status 'children)
  4027. (run-hook-with-args 'org-cycle-hook 'children))
  4028. ((and (eq last-command this-command)
  4029. (eq org-cycle-subtree-status 'children))
  4030. ;; We just showed the children, now show everything.
  4031. (org-show-subtree)
  4032. (message "SUBTREE")
  4033. (setq org-cycle-subtree-status 'subtree)
  4034. (run-hook-with-args 'org-cycle-hook 'subtree))
  4035. (t
  4036. ;; Default action: hide the subtree.
  4037. (hide-subtree)
  4038. (message "FOLDED")
  4039. (setq org-cycle-subtree-status 'folded)
  4040. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4041. ;; TAB emulation and template completion
  4042. (buffer-read-only (org-back-to-heading))
  4043. ((org-try-structure-completion))
  4044. ((org-try-cdlatex-tab))
  4045. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4046. (or (not (bolp))
  4047. (not (looking-at outline-regexp))))
  4048. (call-interactively (global-key-binding "\t")))
  4049. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4050. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4051. (or (and (eq org-cycle-emulate-tab 'white)
  4052. (= (match-end 0) (point-at-eol)))
  4053. (and (eq org-cycle-emulate-tab 'whitestart)
  4054. (>= (match-end 0) pos))))
  4055. t
  4056. (eq org-cycle-emulate-tab t))
  4057. (call-interactively (global-key-binding "\t")))
  4058. (t (save-excursion
  4059. (org-back-to-heading)
  4060. (org-cycle))))))
  4061. ;;;###autoload
  4062. (defun org-global-cycle (&optional arg)
  4063. "Cycle the global visibility. For details see `org-cycle'.
  4064. With C-u prefix arg, switch to startup visibility.
  4065. With a numeric prefix, show all headlines up to that level."
  4066. (interactive "P")
  4067. (let ((org-cycle-include-plain-lists
  4068. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4069. (cond
  4070. ((integerp arg)
  4071. (show-all)
  4072. (hide-sublevels arg)
  4073. (setq org-cycle-global-status 'contents))
  4074. ((equal arg '(4))
  4075. (org-set-startup-visibility)
  4076. (message "Startup visibility, plus VISIBILITY properties."))
  4077. (t
  4078. (org-cycle '(4))))))
  4079. (defun org-set-startup-visibility ()
  4080. "Set the visibility required by startup options and properties."
  4081. (cond
  4082. ((eq org-startup-folded t)
  4083. (org-cycle '(4)))
  4084. ((eq org-startup-folded 'content)
  4085. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4086. (org-cycle '(4)) (org-cycle '(4)))))
  4087. (org-set-visibility-according-to-property 'no-cleanup)
  4088. (org-cycle-hide-archived-subtrees 'all)
  4089. (org-cycle-hide-drawers 'all)
  4090. (org-cycle-show-empty-lines 'all))
  4091. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4092. "Switch subtree visibilities according to :VISIBILITY: property."
  4093. (interactive)
  4094. (let (org-show-entry-below state)
  4095. (save-excursion
  4096. (goto-char (point-min))
  4097. (while (re-search-forward
  4098. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4099. nil t)
  4100. (setq state (match-string 1))
  4101. (save-excursion
  4102. (org-back-to-heading t)
  4103. (hide-subtree)
  4104. (org-reveal)
  4105. (cond
  4106. ((equal state '("fold" "folded"))
  4107. (hide-subtree))
  4108. ((equal state "children")
  4109. (org-show-hidden-entry)
  4110. (show-children))
  4111. ((equal state "content")
  4112. (save-excursion
  4113. (save-restriction
  4114. (org-narrow-to-subtree)
  4115. (org-content))))
  4116. ((member state '("all" "showall"))
  4117. (show-subtree)))))
  4118. (unless no-cleanup
  4119. (org-cycle-hide-archived-subtrees 'all)
  4120. (org-cycle-hide-drawers 'all)
  4121. (org-cycle-show-empty-lines 'all)))))
  4122. (defun org-overview ()
  4123. "Switch to overview mode, showing only top-level headlines.
  4124. Really, this shows all headlines with level equal or greater than the level
  4125. of the first headline in the buffer. This is important, because if the
  4126. first headline is not level one, then (hide-sublevels 1) gives confusing
  4127. results."
  4128. (interactive)
  4129. (let ((level (save-excursion
  4130. (goto-char (point-min))
  4131. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4132. (progn
  4133. (goto-char (match-beginning 0))
  4134. (funcall outline-level))))))
  4135. (and level (hide-sublevels level))))
  4136. (defun org-content (&optional arg)
  4137. "Show all headlines in the buffer, like a table of contents.
  4138. With numerical argument N, show content up to level N."
  4139. (interactive "P")
  4140. (save-excursion
  4141. ;; Visit all headings and show their offspring
  4142. (and (integerp arg) (org-overview))
  4143. (goto-char (point-max))
  4144. (catch 'exit
  4145. (while (and (progn (condition-case nil
  4146. (outline-previous-visible-heading 1)
  4147. (error (goto-char (point-min))))
  4148. t)
  4149. (looking-at outline-regexp))
  4150. (if (integerp arg)
  4151. (show-children (1- arg))
  4152. (show-branches))
  4153. (if (bobp) (throw 'exit nil))))))
  4154. (defun org-optimize-window-after-visibility-change (state)
  4155. "Adjust the window after a change in outline visibility.
  4156. This function is the default value of the hook `org-cycle-hook'."
  4157. (when (get-buffer-window (current-buffer))
  4158. (cond
  4159. ((eq state 'content) nil)
  4160. ((eq state 'all) nil)
  4161. ((eq state 'folded) nil)
  4162. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4163. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4164. (defun org-compact-display-after-subtree-move ()
  4165. "Show a compacter version of the tree of the entry's parent."
  4166. (save-excursion
  4167. (if (org-up-heading-safe)
  4168. (progn
  4169. (hide-subtree)
  4170. (show-entry)
  4171. (show-children)
  4172. (org-cycle-show-empty-lines 'children)
  4173. (org-cycle-hide-drawers 'children))
  4174. (org-overview))))
  4175. (defun org-cycle-show-empty-lines (state)
  4176. "Show empty lines above all visible headlines.
  4177. The region to be covered depends on STATE when called through
  4178. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4179. entire buffer covered. Note that an empty line is only shown if there
  4180. are at least `org-cycle-separator-lines' empty lines before the headline."
  4181. (when (> org-cycle-separator-lines 0)
  4182. (save-excursion
  4183. (let* ((n org-cycle-separator-lines)
  4184. (re (cond
  4185. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4186. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4187. (t (let ((ns (number-to-string (- n 2))))
  4188. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4189. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4190. beg end)
  4191. (cond
  4192. ((memq state '(overview contents t))
  4193. (setq beg (point-min) end (point-max)))
  4194. ((memq state '(children folded))
  4195. (setq beg (point) end (progn (org-end-of-subtree t t)
  4196. (beginning-of-line 2)
  4197. (point)))))
  4198. (when beg
  4199. (goto-char beg)
  4200. (while (re-search-forward re end t)
  4201. (if (not (get-char-property (match-end 1) 'invisible))
  4202. (outline-flag-region
  4203. (match-beginning 1) (match-end 1) nil)))))))
  4204. ;; Never hide empty lines at the end of the file.
  4205. (save-excursion
  4206. (goto-char (point-max))
  4207. (outline-previous-heading)
  4208. (outline-end-of-heading)
  4209. (if (and (looking-at "[ \t\n]+")
  4210. (= (match-end 0) (point-max)))
  4211. (outline-flag-region (point) (match-end 0) nil))))
  4212. (defun org-show-empty-lines-in-parent ()
  4213. "Move to the parent and re-show empty lines before visible headlines."
  4214. (save-excursion
  4215. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4216. (org-cycle-show-empty-lines context))))
  4217. (defun org-cycle-hide-drawers (state)
  4218. "Re-hide all drawers after a visibility state change."
  4219. (when (and (org-mode-p)
  4220. (not (memq state '(overview folded))))
  4221. (save-excursion
  4222. (let* ((globalp (memq state '(contents all)))
  4223. (beg (if globalp (point-min) (point)))
  4224. (end (if globalp (point-max) (org-end-of-subtree t))))
  4225. (goto-char beg)
  4226. (while (re-search-forward org-drawer-regexp end t)
  4227. (org-flag-drawer t))))))
  4228. (defun org-flag-drawer (flag)
  4229. (save-excursion
  4230. (beginning-of-line 1)
  4231. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4232. (let ((b (match-end 0))
  4233. (outline-regexp org-outline-regexp))
  4234. (if (re-search-forward
  4235. "^[ \t]*:END:"
  4236. (save-excursion (outline-next-heading) (point)) t)
  4237. (outline-flag-region b (point-at-eol) flag)
  4238. (error ":END: line missing"))))))
  4239. (defun org-subtree-end-visible-p ()
  4240. "Is the end of the current subtree visible?"
  4241. (pos-visible-in-window-p
  4242. (save-excursion (org-end-of-subtree t) (point))))
  4243. (defun org-first-headline-recenter (&optional N)
  4244. "Move cursor to the first headline and recenter the headline.
  4245. Optional argument N means, put the headline into the Nth line of the window."
  4246. (goto-char (point-min))
  4247. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4248. (beginning-of-line)
  4249. (recenter (prefix-numeric-value N))))
  4250. ;;; Org-goto
  4251. (defvar org-goto-window-configuration nil)
  4252. (defvar org-goto-marker nil)
  4253. (defvar org-goto-map
  4254. (let ((map (make-sparse-keymap)))
  4255. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4256. (while (setq cmd (pop cmds))
  4257. (substitute-key-definition cmd cmd map global-map)))
  4258. (suppress-keymap map)
  4259. (org-defkey map "\C-m" 'org-goto-ret)
  4260. (org-defkey map [(return)] 'org-goto-ret)
  4261. (org-defkey map [(left)] 'org-goto-left)
  4262. (org-defkey map [(right)] 'org-goto-right)
  4263. (org-defkey map [(control ?g)] 'org-goto-quit)
  4264. (org-defkey map "\C-i" 'org-cycle)
  4265. (org-defkey map [(tab)] 'org-cycle)
  4266. (org-defkey map [(down)] 'outline-next-visible-heading)
  4267. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4268. (if org-goto-auto-isearch
  4269. (if (fboundp 'define-key-after)
  4270. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4271. nil)
  4272. (org-defkey map "q" 'org-goto-quit)
  4273. (org-defkey map "n" 'outline-next-visible-heading)
  4274. (org-defkey map "p" 'outline-previous-visible-heading)
  4275. (org-defkey map "f" 'outline-forward-same-level)
  4276. (org-defkey map "b" 'outline-backward-same-level)
  4277. (org-defkey map "u" 'outline-up-heading))
  4278. (org-defkey map "/" 'org-occur)
  4279. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4280. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4281. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4282. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4283. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4284. map))
  4285. (defconst org-goto-help
  4286. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4287. RET=jump to location [Q]uit and return to previous location
  4288. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4289. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4290. ;; FIXME: Docstring doe not mention both interfaces
  4291. (defun org-goto (&optional alternative-interface)
  4292. "Look up a different location in the current file, keeping current visibility.
  4293. When you want look-up or go to a different location in a document, the
  4294. fastest way is often to fold the entire buffer and then dive into the tree.
  4295. This method has the disadvantage, that the previous location will be folded,
  4296. which may not be what you want.
  4297. This command works around this by showing a copy of the current buffer
  4298. in an indirect buffer, in overview mode. You can dive into the tree in
  4299. that copy, use org-occur and incremental search to find a location.
  4300. When pressing RET or `Q', the command returns to the original buffer in
  4301. which the visibility is still unchanged. After RET is will also jump to
  4302. the location selected in the indirect buffer and expose the
  4303. the headline hierarchy above."
  4304. (interactive "P")
  4305. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4306. (org-refile-use-outline-path t)
  4307. (interface
  4308. (if (not alternative-interface)
  4309. org-goto-interface
  4310. (if (eq org-goto-interface 'outline)
  4311. 'outline-path-completion
  4312. 'outline)))
  4313. (org-goto-start-pos (point))
  4314. (selected-point
  4315. (if (eq interface 'outline)
  4316. (car (org-get-location (current-buffer) org-goto-help))
  4317. (nth 3 (org-refile-get-location "Goto: ")))))
  4318. (if selected-point
  4319. (progn
  4320. (org-mark-ring-push org-goto-start-pos)
  4321. (goto-char selected-point)
  4322. (if (or (org-invisible-p) (org-invisible-p2))
  4323. (org-show-context 'org-goto)))
  4324. (message "Quit"))))
  4325. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4326. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4327. (defvar org-goto-local-auto-isearch-map) ; defined below
  4328. (defun org-get-location (buf help)
  4329. "Let the user select a location in the Org-mode buffer BUF.
  4330. This function uses a recursive edit. It returns the selected position
  4331. or nil."
  4332. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4333. (isearch-hide-immediately nil)
  4334. (isearch-search-fun-function
  4335. (lambda () 'org-goto-local-search-headings))
  4336. (org-goto-selected-point org-goto-exit-command))
  4337. (save-excursion
  4338. (save-window-excursion
  4339. (delete-other-windows)
  4340. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4341. (switch-to-buffer
  4342. (condition-case nil
  4343. (make-indirect-buffer (current-buffer) "*org-goto*")
  4344. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4345. (with-output-to-temp-buffer "*Help*"
  4346. (princ help))
  4347. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4348. (setq buffer-read-only nil)
  4349. (let ((org-startup-truncated t)
  4350. (org-startup-folded nil)
  4351. (org-startup-align-all-tables nil))
  4352. (org-mode)
  4353. (org-overview))
  4354. (setq buffer-read-only t)
  4355. (if (and (boundp 'org-goto-start-pos)
  4356. (integer-or-marker-p org-goto-start-pos))
  4357. (let ((org-show-hierarchy-above t)
  4358. (org-show-siblings t)
  4359. (org-show-following-heading t))
  4360. (goto-char org-goto-start-pos)
  4361. (and (org-invisible-p) (org-show-context)))
  4362. (goto-char (point-min)))
  4363. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4364. (message "Select location and press RET")
  4365. (use-local-map org-goto-map)
  4366. (recursive-edit)
  4367. ))
  4368. (kill-buffer "*org-goto*")
  4369. (cons org-goto-selected-point org-goto-exit-command)))
  4370. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4371. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4372. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4373. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4374. (defun org-goto-local-search-headings (string bound noerror)
  4375. "Search and make sure that any matches are in headlines."
  4376. (catch 'return
  4377. (while (if isearch-forward
  4378. (search-forward string bound noerror)
  4379. (search-backward string bound noerror))
  4380. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4381. (and (member :headline context)
  4382. (not (member :tags context))))
  4383. (throw 'return (point))))))
  4384. (defun org-goto-local-auto-isearch ()
  4385. "Start isearch."
  4386. (interactive)
  4387. (goto-char (point-min))
  4388. (let ((keys (this-command-keys)))
  4389. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4390. (isearch-mode t)
  4391. (isearch-process-search-char (string-to-char keys)))))
  4392. (defun org-goto-ret (&optional arg)
  4393. "Finish `org-goto' by going to the new location."
  4394. (interactive "P")
  4395. (setq org-goto-selected-point (point)
  4396. org-goto-exit-command 'return)
  4397. (throw 'exit nil))
  4398. (defun org-goto-left ()
  4399. "Finish `org-goto' by going to the new location."
  4400. (interactive)
  4401. (if (org-on-heading-p)
  4402. (progn
  4403. (beginning-of-line 1)
  4404. (setq org-goto-selected-point (point)
  4405. org-goto-exit-command 'left)
  4406. (throw 'exit nil))
  4407. (error "Not on a heading")))
  4408. (defun org-goto-right ()
  4409. "Finish `org-goto' by going to the new location."
  4410. (interactive)
  4411. (if (org-on-heading-p)
  4412. (progn
  4413. (setq org-goto-selected-point (point)
  4414. org-goto-exit-command 'right)
  4415. (throw 'exit nil))
  4416. (error "Not on a heading")))
  4417. (defun org-goto-quit ()
  4418. "Finish `org-goto' without cursor motion."
  4419. (interactive)
  4420. (setq org-goto-selected-point nil)
  4421. (setq org-goto-exit-command 'quit)
  4422. (throw 'exit nil))
  4423. ;;; Indirect buffer display of subtrees
  4424. (defvar org-indirect-dedicated-frame nil
  4425. "This is the frame being used for indirect tree display.")
  4426. (defvar org-last-indirect-buffer nil)
  4427. (defun org-tree-to-indirect-buffer (&optional arg)
  4428. "Create indirect buffer and narrow it to current subtree.
  4429. With numerical prefix ARG, go up to this level and then take that tree.
  4430. If ARG is negative, go up that many levels.
  4431. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4432. indirect buffer previously made with this command, to avoid proliferation of
  4433. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4434. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4435. is kept so that you can work with several indirect buffers at the same time.
  4436. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4437. requests that a new frame be made for the new buffer, so that the dedicated
  4438. frame is not changed."
  4439. (interactive "P")
  4440. (let ((cbuf (current-buffer))
  4441. (cwin (selected-window))
  4442. (pos (point))
  4443. beg end level heading ibuf)
  4444. (save-excursion
  4445. (org-back-to-heading t)
  4446. (when (numberp arg)
  4447. (setq level (org-outline-level))
  4448. (if (< arg 0) (setq arg (+ level arg)))
  4449. (while (> (setq level (org-outline-level)) arg)
  4450. (outline-up-heading 1 t)))
  4451. (setq beg (point)
  4452. heading (org-get-heading))
  4453. (org-end-of-subtree t) (setq end (point)))
  4454. (if (and (buffer-live-p org-last-indirect-buffer)
  4455. (not (eq org-indirect-buffer-display 'new-frame))
  4456. (not arg))
  4457. (kill-buffer org-last-indirect-buffer))
  4458. (setq ibuf (org-get-indirect-buffer cbuf)
  4459. org-last-indirect-buffer ibuf)
  4460. (cond
  4461. ((or (eq org-indirect-buffer-display 'new-frame)
  4462. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4463. (select-frame (make-frame))
  4464. (delete-other-windows)
  4465. (switch-to-buffer ibuf)
  4466. (org-set-frame-title heading))
  4467. ((eq org-indirect-buffer-display 'dedicated-frame)
  4468. (raise-frame
  4469. (select-frame (or (and org-indirect-dedicated-frame
  4470. (frame-live-p org-indirect-dedicated-frame)
  4471. org-indirect-dedicated-frame)
  4472. (setq org-indirect-dedicated-frame (make-frame)))))
  4473. (delete-other-windows)
  4474. (switch-to-buffer ibuf)
  4475. (org-set-frame-title (concat "Indirect: " heading)))
  4476. ((eq org-indirect-buffer-display 'current-window)
  4477. (switch-to-buffer ibuf))
  4478. ((eq org-indirect-buffer-display 'other-window)
  4479. (pop-to-buffer ibuf))
  4480. (t (error "Invalid value.")))
  4481. (if (featurep 'xemacs)
  4482. (save-excursion (org-mode) (turn-on-font-lock)))
  4483. (narrow-to-region beg end)
  4484. (show-all)
  4485. (goto-char pos)
  4486. (and (window-live-p cwin) (select-window cwin))))
  4487. (defun org-get-indirect-buffer (&optional buffer)
  4488. (setq buffer (or buffer (current-buffer)))
  4489. (let ((n 1) (base (buffer-name buffer)) bname)
  4490. (while (buffer-live-p
  4491. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4492. (setq n (1+ n)))
  4493. (condition-case nil
  4494. (make-indirect-buffer buffer bname 'clone)
  4495. (error (make-indirect-buffer buffer bname)))))
  4496. (defun org-set-frame-title (title)
  4497. "Set the title of the current frame to the string TITLE."
  4498. ;; FIXME: how to name a single frame in XEmacs???
  4499. (unless (featurep 'xemacs)
  4500. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4501. ;;;; Structure editing
  4502. ;;; Inserting headlines
  4503. (defun org-previous-line-empty-p ()
  4504. (save-excursion
  4505. (and (not (bobp))
  4506. (or (beginning-of-line 0) t)
  4507. (save-match-data
  4508. (looking-at "[ \t]*$")))))
  4509. (defun org-insert-heading (&optional force-heading)
  4510. "Insert a new heading or item with same depth at point.
  4511. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4512. If point is at the beginning of a headline, insert a sibling before the
  4513. current headline. If point is not at the beginning, do not split the line,
  4514. but create the new headline after the current line."
  4515. (interactive "P")
  4516. (if (= (buffer-size) 0)
  4517. (insert "\n* ")
  4518. (when (or force-heading (not (org-insert-item)))
  4519. (let* ((empty-line-p nil)
  4520. (head (save-excursion
  4521. (condition-case nil
  4522. (progn
  4523. (org-back-to-heading)
  4524. (setq empty-line-p (org-previous-line-empty-p))
  4525. (match-string 0))
  4526. (error "*"))))
  4527. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4528. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4529. pos hide-previous previous-pos)
  4530. (cond
  4531. ((and (org-on-heading-p) (bolp)
  4532. (or (bobp)
  4533. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4534. ;; insert before the current line
  4535. (open-line (if blank 2 1)))
  4536. ((and (bolp)
  4537. (or (bobp)
  4538. (save-excursion
  4539. (backward-char 1) (not (org-invisible-p)))))
  4540. ;; insert right here
  4541. nil)
  4542. (t
  4543. ;; somewhere in the line
  4544. (save-excursion
  4545. (setq previous-pos (point-at-bol))
  4546. (end-of-line)
  4547. (setq hide-previous (org-invisible-p)))
  4548. (and org-insert-heading-respect-content (org-show-subtree))
  4549. (let ((split
  4550. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4551. (save-excursion
  4552. (let ((p (point)))
  4553. (goto-char (point-at-bol))
  4554. (and (looking-at org-complex-heading-regexp)
  4555. (> p (match-beginning 4)))))))
  4556. tags pos)
  4557. (cond
  4558. (org-insert-heading-respect-content
  4559. (org-end-of-subtree nil t)
  4560. (or (bolp) (newline))
  4561. (or (org-previous-line-empty-p)
  4562. (and blank (newline)))
  4563. (open-line 1))
  4564. ((org-on-heading-p)
  4565. (when hide-previous
  4566. (show-children)
  4567. (org-show-entry))
  4568. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4569. (setq tags (and (match-end 2) (match-string 2)))
  4570. (and (match-end 1)
  4571. (delete-region (match-beginning 1) (match-end 1)))
  4572. (setq pos (point-at-bol))
  4573. (or split (end-of-line 1))
  4574. (delete-horizontal-space)
  4575. (newline (if blank 2 1))
  4576. (when tags
  4577. (save-excursion
  4578. (goto-char pos)
  4579. (end-of-line 1)
  4580. (insert " " tags)
  4581. (org-set-tags nil 'align))))
  4582. (t
  4583. (or split (end-of-line 1))
  4584. (newline (if blank 2 1)))))))
  4585. (insert head) (just-one-space)
  4586. (setq pos (point))
  4587. (end-of-line 1)
  4588. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4589. (when (and org-insert-heading-respect-content hide-previous)
  4590. (save-excursion
  4591. (goto-char previous-pos)
  4592. (hide-subtree)))
  4593. (run-hooks 'org-insert-heading-hook)))))
  4594. (defun org-get-heading (&optional no-tags)
  4595. "Return the heading of the current entry, without the stars."
  4596. (save-excursion
  4597. (org-back-to-heading t)
  4598. (if (looking-at
  4599. (if no-tags
  4600. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4601. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4602. (match-string 1) "")))
  4603. (defun org-heading-components ()
  4604. "Return the components of the current heading.
  4605. This is a list with the following elements:
  4606. - the level as an integer
  4607. - the reduced level, different if `org-odd-levels-only' is set.
  4608. - the TODO keyword, or nil
  4609. - the priority character, like ?A, or nil if no priority is given
  4610. - the headline text itself, or the tags string if no headline text
  4611. - the tags string, or nil."
  4612. (save-excursion
  4613. (org-back-to-heading t)
  4614. (if (looking-at org-complex-heading-regexp)
  4615. (list (length (match-string 1))
  4616. (org-reduced-level (length (match-string 1)))
  4617. (org-match-string-no-properties 2)
  4618. (and (match-end 3) (aref (match-string 3) 2))
  4619. (org-match-string-no-properties 4)
  4620. (org-match-string-no-properties 5)))))
  4621. (defun org-insert-heading-after-current ()
  4622. "Insert a new heading with same level as current, after current subtree."
  4623. (interactive)
  4624. (org-back-to-heading)
  4625. (org-insert-heading)
  4626. (org-move-subtree-down)
  4627. (end-of-line 1))
  4628. (defun org-insert-heading-respect-content ()
  4629. (interactive)
  4630. (let ((org-insert-heading-respect-content t))
  4631. (org-insert-heading t)))
  4632. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4633. (interactive "P")
  4634. (let ((org-insert-heading-respect-content t))
  4635. (org-insert-todo-heading force-state t)))
  4636. (defun org-insert-todo-heading (arg &optional force-heading)
  4637. "Insert a new heading with the same level and TODO state as current heading.
  4638. If the heading has no TODO state, or if the state is DONE, use the first
  4639. state (TODO by default). Also with prefix arg, force first state."
  4640. (interactive "P")
  4641. (when (or force-heading (not (org-insert-item 'checkbox)))
  4642. (org-insert-heading force-heading)
  4643. (save-excursion
  4644. (org-back-to-heading)
  4645. (outline-previous-heading)
  4646. (looking-at org-todo-line-regexp))
  4647. (let*
  4648. ((new-mark-x
  4649. (if (or arg
  4650. (not (match-beginning 2))
  4651. (member (match-string 2) org-done-keywords))
  4652. (car org-todo-keywords-1)
  4653. (match-string 2)))
  4654. (new-mark
  4655. (or
  4656. (run-hook-with-args-until-success
  4657. 'org-todo-get-default-hook new-mark-x nil)
  4658. new-mark-x)))
  4659. (insert new-mark " "))
  4660. (when org-provide-todo-statistics
  4661. (org-update-parent-todo-statistics))))
  4662. (defun org-insert-subheading (arg)
  4663. "Insert a new subheading and demote it.
  4664. Works for outline headings and for plain lists alike."
  4665. (interactive "P")
  4666. (org-insert-heading arg)
  4667. (cond
  4668. ((org-on-heading-p) (org-do-demote))
  4669. ((org-at-item-p) (org-indent-item 1))))
  4670. (defun org-insert-todo-subheading (arg)
  4671. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4672. Works for outline headings and for plain lists alike."
  4673. (interactive "P")
  4674. (org-insert-todo-heading arg)
  4675. (cond
  4676. ((org-on-heading-p) (org-do-demote))
  4677. ((org-at-item-p) (org-indent-item 1))))
  4678. ;;; Promotion and Demotion
  4679. (defun org-promote-subtree ()
  4680. "Promote the entire subtree.
  4681. See also `org-promote'."
  4682. (interactive)
  4683. (save-excursion
  4684. (org-map-tree 'org-promote))
  4685. (org-fix-position-after-promote))
  4686. (defun org-demote-subtree ()
  4687. "Demote the entire subtree. See `org-demote'.
  4688. See also `org-promote'."
  4689. (interactive)
  4690. (save-excursion
  4691. (org-map-tree 'org-demote))
  4692. (org-fix-position-after-promote))
  4693. (defun org-do-promote ()
  4694. "Promote the current heading higher up the tree.
  4695. If the region is active in `transient-mark-mode', promote all headings
  4696. in the region."
  4697. (interactive)
  4698. (save-excursion
  4699. (if (org-region-active-p)
  4700. (org-map-region 'org-promote (region-beginning) (region-end))
  4701. (org-promote)))
  4702. (org-fix-position-after-promote))
  4703. (defun org-do-demote ()
  4704. "Demote the current heading lower down the tree.
  4705. If the region is active in `transient-mark-mode', demote all headings
  4706. in the region."
  4707. (interactive)
  4708. (save-excursion
  4709. (if (org-region-active-p)
  4710. (org-map-region 'org-demote (region-beginning) (region-end))
  4711. (org-demote)))
  4712. (org-fix-position-after-promote))
  4713. (defun org-fix-position-after-promote ()
  4714. "Make sure that after pro/demotion cursor position is right."
  4715. (let ((pos (point)))
  4716. (when (save-excursion
  4717. (beginning-of-line 1)
  4718. (looking-at org-todo-line-regexp)
  4719. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4720. (cond ((eobp) (insert " "))
  4721. ((eolp) (insert " "))
  4722. ((equal (char-after) ?\ ) (forward-char 1))))))
  4723. (defun org-reduced-level (l)
  4724. "Compute the effective level of a heading.
  4725. This takes into account the setting of `org-odd-levels-only'."
  4726. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4727. (defun org-get-valid-level (level &optional change)
  4728. "Rectify a level change under the influence of `org-odd-levels-only'
  4729. LEVEL is a current level, CHANGE is by how much the level should be
  4730. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4731. even level numbers will become the next higher odd number."
  4732. (if org-odd-levels-only
  4733. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4734. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4735. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4736. (max 1 (+ level change))))
  4737. (if (boundp 'define-obsolete-function-alias)
  4738. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4739. (define-obsolete-function-alias 'org-get-legal-level
  4740. 'org-get-valid-level)
  4741. (define-obsolete-function-alias 'org-get-legal-level
  4742. 'org-get-valid-level "23.1")))
  4743. (defun org-promote ()
  4744. "Promote the current heading higher up the tree.
  4745. If the region is active in `transient-mark-mode', promote all headings
  4746. in the region."
  4747. (org-back-to-heading t)
  4748. (let* ((level (save-match-data (funcall outline-level)))
  4749. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4750. (diff (abs (- level (length up-head) -1))))
  4751. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4752. (replace-match up-head nil t)
  4753. ;; Fixup tag positioning
  4754. (and org-auto-align-tags (org-set-tags nil t))
  4755. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4756. (defun org-demote ()
  4757. "Demote the current heading lower down the tree.
  4758. If the region is active in `transient-mark-mode', demote all headings
  4759. in the region."
  4760. (org-back-to-heading t)
  4761. (let* ((level (save-match-data (funcall outline-level)))
  4762. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4763. (diff (abs (- level (length down-head) -1))))
  4764. (replace-match down-head nil t)
  4765. ;; Fixup tag positioning
  4766. (and org-auto-align-tags (org-set-tags nil t))
  4767. (if org-adapt-indentation (org-fixup-indentation diff))))
  4768. (defun org-map-tree (fun)
  4769. "Call FUN for every heading underneath the current one."
  4770. (org-back-to-heading)
  4771. (let ((level (funcall outline-level)))
  4772. (save-excursion
  4773. (funcall fun)
  4774. (while (and (progn
  4775. (outline-next-heading)
  4776. (> (funcall outline-level) level))
  4777. (not (eobp)))
  4778. (funcall fun)))))
  4779. (defun org-map-region (fun beg end)
  4780. "Call FUN for every heading between BEG and END."
  4781. (let ((org-ignore-region t))
  4782. (save-excursion
  4783. (setq end (copy-marker end))
  4784. (goto-char beg)
  4785. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4786. (< (point) end))
  4787. (funcall fun))
  4788. (while (and (progn
  4789. (outline-next-heading)
  4790. (< (point) end))
  4791. (not (eobp)))
  4792. (funcall fun)))))
  4793. (defun org-fixup-indentation (diff)
  4794. "Change the indentation in the current entry by DIFF
  4795. However, if any line in the current entry has no indentation, or if it
  4796. would end up with no indentation after the change, nothing at all is done."
  4797. (save-excursion
  4798. (let ((end (save-excursion (outline-next-heading)
  4799. (point-marker)))
  4800. (prohibit (if (> diff 0)
  4801. "^\\S-"
  4802. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4803. col)
  4804. (unless (save-excursion (end-of-line 1)
  4805. (re-search-forward prohibit end t))
  4806. (while (and (< (point) end)
  4807. (re-search-forward "^[ \t]+" end t))
  4808. (goto-char (match-end 0))
  4809. (setq col (current-column))
  4810. (if (< diff 0) (replace-match ""))
  4811. (org-indent-to-column (+ diff col))))
  4812. (move-marker end nil))))
  4813. (defun org-convert-to-odd-levels ()
  4814. "Convert an org-mode file with all levels allowed to one with odd levels.
  4815. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4816. level 5 etc."
  4817. (interactive)
  4818. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4819. (let ((org-odd-levels-only nil) n)
  4820. (save-excursion
  4821. (goto-char (point-min))
  4822. (while (re-search-forward "^\\*\\*+ " nil t)
  4823. (setq n (- (length (match-string 0)) 2))
  4824. (while (>= (setq n (1- n)) 0)
  4825. (org-demote))
  4826. (end-of-line 1))))))
  4827. (defun org-convert-to-oddeven-levels ()
  4828. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4829. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4830. section with an even level, conversion would destroy the structure of the file. An error
  4831. is signaled in this case."
  4832. (interactive)
  4833. (goto-char (point-min))
  4834. ;; First check if there are no even levels
  4835. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4836. (org-show-context t)
  4837. (error "Not all levels are odd in this file. Conversion not possible."))
  4838. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4839. (let ((org-odd-levels-only nil) n)
  4840. (save-excursion
  4841. (goto-char (point-min))
  4842. (while (re-search-forward "^\\*\\*+ " nil t)
  4843. (setq n (/ (1- (length (match-string 0))) 2))
  4844. (while (>= (setq n (1- n)) 0)
  4845. (org-promote))
  4846. (end-of-line 1))))))
  4847. (defun org-tr-level (n)
  4848. "Make N odd if required."
  4849. (if org-odd-levels-only (1+ (/ n 2)) n))
  4850. ;;; Vertical tree motion, cutting and pasting of subtrees
  4851. (defun org-move-subtree-up (&optional arg)
  4852. "Move the current subtree up past ARG headlines of the same level."
  4853. (interactive "p")
  4854. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4855. (defun org-move-subtree-down (&optional arg)
  4856. "Move the current subtree down past ARG headlines of the same level."
  4857. (interactive "p")
  4858. (setq arg (prefix-numeric-value arg))
  4859. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4860. 'outline-get-last-sibling))
  4861. (ins-point (make-marker))
  4862. (cnt (abs arg))
  4863. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4864. ;; Select the tree
  4865. (org-back-to-heading)
  4866. (setq beg0 (point))
  4867. (save-excursion
  4868. (setq ne-beg (org-back-over-empty-lines))
  4869. (setq beg (point)))
  4870. (save-match-data
  4871. (save-excursion (outline-end-of-heading)
  4872. (setq folded (org-invisible-p)))
  4873. (outline-end-of-subtree))
  4874. (outline-next-heading)
  4875. (setq ne-end (org-back-over-empty-lines))
  4876. (setq end (point))
  4877. (goto-char beg0)
  4878. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4879. ;; include less whitespace
  4880. (save-excursion
  4881. (goto-char beg)
  4882. (forward-line (- ne-beg ne-end))
  4883. (setq beg (point))))
  4884. ;; Find insertion point, with error handling
  4885. (while (> cnt 0)
  4886. (or (and (funcall movfunc) (looking-at outline-regexp))
  4887. (progn (goto-char beg0)
  4888. (error "Cannot move past superior level or buffer limit")))
  4889. (setq cnt (1- cnt)))
  4890. (if (> arg 0)
  4891. ;; Moving forward - still need to move over subtree
  4892. (progn (org-end-of-subtree t t)
  4893. (save-excursion
  4894. (org-back-over-empty-lines)
  4895. (or (bolp) (newline)))))
  4896. (setq ne-ins (org-back-over-empty-lines))
  4897. (move-marker ins-point (point))
  4898. (setq txt (buffer-substring beg end))
  4899. (org-save-markers-in-region beg end)
  4900. (delete-region beg end)
  4901. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4902. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4903. (let ((bbb (point)))
  4904. (insert-before-markers txt)
  4905. (org-reinstall-markers-in-region bbb)
  4906. (move-marker ins-point bbb))
  4907. (or (bolp) (insert "\n"))
  4908. (setq ins-end (point))
  4909. (goto-char ins-point)
  4910. (org-skip-whitespace)
  4911. (when (and (< arg 0)
  4912. (org-first-sibling-p)
  4913. (> ne-ins ne-beg))
  4914. ;; Move whitespace back to beginning
  4915. (save-excursion
  4916. (goto-char ins-end)
  4917. (let ((kill-whole-line t))
  4918. (kill-line (- ne-ins ne-beg)) (point)))
  4919. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4920. (move-marker ins-point nil)
  4921. (org-compact-display-after-subtree-move)
  4922. (org-show-empty-lines-in-parent)
  4923. (unless folded
  4924. (org-show-entry)
  4925. (show-children)
  4926. (org-cycle-hide-drawers 'children))))
  4927. (defvar org-subtree-clip ""
  4928. "Clipboard for cut and paste of subtrees.
  4929. This is actually only a copy of the kill, because we use the normal kill
  4930. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4931. (defvar org-subtree-clip-folded nil
  4932. "Was the last copied subtree folded?
  4933. This is used to fold the tree back after pasting.")
  4934. (defun org-cut-subtree (&optional n)
  4935. "Cut the current subtree into the clipboard.
  4936. With prefix arg N, cut this many sequential subtrees.
  4937. This is a short-hand for marking the subtree and then cutting it."
  4938. (interactive "p")
  4939. (org-copy-subtree n 'cut))
  4940. (defun org-copy-subtree (&optional n cut force-store-markers)
  4941. "Cut the current subtree into the clipboard.
  4942. With prefix arg N, cut this many sequential subtrees.
  4943. This is a short-hand for marking the subtree and then copying it.
  4944. If CUT is non-nil, actually cut the subtree.
  4945. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4946. of some markers in the region, even if CUT is non-nil. This is
  4947. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4948. (interactive "p")
  4949. (let (beg end folded (beg0 (point)))
  4950. (if (interactive-p)
  4951. (org-back-to-heading nil) ; take what looks like a subtree
  4952. (org-back-to-heading t)) ; take what is really there
  4953. (org-back-over-empty-lines)
  4954. (setq beg (point))
  4955. (skip-chars-forward " \t\r\n")
  4956. (save-match-data
  4957. (save-excursion (outline-end-of-heading)
  4958. (setq folded (org-invisible-p)))
  4959. (condition-case nil
  4960. (outline-forward-same-level (1- n))
  4961. (error nil))
  4962. (org-end-of-subtree t t))
  4963. (org-back-over-empty-lines)
  4964. (setq end (point))
  4965. (goto-char beg0)
  4966. (when (> end beg)
  4967. (setq org-subtree-clip-folded folded)
  4968. (when (or cut force-store-markers)
  4969. (org-save-markers-in-region beg end))
  4970. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4971. (setq org-subtree-clip (current-kill 0))
  4972. (message "%s: Subtree(s) with %d characters"
  4973. (if cut "Cut" "Copied")
  4974. (length org-subtree-clip)))))
  4975. (defun org-paste-subtree (&optional level tree for-yank)
  4976. "Paste the clipboard as a subtree, with modification of headline level.
  4977. The entire subtree is promoted or demoted in order to match a new headline
  4978. level.
  4979. If the cursor is at the beginning of a headline, the same level as
  4980. that headline is used to paste the tree
  4981. If not, the new level is derived from the *visible* headings
  4982. before and after the insertion point, and taken to be the inferior headline
  4983. level of the two. So if the previous visible heading is level 3 and the
  4984. next is level 4 (or vice versa), level 4 will be used for insertion.
  4985. This makes sure that the subtree remains an independent subtree and does
  4986. not swallow low level entries.
  4987. You can also force a different level, either by using a numeric prefix
  4988. argument, or by inserting the heading marker by hand. For example, if the
  4989. cursor is after \"*****\", then the tree will be shifted to level 5.
  4990. If optional TREE is given, use this text instead of the kill ring.
  4991. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4992. move back over whitespace before inserting, and move point to the end of
  4993. the inserted text when done."
  4994. (interactive "P")
  4995. (unless (org-kill-is-subtree-p tree)
  4996. (error "%s"
  4997. (substitute-command-keys
  4998. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4999. (let* ((visp (not (org-invisible-p)))
  5000. (txt (or tree (and kill-ring (current-kill 0))))
  5001. (^re (concat "^\\(" outline-regexp "\\)"))
  5002. (re (concat "\\(" outline-regexp "\\)"))
  5003. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5004. (old-level (if (string-match ^re txt)
  5005. (- (match-end 0) (match-beginning 0) 1)
  5006. -1))
  5007. (force-level (cond (level (prefix-numeric-value level))
  5008. ((and (looking-at "[ \t]*$")
  5009. (string-match
  5010. ^re_ (buffer-substring
  5011. (point-at-bol) (point))))
  5012. (- (match-end 1) (match-beginning 1)))
  5013. ((and (bolp)
  5014. (looking-at org-outline-regexp))
  5015. (- (match-end 0) (point) 1))
  5016. (t nil)))
  5017. (previous-level (save-excursion
  5018. (condition-case nil
  5019. (progn
  5020. (outline-previous-visible-heading 1)
  5021. (if (looking-at re)
  5022. (- (match-end 0) (match-beginning 0) 1)
  5023. 1))
  5024. (error 1))))
  5025. (next-level (save-excursion
  5026. (condition-case nil
  5027. (progn
  5028. (or (looking-at outline-regexp)
  5029. (outline-next-visible-heading 1))
  5030. (if (looking-at re)
  5031. (- (match-end 0) (match-beginning 0) 1)
  5032. 1))
  5033. (error 1))))
  5034. (new-level (or force-level (max previous-level next-level)))
  5035. (shift (if (or (= old-level -1)
  5036. (= new-level -1)
  5037. (= old-level new-level))
  5038. 0
  5039. (- new-level old-level)))
  5040. (delta (if (> shift 0) -1 1))
  5041. (func (if (> shift 0) 'org-demote 'org-promote))
  5042. (org-odd-levels-only nil)
  5043. beg end newend)
  5044. ;; Remove the forced level indicator
  5045. (if force-level
  5046. (delete-region (point-at-bol) (point)))
  5047. ;; Paste
  5048. (beginning-of-line 1)
  5049. (unless for-yank (org-back-over-empty-lines))
  5050. (setq beg (point))
  5051. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5052. (insert-before-markers txt)
  5053. (unless (string-match "\n\\'" txt) (insert "\n"))
  5054. (setq newend (point))
  5055. (org-reinstall-markers-in-region beg)
  5056. (setq end (point))
  5057. (goto-char beg)
  5058. (skip-chars-forward " \t\n\r")
  5059. (setq beg (point))
  5060. (if (and (org-invisible-p) visp)
  5061. (save-excursion (outline-show-heading)))
  5062. ;; Shift if necessary
  5063. (unless (= shift 0)
  5064. (save-restriction
  5065. (narrow-to-region beg end)
  5066. (while (not (= shift 0))
  5067. (org-map-region func (point-min) (point-max))
  5068. (setq shift (+ delta shift)))
  5069. (goto-char (point-min))
  5070. (setq newend (point-max))))
  5071. (when (or (interactive-p) for-yank)
  5072. (message "Clipboard pasted as level %d subtree" new-level))
  5073. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5074. kill-ring
  5075. (eq org-subtree-clip (current-kill 0))
  5076. org-subtree-clip-folded)
  5077. ;; The tree was folded before it was killed/copied
  5078. (hide-subtree))
  5079. (and for-yank (goto-char newend))))
  5080. (defun org-kill-is-subtree-p (&optional txt)
  5081. "Check if the current kill is an outline subtree, or a set of trees.
  5082. Returns nil if kill does not start with a headline, or if the first
  5083. headline level is not the largest headline level in the tree.
  5084. So this will actually accept several entries of equal levels as well,
  5085. which is OK for `org-paste-subtree'.
  5086. If optional TXT is given, check this string instead of the current kill."
  5087. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5088. (start-level (and kill
  5089. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5090. org-outline-regexp "\\)")
  5091. kill)
  5092. (- (match-end 2) (match-beginning 2) 1)))
  5093. (re (concat "^" org-outline-regexp))
  5094. (start (1+ (or (match-beginning 2) -1))))
  5095. (if (not start-level)
  5096. (progn
  5097. nil) ;; does not even start with a heading
  5098. (catch 'exit
  5099. (while (setq start (string-match re kill (1+ start)))
  5100. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5101. (throw 'exit nil)))
  5102. t))))
  5103. (defvar org-markers-to-move nil
  5104. "Markers that should be moved with a cut-and-paste operation.
  5105. Those markers are stored together with their positions relative to
  5106. the start of the region.")
  5107. (defun org-save-markers-in-region (beg end)
  5108. "Check markers in region.
  5109. If these markers are between BEG and END, record their position relative
  5110. to BEG, so that after moving the block of text, we can put the markers back
  5111. into place.
  5112. This function gets called just before an entry or tree gets cut from the
  5113. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5114. called immediately, to move the markers with the entries."
  5115. (setq org-markers-to-move nil)
  5116. (when (featurep 'org-clock)
  5117. (org-clock-save-markers-for-cut-and-paste beg end))
  5118. (when (featurep 'org-agenda)
  5119. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5120. (defun org-check-and-save-marker (marker beg end)
  5121. "Check if MARKER is between BEG and END.
  5122. If yes, remember the marker and the distance to BEG."
  5123. (when (and (marker-buffer marker)
  5124. (equal (marker-buffer marker) (current-buffer)))
  5125. (if (and (>= marker beg) (< marker end))
  5126. (push (cons marker (- marker beg)) org-markers-to-move))))
  5127. (defun org-reinstall-markers-in-region (beg)
  5128. "Move all remembered markers to their position relative to BEG."
  5129. (mapc (lambda (x)
  5130. (move-marker (car x) (+ beg (cdr x))))
  5131. org-markers-to-move)
  5132. (setq org-markers-to-move nil))
  5133. (defun org-narrow-to-subtree ()
  5134. "Narrow buffer to the current subtree."
  5135. (interactive)
  5136. (save-excursion
  5137. (save-match-data
  5138. (narrow-to-region
  5139. (progn (org-back-to-heading) (point))
  5140. (progn (org-end-of-subtree t) (point))))))
  5141. ;;; Outline Sorting
  5142. (defun org-sort (with-case)
  5143. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5144. Optional argument WITH-CASE means sort case-sensitively.
  5145. With a double prefix argument, also remove duplicate entries."
  5146. (interactive "P")
  5147. (if (org-at-table-p)
  5148. (org-call-with-arg 'org-table-sort-lines with-case)
  5149. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5150. (defun org-sort-remove-invisible (s)
  5151. (remove-text-properties 0 (length s) org-rm-props s)
  5152. (while (string-match org-bracket-link-regexp s)
  5153. (setq s (replace-match (if (match-end 2)
  5154. (match-string 3 s)
  5155. (match-string 1 s)) t t s)))
  5156. s)
  5157. (defvar org-priority-regexp) ; defined later in the file
  5158. (defun org-sort-entries-or-items
  5159. (&optional with-case sorting-type getkey-func compare-func property)
  5160. "Sort entries on a certain level of an outline tree.
  5161. If there is an active region, the entries in the region are sorted.
  5162. Else, if the cursor is before the first entry, sort the top-level items.
  5163. Else, the children of the entry at point are sorted.
  5164. Sorting can be alphabetically, numerically, and by date/time as given by
  5165. the first time stamp in the entry. The command prompts for the sorting
  5166. type unless it has been given to the function through the SORTING-TYPE
  5167. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5168. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5169. called with point at the beginning of the record. It must return either
  5170. a string or a number that should serve as the sorting key for that record.
  5171. Comparing entries ignores case by default. However, with an optional argument
  5172. WITH-CASE, the sorting considers case as well."
  5173. (interactive "P")
  5174. (let ((case-func (if with-case 'identity 'downcase))
  5175. start beg end stars re re2
  5176. txt what tmp plain-list-p)
  5177. ;; Find beginning and end of region to sort
  5178. (cond
  5179. ((org-region-active-p)
  5180. ;; we will sort the region
  5181. (setq end (region-end)
  5182. what "region")
  5183. (goto-char (region-beginning))
  5184. (if (not (org-on-heading-p)) (outline-next-heading))
  5185. (setq start (point)))
  5186. ((org-at-item-p)
  5187. ;; we will sort this plain list
  5188. (org-beginning-of-item-list) (setq start (point))
  5189. (org-end-of-item-list) (setq end (point))
  5190. (goto-char start)
  5191. (setq plain-list-p t
  5192. what "plain list"))
  5193. ((or (org-on-heading-p)
  5194. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5195. ;; we will sort the children of the current headline
  5196. (org-back-to-heading)
  5197. (setq start (point)
  5198. end (progn (org-end-of-subtree t t)
  5199. (org-back-over-empty-lines)
  5200. (point))
  5201. what "children")
  5202. (goto-char start)
  5203. (show-subtree)
  5204. (outline-next-heading))
  5205. (t
  5206. ;; we will sort the top-level entries in this file
  5207. (goto-char (point-min))
  5208. (or (org-on-heading-p) (outline-next-heading))
  5209. (setq start (point) end (point-max) what "top-level")
  5210. (goto-char start)
  5211. (show-all)))
  5212. (setq beg (point))
  5213. (if (>= beg end) (error "Nothing to sort"))
  5214. (unless plain-list-p
  5215. (looking-at "\\(\\*+\\)")
  5216. (setq stars (match-string 1)
  5217. re (concat "^" (regexp-quote stars) " +")
  5218. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5219. txt (buffer-substring beg end))
  5220. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5221. (if (and (not (equal stars "*")) (string-match re2 txt))
  5222. (error "Region to sort contains a level above the first entry")))
  5223. (unless sorting-type
  5224. (message
  5225. (if plain-list-p
  5226. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5227. "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:")
  5228. what)
  5229. (setq sorting-type (read-char-exclusive))
  5230. (and (= (downcase sorting-type) ?f)
  5231. (setq getkey-func
  5232. (org-ido-completing-read "Sort using function: "
  5233. obarray 'fboundp t nil nil))
  5234. (setq getkey-func (intern getkey-func)))
  5235. (and (= (downcase sorting-type) ?r)
  5236. (setq property
  5237. (org-ido-completing-read "Property: "
  5238. (mapcar 'list (org-buffer-property-keys t))
  5239. nil t))))
  5240. (message "Sorting entries...")
  5241. (save-restriction
  5242. (narrow-to-region start end)
  5243. (let ((dcst (downcase sorting-type))
  5244. (now (current-time)))
  5245. (sort-subr
  5246. (/= dcst sorting-type)
  5247. ;; This function moves to the beginning character of the "record" to
  5248. ;; be sorted.
  5249. (if plain-list-p
  5250. (lambda nil
  5251. (if (org-at-item-p) t (goto-char (point-max))))
  5252. (lambda nil
  5253. (if (re-search-forward re nil t)
  5254. (goto-char (match-beginning 0))
  5255. (goto-char (point-max)))))
  5256. ;; This function moves to the last character of the "record" being
  5257. ;; sorted.
  5258. (if plain-list-p
  5259. 'org-end-of-item
  5260. (lambda nil
  5261. (save-match-data
  5262. (condition-case nil
  5263. (outline-forward-same-level 1)
  5264. (error
  5265. (goto-char (point-max)))))))
  5266. ;; This function returns the value that gets sorted against.
  5267. (if plain-list-p
  5268. (lambda nil
  5269. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5270. (cond
  5271. ((= dcst ?n)
  5272. (string-to-number (buffer-substring (match-end 0)
  5273. (point-at-eol))))
  5274. ((= dcst ?a)
  5275. (buffer-substring (match-end 0) (point-at-eol)))
  5276. ((= dcst ?t)
  5277. (if (re-search-forward org-ts-regexp
  5278. (point-at-eol) t)
  5279. (org-time-string-to-time (match-string 0))
  5280. now))
  5281. ((= dcst ?f)
  5282. (if getkey-func
  5283. (progn
  5284. (setq tmp (funcall getkey-func))
  5285. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5286. tmp)
  5287. (error "Invalid key function `%s'" getkey-func)))
  5288. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5289. (lambda nil
  5290. (cond
  5291. ((= dcst ?n)
  5292. (if (looking-at org-complex-heading-regexp)
  5293. (string-to-number (match-string 4))
  5294. nil))
  5295. ((= dcst ?a)
  5296. (if (looking-at org-complex-heading-regexp)
  5297. (funcall case-func (match-string 4))
  5298. nil))
  5299. ((= dcst ?t)
  5300. (if (re-search-forward org-ts-regexp
  5301. (save-excursion
  5302. (forward-line 2)
  5303. (point)) t)
  5304. (org-time-string-to-time (match-string 0))
  5305. now))
  5306. ((= dcst ?p)
  5307. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5308. (string-to-char (match-string 2))
  5309. org-default-priority))
  5310. ((= dcst ?r)
  5311. (or (org-entry-get nil property) ""))
  5312. ((= dcst ?o)
  5313. (if (looking-at org-complex-heading-regexp)
  5314. (- 9999 (length (member (match-string 2)
  5315. org-todo-keywords-1)))))
  5316. ((= dcst ?f)
  5317. (if getkey-func
  5318. (progn
  5319. (setq tmp (funcall getkey-func))
  5320. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5321. tmp)
  5322. (error "Invalid key function `%s'" getkey-func)))
  5323. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5324. nil
  5325. (cond
  5326. ((= dcst ?a) 'string<)
  5327. ((= dcst ?t) 'time-less-p)
  5328. ((= dcst ?f) compare-func)
  5329. (t nil)))))
  5330. (message "Sorting entries...done")))
  5331. (defun org-do-sort (table what &optional with-case sorting-type)
  5332. "Sort TABLE of WHAT according to SORTING-TYPE.
  5333. The user will be prompted for the SORTING-TYPE if the call to this
  5334. function does not specify it. WHAT is only for the prompt, to indicate
  5335. what is being sorted. The sorting key will be extracted from
  5336. the car of the elements of the table.
  5337. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5338. (unless sorting-type
  5339. (message
  5340. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5341. what)
  5342. (setq sorting-type (read-char-exclusive)))
  5343. (let ((dcst (downcase sorting-type))
  5344. extractfun comparefun)
  5345. ;; Define the appropriate functions
  5346. (cond
  5347. ((= dcst ?n)
  5348. (setq extractfun 'string-to-number
  5349. comparefun (if (= dcst sorting-type) '< '>)))
  5350. ((= dcst ?a)
  5351. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5352. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5353. comparefun (if (= dcst sorting-type)
  5354. 'string<
  5355. (lambda (a b) (and (not (string< a b))
  5356. (not (string= a b)))))))
  5357. ((= dcst ?t)
  5358. (setq extractfun
  5359. (lambda (x)
  5360. (if (or (string-match org-ts-regexp x)
  5361. (string-match org-ts-regexp-both x))
  5362. (time-to-seconds
  5363. (org-time-string-to-time (match-string 0 x)))
  5364. 0))
  5365. comparefun (if (= dcst sorting-type) '< '>)))
  5366. (t (error "Invalid sorting type `%c'" sorting-type)))
  5367. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5368. table)
  5369. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5370. ;;; Editing source examples
  5371. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5372. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5373. (defvar org-edit-src-force-single-line nil)
  5374. (defvar org-edit-src-from-org-mode nil)
  5375. (defvar org-edit-src-picture nil)
  5376. (define-minor-mode org-exit-edit-mode
  5377. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5378. (defun org-edit-src-code ()
  5379. "Edit the source code example at point.
  5380. An indirect buffer is created, and that buffer is then narrowed to the
  5381. example at point and switched to the correct language mode. When done,
  5382. exit by killing the buffer with \\[org-edit-src-exit]."
  5383. (interactive)
  5384. (let ((line (org-current-line))
  5385. (case-fold-search t)
  5386. (msg (substitute-command-keys
  5387. "Edit, then exit with C-c ' (C-c and single quote)"))
  5388. (info (org-edit-src-find-region-and-lang))
  5389. (org-mode-p (eq major-mode 'org-mode))
  5390. beg end lang lang-f single lfmt)
  5391. (if (not info)
  5392. nil
  5393. (setq beg (nth 0 info)
  5394. end (nth 1 info)
  5395. lang (nth 2 info)
  5396. single (nth 3 info)
  5397. lfmt (nth 4 info)
  5398. lang-f (intern (concat lang "-mode")))
  5399. (unless (functionp lang-f)
  5400. (error "No such language mode: %s" lang-f))
  5401. (goto-line line)
  5402. (if (get-buffer "*Org Edit Src Example*")
  5403. (kill-buffer "*Org Edit Src Example*"))
  5404. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5405. "*Org Edit Src Example*"))
  5406. (narrow-to-region beg end)
  5407. (remove-text-properties beg end '(display nil invisible nil
  5408. intangible nil))
  5409. (let ((org-inhibit-startup t))
  5410. (funcall lang-f))
  5411. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5412. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5413. (when lfmt
  5414. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5415. (when org-mode-p
  5416. (goto-char (point-min))
  5417. (while (re-search-forward "^," nil t)
  5418. (replace-match "")))
  5419. (goto-line line)
  5420. (org-exit-edit-mode)
  5421. (org-set-local 'header-line-format msg)
  5422. (message "%s" msg)
  5423. t)))
  5424. (defun org-edit-fixed-width-region ()
  5425. "Edit the fixed-width ascii drawing at point.
  5426. This must be a region where each line starts with a colon followed by
  5427. a space character.
  5428. An indirect buffer is created, and that buffer is then narrowed to the
  5429. example at point and switched to artist-mode. When done,
  5430. exit by killing the buffer with \\[org-edit-src-exit]."
  5431. (interactive)
  5432. (let ((line (org-current-line))
  5433. (case-fold-search t)
  5434. (msg (substitute-command-keys
  5435. "Edit, then exit with C-c ' (C-c and single quote)"))
  5436. (org-mode-p (eq major-mode 'org-mode))
  5437. beg end)
  5438. (beginning-of-line 1)
  5439. (if (looking-at "[ \t]*[^:\n \t]")
  5440. nil
  5441. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5442. (setq beg (point) end beg)
  5443. (save-excursion
  5444. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5445. (setq beg (point-at-bol 2))
  5446. (setq beg (point))))
  5447. (save-excursion
  5448. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5449. (setq end (1- (match-beginning 0)))
  5450. (setq end (point))))
  5451. (goto-line line))
  5452. (if (get-buffer "*Org Edit Picture*")
  5453. (kill-buffer "*Org Edit Picture*"))
  5454. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5455. "*Org Edit Picture*"))
  5456. (narrow-to-region beg end)
  5457. (remove-text-properties beg end '(display nil invisible nil
  5458. intangible nil))
  5459. (when (fboundp 'font-lock-unfontify-region)
  5460. (font-lock-unfontify-region (point-min) (point-max)))
  5461. (cond
  5462. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5463. (fundamental-mode)
  5464. (artist-mode 1))
  5465. (t (funcall org-edit-fixed-width-region-mode)))
  5466. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5467. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5468. (set (make-local-variable 'org-edit-src-picture) t)
  5469. (goto-char (point-min))
  5470. (while (re-search-forward "^[ \t]*: ?" nil t)
  5471. (replace-match ""))
  5472. (goto-line line)
  5473. (org-exit-edit-mode)
  5474. (org-set-local 'header-line-format msg)
  5475. (message "%s" msg)
  5476. t)))
  5477. (defun org-edit-src-find-region-and-lang ()
  5478. "Find the region and language for a local edit.
  5479. Return a list with beginning and end of the region, a string representing
  5480. the language, a switch telling of the content should be in a single line."
  5481. (let ((re-list
  5482. (append
  5483. org-edit-src-region-extra
  5484. '(
  5485. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5486. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5487. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5488. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5489. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5490. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5491. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5492. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5493. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5494. ("^#\\+html:" "\n" "html" single-line)
  5495. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5496. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5497. ("^#\\+latex:" "\n" "latex" single-line)
  5498. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5499. ("^#\\+ascii:" "\n" "ascii" single-line)
  5500. )))
  5501. (pos (point))
  5502. re1 re2 single beg end lang lfmt match-re1)
  5503. (catch 'exit
  5504. (while (setq entry (pop re-list))
  5505. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5506. single (nth 3 entry))
  5507. (save-excursion
  5508. (if (or (looking-at re1)
  5509. (re-search-backward re1 nil t))
  5510. (progn
  5511. (setq match-re1 (match-string 0))
  5512. (setq beg (match-end 0)
  5513. lang (org-edit-src-get-lang lang)
  5514. lfmt (org-edit-src-get-label-format match-re1))
  5515. (if (and (re-search-forward re2 nil t)
  5516. (>= (match-end 0) pos))
  5517. (throw 'exit (list beg (match-beginning 0)
  5518. lang single lfmt))))
  5519. (if (or (looking-at re2)
  5520. (re-search-forward re2 nil t))
  5521. (progn
  5522. (setq end (match-beginning 0))
  5523. (if (and (re-search-backward re1 nil t)
  5524. (<= (match-beginning 0) pos))
  5525. (progn
  5526. (setq lfmt (org-edit-src-get-label-format
  5527. (match-string 0)))
  5528. (throw 'exit
  5529. (list (match-end 0) end
  5530. (org-edit-src-get-lang lang)
  5531. single lfmt))))))))))))
  5532. (defun org-edit-src-get-lang (lang)
  5533. "Extract the src language."
  5534. (let ((m (match-string 0)))
  5535. (cond
  5536. ((stringp lang) lang)
  5537. ((integerp lang) (match-string lang))
  5538. ((and (eq lang 'lang)
  5539. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5540. (match-string 1 m))
  5541. ((and (eq lang 'style)
  5542. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5543. (match-string 1 m))
  5544. (t "fundamental"))))
  5545. (defun org-edit-src-get-label-format (s)
  5546. "Extract the label format."
  5547. (save-match-data
  5548. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5549. (match-string 1 s))))
  5550. (defun org-edit-src-exit ()
  5551. "Exit special edit and protect problematic lines."
  5552. (interactive)
  5553. (unless (buffer-base-buffer (current-buffer))
  5554. (error "This is not an indirect buffer, something is wrong..."))
  5555. (unless (> (point-min) 1)
  5556. (error "This buffer is not narrowed, something is wrong..."))
  5557. (goto-char (point-min))
  5558. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5559. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5560. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5561. (goto-char (point-min))
  5562. (while (re-search-forward "\n" nil t)
  5563. (replace-match " "))
  5564. (goto-char (point-min))
  5565. (if (looking-at "\\s-*") (replace-match " "))
  5566. (if (re-search-forward "\\s-+\\'" nil t)
  5567. (replace-match "")))
  5568. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5569. (goto-char (point-min))
  5570. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5571. (replace-match ",\\1"))
  5572. (when font-lock-mode
  5573. (font-lock-unfontify-region (point-min) (point-max)))
  5574. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5575. (when (org-bound-and-true-p org-edit-src-picture)
  5576. (untabify (point-min) (point-max))
  5577. (goto-char (point-min))
  5578. (while (re-search-forward "^" nil t)
  5579. (replace-match ": "))
  5580. (when font-lock-mode
  5581. (font-lock-unfontify-region (point-min) (point-max)))
  5582. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5583. (kill-buffer (current-buffer))
  5584. (and (org-mode-p) (org-restart-font-lock)))
  5585. ;;; The orgstruct minor mode
  5586. ;; Define a minor mode which can be used in other modes in order to
  5587. ;; integrate the org-mode structure editing commands.
  5588. ;; This is really a hack, because the org-mode structure commands use
  5589. ;; keys which normally belong to the major mode. Here is how it
  5590. ;; works: The minor mode defines all the keys necessary to operate the
  5591. ;; structure commands, but wraps the commands into a function which
  5592. ;; tests if the cursor is currently at a headline or a plain list
  5593. ;; item. If that is the case, the structure command is used,
  5594. ;; temporarily setting many Org-mode variables like regular
  5595. ;; expressions for filling etc. However, when any of those keys is
  5596. ;; used at a different location, function uses `key-binding' to look
  5597. ;; up if the key has an associated command in another currently active
  5598. ;; keymap (minor modes, major mode, global), and executes that
  5599. ;; command. There might be problems if any of the keys is otherwise
  5600. ;; used as a prefix key.
  5601. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5602. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5603. ;; addresses this by checking explicitly for both bindings.
  5604. (defvar orgstruct-mode-map (make-sparse-keymap)
  5605. "Keymap for the minor `orgstruct-mode'.")
  5606. (defvar org-local-vars nil
  5607. "List of local variables, for use by `orgstruct-mode'")
  5608. ;;;###autoload
  5609. (define-minor-mode orgstruct-mode
  5610. "Toggle the minor more `orgstruct-mode'.
  5611. This mode is for using Org-mode structure commands in other modes.
  5612. The following key behave as if Org-mode was active, if the cursor
  5613. is on a headline, or on a plain list item (both in the definition
  5614. of Org-mode).
  5615. M-up Move entry/item up
  5616. M-down Move entry/item down
  5617. M-left Promote
  5618. M-right Demote
  5619. M-S-up Move entry/item up
  5620. M-S-down Move entry/item down
  5621. M-S-left Promote subtree
  5622. M-S-right Demote subtree
  5623. M-q Fill paragraph and items like in Org-mode
  5624. C-c ^ Sort entries
  5625. C-c - Cycle list bullet
  5626. TAB Cycle item visibility
  5627. M-RET Insert new heading/item
  5628. S-M-RET Insert new TODO heading / Checkbox item
  5629. C-c C-c Set tags / toggle checkbox"
  5630. nil " OrgStruct" nil
  5631. (org-load-modules-maybe)
  5632. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5633. ;;;###autoload
  5634. (defun turn-on-orgstruct ()
  5635. "Unconditionally turn on `orgstruct-mode'."
  5636. (orgstruct-mode 1))
  5637. ;;;###autoload
  5638. (defun turn-on-orgstruct++ ()
  5639. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5640. In addition to setting orgstruct-mode, this also exports all indentation and
  5641. autofilling variables from org-mode into the buffer. Note that turning
  5642. off orgstruct-mode will *not* remove these additional settings."
  5643. (orgstruct-mode 1)
  5644. (let (var val)
  5645. (mapc
  5646. (lambda (x)
  5647. (when (string-match
  5648. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5649. (symbol-name (car x)))
  5650. (setq var (car x) val (nth 1 x))
  5651. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5652. org-local-vars)))
  5653. (defun orgstruct-error ()
  5654. "Error when there is no default binding for a structure key."
  5655. (interactive)
  5656. (error "This key has no function outside structure elements"))
  5657. (defun orgstruct-setup ()
  5658. "Setup orgstruct keymaps."
  5659. (let ((nfunc 0)
  5660. (bindings
  5661. (list
  5662. '([(meta up)] org-metaup)
  5663. '([(meta down)] org-metadown)
  5664. '([(meta left)] org-metaleft)
  5665. '([(meta right)] org-metaright)
  5666. '([(meta shift up)] org-shiftmetaup)
  5667. '([(meta shift down)] org-shiftmetadown)
  5668. '([(meta shift left)] org-shiftmetaleft)
  5669. '([(meta shift right)] org-shiftmetaright)
  5670. '([(shift up)] org-shiftup)
  5671. '([(shift down)] org-shiftdown)
  5672. '([(shift left)] org-shiftleft)
  5673. '([(shift right)] org-shiftright)
  5674. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5675. '("\M-q" fill-paragraph)
  5676. '("\C-c^" org-sort)
  5677. '("\C-c-" org-cycle-list-bullet)))
  5678. elt key fun cmd)
  5679. (while (setq elt (pop bindings))
  5680. (setq nfunc (1+ nfunc))
  5681. (setq key (org-key (car elt))
  5682. fun (nth 1 elt)
  5683. cmd (orgstruct-make-binding fun nfunc key))
  5684. (org-defkey orgstruct-mode-map key cmd))
  5685. ;; Special treatment needed for TAB and RET
  5686. (org-defkey orgstruct-mode-map [(tab)]
  5687. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5688. (org-defkey orgstruct-mode-map "\C-i"
  5689. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5690. (org-defkey orgstruct-mode-map "\M-\C-m"
  5691. (orgstruct-make-binding 'org-insert-heading 105
  5692. "\M-\C-m" [(meta return)]))
  5693. (org-defkey orgstruct-mode-map [(meta return)]
  5694. (orgstruct-make-binding 'org-insert-heading 106
  5695. [(meta return)] "\M-\C-m"))
  5696. (org-defkey orgstruct-mode-map [(shift meta return)]
  5697. (orgstruct-make-binding 'org-insert-todo-heading 107
  5698. [(meta return)] "\M-\C-m"))
  5699. (unless org-local-vars
  5700. (setq org-local-vars (org-get-local-variables)))
  5701. t))
  5702. (defun orgstruct-make-binding (fun n &rest keys)
  5703. "Create a function for binding in the structure minor mode.
  5704. FUN is the command to call inside a table. N is used to create a unique
  5705. command name. KEYS are keys that should be checked in for a command
  5706. to execute outside of tables."
  5707. (eval
  5708. (list 'defun
  5709. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5710. '(arg)
  5711. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5712. "Outside of structure, run the binding of `"
  5713. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5714. "'.")
  5715. '(interactive "p")
  5716. (list 'if
  5717. '(org-context-p 'headline 'item)
  5718. (list 'org-run-like-in-org-mode (list 'quote fun))
  5719. (list 'let '(orgstruct-mode)
  5720. (list 'call-interactively
  5721. (append '(or)
  5722. (mapcar (lambda (k)
  5723. (list 'key-binding k))
  5724. keys)
  5725. '('orgstruct-error))))))))
  5726. (defun org-context-p (&rest contexts)
  5727. "Check if local context is any of CONTEXTS.
  5728. Possible values in the list of contexts are `table', `headline', and `item'."
  5729. (let ((pos (point)))
  5730. (goto-char (point-at-bol))
  5731. (prog1 (or (and (memq 'table contexts)
  5732. (looking-at "[ \t]*|"))
  5733. (and (memq 'headline contexts)
  5734. ;;????????? (looking-at "\\*+"))
  5735. (looking-at outline-regexp))
  5736. (and (memq 'item contexts)
  5737. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5738. (goto-char pos))))
  5739. (defun org-get-local-variables ()
  5740. "Return a list of all local variables in an org-mode buffer."
  5741. (let (varlist)
  5742. (with-current-buffer (get-buffer-create "*Org tmp*")
  5743. (erase-buffer)
  5744. (org-mode)
  5745. (setq varlist (buffer-local-variables)))
  5746. (kill-buffer "*Org tmp*")
  5747. (delq nil
  5748. (mapcar
  5749. (lambda (x)
  5750. (setq x
  5751. (if (symbolp x)
  5752. (list x)
  5753. (list (car x) (list 'quote (cdr x)))))
  5754. (if (string-match
  5755. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5756. (symbol-name (car x)))
  5757. x nil))
  5758. varlist))))
  5759. ;;;###autoload
  5760. (defun org-run-like-in-org-mode (cmd)
  5761. (org-load-modules-maybe)
  5762. (unless org-local-vars
  5763. (setq org-local-vars (org-get-local-variables)))
  5764. (eval (list 'let org-local-vars
  5765. (list 'call-interactively (list 'quote cmd)))))
  5766. ;;;; Archiving
  5767. (defun org-get-category (&optional pos)
  5768. "Get the category applying to position POS."
  5769. (get-text-property (or pos (point)) 'org-category))
  5770. (defun org-refresh-category-properties ()
  5771. "Refresh category text properties in the buffer."
  5772. (let ((def-cat (cond
  5773. ((null org-category)
  5774. (if buffer-file-name
  5775. (file-name-sans-extension
  5776. (file-name-nondirectory buffer-file-name))
  5777. "???"))
  5778. ((symbolp org-category) (symbol-name org-category))
  5779. (t org-category)))
  5780. beg end cat pos optionp)
  5781. (org-unmodified
  5782. (save-excursion
  5783. (save-restriction
  5784. (widen)
  5785. (goto-char (point-min))
  5786. (put-text-property (point) (point-max) 'org-category def-cat)
  5787. (while (re-search-forward
  5788. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5789. (setq pos (match-end 0)
  5790. optionp (equal (char-after (match-beginning 0)) ?#)
  5791. cat (org-trim (match-string 2)))
  5792. (if optionp
  5793. (setq beg (point-at-bol) end (point-max))
  5794. (org-back-to-heading t)
  5795. (setq beg (point) end (org-end-of-subtree t t)))
  5796. (put-text-property beg end 'org-category cat)
  5797. (goto-char pos)))))))
  5798. ;;;; Link Stuff
  5799. ;;; Link abbreviations
  5800. (defun org-link-expand-abbrev (link)
  5801. "Apply replacements as defined in `org-link-abbrev-alist."
  5802. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5803. (let* ((key (match-string 1 link))
  5804. (as (or (assoc key org-link-abbrev-alist-local)
  5805. (assoc key org-link-abbrev-alist)))
  5806. (tag (and (match-end 2) (match-string 3 link)))
  5807. rpl)
  5808. (if (not as)
  5809. link
  5810. (setq rpl (cdr as))
  5811. (cond
  5812. ((symbolp rpl) (funcall rpl tag))
  5813. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5814. ((string-match "%h" rpl)
  5815. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5816. (t (concat rpl tag)))))
  5817. link))
  5818. ;;; Storing and inserting links
  5819. (defvar org-insert-link-history nil
  5820. "Minibuffer history for links inserted with `org-insert-link'.")
  5821. (defvar org-stored-links nil
  5822. "Contains the links stored with `org-store-link'.")
  5823. (defvar org-store-link-plist nil
  5824. "Plist with info about the most recently link created with `org-store-link'.")
  5825. (defvar org-link-protocols nil
  5826. "Link protocols added to Org-mode using `org-add-link-type'.")
  5827. (defvar org-store-link-functions nil
  5828. "List of functions that are called to create and store a link.
  5829. Each function will be called in turn until one returns a non-nil
  5830. value. Each function should check if it is responsible for creating
  5831. this link (for example by looking at the major mode).
  5832. If not, it must exit and return nil.
  5833. If yes, it should return a non-nil value after a calling
  5834. `org-store-link-props' with a list of properties and values.
  5835. Special properties are:
  5836. :type The link prefix. like \"http\". This must be given.
  5837. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5838. This is obligatory as well.
  5839. :description Optional default description for the second pair
  5840. of brackets in an Org-mode link. The user can still change
  5841. this when inserting this link into an Org-mode buffer.
  5842. In addition to these, any additional properties can be specified
  5843. and then used in remember templates.")
  5844. (defun org-add-link-type (type &optional follow export)
  5845. "Add TYPE to the list of `org-link-types'.
  5846. Re-compute all regular expressions depending on `org-link-types'
  5847. FOLLOW and EXPORT are two functions.
  5848. FOLLOW should take the link path as the single argument and do whatever
  5849. is necessary to follow the link, for example find a file or display
  5850. a mail message.
  5851. EXPORT should format the link path for export to one of the export formats.
  5852. It should be a function accepting three arguments:
  5853. path the path of the link, the text after the prefix (like \"http:\")
  5854. desc the description of the link, if any, nil if there was no description
  5855. format the export format, a symbol like `html' or `latex'.
  5856. The function may use the FORMAT information to return different values
  5857. depending on the format. The return value will be put literally into
  5858. the exported file.
  5859. Org-mode has a built-in default for exporting links. If you are happy with
  5860. this default, there is no need to define an export function for the link
  5861. type. For a simple example of an export function, see `org-bbdb.el'."
  5862. (add-to-list 'org-link-types type t)
  5863. (org-make-link-regexps)
  5864. (if (assoc type org-link-protocols)
  5865. (setcdr (assoc type org-link-protocols) (list follow export))
  5866. (push (list type follow export) org-link-protocols)))
  5867. ;;;###autoload
  5868. (defun org-store-link (arg)
  5869. "\\<org-mode-map>Store an org-link to the current location.
  5870. This link is added to `org-stored-links' and can later be inserted
  5871. into an org-buffer with \\[org-insert-link].
  5872. For some link types, a prefix arg is interpreted:
  5873. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5874. For file links, arg negates `org-context-in-file-links'."
  5875. (interactive "P")
  5876. (org-load-modules-maybe)
  5877. (setq org-store-link-plist nil) ; reset
  5878. (let (link cpltxt desc description search txt)
  5879. (cond
  5880. ((run-hook-with-args-until-success 'org-store-link-functions)
  5881. (setq link (plist-get org-store-link-plist :link)
  5882. desc (or (plist-get org-store-link-plist :description) link)))
  5883. ((equal (buffer-name) "*Org Edit Src Example*")
  5884. (let (label gc)
  5885. (while (or (not label)
  5886. (save-excursion
  5887. (save-restriction
  5888. (widen)
  5889. (goto-char (point-min))
  5890. (re-search-forward
  5891. (regexp-quote (format org-coderef-label-format label))
  5892. nil t))))
  5893. (when label (message "Label exists already") (sit-for 2))
  5894. (setq label (read-string "Code line label: " label)))
  5895. (end-of-line 1)
  5896. (setq link (format org-coderef-label-format label))
  5897. (setq gc (- 79 (length link)))
  5898. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5899. (insert link)
  5900. (setq link (concat "(" label ")") desc nil)))
  5901. ((eq major-mode 'calendar-mode)
  5902. (let ((cd (calendar-cursor-to-date)))
  5903. (setq link
  5904. (format-time-string
  5905. (car org-time-stamp-formats)
  5906. (apply 'encode-time
  5907. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5908. nil nil nil))))
  5909. (org-store-link-props :type "calendar" :date cd)))
  5910. ((eq major-mode 'w3-mode)
  5911. (setq cpltxt (url-view-url t)
  5912. link (org-make-link cpltxt))
  5913. (org-store-link-props :type "w3" :url (url-view-url t)))
  5914. ((eq major-mode 'w3m-mode)
  5915. (setq cpltxt (or w3m-current-title w3m-current-url)
  5916. link (org-make-link w3m-current-url))
  5917. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5918. ((setq search (run-hook-with-args-until-success
  5919. 'org-create-file-search-functions))
  5920. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5921. "::" search))
  5922. (setq cpltxt (or description link)))
  5923. ((eq major-mode 'image-mode)
  5924. (setq cpltxt (concat "file:"
  5925. (abbreviate-file-name buffer-file-name))
  5926. link (org-make-link cpltxt))
  5927. (org-store-link-props :type "image" :file buffer-file-name))
  5928. ((eq major-mode 'dired-mode)
  5929. ;; link to the file in the current line
  5930. (setq cpltxt (concat "file:"
  5931. (abbreviate-file-name
  5932. (expand-file-name
  5933. (dired-get-filename nil t))))
  5934. link (org-make-link cpltxt)))
  5935. ((and buffer-file-name (org-mode-p))
  5936. (cond
  5937. ((org-in-regexp "<<\\(.*?\\)>>")
  5938. (setq cpltxt
  5939. (concat "file:"
  5940. (abbreviate-file-name buffer-file-name)
  5941. "::" (match-string 1))
  5942. link (org-make-link cpltxt)))
  5943. ((and (featurep 'org-id)
  5944. (or (eq org-link-to-org-use-id t)
  5945. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5946. (interactive-p))
  5947. (and org-link-to-org-use-id
  5948. (condition-case nil
  5949. (org-entry-get nil "ID")
  5950. (error nil)))))
  5951. ;; We can make a link using the ID.
  5952. (setq link (condition-case nil
  5953. (prog1 (org-id-store-link)
  5954. (setq desc (plist-get org-store-link-plist
  5955. :description)))
  5956. (error
  5957. ;; probably before first headline, link to file only
  5958. (concat "file:"
  5959. (abbreviate-file-name buffer-file-name))))))
  5960. (t
  5961. ;; Just link to current headline
  5962. (setq cpltxt (concat "file:"
  5963. (abbreviate-file-name buffer-file-name)))
  5964. ;; Add a context search string
  5965. (when (org-xor org-context-in-file-links arg)
  5966. (setq txt (cond
  5967. ((org-on-heading-p) nil)
  5968. ((org-region-active-p)
  5969. (buffer-substring (region-beginning) (region-end)))
  5970. (t nil)))
  5971. (when (or (null txt) (string-match "\\S-" txt))
  5972. (setq cpltxt
  5973. (concat cpltxt "::"
  5974. (condition-case nil
  5975. (org-make-org-heading-search-string txt)
  5976. (error "")))
  5977. desc "NONE")))
  5978. (if (string-match "::\\'" cpltxt)
  5979. (setq cpltxt (substring cpltxt 0 -2)))
  5980. (setq link (org-make-link cpltxt)))))
  5981. ((buffer-file-name (buffer-base-buffer))
  5982. ;; Just link to this file here.
  5983. (setq cpltxt (concat "file:"
  5984. (abbreviate-file-name
  5985. (buffer-file-name (buffer-base-buffer)))))
  5986. ;; Add a context string
  5987. (when (org-xor org-context-in-file-links arg)
  5988. (setq txt (if (org-region-active-p)
  5989. (buffer-substring (region-beginning) (region-end))
  5990. (buffer-substring (point-at-bol) (point-at-eol))))
  5991. ;; Only use search option if there is some text.
  5992. (when (string-match "\\S-" txt)
  5993. (setq cpltxt
  5994. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5995. desc "NONE")))
  5996. (setq link (org-make-link cpltxt)))
  5997. ((interactive-p)
  5998. (error "Cannot link to a buffer which is not visiting a file"))
  5999. (t (setq link nil)))
  6000. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6001. (setq link (or link cpltxt)
  6002. desc (or desc cpltxt))
  6003. (if (equal desc "NONE") (setq desc nil))
  6004. (if (and (interactive-p) link)
  6005. (progn
  6006. (setq org-stored-links
  6007. (cons (list link desc) org-stored-links))
  6008. (message "Stored: %s" (or desc link)))
  6009. (and link (org-make-link-string link desc)))))
  6010. (defun org-store-link-props (&rest plist)
  6011. "Store link properties, extract names and addresses."
  6012. (let (x adr)
  6013. (when (setq x (plist-get plist :from))
  6014. (setq adr (mail-extract-address-components x))
  6015. (setq plist (plist-put plist :fromname (car adr)))
  6016. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6017. (when (setq x (plist-get plist :to))
  6018. (setq adr (mail-extract-address-components x))
  6019. (setq plist (plist-put plist :toname (car adr)))
  6020. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6021. (let ((from (plist-get plist :from))
  6022. (to (plist-get plist :to)))
  6023. (when (and from to org-from-is-user-regexp)
  6024. (setq plist
  6025. (plist-put plist :fromto
  6026. (if (string-match org-from-is-user-regexp from)
  6027. (concat "to %t")
  6028. (concat "from %f"))))))
  6029. (setq org-store-link-plist plist))
  6030. (defun org-add-link-props (&rest plist)
  6031. "Add these properties to the link property list."
  6032. (let (key value)
  6033. (while plist
  6034. (setq key (pop plist) value (pop plist))
  6035. (setq org-store-link-plist
  6036. (plist-put org-store-link-plist key value)))))
  6037. (defun org-email-link-description (&optional fmt)
  6038. "Return the description part of an email link.
  6039. This takes information from `org-store-link-plist' and formats it
  6040. according to FMT (default from `org-email-link-description-format')."
  6041. (setq fmt (or fmt org-email-link-description-format))
  6042. (let* ((p org-store-link-plist)
  6043. (to (plist-get p :toaddress))
  6044. (from (plist-get p :fromaddress))
  6045. (table
  6046. (list
  6047. (cons "%c" (plist-get p :fromto))
  6048. (cons "%F" (plist-get p :from))
  6049. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6050. (cons "%T" (plist-get p :to))
  6051. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6052. (cons "%s" (plist-get p :subject))
  6053. (cons "%m" (plist-get p :message-id)))))
  6054. (when (string-match "%c" fmt)
  6055. ;; Check if the user wrote this message
  6056. (if (and org-from-is-user-regexp from to
  6057. (save-match-data (string-match org-from-is-user-regexp from)))
  6058. (setq fmt (replace-match "to %t" t t fmt))
  6059. (setq fmt (replace-match "from %f" t t fmt))))
  6060. (org-replace-escapes fmt table)))
  6061. (defun org-make-org-heading-search-string (&optional string heading)
  6062. "Make search string for STRING or current headline."
  6063. (interactive)
  6064. (let ((s (or string (org-get-heading))))
  6065. (unless (and string (not heading))
  6066. ;; We are using a headline, clean up garbage in there.
  6067. (if (string-match org-todo-regexp s)
  6068. (setq s (replace-match "" t t s)))
  6069. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6070. (setq s (replace-match "" t t s)))
  6071. (setq s (org-trim s))
  6072. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6073. org-comment-string "\\)") s)
  6074. (setq s (replace-match "" t t s)))
  6075. (while (string-match org-ts-regexp s)
  6076. (setq s (replace-match "" t t s))))
  6077. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6078. (setq s (replace-match " " t t s)))
  6079. (or string (setq s (concat "*" s))) ; Add * for headlines
  6080. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6081. (defun org-make-link (&rest strings)
  6082. "Concatenate STRINGS."
  6083. (apply 'concat strings))
  6084. (defun org-make-link-string (link &optional description)
  6085. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6086. (unless (string-match "\\S-" link)
  6087. (error "Empty link"))
  6088. (when (stringp description)
  6089. ;; Remove brackets from the description, they are fatal.
  6090. (while (string-match "\\[" description)
  6091. (setq description (replace-match "{" t t description)))
  6092. (while (string-match "\\]" description)
  6093. (setq description (replace-match "}" t t description))))
  6094. (when (equal (org-link-escape link) description)
  6095. ;; No description needed, it is identical
  6096. (setq description nil))
  6097. (when (and (not description)
  6098. (not (equal link (org-link-escape link))))
  6099. (setq description (org-extract-attributes link)))
  6100. (concat "[[" (org-link-escape link) "]"
  6101. (if description (concat "[" description "]") "")
  6102. "]"))
  6103. (defconst org-link-escape-chars
  6104. '((?\ . "%20")
  6105. (?\[ . "%5B")
  6106. (?\] . "%5D")
  6107. (?\340 . "%E0") ; `a
  6108. (?\342 . "%E2") ; ^a
  6109. (?\347 . "%E7") ; ,c
  6110. (?\350 . "%E8") ; `e
  6111. (?\351 . "%E9") ; 'e
  6112. (?\352 . "%EA") ; ^e
  6113. (?\356 . "%EE") ; ^i
  6114. (?\364 . "%F4") ; ^o
  6115. (?\371 . "%F9") ; `u
  6116. (?\373 . "%FB") ; ^u
  6117. (?\; . "%3B")
  6118. (?? . "%3F")
  6119. (?= . "%3D")
  6120. (?+ . "%2B")
  6121. )
  6122. "Association list of escapes for some characters problematic in links.
  6123. This is the list that is used for internal purposes.")
  6124. (defconst org-link-escape-chars-browser
  6125. '((?\ . "%20")) ; 32 for the SPC char
  6126. "Association list of escapes for some characters problematic in links.
  6127. This is the list that is used before handing over to the browser.")
  6128. (defun org-link-escape (text &optional table)
  6129. "Escape characters in TEXT that are problematic for links."
  6130. (setq table (or table org-link-escape-chars))
  6131. (when text
  6132. (let ((re (mapconcat (lambda (x) (regexp-quote
  6133. (char-to-string (car x))))
  6134. table "\\|")))
  6135. (while (string-match re text)
  6136. (setq text
  6137. (replace-match
  6138. (cdr (assoc (string-to-char (match-string 0 text))
  6139. table))
  6140. t t text)))
  6141. text)))
  6142. (defun org-link-unescape (text &optional table)
  6143. "Reverse the action of `org-link-escape'."
  6144. (setq table (or table org-link-escape-chars))
  6145. (when text
  6146. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6147. table "\\|")))
  6148. (while (string-match re text)
  6149. (setq text
  6150. (replace-match
  6151. (char-to-string (car (rassoc (match-string 0 text) table)))
  6152. t t text)))
  6153. text)))
  6154. (defun org-xor (a b)
  6155. "Exclusive or."
  6156. (if a (not b) b))
  6157. (defun org-fixup-message-id-for-http (s)
  6158. "Replace special characters in a message id, so it can be used in an http query."
  6159. (while (string-match "<" s)
  6160. (setq s (replace-match "%3C" t t s)))
  6161. (while (string-match ">" s)
  6162. (setq s (replace-match "%3E" t t s)))
  6163. (while (string-match "@" s)
  6164. (setq s (replace-match "%40" t t s)))
  6165. s)
  6166. ;;;###autoload
  6167. (defun org-insert-link-global ()
  6168. "Insert a link like Org-mode does.
  6169. This command can be called in any mode to insert a link in Org-mode syntax."
  6170. (interactive)
  6171. (org-load-modules-maybe)
  6172. (org-run-like-in-org-mode 'org-insert-link))
  6173. (defun org-insert-link (&optional complete-file link-location)
  6174. "Insert a link. At the prompt, enter the link.
  6175. Completion can be used to insert any of the link protocol prefixes like
  6176. http or ftp in use.
  6177. The history can be used to select a link previously stored with
  6178. `org-store-link'. When the empty string is entered (i.e. if you just
  6179. press RET at the prompt), the link defaults to the most recently
  6180. stored link. As SPC triggers completion in the minibuffer, you need to
  6181. use M-SPC or C-q SPC to force the insertion of a space character.
  6182. You will also be prompted for a description, and if one is given, it will
  6183. be displayed in the buffer instead of the link.
  6184. If there is already a link at point, this command will allow you to edit link
  6185. and description parts.
  6186. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6187. be selected using completion. The path to the file will be relative to the
  6188. current directory if the file is in the current directory or a subdirectory.
  6189. Otherwise, the link will be the absolute path as completed in the minibuffer
  6190. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6191. option `org-link-file-path-type'.
  6192. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6193. the current directory or below.
  6194. With three \\[universal-argument] prefixes, negate the meaning of
  6195. `org-keep-stored-link-after-insertion'.
  6196. If `org-make-link-description-function' is non-nil, this function will be
  6197. called with the link target, and the result will be the default
  6198. link description.
  6199. If the LINK-LOCATION parameter is non-nil, this value will be
  6200. used as the link location instead of reading one interactively."
  6201. (interactive "P")
  6202. (let* ((wcf (current-window-configuration))
  6203. (region (if (org-region-active-p)
  6204. (buffer-substring (region-beginning) (region-end))))
  6205. (remove (and region (list (region-beginning) (region-end))))
  6206. (desc region)
  6207. tmphist ; byte-compile incorrectly complains about this
  6208. (link link-location)
  6209. entry file)
  6210. (cond
  6211. (link-location) ; specified by arg, just use it.
  6212. ((org-in-regexp org-bracket-link-regexp 1)
  6213. ;; We do have a link at point, and we are going to edit it.
  6214. (setq remove (list (match-beginning 0) (match-end 0)))
  6215. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6216. (setq link (read-string "Link: "
  6217. (org-link-unescape
  6218. (org-match-string-no-properties 1)))))
  6219. ((or (org-in-regexp org-angle-link-re)
  6220. (org-in-regexp org-plain-link-re))
  6221. ;; Convert to bracket link
  6222. (setq remove (list (match-beginning 0) (match-end 0))
  6223. link (read-string "Link: "
  6224. (org-remove-angle-brackets (match-string 0)))))
  6225. ((member complete-file '((4) (16)))
  6226. ;; Completing read for file names.
  6227. (setq file (read-file-name "File: "))
  6228. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6229. (pwd1 (file-name-as-directory (abbreviate-file-name
  6230. (expand-file-name ".")))))
  6231. (cond
  6232. ((equal complete-file '(16))
  6233. (setq link (org-make-link
  6234. "file:"
  6235. (abbreviate-file-name (expand-file-name file)))))
  6236. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6237. (setq link (org-make-link "file:" (match-string 1 file))))
  6238. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6239. (expand-file-name file))
  6240. (setq link (org-make-link
  6241. "file:" (match-string 1 (expand-file-name file)))))
  6242. (t (setq link (org-make-link "file:" file))))))
  6243. (t
  6244. ;; Read link, with completion for stored links.
  6245. (with-output-to-temp-buffer "*Org Links*"
  6246. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6247. (when org-stored-links
  6248. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6249. (princ (mapconcat
  6250. (lambda (x)
  6251. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6252. (reverse org-stored-links) "\n"))))
  6253. (let ((cw (selected-window)))
  6254. (select-window (get-buffer-window "*Org Links*"))
  6255. (setq truncate-lines t)
  6256. (org-fit-window-to-buffer)
  6257. (select-window cw))
  6258. ;; Fake a link history, containing the stored links.
  6259. (setq tmphist (append (mapcar 'car org-stored-links)
  6260. org-insert-link-history))
  6261. (unwind-protect
  6262. (setq link
  6263. (let ((org-completion-use-ido nil))
  6264. (org-completing-read
  6265. "Link: "
  6266. (append
  6267. (mapcar (lambda (x) (list (concat (car x) ":")))
  6268. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6269. (mapcar (lambda (x) (list (concat x ":")))
  6270. org-link-types))
  6271. nil nil nil
  6272. 'tmphist
  6273. (or (car (car org-stored-links))))))
  6274. (set-window-configuration wcf)
  6275. (kill-buffer "*Org Links*"))
  6276. (setq entry (assoc link org-stored-links))
  6277. (or entry (push link org-insert-link-history))
  6278. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6279. (not org-keep-stored-link-after-insertion))
  6280. (setq org-stored-links (delq (assoc link org-stored-links)
  6281. org-stored-links)))
  6282. (setq desc (or desc (nth 1 entry)))))
  6283. (if (string-match org-plain-link-re link)
  6284. ;; URL-like link, normalize the use of angular brackets.
  6285. (setq link (org-make-link (org-remove-angle-brackets link))))
  6286. ;; Check if we are linking to the current file with a search option
  6287. ;; If yes, simplify the link by using only the search option.
  6288. (when (and buffer-file-name
  6289. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6290. (let* ((path (match-string 1 link))
  6291. (case-fold-search nil)
  6292. (search (match-string 2 link)))
  6293. (save-match-data
  6294. (if (equal (file-truename buffer-file-name) (file-truename path))
  6295. ;; We are linking to this same file, with a search option
  6296. (setq link search)))))
  6297. ;; Check if we can/should use a relative path. If yes, simplify the link
  6298. (when (string-match "^file:\\(.*\\)" link)
  6299. (let* ((path (match-string 1 link))
  6300. (origpath path)
  6301. (case-fold-search nil))
  6302. (cond
  6303. ((or (eq org-link-file-path-type 'absolute)
  6304. (equal complete-file '(16)))
  6305. (setq path (abbreviate-file-name (expand-file-name path))))
  6306. ((eq org-link-file-path-type 'noabbrev)
  6307. (setq path (expand-file-name path)))
  6308. ((eq org-link-file-path-type 'relative)
  6309. (setq path (file-relative-name path)))
  6310. (t
  6311. (save-match-data
  6312. (if (string-match (concat "^" (regexp-quote
  6313. (file-name-as-directory
  6314. (expand-file-name "."))))
  6315. (expand-file-name path))
  6316. ;; We are linking a file with relative path name.
  6317. (setq path (substring (expand-file-name path)
  6318. (match-end 0)))
  6319. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6320. (setq link (concat "file:" path))
  6321. (if (equal desc origpath)
  6322. (setq desc path))))
  6323. (if org-make-link-description-function
  6324. (setq desc (funcall org-make-link-description-function link desc)))
  6325. (setq desc (read-string "Description: " desc))
  6326. (unless (string-match "\\S-" desc) (setq desc nil))
  6327. (if remove (apply 'delete-region remove))
  6328. (insert (org-make-link-string link desc))))
  6329. (defun org-completing-read (&rest args)
  6330. "Completing-read with SPACE being a normal character."
  6331. (let ((minibuffer-local-completion-map
  6332. (copy-keymap minibuffer-local-completion-map)))
  6333. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6334. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6335. (apply 'org-ido-completing-read args)))
  6336. (defun org-ido-completing-read (&rest args)
  6337. "Completing-read using `ido-mode' speedups if available"
  6338. (if (and org-completion-use-ido
  6339. (fboundp 'ido-completing-read)
  6340. (boundp 'ido-mode) ido-mode
  6341. (listp (second args)))
  6342. (let ((ido-enter-matching-directory nil))
  6343. (apply 'ido-completing-read (concat (car args))
  6344. (mapcar (lambda (x) (car x)) (nth 1 args))
  6345. (cddr args)))
  6346. (apply 'completing-read args)))
  6347. (defun org-extract-attributes (s)
  6348. "Extract the attributes cookie from a string and set as text property."
  6349. (let (a attr (start 0) key value)
  6350. (save-match-data
  6351. (when (string-match "{{\\([^}]+\\)}}$" s)
  6352. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6353. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6354. (setq key (match-string 1 a) value (match-string 2 a)
  6355. start (match-end 0)
  6356. attr (plist-put attr (intern key) value))))
  6357. (org-add-props s nil 'org-attr attr))
  6358. s))
  6359. (defun org-attributes-to-string (plist)
  6360. "Format a property list into an HTML attribute list."
  6361. (let ((s "") key value)
  6362. (while plist
  6363. (setq key (pop plist) value (pop plist))
  6364. (and value
  6365. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6366. s))
  6367. ;;; Opening/following a link
  6368. (defvar org-link-search-failed nil)
  6369. (defun org-next-link ()
  6370. "Move forward to the next link.
  6371. If the link is in hidden text, expose it."
  6372. (interactive)
  6373. (when (and org-link-search-failed (eq this-command last-command))
  6374. (goto-char (point-min))
  6375. (message "Link search wrapped back to beginning of buffer"))
  6376. (setq org-link-search-failed nil)
  6377. (let* ((pos (point))
  6378. (ct (org-context))
  6379. (a (assoc :link ct)))
  6380. (if a (goto-char (nth 2 a)))
  6381. (if (re-search-forward org-any-link-re nil t)
  6382. (progn
  6383. (goto-char (match-beginning 0))
  6384. (if (org-invisible-p) (org-show-context)))
  6385. (goto-char pos)
  6386. (setq org-link-search-failed t)
  6387. (error "No further link found"))))
  6388. (defun org-previous-link ()
  6389. "Move backward to the previous link.
  6390. If the link is in hidden text, expose it."
  6391. (interactive)
  6392. (when (and org-link-search-failed (eq this-command last-command))
  6393. (goto-char (point-max))
  6394. (message "Link search wrapped back to end of buffer"))
  6395. (setq org-link-search-failed nil)
  6396. (let* ((pos (point))
  6397. (ct (org-context))
  6398. (a (assoc :link ct)))
  6399. (if a (goto-char (nth 1 a)))
  6400. (if (re-search-backward org-any-link-re nil t)
  6401. (progn
  6402. (goto-char (match-beginning 0))
  6403. (if (org-invisible-p) (org-show-context)))
  6404. (goto-char pos)
  6405. (setq org-link-search-failed t)
  6406. (error "No further link found"))))
  6407. (defun org-translate-link (s)
  6408. "Translate a link string if a translation function has been defined."
  6409. (if (and org-link-translation-function
  6410. (fboundp org-link-translation-function)
  6411. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6412. (progn
  6413. (setq s (funcall org-link-translation-function
  6414. (match-string 1) (match-string 2)))
  6415. (concat (car s) ":" (cdr s)))
  6416. s))
  6417. (defun org-translate-link-from-planner (type path)
  6418. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6419. This is still an experimental function, your mileage may vary."
  6420. (cond
  6421. ((member type '("http" "https" "news" "ftp"))
  6422. ;; standard Internet links are the same.
  6423. nil)
  6424. ((and (equal type "irc") (string-match "^//" path))
  6425. ;; Planner has two / at the beginning of an irc link, we have 1.
  6426. ;; We should have zero, actually....
  6427. (setq path (substring path 1)))
  6428. ((and (equal type "lisp") (string-match "^/" path))
  6429. ;; Planner has a slash, we do not.
  6430. (setq type "elisp" path (substring path 1)))
  6431. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6432. ;; A typical message link. Planner has the id after the fina slash,
  6433. ;; we separate it with a hash mark
  6434. (setq path (concat (match-string 1 path) "#"
  6435. (org-remove-angle-brackets (match-string 2 path)))))
  6436. )
  6437. (cons type path))
  6438. (defun org-find-file-at-mouse (ev)
  6439. "Open file link or URL at mouse."
  6440. (interactive "e")
  6441. (mouse-set-point ev)
  6442. (org-open-at-point 'in-emacs))
  6443. (defun org-open-at-mouse (ev)
  6444. "Open file link or URL at mouse."
  6445. (interactive "e")
  6446. (mouse-set-point ev)
  6447. (if (eq major-mode 'org-agenda-mode)
  6448. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6449. (org-open-at-point))
  6450. (defvar org-window-config-before-follow-link nil
  6451. "The window configuration before following a link.
  6452. This is saved in case the need arises to restore it.")
  6453. (defvar org-open-link-marker (make-marker)
  6454. "Marker pointing to the location where `org-open-at-point; was called.")
  6455. ;;;###autoload
  6456. (defun org-open-at-point-global ()
  6457. "Follow a link like Org-mode does.
  6458. This command can be called in any mode to follow a link that has
  6459. Org-mode syntax."
  6460. (interactive)
  6461. (org-run-like-in-org-mode 'org-open-at-point))
  6462. ;;;###autoload
  6463. (defun org-open-link-from-string (s &optional arg)
  6464. "Open a link in the string S, as if it was in Org-mode."
  6465. (interactive "sLink: \nP")
  6466. (with-temp-buffer
  6467. (let ((org-inhibit-startup t))
  6468. (org-mode)
  6469. (insert s)
  6470. (goto-char (point-min))
  6471. (org-open-at-point arg))))
  6472. (defun org-open-at-point (&optional in-emacs)
  6473. "Open link at or after point.
  6474. If there is no link at point, this function will search forward up to
  6475. the end of the current subtree.
  6476. Normally, files will be opened by an appropriate application. If the
  6477. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6478. With a double prefix argument, try to open outside of Emacs, in the
  6479. application the system uses for this file type."
  6480. (interactive "P")
  6481. (org-load-modules-maybe)
  6482. (move-marker org-open-link-marker (point))
  6483. (setq org-window-config-before-follow-link (current-window-configuration))
  6484. (org-remove-occur-highlights nil nil t)
  6485. (cond
  6486. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6487. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6488. (org-footnote-action))
  6489. (t
  6490. (let (type path link line search (pos (point)))
  6491. (catch 'match
  6492. (save-excursion
  6493. (skip-chars-forward "^]\n\r")
  6494. (when (org-in-regexp org-bracket-link-regexp)
  6495. (setq link (org-extract-attributes
  6496. (org-link-unescape (org-match-string-no-properties 1))))
  6497. (while (string-match " *\n *" link)
  6498. (setq link (replace-match " " t t link)))
  6499. (setq link (org-link-expand-abbrev link))
  6500. (cond
  6501. ((or (file-name-absolute-p link)
  6502. (string-match "^\\.\\.?/" link))
  6503. (setq type "file" path link))
  6504. ((string-match org-link-re-with-space3 link)
  6505. (setq type (match-string 1 link) path (match-string 2 link)))
  6506. (t (setq type "thisfile" path link)))
  6507. (throw 'match t)))
  6508. (when (get-text-property (point) 'org-linked-text)
  6509. (setq type "thisfile"
  6510. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6511. (1+ (point)) (point))
  6512. path (buffer-substring
  6513. (previous-single-property-change pos 'org-linked-text)
  6514. (next-single-property-change pos 'org-linked-text)))
  6515. (throw 'match t))
  6516. (save-excursion
  6517. (when (or (org-in-regexp org-angle-link-re)
  6518. (org-in-regexp org-plain-link-re))
  6519. (setq type (match-string 1) path (match-string 2))
  6520. (throw 'match t)))
  6521. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6522. (setq type "tree-match"
  6523. path (match-string 1))
  6524. (throw 'match t))
  6525. (save-excursion
  6526. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6527. (setq type "tags"
  6528. path (match-string 1))
  6529. (while (string-match ":" path)
  6530. (setq path (replace-match "+" t t path)))
  6531. (throw 'match t))))
  6532. (unless path
  6533. (error "No link found"))
  6534. ;; Remove any trailing spaces in path
  6535. (if (string-match " +\\'" path)
  6536. (setq path (replace-match "" t t path)))
  6537. (if (and org-link-translation-function
  6538. (fboundp org-link-translation-function))
  6539. ;; Check if we need to translate the link
  6540. (let ((tmp (funcall org-link-translation-function type path)))
  6541. (setq type (car tmp) path (cdr tmp))))
  6542. (cond
  6543. ((assoc type org-link-protocols)
  6544. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6545. ((equal type "mailto")
  6546. (let ((cmd (car org-link-mailto-program))
  6547. (args (cdr org-link-mailto-program)) args1
  6548. (address path) (subject "") a)
  6549. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6550. (setq address (match-string 1 path)
  6551. subject (org-link-escape (match-string 2 path))))
  6552. (while args
  6553. (cond
  6554. ((not (stringp (car args))) (push (pop args) args1))
  6555. (t (setq a (pop args))
  6556. (if (string-match "%a" a)
  6557. (setq a (replace-match address t t a)))
  6558. (if (string-match "%s" a)
  6559. (setq a (replace-match subject t t a)))
  6560. (push a args1))))
  6561. (apply cmd (nreverse args1))))
  6562. ((member type '("http" "https" "ftp" "news"))
  6563. (browse-url (concat type ":" (org-link-escape
  6564. path org-link-escape-chars-browser))))
  6565. ((member type '("message"))
  6566. (browse-url (concat type ":" path)))
  6567. ((string= type "tags")
  6568. (org-tags-view in-emacs path))
  6569. ((string= type "thisfile")
  6570. (if in-emacs
  6571. (switch-to-buffer-other-window
  6572. (org-get-buffer-for-internal-link (current-buffer)))
  6573. (org-mark-ring-push))
  6574. (let ((cmd `(org-link-search
  6575. ,path
  6576. ,(cond ((equal in-emacs '(4)) 'occur)
  6577. ((equal in-emacs '(16)) 'org-occur)
  6578. (t nil))
  6579. ,pos)))
  6580. (condition-case nil (eval cmd)
  6581. (error (progn (widen) (eval cmd))))))
  6582. ((string= type "tree-match")
  6583. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6584. ((string= type "file")
  6585. (if (string-match "::\\([0-9]+\\)\\'" path)
  6586. (setq line (string-to-number (match-string 1 path))
  6587. path (substring path 0 (match-beginning 0)))
  6588. (if (string-match "::\\(.+\\)\\'" path)
  6589. (setq search (match-string 1 path)
  6590. path (substring path 0 (match-beginning 0)))))
  6591. (if (string-match "[*?{]" (file-name-nondirectory path))
  6592. (dired path)
  6593. (org-open-file path in-emacs line search)))
  6594. ((string= type "news")
  6595. (require 'org-gnus)
  6596. (org-gnus-follow-link path))
  6597. ((string= type "shell")
  6598. (let ((cmd path))
  6599. (if (or (not org-confirm-shell-link-function)
  6600. (funcall org-confirm-shell-link-function
  6601. (format "Execute \"%s\" in shell? "
  6602. (org-add-props cmd nil
  6603. 'face 'org-warning))))
  6604. (progn
  6605. (message "Executing %s" cmd)
  6606. (shell-command cmd))
  6607. (error "Abort"))))
  6608. ((string= type "elisp")
  6609. (let ((cmd path))
  6610. (if (or (not org-confirm-elisp-link-function)
  6611. (funcall org-confirm-elisp-link-function
  6612. (format "Execute \"%s\" as elisp? "
  6613. (org-add-props cmd nil
  6614. 'face 'org-warning))))
  6615. (message "%s => %s" cmd
  6616. (if (equal (string-to-char cmd) ?\()
  6617. (eval (read cmd))
  6618. (call-interactively (read cmd))))
  6619. (error "Abort"))))
  6620. (t
  6621. (browse-url-at-point))))))
  6622. (move-marker org-open-link-marker nil)
  6623. (run-hook-with-args 'org-follow-link-hook))
  6624. ;;;; Time estimates
  6625. (defun org-get-effort (&optional pom)
  6626. "Get the effort estimate for the current entry."
  6627. (org-entry-get pom org-effort-property))
  6628. ;;; File search
  6629. (defvar org-create-file-search-functions nil
  6630. "List of functions to construct the right search string for a file link.
  6631. These functions are called in turn with point at the location to
  6632. which the link should point.
  6633. A function in the hook should first test if it would like to
  6634. handle this file type, for example by checking the major-mode or
  6635. the file extension. If it decides not to handle this file, it
  6636. should just return nil to give other functions a chance. If it
  6637. does handle the file, it must return the search string to be used
  6638. when following the link. The search string will be part of the
  6639. file link, given after a double colon, and `org-open-at-point'
  6640. will automatically search for it. If special measures must be
  6641. taken to make the search successful, another function should be
  6642. added to the companion hook `org-execute-file-search-functions',
  6643. which see.
  6644. A function in this hook may also use `setq' to set the variable
  6645. `description' to provide a suggestion for the descriptive text to
  6646. be used for this link when it gets inserted into an Org-mode
  6647. buffer with \\[org-insert-link].")
  6648. (defvar org-execute-file-search-functions nil
  6649. "List of functions to execute a file search triggered by a link.
  6650. Functions added to this hook must accept a single argument, the
  6651. search string that was part of the file link, the part after the
  6652. double colon. The function must first check if it would like to
  6653. handle this search, for example by checking the major-mode or the
  6654. file extension. If it decides not to handle this search, it
  6655. should just return nil to give other functions a chance. If it
  6656. does handle the search, it must return a non-nil value to keep
  6657. other functions from trying.
  6658. Each function can access the current prefix argument through the
  6659. variable `current-prefix-argument'. Note that a single prefix is
  6660. used to force opening a link in Emacs, so it may be good to only
  6661. use a numeric or double prefix to guide the search function.
  6662. In case this is needed, a function in this hook can also restore
  6663. the window configuration before `org-open-at-point' was called using:
  6664. (set-window-configuration org-window-config-before-follow-link)")
  6665. (defun org-link-search (s &optional type avoid-pos)
  6666. "Search for a link search option.
  6667. If S is surrounded by forward slashes, it is interpreted as a
  6668. regular expression. In org-mode files, this will create an `org-occur'
  6669. sparse tree. In ordinary files, `occur' will be used to list matches.
  6670. If the current buffer is in `dired-mode', grep will be used to search
  6671. in all files. If AVOID-POS is given, ignore matches near that position."
  6672. (let ((case-fold-search t)
  6673. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6674. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6675. (append '(("") (" ") ("\t") ("\n"))
  6676. org-emphasis-alist)
  6677. "\\|") "\\)"))
  6678. (pos (point))
  6679. (pre nil) (post nil)
  6680. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6681. (cond
  6682. ;; First check if there are any special
  6683. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6684. ;; Now try the builtin stuff
  6685. ((save-excursion
  6686. (goto-char (point-min))
  6687. (and
  6688. (re-search-forward
  6689. (concat "<<" (regexp-quote s0) ">>") nil t)
  6690. (setq type 'dedicated
  6691. pos (match-beginning 0))))
  6692. ;; There is an exact target for this
  6693. (goto-char pos))
  6694. ((and (string-match "^(\\(.*\\))$" s0)
  6695. (save-excursion
  6696. (goto-char (point-min))
  6697. (and
  6698. (re-search-forward
  6699. (concat "[^[]" (regexp-quote
  6700. (format org-coderef-label-format
  6701. (match-string 1 s0))))
  6702. nil t)
  6703. (setq type 'dedicated
  6704. pos (1+ (match-beginning 0))))))
  6705. ;; There is a coderef target for this
  6706. (goto-char pos))
  6707. ((string-match "^/\\(.*\\)/$" s)
  6708. ;; A regular expression
  6709. (cond
  6710. ((org-mode-p)
  6711. (org-occur (match-string 1 s)))
  6712. ;;((eq major-mode 'dired-mode)
  6713. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6714. (t (org-do-occur (match-string 1 s)))))
  6715. (t
  6716. ;; A normal search strings
  6717. (when (equal (string-to-char s) ?*)
  6718. ;; Anchor on headlines, post may include tags.
  6719. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6720. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6721. s (substring s 1)))
  6722. (remove-text-properties
  6723. 0 (length s)
  6724. '(face nil mouse-face nil keymap nil fontified nil) s)
  6725. ;; Make a series of regular expressions to find a match
  6726. (setq words (org-split-string s "[ \n\r\t]+")
  6727. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6728. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6729. "\\)" markers)
  6730. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6731. re2a (concat "[ \t\r\n]" re2a_)
  6732. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6733. re4 (concat "[^a-zA-Z_]" re4_)
  6734. re1 (concat pre re2 post)
  6735. re3 (concat pre (if pre re4_ re4) post)
  6736. re5 (concat pre ".*" re4)
  6737. re2 (concat pre re2)
  6738. re2a (concat pre (if pre re2a_ re2a))
  6739. re4 (concat pre (if pre re4_ re4))
  6740. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6741. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6742. re5 "\\)"
  6743. ))
  6744. (cond
  6745. ((eq type 'org-occur) (org-occur reall))
  6746. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6747. (t (goto-char (point-min))
  6748. (setq type 'fuzzy)
  6749. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6750. (org-search-not-self 1 re1 nil t)
  6751. (org-search-not-self 1 re2 nil t)
  6752. (org-search-not-self 1 re2a nil t)
  6753. (org-search-not-self 1 re3 nil t)
  6754. (org-search-not-self 1 re4 nil t)
  6755. (org-search-not-self 1 re5 nil t)
  6756. )
  6757. (goto-char (match-beginning 1))
  6758. (goto-char pos)
  6759. (error "No match")))))
  6760. (t
  6761. ;; Normal string-search
  6762. (goto-char (point-min))
  6763. (if (search-forward s nil t)
  6764. (goto-char (match-beginning 0))
  6765. (error "No match"))))
  6766. (and (org-mode-p) (org-show-context 'link-search))
  6767. type))
  6768. (defun org-search-not-self (group &rest args)
  6769. "Execute `re-search-forward', but only accept matches that do not
  6770. enclose the position of `org-open-link-marker'."
  6771. (let ((m org-open-link-marker))
  6772. (catch 'exit
  6773. (while (apply 're-search-forward args)
  6774. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6775. (goto-char (match-end group))
  6776. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6777. (> (match-beginning 0) (marker-position m))
  6778. (< (match-end 0) (marker-position m)))
  6779. (save-match-data
  6780. (or (not (org-in-regexp
  6781. org-bracket-link-analytic-regexp 1))
  6782. (not (match-end 4)) ; no description
  6783. (and (<= (match-beginning 4) (point))
  6784. (>= (match-end 4) (point))))))
  6785. (throw 'exit (point))))))))
  6786. (defun org-get-buffer-for-internal-link (buffer)
  6787. "Return a buffer to be used for displaying the link target of internal links."
  6788. (cond
  6789. ((not org-display-internal-link-with-indirect-buffer)
  6790. buffer)
  6791. ((string-match "(Clone)$" (buffer-name buffer))
  6792. (message "Buffer is already a clone, not making another one")
  6793. ;; we also do not modify visibility in this case
  6794. buffer)
  6795. (t ; make a new indirect buffer for displaying the link
  6796. (let* ((bn (buffer-name buffer))
  6797. (ibn (concat bn "(Clone)"))
  6798. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6799. (with-current-buffer ib (org-overview))
  6800. ib))))
  6801. (defun org-do-occur (regexp &optional cleanup)
  6802. "Call the Emacs command `occur'.
  6803. If CLEANUP is non-nil, remove the printout of the regular expression
  6804. in the *Occur* buffer. This is useful if the regex is long and not useful
  6805. to read."
  6806. (occur regexp)
  6807. (when cleanup
  6808. (let ((cwin (selected-window)) win beg end)
  6809. (when (setq win (get-buffer-window "*Occur*"))
  6810. (select-window win))
  6811. (goto-char (point-min))
  6812. (when (re-search-forward "match[a-z]+" nil t)
  6813. (setq beg (match-end 0))
  6814. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6815. (setq end (1- (match-beginning 0)))))
  6816. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6817. (goto-char (point-min))
  6818. (select-window cwin))))
  6819. ;;; The mark ring for links jumps
  6820. (defvar org-mark-ring nil
  6821. "Mark ring for positions before jumps in Org-mode.")
  6822. (defvar org-mark-ring-last-goto nil
  6823. "Last position in the mark ring used to go back.")
  6824. ;; Fill and close the ring
  6825. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6826. (loop for i from 1 to org-mark-ring-length do
  6827. (push (make-marker) org-mark-ring))
  6828. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6829. org-mark-ring)
  6830. (defun org-mark-ring-push (&optional pos buffer)
  6831. "Put the current position or POS into the mark ring and rotate it."
  6832. (interactive)
  6833. (setq pos (or pos (point)))
  6834. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6835. (move-marker (car org-mark-ring)
  6836. (or pos (point))
  6837. (or buffer (current-buffer)))
  6838. (message "%s"
  6839. (substitute-command-keys
  6840. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6841. (defun org-mark-ring-goto (&optional n)
  6842. "Jump to the previous position in the mark ring.
  6843. With prefix arg N, jump back that many stored positions. When
  6844. called several times in succession, walk through the entire ring.
  6845. Org-mode commands jumping to a different position in the current file,
  6846. or to another Org-mode file, automatically push the old position
  6847. onto the ring."
  6848. (interactive "p")
  6849. (let (p m)
  6850. (if (eq last-command this-command)
  6851. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6852. (setq p org-mark-ring))
  6853. (setq org-mark-ring-last-goto p)
  6854. (setq m (car p))
  6855. (switch-to-buffer (marker-buffer m))
  6856. (goto-char m)
  6857. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6858. (defun org-remove-angle-brackets (s)
  6859. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6860. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6861. s)
  6862. (defun org-add-angle-brackets (s)
  6863. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6864. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6865. s)
  6866. (defun org-remove-double-quotes (s)
  6867. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6868. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6869. s)
  6870. ;;; Following specific links
  6871. (defun org-follow-timestamp-link ()
  6872. (cond
  6873. ((org-at-date-range-p t)
  6874. (let ((org-agenda-start-on-weekday)
  6875. (t1 (match-string 1))
  6876. (t2 (match-string 2)))
  6877. (setq t1 (time-to-days (org-time-string-to-time t1))
  6878. t2 (time-to-days (org-time-string-to-time t2)))
  6879. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6880. ((org-at-timestamp-p t)
  6881. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6882. (substring (match-string 1) 0 10)))
  6883. 1))
  6884. (t (error "This should not happen"))))
  6885. ;;; Following file links
  6886. (defvar org-wait nil)
  6887. (defun org-open-file (path &optional in-emacs line search)
  6888. "Open the file at PATH.
  6889. First, this expands any special file name abbreviations. Then the
  6890. configuration variable `org-file-apps' is checked if it contains an
  6891. entry for this file type, and if yes, the corresponding command is launched.
  6892. If no application is found, Emacs simply visits the file.
  6893. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6894. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6895. and o use an external application to visit the file.
  6896. Optional LINE specifies a line to go to, optional SEARCH a string to
  6897. search for. If LINE or SEARCH is given, the file will always be
  6898. opened in Emacs.
  6899. If the file does not exist, an error is thrown."
  6900. (setq in-emacs (or in-emacs line search))
  6901. (let* ((file (if (equal path "")
  6902. buffer-file-name
  6903. (substitute-in-file-name (expand-file-name path))))
  6904. (apps (append org-file-apps (org-default-apps)))
  6905. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6906. (dirp (if remp nil (file-directory-p file)))
  6907. (file (if (and dirp org-open-directory-means-index-dot-org)
  6908. (concat (file-name-as-directory file) "index.org")
  6909. file))
  6910. (a-m-a-p (assq 'auto-mode apps))
  6911. (dfile (downcase file))
  6912. (old-buffer (current-buffer))
  6913. (old-pos (point))
  6914. (old-mode major-mode)
  6915. ext cmd)
  6916. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6917. (setq ext (match-string 1 dfile))
  6918. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6919. (setq ext (match-string 1 dfile))))
  6920. (cond
  6921. ((equal in-emacs '(16))
  6922. (setq cmd (cdr (assoc 'system apps))))
  6923. (in-emacs (setq cmd 'emacs))
  6924. (t
  6925. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6926. (and dirp (cdr (assoc 'directory apps)))
  6927. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6928. 'string-match)
  6929. (cdr (assoc ext apps))
  6930. (cdr (assoc t apps))))))
  6931. (when (eq cmd 'system)
  6932. (setq cmd (cdr (assoc 'system apps))))
  6933. (when (eq cmd 'default)
  6934. (setq cmd (cdr (assoc t apps))))
  6935. (when (eq cmd 'mailcap)
  6936. (require 'mailcap)
  6937. (mailcap-parse-mailcaps)
  6938. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6939. (command (mailcap-mime-info mime-type)))
  6940. (if (stringp command)
  6941. (setq cmd command)
  6942. (setq cmd 'emacs))))
  6943. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6944. (not (file-exists-p file))
  6945. (not org-open-non-existing-files))
  6946. (error "No such file: %s" file))
  6947. (cond
  6948. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6949. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6950. (while (string-match "['\"]%s['\"]" cmd)
  6951. (setq cmd (replace-match "%s" t t cmd)))
  6952. (while (string-match "%s" cmd)
  6953. (setq cmd (replace-match
  6954. (save-match-data
  6955. (shell-quote-argument
  6956. (convert-standard-filename file)))
  6957. t t cmd)))
  6958. (save-window-excursion
  6959. (start-process-shell-command cmd nil cmd)
  6960. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6961. ))
  6962. ((or (stringp cmd)
  6963. (eq cmd 'emacs))
  6964. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6965. (widen)
  6966. (if line (goto-line line)
  6967. (if search (org-link-search search))))
  6968. ((consp cmd)
  6969. (let ((file (convert-standard-filename file)))
  6970. (eval cmd)))
  6971. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6972. (and (org-mode-p) (eq old-mode 'org-mode)
  6973. (or (not (equal old-buffer (current-buffer)))
  6974. (not (equal old-pos (point))))
  6975. (org-mark-ring-push old-pos old-buffer))))
  6976. (defun org-default-apps ()
  6977. "Return the default applications for this operating system."
  6978. (cond
  6979. ((eq system-type 'darwin)
  6980. org-file-apps-defaults-macosx)
  6981. ((eq system-type 'windows-nt)
  6982. org-file-apps-defaults-windowsnt)
  6983. (t org-file-apps-defaults-gnu)))
  6984. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6985. "Convert extensions to regular expressions in the cars of LIST.
  6986. Also, weed out any non-string entries, because the return value is used
  6987. only for regexp matching.
  6988. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6989. point to the symbol `emacs', indicating that the file should
  6990. be opened in Emacs."
  6991. (append
  6992. (delq nil
  6993. (mapcar (lambda (x)
  6994. (if (not (stringp (car x)))
  6995. nil
  6996. (if (string-match "\\W" (car x))
  6997. x
  6998. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6999. list))
  7000. (if add-auto-mode
  7001. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7002. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7003. (defun org-file-remote-p (file)
  7004. "Test whether FILE specifies a location on a remote system.
  7005. Return non-nil if the location is indeed remote.
  7006. For example, the filename \"/user@host:/foo\" specifies a location
  7007. on the system \"/user@host:\"."
  7008. (cond ((fboundp 'file-remote-p)
  7009. (file-remote-p file))
  7010. ((fboundp 'tramp-handle-file-remote-p)
  7011. (tramp-handle-file-remote-p file))
  7012. ((and (boundp 'ange-ftp-name-format)
  7013. (string-match (car ange-ftp-name-format) file))
  7014. t)
  7015. (t nil)))
  7016. ;;;; Refiling
  7017. (defun org-get-org-file ()
  7018. "Read a filename, with default directory `org-directory'."
  7019. (let ((default (or org-default-notes-file remember-data-file)))
  7020. (read-file-name (format "File name [%s]: " default)
  7021. (file-name-as-directory org-directory)
  7022. default)))
  7023. (defun org-notes-order-reversed-p ()
  7024. "Check if the current file should receive notes in reversed order."
  7025. (cond
  7026. ((not org-reverse-note-order) nil)
  7027. ((eq t org-reverse-note-order) t)
  7028. ((not (listp org-reverse-note-order)) nil)
  7029. (t (catch 'exit
  7030. (let ((all org-reverse-note-order)
  7031. entry)
  7032. (while (setq entry (pop all))
  7033. (if (string-match (car entry) buffer-file-name)
  7034. (throw 'exit (cdr entry))))
  7035. nil)))))
  7036. (defvar org-refile-target-table nil
  7037. "The list of refile targets, created by `org-refile'.")
  7038. (defvar org-agenda-new-buffers nil
  7039. "Buffers created to visit agenda files.")
  7040. (defun org-get-refile-targets (&optional default-buffer)
  7041. "Produce a table with refile targets."
  7042. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7043. targets txt re files f desc descre fast-path-p level)
  7044. (message "Getting targets...")
  7045. (with-current-buffer (or default-buffer (current-buffer))
  7046. (while (setq entry (pop entries))
  7047. (setq files (car entry) desc (cdr entry))
  7048. (setq fast-path-p nil)
  7049. (cond
  7050. ((null files) (setq files (list (current-buffer))))
  7051. ((eq files 'org-agenda-files)
  7052. (setq files (org-agenda-files 'unrestricted)))
  7053. ((and (symbolp files) (fboundp files))
  7054. (setq files (funcall files)))
  7055. ((and (symbolp files) (boundp files))
  7056. (setq files (symbol-value files))))
  7057. (if (stringp files) (setq files (list files)))
  7058. (cond
  7059. ((eq (car desc) :tag)
  7060. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7061. ((eq (car desc) :todo)
  7062. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7063. ((eq (car desc) :regexp)
  7064. (setq descre (cdr desc)))
  7065. ((eq (car desc) :level)
  7066. (setq descre (concat "^\\*\\{" (number-to-string
  7067. (if org-odd-levels-only
  7068. (1- (* 2 (cdr desc)))
  7069. (cdr desc)))
  7070. "\\}[ \t]")))
  7071. ((eq (car desc) :maxlevel)
  7072. (setq fast-path-p t)
  7073. (setq descre (concat "^\\*\\{1," (number-to-string
  7074. (if org-odd-levels-only
  7075. (1- (* 2 (cdr desc)))
  7076. (cdr desc)))
  7077. "\\}[ \t]")))
  7078. (t (error "Bad refiling target description %s" desc)))
  7079. (while (setq f (pop files))
  7080. (save-excursion
  7081. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7082. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7083. (setq f (expand-file-name f))
  7084. (save-excursion
  7085. (save-restriction
  7086. (widen)
  7087. (goto-char (point-min))
  7088. (while (re-search-forward descre nil t)
  7089. (goto-char (point-at-bol))
  7090. (when (looking-at org-complex-heading-regexp)
  7091. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7092. txt (org-link-display-format (match-string 4))
  7093. re (concat "^" (regexp-quote
  7094. (buffer-substring (match-beginning 1)
  7095. (match-end 4)))))
  7096. (if (match-end 5) (setq re (concat re "[ \t]+"
  7097. (regexp-quote
  7098. (match-string 5)))))
  7099. (setq re (concat re "[ \t]*$"))
  7100. (when org-refile-use-outline-path
  7101. (setq txt (mapconcat 'org-protect-slash
  7102. (append
  7103. (if (eq org-refile-use-outline-path 'file)
  7104. (list (file-name-nondirectory
  7105. (buffer-file-name (buffer-base-buffer))))
  7106. (if (eq org-refile-use-outline-path 'full-file-path)
  7107. (list (buffer-file-name (buffer-base-buffer)))))
  7108. (org-get-outline-path fast-path-p level txt)
  7109. (list txt))
  7110. "/")))
  7111. (push (list txt f re (point)) targets))
  7112. (goto-char (point-at-eol))))))))
  7113. (message "Getting targets...done")
  7114. (nreverse targets))))
  7115. (defun org-protect-slash (s)
  7116. (while (string-match "/" s)
  7117. (setq s (replace-match "\\" t t s)))
  7118. s)
  7119. (defvar org-olpa (make-vector 20 nil))
  7120. (defun org-get-outline-path (&optional fastp level heading)
  7121. "Return the outline path to the current entry, as a list."
  7122. (if fastp
  7123. (progn
  7124. (if (> level 19)
  7125. (error "Outline path failure, more than 19 levels."))
  7126. (loop for i from level upto 19 do
  7127. (aset org-olpa i nil))
  7128. (prog1
  7129. (delq nil (append org-olpa nil))
  7130. (aset org-olpa level heading)))
  7131. (let (rtn)
  7132. (save-excursion
  7133. (while (org-up-heading-safe)
  7134. (when (looking-at org-complex-heading-regexp)
  7135. (push (org-match-string-no-properties 4) rtn)))
  7136. rtn))))
  7137. (defvar org-refile-history nil
  7138. "History for refiling operations.")
  7139. (defun org-refile (&optional goto default-buffer)
  7140. "Move the entry at point to another heading.
  7141. The list of target headings is compiled using the information in
  7142. `org-refile-targets', which see. This list is created before each use
  7143. and will therefore always be up-to-date.
  7144. At the target location, the entry is filed as a subitem of the target heading.
  7145. Depending on `org-reverse-note-order', the new subitem will either be the
  7146. first or the last subitem.
  7147. If there is an active region, all entries in that region will be moved.
  7148. However, the region must fulfil the requirement that the first heading
  7149. is the first one sets the top-level of the moved text - at most siblings
  7150. below it are allowed.
  7151. With prefix arg GOTO, the command will only visit the target location,
  7152. not actually move anything.
  7153. With a double prefix `C-u C-u', go to the location where the last refiling
  7154. operation has put the subtree."
  7155. (interactive "P")
  7156. (let* ((cbuf (current-buffer))
  7157. (regionp (org-region-active-p))
  7158. (region-start (and regionp (region-beginning)))
  7159. (region-end (and regionp (region-end)))
  7160. (region-length (and regionp (- region-end region-start)))
  7161. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7162. pos it nbuf file re level reversed)
  7163. (when regionp (goto-char region-start)
  7164. (unless (org-kill-is-subtree-p
  7165. (buffer-substring region-start region-end))
  7166. (error "The region is not a (sequence of) subtree(s)")))
  7167. (if (equal goto '(16))
  7168. (org-refile-goto-last-stored)
  7169. (when (setq it (org-refile-get-location
  7170. (if goto "Goto: " "Refile to: ") default-buffer))
  7171. (setq file (nth 1 it)
  7172. re (nth 2 it)
  7173. pos (nth 3 it))
  7174. (if (and (equal (buffer-file-name) file)
  7175. (if regionp
  7176. (and (>= pos region-start)
  7177. (<= pos region-end))
  7178. (and (>= pos (point))
  7179. (< pos (save-excursion
  7180. (org-end-of-subtree t t))))))
  7181. (error "Cannot refile to position inside the tree or region"))
  7182. (setq nbuf (or (find-buffer-visiting file)
  7183. (find-file-noselect file)))
  7184. (if goto
  7185. (progn
  7186. (switch-to-buffer nbuf)
  7187. (goto-char pos)
  7188. (org-show-context 'org-goto))
  7189. (if regionp
  7190. (progn
  7191. (kill-new (buffer-substring region-start region-end))
  7192. (org-save-markers-in-region region-start region-end))
  7193. (org-copy-subtree 1 nil t))
  7194. (save-excursion
  7195. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7196. (find-file-noselect file))))
  7197. (setq reversed (org-notes-order-reversed-p))
  7198. (save-excursion
  7199. (save-restriction
  7200. (widen)
  7201. (goto-char pos)
  7202. (looking-at outline-regexp)
  7203. (setq level (org-get-valid-level (funcall outline-level) 1))
  7204. (goto-char
  7205. (if reversed
  7206. (or (outline-next-heading) (point-max))
  7207. (or (save-excursion (outline-get-next-sibling))
  7208. (org-end-of-subtree t t)
  7209. (point-max))))
  7210. (if (not (bolp)) (newline))
  7211. (bookmark-set "org-refile-last-stored")
  7212. (org-paste-subtree level))))
  7213. (if regionp
  7214. (delete-region (point) (+ (point) region-length))
  7215. (org-cut-subtree))
  7216. (setq org-markers-to-move nil)
  7217. (message "Refiled to \"%s\"" (car it)))))))
  7218. (defun org-refile-goto-last-stored ()
  7219. "Go to the location where the last refile was stored."
  7220. (interactive)
  7221. (bookmark-jump "org-refile-last-stored")
  7222. (message "This is the location of the last refile"))
  7223. (defun org-refile-get-location (&optional prompt default-buffer)
  7224. "Prompt the user for a refile location, using PROMPT."
  7225. (let ((org-refile-targets org-refile-targets)
  7226. (org-refile-use-outline-path org-refile-use-outline-path))
  7227. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7228. (unless org-refile-target-table
  7229. (error "No refile targets"))
  7230. (let* ((cbuf (current-buffer))
  7231. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7232. (cfunc (if (and org-refile-use-outline-path
  7233. org-outline-path-complete-in-steps)
  7234. 'org-olpath-completing-read
  7235. 'org-ido-completing-read))
  7236. (extra (if org-refile-use-outline-path "/" ""))
  7237. (filename (and cfn (expand-file-name cfn)))
  7238. (tbl (mapcar
  7239. (lambda (x)
  7240. (if (not (equal filename (nth 1 x)))
  7241. (cons (concat (car x) extra " ("
  7242. (file-name-nondirectory (nth 1 x)) ")")
  7243. (cdr x))
  7244. (cons (concat (car x) extra) (cdr x))))
  7245. org-refile-target-table))
  7246. (completion-ignore-case t))
  7247. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7248. tbl)))
  7249. (defun org-olpath-completing-read (prompt collection &rest args)
  7250. "Read an outline path like a file name."
  7251. (let ((thetable collection)
  7252. (org-completion-use-ido nil)) ; does not work with ido.
  7253. (apply
  7254. 'org-ido-completing-read prompt
  7255. (lambda (string predicate &optional flag)
  7256. (let (rtn r f (l (length string)))
  7257. (cond
  7258. ((eq flag nil)
  7259. ;; try completion
  7260. (try-completion string thetable))
  7261. ((eq flag t)
  7262. ;; all-completions
  7263. (setq rtn (all-completions string thetable predicate))
  7264. (mapcar
  7265. (lambda (x)
  7266. (setq r (substring x l))
  7267. (if (string-match " ([^)]*)$" x)
  7268. (setq f (match-string 0 x))
  7269. (setq f ""))
  7270. (if (string-match "/" r)
  7271. (concat string (substring r 0 (match-end 0)) f)
  7272. x))
  7273. rtn))
  7274. ((eq flag 'lambda)
  7275. ;; exact match?
  7276. (assoc string thetable)))
  7277. ))
  7278. args)))
  7279. ;;;; Dynamic blocks
  7280. (defun org-find-dblock (name)
  7281. "Find the first dynamic block with name NAME in the buffer.
  7282. If not found, stay at current position and return nil."
  7283. (let (pos)
  7284. (save-excursion
  7285. (goto-char (point-min))
  7286. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7287. nil t)
  7288. (match-beginning 0))))
  7289. (if pos (goto-char pos))
  7290. pos))
  7291. (defconst org-dblock-start-re
  7292. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7293. "Matches the startline of a dynamic block, with parameters.")
  7294. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7295. "Matches the end of a dynamic block.")
  7296. (defun org-create-dblock (plist)
  7297. "Create a dynamic block section, with parameters taken from PLIST.
  7298. PLIST must contain a :name entry which is used as name of the block."
  7299. (unless (bolp) (newline))
  7300. (let ((name (plist-get plist :name)))
  7301. (insert "#+BEGIN: " name)
  7302. (while plist
  7303. (if (eq (car plist) :name)
  7304. (setq plist (cddr plist))
  7305. (insert " " (prin1-to-string (pop plist)))))
  7306. (insert "\n\n#+END:\n")
  7307. (beginning-of-line -2)))
  7308. (defun org-prepare-dblock ()
  7309. "Prepare dynamic block for refresh.
  7310. This empties the block, puts the cursor at the insert position and returns
  7311. the property list including an extra property :name with the block name."
  7312. (unless (looking-at org-dblock-start-re)
  7313. (error "Not at a dynamic block"))
  7314. (let* ((begdel (1+ (match-end 0)))
  7315. (name (org-no-properties (match-string 1)))
  7316. (params (append (list :name name)
  7317. (read (concat "(" (match-string 3) ")")))))
  7318. (unless (re-search-forward org-dblock-end-re nil t)
  7319. (error "Dynamic block not terminated"))
  7320. (setq params
  7321. (append params
  7322. (list :content (buffer-substring
  7323. begdel (match-beginning 0)))))
  7324. (delete-region begdel (match-beginning 0))
  7325. (goto-char begdel)
  7326. (open-line 1)
  7327. params))
  7328. (defun org-map-dblocks (&optional command)
  7329. "Apply COMMAND to all dynamic blocks in the current buffer.
  7330. If COMMAND is not given, use `org-update-dblock'."
  7331. (let ((cmd (or command 'org-update-dblock))
  7332. pos)
  7333. (save-excursion
  7334. (goto-char (point-min))
  7335. (while (re-search-forward org-dblock-start-re nil t)
  7336. (goto-char (setq pos (match-beginning 0)))
  7337. (condition-case nil
  7338. (funcall cmd)
  7339. (error (message "Error during update of dynamic block")))
  7340. (goto-char pos)
  7341. (unless (re-search-forward org-dblock-end-re nil t)
  7342. (error "Dynamic block not terminated"))))))
  7343. (defun org-dblock-update (&optional arg)
  7344. "User command for updating dynamic blocks.
  7345. Update the dynamic block at point. With prefix ARG, update all dynamic
  7346. blocks in the buffer."
  7347. (interactive "P")
  7348. (if arg
  7349. (org-update-all-dblocks)
  7350. (or (looking-at org-dblock-start-re)
  7351. (org-beginning-of-dblock))
  7352. (org-update-dblock)))
  7353. (defun org-update-dblock ()
  7354. "Update the dynamic block at point
  7355. This means to empty the block, parse for parameters and then call
  7356. the correct writing function."
  7357. (save-window-excursion
  7358. (let* ((pos (point))
  7359. (line (org-current-line))
  7360. (params (org-prepare-dblock))
  7361. (name (plist-get params :name))
  7362. (cmd (intern (concat "org-dblock-write:" name))))
  7363. (message "Updating dynamic block `%s' at line %d..." name line)
  7364. (funcall cmd params)
  7365. (message "Updating dynamic block `%s' at line %d...done" name line)
  7366. (goto-char pos))))
  7367. (defun org-beginning-of-dblock ()
  7368. "Find the beginning of the dynamic block at point.
  7369. Error if there is no such block at point."
  7370. (let ((pos (point))
  7371. beg)
  7372. (end-of-line 1)
  7373. (if (and (re-search-backward org-dblock-start-re nil t)
  7374. (setq beg (match-beginning 0))
  7375. (re-search-forward org-dblock-end-re nil t)
  7376. (> (match-end 0) pos))
  7377. (goto-char beg)
  7378. (goto-char pos)
  7379. (error "Not in a dynamic block"))))
  7380. (defun org-update-all-dblocks ()
  7381. "Update all dynamic blocks in the buffer.
  7382. This function can be used in a hook."
  7383. (when (org-mode-p)
  7384. (org-map-dblocks 'org-update-dblock)))
  7385. ;;;; Completion
  7386. (defconst org-additional-option-like-keywords
  7387. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7388. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7389. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7390. "BEGIN_QUOTE" "END_QUOTE"
  7391. "BEGIN_VERSE" "END_VERSE"
  7392. "BEGIN_SRC" "END_SRC"
  7393. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7394. (defcustom org-structure-template-alist
  7395. '(
  7396. ("s" "#+begin_src ?\n\n#+end_src"
  7397. "<src lang=\"?\">\n\n</src>")
  7398. ("e" "#+begin_example\n?\n#+end_example"
  7399. "<example>\n?\n</example>")
  7400. ("q" "#+begin_quote\n?\n#+end_quote"
  7401. "<quote>\n?\n</quote>")
  7402. ("v" "#+begin_verse\n?\n#+end_verse"
  7403. "<verse>\n?\n/verse>")
  7404. ("l" "#+begin_latex\n?\n#+end_latex"
  7405. "<literal style=\"latex\">\n?\n</literal>")
  7406. ("L" "#+latex: "
  7407. "<literal style=\"latex\">?</literal>")
  7408. ("h" "#+begin_html\n?\n#+end_html"
  7409. "<literal style=\"html\">\n?\n</literal>")
  7410. ("H" "#+html: "
  7411. "<literal style=\"html\">?</literal>")
  7412. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7413. ("A" "#+ascii: ")
  7414. ("i" "#+include %file ?"
  7415. "<include file=%file markup=\"?\">")
  7416. )
  7417. "Structure completion elements.
  7418. This is a list of abbreviation keys and values. The value gets inserted
  7419. it you type @samp{.} followed by the key and then the completion key,
  7420. usually `M-TAB'. %file will be replaced by a file name after prompting
  7421. for the file using completion.
  7422. There are two templates for each key, the first uses the original Org syntax,
  7423. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7424. the default when the /org-mtags.el/ module has been loaded. See also the
  7425. variable `org-mtags-prefer-muse-templates'.
  7426. This is an experimental feature, it is undecided if it is going to stay in."
  7427. :group 'org-completion
  7428. :type '(repeat
  7429. (string :tag "Key")
  7430. (string :tag "Template")
  7431. (string :tag "Muse Template")))
  7432. (defun org-try-structure-completion ()
  7433. "Try to complete a structure template before point.
  7434. This looks for strings like \"<e\" on an otherwise empty line and
  7435. expands them."
  7436. (let ((l (buffer-substring (point-at-bol) (point)))
  7437. a)
  7438. (when (and (looking-at "[ \t]*$")
  7439. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7440. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7441. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7442. (match-beginning 1)) a)
  7443. t)))
  7444. (defun org-complete-expand-structure-template (start cell)
  7445. "Expand a structure template."
  7446. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7447. (rpl (nth (if musep 2 1) cell)))
  7448. (delete-region start (point))
  7449. (when (string-match "\\`#\\+" rpl)
  7450. (cond
  7451. ((bolp))
  7452. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7453. (delete-region (point-at-bol) (point)))
  7454. (t (newline))))
  7455. (setq start (point))
  7456. (if (string-match "%file" rpl)
  7457. (setq rpl (replace-match
  7458. (concat
  7459. "\""
  7460. (save-match-data
  7461. (abbreviate-file-name (read-file-name "Include file: ")))
  7462. "\"")
  7463. t t rpl)))
  7464. (insert rpl)
  7465. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7466. (defun org-complete (&optional arg)
  7467. "Perform completion on word at point.
  7468. At the beginning of a headline, this completes TODO keywords as given in
  7469. `org-todo-keywords'.
  7470. If the current word is preceded by a backslash, completes the TeX symbols
  7471. that are supported for HTML support.
  7472. If the current word is preceded by \"#+\", completes special words for
  7473. setting file options.
  7474. In the line after \"#+STARTUP:, complete valid keywords.\"
  7475. At all other locations, this simply calls the value of
  7476. `org-completion-fallback-command'."
  7477. (interactive "P")
  7478. (org-without-partial-completion
  7479. (catch 'exit
  7480. (let* ((a nil)
  7481. (end (point))
  7482. (beg1 (save-excursion
  7483. (skip-chars-backward (org-re "[:alnum:]_@"))
  7484. (point)))
  7485. (beg (save-excursion
  7486. (skip-chars-backward "a-zA-Z0-9_:$")
  7487. (point)))
  7488. (confirm (lambda (x) (stringp (car x))))
  7489. (searchhead (equal (char-before beg) ?*))
  7490. (struct
  7491. (when (and (member (char-before beg1) '(?. ?<))
  7492. (setq a (assoc (buffer-substring beg1 (point))
  7493. org-structure-template-alist)))
  7494. (org-complete-expand-structure-template (1- beg1) a)
  7495. (throw 'exit t)))
  7496. (tag (and (equal (char-before beg1) ?:)
  7497. (equal (char-after (point-at-bol)) ?*)))
  7498. (prop (and (equal (char-before beg1) ?:)
  7499. (not (equal (char-after (point-at-bol)) ?*))))
  7500. (texp (equal (char-before beg) ?\\))
  7501. (link (equal (char-before beg) ?\[))
  7502. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7503. beg)
  7504. "#+"))
  7505. (startup (string-match "^#\\+STARTUP:.*"
  7506. (buffer-substring (point-at-bol) (point))))
  7507. (completion-ignore-case opt)
  7508. (type nil)
  7509. (tbl nil)
  7510. (table (cond
  7511. (opt
  7512. (setq type :opt)
  7513. (require 'org-exp)
  7514. (append
  7515. (mapcar
  7516. (lambda (x)
  7517. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7518. (cons (match-string 2 x) (match-string 1 x)))
  7519. (org-split-string (org-get-current-options) "\n"))
  7520. (mapcar 'list org-additional-option-like-keywords)))
  7521. (startup
  7522. (setq type :startup)
  7523. org-startup-options)
  7524. (link (append org-link-abbrev-alist-local
  7525. org-link-abbrev-alist))
  7526. (texp
  7527. (setq type :tex)
  7528. org-html-entities)
  7529. ((string-match "\\`\\*+[ \t]+\\'"
  7530. (buffer-substring (point-at-bol) beg))
  7531. (setq type :todo)
  7532. (mapcar 'list org-todo-keywords-1))
  7533. (searchhead
  7534. (setq type :searchhead)
  7535. (save-excursion
  7536. (goto-char (point-min))
  7537. (while (re-search-forward org-todo-line-regexp nil t)
  7538. (push (list
  7539. (org-make-org-heading-search-string
  7540. (match-string 3) t))
  7541. tbl)))
  7542. tbl)
  7543. (tag (setq type :tag beg beg1)
  7544. (or org-tag-alist (org-get-buffer-tags)))
  7545. (prop (setq type :prop beg beg1)
  7546. (mapcar 'list (org-buffer-property-keys nil t t)))
  7547. (t (progn
  7548. (call-interactively org-completion-fallback-command)
  7549. (throw 'exit nil)))))
  7550. (pattern (buffer-substring-no-properties beg end))
  7551. (completion (try-completion pattern table confirm)))
  7552. (cond ((eq completion t)
  7553. (if (not (assoc (upcase pattern) table))
  7554. (message "Already complete")
  7555. (if (and (equal type :opt)
  7556. (not (member (car (assoc (upcase pattern) table))
  7557. org-additional-option-like-keywords)))
  7558. (insert (substring (cdr (assoc (upcase pattern) table))
  7559. (length pattern)))
  7560. (if (memq type '(:tag :prop)) (insert ":")))))
  7561. ((null completion)
  7562. (message "Can't find completion for \"%s\"" pattern)
  7563. (ding))
  7564. ((not (string= pattern completion))
  7565. (delete-region beg end)
  7566. (if (string-match " +$" completion)
  7567. (setq completion (replace-match "" t t completion)))
  7568. (insert completion)
  7569. (if (get-buffer-window "*Completions*")
  7570. (delete-window (get-buffer-window "*Completions*")))
  7571. (if (assoc completion table)
  7572. (if (eq type :todo) (insert " ")
  7573. (if (memq type '(:tag :prop)) (insert ":"))))
  7574. (if (and (equal type :opt) (assoc completion table))
  7575. (message "%s" (substitute-command-keys
  7576. "Press \\[org-complete] again to insert example settings"))))
  7577. (t
  7578. (message "Making completion list...")
  7579. (let ((list (sort (all-completions pattern table confirm)
  7580. 'string<)))
  7581. (with-output-to-temp-buffer "*Completions*"
  7582. (condition-case nil
  7583. ;; Protection needed for XEmacs and emacs 21
  7584. (display-completion-list list pattern)
  7585. (error (display-completion-list list)))))
  7586. (message "Making completion list...%s" "done")))))))
  7587. ;;;; TODO, DEADLINE, Comments
  7588. (defun org-toggle-comment ()
  7589. "Change the COMMENT state of an entry."
  7590. (interactive)
  7591. (save-excursion
  7592. (org-back-to-heading)
  7593. (let (case-fold-search)
  7594. (if (looking-at (concat outline-regexp
  7595. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7596. (replace-match "" t t nil 1)
  7597. (if (looking-at outline-regexp)
  7598. (progn
  7599. (goto-char (match-end 0))
  7600. (insert org-comment-string " ")))))))
  7601. (defvar org-last-todo-state-is-todo nil
  7602. "This is non-nil when the last TODO state change led to a TODO state.
  7603. If the last change removed the TODO tag or switched to DONE, then
  7604. this is nil.")
  7605. (defvar org-setting-tags nil) ; dynamically skipped
  7606. (defun org-parse-local-options (string var)
  7607. "Parse STRING for startup setting relevant for variable VAR."
  7608. (let ((rtn (symbol-value var))
  7609. e opts)
  7610. (save-match-data
  7611. (if (or (not string) (not (string-match "\\S-" string)))
  7612. rtn
  7613. (setq opts (delq nil (mapcar (lambda (x)
  7614. (setq e (assoc x org-startup-options))
  7615. (if (eq (nth 1 e) var) e nil))
  7616. (org-split-string string "[ \t]+"))))
  7617. (if (not opts)
  7618. rtn
  7619. (setq rtn nil)
  7620. (while (setq e (pop opts))
  7621. (if (not (nth 3 e))
  7622. (setq rtn (nth 2 e))
  7623. (if (not (listp rtn)) (setq rtn nil))
  7624. (push (nth 2 e) rtn)))
  7625. rtn)))))
  7626. (defvar org-todo-setup-filter-hook nil
  7627. "Hook for functions that pre-filter todo specs.
  7628. Each function takes a todo spec and returns either `nil' or the spec
  7629. transformed into canonical form." )
  7630. (defvar org-todo-get-default-hook nil
  7631. "Hook for functions that get a default item for todo.
  7632. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7633. `nil' or a string to be used for the todo mark." )
  7634. (defvar org-agenda-headline-snapshot-before-repeat)
  7635. (defun org-todo (&optional arg)
  7636. "Change the TODO state of an item.
  7637. The state of an item is given by a keyword at the start of the heading,
  7638. like
  7639. *** TODO Write paper
  7640. *** DONE Call mom
  7641. The different keywords are specified in the variable `org-todo-keywords'.
  7642. By default the available states are \"TODO\" and \"DONE\".
  7643. So for this example: when the item starts with TODO, it is changed to DONE.
  7644. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7645. DONE are present, add TODO at the beginning of the heading.
  7646. With C-u prefix arg, use completion to determine the new state.
  7647. With numeric prefix arg, switch to that state.
  7648. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7649. With a tripple C-u prefix, circumvent any state blocking.
  7650. For calling through lisp, arg is also interpreted in the following way:
  7651. 'none -> empty state
  7652. \"\"(empty string) -> switch to empty state
  7653. 'done -> switch to DONE
  7654. 'nextset -> switch to the next set of keywords
  7655. 'previousset -> switch to the previous set of keywords
  7656. \"WAITING\" -> switch to the specified keyword, but only if it
  7657. really is a member of `org-todo-keywords'."
  7658. (interactive "P")
  7659. (if (equal arg '(16)) (setq arg 'nextset))
  7660. (let ((org-blocker-hook org-blocker-hook))
  7661. (when (equal arg '(64))
  7662. (setq arg nil org-blocker-hook nil))
  7663. (save-excursion
  7664. (catch 'exit
  7665. (org-back-to-heading)
  7666. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7667. (or (looking-at (concat " +" org-todo-regexp " *"))
  7668. (looking-at " *"))
  7669. (let* ((match-data (match-data))
  7670. (startpos (point-at-bol))
  7671. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7672. (org-log-done org-log-done)
  7673. (org-log-repeat org-log-repeat)
  7674. (org-todo-log-states org-todo-log-states)
  7675. (this (match-string 1))
  7676. (hl-pos (match-beginning 0))
  7677. (head (org-get-todo-sequence-head this))
  7678. (ass (assoc head org-todo-kwd-alist))
  7679. (interpret (nth 1 ass))
  7680. (done-word (nth 3 ass))
  7681. (final-done-word (nth 4 ass))
  7682. (last-state (or this ""))
  7683. (completion-ignore-case t)
  7684. (member (member this org-todo-keywords-1))
  7685. (tail (cdr member))
  7686. (state (cond
  7687. ((and org-todo-key-trigger
  7688. (or (and (equal arg '(4))
  7689. (eq org-use-fast-todo-selection 'prefix))
  7690. (and (not arg) org-use-fast-todo-selection
  7691. (not (eq org-use-fast-todo-selection
  7692. 'prefix)))))
  7693. ;; Use fast selection
  7694. (org-fast-todo-selection))
  7695. ((and (equal arg '(4))
  7696. (or (not org-use-fast-todo-selection)
  7697. (not org-todo-key-trigger)))
  7698. ;; Read a state with completion
  7699. (org-ido-completing-read
  7700. "State: " (mapcar (lambda(x) (list x))
  7701. org-todo-keywords-1)
  7702. nil t))
  7703. ((eq arg 'right)
  7704. (if this
  7705. (if tail (car tail) nil)
  7706. (car org-todo-keywords-1)))
  7707. ((eq arg 'left)
  7708. (if (equal member org-todo-keywords-1)
  7709. nil
  7710. (if this
  7711. (nth (- (length org-todo-keywords-1)
  7712. (length tail) 2)
  7713. org-todo-keywords-1)
  7714. (org-last org-todo-keywords-1))))
  7715. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7716. (setq arg nil))) ; hack to fall back to cycling
  7717. (arg
  7718. ;; user or caller requests a specific state
  7719. (cond
  7720. ((equal arg "") nil)
  7721. ((eq arg 'none) nil)
  7722. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7723. ((eq arg 'nextset)
  7724. (or (car (cdr (member head org-todo-heads)))
  7725. (car org-todo-heads)))
  7726. ((eq arg 'previousset)
  7727. (let ((org-todo-heads (reverse org-todo-heads)))
  7728. (or (car (cdr (member head org-todo-heads)))
  7729. (car org-todo-heads))))
  7730. ((car (member arg org-todo-keywords-1)))
  7731. ((nth (1- (prefix-numeric-value arg))
  7732. org-todo-keywords-1))))
  7733. ((null member) (or head (car org-todo-keywords-1)))
  7734. ((equal this final-done-word) nil) ;; -> make empty
  7735. ((null tail) nil) ;; -> first entry
  7736. ((memq interpret '(type priority))
  7737. (if (eq this-command last-command)
  7738. (car tail)
  7739. (if (> (length tail) 0)
  7740. (or done-word (car org-done-keywords))
  7741. nil)))
  7742. (t
  7743. (car tail))))
  7744. (state (or
  7745. (run-hook-with-args-until-success
  7746. 'org-todo-get-default-hook state last-state)
  7747. state))
  7748. (next (if state (concat " " state " ") " "))
  7749. (change-plist (list :type 'todo-state-change :from this :to state
  7750. :position startpos))
  7751. dolog now-done-p)
  7752. (when org-blocker-hook
  7753. (setq org-last-todo-state-is-todo
  7754. (not (member this org-done-keywords)))
  7755. (unless (save-excursion
  7756. (save-match-data
  7757. (run-hook-with-args-until-failure
  7758. 'org-blocker-hook change-plist)))
  7759. (if (interactive-p)
  7760. (error "TODO state change from %s to %s blocked" this state)
  7761. ;; fail silently
  7762. (message "TODO state change from %s to %s blocked" this state)
  7763. (throw 'exit nil))))
  7764. (store-match-data match-data)
  7765. (replace-match next t t)
  7766. (unless (pos-visible-in-window-p hl-pos)
  7767. (message "TODO state changed to %s" (org-trim next)))
  7768. (unless head
  7769. (setq head (org-get-todo-sequence-head state)
  7770. ass (assoc head org-todo-kwd-alist)
  7771. interpret (nth 1 ass)
  7772. done-word (nth 3 ass)
  7773. final-done-word (nth 4 ass)))
  7774. (when (memq arg '(nextset previousset))
  7775. (message "Keyword-Set %d/%d: %s"
  7776. (- (length org-todo-sets) -1
  7777. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7778. (length org-todo-sets)
  7779. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7780. (setq org-last-todo-state-is-todo
  7781. (not (member state org-done-keywords)))
  7782. (setq now-done-p (and (member state org-done-keywords)
  7783. (not (member this org-done-keywords))))
  7784. (and logging (org-local-logging logging))
  7785. (when (and (or org-todo-log-states org-log-done)
  7786. (not (memq arg '(nextset previousset))))
  7787. ;; we need to look at recording a time and note
  7788. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7789. (nth 2 (assoc this org-todo-log-states))))
  7790. (when (and state
  7791. (member state org-not-done-keywords)
  7792. (not (member this org-not-done-keywords)))
  7793. ;; This is now a todo state and was not one before
  7794. ;; If there was a CLOSED time stamp, get rid of it.
  7795. (org-add-planning-info nil nil 'closed))
  7796. (when (and now-done-p org-log-done)
  7797. ;; It is now done, and it was not done before
  7798. (org-add-planning-info 'closed (org-current-time))
  7799. (if (and (not dolog) (eq 'note org-log-done))
  7800. (org-add-log-setup 'done state this 'findpos 'note)))
  7801. (when (and state dolog)
  7802. ;; This is a non-nil state, and we need to log it
  7803. (org-add-log-setup 'state state this 'findpos dolog)))
  7804. ;; Fixup tag positioning
  7805. (org-todo-trigger-tag-changes state)
  7806. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7807. (when org-provide-todo-statistics
  7808. (org-update-parent-todo-statistics))
  7809. (run-hooks 'org-after-todo-state-change-hook)
  7810. (if (and arg (not (member state org-done-keywords)))
  7811. (setq head (org-get-todo-sequence-head state)))
  7812. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7813. ;; Do we need to trigger a repeat?
  7814. (when now-done-p
  7815. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7816. ;; This is for the agenda, take a snapshot of the headline.
  7817. (save-match-data
  7818. (setq org-agenda-headline-snapshot-before-repeat
  7819. (org-get-heading))))
  7820. (org-auto-repeat-maybe state))
  7821. ;; Fixup cursor location if close to the keyword
  7822. (if (and (outline-on-heading-p)
  7823. (not (bolp))
  7824. (save-excursion (beginning-of-line 1)
  7825. (looking-at org-todo-line-regexp))
  7826. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7827. (progn
  7828. (goto-char (or (match-end 2) (match-end 1)))
  7829. (just-one-space)))
  7830. (when org-trigger-hook
  7831. (save-excursion
  7832. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7833. (defun org-block-todo-from-children-or-siblings (change-plist)
  7834. "Block turning an entry into a TODO, using the hierarchy.
  7835. This checks whether the current task should be blocked from state
  7836. changes. Such blocking occurs when:
  7837. 1. The task has children which are not all in a completed state.
  7838. 2. A task has a parent with the property :ORDERED:, and there
  7839. are siblings prior to the current task with incomplete
  7840. status."
  7841. (catch 'dont-block
  7842. ;; If this is not a todo state change, or if this entry is already DONE,
  7843. ;; do not block
  7844. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7845. (member (plist-get change-plist :from)
  7846. (cons 'done org-done-keywords))
  7847. (member (plist-get change-plist :to)
  7848. (cons 'todo org-not-done-keywords)))
  7849. (throw 'dont-block t))
  7850. ;; If this task has children, and any are undone, it's blocked
  7851. (save-excursion
  7852. (org-back-to-heading t)
  7853. (let ((this-level (funcall outline-level)))
  7854. (outline-next-heading)
  7855. (let ((child-level (funcall outline-level)))
  7856. (while (and (not (eobp))
  7857. (> child-level this-level))
  7858. ;; this todo has children, check whether they are all
  7859. ;; completed
  7860. (if (and (not (org-entry-is-done-p))
  7861. (org-entry-is-todo-p))
  7862. (throw 'dont-block nil))
  7863. (outline-next-heading)
  7864. (setq child-level (funcall outline-level))))))
  7865. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7866. ;; any previous siblings are undone, it's blocked
  7867. (save-excursion
  7868. (org-back-to-heading t)
  7869. (when (save-excursion
  7870. (ignore-errors
  7871. (outline-up-heading 1)
  7872. (org-entry-get (point) "ORDERED")))
  7873. (let* ((this-level (funcall outline-level))
  7874. (current-level this-level))
  7875. (while (and (not (bobp))
  7876. (= current-level this-level))
  7877. (outline-previous-heading)
  7878. (setq current-level (funcall outline-level))
  7879. (if (= current-level this-level)
  7880. ;; this todo has children, check whether they are all
  7881. ;; completed
  7882. (if (and (not (org-entry-is-done-p))
  7883. (org-entry-is-todo-p))
  7884. (throw 'dont-block nil)))))))
  7885. t)) ; don't block
  7886. (defun org-toggle-ordered-property ()
  7887. "Toggle the ORDERED property of the current entry."
  7888. (interactive)
  7889. (save-excursion
  7890. (org-back-to-heading)
  7891. (if (org-entry-get nil "ORDERED")
  7892. (progn
  7893. (org-delete-property "ORDERED")
  7894. (message "Subtasks can be completed in arbitrary order or parallel"))
  7895. (org-entry-put nil "ORDERED" "t")
  7896. (message "Subtasks must be completed in sequence"))))
  7897. (defun org-block-todo-from-checkboxes (change-plist)
  7898. "Block turning an entry into a TODO, using checkboxes.
  7899. This checks whether the current task should be blocked from state
  7900. changes because there are uncheckd boxes in this entry."
  7901. (catch 'dont-block
  7902. ;; If this is not a todo state change, or if this entry is already DONE,
  7903. ;; do not block
  7904. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7905. (member (plist-get change-plist :from)
  7906. (cons 'done org-done-keywords))
  7907. (member (plist-get change-plist :to)
  7908. (cons 'todo org-not-done-keywords)))
  7909. (throw 'dont-block t))
  7910. ;; If this task has checkboxes that are not checked, it's blocked
  7911. (save-excursion
  7912. (org-back-to-heading t)
  7913. (let ((beg (point)) end)
  7914. (outline-next-heading)
  7915. (setq end (point))
  7916. (goto-char beg)
  7917. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7918. end t)
  7919. (throw 'dont-block nil))))
  7920. t)) ; do not block
  7921. (defun org-update-parent-todo-statistics ()
  7922. "Update any statistics cookie in the parent of the current headline."
  7923. (interactive)
  7924. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7925. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  7926. (catch 'exit
  7927. (save-excursion
  7928. (setq level (org-up-heading-safe))
  7929. (unless level
  7930. (throw 'exit nil))
  7931. (while (re-search-forward box-re (point-at-eol) t)
  7932. (setq cnt-all 0 cnt-done 0 cookie-present t)
  7933. (setq is-percent (match-end 2))
  7934. (save-match-data
  7935. (unless (outline-next-heading) (throw 'exit nil))
  7936. (while (looking-at org-todo-line-regexp)
  7937. (setq kwd (match-string 2))
  7938. (and kwd (setq cnt-all (1+ cnt-all)))
  7939. (and (member kwd org-done-keywords)
  7940. (setq cnt-done (1+ cnt-done)))
  7941. (condition-case nil
  7942. (org-forward-same-level 1)
  7943. (error (end-of-line 1)))))
  7944. (replace-match
  7945. (if is-percent
  7946. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7947. (format "[%d/%d]" cnt-done cnt-all))))
  7948. (when cookie-present
  7949. (run-hook-with-args 'org-after-todo-statistics-hook
  7950. cnt-done (- cnt-all cnt-done)))))))
  7951. (defvar org-after-todo-statistics-hook nil
  7952. "Hook that is called after a TODO statistics cookie has been updated.
  7953. Each function is called with two arguments: the number of not-done entries
  7954. and the number of done entries.
  7955. For example, the following function, when added to this hook, will switch
  7956. an entry to DONE when all children are done, and back to TODO when new
  7957. entries are set to a TODO status. Note that this hook is only called
  7958. when there is a statistics cookie in the headline!
  7959. (defun org-summary-todo (n-done n-not-done)
  7960. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7961. (let (org-log-done org-log-states) ; turn off logging
  7962. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7963. ")
  7964. (defun org-todo-trigger-tag-changes (state)
  7965. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7966. (let ((l org-todo-state-tags-triggers)
  7967. changes)
  7968. (when (or (not state) (equal state ""))
  7969. (setq changes (append changes (cdr (assoc "" l)))))
  7970. (when (and (stringp state) (> (length state) 0))
  7971. (setq changes (append changes (cdr (assoc state l)))))
  7972. (when (member state org-not-done-keywords)
  7973. (setq changes (append changes (cdr (assoc 'todo l)))))
  7974. (when (member state org-done-keywords)
  7975. (setq changes (append changes (cdr (assoc 'done l)))))
  7976. (dolist (c changes)
  7977. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7978. (defun org-local-logging (value)
  7979. "Get logging settings from a property VALUE."
  7980. (let* (words w a)
  7981. ;; directly set the variables, they are already local.
  7982. (setq org-log-done nil
  7983. org-log-repeat nil
  7984. org-todo-log-states nil)
  7985. (setq words (org-split-string value))
  7986. (while (setq w (pop words))
  7987. (cond
  7988. ((setq a (assoc w org-startup-options))
  7989. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7990. (set (nth 1 a) (nth 2 a))))
  7991. ((setq a (org-extract-log-state-settings w))
  7992. (and (member (car a) org-todo-keywords-1)
  7993. (push a org-todo-log-states)))))))
  7994. (defun org-get-todo-sequence-head (kwd)
  7995. "Return the head of the TODO sequence to which KWD belongs.
  7996. If KWD is not set, check if there is a text property remembering the
  7997. right sequence."
  7998. (let (p)
  7999. (cond
  8000. ((not kwd)
  8001. (or (get-text-property (point-at-bol) 'org-todo-head)
  8002. (progn
  8003. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8004. nil (point-at-eol)))
  8005. (get-text-property p 'org-todo-head))))
  8006. ((not (member kwd org-todo-keywords-1))
  8007. (car org-todo-keywords-1))
  8008. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8009. (defun org-fast-todo-selection ()
  8010. "Fast TODO keyword selection with single keys.
  8011. Returns the new TODO keyword, or nil if no state change should occur."
  8012. (let* ((fulltable org-todo-key-alist)
  8013. (done-keywords org-done-keywords) ;; needed for the faces.
  8014. (maxlen (apply 'max (mapcar
  8015. (lambda (x)
  8016. (if (stringp (car x)) (string-width (car x)) 0))
  8017. fulltable)))
  8018. (expert nil)
  8019. (fwidth (+ maxlen 3 1 3))
  8020. (ncol (/ (- (window-width) 4) fwidth))
  8021. tg cnt e c tbl
  8022. groups ingroup)
  8023. (save-excursion
  8024. (save-window-excursion
  8025. (if expert
  8026. (set-buffer (get-buffer-create " *Org todo*"))
  8027. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8028. (erase-buffer)
  8029. (org-set-local 'org-done-keywords done-keywords)
  8030. (setq tbl fulltable cnt 0)
  8031. (while (setq e (pop tbl))
  8032. (cond
  8033. ((equal e '(:startgroup))
  8034. (push '() groups) (setq ingroup t)
  8035. (when (not (= cnt 0))
  8036. (setq cnt 0)
  8037. (insert "\n"))
  8038. (insert "{ "))
  8039. ((equal e '(:endgroup))
  8040. (setq ingroup nil cnt 0)
  8041. (insert "}\n"))
  8042. (t
  8043. (setq tg (car e) c (cdr e))
  8044. (if ingroup (push tg (car groups)))
  8045. (setq tg (org-add-props tg nil 'face
  8046. (org-get-todo-face tg)))
  8047. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8048. (insert "[" c "] " tg (make-string
  8049. (- fwidth 4 (length tg)) ?\ ))
  8050. (when (= (setq cnt (1+ cnt)) ncol)
  8051. (insert "\n")
  8052. (if ingroup (insert " "))
  8053. (setq cnt 0)))))
  8054. (insert "\n")
  8055. (goto-char (point-min))
  8056. (if (not expert) (org-fit-window-to-buffer))
  8057. (message "[a-z..]:Set [SPC]:clear")
  8058. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8059. (cond
  8060. ((or (= c ?\C-g)
  8061. (and (= c ?q) (not (rassoc c fulltable))))
  8062. (setq quit-flag t))
  8063. ((= c ?\ ) nil)
  8064. ((setq e (rassoc c fulltable) tg (car e))
  8065. tg)
  8066. (t (setq quit-flag t)))))))
  8067. (defun org-entry-is-todo-p ()
  8068. (member (org-get-todo-state) org-not-done-keywords))
  8069. (defun org-entry-is-done-p ()
  8070. (member (org-get-todo-state) org-done-keywords))
  8071. (defun org-get-todo-state ()
  8072. (save-excursion
  8073. (org-back-to-heading t)
  8074. (and (looking-at org-todo-line-regexp)
  8075. (match-end 2)
  8076. (match-string 2))))
  8077. (defun org-at-date-range-p (&optional inactive-ok)
  8078. "Is the cursor inside a date range?"
  8079. (interactive)
  8080. (save-excursion
  8081. (catch 'exit
  8082. (let ((pos (point)))
  8083. (skip-chars-backward "^[<\r\n")
  8084. (skip-chars-backward "<[")
  8085. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8086. (>= (match-end 0) pos)
  8087. (throw 'exit t))
  8088. (skip-chars-backward "^<[\r\n")
  8089. (skip-chars-backward "<[")
  8090. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8091. (>= (match-end 0) pos)
  8092. (throw 'exit t)))
  8093. nil)))
  8094. (defun org-get-repeat ()
  8095. "Check if there is a deadline/schedule with repeater in this entry."
  8096. (save-match-data
  8097. (save-excursion
  8098. (org-back-to-heading t)
  8099. (if (re-search-forward
  8100. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8101. (match-string 1)))))
  8102. (defvar org-last-changed-timestamp)
  8103. (defvar org-last-inserted-timestamp)
  8104. (defvar org-log-post-message)
  8105. (defvar org-log-note-purpose)
  8106. (defvar org-log-note-how)
  8107. (defvar org-log-note-extra)
  8108. (defun org-auto-repeat-maybe (done-word)
  8109. "Check if the current headline contains a repeated deadline/schedule.
  8110. If yes, set TODO state back to what it was and change the base date
  8111. of repeating deadline/scheduled time stamps to new date.
  8112. This function is run automatically after each state change to a DONE state."
  8113. ;; last-state is dynamically scoped into this function
  8114. (let* ((repeat (org-get-repeat))
  8115. (aa (assoc last-state org-todo-kwd-alist))
  8116. (interpret (nth 1 aa))
  8117. (head (nth 2 aa))
  8118. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8119. (msg "Entry repeats: ")
  8120. (org-log-done nil)
  8121. (org-todo-log-states nil)
  8122. (nshiftmax 10) (nshift 0)
  8123. re type n what ts time)
  8124. (when repeat
  8125. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8126. (org-todo (if (eq interpret 'type) last-state head))
  8127. (when org-log-repeat
  8128. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8129. (memq 'org-add-log-note post-command-hook))
  8130. ;; OK, we are already setup for some record
  8131. (if (eq org-log-repeat 'note)
  8132. ;; make sure we take a note, not only a time stamp
  8133. (setq org-log-note-how 'note))
  8134. ;; Set up for taking a record
  8135. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8136. last-state
  8137. 'findpos org-log-repeat)))
  8138. (org-back-to-heading t)
  8139. (org-add-planning-info nil nil 'closed)
  8140. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8141. org-deadline-time-regexp "\\)\\|\\("
  8142. org-ts-regexp "\\)"))
  8143. (while (re-search-forward
  8144. re (save-excursion (outline-next-heading) (point)) t)
  8145. (setq type (if (match-end 1) org-scheduled-string
  8146. (if (match-end 3) org-deadline-string "Plain:"))
  8147. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8148. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8149. (setq n (string-to-number (match-string 2 ts))
  8150. what (match-string 3 ts))
  8151. (if (equal what "w") (setq n (* n 7) what "d"))
  8152. ;; Preparation, see if we need to modify the start date for the change
  8153. (when (match-end 1)
  8154. (setq time (save-match-data (org-time-string-to-time ts)))
  8155. (cond
  8156. ((equal (match-string 1 ts) ".")
  8157. ;; Shift starting date to today
  8158. (org-timestamp-change
  8159. (- (time-to-days (current-time)) (time-to-days time))
  8160. 'day))
  8161. ((equal (match-string 1 ts) "+")
  8162. (while (or (= nshift 0)
  8163. (<= (time-to-days time) (time-to-days (current-time))))
  8164. (when (= (incf nshift) nshiftmax)
  8165. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8166. (error "Abort")))
  8167. (org-timestamp-change n (cdr (assoc what whata)))
  8168. (org-at-timestamp-p t)
  8169. (setq ts (match-string 1))
  8170. (setq time (save-match-data (org-time-string-to-time ts))))
  8171. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8172. ;; rematch, so that we have everything in place for the real shift
  8173. (org-at-timestamp-p t)
  8174. (setq ts (match-string 1))
  8175. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8176. (org-timestamp-change n (cdr (assoc what whata)))
  8177. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8178. (setq org-log-post-message msg)
  8179. (message "%s" msg))))
  8180. (defun org-show-todo-tree (arg)
  8181. "Make a compact tree which shows all headlines marked with TODO.
  8182. The tree will show the lines where the regexp matches, and all higher
  8183. headlines above the match.
  8184. With a \\[universal-argument] prefix, also show the DONE entries.
  8185. With a numeric prefix N, construct a sparse tree for the Nth element
  8186. of `org-todo-keywords-1'."
  8187. (interactive "P")
  8188. (let ((case-fold-search nil)
  8189. (kwd-re
  8190. (cond ((null arg) org-not-done-regexp)
  8191. ((equal arg '(4))
  8192. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8193. (mapcar 'list org-todo-keywords-1))))
  8194. (concat "\\("
  8195. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8196. "\\)\\>")))
  8197. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8198. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8199. org-todo-keywords-1)))
  8200. (t (error "Invalid prefix argument: %s" arg)))))
  8201. (message "%d TODO entries found"
  8202. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8203. (defun org-deadline (&optional remove time)
  8204. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8205. With argument REMOVE, remove any deadline from the item.
  8206. When TIME is set, it should be an internal time specification, and the
  8207. scheduling will use the corresponding date."
  8208. (interactive "P")
  8209. (if remove
  8210. (progn
  8211. (org-remove-timestamp-with-keyword org-deadline-string)
  8212. (message "Item no longer has a deadline."))
  8213. (if (org-get-repeat)
  8214. (error "Cannot change deadline on task with repeater, please do that by hand")
  8215. (org-add-planning-info 'deadline time 'closed)
  8216. (message "Deadline on %s" org-last-inserted-timestamp))))
  8217. (defun org-schedule (&optional remove time)
  8218. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8219. With argument REMOVE, remove any scheduling date from the item.
  8220. When TIME is set, it should be an internal time specification, and the
  8221. scheduling will use the corresponding date."
  8222. (interactive "P")
  8223. (if remove
  8224. (progn
  8225. (org-remove-timestamp-with-keyword org-scheduled-string)
  8226. (message "Item is no longer scheduled."))
  8227. (if (org-get-repeat)
  8228. (error "Cannot reschedule task with repeater, please do that by hand")
  8229. (org-add-planning-info 'scheduled time 'closed)
  8230. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8231. (defun org-get-scheduled-time (pom &optional inherit)
  8232. "Get the scheduled time as a time tuple, of a format suitable
  8233. for calling org-schedule with, or if there is no scheduling,
  8234. returns nil."
  8235. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8236. (when time
  8237. (apply 'encode-time (org-parse-time-string time)))))
  8238. (defun org-get-deadline-time (pom &optional inherit)
  8239. "Get the deadine as a time tuple, of a format suitable for
  8240. calling org-deadlin with, or if there is no scheduling, returns
  8241. nil."
  8242. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8243. (when time
  8244. (apply 'encode-time (org-parse-time-string time)))))
  8245. (defun org-remove-timestamp-with-keyword (keyword)
  8246. "Remove all time stamps with KEYWORD in the current entry."
  8247. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8248. beg)
  8249. (save-excursion
  8250. (org-back-to-heading t)
  8251. (setq beg (point))
  8252. (org-end-of-subtree t t)
  8253. (while (re-search-backward re beg t)
  8254. (replace-match "")
  8255. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8256. (equal (char-before) ?\ ))
  8257. (backward-delete-char 1)
  8258. (if (string-match "^[ \t]*$" (buffer-substring
  8259. (point-at-bol) (point-at-eol)))
  8260. (delete-region (point-at-bol)
  8261. (min (point-max) (1+ (point-at-eol))))))))))
  8262. (defun org-add-planning-info (what &optional time &rest remove)
  8263. "Insert new timestamp with keyword in the line directly after the headline.
  8264. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8265. If non is given, the user is prompted for a date.
  8266. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8267. be removed."
  8268. (interactive)
  8269. (let (org-time-was-given org-end-time-was-given ts
  8270. end default-time default-input)
  8271. (when (and (not time) (memq what '(scheduled deadline)))
  8272. ;; Try to get a default date/time from existing timestamp
  8273. (save-excursion
  8274. (org-back-to-heading t)
  8275. (setq end (save-excursion (outline-next-heading) (point)))
  8276. (when (re-search-forward (if (eq what 'scheduled)
  8277. org-scheduled-time-regexp
  8278. org-deadline-time-regexp)
  8279. end t)
  8280. (setq ts (match-string 1)
  8281. default-time
  8282. (apply 'encode-time (org-parse-time-string ts))
  8283. default-input (and ts (org-get-compact-tod ts))))))
  8284. (when what
  8285. ;; If necessary, get the time from the user
  8286. (setq time (or time (org-read-date nil 'to-time nil nil
  8287. default-time default-input))))
  8288. (when (and org-insert-labeled-timestamps-at-point
  8289. (member what '(scheduled deadline)))
  8290. (insert
  8291. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8292. (org-insert-time-stamp time org-time-was-given
  8293. nil nil nil (list org-end-time-was-given))
  8294. (setq what nil))
  8295. (save-excursion
  8296. (save-restriction
  8297. (let (col list elt ts buffer-invisibility-spec)
  8298. (org-back-to-heading t)
  8299. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8300. (goto-char (match-end 1))
  8301. (setq col (current-column))
  8302. (goto-char (match-end 0))
  8303. (if (eobp) (insert "\n") (forward-char 1))
  8304. (if (and (not (looking-at outline-regexp))
  8305. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8306. "[^\r\n]*"))
  8307. (not (equal (match-string 1) org-clock-string)))
  8308. (narrow-to-region (match-beginning 0) (match-end 0))
  8309. (insert-before-markers "\n")
  8310. (backward-char 1)
  8311. (narrow-to-region (point) (point))
  8312. (and org-adapt-indentation (org-indent-to-column col)))
  8313. ;; Check if we have to remove something.
  8314. (setq list (cons what remove))
  8315. (while list
  8316. (setq elt (pop list))
  8317. (goto-char (point-min))
  8318. (when (or (and (eq elt 'scheduled)
  8319. (re-search-forward org-scheduled-time-regexp nil t))
  8320. (and (eq elt 'deadline)
  8321. (re-search-forward org-deadline-time-regexp nil t))
  8322. (and (eq elt 'closed)
  8323. (re-search-forward org-closed-time-regexp nil t)))
  8324. (replace-match "")
  8325. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8326. (if (looking-at " +") (replace-match ""))))
  8327. (goto-char (point-max))
  8328. (when what
  8329. (insert
  8330. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8331. (cond ((eq what 'scheduled) org-scheduled-string)
  8332. ((eq what 'deadline) org-deadline-string)
  8333. ((eq what 'closed) org-closed-string))
  8334. " ")
  8335. (setq ts (org-insert-time-stamp
  8336. time
  8337. (or org-time-was-given
  8338. (and (eq what 'closed) org-log-done-with-time))
  8339. (eq what 'closed)
  8340. nil nil (list org-end-time-was-given)))
  8341. (end-of-line 1))
  8342. (goto-char (point-min))
  8343. (widen)
  8344. (if (and (looking-at "[ \t]+\n")
  8345. (equal (char-before) ?\n))
  8346. (delete-region (1- (point)) (point-at-eol)))
  8347. ts)))))
  8348. (defvar org-log-note-marker (make-marker))
  8349. (defvar org-log-note-purpose nil)
  8350. (defvar org-log-note-state nil)
  8351. (defvar org-log-note-previous-state nil)
  8352. (defvar org-log-note-how nil)
  8353. (defvar org-log-note-extra nil)
  8354. (defvar org-log-note-window-configuration nil)
  8355. (defvar org-log-note-return-to (make-marker))
  8356. (defvar org-log-post-message nil
  8357. "Message to be displayed after a log note has been stored.
  8358. The auto-repeater uses this.")
  8359. (defun org-add-note ()
  8360. "Add a note to the current entry.
  8361. This is done in the same way as adding a state change note."
  8362. (interactive)
  8363. (org-add-log-setup 'note nil nil 'findpos nil))
  8364. (defvar org-property-end-re)
  8365. (defun org-add-log-setup (&optional purpose state prev-state
  8366. findpos how &optional extra)
  8367. "Set up the post command hook to take a note.
  8368. If this is about to TODO state change, the new state is expected in STATE.
  8369. When FINDPOS is non-nil, find the correct position for the note in
  8370. the current entry. If not, assume that it can be inserted at point.
  8371. HOW is an indicator what kind of note should be created.
  8372. EXTRA is additional text that will be inserted into the notes buffer."
  8373. (let ((drawer (cond ((stringp org-log-into-drawer)
  8374. org-log-into-drawer)
  8375. (org-log-into-drawer "LOGBOOK")
  8376. (t nil))))
  8377. (save-restriction
  8378. (save-excursion
  8379. (when findpos
  8380. (org-back-to-heading t)
  8381. (narrow-to-region (point) (save-excursion
  8382. (outline-next-heading) (point)))
  8383. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8384. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8385. "[^\r\n]*\\)?"))
  8386. (goto-char (match-end 0))
  8387. (cond
  8388. (drawer
  8389. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8390. nil t)
  8391. (progn
  8392. (goto-char (match-end 0))
  8393. (or org-log-states-order-reversed
  8394. (and (re-search-forward org-property-end-re nil t)
  8395. (goto-char (1- (match-beginning 0))))))
  8396. (insert "\n:" drawer ":\n:END:")
  8397. (beginning-of-line 0)
  8398. (org-indent-line-function)
  8399. (beginning-of-line 2)
  8400. (org-indent-line-function)
  8401. (end-of-line 0)))
  8402. ((and org-log-state-notes-insert-after-drawers
  8403. (save-excursion
  8404. (forward-line) (looking-at org-drawer-regexp)))
  8405. (forward-line)
  8406. (while (looking-at org-drawer-regexp)
  8407. (goto-char (match-end 0))
  8408. (re-search-forward org-property-end-re (point-max) t)
  8409. (forward-line))
  8410. (forward-line -1)))
  8411. (unless org-log-states-order-reversed
  8412. (and (= (char-after) ?\n) (forward-char 1))
  8413. (org-skip-over-state-notes)
  8414. (skip-chars-backward " \t\n\r")))
  8415. (move-marker org-log-note-marker (point))
  8416. (setq org-log-note-purpose purpose
  8417. org-log-note-state state
  8418. org-log-note-previous-state prev-state
  8419. org-log-note-how how
  8420. org-log-note-extra extra)
  8421. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8422. (defun org-skip-over-state-notes ()
  8423. "Skip past the list of State notes in an entry."
  8424. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8425. (while (looking-at "[ \t]*- State")
  8426. (condition-case nil
  8427. (org-next-item)
  8428. (error (org-end-of-item)))))
  8429. (defun org-add-log-note (&optional purpose)
  8430. "Pop up a window for taking a note, and add this note later at point."
  8431. (remove-hook 'post-command-hook 'org-add-log-note)
  8432. (setq org-log-note-window-configuration (current-window-configuration))
  8433. (delete-other-windows)
  8434. (move-marker org-log-note-return-to (point))
  8435. (switch-to-buffer (marker-buffer org-log-note-marker))
  8436. (goto-char org-log-note-marker)
  8437. (org-switch-to-buffer-other-window "*Org Note*")
  8438. (erase-buffer)
  8439. (if (memq org-log-note-how '(time state))
  8440. (let (current-prefix-arg) (org-store-log-note))
  8441. (let ((org-inhibit-startup t)) (org-mode))
  8442. (insert (format "# Insert note for %s.
  8443. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8444. (cond
  8445. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8446. ((eq org-log-note-purpose 'done) "closed todo item")
  8447. ((eq org-log-note-purpose 'state)
  8448. (format "state change from \"%s\" to \"%s\""
  8449. (or org-log-note-previous-state "")
  8450. (or org-log-note-state "")))
  8451. ((eq org-log-note-purpose 'note)
  8452. "this entry")
  8453. (t (error "This should not happen")))))
  8454. (if org-log-note-extra (insert org-log-note-extra))
  8455. (org-set-local 'org-finish-function 'org-store-log-note)))
  8456. (defvar org-note-abort nil) ; dynamically scoped
  8457. (defun org-store-log-note ()
  8458. "Finish taking a log note, and insert it to where it belongs."
  8459. (let ((txt (buffer-string))
  8460. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8461. lines ind)
  8462. (kill-buffer (current-buffer))
  8463. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8464. (setq txt (replace-match "" t t txt)))
  8465. (if (string-match "\\s-+\\'" txt)
  8466. (setq txt (replace-match "" t t txt)))
  8467. (setq lines (org-split-string txt "\n"))
  8468. (when (and note (string-match "\\S-" note))
  8469. (setq note
  8470. (org-replace-escapes
  8471. note
  8472. (list (cons "%u" (user-login-name))
  8473. (cons "%U" user-full-name)
  8474. (cons "%t" (format-time-string
  8475. (org-time-stamp-format 'long 'inactive)
  8476. (current-time)))
  8477. (cons "%s" (if org-log-note-state
  8478. (concat "\"" org-log-note-state "\"")
  8479. ""))
  8480. (cons "%S" (if org-log-note-previous-state
  8481. (concat "\"" org-log-note-previous-state "\"")
  8482. "\"\"")))))
  8483. (if lines (setq note (concat note " \\\\")))
  8484. (push note lines))
  8485. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8486. (when lines
  8487. (save-excursion
  8488. (set-buffer (marker-buffer org-log-note-marker))
  8489. (save-excursion
  8490. (goto-char org-log-note-marker)
  8491. (move-marker org-log-note-marker nil)
  8492. (end-of-line 1)
  8493. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8494. (insert "- " (pop lines))
  8495. (org-indent-line-function)
  8496. (beginning-of-line 1)
  8497. (looking-at "[ \t]*")
  8498. (setq ind (concat (match-string 0) " "))
  8499. (end-of-line 1)
  8500. (while lines (insert "\n" ind (pop lines)))
  8501. (message "Note stored")
  8502. (org-back-to-heading t)
  8503. (org-cycle-hide-drawers 'children)))))
  8504. (set-window-configuration org-log-note-window-configuration)
  8505. (with-current-buffer (marker-buffer org-log-note-return-to)
  8506. (goto-char org-log-note-return-to))
  8507. (move-marker org-log-note-return-to nil)
  8508. (and org-log-post-message (message "%s" org-log-post-message)))
  8509. (defun org-sparse-tree (&optional arg)
  8510. "Create a sparse tree, prompt for the details.
  8511. This command can create sparse trees. You first need to select the type
  8512. of match used to create the tree:
  8513. t Show entries with a specific TODO keyword.
  8514. T Show entries selected by a tags match.
  8515. p Enter a property name and its value (both with completion on existing
  8516. names/values) and show entries with that property.
  8517. r Show entries matching a regular expression
  8518. d Show deadlines due within `org-deadline-warning-days'."
  8519. (interactive "P")
  8520. (let (ans kwd value)
  8521. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8522. (setq ans (read-char-exclusive))
  8523. (cond
  8524. ((equal ans ?d)
  8525. (call-interactively 'org-check-deadlines))
  8526. ((equal ans ?b)
  8527. (call-interactively 'org-check-before-date))
  8528. ((equal ans ?t)
  8529. (org-show-todo-tree '(4)))
  8530. ((equal ans ?T)
  8531. (call-interactively 'org-tags-sparse-tree))
  8532. ((member ans '(?p ?P))
  8533. (setq kwd (org-ido-completing-read "Property: "
  8534. (mapcar 'list (org-buffer-property-keys))))
  8535. (setq value (org-ido-completing-read "Value: "
  8536. (mapcar 'list (org-property-values kwd))))
  8537. (unless (string-match "\\`{.*}\\'" value)
  8538. (setq value (concat "\"" value "\"")))
  8539. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8540. ((member ans '(?r ?R ?/))
  8541. (call-interactively 'org-occur))
  8542. (t (error "No such sparse tree command \"%c\"" ans)))))
  8543. (defvar org-occur-highlights nil
  8544. "List of overlays used for occur matches.")
  8545. (make-variable-buffer-local 'org-occur-highlights)
  8546. (defvar org-occur-parameters nil
  8547. "Parameters of the active org-occur calls.
  8548. This is a list, each call to org-occur pushes as cons cell,
  8549. containing the regular expression and the callback, onto the list.
  8550. The list can contain several entries if `org-occur' has been called
  8551. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8552. will only contain one set of parameters. When the highlights are
  8553. removed (for example with `C-c C-c', or with the next edit (depending
  8554. on `org-remove-highlights-with-change'), this variable is emptied
  8555. as well.")
  8556. (make-variable-buffer-local 'org-occur-parameters)
  8557. (defun org-occur (regexp &optional keep-previous callback)
  8558. "Make a compact tree which shows all matches of REGEXP.
  8559. The tree will show the lines where the regexp matches, and all higher
  8560. headlines above the match. It will also show the heading after the match,
  8561. to make sure editing the matching entry is easy.
  8562. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8563. call to `org-occur' will be kept, to allow stacking of calls to this
  8564. command.
  8565. If CALLBACK is non-nil, it is a function which is called to confirm
  8566. that the match should indeed be shown."
  8567. (interactive "sRegexp: \nP")
  8568. (unless keep-previous
  8569. (org-remove-occur-highlights nil nil t))
  8570. (push (cons regexp callback) org-occur-parameters)
  8571. (let ((cnt 0))
  8572. (save-excursion
  8573. (goto-char (point-min))
  8574. (if (or (not keep-previous) ; do not want to keep
  8575. (not org-occur-highlights)) ; no previous matches
  8576. ;; hide everything
  8577. (org-overview))
  8578. (while (re-search-forward regexp nil t)
  8579. (when (or (not callback)
  8580. (save-match-data (funcall callback)))
  8581. (setq cnt (1+ cnt))
  8582. (when org-highlight-sparse-tree-matches
  8583. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8584. (org-show-context 'occur-tree))))
  8585. (when org-remove-highlights-with-change
  8586. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8587. nil 'local))
  8588. (unless org-sparse-tree-open-archived-trees
  8589. (org-hide-archived-subtrees (point-min) (point-max)))
  8590. (run-hooks 'org-occur-hook)
  8591. (if (interactive-p)
  8592. (message "%d match(es) for regexp %s" cnt regexp))
  8593. cnt))
  8594. (defun org-show-context (&optional key)
  8595. "Make sure point and context and visible.
  8596. How much context is shown depends upon the variables
  8597. `org-show-hierarchy-above', `org-show-following-heading'. and
  8598. `org-show-siblings'."
  8599. (let ((heading-p (org-on-heading-p t))
  8600. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8601. (following-p (org-get-alist-option org-show-following-heading key))
  8602. (entry-p (org-get-alist-option org-show-entry-below key))
  8603. (siblings-p (org-get-alist-option org-show-siblings key)))
  8604. (catch 'exit
  8605. ;; Show heading or entry text
  8606. (if (and heading-p (not entry-p))
  8607. (org-flag-heading nil) ; only show the heading
  8608. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8609. (org-show-hidden-entry))) ; show entire entry
  8610. (when following-p
  8611. ;; Show next sibling, or heading below text
  8612. (save-excursion
  8613. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8614. (org-flag-heading nil))))
  8615. (when siblings-p (org-show-siblings))
  8616. (when hierarchy-p
  8617. ;; show all higher headings, possibly with siblings
  8618. (save-excursion
  8619. (while (and (condition-case nil
  8620. (progn (org-up-heading-all 1) t)
  8621. (error nil))
  8622. (not (bobp)))
  8623. (org-flag-heading nil)
  8624. (when siblings-p (org-show-siblings))))))))
  8625. (defun org-reveal (&optional siblings)
  8626. "Show current entry, hierarchy above it, and the following headline.
  8627. This can be used to show a consistent set of context around locations
  8628. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8629. not t for the search context.
  8630. With optional argument SIBLINGS, on each level of the hierarchy all
  8631. siblings are shown. This repairs the tree structure to what it would
  8632. look like when opened with hierarchical calls to `org-cycle'."
  8633. (interactive "P")
  8634. (let ((org-show-hierarchy-above t)
  8635. (org-show-following-heading t)
  8636. (org-show-siblings (if siblings t org-show-siblings)))
  8637. (org-show-context nil)))
  8638. (defun org-highlight-new-match (beg end)
  8639. "Highlight from BEG to END and mark the highlight is an occur headline."
  8640. (let ((ov (org-make-overlay beg end)))
  8641. (org-overlay-put ov 'face 'secondary-selection)
  8642. (push ov org-occur-highlights)))
  8643. (defun org-remove-occur-highlights (&optional beg end noremove)
  8644. "Remove the occur highlights from the buffer.
  8645. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8646. from the `before-change-functions' in the current buffer."
  8647. (interactive)
  8648. (unless org-inhibit-highlight-removal
  8649. (mapc 'org-delete-overlay org-occur-highlights)
  8650. (setq org-occur-highlights nil)
  8651. (setq org-occur-parameters nil)
  8652. (unless noremove
  8653. (remove-hook 'before-change-functions
  8654. 'org-remove-occur-highlights 'local))))
  8655. ;;;; Priorities
  8656. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8657. "Regular expression matching the priority indicator.")
  8658. (defvar org-remove-priority-next-time nil)
  8659. (defun org-priority-up ()
  8660. "Increase the priority of the current item."
  8661. (interactive)
  8662. (org-priority 'up))
  8663. (defun org-priority-down ()
  8664. "Decrease the priority of the current item."
  8665. (interactive)
  8666. (org-priority 'down))
  8667. (defun org-priority (&optional action)
  8668. "Change the priority of an item by ARG.
  8669. ACTION can be `set', `up', `down', or a character."
  8670. (interactive)
  8671. (setq action (or action 'set))
  8672. (let (current new news have remove)
  8673. (save-excursion
  8674. (org-back-to-heading)
  8675. (if (looking-at org-priority-regexp)
  8676. (setq current (string-to-char (match-string 2))
  8677. have t)
  8678. (setq current org-default-priority))
  8679. (cond
  8680. ((or (eq action 'set)
  8681. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8682. (if (not (eq action 'set))
  8683. (setq new action)
  8684. (message "Priority %c-%c, SPC to remove: "
  8685. org-highest-priority org-lowest-priority)
  8686. (setq new (read-char-exclusive)))
  8687. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8688. (= (upcase org-lowest-priority) org-lowest-priority))
  8689. (setq new (upcase new)))
  8690. (cond ((equal new ?\ ) (setq remove t))
  8691. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8692. (error "Priority must be between `%c' and `%c'"
  8693. org-highest-priority org-lowest-priority))))
  8694. ((eq action 'up)
  8695. (if (and (not have) (eq last-command this-command))
  8696. (setq new org-lowest-priority)
  8697. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8698. org-default-priority (1- current)))))
  8699. ((eq action 'down)
  8700. (if (and (not have) (eq last-command this-command))
  8701. (setq new org-highest-priority)
  8702. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8703. org-default-priority (1+ current)))))
  8704. (t (error "Invalid action")))
  8705. (if (or (< (upcase new) org-highest-priority)
  8706. (> (upcase new) org-lowest-priority))
  8707. (setq remove t))
  8708. (setq news (format "%c" new))
  8709. (if have
  8710. (if remove
  8711. (replace-match "" t t nil 1)
  8712. (replace-match news t t nil 2))
  8713. (if remove
  8714. (error "No priority cookie found in line")
  8715. (looking-at org-todo-line-regexp)
  8716. (if (match-end 2)
  8717. (progn
  8718. (goto-char (match-end 2))
  8719. (insert " [#" news "]"))
  8720. (goto-char (match-beginning 3))
  8721. (insert "[#" news "] ")))))
  8722. (org-preserve-lc (org-set-tags nil 'align))
  8723. (if remove
  8724. (message "Priority removed")
  8725. (message "Priority of current item set to %s" news))))
  8726. (defun org-get-priority (s)
  8727. "Find priority cookie and return priority."
  8728. (save-match-data
  8729. (if (not (string-match org-priority-regexp s))
  8730. (* 1000 (- org-lowest-priority org-default-priority))
  8731. (* 1000 (- org-lowest-priority
  8732. (string-to-char (match-string 2 s)))))))
  8733. ;;;; Tags
  8734. (defvar org-agenda-archives-mode)
  8735. (defun org-scan-tags (action matcher &optional todo-only)
  8736. "Scan headline tags with inheritance and produce output ACTION.
  8737. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8738. or `agenda' to produce an entry list for an agenda view. It can also be
  8739. a Lisp form or a function that should be called at each matched headline, in
  8740. this case the return value is a list of all return values from these calls.
  8741. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8742. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8743. only lines with a TODO keyword are included in the output."
  8744. (require 'org-agenda)
  8745. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8746. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8747. (org-re
  8748. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8749. (props (list 'face 'default
  8750. 'done-face 'org-done
  8751. 'undone-face 'default
  8752. 'mouse-face 'highlight
  8753. 'org-not-done-regexp org-not-done-regexp
  8754. 'org-todo-regexp org-todo-regexp
  8755. 'keymap org-agenda-keymap
  8756. 'help-echo
  8757. (format "mouse-2 or RET jump to org file %s"
  8758. (abbreviate-file-name
  8759. (or (buffer-file-name (buffer-base-buffer))
  8760. (buffer-name (buffer-base-buffer)))))))
  8761. (case-fold-search nil)
  8762. lspos tags tags-list
  8763. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8764. (llast 0) rtn rtn1 level category i txt
  8765. todo marker entry priority)
  8766. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8767. (setq action (list 'lambda nil action)))
  8768. (save-excursion
  8769. (goto-char (point-min))
  8770. (when (eq action 'sparse-tree)
  8771. (org-overview)
  8772. (org-remove-occur-highlights))
  8773. (while (re-search-forward re nil t)
  8774. (catch :skip
  8775. (setq todo (if (match-end 1) (match-string 2))
  8776. tags (if (match-end 4) (match-string 4)))
  8777. (goto-char (setq lspos (1+ (match-beginning 0))))
  8778. (setq level (org-reduced-level (funcall outline-level))
  8779. category (org-get-category))
  8780. (setq i llast llast level)
  8781. ;; remove tag lists from same and sublevels
  8782. (while (>= i level)
  8783. (when (setq entry (assoc i tags-alist))
  8784. (setq tags-alist (delete entry tags-alist)))
  8785. (setq i (1- i)))
  8786. ;; add the next tags
  8787. (when tags
  8788. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8789. tags-alist
  8790. (cons (cons level tags) tags-alist)))
  8791. ;; compile tags for current headline
  8792. (setq tags-list
  8793. (if org-use-tag-inheritance
  8794. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8795. tags))
  8796. (when org-use-tag-inheritance
  8797. (setcdr (car tags-alist)
  8798. (mapcar (lambda (x)
  8799. (setq x (copy-sequence x))
  8800. (org-add-prop-inherited x))
  8801. (cdar tags-alist))))
  8802. (when (and tags org-use-tag-inheritance
  8803. (not (eq t org-use-tag-inheritance)))
  8804. ;; selective inheritance, remove uninherited ones
  8805. (setcdr (car tags-alist)
  8806. (org-remove-uniherited-tags (cdar tags-alist))))
  8807. (when (and (or (not todo-only)
  8808. (and (member todo org-not-done-keywords)
  8809. (or (not org-agenda-tags-todo-honor-ignore-options)
  8810. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8811. (let ((case-fold-search t)) (eval matcher))
  8812. (or
  8813. (not (member org-archive-tag tags-list))
  8814. ;; we have an archive tag, should we use this anyway?
  8815. (or (not org-agenda-skip-archived-trees)
  8816. (and (eq action 'agenda) org-agenda-archives-mode))))
  8817. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8818. ;; select this headline
  8819. (cond
  8820. ((eq action 'sparse-tree)
  8821. (and org-highlight-sparse-tree-matches
  8822. (org-get-heading) (match-end 0)
  8823. (org-highlight-new-match
  8824. (match-beginning 0) (match-beginning 1)))
  8825. (org-show-context 'tags-tree))
  8826. ((eq action 'agenda)
  8827. (setq txt (org-format-agenda-item
  8828. ""
  8829. (concat
  8830. (if org-tags-match-list-sublevels
  8831. (make-string (1- level) ?.) "")
  8832. (org-get-heading))
  8833. category (org-get-tags-at))
  8834. priority (org-get-priority txt))
  8835. (goto-char lspos)
  8836. (setq marker (org-agenda-new-marker))
  8837. (org-add-props txt props
  8838. 'org-marker marker 'org-hd-marker marker 'org-category category
  8839. 'todo-state todo
  8840. 'priority priority 'type "tagsmatch")
  8841. (push txt rtn))
  8842. ((functionp action)
  8843. (save-excursion
  8844. (setq rtn1 (funcall action))
  8845. (push rtn1 rtn))
  8846. (goto-char (point-at-eol)))
  8847. (t (error "Invalid action")))
  8848. ;; if we are to skip sublevels, jump to end of subtree
  8849. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8850. (when (and (eq action 'sparse-tree)
  8851. (not org-sparse-tree-open-archived-trees))
  8852. (org-hide-archived-subtrees (point-min) (point-max)))
  8853. (nreverse rtn)))
  8854. (defun org-remove-uniherited-tags (tags)
  8855. "Remove all tags that are not inherited from the list TAGS."
  8856. (cond
  8857. ((eq org-use-tag-inheritance t)
  8858. (if org-tags-exclude-from-inheritance
  8859. (org-delete-all org-tags-exclude-from-inheritance tags)
  8860. tags))
  8861. ((not org-use-tag-inheritance) nil)
  8862. ((stringp org-use-tag-inheritance)
  8863. (delq nil (mapcar
  8864. (lambda (x)
  8865. (if (and (string-match org-use-tag-inheritance x)
  8866. (not (member x org-tags-exclude-from-inheritance)))
  8867. x nil))
  8868. tags)))
  8869. ((listp org-use-tag-inheritance)
  8870. (delq nil (mapcar
  8871. (lambda (x)
  8872. (if (member x org-use-tag-inheritance) x nil))
  8873. tags)))))
  8874. (defvar todo-only) ;; dynamically scoped
  8875. (defun org-tags-sparse-tree (&optional todo-only match)
  8876. "Create a sparse tree according to tags string MATCH.
  8877. MATCH can contain positive and negative selection of tags, like
  8878. \"+WORK+URGENT-WITHBOSS\".
  8879. If optional argument TODO-ONLY is non-nil, only select lines that are
  8880. also TODO lines."
  8881. (interactive "P")
  8882. (org-prepare-agenda-buffers (list (current-buffer)))
  8883. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8884. (defvar org-cached-props nil)
  8885. (defun org-cached-entry-get (pom property)
  8886. (if (or (eq t org-use-property-inheritance)
  8887. (and (stringp org-use-property-inheritance)
  8888. (string-match org-use-property-inheritance property))
  8889. (and (listp org-use-property-inheritance)
  8890. (member property org-use-property-inheritance)))
  8891. ;; Caching is not possible, check it directly
  8892. (org-entry-get pom property 'inherit)
  8893. ;; Get all properties, so that we can do complicated checks easily
  8894. (cdr (assoc property (or org-cached-props
  8895. (setq org-cached-props
  8896. (org-entry-properties pom)))))))
  8897. (defun org-global-tags-completion-table (&optional files)
  8898. "Return the list of all tags in all agenda buffer/files."
  8899. (save-excursion
  8900. (org-uniquify
  8901. (delq nil
  8902. (apply 'append
  8903. (mapcar
  8904. (lambda (file)
  8905. (set-buffer (find-file-noselect file))
  8906. (append (org-get-buffer-tags)
  8907. (mapcar (lambda (x) (if (stringp (car-safe x))
  8908. (list (car-safe x)) nil))
  8909. org-tag-alist)))
  8910. (if (and files (car files))
  8911. files
  8912. (org-agenda-files))))))))
  8913. (defun org-make-tags-matcher (match)
  8914. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8915. ;; todo-only is scoped dynamically into this function, and the function
  8916. ;; may change it if the matcher asks for it.
  8917. (unless match
  8918. ;; Get a new match request, with completion
  8919. (let ((org-last-tags-completion-table
  8920. (org-global-tags-completion-table)))
  8921. (setq match (org-completing-read
  8922. "Match: " 'org-tags-completion-function nil nil nil
  8923. 'org-tags-history))))
  8924. ;; Parse the string and create a lisp form
  8925. (let ((match0 match)
  8926. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8927. minus tag mm
  8928. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8929. orterms term orlist re-p str-p level-p level-op time-p
  8930. prop-p pn pv po cat-p gv rest)
  8931. (if (string-match "/+" match)
  8932. ;; match contains also a todo-matching request
  8933. (progn
  8934. (setq tagsmatch (substring match 0 (match-beginning 0))
  8935. todomatch (substring match (match-end 0)))
  8936. (if (string-match "^!" todomatch)
  8937. (setq todo-only t todomatch (substring todomatch 1)))
  8938. (if (string-match "^\\s-*$" todomatch)
  8939. (setq todomatch nil)))
  8940. ;; only matching tags
  8941. (setq tagsmatch match todomatch nil))
  8942. ;; Make the tags matcher
  8943. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8944. (setq tagsmatcher t)
  8945. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8946. (while (setq term (pop orterms))
  8947. (while (and (equal (substring term -1) "\\") orterms)
  8948. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8949. (while (string-match re term)
  8950. (setq rest (substring term (match-end 0))
  8951. minus (and (match-end 1)
  8952. (equal (match-string 1 term) "-"))
  8953. tag (match-string 2 term)
  8954. re-p (equal (string-to-char tag) ?{)
  8955. level-p (match-end 4)
  8956. prop-p (match-end 5)
  8957. mm (cond
  8958. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8959. (level-p
  8960. (setq level-op (org-op-to-function (match-string 3 term)))
  8961. `(,level-op level ,(string-to-number
  8962. (match-string 4 term))))
  8963. (prop-p
  8964. (setq pn (match-string 5 term)
  8965. po (match-string 6 term)
  8966. pv (match-string 7 term)
  8967. cat-p (equal pn "CATEGORY")
  8968. re-p (equal (string-to-char pv) ?{)
  8969. str-p (equal (string-to-char pv) ?\")
  8970. time-p (save-match-data
  8971. (string-match "^\"[[<].*[]>]\"$" pv))
  8972. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8973. (if time-p (setq pv (org-matcher-time pv)))
  8974. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8975. (cond
  8976. ((equal pn "CATEGORY")
  8977. (setq gv '(get-text-property (point) 'org-category)))
  8978. ((equal pn "TODO")
  8979. (setq gv 'todo))
  8980. (t
  8981. (setq gv `(org-cached-entry-get nil ,pn))))
  8982. (if re-p
  8983. (if (eq po 'org<>)
  8984. `(not (string-match ,pv (or ,gv "")))
  8985. `(string-match ,pv (or ,gv "")))
  8986. (if str-p
  8987. `(,po (or ,gv "") ,pv)
  8988. `(,po (string-to-number (or ,gv ""))
  8989. ,(string-to-number pv) ))))
  8990. (t `(member ,(downcase tag) tags-list)))
  8991. mm (if minus (list 'not mm) mm)
  8992. term rest)
  8993. (push mm tagsmatcher))
  8994. (push (if (> (length tagsmatcher) 1)
  8995. (cons 'and tagsmatcher)
  8996. (car tagsmatcher))
  8997. orlist)
  8998. (setq tagsmatcher nil))
  8999. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9000. (setq tagsmatcher
  9001. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9002. ;; Make the todo matcher
  9003. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9004. (setq todomatcher t)
  9005. (setq orterms (org-split-string todomatch "|") orlist nil)
  9006. (while (setq term (pop orterms))
  9007. (while (string-match re term)
  9008. (setq minus (and (match-end 1)
  9009. (equal (match-string 1 term) "-"))
  9010. kwd (match-string 2 term)
  9011. re-p (equal (string-to-char kwd) ?{)
  9012. term (substring term (match-end 0))
  9013. mm (if re-p
  9014. `(string-match ,(substring kwd 1 -1) todo)
  9015. (list 'equal 'todo kwd))
  9016. mm (if minus (list 'not mm) mm))
  9017. (push mm todomatcher))
  9018. (push (if (> (length todomatcher) 1)
  9019. (cons 'and todomatcher)
  9020. (car todomatcher))
  9021. orlist)
  9022. (setq todomatcher nil))
  9023. (setq todomatcher (if (> (length orlist) 1)
  9024. (cons 'or orlist) (car orlist))))
  9025. ;; Return the string and lisp forms of the matcher
  9026. (setq matcher (if todomatcher
  9027. (list 'and tagsmatcher todomatcher)
  9028. tagsmatcher))
  9029. (cons match0 matcher)))
  9030. (defun org-op-to-function (op &optional stringp)
  9031. "Turn an operator into the appropriate function."
  9032. (setq op
  9033. (cond
  9034. ((equal op "<" ) '(< string< org-time<))
  9035. ((equal op ">" ) '(> org-string> org-time>))
  9036. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9037. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9038. ((member op '("=" "==")) '(= string= org-time=))
  9039. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9040. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9041. (defun org<> (a b) (not (= a b)))
  9042. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9043. (defun org-string>= (a b) (not (string< a b)))
  9044. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9045. (defun org-string<> (a b) (not (string= a b)))
  9046. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9047. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9048. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9049. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9050. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9051. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9052. (defun org-2ft (s)
  9053. "Convert S to a floating point time.
  9054. If S is already a number, just return it. If it is a string, parse
  9055. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9056. (cond
  9057. ((numberp s) s)
  9058. ((stringp s)
  9059. (condition-case nil
  9060. (float-time (apply 'encode-time (org-parse-time-string s)))
  9061. (error 0.)))
  9062. (t 0.)))
  9063. (defun org-time-today ()
  9064. "Time in seconds today at 0:00.
  9065. Returns the float number of seconds since the beginning of the
  9066. epoch to the beginning of today (00:00)."
  9067. (float-time (apply 'encode-time
  9068. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9069. (defun org-matcher-time (s)
  9070. "Interpret a time comparison value."
  9071. (save-match-data
  9072. (cond
  9073. ((string= s "<now>") (float-time))
  9074. ((string= s "<today>") (org-time-today))
  9075. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9076. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9077. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9078. (+ (org-time-today)
  9079. (* (string-to-number (match-string 1 s))
  9080. (cdr (assoc (match-string 2 s)
  9081. '(("d" . 86400.0) ("w" . 604800.0)
  9082. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9083. (t (org-2ft s)))))
  9084. (defun org-match-any-p (re list)
  9085. "Does re match any element of list?"
  9086. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9087. (delq nil list))
  9088. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9089. (defvar org-tags-overlay (org-make-overlay 1 1))
  9090. (org-detach-overlay org-tags-overlay)
  9091. (defun org-get-local-tags-at (&optional pos)
  9092. "Get a list of tags defined in the current headline."
  9093. (org-get-tags-at pos 'local))
  9094. (defun org-get-local-tags ()
  9095. "Get a list of tags defined in the current headline."
  9096. (org-get-tags-at nil 'local))
  9097. (defun org-get-tags-at (&optional pos local)
  9098. "Get a list of all headline tags applicable at POS.
  9099. POS defaults to point. If tags are inherited, the list contains
  9100. the targets in the same sequence as the headlines appear, i.e.
  9101. the tags of the current headline come last.
  9102. When LOCAL is non-nil, only return tags from the current headline,
  9103. ignore inherited ones."
  9104. (interactive)
  9105. (let (tags ltags lastpos parent)
  9106. (save-excursion
  9107. (save-restriction
  9108. (widen)
  9109. (goto-char (or pos (point)))
  9110. (save-match-data
  9111. (catch 'done
  9112. (condition-case nil
  9113. (progn
  9114. (org-back-to-heading t)
  9115. (while (not (equal lastpos (point)))
  9116. (setq lastpos (point))
  9117. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9118. (setq ltags (org-split-string
  9119. (org-match-string-no-properties 1) ":"))
  9120. (when parent
  9121. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9122. (setq tags (append
  9123. (if parent
  9124. (org-remove-uniherited-tags ltags)
  9125. ltags)
  9126. tags)))
  9127. (or org-use-tag-inheritance (throw 'done t))
  9128. (if local (throw 'done t))
  9129. (org-up-heading-all 1)
  9130. (setq parent t)))
  9131. (error nil)))))
  9132. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9133. (defun org-add-prop-inherited (s)
  9134. (add-text-properties 0 (length s) '(inherited t) s)
  9135. s)
  9136. (defun org-toggle-tag (tag &optional onoff)
  9137. "Toggle the tag TAG for the current line.
  9138. If ONOFF is `on' or `off', don't toggle but set to this state."
  9139. (let (res current)
  9140. (save-excursion
  9141. (org-back-to-heading t)
  9142. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9143. (point-at-eol) t)
  9144. (progn
  9145. (setq current (match-string 1))
  9146. (replace-match ""))
  9147. (setq current ""))
  9148. (setq current (nreverse (org-split-string current ":")))
  9149. (cond
  9150. ((eq onoff 'on)
  9151. (setq res t)
  9152. (or (member tag current) (push tag current)))
  9153. ((eq onoff 'off)
  9154. (or (not (member tag current)) (setq current (delete tag current))))
  9155. (t (if (member tag current)
  9156. (setq current (delete tag current))
  9157. (setq res t)
  9158. (push tag current))))
  9159. (end-of-line 1)
  9160. (if current
  9161. (progn
  9162. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9163. (org-set-tags nil t))
  9164. (delete-horizontal-space))
  9165. (run-hooks 'org-after-tags-change-hook))
  9166. res))
  9167. (defun org-align-tags-here (to-col)
  9168. ;; Assumes that this is a headline
  9169. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9170. (beginning-of-line 1)
  9171. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9172. (< pos (match-beginning 2)))
  9173. (progn
  9174. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9175. (goto-char (match-beginning 1))
  9176. (insert " ")
  9177. (delete-region (point) (1+ (match-beginning 2)))
  9178. (setq ncol (max (1+ (current-column))
  9179. (1+ col)
  9180. (if (> to-col 0)
  9181. to-col
  9182. (- (abs to-col) tags-l))))
  9183. (setq p (point))
  9184. (insert (make-string (- ncol (current-column)) ?\ ))
  9185. (setq ncol (current-column))
  9186. (when indent-tabs-mode (tabify p (point-at-eol)))
  9187. (org-move-to-column (min ncol col) t))
  9188. (goto-char pos))))
  9189. (defun org-set-tags-command (&optional arg just-align)
  9190. "Call the set-tags command for the current entry."
  9191. (interactive "P")
  9192. (if (org-on-heading-p)
  9193. (org-set-tags arg just-align)
  9194. (save-excursion
  9195. (org-back-to-heading t)
  9196. (org-set-tags arg just-align))))
  9197. (defun org-set-tags (&optional arg just-align)
  9198. "Set the tags for the current headline.
  9199. With prefix ARG, realign all tags in headings in the current buffer."
  9200. (interactive "P")
  9201. (let* ((re (concat "^" outline-regexp))
  9202. (current (org-get-tags-string))
  9203. (col (current-column))
  9204. (org-setting-tags t)
  9205. table current-tags inherited-tags ; computed below when needed
  9206. tags p0 c0 c1 rpl)
  9207. (if arg
  9208. (save-excursion
  9209. (goto-char (point-min))
  9210. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9211. (while (re-search-forward re nil t)
  9212. (org-set-tags nil t)
  9213. (end-of-line 1)))
  9214. (message "All tags realigned to column %d" org-tags-column))
  9215. (if just-align
  9216. (setq tags current)
  9217. ;; Get a new set of tags from the user
  9218. (save-excursion
  9219. (setq table (or org-tag-alist (org-get-buffer-tags))
  9220. org-last-tags-completion-table table
  9221. current-tags (org-split-string current ":")
  9222. inherited-tags (nreverse
  9223. (nthcdr (length current-tags)
  9224. (nreverse (org-get-tags-at))))
  9225. tags
  9226. (if (or (eq t org-use-fast-tag-selection)
  9227. (and org-use-fast-tag-selection
  9228. (delq nil (mapcar 'cdr table))))
  9229. (org-fast-tag-selection
  9230. current-tags inherited-tags table
  9231. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9232. (let ((org-add-colon-after-tag-completion t))
  9233. (org-trim
  9234. (org-without-partial-completion
  9235. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9236. nil nil current 'org-tags-history)))))))
  9237. (while (string-match "[-+&]+" tags)
  9238. ;; No boolean logic, just a list
  9239. (setq tags (replace-match ":" t t tags))))
  9240. (if (string-match "\\`[\t ]*\\'" tags)
  9241. (setq tags "")
  9242. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9243. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9244. ;; Insert new tags at the correct column
  9245. (beginning-of-line 1)
  9246. (cond
  9247. ((and (equal current "") (equal tags "")))
  9248. ((re-search-forward
  9249. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9250. (point-at-eol) t)
  9251. (if (equal tags "")
  9252. (setq rpl "")
  9253. (goto-char (match-beginning 0))
  9254. (setq c0 (current-column) p0 (point)
  9255. c1 (max (1+ c0) (if (> org-tags-column 0)
  9256. org-tags-column
  9257. (- (- org-tags-column) (length tags))))
  9258. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9259. (replace-match rpl t t)
  9260. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9261. tags)
  9262. (t (error "Tags alignment failed")))
  9263. (org-move-to-column col)
  9264. (unless just-align
  9265. (run-hooks 'org-after-tags-change-hook)))))
  9266. (defun org-change-tag-in-region (beg end tag off)
  9267. "Add or remove TAG for each entry in the region.
  9268. This works in the agenda, and also in an org-mode buffer."
  9269. (interactive
  9270. (list (region-beginning) (region-end)
  9271. (let ((org-last-tags-completion-table
  9272. (if (org-mode-p)
  9273. (org-get-buffer-tags)
  9274. (org-global-tags-completion-table))))
  9275. (org-ido-completing-read
  9276. "Tag: " 'org-tags-completion-function nil nil nil
  9277. 'org-tags-history))
  9278. (progn
  9279. (message "[s]et or [r]emove? ")
  9280. (equal (read-char-exclusive) ?r))))
  9281. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9282. (let ((agendap (equal major-mode 'org-agenda-mode))
  9283. l1 l2 m buf pos newhead (cnt 0))
  9284. (goto-char end)
  9285. (setq l2 (1- (org-current-line)))
  9286. (goto-char beg)
  9287. (setq l1 (org-current-line))
  9288. (loop for l from l1 to l2 do
  9289. (goto-line l)
  9290. (setq m (get-text-property (point) 'org-hd-marker))
  9291. (when (or (and (org-mode-p) (org-on-heading-p))
  9292. (and agendap m))
  9293. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9294. pos (if agendap m (point)))
  9295. (with-current-buffer buf
  9296. (save-excursion
  9297. (save-restriction
  9298. (goto-char pos)
  9299. (setq cnt (1+ cnt))
  9300. (org-toggle-tag tag (if off 'off 'on))
  9301. (setq newhead (org-get-heading)))))
  9302. (and agendap (org-agenda-change-all-lines newhead m))))
  9303. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9304. (defun org-tags-completion-function (string predicate &optional flag)
  9305. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9306. (confirm (lambda (x) (stringp (car x)))))
  9307. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9308. (setq s1 (match-string 1 string)
  9309. s2 (match-string 2 string))
  9310. (setq s1 "" s2 string))
  9311. (cond
  9312. ((eq flag nil)
  9313. ;; try completion
  9314. (setq rtn (try-completion s2 ctable confirm))
  9315. (if (stringp rtn)
  9316. (setq rtn
  9317. (concat s1 s2 (substring rtn (length s2))
  9318. (if (and org-add-colon-after-tag-completion
  9319. (assoc rtn ctable))
  9320. ":" ""))))
  9321. rtn)
  9322. ((eq flag t)
  9323. ;; all-completions
  9324. (all-completions s2 ctable confirm)
  9325. )
  9326. ((eq flag 'lambda)
  9327. ;; exact match?
  9328. (assoc s2 ctable)))
  9329. ))
  9330. (defun org-fast-tag-insert (kwd tags face &optional end)
  9331. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9332. (insert (format "%-12s" (concat kwd ":"))
  9333. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9334. (or end "")))
  9335. (defun org-fast-tag-show-exit (flag)
  9336. (save-excursion
  9337. (goto-line 3)
  9338. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9339. (replace-match ""))
  9340. (when flag
  9341. (end-of-line 1)
  9342. (org-move-to-column (- (window-width) 19) t)
  9343. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9344. (defun org-set-current-tags-overlay (current prefix)
  9345. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9346. (if (featurep 'xemacs)
  9347. (org-overlay-display org-tags-overlay (concat prefix s)
  9348. 'secondary-selection)
  9349. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9350. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9351. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9352. "Fast tag selection with single keys.
  9353. CURRENT is the current list of tags in the headline, INHERITED is the
  9354. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9355. possibly with grouping information. TODO-TABLE is a similar table with
  9356. TODO keywords, should these have keys assigned to them.
  9357. If the keys are nil, a-z are automatically assigned.
  9358. Returns the new tags string, or nil to not change the current settings."
  9359. (let* ((fulltable (append table todo-table))
  9360. (maxlen (apply 'max (mapcar
  9361. (lambda (x)
  9362. (if (stringp (car x)) (string-width (car x)) 0))
  9363. fulltable)))
  9364. (buf (current-buffer))
  9365. (expert (eq org-fast-tag-selection-single-key 'expert))
  9366. (buffer-tags nil)
  9367. (fwidth (+ maxlen 3 1 3))
  9368. (ncol (/ (- (window-width) 4) fwidth))
  9369. (i-face 'org-done)
  9370. (c-face 'org-todo)
  9371. tg cnt e c char c1 c2 ntable tbl rtn
  9372. ov-start ov-end ov-prefix
  9373. (exit-after-next org-fast-tag-selection-single-key)
  9374. (done-keywords org-done-keywords)
  9375. groups ingroup)
  9376. (save-excursion
  9377. (beginning-of-line 1)
  9378. (if (looking-at
  9379. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9380. (setq ov-start (match-beginning 1)
  9381. ov-end (match-end 1)
  9382. ov-prefix "")
  9383. (setq ov-start (1- (point-at-eol))
  9384. ov-end (1+ ov-start))
  9385. (skip-chars-forward "^\n\r")
  9386. (setq ov-prefix
  9387. (concat
  9388. (buffer-substring (1- (point)) (point))
  9389. (if (> (current-column) org-tags-column)
  9390. " "
  9391. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9392. (org-move-overlay org-tags-overlay ov-start ov-end)
  9393. (save-window-excursion
  9394. (if expert
  9395. (set-buffer (get-buffer-create " *Org tags*"))
  9396. (delete-other-windows)
  9397. (split-window-vertically)
  9398. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9399. (erase-buffer)
  9400. (org-set-local 'org-done-keywords done-keywords)
  9401. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9402. (org-fast-tag-insert "Current" current c-face "\n\n")
  9403. (org-fast-tag-show-exit exit-after-next)
  9404. (org-set-current-tags-overlay current ov-prefix)
  9405. (setq tbl fulltable char ?a cnt 0)
  9406. (while (setq e (pop tbl))
  9407. (cond
  9408. ((equal e '(:startgroup))
  9409. (push '() groups) (setq ingroup t)
  9410. (when (not (= cnt 0))
  9411. (setq cnt 0)
  9412. (insert "\n"))
  9413. (insert "{ "))
  9414. ((equal e '(:endgroup))
  9415. (setq ingroup nil cnt 0)
  9416. (insert "}\n"))
  9417. (t
  9418. (setq tg (car e) c2 nil)
  9419. (if (cdr e)
  9420. (setq c (cdr e))
  9421. ;; automatically assign a character.
  9422. (setq c1 (string-to-char
  9423. (downcase (substring
  9424. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9425. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9426. (while (or (rassoc char ntable) (rassoc char table))
  9427. (setq char (1+ char)))
  9428. (setq c2 c1))
  9429. (setq c (or c2 char)))
  9430. (if ingroup (push tg (car groups)))
  9431. (setq tg (org-add-props tg nil 'face
  9432. (cond
  9433. ((not (assoc tg table))
  9434. (org-get-todo-face tg))
  9435. ((member tg current) c-face)
  9436. ((member tg inherited) i-face)
  9437. (t nil))))
  9438. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9439. (insert "[" c "] " tg (make-string
  9440. (- fwidth 4 (length tg)) ?\ ))
  9441. (push (cons tg c) ntable)
  9442. (when (= (setq cnt (1+ cnt)) ncol)
  9443. (insert "\n")
  9444. (if ingroup (insert " "))
  9445. (setq cnt 0)))))
  9446. (setq ntable (nreverse ntable))
  9447. (insert "\n")
  9448. (goto-char (point-min))
  9449. (if (not expert) (org-fit-window-to-buffer))
  9450. (setq rtn
  9451. (catch 'exit
  9452. (while t
  9453. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9454. (if groups " [!] no groups" " [!]groups")
  9455. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9456. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9457. (cond
  9458. ((= c ?\r) (throw 'exit t))
  9459. ((= c ?!)
  9460. (setq groups (not groups))
  9461. (goto-char (point-min))
  9462. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9463. ((= c ?\C-c)
  9464. (if (not expert)
  9465. (org-fast-tag-show-exit
  9466. (setq exit-after-next (not exit-after-next)))
  9467. (setq expert nil)
  9468. (delete-other-windows)
  9469. (split-window-vertically)
  9470. (org-switch-to-buffer-other-window " *Org tags*")
  9471. (org-fit-window-to-buffer)))
  9472. ((or (= c ?\C-g)
  9473. (and (= c ?q) (not (rassoc c ntable))))
  9474. (org-detach-overlay org-tags-overlay)
  9475. (setq quit-flag t))
  9476. ((= c ?\ )
  9477. (setq current nil)
  9478. (if exit-after-next (setq exit-after-next 'now)))
  9479. ((= c ?\t)
  9480. (condition-case nil
  9481. (setq tg (org-ido-completing-read
  9482. "Tag: "
  9483. (or buffer-tags
  9484. (with-current-buffer buf
  9485. (org-get-buffer-tags)))))
  9486. (quit (setq tg "")))
  9487. (when (string-match "\\S-" tg)
  9488. (add-to-list 'buffer-tags (list tg))
  9489. (if (member tg current)
  9490. (setq current (delete tg current))
  9491. (push tg current)))
  9492. (if exit-after-next (setq exit-after-next 'now)))
  9493. ((setq e (rassoc c todo-table) tg (car e))
  9494. (with-current-buffer buf
  9495. (save-excursion (org-todo tg)))
  9496. (if exit-after-next (setq exit-after-next 'now)))
  9497. ((setq e (rassoc c ntable) tg (car e))
  9498. (if (member tg current)
  9499. (setq current (delete tg current))
  9500. (loop for g in groups do
  9501. (if (member tg g)
  9502. (mapc (lambda (x)
  9503. (setq current (delete x current)))
  9504. g)))
  9505. (push tg current))
  9506. (if exit-after-next (setq exit-after-next 'now))))
  9507. ;; Create a sorted list
  9508. (setq current
  9509. (sort current
  9510. (lambda (a b)
  9511. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9512. (if (eq exit-after-next 'now) (throw 'exit t))
  9513. (goto-char (point-min))
  9514. (beginning-of-line 2)
  9515. (delete-region (point) (point-at-eol))
  9516. (org-fast-tag-insert "Current" current c-face)
  9517. (org-set-current-tags-overlay current ov-prefix)
  9518. (while (re-search-forward
  9519. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9520. (setq tg (match-string 1))
  9521. (add-text-properties
  9522. (match-beginning 1) (match-end 1)
  9523. (list 'face
  9524. (cond
  9525. ((member tg current) c-face)
  9526. ((member tg inherited) i-face)
  9527. (t (get-text-property (match-beginning 1) 'face))))))
  9528. (goto-char (point-min)))))
  9529. (org-detach-overlay org-tags-overlay)
  9530. (if rtn
  9531. (mapconcat 'identity current ":")
  9532. nil))))
  9533. (defun org-get-tags-string ()
  9534. "Get the TAGS string in the current headline."
  9535. (unless (org-on-heading-p t)
  9536. (error "Not on a heading"))
  9537. (save-excursion
  9538. (beginning-of-line 1)
  9539. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9540. (org-match-string-no-properties 1)
  9541. "")))
  9542. (defun org-get-tags ()
  9543. "Get the list of tags specified in the current headline."
  9544. (org-split-string (org-get-tags-string) ":"))
  9545. (defun org-get-buffer-tags ()
  9546. "Get a table of all tags used in the buffer, for completion."
  9547. (let (tags)
  9548. (save-excursion
  9549. (goto-char (point-min))
  9550. (while (re-search-forward
  9551. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9552. (when (equal (char-after (point-at-bol 0)) ?*)
  9553. (mapc (lambda (x) (add-to-list 'tags x))
  9554. (org-split-string (org-match-string-no-properties 1) ":")))))
  9555. (mapcar 'list tags)))
  9556. ;;;; The mapping API
  9557. ;;;###autoload
  9558. (defun org-map-entries (func &optional match scope &rest skip)
  9559. "Call FUNC at each headline selected by MATCH in SCOPE.
  9560. FUNC is a function or a lisp form. The function will be called without
  9561. arguments, with the cursor positioned at the beginning of the headline.
  9562. The return values of all calls to the function will be collected and
  9563. returned as a list.
  9564. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9565. Only headlines that are matched by this query will be considered during
  9566. the iteration. When MATCH is nil or t, all headlines will be
  9567. visited by the iteration.
  9568. SCOPE determines the scope of this command. It can be any of:
  9569. nil The current buffer, respecting the restriction if any
  9570. tree The subtree started with the entry at point
  9571. file The current buffer, without restriction
  9572. file-with-archives
  9573. The current buffer, and any archives associated with it
  9574. agenda All agenda files
  9575. agenda-with-archives
  9576. All agenda files with any archive files associated with them
  9577. \(file1 file2 ...)
  9578. If this is a list, all files in the list will be scanned
  9579. The remaining args are treated as settings for the skipping facilities of
  9580. the scanner. The following items can be given here:
  9581. archive skip trees with the archive tag.
  9582. comment skip trees with the COMMENT keyword
  9583. function or Emacs Lisp form:
  9584. will be used as value for `org-agenda-skip-function', so whenever
  9585. the the function returns t, FUNC will not be called for that
  9586. entry and search will continue from the point where the
  9587. function leaves it."
  9588. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9589. (org-agenda-skip-archived-trees (memq 'archive skip))
  9590. (org-agenda-skip-comment-trees (memq 'comment skip))
  9591. (org-agenda-skip-function
  9592. (car (org-delete-all '(comment archive) skip)))
  9593. (org-tags-match-list-sublevels t)
  9594. matcher file res
  9595. org-todo-keywords-for-agenda
  9596. org-done-keywords-for-agenda
  9597. org-todo-keyword-alist-for-agenda
  9598. org-tag-alist-for-agenda)
  9599. (cond
  9600. ((eq match t) (setq matcher t))
  9601. ((eq match nil) (setq matcher t))
  9602. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9603. (save-excursion
  9604. (save-restriction
  9605. (when (eq scope 'tree)
  9606. (org-back-to-heading t)
  9607. (org-narrow-to-subtree)
  9608. (setq scope nil))
  9609. (if (not scope)
  9610. (progn
  9611. (org-prepare-agenda-buffers
  9612. (list (buffer-file-name (current-buffer))))
  9613. (setq res (org-scan-tags func matcher)))
  9614. ;; Get the right scope
  9615. (cond
  9616. ((and scope (listp scope) (symbolp (car scope)))
  9617. (setq scope (eval scope)))
  9618. ((eq scope 'agenda)
  9619. (setq scope (org-agenda-files t)))
  9620. ((eq scope 'agenda-with-archives)
  9621. (setq scope (org-agenda-files t))
  9622. (setq scope (org-add-archive-files scope)))
  9623. ((eq scope 'file)
  9624. (setq scope (list (buffer-file-name))))
  9625. ((eq scope 'file-with-archives)
  9626. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9627. (org-prepare-agenda-buffers scope)
  9628. (while (setq file (pop scope))
  9629. (with-current-buffer (org-find-base-buffer-visiting file)
  9630. (save-excursion
  9631. (save-restriction
  9632. (widen)
  9633. (goto-char (point-min))
  9634. (setq res (append res (org-scan-tags func matcher))))))))))
  9635. res))
  9636. ;;;; Properties
  9637. ;;; Setting and retrieving properties
  9638. (defconst org-special-properties
  9639. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9640. "TIMESTAMP" "TIMESTAMP_IA")
  9641. "The special properties valid in Org-mode.
  9642. These are properties that are not defined in the property drawer,
  9643. but in some other way.")
  9644. (defconst org-default-properties
  9645. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9646. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9647. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9648. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9649. "Some properties that are used by Org-mode for various purposes.
  9650. Being in this list makes sure that they are offered for completion.")
  9651. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9652. "Regular expression matching the first line of a property drawer.")
  9653. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9654. "Regular expression matching the first line of a property drawer.")
  9655. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9656. "Regular expression matching the first line of a property drawer.")
  9657. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9658. "Regular expression matching the first line of a property drawer.")
  9659. (defconst org-property-drawer-re
  9660. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9661. org-property-end-re "\\)\n?")
  9662. "Matches an entire property drawer.")
  9663. (defconst org-clock-drawer-re
  9664. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9665. org-property-end-re "\\)\n?")
  9666. "Matches an entire clock drawer.")
  9667. (defun org-property-action ()
  9668. "Do an action on properties."
  9669. (interactive)
  9670. (let (c)
  9671. (org-at-property-p)
  9672. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9673. (setq c (read-char-exclusive))
  9674. (cond
  9675. ((equal c ?s)
  9676. (call-interactively 'org-set-property))
  9677. ((equal c ?d)
  9678. (call-interactively 'org-delete-property))
  9679. ((equal c ?D)
  9680. (call-interactively 'org-delete-property-globally))
  9681. ((equal c ?c)
  9682. (call-interactively 'org-compute-property-at-point))
  9683. (t (error "No such property action %c" c)))))
  9684. (defun org-at-property-p ()
  9685. "Is the cursor in a property line?"
  9686. ;; FIXME: Does not check if we are actually in the drawer.
  9687. ;; FIXME: also returns true on any drawers.....
  9688. ;; This is used by C-c C-c for property action.
  9689. (save-excursion
  9690. (beginning-of-line 1)
  9691. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9692. (defun org-get-property-block (&optional beg end force)
  9693. "Return the (beg . end) range of the body of the property drawer.
  9694. BEG and END can be beginning and end of subtree, if not given
  9695. they will be found.
  9696. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9697. (catch 'exit
  9698. (save-excursion
  9699. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9700. (end (or end (progn (outline-next-heading) (point)))))
  9701. (goto-char beg)
  9702. (if (re-search-forward org-property-start-re end t)
  9703. (setq beg (1+ (match-end 0)))
  9704. (if force
  9705. (save-excursion
  9706. (org-insert-property-drawer)
  9707. (setq end (progn (outline-next-heading) (point))))
  9708. (throw 'exit nil))
  9709. (goto-char beg)
  9710. (if (re-search-forward org-property-start-re end t)
  9711. (setq beg (1+ (match-end 0)))))
  9712. (if (re-search-forward org-property-end-re end t)
  9713. (setq end (match-beginning 0))
  9714. (or force (throw 'exit nil))
  9715. (goto-char beg)
  9716. (setq end beg)
  9717. (org-indent-line-function)
  9718. (insert ":END:\n"))
  9719. (cons beg end)))))
  9720. (defun org-entry-properties (&optional pom which)
  9721. "Get all properties of the entry at point-or-marker POM.
  9722. This includes the TODO keyword, the tags, time strings for deadline,
  9723. scheduled, and clocking, and any additional properties defined in the
  9724. entry. The return value is an alist, keys may occur multiple times
  9725. if the property key was used several times.
  9726. POM may also be nil, in which case the current entry is used.
  9727. If WHICH is nil or `all', get all properties. If WHICH is
  9728. `special' or `standard', only get that subclass."
  9729. (setq which (or which 'all))
  9730. (org-with-point-at pom
  9731. (let ((clockstr (substring org-clock-string 0 -1))
  9732. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9733. beg end range props sum-props key value string clocksum)
  9734. (save-excursion
  9735. (when (condition-case nil
  9736. (and (org-mode-p) (org-back-to-heading t))
  9737. (error nil))
  9738. (setq beg (point))
  9739. (setq sum-props (get-text-property (point) 'org-summaries))
  9740. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9741. (outline-next-heading)
  9742. (setq end (point))
  9743. (when (memq which '(all special))
  9744. ;; Get the special properties, like TODO and tags
  9745. (goto-char beg)
  9746. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9747. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9748. (when (looking-at org-priority-regexp)
  9749. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9750. (when (and (setq value (org-get-tags-string))
  9751. (string-match "\\S-" value))
  9752. (push (cons "TAGS" value) props))
  9753. (when (setq value (org-get-tags-at))
  9754. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9755. props))
  9756. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9757. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9758. string (if (equal key clockstr)
  9759. (org-no-properties
  9760. (org-trim
  9761. (buffer-substring
  9762. (match-beginning 3) (goto-char (point-at-eol)))))
  9763. (substring (org-match-string-no-properties 3) 1 -1)))
  9764. (unless key
  9765. (if (= (char-after (match-beginning 3)) ?\[)
  9766. (setq key "TIMESTAMP_IA")
  9767. (setq key "TIMESTAMP")))
  9768. (when (or (equal key clockstr) (not (assoc key props)))
  9769. (push (cons key string) props)))
  9770. )
  9771. (when (memq which '(all standard))
  9772. ;; Get the standard properties, like :PORP: ...
  9773. (setq range (org-get-property-block beg end))
  9774. (when range
  9775. (goto-char (car range))
  9776. (while (re-search-forward
  9777. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9778. (cdr range) t)
  9779. (setq key (org-match-string-no-properties 1)
  9780. value (org-trim (or (org-match-string-no-properties 2) "")))
  9781. (unless (member key excluded)
  9782. (push (cons key (or value "")) props)))))
  9783. (if clocksum
  9784. (push (cons "CLOCKSUM"
  9785. (org-columns-number-to-string (/ (float clocksum) 60.)
  9786. 'add_times))
  9787. props))
  9788. (unless (assoc "CATEGORY" props)
  9789. (setq value (or (org-get-category)
  9790. (progn (org-refresh-category-properties)
  9791. (org-get-category))))
  9792. (push (cons "CATEGORY" value) props))
  9793. (append sum-props (nreverse props)))))))
  9794. (defun org-entry-get (pom property &optional inherit)
  9795. "Get value of PROPERTY for entry at point-or-marker POM.
  9796. If INHERIT is non-nil and the entry does not have the property,
  9797. then also check higher levels of the hierarchy.
  9798. If INHERIT is the symbol `selective', use inheritance only if the setting
  9799. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9800. If the property is present but empty, the return value is the empty string.
  9801. If the property is not present at all, nil is returned."
  9802. (org-with-point-at pom
  9803. (if (and inherit (if (eq inherit 'selective)
  9804. (org-property-inherit-p property)
  9805. t))
  9806. (org-entry-get-with-inheritance property)
  9807. (if (member property org-special-properties)
  9808. ;; We need a special property. Use brute force, get all properties.
  9809. (cdr (assoc property (org-entry-properties nil 'special)))
  9810. (let ((range (org-get-property-block)))
  9811. (if (and range
  9812. (goto-char (car range))
  9813. (re-search-forward
  9814. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9815. (cdr range) t))
  9816. ;; Found the property, return it.
  9817. (if (match-end 1)
  9818. (org-match-string-no-properties 1)
  9819. "")))))))
  9820. (defun org-property-or-variable-value (var &optional inherit)
  9821. "Check if there is a property fixing the value of VAR.
  9822. If yes, return this value. If not, return the current value of the variable."
  9823. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9824. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9825. (read prop)
  9826. (symbol-value var))))
  9827. (defun org-entry-delete (pom property)
  9828. "Delete the property PROPERTY from entry at point-or-marker POM."
  9829. (org-with-point-at pom
  9830. (if (member property org-special-properties)
  9831. nil ; cannot delete these properties.
  9832. (let ((range (org-get-property-block)))
  9833. (if (and range
  9834. (goto-char (car range))
  9835. (re-search-forward
  9836. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9837. (cdr range) t))
  9838. (progn
  9839. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9840. t)
  9841. nil)))))
  9842. ;; Multi-values properties are properties that contain multiple values
  9843. ;; These values are assumed to be single words, separated by whitespace.
  9844. (defun org-entry-add-to-multivalued-property (pom property value)
  9845. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9846. (let* ((old (org-entry-get pom property))
  9847. (values (and old (org-split-string old "[ \t]"))))
  9848. (setq value (org-entry-protect-space value))
  9849. (unless (member value values)
  9850. (setq values (cons value values))
  9851. (org-entry-put pom property
  9852. (mapconcat 'identity values " ")))))
  9853. (defun org-entry-remove-from-multivalued-property (pom property value)
  9854. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9855. (let* ((old (org-entry-get pom property))
  9856. (values (and old (org-split-string old "[ \t]"))))
  9857. (setq value (org-entry-protect-space value))
  9858. (when (member value values)
  9859. (setq values (delete value values))
  9860. (org-entry-put pom property
  9861. (mapconcat 'identity values " ")))))
  9862. (defun org-entry-member-in-multivalued-property (pom property value)
  9863. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9864. (let* ((old (org-entry-get pom property))
  9865. (values (and old (org-split-string old "[ \t]"))))
  9866. (setq value (org-entry-protect-space value))
  9867. (member value values)))
  9868. (defun org-entry-get-multivalued-property (pom property)
  9869. "Return a list of values in a multivalued property."
  9870. (let* ((value (org-entry-get pom property))
  9871. (values (and value (org-split-string value "[ \t]"))))
  9872. (mapcar 'org-entry-restore-space values)))
  9873. (defun org-entry-put-multivalued-property (pom property &rest values)
  9874. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9875. VALUES should be a list of strings. Spaces will be protected."
  9876. (org-entry-put pom property
  9877. (mapconcat 'org-entry-protect-space values " "))
  9878. (let* ((value (org-entry-get pom property))
  9879. (values (and value (org-split-string value "[ \t]"))))
  9880. (mapcar 'org-entry-restore-space values)))
  9881. (defun org-entry-protect-space (s)
  9882. "Protect spaces and newline in string S."
  9883. (while (string-match " " s)
  9884. (setq s (replace-match "%20" t t s)))
  9885. (while (string-match "\n" s)
  9886. (setq s (replace-match "%0A" t t s)))
  9887. s)
  9888. (defun org-entry-restore-space (s)
  9889. "Restore spaces and newline in string S."
  9890. (while (string-match "%20" s)
  9891. (setq s (replace-match " " t t s)))
  9892. (while (string-match "%0A" s)
  9893. (setq s (replace-match "\n" t t s)))
  9894. s)
  9895. (defvar org-entry-property-inherited-from (make-marker)
  9896. "Marker pointing to the entry from where a property was inherited.
  9897. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9898. location of the entry where the inheritance search matched. If there was
  9899. no match, the marker will point nowhere.
  9900. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9901. is set.")
  9902. (defun org-entry-get-with-inheritance (property)
  9903. "Get entry property, and search higher levels if not present."
  9904. (move-marker org-entry-property-inherited-from nil)
  9905. (let (tmp)
  9906. (save-excursion
  9907. (save-restriction
  9908. (widen)
  9909. (catch 'ex
  9910. (while t
  9911. (when (setq tmp (org-entry-get nil property))
  9912. (org-back-to-heading t)
  9913. (move-marker org-entry-property-inherited-from (point))
  9914. (throw 'ex tmp))
  9915. (or (org-up-heading-safe) (throw 'ex nil)))))
  9916. (or tmp
  9917. (cdr (assoc property org-file-properties))
  9918. (cdr (assoc property org-global-properties))
  9919. (cdr (assoc property org-global-properties-fixed))))))
  9920. (defun org-entry-put (pom property value)
  9921. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9922. (org-with-point-at pom
  9923. (org-back-to-heading t)
  9924. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9925. range)
  9926. (cond
  9927. ((equal property "TODO")
  9928. (when (and (stringp value) (string-match "\\S-" value)
  9929. (not (member value org-todo-keywords-1)))
  9930. (error "\"%s\" is not a valid TODO state" value))
  9931. (if (or (not value)
  9932. (not (string-match "\\S-" value)))
  9933. (setq value 'none))
  9934. (org-todo value)
  9935. (org-set-tags nil 'align))
  9936. ((equal property "PRIORITY")
  9937. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9938. (string-to-char value) ?\ ))
  9939. (org-set-tags nil 'align))
  9940. ((equal property "SCHEDULED")
  9941. (if (re-search-forward org-scheduled-time-regexp end t)
  9942. (cond
  9943. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9944. ((eq value 'later) (org-timestamp-change 1 'day))
  9945. (t (call-interactively 'org-schedule)))
  9946. (call-interactively 'org-schedule)))
  9947. ((equal property "DEADLINE")
  9948. (if (re-search-forward org-deadline-time-regexp end t)
  9949. (cond
  9950. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9951. ((eq value 'later) (org-timestamp-change 1 'day))
  9952. (t (call-interactively 'org-deadline)))
  9953. (call-interactively 'org-deadline)))
  9954. ((member property org-special-properties)
  9955. (error "The %s property can not yet be set with `org-entry-put'"
  9956. property))
  9957. (t ; a non-special property
  9958. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9959. (setq range (org-get-property-block beg end 'force))
  9960. (goto-char (car range))
  9961. (if (re-search-forward
  9962. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9963. (progn
  9964. (delete-region (match-beginning 1) (match-end 1))
  9965. (goto-char (match-beginning 1)))
  9966. (goto-char (cdr range))
  9967. (insert "\n")
  9968. (backward-char 1)
  9969. (org-indent-line-function)
  9970. (insert ":" property ":"))
  9971. (and value (insert " " value))
  9972. (org-indent-line-function)))))))
  9973. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9974. "Get all property keys in the current buffer.
  9975. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9976. like tags and TODO state.
  9977. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9978. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9979. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9980. formats in the current buffer."
  9981. (let (rtn range cfmt s p)
  9982. (save-excursion
  9983. (save-restriction
  9984. (widen)
  9985. (goto-char (point-min))
  9986. (while (re-search-forward org-property-start-re nil t)
  9987. (setq range (org-get-property-block))
  9988. (goto-char (car range))
  9989. (while (re-search-forward
  9990. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9991. (cdr range) t)
  9992. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9993. (outline-next-heading))))
  9994. (when include-specials
  9995. (setq rtn (append org-special-properties rtn)))
  9996. (when include-defaults
  9997. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9998. (when include-columns
  9999. (save-excursion
  10000. (save-restriction
  10001. (widen)
  10002. (goto-char (point-min))
  10003. (while (re-search-forward
  10004. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10005. nil t)
  10006. (setq cfmt (match-string 2) s 0)
  10007. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10008. cfmt s)
  10009. (setq s (match-end 0)
  10010. p (match-string 1 cfmt))
  10011. (unless (or (equal p "ITEM")
  10012. (member p org-special-properties))
  10013. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10014. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10015. (defun org-property-values (key)
  10016. "Return a list of all values of property KEY."
  10017. (save-excursion
  10018. (save-restriction
  10019. (widen)
  10020. (goto-char (point-min))
  10021. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10022. values)
  10023. (while (re-search-forward re nil t)
  10024. (add-to-list 'values (org-trim (match-string 1))))
  10025. (delete "" values)))))
  10026. (defun org-insert-property-drawer ()
  10027. "Insert a property drawer into the current entry."
  10028. (interactive)
  10029. (org-back-to-heading t)
  10030. (looking-at outline-regexp)
  10031. (let ((indent (- (match-end 0)(match-beginning 0)))
  10032. (beg (point))
  10033. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10034. end hiddenp)
  10035. (outline-next-heading)
  10036. (setq end (point))
  10037. (goto-char beg)
  10038. (while (re-search-forward re end t))
  10039. (setq hiddenp (org-invisible-p))
  10040. (end-of-line 1)
  10041. (and (equal (char-after) ?\n) (forward-char 1))
  10042. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10043. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10044. ;; just skip this line
  10045. (beginning-of-line 2)
  10046. ;; Drawer start, find the end
  10047. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10048. (beginning-of-line 1)))
  10049. (org-skip-over-state-notes)
  10050. (skip-chars-backward " \t\n\r")
  10051. (if (eq (char-before) ?*) (forward-char 1))
  10052. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10053. (beginning-of-line 0)
  10054. (org-indent-to-column indent)
  10055. (beginning-of-line 2)
  10056. (org-indent-to-column indent)
  10057. (beginning-of-line 0)
  10058. (if hiddenp
  10059. (save-excursion
  10060. (org-back-to-heading t)
  10061. (hide-entry))
  10062. (org-flag-drawer t))))
  10063. (defun org-set-property (property value)
  10064. "In the current entry, set PROPERTY to VALUE.
  10065. When called interactively, this will prompt for a property name, offering
  10066. completion on existing and default properties. And then it will prompt
  10067. for a value, offering completion either on allowed values (via an inherited
  10068. xxx_ALL property) or on existing values in other instances of this property
  10069. in the current file."
  10070. (interactive
  10071. (let* ((completion-ignore-case t)
  10072. (keys (org-buffer-property-keys nil t t))
  10073. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10074. (prop (if (member prop0 keys)
  10075. prop0
  10076. (or (cdr (assoc (downcase prop0)
  10077. (mapcar (lambda (x) (cons (downcase x) x))
  10078. keys)))
  10079. prop0)))
  10080. (cur (org-entry-get nil prop))
  10081. (allowed (org-property-get-allowed-values nil prop 'table))
  10082. (existing (mapcar 'list (org-property-values prop)))
  10083. (val (if allowed
  10084. (org-completing-read "Value: " allowed nil 'req-match)
  10085. (org-completing-read
  10086. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10087. (concat "[" cur "]") "")
  10088. ": ")
  10089. existing nil nil "" nil cur))))
  10090. (list prop (if (equal val "") cur val))))
  10091. (unless (equal (org-entry-get nil property) value)
  10092. (org-entry-put nil property value)))
  10093. (defun org-delete-property (property)
  10094. "In the current entry, delete PROPERTY."
  10095. (interactive
  10096. (let* ((completion-ignore-case t)
  10097. (prop (org-ido-completing-read
  10098. "Property: " (org-entry-properties nil 'standard))))
  10099. (list prop)))
  10100. (message "Property %s %s" property
  10101. (if (org-entry-delete nil property)
  10102. "deleted"
  10103. "was not present in the entry")))
  10104. (defun org-delete-property-globally (property)
  10105. "Remove PROPERTY globally, from all entries."
  10106. (interactive
  10107. (let* ((completion-ignore-case t)
  10108. (prop (org-ido-completing-read
  10109. "Globally remove property: "
  10110. (mapcar 'list (org-buffer-property-keys)))))
  10111. (list prop)))
  10112. (save-excursion
  10113. (save-restriction
  10114. (widen)
  10115. (goto-char (point-min))
  10116. (let ((cnt 0))
  10117. (while (re-search-forward
  10118. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10119. nil t)
  10120. (setq cnt (1+ cnt))
  10121. (replace-match ""))
  10122. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10123. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10124. (defun org-compute-property-at-point ()
  10125. "Compute the property at point.
  10126. This looks for an enclosing column format, extracts the operator and
  10127. then applies it to the property in the column format's scope."
  10128. (interactive)
  10129. (unless (org-at-property-p)
  10130. (error "Not at a property"))
  10131. (let ((prop (org-match-string-no-properties 2)))
  10132. (org-columns-get-format-and-top-level)
  10133. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10134. (error "No operator defined for property %s" prop))
  10135. (org-columns-compute prop)))
  10136. (defun org-property-get-allowed-values (pom property &optional table)
  10137. "Get allowed values for the property PROPERTY.
  10138. When TABLE is non-nil, return an alist that can directly be used for
  10139. completion."
  10140. (let (vals)
  10141. (cond
  10142. ((equal property "TODO")
  10143. (setq vals (org-with-point-at pom
  10144. (append org-todo-keywords-1 '("")))))
  10145. ((equal property "PRIORITY")
  10146. (let ((n org-lowest-priority))
  10147. (while (>= n org-highest-priority)
  10148. (push (char-to-string n) vals)
  10149. (setq n (1- n)))))
  10150. ((member property org-special-properties))
  10151. (t
  10152. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10153. (when (and vals (string-match "\\S-" vals))
  10154. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10155. (setq vals (mapcar (lambda (x)
  10156. (cond ((stringp x) x)
  10157. ((numberp x) (number-to-string x))
  10158. ((symbolp x) (symbol-name x))
  10159. (t "???")))
  10160. vals)))))
  10161. (if table (mapcar 'list vals) vals)))
  10162. (defun org-property-previous-allowed-value (&optional previous)
  10163. "Switch to the next allowed value for this property."
  10164. (interactive)
  10165. (org-property-next-allowed-value t))
  10166. (defun org-property-next-allowed-value (&optional previous)
  10167. "Switch to the next allowed value for this property."
  10168. (interactive)
  10169. (unless (org-at-property-p)
  10170. (error "Not at a property"))
  10171. (let* ((key (match-string 2))
  10172. (value (match-string 3))
  10173. (allowed (or (org-property-get-allowed-values (point) key)
  10174. (and (member value '("[ ]" "[-]" "[X]"))
  10175. '("[ ]" "[X]"))))
  10176. nval)
  10177. (unless allowed
  10178. (error "Allowed values for this property have not been defined"))
  10179. (if previous (setq allowed (reverse allowed)))
  10180. (if (member value allowed)
  10181. (setq nval (car (cdr (member value allowed)))))
  10182. (setq nval (or nval (car allowed)))
  10183. (if (equal nval value)
  10184. (error "Only one allowed value for this property"))
  10185. (org-at-property-p)
  10186. (replace-match (concat " :" key ": " nval) t t)
  10187. (org-indent-line-function)
  10188. (beginning-of-line 1)
  10189. (skip-chars-forward " \t")))
  10190. (defun org-find-entry-with-id (ident)
  10191. "Locate the entry that contains the ID property with exact value IDENT.
  10192. IDENT can be a string, a symbol or a number, this function will search for
  10193. the string representation of it.
  10194. Return the position where this entry starts, or nil if there is no such entry."
  10195. (interactive "sID: ")
  10196. (let ((id (cond
  10197. ((stringp ident) ident)
  10198. ((symbol-name ident) (symbol-name ident))
  10199. ((numberp ident) (number-to-string ident))
  10200. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10201. (case-fold-search nil))
  10202. (save-excursion
  10203. (save-restriction
  10204. (widen)
  10205. (goto-char (point-min))
  10206. (when (re-search-forward
  10207. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10208. nil t)
  10209. (org-back-to-heading)
  10210. (point))))))
  10211. ;;;; Timestamps
  10212. (defvar org-last-changed-timestamp nil)
  10213. (defvar org-last-inserted-timestamp nil
  10214. "The last time stamp inserted with `org-insert-time-stamp'.")
  10215. (defvar org-time-was-given) ; dynamically scoped parameter
  10216. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10217. (defvar org-ts-what) ; dynamically scoped parameter
  10218. (defun org-time-stamp (arg &optional inactive)
  10219. "Prompt for a date/time and insert a time stamp.
  10220. If the user specifies a time like HH:MM, or if this command is called
  10221. with a prefix argument, the time stamp will contain date and time.
  10222. Otherwise, only the date will be included. All parts of a date not
  10223. specified by the user will be filled in from the current date/time.
  10224. So if you press just return without typing anything, the time stamp
  10225. will represent the current date/time. If there is already a timestamp
  10226. at the cursor, it will be modified."
  10227. (interactive "P")
  10228. (let* ((ts nil)
  10229. (default-time
  10230. ;; Default time is either today, or, when entering a range,
  10231. ;; the range start.
  10232. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10233. (save-excursion
  10234. (re-search-backward
  10235. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10236. (- (point) 20) t)))
  10237. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10238. (current-time)))
  10239. (default-input (and ts (org-get-compact-tod ts)))
  10240. org-time-was-given org-end-time-was-given time)
  10241. (cond
  10242. ((and (org-at-timestamp-p t)
  10243. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10244. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10245. (insert "--")
  10246. (setq time (let ((this-command this-command))
  10247. (org-read-date arg 'totime nil nil
  10248. default-time default-input)))
  10249. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10250. ((org-at-timestamp-p t)
  10251. (setq time (let ((this-command this-command))
  10252. (org-read-date arg 'totime nil nil default-time default-input)))
  10253. (when (org-at-timestamp-p t) ; just to get the match data
  10254. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10255. (replace-match "")
  10256. (setq org-last-changed-timestamp
  10257. (org-insert-time-stamp
  10258. time (or org-time-was-given arg)
  10259. inactive nil nil (list org-end-time-was-given))))
  10260. (message "Timestamp updated"))
  10261. (t
  10262. (setq time (let ((this-command this-command))
  10263. (org-read-date arg 'totime nil nil default-time default-input)))
  10264. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10265. nil nil (list org-end-time-was-given))))))
  10266. ;; FIXME: can we use this for something else, like computing time differences?
  10267. (defun org-get-compact-tod (s)
  10268. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10269. (let* ((t1 (match-string 1 s))
  10270. (h1 (string-to-number (match-string 2 s)))
  10271. (m1 (string-to-number (match-string 3 s)))
  10272. (t2 (and (match-end 4) (match-string 5 s)))
  10273. (h2 (and t2 (string-to-number (match-string 6 s))))
  10274. (m2 (and t2 (string-to-number (match-string 7 s))))
  10275. dh dm)
  10276. (if (not t2)
  10277. t1
  10278. (setq dh (- h2 h1) dm (- m2 m1))
  10279. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10280. (concat t1 "+" (number-to-string dh)
  10281. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10282. (defun org-time-stamp-inactive (&optional arg)
  10283. "Insert an inactive time stamp.
  10284. An inactive time stamp is enclosed in square brackets instead of angle
  10285. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10286. does not link to the calendar and cannot be changed with the S-cursor keys.
  10287. So these are more for recording a certain time/date."
  10288. (interactive "P")
  10289. (org-time-stamp arg 'inactive))
  10290. (defvar org-date-ovl (org-make-overlay 1 1))
  10291. (org-overlay-put org-date-ovl 'face 'org-warning)
  10292. (org-detach-overlay org-date-ovl)
  10293. (defvar org-ans1) ; dynamically scoped parameter
  10294. (defvar org-ans2) ; dynamically scoped parameter
  10295. (defvar org-plain-time-of-day-regexp) ; defined below
  10296. (defvar org-overriding-default-time nil) ; dynamically scoped
  10297. (defvar org-read-date-overlay nil)
  10298. (defvar org-dcst nil) ; dynamically scoped
  10299. (defun org-read-date (&optional with-time to-time from-string prompt
  10300. default-time default-input)
  10301. "Read a date, possibly a time, and make things smooth for the user.
  10302. The prompt will suggest to enter an ISO date, but you can also enter anything
  10303. which will at least partially be understood by `parse-time-string'.
  10304. Unrecognized parts of the date will default to the current day, month, year,
  10305. hour and minute. If this command is called to replace a timestamp at point,
  10306. of to enter the second timestamp of a range, the default time is taken from the
  10307. existing stamp. For example,
  10308. 3-2-5 --> 2003-02-05
  10309. feb 15 --> currentyear-02-15
  10310. sep 12 9 --> 2009-09-12
  10311. 12:45 --> today 12:45
  10312. 22 sept 0:34 --> currentyear-09-22 0:34
  10313. 12 --> currentyear-currentmonth-12
  10314. Fri --> nearest Friday (today or later)
  10315. etc.
  10316. Furthermore you can specify a relative date by giving, as the *first* thing
  10317. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10318. change in days weeks, months, years.
  10319. With a single plus or minus, the date is relative to today. With a double
  10320. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10321. +4d --> four days from today
  10322. +4 --> same as above
  10323. +2w --> two weeks from today
  10324. ++5 --> five days from default date
  10325. The function understands only English month and weekday abbreviations,
  10326. but this can be configured with the variables `parse-time-months' and
  10327. `parse-time-weekdays'.
  10328. While prompting, a calendar is popped up - you can also select the
  10329. date with the mouse (button 1). The calendar shows a period of three
  10330. months. To scroll it to other months, use the keys `>' and `<'.
  10331. If you don't like the calendar, turn it off with
  10332. \(setq org-read-date-popup-calendar nil)
  10333. With optional argument TO-TIME, the date will immediately be converted
  10334. to an internal time.
  10335. With an optional argument WITH-TIME, the prompt will suggest to also
  10336. insert a time. Note that when WITH-TIME is not set, you can still
  10337. enter a time, and this function will inform the calling routine about
  10338. this change. The calling routine may then choose to change the format
  10339. used to insert the time stamp into the buffer to include the time.
  10340. With optional argument FROM-STRING, read from this string instead from
  10341. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10342. the time/date that is used for everything that is not specified by the
  10343. user."
  10344. (require 'parse-time)
  10345. (let* ((org-time-stamp-rounding-minutes
  10346. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10347. (org-dcst org-display-custom-times)
  10348. (ct (org-current-time))
  10349. (def (or org-overriding-default-time default-time ct))
  10350. (defdecode (decode-time def))
  10351. (dummy (progn
  10352. (when (< (nth 2 defdecode) org-extend-today-until)
  10353. (setcar (nthcdr 2 defdecode) -1)
  10354. (setcar (nthcdr 1 defdecode) 59)
  10355. (setq def (apply 'encode-time defdecode)
  10356. defdecode (decode-time def)))))
  10357. (calendar-move-hook nil)
  10358. (calendar-view-diary-initially-flag nil)
  10359. (view-diary-entries-initially nil)
  10360. (calendar-view-holidays-initially-flag nil)
  10361. (view-calendar-holidays-initially nil)
  10362. (timestr (format-time-string
  10363. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10364. (prompt (concat (if prompt (concat prompt " ") "")
  10365. (format "Date+time [%s]: " timestr)))
  10366. ans (org-ans0 "") org-ans1 org-ans2 final)
  10367. (cond
  10368. (from-string (setq ans from-string))
  10369. (org-read-date-popup-calendar
  10370. (save-excursion
  10371. (save-window-excursion
  10372. (calendar)
  10373. (calendar-forward-day (- (time-to-days def)
  10374. (calendar-absolute-from-gregorian
  10375. (calendar-current-date))))
  10376. (org-eval-in-calendar nil t)
  10377. (let* ((old-map (current-local-map))
  10378. (map (copy-keymap calendar-mode-map))
  10379. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10380. (org-defkey map (kbd "RET") 'org-calendar-select)
  10381. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10382. 'org-calendar-select-mouse)
  10383. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10384. 'org-calendar-select-mouse)
  10385. (org-defkey minibuffer-local-map [(meta shift left)]
  10386. (lambda () (interactive)
  10387. (org-eval-in-calendar '(calendar-backward-month 1))))
  10388. (org-defkey minibuffer-local-map [(meta shift right)]
  10389. (lambda () (interactive)
  10390. (org-eval-in-calendar '(calendar-forward-month 1))))
  10391. (org-defkey minibuffer-local-map [(meta shift up)]
  10392. (lambda () (interactive)
  10393. (org-eval-in-calendar '(calendar-backward-year 1))))
  10394. (org-defkey minibuffer-local-map [(meta shift down)]
  10395. (lambda () (interactive)
  10396. (org-eval-in-calendar '(calendar-forward-year 1))))
  10397. (org-defkey minibuffer-local-map [(shift up)]
  10398. (lambda () (interactive)
  10399. (org-eval-in-calendar '(calendar-backward-week 1))))
  10400. (org-defkey minibuffer-local-map [(shift down)]
  10401. (lambda () (interactive)
  10402. (org-eval-in-calendar '(calendar-forward-week 1))))
  10403. (org-defkey minibuffer-local-map [(shift left)]
  10404. (lambda () (interactive)
  10405. (org-eval-in-calendar '(calendar-backward-day 1))))
  10406. (org-defkey minibuffer-local-map [(shift right)]
  10407. (lambda () (interactive)
  10408. (org-eval-in-calendar '(calendar-forward-day 1))))
  10409. (org-defkey minibuffer-local-map ">"
  10410. (lambda () (interactive)
  10411. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10412. (org-defkey minibuffer-local-map "<"
  10413. (lambda () (interactive)
  10414. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10415. (unwind-protect
  10416. (progn
  10417. (use-local-map map)
  10418. (add-hook 'post-command-hook 'org-read-date-display)
  10419. (setq org-ans0 (read-string prompt default-input nil nil))
  10420. ;; org-ans0: from prompt
  10421. ;; org-ans1: from mouse click
  10422. ;; org-ans2: from calendar motion
  10423. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10424. (remove-hook 'post-command-hook 'org-read-date-display)
  10425. (use-local-map old-map)
  10426. (when org-read-date-overlay
  10427. (org-delete-overlay org-read-date-overlay)
  10428. (setq org-read-date-overlay nil)))))))
  10429. (t ; Naked prompt only
  10430. (unwind-protect
  10431. (setq ans (read-string prompt default-input nil timestr))
  10432. (when org-read-date-overlay
  10433. (org-delete-overlay org-read-date-overlay)
  10434. (setq org-read-date-overlay nil)))))
  10435. (setq final (org-read-date-analyze ans def defdecode))
  10436. (if to-time
  10437. (apply 'encode-time final)
  10438. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10439. (format "%04d-%02d-%02d %02d:%02d"
  10440. (nth 5 final) (nth 4 final) (nth 3 final)
  10441. (nth 2 final) (nth 1 final))
  10442. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10443. (defvar def)
  10444. (defvar defdecode)
  10445. (defvar with-time)
  10446. (defun org-read-date-display ()
  10447. "Display the current date prompt interpretation in the minibuffer."
  10448. (when org-read-date-display-live
  10449. (when org-read-date-overlay
  10450. (org-delete-overlay org-read-date-overlay))
  10451. (let ((p (point)))
  10452. (end-of-line 1)
  10453. (while (not (equal (buffer-substring
  10454. (max (point-min) (- (point) 4)) (point))
  10455. " "))
  10456. (insert " "))
  10457. (goto-char p))
  10458. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10459. " " (or org-ans1 org-ans2)))
  10460. (org-end-time-was-given nil)
  10461. (f (org-read-date-analyze ans def defdecode))
  10462. (fmts (if org-dcst
  10463. org-time-stamp-custom-formats
  10464. org-time-stamp-formats))
  10465. (fmt (if (or with-time
  10466. (and (boundp 'org-time-was-given) org-time-was-given))
  10467. (cdr fmts)
  10468. (car fmts)))
  10469. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10470. (when (and org-end-time-was-given
  10471. (string-match org-plain-time-of-day-regexp txt))
  10472. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10473. org-end-time-was-given
  10474. (substring txt (match-end 0)))))
  10475. (setq org-read-date-overlay
  10476. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10477. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10478. (defun org-read-date-analyze (ans def defdecode)
  10479. "Analyse the combined answer of the date prompt."
  10480. ;; FIXME: cleanup and comment
  10481. (let (delta deltan deltaw deltadef year month day
  10482. hour minute second wday pm h2 m2 tl wday1
  10483. iso-year iso-weekday iso-week iso-year iso-date)
  10484. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10485. (setq ans "+0"))
  10486. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10487. (setq ans (replace-match "" t t ans)
  10488. deltan (car delta)
  10489. deltaw (nth 1 delta)
  10490. deltadef (nth 2 delta)))
  10491. ;; Check if there is an iso week date in there
  10492. ;; If yes, sore the info and postpone interpreting it until the rest
  10493. ;; of the parsing is done
  10494. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10495. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10496. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10497. iso-week (string-to-number (match-string 2 ans)))
  10498. (setq ans (replace-match "" t t ans)))
  10499. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10500. (when (string-match
  10501. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10502. (setq year (if (match-end 2)
  10503. (string-to-number (match-string 2 ans))
  10504. (string-to-number (format-time-string "%Y")))
  10505. month (string-to-number (match-string 3 ans))
  10506. day (string-to-number (match-string 4 ans)))
  10507. (if (< year 100) (setq year (+ 2000 year)))
  10508. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10509. t nil ans)))
  10510. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10511. ;; If there is a time with am/pm, and *no* time without it, we convert
  10512. ;; so that matching will be successful.
  10513. (loop for i from 1 to 2 do ; twice, for end time as well
  10514. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10515. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10516. (setq hour (string-to-number (match-string 1 ans))
  10517. minute (if (match-end 3)
  10518. (string-to-number (match-string 3 ans))
  10519. 0)
  10520. pm (equal ?p
  10521. (string-to-char (downcase (match-string 4 ans)))))
  10522. (if (and (= hour 12) (not pm))
  10523. (setq hour 0)
  10524. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10525. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10526. t t ans))))
  10527. ;; Check if a time range is given as a duration
  10528. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10529. (setq hour (string-to-number (match-string 1 ans))
  10530. h2 (+ hour (string-to-number (match-string 3 ans)))
  10531. minute (string-to-number (match-string 2 ans))
  10532. m2 (+ minute (if (match-end 5) (string-to-number
  10533. (match-string 5 ans))0)))
  10534. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10535. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10536. t t ans)))
  10537. ;; Check if there is a time range
  10538. (when (boundp 'org-end-time-was-given)
  10539. (setq org-time-was-given nil)
  10540. (when (and (string-match org-plain-time-of-day-regexp ans)
  10541. (match-end 8))
  10542. (setq org-end-time-was-given (match-string 8 ans))
  10543. (setq ans (concat (substring ans 0 (match-beginning 7))
  10544. (substring ans (match-end 7))))))
  10545. (setq tl (parse-time-string ans)
  10546. day (or (nth 3 tl) (nth 3 defdecode))
  10547. month (or (nth 4 tl)
  10548. (if (and org-read-date-prefer-future
  10549. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10550. (1+ (nth 4 defdecode))
  10551. (nth 4 defdecode)))
  10552. year (or (nth 5 tl)
  10553. (if (and org-read-date-prefer-future
  10554. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10555. (1+ (nth 5 defdecode))
  10556. (nth 5 defdecode)))
  10557. hour (or (nth 2 tl) (nth 2 defdecode))
  10558. minute (or (nth 1 tl) (nth 1 defdecode))
  10559. second (or (nth 0 tl) 0)
  10560. wday (nth 6 tl))
  10561. ;; Special date definitions below
  10562. (cond
  10563. (iso-week
  10564. ;; There was an iso week
  10565. (setq year (or iso-year year)
  10566. day (or iso-weekday wday 1)
  10567. wday nil ; to make sure that the trigger below does not match
  10568. iso-date (calendar-gregorian-from-absolute
  10569. (calendar-absolute-from-iso
  10570. (list iso-week day year))))
  10571. ; FIXME: Should we also push ISO weeks into the future?
  10572. ; (when (and org-read-date-prefer-future
  10573. ; (not iso-year)
  10574. ; (< (calendar-absolute-from-gregorian iso-date)
  10575. ; (time-to-days (current-time))))
  10576. ; (setq year (1+ year)
  10577. ; iso-date (calendar-gregorian-from-absolute
  10578. ; (calendar-absolute-from-iso
  10579. ; (list iso-week day year)))))
  10580. (setq month (car iso-date)
  10581. year (nth 2 iso-date)
  10582. day (nth 1 iso-date)))
  10583. (deltan
  10584. (unless deltadef
  10585. (let ((now (decode-time (current-time))))
  10586. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10587. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10588. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10589. ((equal deltaw "m") (setq month (+ month deltan)))
  10590. ((equal deltaw "y") (setq year (+ year deltan)))))
  10591. ((and wday (not (nth 3 tl)))
  10592. ;; Weekday was given, but no day, so pick that day in the week
  10593. ;; on or after the derived date.
  10594. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10595. (unless (equal wday wday1)
  10596. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10597. (if (and (boundp 'org-time-was-given)
  10598. (nth 2 tl))
  10599. (setq org-time-was-given t))
  10600. (if (< year 100) (setq year (+ 2000 year)))
  10601. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10602. (list second minute hour day month year)))
  10603. (defvar parse-time-weekdays)
  10604. (defun org-read-date-get-relative (s today default)
  10605. "Check string S for special relative date string.
  10606. TODAY and DEFAULT are internal times, for today and for a default.
  10607. Return shift list (N what def-flag)
  10608. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10609. N is the number of WHATs to shift.
  10610. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10611. the DEFAULT date rather than TODAY."
  10612. (when (and
  10613. (string-match
  10614. (concat
  10615. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10616. "\\([0-9]+\\)?"
  10617. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10618. "\\([ \t]\\|$\\)") s)
  10619. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10620. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10621. (string-to-char (substring (match-string 1 s) -1))
  10622. ?+))
  10623. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10624. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10625. (what (if (match-end 3) (match-string 3 s) "d"))
  10626. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10627. (date (if rel default today))
  10628. (wday (nth 6 (decode-time date)))
  10629. delta)
  10630. (if wday1
  10631. (progn
  10632. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10633. (if (= dir ?-) (setq delta (- delta 7)))
  10634. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10635. (list delta "d" rel))
  10636. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10637. (defun org-eval-in-calendar (form &optional keepdate)
  10638. "Eval FORM in the calendar window and return to current window.
  10639. Also, store the cursor date in variable org-ans2."
  10640. (let ((sw (selected-window)))
  10641. (select-window (get-buffer-window "*Calendar*"))
  10642. (eval form)
  10643. (when (and (not keepdate) (calendar-cursor-to-date))
  10644. (let* ((date (calendar-cursor-to-date))
  10645. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10646. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10647. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10648. (select-window sw)))
  10649. (defun org-calendar-select ()
  10650. "Return to `org-read-date' with the date currently selected.
  10651. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10652. (interactive)
  10653. (when (calendar-cursor-to-date)
  10654. (let* ((date (calendar-cursor-to-date))
  10655. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10656. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10657. (if (active-minibuffer-window) (exit-minibuffer))))
  10658. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10659. "Insert a date stamp for the date given by the internal TIME.
  10660. WITH-HM means, use the stamp format that includes the time of the day.
  10661. INACTIVE means use square brackets instead of angular ones, so that the
  10662. stamp will not contribute to the agenda.
  10663. PRE and POST are optional strings to be inserted before and after the
  10664. stamp.
  10665. The command returns the inserted time stamp."
  10666. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10667. stamp)
  10668. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10669. (insert-before-markers (or pre ""))
  10670. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10671. (when (listp extra)
  10672. (setq extra (car extra))
  10673. (if (and (stringp extra)
  10674. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10675. (setq extra (format "-%02d:%02d"
  10676. (string-to-number (match-string 1 extra))
  10677. (string-to-number (match-string 2 extra))))
  10678. (setq extra nil)))
  10679. (when extra
  10680. (backward-char 1)
  10681. (insert-before-markers extra)
  10682. (forward-char 1))
  10683. (insert-before-markers (or post ""))
  10684. (setq org-last-inserted-timestamp stamp)))
  10685. (defun org-toggle-time-stamp-overlays ()
  10686. "Toggle the use of custom time stamp formats."
  10687. (interactive)
  10688. (setq org-display-custom-times (not org-display-custom-times))
  10689. (unless org-display-custom-times
  10690. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10691. (while (setq p (next-single-property-change p 'display))
  10692. (if (and (get-text-property p 'display)
  10693. (eq (get-text-property p 'face) 'org-date))
  10694. (remove-text-properties
  10695. p (setq p (next-single-property-change p 'display))
  10696. '(display t))))
  10697. (set-buffer-modified-p bmp)))
  10698. (if (featurep 'xemacs)
  10699. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10700. (org-restart-font-lock)
  10701. (setq org-table-may-need-update t)
  10702. (if org-display-custom-times
  10703. (message "Time stamps are overlayed with custom format")
  10704. (message "Time stamp overlays removed")))
  10705. (defun org-display-custom-time (beg end)
  10706. "Overlay modified time stamp format over timestamp between BEG and END."
  10707. (let* ((ts (buffer-substring beg end))
  10708. t1 w1 with-hm tf time str w2 (off 0))
  10709. (save-match-data
  10710. (setq t1 (org-parse-time-string ts t))
  10711. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10712. (setq off (- (match-end 0) (match-beginning 0)))))
  10713. (setq end (- end off))
  10714. (setq w1 (- end beg)
  10715. with-hm (and (nth 1 t1) (nth 2 t1))
  10716. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10717. time (org-fix-decoded-time t1)
  10718. str (org-add-props
  10719. (format-time-string
  10720. (substring tf 1 -1) (apply 'encode-time time))
  10721. nil 'mouse-face 'highlight)
  10722. w2 (length str))
  10723. (if (not (= w2 w1))
  10724. (add-text-properties (1+ beg) (+ 2 beg)
  10725. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10726. (if (featurep 'xemacs)
  10727. (progn
  10728. (put-text-property beg end 'invisible t)
  10729. (put-text-property beg end 'end-glyph (make-glyph str)))
  10730. (put-text-property beg end 'display str))))
  10731. (defun org-translate-time (string)
  10732. "Translate all timestamps in STRING to custom format.
  10733. But do this only if the variable `org-display-custom-times' is set."
  10734. (when org-display-custom-times
  10735. (save-match-data
  10736. (let* ((start 0)
  10737. (re org-ts-regexp-both)
  10738. t1 with-hm inactive tf time str beg end)
  10739. (while (setq start (string-match re string start))
  10740. (setq beg (match-beginning 0)
  10741. end (match-end 0)
  10742. t1 (save-match-data
  10743. (org-parse-time-string (substring string beg end) t))
  10744. with-hm (and (nth 1 t1) (nth 2 t1))
  10745. inactive (equal (substring string beg (1+ beg)) "[")
  10746. tf (funcall (if with-hm 'cdr 'car)
  10747. org-time-stamp-custom-formats)
  10748. time (org-fix-decoded-time t1)
  10749. str (format-time-string
  10750. (concat
  10751. (if inactive "[" "<") (substring tf 1 -1)
  10752. (if inactive "]" ">"))
  10753. (apply 'encode-time time))
  10754. string (replace-match str t t string)
  10755. start (+ start (length str)))))))
  10756. string)
  10757. (defun org-fix-decoded-time (time)
  10758. "Set 0 instead of nil for the first 6 elements of time.
  10759. Don't touch the rest."
  10760. (let ((n 0))
  10761. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10762. (defun org-days-to-time (timestamp-string)
  10763. "Difference between TIMESTAMP-STRING and now in days."
  10764. (- (time-to-days (org-time-string-to-time timestamp-string))
  10765. (time-to-days (current-time))))
  10766. (defun org-deadline-close (timestamp-string &optional ndays)
  10767. "Is the time in TIMESTAMP-STRING close to the current date?"
  10768. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10769. (and (< (org-days-to-time timestamp-string) ndays)
  10770. (not (org-entry-is-done-p))))
  10771. (defun org-get-wdays (ts)
  10772. "Get the deadline lead time appropriate for timestring TS."
  10773. (cond
  10774. ((<= org-deadline-warning-days 0)
  10775. ;; 0 or negative, enforce this value no matter what
  10776. (- org-deadline-warning-days))
  10777. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  10778. ;; lead time is specified.
  10779. (floor (* (string-to-number (match-string 1 ts))
  10780. (cdr (assoc (match-string 2 ts)
  10781. '(("d" . 1) ("w" . 7)
  10782. ("m" . 30.4) ("y" . 365.25)))))))
  10783. ;; go for the default.
  10784. (t org-deadline-warning-days)))
  10785. (defun org-calendar-select-mouse (ev)
  10786. "Return to `org-read-date' with the date currently selected.
  10787. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10788. (interactive "e")
  10789. (mouse-set-point ev)
  10790. (when (calendar-cursor-to-date)
  10791. (let* ((date (calendar-cursor-to-date))
  10792. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10793. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10794. (if (active-minibuffer-window) (exit-minibuffer))))
  10795. (defun org-check-deadlines (ndays)
  10796. "Check if there are any deadlines due or past due.
  10797. A deadline is considered due if it happens within `org-deadline-warning-days'
  10798. days from today's date. If the deadline appears in an entry marked DONE,
  10799. it is not shown. The prefix arg NDAYS can be used to test that many
  10800. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10801. (interactive "P")
  10802. (let* ((org-warn-days
  10803. (cond
  10804. ((equal ndays '(4)) 100000)
  10805. (ndays (prefix-numeric-value ndays))
  10806. (t (abs org-deadline-warning-days))))
  10807. (case-fold-search nil)
  10808. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10809. (callback
  10810. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10811. (message "%d deadlines past-due or due within %d days"
  10812. (org-occur regexp nil callback)
  10813. org-warn-days)))
  10814. (defun org-check-before-date (date)
  10815. "Check if there are deadlines or scheduled entries before DATE."
  10816. (interactive (list (org-read-date)))
  10817. (let ((case-fold-search nil)
  10818. (regexp (concat "\\<\\(" org-deadline-string
  10819. "\\|" org-scheduled-string
  10820. "\\) *<\\([^>]+\\)>"))
  10821. (callback
  10822. (lambda () (time-less-p
  10823. (org-time-string-to-time (match-string 2))
  10824. (org-time-string-to-time date)))))
  10825. (message "%d entries before %s"
  10826. (org-occur regexp nil callback) date)))
  10827. (defun org-evaluate-time-range (&optional to-buffer)
  10828. "Evaluate a time range by computing the difference between start and end.
  10829. Normally the result is just printed in the echo area, but with prefix arg
  10830. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10831. If the time range is actually in a table, the result is inserted into the
  10832. next column.
  10833. For time difference computation, a year is assumed to be exactly 365
  10834. days in order to avoid rounding problems."
  10835. (interactive "P")
  10836. (or
  10837. (org-clock-update-time-maybe)
  10838. (save-excursion
  10839. (unless (org-at-date-range-p t)
  10840. (goto-char (point-at-bol))
  10841. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10842. (if (not (org-at-date-range-p t))
  10843. (error "Not at a time-stamp range, and none found in current line")))
  10844. (let* ((ts1 (match-string 1))
  10845. (ts2 (match-string 2))
  10846. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10847. (match-end (match-end 0))
  10848. (time1 (org-time-string-to-time ts1))
  10849. (time2 (org-time-string-to-time ts2))
  10850. (t1 (time-to-seconds time1))
  10851. (t2 (time-to-seconds time2))
  10852. (diff (abs (- t2 t1)))
  10853. (negative (< (- t2 t1) 0))
  10854. ;; (ys (floor (* 365 24 60 60)))
  10855. (ds (* 24 60 60))
  10856. (hs (* 60 60))
  10857. (fy "%dy %dd %02d:%02d")
  10858. (fy1 "%dy %dd")
  10859. (fd "%dd %02d:%02d")
  10860. (fd1 "%dd")
  10861. (fh "%02d:%02d")
  10862. y d h m align)
  10863. (if havetime
  10864. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10865. y 0
  10866. d (floor (/ diff ds)) diff (mod diff ds)
  10867. h (floor (/ diff hs)) diff (mod diff hs)
  10868. m (floor (/ diff 60)))
  10869. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10870. y 0
  10871. d (floor (+ (/ diff ds) 0.5))
  10872. h 0 m 0))
  10873. (if (not to-buffer)
  10874. (message "%s" (org-make-tdiff-string y d h m))
  10875. (if (org-at-table-p)
  10876. (progn
  10877. (goto-char match-end)
  10878. (setq align t)
  10879. (and (looking-at " *|") (goto-char (match-end 0))))
  10880. (goto-char match-end))
  10881. (if (looking-at
  10882. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10883. (replace-match ""))
  10884. (if negative (insert " -"))
  10885. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10886. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10887. (insert " " (format fh h m))))
  10888. (if align (org-table-align))
  10889. (message "Time difference inserted")))))
  10890. (defun org-make-tdiff-string (y d h m)
  10891. (let ((fmt "")
  10892. (l nil))
  10893. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10894. l (push y l)))
  10895. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10896. l (push d l)))
  10897. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10898. l (push h l)))
  10899. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10900. l (push m l)))
  10901. (apply 'format fmt (nreverse l))))
  10902. (defun org-time-string-to-time (s)
  10903. (apply 'encode-time (org-parse-time-string s)))
  10904. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10905. "Convert a time stamp to an absolute day number.
  10906. If there is a specifyer for a cyclic time stamp, get the closest date to
  10907. DAYNR.
  10908. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10909. (cond
  10910. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10911. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10912. daynr
  10913. (+ daynr 1000)))
  10914. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10915. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10916. (time-to-days (current-time))) (match-string 0 s)
  10917. prefer show-all))
  10918. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10919. (defun org-days-to-iso-week (days)
  10920. "Return the iso week number."
  10921. (require 'cal-iso)
  10922. (car (calendar-iso-from-absolute days)))
  10923. (defun org-small-year-to-year (year)
  10924. "Convert 2-digit years into 4-digit years.
  10925. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10926. The year 2000 cannot be abbreviated. Any year larger than 99
  10927. is returned unchanged."
  10928. (if (< year 38)
  10929. (setq year (+ 2000 year))
  10930. (if (< year 100)
  10931. (setq year (+ 1900 year))))
  10932. year)
  10933. (defun org-time-from-absolute (d)
  10934. "Return the time corresponding to date D.
  10935. D may be an absolute day number, or a calendar-type list (month day year)."
  10936. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10937. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10938. (defun org-calendar-holiday ()
  10939. "List of holidays, for Diary display in Org-mode."
  10940. (require 'holidays)
  10941. (let ((hl (funcall
  10942. (if (fboundp 'calendar-check-holidays)
  10943. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10944. (if hl (mapconcat 'identity hl "; "))))
  10945. (defun org-diary-sexp-entry (sexp entry date)
  10946. "Process a SEXP diary ENTRY for DATE."
  10947. (require 'diary-lib)
  10948. (let ((result (if calendar-debug-sexp
  10949. (let ((stack-trace-on-error t))
  10950. (eval (car (read-from-string sexp))))
  10951. (condition-case nil
  10952. (eval (car (read-from-string sexp)))
  10953. (error
  10954. (beep)
  10955. (message "Bad sexp at line %d in %s: %s"
  10956. (org-current-line)
  10957. (buffer-file-name) sexp)
  10958. (sleep-for 2))))))
  10959. (cond ((stringp result) result)
  10960. ((and (consp result)
  10961. (stringp (cdr result))) (cdr result))
  10962. (result entry)
  10963. (t nil))))
  10964. (defun org-diary-to-ical-string (frombuf)
  10965. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10966. This uses the icalendar.el library."
  10967. (let* ((tmpdir (if (featurep 'xemacs)
  10968. (temp-directory)
  10969. temporary-file-directory))
  10970. (tmpfile (make-temp-name
  10971. (expand-file-name "orgics" tmpdir)))
  10972. buf rtn b e)
  10973. (save-excursion
  10974. (set-buffer frombuf)
  10975. (icalendar-export-region (point-min) (point-max) tmpfile)
  10976. (setq buf (find-buffer-visiting tmpfile))
  10977. (set-buffer buf)
  10978. (goto-char (point-min))
  10979. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10980. (setq b (match-beginning 0)))
  10981. (goto-char (point-max))
  10982. (if (re-search-backward "^END:VEVENT" nil t)
  10983. (setq e (match-end 0)))
  10984. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10985. (kill-buffer buf)
  10986. (delete-file tmpfile)
  10987. rtn))
  10988. (defun org-closest-date (start current change prefer show-all)
  10989. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10990. When PREFER is `past' return a date that is either CURRENT or past.
  10991. When PREFER is `future', return a date that is either CURRENT or future.
  10992. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10993. ;; Make the proper lists from the dates
  10994. (catch 'exit
  10995. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10996. dn dw sday cday n1 n2 n0
  10997. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10998. (setq start (org-date-to-gregorian start)
  10999. current (org-date-to-gregorian
  11000. (if show-all
  11001. current
  11002. (time-to-days (current-time))))
  11003. sday (calendar-absolute-from-gregorian start)
  11004. cday (calendar-absolute-from-gregorian current))
  11005. (if (<= cday sday) (throw 'exit sday))
  11006. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11007. (setq dn (string-to-number (match-string 1 change))
  11008. dw (cdr (assoc (match-string 2 change) a1)))
  11009. (error "Invalid change specifyer: %s" change))
  11010. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11011. (cond
  11012. ((eq dw 'day)
  11013. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11014. n2 (+ n1 dn)))
  11015. ((eq dw 'year)
  11016. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11017. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11018. (setq date1 (list m d y1)
  11019. n1 (calendar-absolute-from-gregorian date1)
  11020. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11021. n2 (calendar-absolute-from-gregorian date2)))
  11022. ((eq dw 'month)
  11023. ;; approx number of month between the two dates
  11024. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11025. ;; How often does dn fit in there?
  11026. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11027. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11028. m (+ m nm)
  11029. ny (floor (/ m 12))
  11030. y (+ y ny)
  11031. m (- m (* ny 12)))
  11032. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11033. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11034. (setq m2 (+ m dn) y2 y)
  11035. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11036. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11037. (while (<= n2 cday)
  11038. (setq n1 n2 m m2 y y2)
  11039. (setq m2 (+ m dn) y2 y)
  11040. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11041. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11042. ;; Make sure n1 is the earlier date
  11043. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11044. (if show-all
  11045. (cond
  11046. ((eq prefer 'past) n1)
  11047. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11048. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11049. (cond
  11050. ((eq prefer 'past) n1)
  11051. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11052. (t (if (= cday n1) n1 n2)))))))
  11053. (defun org-date-to-gregorian (date)
  11054. "Turn any specification of DATE into a gregorian date for the calendar."
  11055. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11056. ((and (listp date) (= (length date) 3)) date)
  11057. ((stringp date)
  11058. (setq date (org-parse-time-string date))
  11059. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11060. ((listp date)
  11061. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11062. (defun org-parse-time-string (s &optional nodefault)
  11063. "Parse the standard Org-mode time string.
  11064. This should be a lot faster than the normal `parse-time-string'.
  11065. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11066. hour and minute fields will be nil if not given."
  11067. (if (string-match org-ts-regexp0 s)
  11068. (list 0
  11069. (if (or (match-beginning 8) (not nodefault))
  11070. (string-to-number (or (match-string 8 s) "0")))
  11071. (if (or (match-beginning 7) (not nodefault))
  11072. (string-to-number (or (match-string 7 s) "0")))
  11073. (string-to-number (match-string 4 s))
  11074. (string-to-number (match-string 3 s))
  11075. (string-to-number (match-string 2 s))
  11076. nil nil nil)
  11077. (make-list 9 0)))
  11078. (defun org-timestamp-up (&optional arg)
  11079. "Increase the date item at the cursor by one.
  11080. If the cursor is on the year, change the year. If it is on the month or
  11081. the day, change that.
  11082. With prefix ARG, change by that many units."
  11083. (interactive "p")
  11084. (org-timestamp-change (prefix-numeric-value arg)))
  11085. (defun org-timestamp-down (&optional arg)
  11086. "Decrease the date item at the cursor by one.
  11087. If the cursor is on the year, change the year. If it is on the month or
  11088. the day, change that.
  11089. With prefix ARG, change by that many units."
  11090. (interactive "p")
  11091. (org-timestamp-change (- (prefix-numeric-value arg))))
  11092. (defun org-timestamp-up-day (&optional arg)
  11093. "Increase the date in the time stamp by one day.
  11094. With prefix ARG, change that many days."
  11095. (interactive "p")
  11096. (if (and (not (org-at-timestamp-p t))
  11097. (org-on-heading-p))
  11098. (org-todo 'up)
  11099. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11100. (defun org-timestamp-down-day (&optional arg)
  11101. "Decrease the date in the time stamp by one day.
  11102. With prefix ARG, change that many days."
  11103. (interactive "p")
  11104. (if (and (not (org-at-timestamp-p t))
  11105. (org-on-heading-p))
  11106. (org-todo 'down)
  11107. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11108. (defun org-at-timestamp-p (&optional inactive-ok)
  11109. "Determine if the cursor is in or at a timestamp."
  11110. (interactive)
  11111. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11112. (pos (point))
  11113. (ans (or (looking-at tsr)
  11114. (save-excursion
  11115. (skip-chars-backward "^[<\n\r\t")
  11116. (if (> (point) (point-min)) (backward-char 1))
  11117. (and (looking-at tsr)
  11118. (> (- (match-end 0) pos) -1))))))
  11119. (and ans
  11120. (boundp 'org-ts-what)
  11121. (setq org-ts-what
  11122. (cond
  11123. ((= pos (match-beginning 0)) 'bracket)
  11124. ((= pos (1- (match-end 0))) 'bracket)
  11125. ((org-pos-in-match-range pos 2) 'year)
  11126. ((org-pos-in-match-range pos 3) 'month)
  11127. ((org-pos-in-match-range pos 7) 'hour)
  11128. ((org-pos-in-match-range pos 8) 'minute)
  11129. ((or (org-pos-in-match-range pos 4)
  11130. (org-pos-in-match-range pos 5)) 'day)
  11131. ((and (> pos (or (match-end 8) (match-end 5)))
  11132. (< pos (match-end 0)))
  11133. (- pos (or (match-end 8) (match-end 5))))
  11134. (t 'day))))
  11135. ans))
  11136. (defun org-toggle-timestamp-type ()
  11137. "Toggle the type (<active> or [inactive]) of a time stamp."
  11138. (interactive)
  11139. (when (org-at-timestamp-p t)
  11140. (let ((beg (match-beginning 0)) (end (match-end 0))
  11141. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11142. (save-excursion
  11143. (goto-char beg)
  11144. (while (re-search-forward "[][<>]" end t)
  11145. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11146. t t)))
  11147. (message "Timestamp is now %sactive"
  11148. (if (equal (char-after beg) ?<) "" "in")))))
  11149. (defun org-timestamp-change (n &optional what)
  11150. "Change the date in the time stamp at point.
  11151. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11152. `year', `minute', `second'. If WHAT is not given, the cursor position
  11153. in the timestamp determines what will be changed."
  11154. (let ((pos (point))
  11155. with-hm inactive
  11156. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11157. org-ts-what
  11158. extra rem
  11159. ts time time0)
  11160. (if (not (org-at-timestamp-p t))
  11161. (error "Not at a timestamp"))
  11162. (if (and (not what) (eq org-ts-what 'bracket))
  11163. (org-toggle-timestamp-type)
  11164. (if (and (not what) (not (eq org-ts-what 'day))
  11165. org-display-custom-times
  11166. (get-text-property (point) 'display)
  11167. (not (get-text-property (1- (point)) 'display)))
  11168. (setq org-ts-what 'day))
  11169. (setq org-ts-what (or what org-ts-what)
  11170. inactive (= (char-after (match-beginning 0)) ?\[)
  11171. ts (match-string 0))
  11172. (replace-match "")
  11173. (if (string-match
  11174. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11175. ts)
  11176. (setq extra (match-string 1 ts)))
  11177. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11178. (setq with-hm t))
  11179. (setq time0 (org-parse-time-string ts))
  11180. (when (and (eq org-ts-what 'minute)
  11181. (eq current-prefix-arg nil))
  11182. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11183. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11184. (setcar (cdr time0) (+ (nth 1 time0)
  11185. (if (> n 0) (- rem) (- dm rem))))))
  11186. (setq time
  11187. (encode-time (or (car time0) 0)
  11188. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11189. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11190. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11191. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11192. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11193. (nthcdr 6 time0)))
  11194. (when (integerp org-ts-what)
  11195. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11196. (if (eq what 'calendar)
  11197. (let ((cal-date (org-get-date-from-calendar)))
  11198. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11199. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11200. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11201. (setcar time0 (or (car time0) 0))
  11202. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11203. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11204. (setq time (apply 'encode-time time0))))
  11205. (setq org-last-changed-timestamp
  11206. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11207. (org-clock-update-time-maybe)
  11208. (goto-char pos)
  11209. ;; Try to recenter the calendar window, if any
  11210. (if (and org-calendar-follow-timestamp-change
  11211. (get-buffer-window "*Calendar*" t)
  11212. (memq org-ts-what '(day month year)))
  11213. (org-recenter-calendar (time-to-days time))))))
  11214. (defun org-modify-ts-extra (s pos n dm)
  11215. "Change the different parts of the lead-time and repeat fields in timestamp."
  11216. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11217. ng h m new rem)
  11218. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11219. (cond
  11220. ((or (org-pos-in-match-range pos 2)
  11221. (org-pos-in-match-range pos 3))
  11222. (setq m (string-to-number (match-string 3 s))
  11223. h (string-to-number (match-string 2 s)))
  11224. (if (org-pos-in-match-range pos 2)
  11225. (setq h (+ h n))
  11226. (setq n (* dm (org-no-warnings (signum n))))
  11227. (when (not (= 0 (setq rem (% m dm))))
  11228. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11229. (setq m (+ m n)))
  11230. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11231. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11232. (setq h (min 24 (max 0 h)))
  11233. (setq ng 1 new (format "-%02d:%02d" h m)))
  11234. ((org-pos-in-match-range pos 6)
  11235. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11236. ((org-pos-in-match-range pos 5)
  11237. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11238. ((org-pos-in-match-range pos 9)
  11239. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11240. ((org-pos-in-match-range pos 8)
  11241. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11242. (when ng
  11243. (setq s (concat
  11244. (substring s 0 (match-beginning ng))
  11245. new
  11246. (substring s (match-end ng))))))
  11247. s))
  11248. (defun org-recenter-calendar (date)
  11249. "If the calendar is visible, recenter it to DATE."
  11250. (let* ((win (selected-window))
  11251. (cwin (get-buffer-window "*Calendar*" t))
  11252. (calendar-move-hook nil))
  11253. (when cwin
  11254. (select-window cwin)
  11255. (calendar-goto-date (if (listp date) date
  11256. (calendar-gregorian-from-absolute date)))
  11257. (select-window win))))
  11258. (defun org-goto-calendar (&optional arg)
  11259. "Go to the Emacs calendar at the current date.
  11260. If there is a time stamp in the current line, go to that date.
  11261. A prefix ARG can be used to force the current date."
  11262. (interactive "P")
  11263. (let ((tsr org-ts-regexp) diff
  11264. (calendar-move-hook nil)
  11265. (calendar-view-holidays-initially-flag nil)
  11266. (view-calendar-holidays-initially nil)
  11267. (calendar-view-diary-initially-flag nil)
  11268. (view-diary-entries-initially nil))
  11269. (if (or (org-at-timestamp-p)
  11270. (save-excursion
  11271. (beginning-of-line 1)
  11272. (looking-at (concat ".*" tsr))))
  11273. (let ((d1 (time-to-days (current-time)))
  11274. (d2 (time-to-days
  11275. (org-time-string-to-time (match-string 1)))))
  11276. (setq diff (- d2 d1))))
  11277. (calendar)
  11278. (calendar-goto-today)
  11279. (if (and diff (not arg)) (calendar-forward-day diff))))
  11280. (defun org-get-date-from-calendar ()
  11281. "Return a list (month day year) of date at point in calendar."
  11282. (with-current-buffer "*Calendar*"
  11283. (save-match-data
  11284. (calendar-cursor-to-date))))
  11285. (defun org-date-from-calendar ()
  11286. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11287. If there is already a time stamp at the cursor position, update it."
  11288. (interactive)
  11289. (if (org-at-timestamp-p t)
  11290. (org-timestamp-change 0 'calendar)
  11291. (let ((cal-date (org-get-date-from-calendar)))
  11292. (org-insert-time-stamp
  11293. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11294. (defun org-minutes-to-hh:mm-string (m)
  11295. "Compute H:MM from a number of minutes."
  11296. (let ((h (/ m 60)))
  11297. (setq m (- m (* 60 h)))
  11298. (format org-time-clocksum-format h m)))
  11299. (defun org-hh:mm-string-to-minutes (s)
  11300. "Convert a string H:MM to a number of minutes."
  11301. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11302. (+ (* (string-to-number (match-string 1 s)) 60)
  11303. (string-to-number (match-string 2 s)))
  11304. 0))
  11305. ;;;; Files
  11306. (defun org-save-all-org-buffers ()
  11307. "Save all Org-mode buffers without user confirmation."
  11308. (interactive)
  11309. (message "Saving all Org-mode buffers...")
  11310. (save-some-buffers t 'org-mode-p)
  11311. (when (featurep 'org-id) (org-id-locations-save))
  11312. (message "Saving all Org-mode buffers... done"))
  11313. (defun org-revert-all-org-buffers ()
  11314. "Revert all Org-mode buffers.
  11315. Prompt for confirmation when there are unsaved changes.
  11316. Be sure you know what you are doing before letting this function
  11317. overwrite your changes.
  11318. This function is useful in a setup where one tracks org files
  11319. with a version control system, to revert on one machine after pulling
  11320. changes from another. I believe the procedure must be like this:
  11321. 1. M-x org-save-all-org-buffers
  11322. 2. Pull changes from the other machine, resolve conflicts
  11323. 3. M-x org-revert-all-org-buffers"
  11324. (interactive)
  11325. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11326. (error "Abort"))
  11327. (save-excursion
  11328. (save-window-excursion
  11329. (mapc
  11330. (lambda (b)
  11331. (when (and (with-current-buffer b (org-mode-p))
  11332. (with-current-buffer b buffer-file-name))
  11333. (switch-to-buffer b)
  11334. (revert-buffer t 'no-confirm)))
  11335. (buffer-list))
  11336. (when (and (featurep 'org-id) org-id-track-globally)
  11337. (org-id-locations-load)))))
  11338. ;;;; Agenda files
  11339. ;;;###autoload
  11340. (defun org-iswitchb (&optional arg)
  11341. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11342. With a prefix argument, restrict available to files.
  11343. With two prefix arguments, restrict available buffers to agenda files.
  11344. Due to some yet unresolved reason, the global function
  11345. `iswitchb-mode' needs to be active for this function to work."
  11346. (interactive "P")
  11347. (require 'iswitchb)
  11348. (let ((enabled iswitchb-mode) blist)
  11349. (or enabled (iswitchb-mode 1))
  11350. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11351. ((equal arg '(16)) (org-buffer-list 'agenda))
  11352. (t (org-buffer-list))))
  11353. (unwind-protect
  11354. (let ((iswitchb-make-buflist-hook
  11355. (lambda ()
  11356. (setq iswitchb-temp-buflist
  11357. (mapcar 'buffer-name blist)))))
  11358. (switch-to-buffer
  11359. (iswitchb-read-buffer
  11360. "Switch-to: " nil t))
  11361. (or enabled (iswitchb-mode -1))))))
  11362. ;;;###autoload
  11363. (defun org-ido-switchb (&optional arg)
  11364. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11365. With a prefix argument, restrict available to files.
  11366. With two prefix arguments, restrict available buffers to agenda files."
  11367. (interactive "P")
  11368. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11369. ((equal arg '(16)) (org-buffer-list 'agenda))
  11370. (t (org-buffer-list)))))
  11371. (switch-to-buffer
  11372. (org-ido-completing-read "Org buffer: "
  11373. (mapcar 'buffer-name blist)
  11374. nil t))))
  11375. (defun org-buffer-list (&optional predicate exclude-tmp)
  11376. "Return a list of Org buffers.
  11377. PREDICATE can be `export', `files' or `agenda'.
  11378. export restrict the list to Export buffers.
  11379. files restrict the list to buffers visiting Org files.
  11380. agenda restrict the list to buffers visiting agenda files.
  11381. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11382. (let* ((bfn nil)
  11383. (agenda-files (and (eq predicate 'agenda)
  11384. (mapcar 'file-truename (org-agenda-files t))))
  11385. (filter
  11386. (cond
  11387. ((eq predicate 'files)
  11388. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11389. ((eq predicate 'export)
  11390. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11391. ((eq predicate 'agenda)
  11392. (lambda (b)
  11393. (with-current-buffer b
  11394. (and (eq major-mode 'org-mode)
  11395. (setq bfn (buffer-file-name b))
  11396. (member (file-truename bfn) agenda-files)))))
  11397. (t (lambda (b) (with-current-buffer b
  11398. (or (eq major-mode 'org-mode)
  11399. (string-match "\*Org .*Export"
  11400. (buffer-name b)))))))))
  11401. (delq nil
  11402. (mapcar
  11403. (lambda(b)
  11404. (if (and (funcall filter b)
  11405. (or (not exclude-tmp)
  11406. (not (string-match "tmp" (buffer-name b)))))
  11407. b
  11408. nil))
  11409. (buffer-list)))))
  11410. (defun org-agenda-files (&optional unrestricted archives)
  11411. "Get the list of agenda files.
  11412. Optional UNRESTRICTED means return the full list even if a restriction
  11413. is currently in place.
  11414. When ARCHIVES is t, include all archive files hat are really being
  11415. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11416. `org-agenda-archives-mode' is t."
  11417. (let ((files
  11418. (cond
  11419. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11420. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11421. ((listp org-agenda-files) org-agenda-files)
  11422. (t (error "Invalid value of `org-agenda-files'")))))
  11423. (setq files (apply 'append
  11424. (mapcar (lambda (f)
  11425. (if (file-directory-p f)
  11426. (directory-files
  11427. f t org-agenda-file-regexp)
  11428. (list f)))
  11429. files)))
  11430. (when org-agenda-skip-unavailable-files
  11431. (setq files (delq nil
  11432. (mapcar (function
  11433. (lambda (file)
  11434. (and (file-readable-p file) file)))
  11435. files))))
  11436. (when (or (eq archives t)
  11437. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11438. (setq files (org-add-archive-files files)))
  11439. files))
  11440. (defun org-edit-agenda-file-list ()
  11441. "Edit the list of agenda files.
  11442. Depending on setup, this either uses customize to edit the variable
  11443. `org-agenda-files', or it visits the file that is holding the list. In the
  11444. latter case, the buffer is set up in a way that saving it automatically kills
  11445. the buffer and restores the previous window configuration."
  11446. (interactive)
  11447. (if (stringp org-agenda-files)
  11448. (let ((cw (current-window-configuration)))
  11449. (find-file org-agenda-files)
  11450. (org-set-local 'org-window-configuration cw)
  11451. (org-add-hook 'after-save-hook
  11452. (lambda ()
  11453. (set-window-configuration
  11454. (prog1 org-window-configuration
  11455. (kill-buffer (current-buffer))))
  11456. (org-install-agenda-files-menu)
  11457. (message "New agenda file list installed"))
  11458. nil 'local)
  11459. (message "%s" (substitute-command-keys
  11460. "Edit list and finish with \\[save-buffer]")))
  11461. (customize-variable 'org-agenda-files)))
  11462. (defun org-store-new-agenda-file-list (list)
  11463. "Set new value for the agenda file list and save it correctly."
  11464. (if (stringp org-agenda-files)
  11465. (let ((f org-agenda-files) b)
  11466. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11467. (with-temp-file f
  11468. (insert (mapconcat 'identity list "\n") "\n")))
  11469. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11470. (setq org-agenda-files list)
  11471. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11472. (defun org-read-agenda-file-list ()
  11473. "Read the list of agenda files from a file."
  11474. (when (file-directory-p org-agenda-files)
  11475. (error "`org-agenda-files' cannot be a single directory"))
  11476. (when (stringp org-agenda-files)
  11477. (with-temp-buffer
  11478. (insert-file-contents org-agenda-files)
  11479. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11480. ;;;###autoload
  11481. (defun org-cycle-agenda-files ()
  11482. "Cycle through the files in `org-agenda-files'.
  11483. If the current buffer visits an agenda file, find the next one in the list.
  11484. If the current buffer does not, find the first agenda file."
  11485. (interactive)
  11486. (let* ((fs (org-agenda-files t))
  11487. (files (append fs (list (car fs))))
  11488. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11489. file)
  11490. (unless files (error "No agenda files"))
  11491. (catch 'exit
  11492. (while (setq file (pop files))
  11493. (if (equal (file-truename file) tcf)
  11494. (when (car files)
  11495. (find-file (car files))
  11496. (throw 'exit t))))
  11497. (find-file (car fs)))
  11498. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11499. (defun org-agenda-file-to-front (&optional to-end)
  11500. "Move/add the current file to the top of the agenda file list.
  11501. If the file is not present in the list, it is added to the front. If it is
  11502. present, it is moved there. With optional argument TO-END, add/move to the
  11503. end of the list."
  11504. (interactive "P")
  11505. (let ((org-agenda-skip-unavailable-files nil)
  11506. (file-alist (mapcar (lambda (x)
  11507. (cons (file-truename x) x))
  11508. (org-agenda-files t)))
  11509. (ctf (file-truename buffer-file-name))
  11510. x had)
  11511. (setq x (assoc ctf file-alist) had x)
  11512. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11513. (if to-end
  11514. (setq file-alist (append (delq x file-alist) (list x)))
  11515. (setq file-alist (cons x (delq x file-alist))))
  11516. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11517. (org-install-agenda-files-menu)
  11518. (message "File %s to %s of agenda file list"
  11519. (if had "moved" "added") (if to-end "end" "front"))))
  11520. (defun org-remove-file (&optional file)
  11521. "Remove current file from the list of files in variable `org-agenda-files'.
  11522. These are the files which are being checked for agenda entries.
  11523. Optional argument FILE means, use this file instead of the current."
  11524. (interactive)
  11525. (let* ((org-agenda-skip-unavailable-files nil)
  11526. (file (or file buffer-file-name))
  11527. (true-file (file-truename file))
  11528. (afile (abbreviate-file-name file))
  11529. (files (delq nil (mapcar
  11530. (lambda (x)
  11531. (if (equal true-file
  11532. (file-truename x))
  11533. nil x))
  11534. (org-agenda-files t)))))
  11535. (if (not (= (length files) (length (org-agenda-files t))))
  11536. (progn
  11537. (org-store-new-agenda-file-list files)
  11538. (org-install-agenda-files-menu)
  11539. (message "Removed file: %s" afile))
  11540. (message "File was not in list: %s (not removed)" afile))))
  11541. (defun org-file-menu-entry (file)
  11542. (vector file (list 'find-file file) t))
  11543. (defun org-check-agenda-file (file)
  11544. "Make sure FILE exists. If not, ask user what to do."
  11545. (when (not (file-exists-p file))
  11546. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11547. (abbreviate-file-name file))
  11548. (let ((r (downcase (read-char-exclusive))))
  11549. (cond
  11550. ((equal r ?r)
  11551. (org-remove-file file)
  11552. (throw 'nextfile t))
  11553. (t (error "Abort"))))))
  11554. (defun org-get-agenda-file-buffer (file)
  11555. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11556. it to the list of buffers which might be released later."
  11557. (let ((buf (org-find-base-buffer-visiting file)))
  11558. (if buf
  11559. buf ; just return it
  11560. ;; Make a new buffer and remember it
  11561. (setq buf (find-file-noselect file))
  11562. (if buf (push buf org-agenda-new-buffers))
  11563. buf)))
  11564. (defun org-release-buffers (blist)
  11565. "Release all buffers in list, asking the user for confirmation when needed.
  11566. When a buffer is unmodified, it is just killed. When modified, it is saved
  11567. \(if the user agrees) and then killed."
  11568. (let (buf file)
  11569. (while (setq buf (pop blist))
  11570. (setq file (buffer-file-name buf))
  11571. (when (and (buffer-modified-p buf)
  11572. file
  11573. (y-or-n-p (format "Save file %s? " file)))
  11574. (with-current-buffer buf (save-buffer)))
  11575. (kill-buffer buf))))
  11576. (defun org-prepare-agenda-buffers (files)
  11577. "Create buffers for all agenda files, protect archived trees and comments."
  11578. (interactive)
  11579. (let ((pa '(:org-archived t))
  11580. (pc '(:org-comment t))
  11581. (pall '(:org-archived t :org-comment t))
  11582. (inhibit-read-only t)
  11583. (rea (concat ":" org-archive-tag ":"))
  11584. bmp file re)
  11585. (save-excursion
  11586. (save-restriction
  11587. (while (setq file (pop files))
  11588. (if (bufferp file)
  11589. (set-buffer file)
  11590. (org-check-agenda-file file)
  11591. (set-buffer (org-get-agenda-file-buffer file)))
  11592. (widen)
  11593. (setq bmp (buffer-modified-p))
  11594. (org-refresh-category-properties)
  11595. (setq org-todo-keywords-for-agenda
  11596. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11597. (setq org-done-keywords-for-agenda
  11598. (append org-done-keywords-for-agenda org-done-keywords))
  11599. (setq org-todo-keyword-alist-for-agenda
  11600. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11601. (setq org-tag-alist-for-agenda
  11602. (append org-tag-alist-for-agenda org-tag-alist))
  11603. (save-excursion
  11604. (remove-text-properties (point-min) (point-max) pall)
  11605. (when org-agenda-skip-archived-trees
  11606. (goto-char (point-min))
  11607. (while (re-search-forward rea nil t)
  11608. (if (org-on-heading-p t)
  11609. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11610. (goto-char (point-min))
  11611. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11612. (while (re-search-forward re nil t)
  11613. (add-text-properties
  11614. (match-beginning 0) (org-end-of-subtree t) pc)))
  11615. (set-buffer-modified-p bmp))))
  11616. (setq org-todo-keyword-alist-for-agenda
  11617. (org-uniquify org-todo-keyword-alist-for-agenda)
  11618. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11619. ;;;; Embedded LaTeX
  11620. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11621. "Keymap for the minor `org-cdlatex-mode'.")
  11622. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11623. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11624. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11625. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11626. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11627. (defvar org-cdlatex-texmathp-advice-is-done nil
  11628. "Flag remembering if we have applied the advice to texmathp already.")
  11629. (define-minor-mode org-cdlatex-mode
  11630. "Toggle the minor `org-cdlatex-mode'.
  11631. This mode supports entering LaTeX environment and math in LaTeX fragments
  11632. in Org-mode.
  11633. \\{org-cdlatex-mode-map}"
  11634. nil " OCDL" nil
  11635. (when org-cdlatex-mode (require 'cdlatex))
  11636. (unless org-cdlatex-texmathp-advice-is-done
  11637. (setq org-cdlatex-texmathp-advice-is-done t)
  11638. (defadvice texmathp (around org-math-always-on activate)
  11639. "Always return t in org-mode buffers.
  11640. This is because we want to insert math symbols without dollars even outside
  11641. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11642. an embedded LaTeX fragment, let texmathp do its job.
  11643. \\[org-cdlatex-mode-map]"
  11644. (interactive)
  11645. (let (p)
  11646. (cond
  11647. ((not (org-mode-p)) ad-do-it)
  11648. ((eq this-command 'cdlatex-math-symbol)
  11649. (setq ad-return-value t
  11650. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11651. (t
  11652. (let ((p (org-inside-LaTeX-fragment-p)))
  11653. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11654. (setq ad-return-value t
  11655. texmathp-why '("Org-mode embedded math" . 0))
  11656. (if p ad-do-it)))))))))
  11657. (defun turn-on-org-cdlatex ()
  11658. "Unconditionally turn on `org-cdlatex-mode'."
  11659. (org-cdlatex-mode 1))
  11660. (defun org-inside-LaTeX-fragment-p ()
  11661. "Test if point is inside a LaTeX fragment.
  11662. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11663. sequence appearing also before point.
  11664. Even though the matchers for math are configurable, this function assumes
  11665. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11666. delimiters are skipped when they have been removed by customization.
  11667. The return value is nil, or a cons cell with the delimiter and
  11668. and the position of this delimiter.
  11669. This function does a reasonably good job, but can locally be fooled by
  11670. for example currency specifications. For example it will assume being in
  11671. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11672. fragments that are properly closed, but during editing, we have to live
  11673. with the uncertainty caused by missing closing delimiters. This function
  11674. looks only before point, not after."
  11675. (catch 'exit
  11676. (let ((pos (point))
  11677. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11678. (lim (progn
  11679. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11680. (point)))
  11681. dd-on str (start 0) m re)
  11682. (goto-char pos)
  11683. (when dodollar
  11684. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11685. re (nth 1 (assoc "$" org-latex-regexps)))
  11686. (while (string-match re str start)
  11687. (cond
  11688. ((= (match-end 0) (length str))
  11689. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11690. ((= (match-end 0) (- (length str) 5))
  11691. (throw 'exit nil))
  11692. (t (setq start (match-end 0))))))
  11693. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11694. (goto-char pos)
  11695. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11696. (and (match-beginning 2) (throw 'exit nil))
  11697. ;; count $$
  11698. (while (re-search-backward "\\$\\$" lim t)
  11699. (setq dd-on (not dd-on)))
  11700. (goto-char pos)
  11701. (if dd-on (cons "$$" m))))))
  11702. (defun org-try-cdlatex-tab ()
  11703. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11704. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11705. - inside a LaTeX fragment, or
  11706. - after the first word in a line, where an abbreviation expansion could
  11707. insert a LaTeX environment."
  11708. (when org-cdlatex-mode
  11709. (cond
  11710. ((save-excursion
  11711. (skip-chars-backward "a-zA-Z0-9*")
  11712. (skip-chars-backward " \t")
  11713. (bolp))
  11714. (cdlatex-tab) t)
  11715. ((org-inside-LaTeX-fragment-p)
  11716. (cdlatex-tab) t)
  11717. (t nil))))
  11718. (defun org-cdlatex-underscore-caret (&optional arg)
  11719. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11720. Revert to the normal definition outside of these fragments."
  11721. (interactive "P")
  11722. (if (org-inside-LaTeX-fragment-p)
  11723. (call-interactively 'cdlatex-sub-superscript)
  11724. (let (org-cdlatex-mode)
  11725. (call-interactively (key-binding (vector last-input-event))))))
  11726. (defun org-cdlatex-math-modify (&optional arg)
  11727. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11728. Revert to the normal definition outside of these fragments."
  11729. (interactive "P")
  11730. (if (org-inside-LaTeX-fragment-p)
  11731. (call-interactively 'cdlatex-math-modify)
  11732. (let (org-cdlatex-mode)
  11733. (call-interactively (key-binding (vector last-input-event))))))
  11734. (defvar org-latex-fragment-image-overlays nil
  11735. "List of overlays carrying the images of latex fragments.")
  11736. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11737. (defun org-remove-latex-fragment-image-overlays ()
  11738. "Remove all overlays with LaTeX fragment images in current buffer."
  11739. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11740. (setq org-latex-fragment-image-overlays nil))
  11741. (defun org-preview-latex-fragment (&optional subtree)
  11742. "Preview the LaTeX fragment at point, or all locally or globally.
  11743. If the cursor is in a LaTeX fragment, create the image and overlay
  11744. it over the source code. If there is no fragment at point, display
  11745. all fragments in the current text, from one headline to the next. With
  11746. prefix SUBTREE, display all fragments in the current subtree. With a
  11747. double prefix `C-u C-u', or when the cursor is before the first headline,
  11748. display all fragments in the buffer.
  11749. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11750. (interactive "P")
  11751. (org-remove-latex-fragment-image-overlays)
  11752. (save-excursion
  11753. (save-restriction
  11754. (let (beg end at msg)
  11755. (cond
  11756. ((or (equal subtree '(16))
  11757. (not (save-excursion
  11758. (re-search-backward (concat "^" outline-regexp) nil t))))
  11759. (setq beg (point-min) end (point-max)
  11760. msg "Creating images for buffer...%s"))
  11761. ((equal subtree '(4))
  11762. (org-back-to-heading)
  11763. (setq beg (point) end (org-end-of-subtree t)
  11764. msg "Creating images for subtree...%s"))
  11765. (t
  11766. (if (setq at (org-inside-LaTeX-fragment-p))
  11767. (goto-char (max (point-min) (- (cdr at) 2)))
  11768. (org-back-to-heading))
  11769. (setq beg (point) end (progn (outline-next-heading) (point))
  11770. msg (if at "Creating image...%s"
  11771. "Creating images for entry...%s"))))
  11772. (message msg "")
  11773. (narrow-to-region beg end)
  11774. (goto-char beg)
  11775. (org-format-latex
  11776. (concat "ltxpng/" (file-name-sans-extension
  11777. (file-name-nondirectory
  11778. buffer-file-name)))
  11779. default-directory 'overlays msg at 'forbuffer)
  11780. (message msg "done. Use `C-c C-c' to remove images.")))))
  11781. (defvar org-latex-regexps
  11782. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11783. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11784. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11785. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11786. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11787. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11788. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11789. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11790. "Regular expressions for matching embedded LaTeX.")
  11791. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11792. "Replace LaTeX fragments with links to an image, and produce images."
  11793. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11794. (let* ((prefixnodir (file-name-nondirectory prefix))
  11795. (absprefix (expand-file-name prefix dir))
  11796. (todir (file-name-directory absprefix))
  11797. (opt org-format-latex-options)
  11798. (matchers (plist-get opt :matchers))
  11799. (re-list org-latex-regexps)
  11800. (cnt 0) txt link beg end re e checkdir
  11801. m n block linkfile movefile ov)
  11802. ;; Check if there are old images files with this prefix, and remove them
  11803. (when (file-directory-p todir)
  11804. (mapc 'delete-file
  11805. (directory-files
  11806. todir 'full
  11807. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11808. ;; Check the different regular expressions
  11809. (while (setq e (pop re-list))
  11810. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11811. block (if (nth 3 e) "\n\n" ""))
  11812. (when (member m matchers)
  11813. (goto-char (point-min))
  11814. (while (re-search-forward re nil t)
  11815. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11816. (not (get-text-property (match-beginning n)
  11817. 'org-protected)))
  11818. (setq txt (match-string n)
  11819. beg (match-beginning n) end (match-end n)
  11820. cnt (1+ cnt)
  11821. linkfile (format "%s_%04d.png" prefix cnt)
  11822. movefile (format "%s_%04d.png" absprefix cnt)
  11823. link (concat block "[[file:" linkfile "]]" block))
  11824. (if msg (message msg cnt))
  11825. (goto-char beg)
  11826. (unless checkdir ; make sure the directory exists
  11827. (setq checkdir t)
  11828. (or (file-directory-p todir) (make-directory todir)))
  11829. (org-create-formula-image
  11830. txt movefile opt forbuffer)
  11831. (if overlays
  11832. (progn
  11833. (setq ov (org-make-overlay beg end))
  11834. (if (featurep 'xemacs)
  11835. (progn
  11836. (org-overlay-put ov 'invisible t)
  11837. (org-overlay-put
  11838. ov 'end-glyph
  11839. (make-glyph (vector 'png :file movefile))))
  11840. (org-overlay-put
  11841. ov 'display
  11842. (list 'image :type 'png :file movefile :ascent 'center)))
  11843. (push ov org-latex-fragment-image-overlays)
  11844. (goto-char end))
  11845. (delete-region beg end)
  11846. (insert link))))))))
  11847. ;; This function borrows from Ganesh Swami's latex2png.el
  11848. (defun org-create-formula-image (string tofile options buffer)
  11849. (let* ((tmpdir (if (featurep 'xemacs)
  11850. (temp-directory)
  11851. temporary-file-directory))
  11852. (texfilebase (make-temp-name
  11853. (expand-file-name "orgtex" tmpdir)))
  11854. (texfile (concat texfilebase ".tex"))
  11855. (dvifile (concat texfilebase ".dvi"))
  11856. (pngfile (concat texfilebase ".png"))
  11857. (fnh (if (featurep 'xemacs)
  11858. (font-height (get-face-font 'default))
  11859. (face-attribute 'default :height nil)))
  11860. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11861. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11862. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11863. "Black"))
  11864. (bg (or (plist-get options (if buffer :background :html-background))
  11865. "Transparent")))
  11866. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11867. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11868. (with-temp-file texfile
  11869. (insert org-format-latex-header
  11870. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11871. (let ((dir default-directory))
  11872. (condition-case nil
  11873. (progn
  11874. (cd tmpdir)
  11875. (call-process "latex" nil nil nil texfile))
  11876. (error nil))
  11877. (cd dir))
  11878. (if (not (file-exists-p dvifile))
  11879. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11880. (condition-case nil
  11881. (call-process "dvipng" nil nil nil
  11882. "-E" "-fg" fg "-bg" bg
  11883. "-D" dpi
  11884. ;;"-x" scale "-y" scale
  11885. "-T" "tight"
  11886. "-o" pngfile
  11887. dvifile)
  11888. (error nil))
  11889. (if (not (file-exists-p pngfile))
  11890. (progn (message "Failed to create png file from %s" texfile) nil)
  11891. ;; Use the requested file name and clean up
  11892. (copy-file pngfile tofile 'replace)
  11893. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11894. (delete-file (concat texfilebase e)))
  11895. pngfile))))
  11896. (defun org-dvipng-color (attr)
  11897. "Return an rgb color specification for dvipng."
  11898. (apply 'format "rgb %s %s %s"
  11899. (mapcar 'org-normalize-color
  11900. (color-values (face-attribute 'default attr nil)))))
  11901. (defun org-normalize-color (value)
  11902. "Return string to be used as color value for an RGB component."
  11903. (format "%g" (/ value 65535.0)))
  11904. ;;;; Key bindings
  11905. ;; Make `C-c C-x' a prefix key
  11906. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11907. ;; TAB key with modifiers
  11908. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11909. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11910. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11911. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11912. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11913. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11914. ;; The following line is necessary under Suse GNU/Linux
  11915. (unless (featurep 'xemacs)
  11916. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11917. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11918. (define-key org-mode-map [backtab] 'org-shifttab)
  11919. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11920. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11921. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11922. ;; Cursor keys with modifiers
  11923. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11924. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11925. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11926. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11927. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11928. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11929. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11930. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11931. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11932. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11933. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11934. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11935. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11936. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11937. ;;; Extra keys for tty access.
  11938. ;; We only set them when really needed because otherwise the
  11939. ;; menus don't show the simple keys
  11940. (when (or org-use-extra-keys
  11941. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11942. (not window-system))
  11943. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11944. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11945. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11946. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11947. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11948. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11949. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11950. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11951. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11952. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11953. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11954. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11955. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11956. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11957. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11958. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11959. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11960. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11961. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11962. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11963. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11964. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11965. ;; All the other keys
  11966. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11967. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11968. (if (boundp 'narrow-map)
  11969. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11970. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11971. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11972. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11973. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11974. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11975. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11976. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11977. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11978. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11979. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11980. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11981. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11982. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11983. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11984. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11985. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11986. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11987. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11988. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11989. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11990. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11991. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11992. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11993. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11994. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11995. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11996. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11997. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11998. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11999. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12000. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12001. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12002. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12003. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12004. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12005. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12006. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12007. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12008. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12009. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12010. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12011. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12012. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12013. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12014. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12015. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12016. (org-defkey org-mode-map "\C-m" 'org-return)
  12017. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12018. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12019. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12020. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12021. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12022. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12023. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12024. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12025. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12026. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12027. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12028. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12029. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12030. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12031. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12032. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12033. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12034. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12035. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12036. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12037. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12038. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12039. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12040. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12041. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12042. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12043. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12044. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12045. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12046. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12047. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12048. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12049. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12050. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12051. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12052. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12053. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12054. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12055. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12056. (when (featurep 'xemacs)
  12057. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12058. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12059. (defun org-self-insert-command (N)
  12060. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12061. If the cursor is in a table looking at whitespace, the whitespace is
  12062. overwritten, and the table is not marked as requiring realignment."
  12063. (interactive "p")
  12064. (if (and (org-table-p)
  12065. (progn
  12066. ;; check if we blank the field, and if that triggers align
  12067. (and (featurep 'org-table) org-table-auto-blank-field
  12068. (member last-command
  12069. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12070. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12071. ;; got extra space, this field does not determine column width
  12072. (let (org-table-may-need-update) (org-table-blank-field))
  12073. ;; no extra space, this field may determine column width
  12074. (org-table-blank-field)))
  12075. t)
  12076. (eq N 1)
  12077. (looking-at "[^|\n]* |"))
  12078. (let (org-table-may-need-update)
  12079. (goto-char (1- (match-end 0)))
  12080. (delete-backward-char 1)
  12081. (goto-char (match-beginning 0))
  12082. (self-insert-command N))
  12083. (setq org-table-may-need-update t)
  12084. (self-insert-command N)
  12085. (org-fix-tags-on-the-fly)))
  12086. (defun org-fix-tags-on-the-fly ()
  12087. (when (and (equal (char-after (point-at-bol)) ?*)
  12088. (org-on-heading-p))
  12089. (org-align-tags-here org-tags-column)))
  12090. (defun org-delete-backward-char (N)
  12091. "Like `delete-backward-char', insert whitespace at field end in tables.
  12092. When deleting backwards, in tables this function will insert whitespace in
  12093. front of the next \"|\" separator, to keep the table aligned. The table will
  12094. still be marked for re-alignment if the field did fill the entire column,
  12095. because, in this case the deletion might narrow the column."
  12096. (interactive "p")
  12097. (if (and (org-table-p)
  12098. (eq N 1)
  12099. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12100. (looking-at ".*?|"))
  12101. (let ((pos (point))
  12102. (noalign (looking-at "[^|\n\r]* |"))
  12103. (c org-table-may-need-update))
  12104. (backward-delete-char N)
  12105. (skip-chars-forward "^|")
  12106. (insert " ")
  12107. (goto-char (1- pos))
  12108. ;; noalign: if there were two spaces at the end, this field
  12109. ;; does not determine the width of the column.
  12110. (if noalign (setq org-table-may-need-update c)))
  12111. (backward-delete-char N)
  12112. (org-fix-tags-on-the-fly)))
  12113. (defun org-delete-char (N)
  12114. "Like `delete-char', but insert whitespace at field end in tables.
  12115. When deleting characters, in tables this function will insert whitespace in
  12116. front of the next \"|\" separator, to keep the table aligned. The table will
  12117. still be marked for re-alignment if the field did fill the entire column,
  12118. because, in this case the deletion might narrow the column."
  12119. (interactive "p")
  12120. (if (and (org-table-p)
  12121. (not (bolp))
  12122. (not (= (char-after) ?|))
  12123. (eq N 1))
  12124. (if (looking-at ".*?|")
  12125. (let ((pos (point))
  12126. (noalign (looking-at "[^|\n\r]* |"))
  12127. (c org-table-may-need-update))
  12128. (replace-match (concat
  12129. (substring (match-string 0) 1 -1)
  12130. " |"))
  12131. (goto-char pos)
  12132. ;; noalign: if there were two spaces at the end, this field
  12133. ;; does not determine the width of the column.
  12134. (if noalign (setq org-table-may-need-update c)))
  12135. (delete-char N))
  12136. (delete-char N)
  12137. (org-fix-tags-on-the-fly)))
  12138. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12139. (put 'org-self-insert-command 'delete-selection t)
  12140. (put 'orgtbl-self-insert-command 'delete-selection t)
  12141. (put 'org-delete-char 'delete-selection 'supersede)
  12142. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12143. (put 'org-yank 'delete-selection 'yank)
  12144. ;; Make `flyspell-mode' delay after some commands
  12145. (put 'org-self-insert-command 'flyspell-delayed t)
  12146. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12147. (put 'org-delete-char 'flyspell-delayed t)
  12148. (put 'org-delete-backward-char 'flyspell-delayed t)
  12149. ;; Make pabbrev-mode expand after org-mode commands
  12150. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12151. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12152. ;; How to do this: Measure non-white length of current string
  12153. ;; If equal to column width, we should realign.
  12154. (defun org-remap (map &rest commands)
  12155. "In MAP, remap the functions given in COMMANDS.
  12156. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12157. (let (new old)
  12158. (while commands
  12159. (setq old (pop commands) new (pop commands))
  12160. (if (fboundp 'command-remapping)
  12161. (org-defkey map (vector 'remap old) new)
  12162. (substitute-key-definition old new map global-map)))))
  12163. (when (eq org-enable-table-editor 'optimized)
  12164. ;; If the user wants maximum table support, we need to hijack
  12165. ;; some standard editing functions
  12166. (org-remap org-mode-map
  12167. 'self-insert-command 'org-self-insert-command
  12168. 'delete-char 'org-delete-char
  12169. 'delete-backward-char 'org-delete-backward-char)
  12170. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12171. (defvar org-ctrl-c-ctrl-c-hook nil
  12172. "Hook for functions attaching themselves to `C-c C-c'.
  12173. This can be used to add additional functionality to the C-c C-c key which
  12174. executes context-dependent commands.
  12175. Each function will be called with no arguments. The function must check
  12176. if the context is appropriate for it to act. If yes, it should do its
  12177. thing and then return a non-nil value. If the context is wrong,
  12178. just do nothing.")
  12179. (defvar org-metaleft-hook nil
  12180. "Hook for functions attaching themselves to `M-left'.
  12181. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12182. (defvar org-metaright-hook nil
  12183. "Hook for functions attaching themselves to `M-right'.
  12184. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12185. (defvar org-metaup-hook nil
  12186. "Hook for functions attaching themselves to `M-up'.
  12187. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12188. (defvar org-metadown-hook nil
  12189. "Hook for functions attaching themselves to `M-down'.
  12190. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12191. (defvar org-shiftmetaleft-hook nil
  12192. "Hook for functions attaching themselves to `M-S-left'.
  12193. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12194. (defvar org-shiftmetaright-hook nil
  12195. "Hook for functions attaching themselves to `M-S-right'.
  12196. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12197. (defvar org-shiftmetaup-hook nil
  12198. "Hook for functions attaching themselves to `M-S-up'.
  12199. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12200. (defvar org-shiftmetadown-hook nil
  12201. "Hook for functions attaching themselves to `M-S-down'.
  12202. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12203. (defvar org-metareturn-hook nil
  12204. "Hook for functions attaching themselves to `M-RET'.
  12205. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12206. (defun org-modifier-cursor-error ()
  12207. "Throw an error, a modified cursor command was applied in wrong context."
  12208. (error "This command is active in special context like tables, headlines or items"))
  12209. (defun org-shiftselect-error ()
  12210. "Throw an error because Shift-Cursor command was applied in wrong context."
  12211. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12212. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12213. (error "This command works only in special context like headlines or timestamps.")))
  12214. (defun org-call-for-shift-select (cmd)
  12215. (let ((this-command-keys-shift-translated t))
  12216. (call-interactively cmd)))
  12217. (defun org-shifttab (&optional arg)
  12218. "Global visibility cycling or move to previous table field.
  12219. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12220. on context.
  12221. See the individual commands for more information."
  12222. (interactive "P")
  12223. (cond
  12224. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12225. ((integerp arg)
  12226. (message "Content view to level: %d" arg)
  12227. (org-content (prefix-numeric-value arg))
  12228. (setq org-cycle-global-status 'overview))
  12229. (t (call-interactively 'org-global-cycle))))
  12230. (defun org-shiftmetaleft ()
  12231. "Promote subtree or delete table column.
  12232. Calls `org-promote-subtree', `org-outdent-item',
  12233. or `org-table-delete-column', depending on context.
  12234. See the individual commands for more information."
  12235. (interactive)
  12236. (cond
  12237. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12238. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12239. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12240. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12241. (t (org-modifier-cursor-error))))
  12242. (defun org-shiftmetaright ()
  12243. "Demote subtree or insert table column.
  12244. Calls `org-demote-subtree', `org-indent-item',
  12245. or `org-table-insert-column', depending on context.
  12246. See the individual commands for more information."
  12247. (interactive)
  12248. (cond
  12249. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12250. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12251. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12252. ((org-at-item-p) (call-interactively 'org-indent-item))
  12253. (t (org-modifier-cursor-error))))
  12254. (defun org-shiftmetaup (&optional arg)
  12255. "Move subtree up or kill table row.
  12256. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12257. `org-move-item-up' depending on context. See the individual commands
  12258. for more information."
  12259. (interactive "P")
  12260. (cond
  12261. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12262. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12263. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12264. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12265. (t (org-modifier-cursor-error))))
  12266. (defun org-shiftmetadown (&optional arg)
  12267. "Move subtree down or insert table row.
  12268. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12269. `org-move-item-down', depending on context. See the individual
  12270. commands for more information."
  12271. (interactive "P")
  12272. (cond
  12273. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12274. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12275. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12276. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12277. (t (org-modifier-cursor-error))))
  12278. (defun org-metaleft (&optional arg)
  12279. "Promote heading or move table column to left.
  12280. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12281. With no specific context, calls the Emacs default `backward-word'.
  12282. See the individual commands for more information."
  12283. (interactive "P")
  12284. (cond
  12285. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12286. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12287. ((or (org-on-heading-p) (org-region-active-p))
  12288. (call-interactively 'org-do-promote))
  12289. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12290. (t (call-interactively 'backward-word))))
  12291. (defun org-metaright (&optional arg)
  12292. "Demote subtree or move table column to right.
  12293. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12294. With no specific context, calls the Emacs default `forward-word'.
  12295. See the individual commands for more information."
  12296. (interactive "P")
  12297. (cond
  12298. ((run-hook-with-args-until-success 'org-metaright-hook))
  12299. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12300. ((or (org-on-heading-p) (org-region-active-p))
  12301. (call-interactively 'org-do-demote))
  12302. ((org-at-item-p) (call-interactively 'org-indent-item))
  12303. (t (call-interactively 'forward-word))))
  12304. (defun org-metaup (&optional arg)
  12305. "Move subtree up or move table row up.
  12306. Calls `org-move-subtree-up' or `org-table-move-row' or
  12307. `org-move-item-up', depending on context. See the individual commands
  12308. for more information."
  12309. (interactive "P")
  12310. (cond
  12311. ((run-hook-with-args-until-success 'org-metaup-hook))
  12312. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12313. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12314. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12315. (t (transpose-lines 1) (beginning-of-line -1))))
  12316. (defun org-metadown (&optional arg)
  12317. "Move subtree down or move table row down.
  12318. Calls `org-move-subtree-down' or `org-table-move-row' or
  12319. `org-move-item-down', depending on context. See the individual
  12320. commands for more information."
  12321. (interactive "P")
  12322. (cond
  12323. ((run-hook-with-args-until-success 'org-metadown-hook))
  12324. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12325. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12326. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12327. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12328. (defun org-shiftup (&optional arg)
  12329. "Increase item in timestamp or increase priority of current headline.
  12330. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12331. depending on context. See the individual commands for more information."
  12332. (interactive "P")
  12333. (cond
  12334. ((and org-support-shift-select (org-region-active-p))
  12335. (org-call-for-shift-select 'previous-line))
  12336. ((org-at-timestamp-p t)
  12337. (call-interactively (if org-edit-timestamp-down-means-later
  12338. 'org-timestamp-down 'org-timestamp-up)))
  12339. ((and (not (eq org-support-shift-select 'always))
  12340. (org-on-heading-p))
  12341. (call-interactively 'org-priority-up))
  12342. ((and (not org-support-shift-select) (org-at-item-p))
  12343. (call-interactively 'org-previous-item))
  12344. ((org-clocktable-try-shift 'up arg))
  12345. (org-support-shift-select
  12346. (org-call-for-shift-select 'previous-line))
  12347. (t (org-shiftselect-error))))
  12348. (defun org-shiftdown (&optional arg)
  12349. "Decrease item in timestamp or decrease priority of current headline.
  12350. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12351. depending on context. See the individual commands for more information."
  12352. (interactive "P")
  12353. (cond
  12354. ((and org-support-shift-select (org-region-active-p))
  12355. (org-call-for-shift-select 'next-line))
  12356. ((org-at-timestamp-p t)
  12357. (call-interactively (if org-edit-timestamp-down-means-later
  12358. 'org-timestamp-up 'org-timestamp-down)))
  12359. ((and (not (eq org-support-shift-select 'always))
  12360. (org-on-heading-p))
  12361. (call-interactively 'org-priority-down))
  12362. ((and (not org-support-shift-select) (org-at-item-p))
  12363. (call-interactively 'org-next-item))
  12364. ((org-clocktable-try-shift 'down arg))
  12365. (org-support-shift-select
  12366. (org-call-for-shift-select 'next-line))
  12367. (t (org-shiftselect-error))))
  12368. (defun org-shiftright (&optional arg)
  12369. "Cycle the thing at point or in the current line, depending on context.
  12370. Depending on context, this does one of the following:
  12371. - switch a timestamp at point one day into the future
  12372. - on a headline, switch to the next TODO keyword.
  12373. - on an item, switch entire list to the next bullet type
  12374. - on a property line, switch to the next allowed value
  12375. - on a clocktable definition line, move time block into the future"
  12376. (interactive "P")
  12377. (cond
  12378. ((and org-support-shift-select (org-region-active-p))
  12379. (org-call-for-shift-select 'forward-char))
  12380. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12381. ((and (not (eq org-support-shift-select 'always))
  12382. (org-on-heading-p))
  12383. (org-call-with-arg 'org-todo 'right))
  12384. ((or (and org-support-shift-select
  12385. (not (eq org-support-shift-select 'always))
  12386. (org-at-item-bullet-p))
  12387. (and (not org-support-shift-select) (org-at-item-p)))
  12388. (org-call-with-arg 'org-cycle-list-bullet nil))
  12389. ((and (not (eq org-support-shift-select 'always))
  12390. (org-at-property-p))
  12391. (call-interactively 'org-property-next-allowed-value))
  12392. ((org-clocktable-try-shift 'right arg))
  12393. (org-support-shift-select
  12394. (org-call-for-shift-select 'forward-char))
  12395. (t (org-shiftselect-error))))
  12396. (defun org-shiftleft (&optional arg)
  12397. "Cycle the thing at point or in the current line, depending on context.
  12398. Depending on context, this does one of the following:
  12399. - switch a timestamp at point one day into the past
  12400. - on a headline, switch to the previous TODO keyword.
  12401. - on an item, switch entire list to the previous bullet type
  12402. - on a property line, switch to the previous allowed value
  12403. - on a clocktable definition line, move time block into the past"
  12404. (interactive "P")
  12405. (cond
  12406. ((and org-support-shift-select (org-region-active-p))
  12407. (org-call-for-shift-select 'backward-char))
  12408. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12409. ((and (not (eq org-support-shift-select 'always))
  12410. (org-on-heading-p))
  12411. (org-call-with-arg 'org-todo 'left))
  12412. ((or (and org-support-shift-select
  12413. (not (eq org-support-shift-select 'always))
  12414. (org-at-item-bullet-p))
  12415. (and (not org-support-shift-select) (org-at-item-p)))
  12416. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12417. ((and (not (eq org-support-shift-select 'always))
  12418. (org-at-property-p))
  12419. (call-interactively 'org-property-previous-allowed-value))
  12420. ((org-clocktable-try-shift 'left arg))
  12421. (org-support-shift-select
  12422. (org-call-for-shift-select 'backward-char))
  12423. (t (org-shiftselect-error))))
  12424. (defun org-shiftcontrolright ()
  12425. "Switch to next TODO set."
  12426. (interactive)
  12427. (cond
  12428. ((and org-support-shift-select (org-region-active-p))
  12429. (org-call-for-shift-select 'forward-word))
  12430. ((and (not (eq org-support-shift-select 'always))
  12431. (org-on-heading-p))
  12432. (org-call-with-arg 'org-todo 'nextset))
  12433. (org-support-shift-select
  12434. (org-call-for-shift-select 'forward-word))
  12435. (t (org-shiftselect-error))))
  12436. (defun org-shiftcontrolleft ()
  12437. "Switch to previous TODO set."
  12438. (interactive)
  12439. (cond
  12440. ((and org-support-shift-select (org-region-active-p))
  12441. (org-call-for-shift-select 'backward-word))
  12442. ((and (not (eq org-support-shift-select 'always))
  12443. (org-on-heading-p))
  12444. (org-call-with-arg 'org-todo 'previousset))
  12445. (org-support-shift-select
  12446. (org-call-for-shift-select 'backward-word))
  12447. (t (org-shiftselect-error))))
  12448. (defun org-ctrl-c-ret ()
  12449. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12450. (interactive)
  12451. (cond
  12452. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12453. (t (call-interactively 'org-insert-heading))))
  12454. (defun org-copy-special ()
  12455. "Copy region in table or copy current subtree.
  12456. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12457. See the individual commands for more information."
  12458. (interactive)
  12459. (call-interactively
  12460. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12461. (defun org-cut-special ()
  12462. "Cut region in table or cut current subtree.
  12463. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12464. See the individual commands for more information."
  12465. (interactive)
  12466. (call-interactively
  12467. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12468. (defun org-paste-special (arg)
  12469. "Paste rectangular region into table, or past subtree relative to level.
  12470. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12471. See the individual commands for more information."
  12472. (interactive "P")
  12473. (if (org-at-table-p)
  12474. (org-table-paste-rectangle)
  12475. (org-paste-subtree arg)))
  12476. (defun org-edit-special ()
  12477. "Call a special editor for the stuff at point.
  12478. When at a table, call the formula editor with `org-table-edit-formulas'.
  12479. When at the first line of an src example, call `org-edit-src-code'.
  12480. When in an #+include line, visit the include file. Otherwise call
  12481. `ffap' to visit the file at point."
  12482. (interactive)
  12483. (cond
  12484. ((org-at-table-p)
  12485. (call-interactively 'org-table-edit-formulas))
  12486. ((save-excursion
  12487. (beginning-of-line 1)
  12488. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12489. (find-file (org-trim (match-string 1))))
  12490. ((org-edit-src-code))
  12491. ((org-edit-fixed-width-region))
  12492. (t (call-interactively 'ffap))))
  12493. (defun org-ctrl-c-ctrl-c (&optional arg)
  12494. "Set tags in headline, or update according to changed information at point.
  12495. This command does many different things, depending on context:
  12496. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12497. this is what we do.
  12498. - If the cursor is in a headline, prompt for tags and insert them
  12499. into the current line, aligned to `org-tags-column'. When called
  12500. with prefix arg, realign all tags in the current buffer.
  12501. - If the cursor is in one of the special #+KEYWORD lines, this
  12502. triggers scanning the buffer for these lines and updating the
  12503. information.
  12504. - If the cursor is inside a table, realign the table. This command
  12505. works even if the automatic table editor has been turned off.
  12506. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12507. the entire table.
  12508. - If the cursor is at a footnote reference or definition, jump to
  12509. the corresponding definition or references, respectively.
  12510. - If the cursor is a the beginning of a dynamic block, update it.
  12511. - If the cursor is inside a table created by the table.el package,
  12512. activate that table.
  12513. - If the current buffer is a remember buffer, close note and file
  12514. it. A prefix argument of 1 files to the default location
  12515. without further interaction. A prefix argument of 2 files to
  12516. the currently clocking task.
  12517. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12518. links in this buffer.
  12519. - If the cursor is on a numbered item in a plain list, renumber the
  12520. ordered list.
  12521. - If the cursor is on a checkbox, toggle it."
  12522. (interactive "P")
  12523. (let ((org-enable-table-editor t))
  12524. (cond
  12525. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12526. org-occur-highlights
  12527. org-latex-fragment-image-overlays)
  12528. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12529. (org-remove-occur-highlights)
  12530. (org-remove-latex-fragment-image-overlays)
  12531. (message "Temporary highlights/overlays removed from current buffer"))
  12532. ((and (local-variable-p 'org-finish-function (current-buffer))
  12533. (fboundp org-finish-function))
  12534. (funcall org-finish-function))
  12535. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12536. ((org-at-property-p)
  12537. (call-interactively 'org-property-action))
  12538. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12539. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12540. ((org-at-table.el-p)
  12541. (require 'table)
  12542. (beginning-of-line 1)
  12543. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12544. (call-interactively 'table-recognize-table))
  12545. ((org-at-table-p)
  12546. (org-table-maybe-eval-formula)
  12547. (if arg
  12548. (call-interactively 'org-table-recalculate)
  12549. (org-table-maybe-recalculate-line))
  12550. (call-interactively 'org-table-align))
  12551. ((or (org-footnote-at-reference-p)
  12552. (org-footnote-at-definition-p))
  12553. (call-interactively 'org-footnote-action))
  12554. ((org-at-item-checkbox-p)
  12555. (call-interactively 'org-toggle-checkbox))
  12556. ((org-at-item-p)
  12557. (if arg
  12558. (call-interactively 'org-toggle-checkbox)
  12559. (call-interactively 'org-maybe-renumber-ordered-list)))
  12560. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12561. ;; Dynamic block
  12562. (beginning-of-line 1)
  12563. (save-excursion (org-update-dblock)))
  12564. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12565. (cond
  12566. ((equal (match-string 1) "TBLFM")
  12567. ;; Recalculate the table before this line
  12568. (save-excursion
  12569. (beginning-of-line 1)
  12570. (skip-chars-backward " \r\n\t")
  12571. (if (org-at-table-p)
  12572. (org-call-with-arg 'org-table-recalculate t))))
  12573. (t
  12574. ; (org-set-regexps-and-options)
  12575. ; (org-restart-font-lock)
  12576. (let ((org-inhibit-startup t)) (org-mode-restart))
  12577. (message "Local setup has been refreshed"))))
  12578. (t (error "C-c C-c can do nothing useful at this location.")))))
  12579. (defun org-mode-restart ()
  12580. "Restart Org-mode, to scan again for special lines.
  12581. Also updates the keyword regular expressions."
  12582. (interactive)
  12583. (org-mode)
  12584. (message "Org-mode restarted"))
  12585. (defun org-kill-note-or-show-branches ()
  12586. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12587. (interactive)
  12588. (if (not org-finish-function)
  12589. (call-interactively 'show-branches)
  12590. (let ((org-note-abort t))
  12591. (funcall org-finish-function))))
  12592. (defun org-return (&optional indent)
  12593. "Goto next table row or insert a newline.
  12594. Calls `org-table-next-row' or `newline', depending on context.
  12595. See the individual commands for more information."
  12596. (interactive)
  12597. (cond
  12598. ((bobp) (if indent (newline-and-indent) (newline)))
  12599. ((org-at-table-p)
  12600. (org-table-justify-field-maybe)
  12601. (call-interactively 'org-table-next-row))
  12602. ((and org-return-follows-link
  12603. (eq (get-text-property (point) 'face) 'org-link))
  12604. (call-interactively 'org-open-at-point))
  12605. ((and (org-at-heading-p)
  12606. (looking-at
  12607. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12608. (org-show-entry)
  12609. (end-of-line 1)
  12610. (newline))
  12611. (t (if indent (newline-and-indent) (newline)))))
  12612. (defun org-return-indent ()
  12613. "Goto next table row or insert a newline and indent.
  12614. Calls `org-table-next-row' or `newline-and-indent', depending on
  12615. context. See the individual commands for more information."
  12616. (interactive)
  12617. (org-return t))
  12618. (defun org-ctrl-c-star ()
  12619. "Compute table, or change heading status of lines.
  12620. Calls `org-table-recalculate' or `org-toggle-heading',
  12621. depending on context."
  12622. (interactive)
  12623. (cond
  12624. ((org-at-table-p)
  12625. (call-interactively 'org-table-recalculate))
  12626. (t
  12627. ;; Convert all lines in region to list items
  12628. (call-interactively 'org-toggle-heading))))
  12629. (defun org-ctrl-c-minus ()
  12630. "Insert separator line in table or modify bullet status of line.
  12631. Also turns a plain line or a region of lines into list items.
  12632. Calls `org-table-insert-hline', `org-toggle-item', or
  12633. `org-cycle-list-bullet', depending on context."
  12634. (interactive)
  12635. (cond
  12636. ((org-at-table-p)
  12637. (call-interactively 'org-table-insert-hline))
  12638. ((org-region-active-p)
  12639. (call-interactively 'org-toggle-item))
  12640. ((org-in-item-p)
  12641. (call-interactively 'org-cycle-list-bullet))
  12642. (t
  12643. (call-interactively 'org-toggle-item))))
  12644. (defun org-toggle-item ()
  12645. "Convert headings or normal lines to items, items to normal lines.
  12646. If there is no active region, only the current line is considered.
  12647. If the first line in the region is a headline, convert all headlines to items.
  12648. If the first line in the region is an item, convert all items to normal lines.
  12649. If the first line is normal text, add an item bullet to each line."
  12650. (interactive)
  12651. (let (l2 l beg end)
  12652. (if (org-region-active-p)
  12653. (setq beg (region-beginning) end (region-end))
  12654. (setq beg (point-at-bol)
  12655. end (min (1+ (point-at-eol)) (point-max))))
  12656. (save-excursion
  12657. (goto-char end)
  12658. (setq l2 (org-current-line))
  12659. (goto-char beg)
  12660. (beginning-of-line 1)
  12661. (setq l (1- (org-current-line)))
  12662. (if (org-at-item-p)
  12663. ;; We already have items, de-itemize
  12664. (while (< (setq l (1+ l)) l2)
  12665. (when (org-at-item-p)
  12666. (goto-char (match-beginning 2))
  12667. (delete-region (match-beginning 2) (match-end 2))
  12668. (and (looking-at "[ \t]+") (replace-match "")))
  12669. (beginning-of-line 2))
  12670. (if (org-on-heading-p)
  12671. ;; Headings, convert to items
  12672. (while (< (setq l (1+ l)) l2)
  12673. (if (looking-at org-outline-regexp)
  12674. (replace-match "- " t t))
  12675. (beginning-of-line 2))
  12676. ;; normal lines, turn them into items
  12677. (while (< (setq l (1+ l)) l2)
  12678. (unless (org-at-item-p)
  12679. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12680. (replace-match "\\1- \\2")))
  12681. (beginning-of-line 2)))))))
  12682. (defun org-toggle-heading (&optional nstars)
  12683. "Convert headings to normal text, or items or text to headings.
  12684. If there is no active region, only the current line is considered.
  12685. If the first line is a heading, remove the stars from all headlines
  12686. in the region.
  12687. If the first line is a plain list item, turn all plain list items into
  12688. headings.
  12689. If the first line is a normal line, turn each and every line in the region
  12690. into a heading.
  12691. When converting a line into a heading, the number of stars is chosen
  12692. such that the lines become children of the current entry. However, when
  12693. a prefix argument is given, its value determines the number of stars to add."
  12694. (interactive "P")
  12695. (let (l2 l itemp beg end)
  12696. (if (org-region-active-p)
  12697. (setq beg (region-beginning) end (region-end))
  12698. (setq beg (point-at-bol)
  12699. end (min (1+ (point-at-eol)) (point-max))))
  12700. (save-excursion
  12701. (goto-char end)
  12702. (setq l2 (org-current-line))
  12703. (goto-char beg)
  12704. (beginning-of-line 1)
  12705. (setq l (1- (org-current-line)))
  12706. (if (org-on-heading-p)
  12707. ;; We already have headlines, de-star them
  12708. (while (< (setq l (1+ l)) l2)
  12709. (when (org-on-heading-p t)
  12710. (and (looking-at outline-regexp) (replace-match "")))
  12711. (beginning-of-line 2))
  12712. (setq itemp (org-at-item-p))
  12713. (let* ((stars
  12714. (if nstars
  12715. (make-string (prefix-numeric-value current-prefix-arg)
  12716. ?*)
  12717. (save-excursion
  12718. (re-search-backward org-complex-heading-regexp nil t)
  12719. (or (match-string 1) "*"))))
  12720. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12721. (rpl (concat stars add-stars " ")))
  12722. (while (< (setq l (1+ l)) l2)
  12723. (if itemp
  12724. (and (org-at-item-p) (replace-match rpl t t))
  12725. (unless (org-on-heading-p)
  12726. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12727. (replace-match (concat rpl (match-string 2))))))
  12728. (beginning-of-line 2)))))))
  12729. (defun org-meta-return (&optional arg)
  12730. "Insert a new heading or wrap a region in a table.
  12731. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12732. See the individual commands for more information."
  12733. (interactive "P")
  12734. (cond
  12735. ((run-hook-with-args-until-success 'org-metareturn-hook))
  12736. ((org-at-table-p)
  12737. (call-interactively 'org-table-wrap-region))
  12738. (t (call-interactively 'org-insert-heading))))
  12739. ;;; Menu entries
  12740. ;; Define the Org-mode menus
  12741. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12742. '("Tbl"
  12743. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12744. ["Next Field" org-cycle (org-at-table-p)]
  12745. ["Previous Field" org-shifttab (org-at-table-p)]
  12746. ["Next Row" org-return (org-at-table-p)]
  12747. "--"
  12748. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12749. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12750. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12751. "--"
  12752. ("Column"
  12753. ["Move Column Left" org-metaleft (org-at-table-p)]
  12754. ["Move Column Right" org-metaright (org-at-table-p)]
  12755. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12756. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12757. ("Row"
  12758. ["Move Row Up" org-metaup (org-at-table-p)]
  12759. ["Move Row Down" org-metadown (org-at-table-p)]
  12760. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12761. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12762. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12763. "--"
  12764. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12765. ("Rectangle"
  12766. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12767. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12768. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12769. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12770. "--"
  12771. ("Calculate"
  12772. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12773. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12774. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12775. "--"
  12776. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12777. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12778. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12779. "--"
  12780. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12781. "--"
  12782. ["Sum Column/Rectangle" org-table-sum
  12783. (or (org-at-table-p) (org-region-active-p))]
  12784. ["Which Column?" org-table-current-column (org-at-table-p)])
  12785. ["Debug Formulas"
  12786. org-table-toggle-formula-debugger
  12787. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12788. ["Show Col/Row Numbers"
  12789. org-table-toggle-coordinate-overlays
  12790. :style toggle
  12791. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12792. "--"
  12793. ["Create" org-table-create (and (not (org-at-table-p))
  12794. org-enable-table-editor)]
  12795. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12796. ["Import from File" org-table-import (not (org-at-table-p))]
  12797. ["Export to File" org-table-export (org-at-table-p)]
  12798. "--"
  12799. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12800. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12801. '("Org"
  12802. ("Show/Hide"
  12803. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12804. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12805. ["Sparse Tree..." org-sparse-tree t]
  12806. ["Reveal Context" org-reveal t]
  12807. ["Show All" show-all t]
  12808. "--"
  12809. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12810. "--"
  12811. ["New Heading" org-insert-heading t]
  12812. ("Navigate Headings"
  12813. ["Up" outline-up-heading t]
  12814. ["Next" outline-next-visible-heading t]
  12815. ["Previous" outline-previous-visible-heading t]
  12816. ["Next Same Level" outline-forward-same-level t]
  12817. ["Previous Same Level" outline-backward-same-level t]
  12818. "--"
  12819. ["Jump" org-goto t])
  12820. ("Edit Structure"
  12821. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12822. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12823. "--"
  12824. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12825. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12826. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12827. "--"
  12828. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12829. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12830. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12831. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12832. "--"
  12833. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12834. "--"
  12835. ["Convert to odd levels" org-convert-to-odd-levels t]
  12836. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12837. ("Editing"
  12838. ["Emphasis..." org-emphasize t]
  12839. ["Edit Source Example" org-edit-special t]
  12840. "--"
  12841. ["Footnote new/jump" org-footnote-action t]
  12842. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12843. ("Archive"
  12844. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12845. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12846. ; :active t :keys "C-u C-c C-x C-a"]
  12847. ["Sparse trees open ARCHIVE trees"
  12848. (setq org-sparse-tree-open-archived-trees
  12849. (not org-sparse-tree-open-archived-trees))
  12850. :style toggle :selected org-sparse-tree-open-archived-trees]
  12851. ["Cycling opens ARCHIVE trees"
  12852. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12853. :style toggle :selected org-cycle-open-archived-trees]
  12854. "--"
  12855. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12856. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12857. ; ["Check and Move Children" (org-archive-subtree '(4))
  12858. ; :active t :keys "C-u C-c C-x C-s"]
  12859. )
  12860. "--"
  12861. ("TODO Lists"
  12862. ["TODO/DONE/-" org-todo t]
  12863. ("Select keyword"
  12864. ["Next keyword" org-shiftright (org-on-heading-p)]
  12865. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12866. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12867. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12868. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12869. ["Show TODO Tree" org-show-todo-tree t]
  12870. ["Global TODO list" org-todo-list t]
  12871. "--"
  12872. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12873. :selected org-enforce-todo-dependencies :style toggle :active t]
  12874. "Settings for tree at point"
  12875. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12876. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12877. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12878. ["Do Children parallel" org-toggle-ordered-property :style radio
  12879. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12880. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12881. "--"
  12882. ["Set Priority" org-priority t]
  12883. ["Priority Up" org-shiftup t]
  12884. ["Priority Down" org-shiftdown t])
  12885. ("TAGS and Properties"
  12886. ["Set Tags" org-set-tags-command t]
  12887. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12888. "--"
  12889. ["Set property" org-set-property t]
  12890. ["Column view of properties" org-columns t]
  12891. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12892. ("Dates and Scheduling"
  12893. ["Timestamp" org-time-stamp t]
  12894. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12895. ("Change Date"
  12896. ["1 Day Later" org-shiftright t]
  12897. ["1 Day Earlier" org-shiftleft t]
  12898. ["1 ... Later" org-shiftup t]
  12899. ["1 ... Earlier" org-shiftdown t])
  12900. ["Compute Time Range" org-evaluate-time-range t]
  12901. ["Schedule Item" org-schedule t]
  12902. ["Deadline" org-deadline t]
  12903. "--"
  12904. ["Custom time format" org-toggle-time-stamp-overlays
  12905. :style radio :selected org-display-custom-times]
  12906. "--"
  12907. ["Goto Calendar" org-goto-calendar t]
  12908. ["Date from Calendar" org-date-from-calendar t]
  12909. "--"
  12910. ["Start/Restart Timer" org-timer-start t]
  12911. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12912. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12913. ["Insert Timer String" org-timer t]
  12914. ["Insert Timer Item" org-timer-item t])
  12915. ("Logging work"
  12916. ["Clock in" org-clock-in t]
  12917. ["Clock out" org-clock-out t]
  12918. ["Clock cancel" org-clock-cancel t]
  12919. ["Goto running clock" org-clock-goto t]
  12920. ["Display times" org-clock-display t]
  12921. ["Create clock table" org-clock-report t]
  12922. "--"
  12923. ["Record DONE time"
  12924. (progn (setq org-log-done (not org-log-done))
  12925. (message "Switching to %s will %s record a timestamp"
  12926. (car org-done-keywords)
  12927. (if org-log-done "automatically" "not")))
  12928. :style toggle :selected org-log-done])
  12929. "--"
  12930. ["Agenda Command..." org-agenda t]
  12931. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12932. ("File List for Agenda")
  12933. ("Special views current file"
  12934. ["TODO Tree" org-show-todo-tree t]
  12935. ["Check Deadlines" org-check-deadlines t]
  12936. ["Timeline" org-timeline t]
  12937. ["Tags Tree" org-tags-sparse-tree t])
  12938. "--"
  12939. ("Hyperlinks"
  12940. ["Store Link (Global)" org-store-link t]
  12941. ["Insert Link" org-insert-link t]
  12942. ["Follow Link" org-open-at-point t]
  12943. "--"
  12944. ["Next link" org-next-link t]
  12945. ["Previous link" org-previous-link t]
  12946. "--"
  12947. ["Descriptive Links"
  12948. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12949. :style radio
  12950. :selected (member '(org-link) buffer-invisibility-spec)]
  12951. ["Literal Links"
  12952. (progn
  12953. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12954. :style radio
  12955. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12956. "--"
  12957. ["Export/Publish..." org-export t]
  12958. ("LaTeX"
  12959. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12960. :selected org-cdlatex-mode]
  12961. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12962. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12963. ["Modify math symbol" org-cdlatex-math-modify
  12964. (org-inside-LaTeX-fragment-p)]
  12965. ["Export LaTeX fragments as images"
  12966. (if (featurep 'org-exp)
  12967. (setq org-export-with-LaTeX-fragments
  12968. (not org-export-with-LaTeX-fragments))
  12969. (require 'org-exp))
  12970. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12971. org-export-with-LaTeX-fragments)])
  12972. "--"
  12973. ("Documentation"
  12974. ["Show Version" org-version t]
  12975. ["Info Documentation" org-info t])
  12976. ("Customize"
  12977. ["Browse Org Group" org-customize t]
  12978. "--"
  12979. ["Expand This Menu" org-create-customize-menu
  12980. (fboundp 'customize-menu-create)])
  12981. "--"
  12982. ["Refresh setup" org-mode-restart t]
  12983. ))
  12984. (defun org-info (&optional node)
  12985. "Read documentation for Org-mode in the info system.
  12986. With optional NODE, go directly to that node."
  12987. (interactive)
  12988. (info (format "(org)%s" (or node ""))))
  12989. (defun org-install-agenda-files-menu ()
  12990. (let ((bl (buffer-list)))
  12991. (save-excursion
  12992. (while bl
  12993. (set-buffer (pop bl))
  12994. (if (org-mode-p) (setq bl nil)))
  12995. (when (org-mode-p)
  12996. (easy-menu-change
  12997. '("Org") "File List for Agenda"
  12998. (append
  12999. (list
  13000. ["Edit File List" (org-edit-agenda-file-list) t]
  13001. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13002. ["Remove Current File from List" org-remove-file t]
  13003. ["Cycle through agenda files" org-cycle-agenda-files t]
  13004. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13005. "--")
  13006. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13007. ;;;; Documentation
  13008. ;;;###autoload
  13009. (defun org-require-autoloaded-modules ()
  13010. (interactive)
  13011. (mapc 'require
  13012. '(org-agenda org-archive org-attach org-clock org-colview
  13013. org-exp org-id org-export-latex org-publish
  13014. org-remember org-table org-timer)))
  13015. ;;;###autoload
  13016. (defun org-customize ()
  13017. "Call the customize function with org as argument."
  13018. (interactive)
  13019. (org-load-modules-maybe)
  13020. (org-require-autoloaded-modules)
  13021. (customize-browse 'org))
  13022. (defun org-create-customize-menu ()
  13023. "Create a full customization menu for Org-mode, insert it into the menu."
  13024. (interactive)
  13025. (org-load-modules-maybe)
  13026. (org-require-autoloaded-modules)
  13027. (if (fboundp 'customize-menu-create)
  13028. (progn
  13029. (easy-menu-change
  13030. '("Org") "Customize"
  13031. `(["Browse Org group" org-customize t]
  13032. "--"
  13033. ,(customize-menu-create 'org)
  13034. ["Set" Custom-set t]
  13035. ["Save" Custom-save t]
  13036. ["Reset to Current" Custom-reset-current t]
  13037. ["Reset to Saved" Custom-reset-saved t]
  13038. ["Reset to Standard Settings" Custom-reset-standard t]))
  13039. (message "\"Org\"-menu now contains full customization menu"))
  13040. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13041. ;;;; Miscellaneous stuff
  13042. ;;; Generally useful functions
  13043. (defun org-find-text-property-in-string (prop s)
  13044. "Return the first non-nil value of property PROP in string S."
  13045. (or (get-text-property 0 prop s)
  13046. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13047. prop s)))
  13048. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13049. "Display the given MESSAGE as a warning."
  13050. (if (fboundp 'display-warning)
  13051. (display-warning 'org message
  13052. (if (featurep 'xemacs)
  13053. 'warning
  13054. :warning))
  13055. (let ((buf (get-buffer-create "*Org warnings*")))
  13056. (with-current-buffer buf
  13057. (goto-char (point-max))
  13058. (insert "Warning (Org): " message)
  13059. (unless (bolp)
  13060. (newline)))
  13061. (display-buffer buf)
  13062. (sit-for 0))))
  13063. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13064. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13065. (if (and marker (marker-buffer marker)
  13066. (buffer-live-p (marker-buffer marker)))
  13067. (progn
  13068. (switch-to-buffer (marker-buffer marker))
  13069. (if (or (> marker (point-max)) (< marker (point-min)))
  13070. (widen))
  13071. (goto-char marker)
  13072. (org-show-context 'org-goto))
  13073. (if bookmark
  13074. (bookmark-jump bookmark)
  13075. (error "Cannot find location"))))
  13076. (defun org-quote-csv-field (s)
  13077. "Quote field for inclusion in CSV material."
  13078. (if (string-match "[\",]" s)
  13079. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13080. s))
  13081. (defun org-plist-delete (plist property)
  13082. "Delete PROPERTY from PLIST.
  13083. This is in contrast to merely setting it to 0."
  13084. (let (p)
  13085. (while plist
  13086. (if (not (eq property (car plist)))
  13087. (setq p (plist-put p (car plist) (nth 1 plist))))
  13088. (setq plist (cddr plist)))
  13089. p))
  13090. (defun org-force-self-insert (N)
  13091. "Needed to enforce self-insert under remapping."
  13092. (interactive "p")
  13093. (self-insert-command N))
  13094. (defun org-string-width (s)
  13095. "Compute width of string, ignoring invisible characters.
  13096. This ignores character with invisibility property `org-link', and also
  13097. characters with property `org-cwidth', because these will become invisible
  13098. upon the next fontification round."
  13099. (let (b l)
  13100. (when (or (eq t buffer-invisibility-spec)
  13101. (assq 'org-link buffer-invisibility-spec))
  13102. (while (setq b (text-property-any 0 (length s)
  13103. 'invisible 'org-link s))
  13104. (setq s (concat (substring s 0 b)
  13105. (substring s (or (next-single-property-change
  13106. b 'invisible s) (length s)))))))
  13107. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13108. (setq s (concat (substring s 0 b)
  13109. (substring s (or (next-single-property-change
  13110. b 'org-cwidth s) (length s))))))
  13111. (setq l (string-width s) b -1)
  13112. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13113. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13114. l))
  13115. (defun org-get-indentation (&optional line)
  13116. "Get the indentation of the current line, interpreting tabs.
  13117. When LINE is given, assume it represents a line and compute its indentation."
  13118. (if line
  13119. (if (string-match "^ *" (org-remove-tabs line))
  13120. (match-end 0))
  13121. (save-excursion
  13122. (beginning-of-line 1)
  13123. (skip-chars-forward " \t")
  13124. (current-column))))
  13125. (defun org-remove-tabs (s &optional width)
  13126. "Replace tabulators in S with spaces.
  13127. Assumes that s is a single line, starting in column 0."
  13128. (setq width (or width tab-width))
  13129. (while (string-match "\t" s)
  13130. (setq s (replace-match
  13131. (make-string
  13132. (- (* width (/ (+ (match-beginning 0) width) width))
  13133. (match-beginning 0)) ?\ )
  13134. t t s)))
  13135. s)
  13136. (defun org-fix-indentation (line ind)
  13137. "Fix indentation in LINE.
  13138. IND is a cons cell with target and minimum indentation.
  13139. If the current indentation in LINE is smaller than the minimum,
  13140. leave it alone. If it is larger than ind, set it to the target."
  13141. (let* ((l (org-remove-tabs line))
  13142. (i (org-get-indentation l))
  13143. (i1 (car ind)) (i2 (cdr ind)))
  13144. (if (>= i i2) (setq l (substring line i2)))
  13145. (if (> i1 0)
  13146. (concat (make-string i1 ?\ ) l)
  13147. l)))
  13148. (defun org-base-buffer (buffer)
  13149. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13150. (if (not buffer)
  13151. buffer
  13152. (or (buffer-base-buffer buffer)
  13153. buffer)))
  13154. (defun org-trim (s)
  13155. "Remove whitespace at beginning and end of string."
  13156. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13157. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13158. s)
  13159. (defun org-wrap (string &optional width lines)
  13160. "Wrap string to either a number of lines, or a width in characters.
  13161. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13162. that costs. If there is a word longer than WIDTH, the text is actually
  13163. wrapped to the length of that word.
  13164. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13165. many lines, whatever width that takes.
  13166. The return value is a list of lines, without newlines at the end."
  13167. (let* ((words (org-split-string string "[ \t\n]+"))
  13168. (maxword (apply 'max (mapcar 'org-string-width words)))
  13169. w ll)
  13170. (cond (width
  13171. (org-do-wrap words (max maxword width)))
  13172. (lines
  13173. (setq w maxword)
  13174. (setq ll (org-do-wrap words maxword))
  13175. (if (<= (length ll) lines)
  13176. ll
  13177. (setq ll words)
  13178. (while (> (length ll) lines)
  13179. (setq w (1+ w))
  13180. (setq ll (org-do-wrap words w)))
  13181. ll))
  13182. (t (error "Cannot wrap this")))))
  13183. (defun org-do-wrap (words width)
  13184. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13185. (let (lines line)
  13186. (while words
  13187. (setq line (pop words))
  13188. (while (and words (< (+ (length line) (length (car words))) width))
  13189. (setq line (concat line " " (pop words))))
  13190. (setq lines (push line lines)))
  13191. (nreverse lines)))
  13192. (defun org-split-string (string &optional separators)
  13193. "Splits STRING into substrings at SEPARATORS.
  13194. No empty strings are returned if there are matches at the beginning
  13195. and end of string."
  13196. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13197. (start 0)
  13198. notfirst
  13199. (list nil))
  13200. (while (and (string-match rexp string
  13201. (if (and notfirst
  13202. (= start (match-beginning 0))
  13203. (< start (length string)))
  13204. (1+ start) start))
  13205. (< (match-beginning 0) (length string)))
  13206. (setq notfirst t)
  13207. (or (eq (match-beginning 0) 0)
  13208. (and (eq (match-beginning 0) (match-end 0))
  13209. (eq (match-beginning 0) start))
  13210. (setq list
  13211. (cons (substring string start (match-beginning 0))
  13212. list)))
  13213. (setq start (match-end 0)))
  13214. (or (eq start (length string))
  13215. (setq list
  13216. (cons (substring string start)
  13217. list)))
  13218. (nreverse list)))
  13219. (defun org-context ()
  13220. "Return a list of contexts of the current cursor position.
  13221. If several contexts apply, all are returned.
  13222. Each context entry is a list with a symbol naming the context, and
  13223. two positions indicating start and end of the context. Possible
  13224. contexts are:
  13225. :headline anywhere in a headline
  13226. :headline-stars on the leading stars in a headline
  13227. :todo-keyword on a TODO keyword (including DONE) in a headline
  13228. :tags on the TAGS in a headline
  13229. :priority on the priority cookie in a headline
  13230. :item on the first line of a plain list item
  13231. :item-bullet on the bullet/number of a plain list item
  13232. :checkbox on the checkbox in a plain list item
  13233. :table in an org-mode table
  13234. :table-special on a special filed in a table
  13235. :table-table in a table.el table
  13236. :link on a hyperlink
  13237. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13238. :target on a <<target>>
  13239. :radio-target on a <<<radio-target>>>
  13240. :latex-fragment on a LaTeX fragment
  13241. :latex-preview on a LaTeX fragment with overlayed preview image
  13242. This function expects the position to be visible because it uses font-lock
  13243. faces as a help to recognize the following contexts: :table-special, :link,
  13244. and :keyword."
  13245. (let* ((f (get-text-property (point) 'face))
  13246. (faces (if (listp f) f (list f)))
  13247. (p (point)) clist o)
  13248. ;; First the large context
  13249. (cond
  13250. ((org-on-heading-p t)
  13251. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13252. (when (progn
  13253. (beginning-of-line 1)
  13254. (looking-at org-todo-line-tags-regexp))
  13255. (push (org-point-in-group p 1 :headline-stars) clist)
  13256. (push (org-point-in-group p 2 :todo-keyword) clist)
  13257. (push (org-point-in-group p 4 :tags) clist))
  13258. (goto-char p)
  13259. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13260. (if (looking-at "\\[#[A-Z0-9]\\]")
  13261. (push (org-point-in-group p 0 :priority) clist)))
  13262. ((org-at-item-p)
  13263. (push (org-point-in-group p 2 :item-bullet) clist)
  13264. (push (list :item (point-at-bol)
  13265. (save-excursion (org-end-of-item) (point)))
  13266. clist)
  13267. (and (org-at-item-checkbox-p)
  13268. (push (org-point-in-group p 0 :checkbox) clist)))
  13269. ((org-at-table-p)
  13270. (push (list :table (org-table-begin) (org-table-end)) clist)
  13271. (if (memq 'org-formula faces)
  13272. (push (list :table-special
  13273. (previous-single-property-change p 'face)
  13274. (next-single-property-change p 'face)) clist)))
  13275. ((org-at-table-p 'any)
  13276. (push (list :table-table) clist)))
  13277. (goto-char p)
  13278. ;; Now the small context
  13279. (cond
  13280. ((org-at-timestamp-p)
  13281. (push (org-point-in-group p 0 :timestamp) clist))
  13282. ((memq 'org-link faces)
  13283. (push (list :link
  13284. (previous-single-property-change p 'face)
  13285. (next-single-property-change p 'face)) clist))
  13286. ((memq 'org-special-keyword faces)
  13287. (push (list :keyword
  13288. (previous-single-property-change p 'face)
  13289. (next-single-property-change p 'face)) clist))
  13290. ((org-on-target-p)
  13291. (push (org-point-in-group p 0 :target) clist)
  13292. (goto-char (1- (match-beginning 0)))
  13293. (if (looking-at org-radio-target-regexp)
  13294. (push (org-point-in-group p 0 :radio-target) clist))
  13295. (goto-char p))
  13296. ((setq o (car (delq nil
  13297. (mapcar
  13298. (lambda (x)
  13299. (if (memq x org-latex-fragment-image-overlays) x))
  13300. (org-overlays-at (point))))))
  13301. (push (list :latex-fragment
  13302. (org-overlay-start o) (org-overlay-end o)) clist)
  13303. (push (list :latex-preview
  13304. (org-overlay-start o) (org-overlay-end o)) clist))
  13305. ((org-inside-LaTeX-fragment-p)
  13306. ;; FIXME: positions wrong.
  13307. (push (list :latex-fragment (point) (point)) clist)))
  13308. (setq clist (nreverse (delq nil clist)))
  13309. clist))
  13310. ;; FIXME: Compare with at-regexp-p Do we need both?
  13311. (defun org-in-regexp (re &optional nlines visually)
  13312. "Check if point is inside a match of regexp.
  13313. Normally only the current line is checked, but you can include NLINES extra
  13314. lines both before and after point into the search.
  13315. If VISUALLY is set, require that the cursor is not after the match but
  13316. really on, so that the block visually is on the match."
  13317. (catch 'exit
  13318. (let ((pos (point))
  13319. (eol (point-at-eol (+ 1 (or nlines 0))))
  13320. (inc (if visually 1 0)))
  13321. (save-excursion
  13322. (beginning-of-line (- 1 (or nlines 0)))
  13323. (while (re-search-forward re eol t)
  13324. (if (and (<= (match-beginning 0) pos)
  13325. (>= (+ inc (match-end 0)) pos))
  13326. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13327. (defun org-at-regexp-p (regexp)
  13328. "Is point inside a match of REGEXP in the current line?"
  13329. (catch 'exit
  13330. (save-excursion
  13331. (let ((pos (point)) (end (point-at-eol)))
  13332. (beginning-of-line 1)
  13333. (while (re-search-forward regexp end t)
  13334. (if (and (<= (match-beginning 0) pos)
  13335. (>= (match-end 0) pos))
  13336. (throw 'exit t)))
  13337. nil))))
  13338. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13339. "Call `multi-occur' with buffers for all agenda files."
  13340. (interactive "sOrg-files matching: \np")
  13341. (let* ((files (org-agenda-files))
  13342. (tnames (mapcar 'file-truename files))
  13343. (extra org-agenda-text-search-extra-files)
  13344. f)
  13345. (when (eq (car extra) 'agenda-archives)
  13346. (setq extra (cdr extra))
  13347. (setq files (org-add-archive-files files)))
  13348. (while (setq f (pop extra))
  13349. (unless (member (file-truename f) tnames)
  13350. (add-to-list 'files f 'append)
  13351. (add-to-list 'tnames (file-truename f) 'append)))
  13352. (multi-occur
  13353. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13354. regexp)))
  13355. (if (boundp 'occur-mode-find-occurrence-hook)
  13356. ;; Emacs 23
  13357. (add-hook 'occur-mode-find-occurrence-hook
  13358. (lambda ()
  13359. (when (org-mode-p)
  13360. (org-reveal))))
  13361. ;; Emacs 22
  13362. (defadvice occur-mode-goto-occurrence
  13363. (after org-occur-reveal activate)
  13364. (and (org-mode-p) (org-reveal)))
  13365. (defadvice occur-mode-goto-occurrence-other-window
  13366. (after org-occur-reveal activate)
  13367. (and (org-mode-p) (org-reveal)))
  13368. (defadvice occur-mode-display-occurrence
  13369. (after org-occur-reveal activate)
  13370. (when (org-mode-p)
  13371. (let ((pos (occur-mode-find-occurrence)))
  13372. (with-current-buffer (marker-buffer pos)
  13373. (save-excursion
  13374. (goto-char pos)
  13375. (org-reveal)))))))
  13376. (defun org-uniquify (list)
  13377. "Remove duplicate elements from LIST."
  13378. (let (res)
  13379. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13380. res))
  13381. (defun org-delete-all (elts list)
  13382. "Remove all elements in ELTS from LIST."
  13383. (while elts
  13384. (setq list (delete (pop elts) list)))
  13385. list)
  13386. (defun org-back-over-empty-lines ()
  13387. "Move backwards over whitespace, to the beginning of the first empty line.
  13388. Returns the number of empty lines passed."
  13389. (let ((pos (point)))
  13390. (skip-chars-backward " \t\n\r")
  13391. (beginning-of-line 2)
  13392. (goto-char (min (point) pos))
  13393. (count-lines (point) pos)))
  13394. (defun org-skip-whitespace ()
  13395. (skip-chars-forward " \t\n\r"))
  13396. (defun org-point-in-group (point group &optional context)
  13397. "Check if POINT is in match-group GROUP.
  13398. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13399. match. If the match group does ot exist or point is not inside it,
  13400. return nil."
  13401. (and (match-beginning group)
  13402. (>= point (match-beginning group))
  13403. (<= point (match-end group))
  13404. (if context
  13405. (list context (match-beginning group) (match-end group))
  13406. t)))
  13407. (defun org-switch-to-buffer-other-window (&rest args)
  13408. "Switch to buffer in a second window on the current frame.
  13409. In particular, do not allow pop-up frames."
  13410. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13411. special-display-function)
  13412. (apply 'switch-to-buffer-other-window args)))
  13413. (defun org-combine-plists (&rest plists)
  13414. "Create a single property list from all plists in PLISTS.
  13415. The process starts by copying the first list, and then setting properties
  13416. from the other lists. Settings in the last list are the most significant
  13417. ones and overrule settings in the other lists."
  13418. (let ((rtn (copy-sequence (pop plists)))
  13419. p v ls)
  13420. (while plists
  13421. (setq ls (pop plists))
  13422. (while ls
  13423. (setq p (pop ls) v (pop ls))
  13424. (setq rtn (plist-put rtn p v))))
  13425. rtn))
  13426. (defun org-move-line-down (arg)
  13427. "Move the current line down. With prefix argument, move it past ARG lines."
  13428. (interactive "p")
  13429. (let ((col (current-column))
  13430. beg end pos)
  13431. (beginning-of-line 1) (setq beg (point))
  13432. (beginning-of-line 2) (setq end (point))
  13433. (beginning-of-line (+ 1 arg))
  13434. (setq pos (move-marker (make-marker) (point)))
  13435. (insert (delete-and-extract-region beg end))
  13436. (goto-char pos)
  13437. (org-move-to-column col)))
  13438. (defun org-move-line-up (arg)
  13439. "Move the current line up. With prefix argument, move it past ARG lines."
  13440. (interactive "p")
  13441. (let ((col (current-column))
  13442. beg end pos)
  13443. (beginning-of-line 1) (setq beg (point))
  13444. (beginning-of-line 2) (setq end (point))
  13445. (beginning-of-line (- arg))
  13446. (setq pos (move-marker (make-marker) (point)))
  13447. (insert (delete-and-extract-region beg end))
  13448. (goto-char pos)
  13449. (org-move-to-column col)))
  13450. (defun org-replace-escapes (string table)
  13451. "Replace %-escapes in STRING with values in TABLE.
  13452. TABLE is an association list with keys like \"%a\" and string values.
  13453. The sequences in STRING may contain normal field width and padding information,
  13454. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13455. so values can contain further %-escapes if they are define later in TABLE."
  13456. (let ((case-fold-search nil)
  13457. e re rpl)
  13458. (while (setq e (pop table))
  13459. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13460. (while (string-match re string)
  13461. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13462. (cdr e)))
  13463. (setq string (replace-match rpl t t string))))
  13464. string))
  13465. (defun org-sublist (list start end)
  13466. "Return a section of LIST, from START to END.
  13467. Counting starts at 1."
  13468. (let (rtn (c start))
  13469. (setq list (nthcdr (1- start) list))
  13470. (while (and list (<= c end))
  13471. (push (pop list) rtn)
  13472. (setq c (1+ c)))
  13473. (nreverse rtn)))
  13474. (defun org-find-base-buffer-visiting (file)
  13475. "Like `find-buffer-visiting' but alway return the base buffer and
  13476. not an indirect buffer."
  13477. (let ((buf (find-buffer-visiting file)))
  13478. (if buf
  13479. (or (buffer-base-buffer buf) buf)
  13480. nil)))
  13481. (defun org-image-file-name-regexp (&optional extensions)
  13482. "Return regexp matching the file names of images.
  13483. If EXTENSIONS is given, only match these."
  13484. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13485. (image-file-name-regexp)
  13486. (let ((image-file-name-extensions
  13487. (or extensions
  13488. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13489. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13490. (concat "\\."
  13491. (regexp-opt (nconc (mapcar 'upcase
  13492. image-file-name-extensions)
  13493. image-file-name-extensions)
  13494. t)
  13495. "\\'"))))
  13496. (defun org-file-image-p (file &optional extensions)
  13497. "Return non-nil if FILE is an image."
  13498. (save-match-data
  13499. (string-match (org-image-file-name-regexp extensions) file)))
  13500. (defun org-get-cursor-date ()
  13501. "Return the date at cursor in as a time.
  13502. This works in the calendar and in the agenda, anywhere else it just
  13503. returns the current time."
  13504. (let (date day defd)
  13505. (cond
  13506. ((eq major-mode 'calendar-mode)
  13507. (setq date (calendar-cursor-to-date)
  13508. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13509. ((eq major-mode 'org-agenda-mode)
  13510. (setq day (get-text-property (point) 'day))
  13511. (if day
  13512. (setq date (calendar-gregorian-from-absolute day)
  13513. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13514. (nth 2 date))))))
  13515. (or defd (current-time))))
  13516. (defvar org-agenda-action-marker (make-marker)
  13517. "Marker pointing to the entry for the next agenda action.")
  13518. (defun org-mark-entry-for-agenda-action ()
  13519. "Mark the current entry as target of an agenda action.
  13520. Agenda actions are actions executed from the agenda with the key `k',
  13521. which make use of the date at the cursor."
  13522. (interactive)
  13523. (move-marker org-agenda-action-marker
  13524. (save-excursion (org-back-to-heading t) (point))
  13525. (current-buffer))
  13526. (message
  13527. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13528. ;;; Paragraph filling stuff.
  13529. ;; We want this to be just right, so use the full arsenal.
  13530. (defun org-indent-line-function ()
  13531. "Indent line like previous, but further if previous was headline or item."
  13532. (interactive)
  13533. (let* ((pos (point))
  13534. (itemp (org-at-item-p))
  13535. column bpos bcol tpos tcol bullet btype bullet-type)
  13536. ;; Find the previous relevant line
  13537. (beginning-of-line 1)
  13538. (cond
  13539. ((looking-at "#") (setq column 0))
  13540. ((looking-at "\\*+ ") (setq column 0))
  13541. ((and (looking-at "[ \t]*:END:")
  13542. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13543. (save-excursion
  13544. (goto-char (1- (match-beginning 1)))
  13545. (setq column (current-column))))
  13546. (t
  13547. (beginning-of-line 0)
  13548. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13549. (not (looking-at "[ \t]*:END:"))
  13550. (not (looking-at org-drawer-regexp)))
  13551. (beginning-of-line 0))
  13552. (cond
  13553. ((looking-at "\\*+[ \t]+")
  13554. (if (not org-adapt-indentation)
  13555. (setq column 0)
  13556. (goto-char (match-end 0))
  13557. (setq column (current-column))))
  13558. ((looking-at org-drawer-regexp)
  13559. (goto-char (1- (match-beginning 1)))
  13560. (setq column (current-column)))
  13561. ((looking-at "\\([ \t]*\\):END:")
  13562. (goto-char (match-end 1))
  13563. (setq column (current-column)))
  13564. ((org-in-item-p)
  13565. (org-beginning-of-item)
  13566. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13567. (setq bpos (match-beginning 1) tpos (match-end 0)
  13568. bcol (progn (goto-char bpos) (current-column))
  13569. tcol (progn (goto-char tpos) (current-column))
  13570. bullet (match-string 1)
  13571. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13572. (if (> tcol (+ bcol org-description-max-indent))
  13573. (setq tcol (+ bcol 5)))
  13574. (if (not itemp)
  13575. (setq column tcol)
  13576. (goto-char pos)
  13577. (beginning-of-line 1)
  13578. (if (looking-at "\\S-")
  13579. (progn
  13580. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13581. (setq bullet (match-string 1)
  13582. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13583. (setq column (if (equal btype bullet-type) bcol tcol)))
  13584. (setq column (org-get-indentation)))))
  13585. (t (setq column (org-get-indentation))))))
  13586. (goto-char pos)
  13587. (if (<= (current-column) (current-indentation))
  13588. (org-indent-line-to column)
  13589. (save-excursion (org-indent-line-to column)))
  13590. (setq column (current-column))
  13591. (beginning-of-line 1)
  13592. (if (looking-at
  13593. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13594. (replace-match (concat "\\1" (format org-property-format
  13595. (match-string 2) (match-string 3)))
  13596. t nil))
  13597. (org-move-to-column column)))
  13598. (defun org-set-autofill-regexps ()
  13599. (interactive)
  13600. ;; In the paragraph separator we include headlines, because filling
  13601. ;; text in a line directly attached to a headline would otherwise
  13602. ;; fill the headline as well.
  13603. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13604. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13605. ;; The paragraph starter includes hand-formatted lists.
  13606. (org-set-local 'paragraph-start
  13607. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13608. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13609. ;; But only if the user has not turned off tables or fixed-width regions
  13610. (org-set-local
  13611. 'auto-fill-inhibit-regexp
  13612. (concat "\\*+ \\|#\\+"
  13613. "\\|[ \t]*" org-keyword-time-regexp
  13614. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13615. (concat
  13616. "\\|[ \t]*["
  13617. (if org-enable-table-editor "|" "")
  13618. (if org-enable-fixed-width-editor ":" "")
  13619. "]"))))
  13620. ;; We use our own fill-paragraph function, to make sure that tables
  13621. ;; and fixed-width regions are not wrapped. That function will pass
  13622. ;; through to `fill-paragraph' when appropriate.
  13623. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13624. ; Adaptive filling: To get full control, first make sure that
  13625. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13626. (org-set-local 'adaptive-fill-regexp "\000")
  13627. (org-set-local 'adaptive-fill-function
  13628. 'org-adaptive-fill-function)
  13629. (org-set-local
  13630. 'align-mode-rules-list
  13631. '((org-in-buffer-settings
  13632. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13633. (modes . '(org-mode))))))
  13634. (defun org-fill-paragraph (&optional justify)
  13635. "Re-align a table, pass through to fill-paragraph if no table."
  13636. (let ((table-p (org-at-table-p))
  13637. (table.el-p (org-at-table.el-p)))
  13638. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13639. (save-excursion (goto-char (point-at-bol))
  13640. (looking-at outline-regexp)))
  13641. t) ; skip headlines
  13642. (table.el-p t) ; skip table.el tables
  13643. (table-p (org-table-align) t) ; align org-mode tables
  13644. (t nil)))) ; call paragraph-fill
  13645. ;; For reference, this is the default value of adaptive-fill-regexp
  13646. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13647. (defun org-adaptive-fill-function ()
  13648. "Return a fill prefix for org-mode files.
  13649. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13650. work correctly."
  13651. (cond ((looking-at "#[ \t]+")
  13652. (match-string 0))
  13653. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13654. (save-excursion
  13655. (if (> (match-end 1) (+ (match-beginning 1)
  13656. org-description-max-indent))
  13657. (goto-char (+ (match-beginning 1) 5))
  13658. (goto-char (match-end 0)))
  13659. (make-string (current-column) ?\ )))
  13660. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13661. (save-excursion
  13662. (goto-char (match-end 0))
  13663. (make-string (current-column) ?\ )))
  13664. (t nil)))
  13665. ;;; Other stuff.
  13666. (defun org-toggle-fixed-width-section (arg)
  13667. "Toggle the fixed-width export.
  13668. If there is no active region, the QUOTE keyword at the current headline is
  13669. inserted or removed. When present, it causes the text between this headline
  13670. and the next to be exported as fixed-width text, and unmodified.
  13671. If there is an active region, this command adds or removes a colon as the
  13672. first character of this line. If the first character of a line is a colon,
  13673. this line is also exported in fixed-width font."
  13674. (interactive "P")
  13675. (let* ((cc 0)
  13676. (regionp (org-region-active-p))
  13677. (beg (if regionp (region-beginning) (point)))
  13678. (end (if regionp (region-end)))
  13679. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13680. (case-fold-search nil)
  13681. (re "[ \t]*\\(:\\)")
  13682. off)
  13683. (if regionp
  13684. (save-excursion
  13685. (goto-char beg)
  13686. (setq cc (current-column))
  13687. (beginning-of-line 1)
  13688. (setq off (looking-at re))
  13689. (while (> nlines 0)
  13690. (setq nlines (1- nlines))
  13691. (beginning-of-line 1)
  13692. (cond
  13693. (arg
  13694. (org-move-to-column cc t)
  13695. (insert ":\n")
  13696. (forward-line -1))
  13697. ((and off (looking-at re))
  13698. (replace-match "" t t nil 1))
  13699. ((not off) (org-move-to-column cc t) (insert ":")))
  13700. (forward-line 1)))
  13701. (save-excursion
  13702. (org-back-to-heading)
  13703. (if (looking-at (concat outline-regexp
  13704. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13705. (replace-match "" t t nil 1)
  13706. (if (looking-at outline-regexp)
  13707. (progn
  13708. (goto-char (match-end 0))
  13709. (insert org-quote-string " "))))))))
  13710. ;;;; Functions extending outline functionality
  13711. (defun org-beginning-of-line (&optional arg)
  13712. "Go to the beginning of the current line. If that is invisible, continue
  13713. to a visible line beginning. This makes the function of C-a more intuitive.
  13714. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13715. first attempt, and only move to after the tags when the cursor is already
  13716. beyond the end of the headline."
  13717. (interactive "P")
  13718. (let ((pos (point)) refpos)
  13719. (beginning-of-line 1)
  13720. (if (and arg (fboundp 'move-beginning-of-line))
  13721. (call-interactively 'move-beginning-of-line)
  13722. (if (bobp)
  13723. nil
  13724. (backward-char 1)
  13725. (if (org-invisible-p)
  13726. (while (and (not (bobp)) (org-invisible-p))
  13727. (backward-char 1)
  13728. (beginning-of-line 1))
  13729. (forward-char 1))))
  13730. (when org-special-ctrl-a/e
  13731. (cond
  13732. ((and (looking-at org-complex-heading-regexp)
  13733. (= (char-after (match-end 1)) ?\ ))
  13734. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13735. (point-at-eol)))
  13736. (goto-char
  13737. (if (eq org-special-ctrl-a/e t)
  13738. (cond ((> pos refpos) refpos)
  13739. ((= pos (point)) refpos)
  13740. (t (point)))
  13741. (cond ((> pos (point)) (point))
  13742. ((not (eq last-command this-command)) (point))
  13743. (t refpos)))))
  13744. ((org-at-item-p)
  13745. (goto-char
  13746. (if (eq org-special-ctrl-a/e t)
  13747. (cond ((> pos (match-end 4)) (match-end 4))
  13748. ((= pos (point)) (match-end 4))
  13749. (t (point)))
  13750. (cond ((> pos (point)) (point))
  13751. ((not (eq last-command this-command)) (point))
  13752. (t (match-end 4))))))))
  13753. (org-no-warnings
  13754. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13755. (defun org-end-of-line (&optional arg)
  13756. "Go to the end of the line.
  13757. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13758. first attempt, and only move to after the tags when the cursor is already
  13759. beyond the end of the headline."
  13760. (interactive "P")
  13761. (if (or (not org-special-ctrl-a/e)
  13762. (not (org-on-heading-p))
  13763. arg)
  13764. (call-interactively (if (fboundp 'move-end-of-line)
  13765. 'move-end-of-line
  13766. 'end-of-line))
  13767. (let ((pos (point)))
  13768. (beginning-of-line 1)
  13769. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13770. (if (eq org-special-ctrl-a/e t)
  13771. (if (or (< pos (match-beginning 1))
  13772. (= pos (match-end 0)))
  13773. (goto-char (match-beginning 1))
  13774. (goto-char (match-end 0)))
  13775. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13776. (goto-char (match-end 0))
  13777. (goto-char (match-beginning 1))))
  13778. (call-interactively (if (fboundp 'move-end-of-line)
  13779. 'move-end-of-line
  13780. 'end-of-line)))))
  13781. (org-no-warnings
  13782. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13783. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13784. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13785. (defun org-kill-line (&optional arg)
  13786. "Kill line, to tags or end of line."
  13787. (interactive "P")
  13788. (cond
  13789. ((or (not org-special-ctrl-k)
  13790. (bolp)
  13791. (not (org-on-heading-p)))
  13792. (call-interactively 'kill-line))
  13793. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13794. (kill-region (point) (match-beginning 1))
  13795. (org-set-tags nil t))
  13796. (t (kill-region (point) (point-at-eol)))))
  13797. (define-key org-mode-map "\C-k" 'org-kill-line)
  13798. (defun org-yank (&optional arg)
  13799. "Yank. If the kill is a subtree, treat it specially.
  13800. This command will look at the current kill and check if is a single
  13801. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13802. cursor is at the beginning of a line or after the stars of a currently
  13803. empty headline, then the yank is handled specially. How exactly depends
  13804. on the value of the following variables, both set by default.
  13805. org-yank-folded-subtrees
  13806. When set, the subtree(s) will be folded after insertion, but only
  13807. if doing so would now swallow text after the yanked text.
  13808. org-yank-adjusted-subtrees
  13809. When set, the subtree will be promoted or demoted in order to
  13810. fit into the local outline tree structure, which means that the level
  13811. will be adjusted so that it becomes the smaller one of the two
  13812. *visible* surrounding headings.
  13813. Any prefix to this command will cause `yank' to be called directly with
  13814. no special treatment. In particular, a simple `C-u' prefix will just
  13815. plainly yank the text as it is.
  13816. \[1] Basically, the test checks if the first non-white line is a heading
  13817. and if there are no other headings with fewer stars."
  13818. (interactive "P")
  13819. (setq this-command 'yank)
  13820. (if arg
  13821. (call-interactively 'yank)
  13822. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13823. (and (org-kill-is-subtree-p)
  13824. (or (bolp)
  13825. (and (looking-at "[ \t]*$")
  13826. (string-match
  13827. "\\`\\*+\\'"
  13828. (buffer-substring (point-at-bol) (point)))))))
  13829. swallowp)
  13830. (cond
  13831. ((and subtreep org-yank-folded-subtrees)
  13832. (let ((beg (point))
  13833. end)
  13834. (if (and subtreep org-yank-adjusted-subtrees)
  13835. (org-paste-subtree nil nil 'for-yank)
  13836. (call-interactively 'yank))
  13837. (setq end (point))
  13838. (goto-char beg)
  13839. (when (and (bolp) subtreep
  13840. (not (setq swallowp
  13841. (org-yank-folding-would-swallow-text beg end))))
  13842. (or (looking-at outline-regexp)
  13843. (re-search-forward (concat "^" outline-regexp) end t))
  13844. (while (and (< (point) end) (looking-at outline-regexp))
  13845. (hide-subtree)
  13846. (org-cycle-show-empty-lines 'folded)
  13847. (condition-case nil
  13848. (outline-forward-same-level 1)
  13849. (error (goto-char end)))))
  13850. (when swallowp
  13851. (message
  13852. "Yanked text not folded because that would swallow text"))
  13853. (goto-char end)
  13854. (skip-chars-forward " \t\n\r")
  13855. (beginning-of-line 1)
  13856. (push-mark beg 'nomsg)))
  13857. ((and subtreep org-yank-adjusted-subtrees)
  13858. (let ((beg (point-at-bol)))
  13859. (org-paste-subtree nil nil 'for-yank)
  13860. (push-mark beg 'nomsg)))
  13861. (t
  13862. (call-interactively 'yank))))))
  13863. (defun org-yank-folding-would-swallow-text (beg end)
  13864. "Would hide-subtree at BEG swallow any text after END?"
  13865. (let (level)
  13866. (save-excursion
  13867. (goto-char beg)
  13868. (when (or (looking-at outline-regexp)
  13869. (re-search-forward (concat "^" outline-regexp) end t))
  13870. (setq level (org-outline-level)))
  13871. (goto-char end)
  13872. (skip-chars-forward " \t\r\n\v\f")
  13873. (if (or (eobp)
  13874. (and (bolp) (looking-at org-outline-regexp)
  13875. (<= (org-outline-level) level)))
  13876. nil ; Nothing would be swallowed
  13877. t)))) ; something would swallow
  13878. (define-key org-mode-map "\C-y" 'org-yank)
  13879. (defun org-invisible-p ()
  13880. "Check if point is at a character currently not visible."
  13881. ;; Early versions of noutline don't have `outline-invisible-p'.
  13882. (if (fboundp 'outline-invisible-p)
  13883. (outline-invisible-p)
  13884. (get-char-property (point) 'invisible)))
  13885. (defun org-invisible-p2 ()
  13886. "Check if point is at a character currently not visible."
  13887. (save-excursion
  13888. (if (and (eolp) (not (bobp))) (backward-char 1))
  13889. ;; Early versions of noutline don't have `outline-invisible-p'.
  13890. (if (fboundp 'outline-invisible-p)
  13891. (outline-invisible-p)
  13892. (get-char-property (point) 'invisible))))
  13893. (defun org-back-to-heading (&optional invisible-ok)
  13894. "Call `outline-back-to-heading', but provide a better error message."
  13895. (condition-case nil
  13896. (outline-back-to-heading invisible-ok)
  13897. (error (error "Before first headline at position %d in buffer %s"
  13898. (point) (current-buffer)))))
  13899. (defun org-before-first-heading-p ()
  13900. "Before first heading?"
  13901. (save-excursion
  13902. (null (re-search-backward "^\\*+ " nil t))))
  13903. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13904. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13905. (defun org-at-heading-or-item-p ()
  13906. (or (org-on-heading-p) (org-at-item-p)))
  13907. (defun org-on-target-p ()
  13908. (or (org-in-regexp org-radio-target-regexp)
  13909. (org-in-regexp org-target-regexp)))
  13910. (defun org-up-heading-all (arg)
  13911. "Move to the heading line of which the present line is a subheading.
  13912. This function considers both visible and invisible heading lines.
  13913. With argument, move up ARG levels."
  13914. (if (fboundp 'outline-up-heading-all)
  13915. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13916. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13917. (defun org-up-heading-safe ()
  13918. "Move to the heading line of which the present line is a subheading.
  13919. This version will not throw an error. It will return the level of the
  13920. headline found, or nil if no higher level is found."
  13921. (let (start-level re)
  13922. (org-back-to-heading t)
  13923. (setq start-level (funcall outline-level))
  13924. (if (equal start-level 1)
  13925. nil
  13926. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13927. (if (re-search-backward re nil t)
  13928. (funcall outline-level)))))
  13929. (defun org-first-sibling-p ()
  13930. "Is this heading the first child of its parents?"
  13931. (interactive)
  13932. (let ((re (concat "^" outline-regexp))
  13933. level l)
  13934. (unless (org-at-heading-p t)
  13935. (error "Not at a heading"))
  13936. (setq level (funcall outline-level))
  13937. (save-excursion
  13938. (if (not (re-search-backward re nil t))
  13939. t
  13940. (setq l (funcall outline-level))
  13941. (< l level)))))
  13942. (defun org-goto-sibling (&optional previous)
  13943. "Goto the next sibling, even if it is invisible.
  13944. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13945. when a sibling was found. When none is found, return nil and don't
  13946. move point."
  13947. (let ((fun (if previous 're-search-backward 're-search-forward))
  13948. (pos (point))
  13949. (re (concat "^" outline-regexp))
  13950. level l)
  13951. (when (condition-case nil (org-back-to-heading t) (error nil))
  13952. (setq level (funcall outline-level))
  13953. (catch 'exit
  13954. (or previous (forward-char 1))
  13955. (while (funcall fun re nil t)
  13956. (setq l (funcall outline-level))
  13957. (when (< l level) (goto-char pos) (throw 'exit nil))
  13958. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13959. (goto-char pos)
  13960. nil))))
  13961. (defun org-show-siblings ()
  13962. "Show all siblings of the current headline."
  13963. (save-excursion
  13964. (while (org-goto-sibling) (org-flag-heading nil)))
  13965. (save-excursion
  13966. (while (org-goto-sibling 'previous)
  13967. (org-flag-heading nil))))
  13968. (defun org-show-hidden-entry ()
  13969. "Show an entry where even the heading is hidden."
  13970. (save-excursion
  13971. (org-show-entry)))
  13972. (defun org-flag-heading (flag &optional entry)
  13973. "Flag the current heading. FLAG non-nil means make invisible.
  13974. When ENTRY is non-nil, show the entire entry."
  13975. (save-excursion
  13976. (org-back-to-heading t)
  13977. ;; Check if we should show the entire entry
  13978. (if entry
  13979. (progn
  13980. (org-show-entry)
  13981. (save-excursion
  13982. (and (outline-next-heading)
  13983. (org-flag-heading nil))))
  13984. (outline-flag-region (max (point-min) (1- (point)))
  13985. (save-excursion (outline-end-of-heading) (point))
  13986. flag))))
  13987. (defun org-forward-same-level (arg)
  13988. "Move forward to the ARG'th subheading at same level as this one.
  13989. Stop at the first and last subheadings of a superior heading.
  13990. This is like outline-forward-same-level, but invisible headings are ok."
  13991. (interactive "p")
  13992. (org-back-to-heading t)
  13993. (while (> arg 0)
  13994. (let ((point-to-move-to (save-excursion
  13995. (org-get-next-sibling))))
  13996. (if point-to-move-to
  13997. (progn
  13998. (goto-char point-to-move-to)
  13999. (setq arg (1- arg)))
  14000. (progn
  14001. (setq arg 0)
  14002. (error "No following same-level heading"))))))
  14003. (defun org-get-next-sibling ()
  14004. "Move to next heading of the same level, and return point.
  14005. If there is no such heading, return nil.
  14006. This is like outline-next-sibling, but invisible headings are ok."
  14007. (let ((level (funcall outline-level)))
  14008. (outline-next-heading)
  14009. (while (and (not (eobp)) (> (funcall outline-level) level))
  14010. (outline-next-heading))
  14011. (if (or (eobp) (< (funcall outline-level) level))
  14012. nil
  14013. (point))))
  14014. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14015. ;; This is an exact copy of the original function, but it uses
  14016. ;; `org-back-to-heading', to make it work also in invisible
  14017. ;; trees. And is uses an invisible-OK argument.
  14018. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14019. (org-back-to-heading invisible-OK)
  14020. (let ((first t)
  14021. (level (funcall outline-level)))
  14022. (while (and (not (eobp))
  14023. (or first (> (funcall outline-level) level)))
  14024. (setq first nil)
  14025. (outline-next-heading))
  14026. (unless to-heading
  14027. (if (memq (preceding-char) '(?\n ?\^M))
  14028. (progn
  14029. ;; Go to end of line before heading
  14030. (forward-char -1)
  14031. (if (memq (preceding-char) '(?\n ?\^M))
  14032. ;; leave blank line before heading
  14033. (forward-char -1))))))
  14034. (point))
  14035. (defun org-show-subtree ()
  14036. "Show everything after this heading at deeper levels."
  14037. (outline-flag-region
  14038. (point)
  14039. (save-excursion
  14040. (outline-end-of-subtree) (outline-next-heading) (point))
  14041. nil))
  14042. (defun org-show-entry ()
  14043. "Show the body directly following this heading.
  14044. Show the heading too, if it is currently invisible."
  14045. (interactive)
  14046. (save-excursion
  14047. (condition-case nil
  14048. (progn
  14049. (org-back-to-heading t)
  14050. (outline-flag-region
  14051. (max (point-min) (1- (point)))
  14052. (save-excursion
  14053. (if (re-search-forward
  14054. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14055. (match-beginning 1)
  14056. (point-max)))
  14057. nil))
  14058. (error nil))))
  14059. (defun org-make-options-regexp (kwds &optional extra)
  14060. "Make a regular expression for keyword lines."
  14061. (concat
  14062. "^"
  14063. "#?[ \t]*\\+\\("
  14064. (mapconcat 'regexp-quote kwds "\\|")
  14065. (if extra (concat "\\|" extra))
  14066. "\\):[ \t]*"
  14067. "\\(.+\\)"))
  14068. ;; Make isearch reveal the necessary context
  14069. (defun org-isearch-end ()
  14070. "Reveal context after isearch exits."
  14071. (when isearch-success ; only if search was successful
  14072. (if (featurep 'xemacs)
  14073. ;; Under XEmacs, the hook is run in the correct place,
  14074. ;; we directly show the context.
  14075. (org-show-context 'isearch)
  14076. ;; In Emacs the hook runs *before* restoring the overlays.
  14077. ;; So we have to use a one-time post-command-hook to do this.
  14078. ;; (Emacs 22 has a special variable, see function `org-mode')
  14079. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14080. isearch-mode-end-hook-quit)
  14081. ;; Only when the isearch was not quitted.
  14082. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14083. 'append 'local)))))
  14084. (defun org-isearch-post-command ()
  14085. "Remove self from hook, and show context."
  14086. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14087. (org-show-context 'isearch))
  14088. ;;;; Integration with and fixes for other packages
  14089. ;;; Imenu support
  14090. (defvar org-imenu-markers nil
  14091. "All markers currently used by Imenu.")
  14092. (make-variable-buffer-local 'org-imenu-markers)
  14093. (defun org-imenu-new-marker (&optional pos)
  14094. "Return a new marker for use by Imenu, and remember the marker."
  14095. (let ((m (make-marker)))
  14096. (move-marker m (or pos (point)))
  14097. (push m org-imenu-markers)
  14098. m))
  14099. (defun org-imenu-get-tree ()
  14100. "Produce the index for Imenu."
  14101. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14102. (setq org-imenu-markers nil)
  14103. (let* ((n org-imenu-depth)
  14104. (re (concat "^" outline-regexp))
  14105. (subs (make-vector (1+ n) nil))
  14106. (last-level 0)
  14107. m level head)
  14108. (save-excursion
  14109. (save-restriction
  14110. (widen)
  14111. (goto-char (point-max))
  14112. (while (re-search-backward re nil t)
  14113. (setq level (org-reduced-level (funcall outline-level)))
  14114. (when (<= level n)
  14115. (looking-at org-complex-heading-regexp)
  14116. (setq head (org-link-display-format
  14117. (org-match-string-no-properties 4))
  14118. m (org-imenu-new-marker))
  14119. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14120. (if (>= level last-level)
  14121. (push (cons head m) (aref subs level))
  14122. (push (cons head (aref subs (1+ level))) (aref subs level))
  14123. (loop for i from (1+ level) to n do (aset subs i nil)))
  14124. (setq last-level level)))))
  14125. (aref subs 1)))
  14126. (eval-after-load "imenu"
  14127. '(progn
  14128. (add-hook 'imenu-after-jump-hook
  14129. (lambda ()
  14130. (if (eq major-mode 'org-mode)
  14131. (org-show-context 'org-goto))))))
  14132. (defun org-link-display-format (link)
  14133. "Replace a link with either the description, or the link target
  14134. if no description is present"
  14135. (save-match-data
  14136. (if (string-match org-bracket-link-analytic-regexp link)
  14137. (replace-match (or (match-string 5 link)
  14138. (concat (match-string 1 link)
  14139. (match-string 3 link)))
  14140. nil nil link)
  14141. link)))
  14142. ;; Speedbar support
  14143. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14144. "Overlay marking the agenda restriction line in speedbar.")
  14145. (org-overlay-put org-speedbar-restriction-lock-overlay
  14146. 'face 'org-agenda-restriction-lock)
  14147. (org-overlay-put org-speedbar-restriction-lock-overlay
  14148. 'help-echo "Agendas are currently limited to this item.")
  14149. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14150. (defun org-speedbar-set-agenda-restriction ()
  14151. "Restrict future agenda commands to the location at point in speedbar.
  14152. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14153. (interactive)
  14154. (require 'org-agenda)
  14155. (let (p m tp np dir txt)
  14156. (cond
  14157. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14158. 'org-imenu t))
  14159. (setq m (get-text-property p 'org-imenu-marker))
  14160. (save-excursion
  14161. (save-restriction
  14162. (set-buffer (marker-buffer m))
  14163. (goto-char m)
  14164. (org-agenda-set-restriction-lock 'subtree))))
  14165. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14166. 'speedbar-function 'speedbar-find-file))
  14167. (setq tp (previous-single-property-change
  14168. (1+ p) 'speedbar-function)
  14169. np (next-single-property-change
  14170. tp 'speedbar-function)
  14171. dir (speedbar-line-directory)
  14172. txt (buffer-substring-no-properties (or tp (point-min))
  14173. (or np (point-max))))
  14174. (save-excursion
  14175. (save-restriction
  14176. (set-buffer (find-file-noselect
  14177. (let ((default-directory dir))
  14178. (expand-file-name txt))))
  14179. (unless (org-mode-p)
  14180. (error "Cannot restrict to non-Org-mode file"))
  14181. (org-agenda-set-restriction-lock 'file))))
  14182. (t (error "Don't know how to restrict Org-mode's agenda")))
  14183. (org-move-overlay org-speedbar-restriction-lock-overlay
  14184. (point-at-bol) (point-at-eol))
  14185. (setq current-prefix-arg nil)
  14186. (org-agenda-maybe-redo)))
  14187. (eval-after-load "speedbar"
  14188. '(progn
  14189. (speedbar-add-supported-extension ".org")
  14190. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14191. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14192. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14193. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14194. (add-hook 'speedbar-visiting-tag-hook
  14195. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14196. ;;; Fixes and Hacks for problems with other packages
  14197. ;; Make flyspell not check words in links, to not mess up our keymap
  14198. (defun org-mode-flyspell-verify ()
  14199. "Don't let flyspell put overlays at active buttons."
  14200. (not (get-text-property (point) 'keymap)))
  14201. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14202. (eval-after-load "bookmark"
  14203. '(if (boundp 'bookmark-after-jump-hook)
  14204. ;; We can use the hook
  14205. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14206. ;; Hook not available, use advice
  14207. (defadvice bookmark-jump (after org-make-visible activate)
  14208. "Make the position visible."
  14209. (org-bookmark-jump-unhide))))
  14210. ;; Make sure saveplace show the location if it was hidden
  14211. (eval-after-load "saveplace"
  14212. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14213. "Make the position visible."
  14214. (org-bookmark-jump-unhide)))
  14215. (defun org-bookmark-jump-unhide ()
  14216. "Unhide the current position, to show the bookmark location."
  14217. (and (org-mode-p)
  14218. (or (org-invisible-p)
  14219. (save-excursion (goto-char (max (point-min) (1- (point))))
  14220. (org-invisible-p)))
  14221. (org-show-context 'bookmark-jump)))
  14222. ;; Make session.el ignore our circular variable
  14223. (eval-after-load "session"
  14224. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14225. ;;;; Experimental code
  14226. (defun org-closed-in-range ()
  14227. "Sparse tree of items closed in a certain time range.
  14228. Still experimental, may disappear in the future."
  14229. (interactive)
  14230. ;; Get the time interval from the user.
  14231. (let* ((time1 (time-to-seconds
  14232. (org-read-date nil 'to-time nil "Starting date: ")))
  14233. (time2 (time-to-seconds
  14234. (org-read-date nil 'to-time nil "End date:")))
  14235. ;; callback function
  14236. (callback (lambda ()
  14237. (let ((time
  14238. (time-to-seconds
  14239. (apply 'encode-time
  14240. (org-parse-time-string
  14241. (match-string 1))))))
  14242. ;; check if time in interval
  14243. (and (>= time time1) (<= time time2))))))
  14244. ;; make tree, check each match with the callback
  14245. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14246. ;;;; Finish up
  14247. (provide 'org)
  14248. (run-hooks 'org-load-hook)
  14249. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14250. ;;; org.el ends here