org.el 578 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548
  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 registry: A registry for Org links" org-registry)
  175. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  176. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  177. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  178. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  179. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  180. (defcustom org-support-shift-select nil
  181. "Non-nil means, make shift-cursor commands select text when possible.
  182. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  183. selecting a region, or enlarge thusly regions started in this way.
  184. In Org-mode, in special contexts, these same keys are used for other
  185. purposes, important enough to compete with shift selection. Org tries
  186. to balance these needs by supporting `shift-select-mode' outside these
  187. special contexts, under control of this variable.
  188. The default of this variable is nil, to avoid confusing behavior. Shifted
  189. cursor keys will then execute Org commands in the following contexts:
  190. - on a headline, changing TODO state (left/right) and priority (up/down)
  191. - on a time stamp, changing the time
  192. - in a plain list item, changing the bullet type
  193. - in a property definition line, switching between allowed values
  194. - in the BEGIN line of a clock table (changing the time block).
  195. Outside these contexts, the commands will throw an error.
  196. When this variable is t and the cursor is not in a special context,
  197. Org-mode will support shift-selection for making and enlarging regions.
  198. To make this more effective, the bullet cycling will no longer happen
  199. anywhere in an item line, but only if the cursor is exactly on the bullet.
  200. If you set this variable to the symbol `always', then the keys
  201. will not be special in headlines, property lines, and item lines, to make
  202. shift selection work there as well. If this is what you want, you can
  203. use the following alternative commands: `C-c C-t' and `C-c ,' to
  204. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  205. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  206. edited by hand or in column view.
  207. However, when the cursor is on a timestamp, shift-cursor commands
  208. will still edit the time stamp - this is just too good to give up.
  209. XEmacs user should have this variable set to nil, because shift-select-mode
  210. is Emacs 23 only."
  211. :group 'org
  212. :type '(choice
  213. (const :tag "Never" nil)
  214. (const :tag "When outside special context" t)
  215. (const :tag "Everywhere except timestamps" always)))
  216. (defgroup org-startup nil
  217. "Options concerning startup of Org-mode."
  218. :tag "Org Startup"
  219. :group 'org)
  220. (defcustom org-startup-folded t
  221. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  222. This can also be configured on a per-file basis by adding one of
  223. the following lines anywhere in the buffer:
  224. #+STARTUP: fold
  225. #+STARTUP: nofold
  226. #+STARTUP: content"
  227. :group 'org-startup
  228. :type '(choice
  229. (const :tag "nofold: show all" nil)
  230. (const :tag "fold: overview" t)
  231. (const :tag "content: all headlines" content)))
  232. (defcustom org-startup-truncated t
  233. "Non-nil means, entering Org-mode will set `truncate-lines'.
  234. This is useful since some lines containing links can be very long and
  235. uninteresting. Also tables look terrible when wrapped."
  236. :group 'org-startup
  237. :type 'boolean)
  238. (defcustom org-startup-align-all-tables nil
  239. "Non-nil means, align all tables when visiting a file.
  240. This is useful when the column width in tables is forced with <N> cookies
  241. in table fields. Such tables will look correct only after the first re-align.
  242. This can also be configured on a per-file basis by adding one of
  243. the following lines anywhere in the buffer:
  244. #+STARTUP: align
  245. #+STARTUP: noalign"
  246. :group 'org-startup
  247. :type 'boolean)
  248. (defcustom org-insert-mode-line-in-empty-file nil
  249. "Non-nil means insert the first line setting Org-mode in empty files.
  250. When the function `org-mode' is called interactively in an empty file, this
  251. normally means that the file name does not automatically trigger Org-mode.
  252. To ensure that the file will always be in Org-mode in the future, a
  253. line enforcing Org-mode will be inserted into the buffer, if this option
  254. has been set."
  255. :group 'org-startup
  256. :type 'boolean)
  257. (defcustom org-replace-disputed-keys nil
  258. "Non-nil means use alternative key bindings for some keys.
  259. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  260. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  261. If you want to use Org-mode together with one of these other modes,
  262. or more generally if you would like to move some Org-mode commands to
  263. other keys, set this variable and configure the keys with the variable
  264. `org-disputed-keys'.
  265. This option is only relevant at load-time of Org-mode, and must be set
  266. *before* org.el is loaded. Changing it requires a restart of Emacs to
  267. become effective."
  268. :group 'org-startup
  269. :type 'boolean)
  270. (defcustom org-use-extra-keys nil
  271. "Non-nil means use extra key sequence definitions for certain
  272. commands. This happens automatically if you run XEmacs or if
  273. window-system is nil. This variable lets you do the same
  274. manually. You must set it before loading org.
  275. Example: on Carbon Emacs 22 running graphically, with an external
  276. keyboard on a Powerbook, the default way of setting M-left might
  277. not work for either Alt or ESC. Setting this variable will make
  278. it work for ESC."
  279. :group 'org-startup
  280. :type 'boolean)
  281. (if (fboundp 'defvaralias)
  282. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  283. (defcustom org-disputed-keys
  284. '(([(shift up)] . [(meta p)])
  285. ([(shift down)] . [(meta n)])
  286. ([(shift left)] . [(meta -)])
  287. ([(shift right)] . [(meta +)])
  288. ([(control shift right)] . [(meta shift +)])
  289. ([(control shift left)] . [(meta shift -)]))
  290. "Keys for which Org-mode and other modes compete.
  291. This is an alist, cars are the default keys, second element specifies
  292. the alternative to use when `org-replace-disputed-keys' is t.
  293. Keys can be specified in any syntax supported by `define-key'.
  294. The value of this option takes effect only at Org-mode's startup,
  295. therefore you'll have to restart Emacs to apply it after changing."
  296. :group 'org-startup
  297. :type 'alist)
  298. (defun org-key (key)
  299. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  300. Or return the original if not disputed."
  301. (if org-replace-disputed-keys
  302. (let* ((nkey (key-description key))
  303. (x (org-find-if (lambda (x)
  304. (equal (key-description (car x)) nkey))
  305. org-disputed-keys)))
  306. (if x (cdr x) key))
  307. key))
  308. (defun org-find-if (predicate seq)
  309. (catch 'exit
  310. (while seq
  311. (if (funcall predicate (car seq))
  312. (throw 'exit (car seq))
  313. (pop seq)))))
  314. (defun org-defkey (keymap key def)
  315. "Define a key, possibly translated, as returned by `org-key'."
  316. (define-key keymap (org-key key) def))
  317. (defcustom org-ellipsis nil
  318. "The ellipsis to use in the Org-mode outline.
  319. When nil, just use the standard three dots. When a string, use that instead,
  320. When a face, use the standard 3 dots, but with the specified face.
  321. The change affects only Org-mode (which will then use its own display table).
  322. Changing this requires executing `M-x org-mode' in a buffer to become
  323. effective."
  324. :group 'org-startup
  325. :type '(choice (const :tag "Default" nil)
  326. (face :tag "Face" :value org-warning)
  327. (string :tag "String" :value "...#")))
  328. (defvar org-display-table nil
  329. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  330. (defgroup org-keywords nil
  331. "Keywords in Org-mode."
  332. :tag "Org Keywords"
  333. :group 'org)
  334. (defcustom org-deadline-string "DEADLINE:"
  335. "String to mark deadline entries.
  336. A deadline is this string, followed by a time stamp. Should be a word,
  337. terminated by a colon. You can insert a schedule keyword and
  338. a timestamp with \\[org-deadline].
  339. Changes become only effective after restarting Emacs."
  340. :group 'org-keywords
  341. :type 'string)
  342. (defcustom org-scheduled-string "SCHEDULED:"
  343. "String to mark scheduled TODO entries.
  344. A schedule is this string, followed by a time stamp. Should be a word,
  345. terminated by a colon. You can insert a schedule keyword and
  346. a timestamp with \\[org-schedule].
  347. Changes become only effective after restarting Emacs."
  348. :group 'org-keywords
  349. :type 'string)
  350. (defcustom org-closed-string "CLOSED:"
  351. "String used as the prefix for timestamps logging closing a TODO entry."
  352. :group 'org-keywords
  353. :type 'string)
  354. (defcustom org-clock-string "CLOCK:"
  355. "String used as prefix for timestamps clocking work hours on an item."
  356. :group 'org-keywords
  357. :type 'string)
  358. (defcustom org-comment-string "COMMENT"
  359. "Entries starting with this keyword will never be exported.
  360. An entry can be toggled between COMMENT and normal with
  361. \\[org-toggle-comment].
  362. Changes become only effective after restarting Emacs."
  363. :group 'org-keywords
  364. :type 'string)
  365. (defcustom org-quote-string "QUOTE"
  366. "Entries starting with this keyword will be exported in fixed-width font.
  367. Quoting applies only to the text in the entry following the headline, and does
  368. not extend beyond the next headline, even if that is lower level.
  369. An entry can be toggled between QUOTE and normal with
  370. \\[org-toggle-fixed-width-section]."
  371. :group 'org-keywords
  372. :type 'string)
  373. (defconst org-repeat-re
  374. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  375. "Regular expression for specifying repeated events.
  376. After a match, group 1 contains the repeat expression.")
  377. (defgroup org-structure nil
  378. "Options concerning the general structure of Org-mode files."
  379. :tag "Org Structure"
  380. :group 'org)
  381. (defgroup org-reveal-location nil
  382. "Options about how to make context of a location visible."
  383. :tag "Org Reveal Location"
  384. :group 'org-structure)
  385. (defconst org-context-choice
  386. '(choice
  387. (const :tag "Always" t)
  388. (const :tag "Never" nil)
  389. (repeat :greedy t :tag "Individual contexts"
  390. (cons
  391. (choice :tag "Context"
  392. (const agenda)
  393. (const org-goto)
  394. (const occur-tree)
  395. (const tags-tree)
  396. (const link-search)
  397. (const mark-goto)
  398. (const bookmark-jump)
  399. (const isearch)
  400. (const default))
  401. (boolean))))
  402. "Contexts for the reveal options.")
  403. (defcustom org-show-hierarchy-above '((default . t))
  404. "Non-nil means, show full hierarchy when revealing a location.
  405. Org-mode often shows locations in an org-mode file which might have
  406. been invisible before. When this is set, the hierarchy of headings
  407. above the exposed location is shown.
  408. Turning this off for example for sparse trees makes them very compact.
  409. Instead of t, this can also be an alist specifying this option for different
  410. contexts. Valid contexts are
  411. agenda when exposing an entry from the agenda
  412. org-goto when using the command `org-goto' on key C-c C-j
  413. occur-tree when using the command `org-occur' on key C-c /
  414. tags-tree when constructing a sparse tree based on tags matches
  415. link-search when exposing search matches associated with a link
  416. mark-goto when exposing the jump goal of a mark
  417. bookmark-jump when exposing a bookmark location
  418. isearch when exiting from an incremental search
  419. default default for all contexts not set explicitly"
  420. :group 'org-reveal-location
  421. :type org-context-choice)
  422. (defcustom org-show-following-heading '((default . nil))
  423. "Non-nil means, show following heading when revealing a location.
  424. Org-mode often shows locations in an org-mode file which might have
  425. been invisible before. When this is set, the heading following the
  426. match is shown.
  427. Turning this off for example for sparse trees makes them very compact,
  428. but makes it harder to edit the location of the match. In such a case,
  429. use the command \\[org-reveal] to show more context.
  430. Instead of t, this can also be an alist specifying this option for different
  431. contexts. See `org-show-hierarchy-above' for valid contexts."
  432. :group 'org-reveal-location
  433. :type org-context-choice)
  434. (defcustom org-show-siblings '((default . nil) (isearch t))
  435. "Non-nil means, show all sibling heading when revealing a location.
  436. Org-mode often shows locations in an org-mode file which might have
  437. been invisible before. When this is set, the sibling of the current entry
  438. heading are all made visible. If `org-show-hierarchy-above' is t,
  439. the same happens on each level of the hierarchy above the current entry.
  440. By default this is on for the isearch context, off for all other contexts.
  441. Turning this off for example for sparse trees makes them very compact,
  442. but makes it harder to edit the location of the match. In such a case,
  443. use the command \\[org-reveal] to show more context.
  444. Instead of t, this can also be an alist specifying this option for different
  445. contexts. See `org-show-hierarchy-above' for valid contexts."
  446. :group 'org-reveal-location
  447. :type org-context-choice)
  448. (defcustom org-show-entry-below '((default . nil))
  449. "Non-nil means, show the entry below a headline when revealing a location.
  450. Org-mode often shows locations in an org-mode file which might have
  451. been invisible before. When this is set, the text below the headline that is
  452. exposed is also shown.
  453. By default this is off for all contexts.
  454. Instead of t, this can also be an alist specifying this option for different
  455. contexts. See `org-show-hierarchy-above' for valid contexts."
  456. :group 'org-reveal-location
  457. :type org-context-choice)
  458. (defcustom org-indirect-buffer-display 'other-window
  459. "How should indirect tree buffers be displayed?
  460. This applies to indirect buffers created with the commands
  461. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  462. Valid values are:
  463. current-window Display in the current window
  464. other-window Just display in another window.
  465. dedicated-frame Create one new frame, and re-use it each time.
  466. new-frame Make a new frame each time. Note that in this case
  467. previously-made indirect buffers are kept, and you need to
  468. kill these buffers yourself."
  469. :group 'org-structure
  470. :group 'org-agenda-windows
  471. :type '(choice
  472. (const :tag "In current window" current-window)
  473. (const :tag "In current frame, other window" other-window)
  474. (const :tag "Each time a new frame" new-frame)
  475. (const :tag "One dedicated frame" dedicated-frame)))
  476. (defgroup org-cycle nil
  477. "Options concerning visibility cycling in Org-mode."
  478. :tag "Org Cycle"
  479. :group 'org-structure)
  480. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  481. "Names of drawers. Drawers are not opened by cycling on the headline above.
  482. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  483. this:
  484. :DRAWERNAME:
  485. .....
  486. :END:
  487. The drawer \"PROPERTIES\" is special for capturing properties through
  488. the property API.
  489. Drawers can be defined on the per-file basis with a line like:
  490. #+DRAWERS: HIDDEN STATE PROPERTIES"
  491. :group 'org-structure
  492. :type '(repeat (string :tag "Drawer Name")))
  493. (defcustom org-cycle-global-at-bob nil
  494. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  495. This makes it possible to do global cycling without having to use S-TAB or
  496. C-u TAB. For this special case to work, the first line of the buffer
  497. must not be a headline - it may be empty or some other text. When used in
  498. this way, `org-cycle-hook' is disables temporarily, to make sure the
  499. cursor stays at the beginning of the buffer.
  500. When this option is nil, don't do anything special at the beginning
  501. of the buffer."
  502. :group 'org-cycle
  503. :type 'boolean)
  504. (defcustom org-cycle-emulate-tab t
  505. "Where should `org-cycle' emulate TAB.
  506. nil Never
  507. white Only in completely white lines
  508. whitestart Only at the beginning of lines, before the first non-white char
  509. t Everywhere except in headlines
  510. exc-hl-bol Everywhere except at the start of a headline
  511. If TAB is used in a place where it does not emulate TAB, the current subtree
  512. visibility is cycled."
  513. :group 'org-cycle
  514. :type '(choice (const :tag "Never" nil)
  515. (const :tag "Only in completely white lines" white)
  516. (const :tag "Before first char in a line" whitestart)
  517. (const :tag "Everywhere except in headlines" t)
  518. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  519. ))
  520. (defcustom org-cycle-separator-lines 2
  521. "Number of empty lines needed to keep an empty line between collapsed trees.
  522. If you leave an empty line between the end of a subtree and the following
  523. headline, this empty line is hidden when the subtree is folded.
  524. Org-mode will leave (exactly) one empty line visible if the number of
  525. empty lines is equal or larger to the number given in this variable.
  526. So the default 2 means, at least 2 empty lines after the end of a subtree
  527. are needed to produce free space between a collapsed subtree and the
  528. following headline.
  529. Special case: when 0, never leave empty lines in collapsed view."
  530. :group 'org-cycle
  531. :type 'integer)
  532. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  533. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  534. org-cycle-hide-drawers
  535. org-cycle-show-empty-lines
  536. org-optimize-window-after-visibility-change)
  537. "Hook that is run after `org-cycle' has changed the buffer visibility.
  538. The function(s) in this hook must accept a single argument which indicates
  539. the new state that was set by the most recent `org-cycle' command. The
  540. argument is a symbol. After a global state change, it can have the values
  541. `overview', `content', or `all'. After a local state change, it can have
  542. the values `folded', `children', or `subtree'."
  543. :group 'org-cycle
  544. :type 'hook)
  545. (defgroup org-edit-structure nil
  546. "Options concerning structure editing in Org-mode."
  547. :tag "Org Edit Structure"
  548. :group 'org-structure)
  549. (defcustom org-odd-levels-only nil
  550. "Non-nil means, skip even levels and only use odd levels for the outline.
  551. This has the effect that two stars are being added/taken away in
  552. promotion/demotion commands. It also influences how levels are
  553. handled by the exporters.
  554. Changing it requires restart of `font-lock-mode' to become effective
  555. for fontification also in regions already fontified.
  556. You may also set this on a per-file basis by adding one of the following
  557. lines to the buffer:
  558. #+STARTUP: odd
  559. #+STARTUP: oddeven"
  560. :group 'org-edit-structure
  561. :group 'org-font-lock
  562. :type 'boolean)
  563. (defcustom org-adapt-indentation t
  564. "Non-nil means, adapt indentation when promoting and demoting.
  565. When this is set and the *entire* text in an entry is indented, the
  566. indentation is increased by one space in a demotion command, and
  567. decreased by one in a promotion command. If any line in the entry
  568. body starts at column 0, indentation is not changed at all."
  569. :group 'org-edit-structure
  570. :type 'boolean)
  571. (defcustom org-special-ctrl-a/e nil
  572. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  573. When t, `C-a' will bring back the cursor to the beginning of the
  574. headline text, i.e. after the stars and after a possible TODO keyword.
  575. In an item, this will be the position after the bullet.
  576. When the cursor is already at that position, another `C-a' will bring
  577. it to the beginning of the line.
  578. `C-e' will jump to the end of the headline, ignoring the presence of tags
  579. in the headline. A second `C-e' will then jump to the true end of the
  580. line, after any tags.
  581. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  582. and only a directly following, identical keypress will bring the cursor
  583. to the special positions."
  584. :group 'org-edit-structure
  585. :type '(choice
  586. (const :tag "off" nil)
  587. (const :tag "after bullet first" t)
  588. (const :tag "border first" reversed)))
  589. (if (fboundp 'defvaralias)
  590. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  591. (defcustom org-special-ctrl-k nil
  592. "Non-nil means `C-k' will behave specially in headlines.
  593. When nil, `C-k' will call the default `kill-line' command.
  594. When t, the following will happen while the cursor is in the headline:
  595. - When the cursor is at the beginning of a headline, kill the entire
  596. line and possible the folded subtree below the line.
  597. - When in the middle of the headline text, kill the headline up to the tags.
  598. - When after the headline text, kill the tags."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-yank-folded-subtrees t
  602. "Non-nil means, when yanking subtrees, fold them.
  603. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  604. it starts with a heading and all other headings in it are either children
  605. or siblings, then fold all the subtrees. However, do this only if no
  606. text after the yank would be swallowed into a folded tree by this action."
  607. :group 'org-edit-structure
  608. :type 'boolean)
  609. (defcustom org-yank-adjusted-subtrees nil
  610. "Non-nil means, when yanking subtrees, adjust the level.
  611. With this setting, `org-paste-subtree' is used to insert the subtree, see
  612. this function for details."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defcustom org-M-RET-may-split-line '((default . t))
  616. "Non-nil means, M-RET will split the line at the cursor position.
  617. When nil, it will go to the end of the line before making a
  618. new line.
  619. You may also set this option in a different way for different
  620. contexts. Valid contexts are:
  621. headline when creating a new headline
  622. item when creating a new item
  623. table in a table field
  624. default the value to be used for all contexts not explicitly
  625. customized"
  626. :group 'org-structure
  627. :group 'org-table
  628. :type '(choice
  629. (const :tag "Always" t)
  630. (const :tag "Never" nil)
  631. (repeat :greedy t :tag "Individual contexts"
  632. (cons
  633. (choice :tag "Context"
  634. (const headline)
  635. (const item)
  636. (const table)
  637. (const default))
  638. (boolean)))))
  639. (defcustom org-insert-heading-respect-content nil
  640. "Non-nil means, insert new headings after the current subtree.
  641. When nil, the new heading is created directly after the current line.
  642. The commands \\[org-insert-heading-respect-content] and
  643. \\[org-insert-todo-heading-respect-content] turn this variable on
  644. for the duration of the command."
  645. :group 'org-structure
  646. :type 'boolean)
  647. (defcustom org-blank-before-new-entry '((heading . auto)
  648. (plain-list-item . auto))
  649. "Should `org-insert-heading' leave a blank line before new heading/item?
  650. The value is an alist, with `heading' and `plain-list-item' as car,
  651. and a boolean flag as cdr."
  652. :group 'org-edit-structure
  653. :type '(list
  654. (cons (const heading)
  655. (choice (const :tag "Never" nil)
  656. (const :tag "Always" t)
  657. (const :tag "Auto" auto)))
  658. (cons (const plain-list-item)
  659. (choice (const :tag "Never" nil)
  660. (const :tag "Always" t)
  661. (const :tag "Auto" auto)))))
  662. (defcustom org-insert-heading-hook nil
  663. "Hook being run after inserting a new heading."
  664. :group 'org-edit-structure
  665. :type 'hook)
  666. (defcustom org-enable-fixed-width-editor t
  667. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  668. This currently only means, they are never auto-wrapped.
  669. When nil, such lines will be treated like ordinary lines.
  670. See also the QUOTE keyword."
  671. :group 'org-edit-structure
  672. :type 'boolean)
  673. (defcustom org-edit-src-region-extra nil
  674. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  675. For examples see the function `org-edit-src-find-region-and-lang'.
  676. The regular expression identifying the begin marker should end with a newline,
  677. and the regexp marking the end line should start with a newline, to make sure
  678. there are kept outside the narrowed region."
  679. :group 'org-edit-structure
  680. :type '(repeat
  681. (list
  682. (regexp :tag "begin regexp")
  683. (regexp :tag "end regexp")
  684. (choice :tag "language"
  685. (string :tag "specify")
  686. (integer :tag "from match group")
  687. (const :tag "from `lang' element")
  688. (const :tag "from `style' element")))))
  689. (defcustom org-coderef-label-format "(ref:%s)"
  690. "The default coderef format.
  691. This format string will be used to search for coderef labels in literal
  692. examples (EXAMPLE and SRC blocks). The format can be overwritten
  693. an individual literal example with the -f option, like
  694. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  695. ...
  696. #+END_SRC
  697. If you want to use this for HTML export, make sure that the format does
  698. not introduce special font-locking, and avoid the HTML special
  699. characters `<', `>', and `&'. The reason for this restriction is that
  700. the labels are searched for only after htmlize has done its job."
  701. :group 'org-edit-structure ; FIXME this is not in the right group
  702. :type 'string)
  703. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  704. "The mode that should be used to edit fixed-width regions.
  705. These are the regions where each line starts with a colon."
  706. :group 'org-edit-structure
  707. :type '(choice
  708. (const artist-mode)
  709. (const picture-mode)
  710. (const fundamental-mode)
  711. (function :tag "Other (specify)")))
  712. (defcustom org-goto-auto-isearch t
  713. "Non-nil means, typing characters in org-goto starts incremental search."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defgroup org-sparse-trees nil
  717. "Options concerning sparse trees in Org-mode."
  718. :tag "Org Sparse Trees"
  719. :group 'org-structure)
  720. (defcustom org-highlight-sparse-tree-matches t
  721. "Non-nil means, highlight all matches that define a sparse tree.
  722. The highlights will automatically disappear the next time the buffer is
  723. changed by an edit command."
  724. :group 'org-sparse-trees
  725. :type 'boolean)
  726. (defcustom org-remove-highlights-with-change t
  727. "Non-nil means, any change to the buffer will remove temporary highlights.
  728. Such highlights are created by `org-occur' and `org-clock-display'.
  729. When nil, `C-c C-c needs to be used to get rid of the highlights.
  730. The highlights created by `org-preview-latex-fragment' always need
  731. `C-c C-c' to be removed."
  732. :group 'org-sparse-trees
  733. :group 'org-time
  734. :type 'boolean)
  735. (defcustom org-occur-hook '(org-first-headline-recenter)
  736. "Hook that is run after `org-occur' has constructed a sparse tree.
  737. This can be used to recenter the window to show as much of the structure
  738. as possible."
  739. :group 'org-sparse-trees
  740. :type 'hook)
  741. (defgroup org-imenu-and-speedbar nil
  742. "Options concerning imenu and speedbar in Org-mode."
  743. :tag "Org Imenu and Speedbar"
  744. :group 'org-structure)
  745. (defcustom org-imenu-depth 2
  746. "The maximum level for Imenu access to Org-mode headlines.
  747. This also applied for speedbar access."
  748. :group 'org-imenu-and-speedbar
  749. :type 'number)
  750. (defgroup org-table nil
  751. "Options concerning tables in Org-mode."
  752. :tag "Org Table"
  753. :group 'org)
  754. (defcustom org-enable-table-editor 'optimized
  755. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  756. When nil, such lines will be treated like ordinary lines.
  757. When equal to the symbol `optimized', the table editor will be optimized to
  758. do the following:
  759. - Automatic overwrite mode in front of whitespace in table fields.
  760. This makes the structure of the table stay in tact as long as the edited
  761. field does not exceed the column width.
  762. - Minimize the number of realigns. Normally, the table is aligned each time
  763. TAB or RET are pressed to move to another field. With optimization this
  764. happens only if changes to a field might have changed the column width.
  765. Optimization requires replacing the functions `self-insert-command',
  766. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  767. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  768. very good at guessing when a re-align will be necessary, but you can always
  769. force one with \\[org-ctrl-c-ctrl-c].
  770. If you would like to use the optimized version in Org-mode, but the
  771. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  772. This variable can be used to turn on and off the table editor during a session,
  773. but in order to toggle optimization, a restart is required.
  774. See also the variable `org-table-auto-blank-field'."
  775. :group 'org-table
  776. :type '(choice
  777. (const :tag "off" nil)
  778. (const :tag "on" t)
  779. (const :tag "on, optimized" optimized)))
  780. (defcustom org-table-tab-recognizes-table.el t
  781. "Non-nil means, TAB will automatically notice a table.el table.
  782. When it sees such a table, it moves point into it and - if necessary -
  783. calls `table-recognize-table'."
  784. :group 'org-table-editing
  785. :type 'boolean)
  786. (defgroup org-link nil
  787. "Options concerning links in Org-mode."
  788. :tag "Org Link"
  789. :group 'org)
  790. (defvar org-link-abbrev-alist-local nil
  791. "Buffer-local version of `org-link-abbrev-alist', which see.
  792. The value of this is taken from the #+LINK lines.")
  793. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  794. (defcustom org-link-abbrev-alist nil
  795. "Alist of link abbreviations.
  796. The car of each element is a string, to be replaced at the start of a link.
  797. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  798. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  799. [[linkkey:tag][description]]
  800. If REPLACE is a string, the tag will simply be appended to create the link.
  801. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  802. the placeholder \"%h\" will cause a url-encoded version of the tag to
  803. be inserted at that point (see the function `url-hexify-string').
  804. REPLACE may also be a function that will be called with the tag as the
  805. only argument to create the link, which should be returned as a string.
  806. See the manual for examples."
  807. :group 'org-link
  808. :type '(repeat
  809. (cons
  810. (string :tag "Protocol")
  811. (choice
  812. (string :tag "Format")
  813. (function)))))
  814. (defcustom org-descriptive-links t
  815. "Non-nil means, hide link part and only show description of bracket links.
  816. Bracket links are like [[link][description]]. This variable sets the initial
  817. state in new org-mode buffers. The setting can then be toggled on a
  818. per-buffer basis from the Org->Hyperlinks menu."
  819. :group 'org-link
  820. :type 'boolean)
  821. (defcustom org-link-file-path-type 'adaptive
  822. "How the path name in file links should be stored.
  823. Valid values are:
  824. relative Relative to the current directory, i.e. the directory of the file
  825. into which the link is being inserted.
  826. absolute Absolute path, if possible with ~ for home directory.
  827. noabbrev Absolute path, no abbreviation of home directory.
  828. adaptive Use relative path for files in the current directory and sub-
  829. directories of it. For other files, use an absolute path."
  830. :group 'org-link
  831. :type '(choice
  832. (const relative)
  833. (const absolute)
  834. (const noabbrev)
  835. (const adaptive)))
  836. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  837. "Types of links that should be activated in Org-mode files.
  838. This is a list of symbols, each leading to the activation of a certain link
  839. type. In principle, it does not hurt to turn on most link types - there may
  840. be a small gain when turning off unused link types. The types are:
  841. bracket The recommended [[link][description]] or [[link]] links with hiding.
  842. angular Links in angular brackets that may contain whitespace like
  843. <bbdb:Carsten Dominik>.
  844. plain Plain links in normal text, no whitespace, like http://google.com.
  845. radio Text that is matched by a radio target, see manual for details.
  846. tag Tag settings in a headline (link to tag search).
  847. date Time stamps (link to calendar).
  848. footnote Footnote labels.
  849. Changing this variable requires a restart of Emacs to become effective."
  850. :group 'org-link
  851. :type '(set :greedy t
  852. (const :tag "Double bracket links (new style)" bracket)
  853. (const :tag "Angular bracket links (old style)" angular)
  854. (const :tag "Plain text links" plain)
  855. (const :tag "Radio target matches" radio)
  856. (const :tag "Tags" tag)
  857. (const :tag "Timestamps" date)
  858. (const :tag "Footnotes" footnote)))
  859. (defcustom org-make-link-description-function nil
  860. "Function to use to generate link descriptions from links. If
  861. nil the link location will be used. This function must take two
  862. parameters; the first is the link and the second the description
  863. org-insert-link has generated, and should return the description
  864. to use."
  865. :group 'org-link
  866. :type 'function)
  867. (defgroup org-link-store nil
  868. "Options concerning storing links in Org-mode."
  869. :tag "Org Store Link"
  870. :group 'org-link)
  871. (defcustom org-email-link-description-format "Email %c: %.30s"
  872. "Format of the description part of a link to an email or usenet message.
  873. The following %-escapes will be replaced by corresponding information:
  874. %F full \"From\" field
  875. %f name, taken from \"From\" field, address if no name
  876. %T full \"To\" field
  877. %t first name in \"To\" field, address if no name
  878. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  879. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  880. %s subject
  881. %m message-id.
  882. You may use normal field width specification between the % and the letter.
  883. This is for example useful to limit the length of the subject.
  884. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  885. :group 'org-link-store
  886. :type 'string)
  887. (defcustom org-from-is-user-regexp
  888. (let (r1 r2)
  889. (when (and user-mail-address (not (string= user-mail-address "")))
  890. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  891. (when (and user-full-name (not (string= user-full-name "")))
  892. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  893. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  894. "Regexp matched against the \"From:\" header of an email or usenet message.
  895. It should match if the message is from the user him/herself."
  896. :group 'org-link-store
  897. :type 'regexp)
  898. (defcustom org-link-to-org-use-id 'create-if-interactive
  899. "Non-nil means, storing a link to an Org file will use entry IDs.
  900. Note that before this variable is even considered, org-id must be loaded,
  901. to please customize `org-modules' and turn it on.
  902. The variable can have the following values:
  903. t Create an ID if needed to make a link to the current entry.
  904. create-if-interactive
  905. If `org-store-link' is called directly (interactively, as a user
  906. command), do create an ID to support the link. But when doing the
  907. job for remember, only use the ID if it already exists. The
  908. purpose of this setting is to avoid proliferation of unwanted
  909. IDs, just because you happen to be in an Org file when you
  910. call `org-remember' that automatically and preemptively
  911. creates a link. If you do want to get an ID link in a remember
  912. template to an entry not having an ID, create it first by
  913. explicitly creating a link to it, using `C-c C-l' first.
  914. use-existing
  915. Use existing ID, do not create one.
  916. nil Never use an ID to make a link, instead link using a text search for
  917. the headline text."
  918. :group 'org-link-store
  919. :type '(choice
  920. (const :tag "Create ID to make link" t)
  921. (const :tag "Create if string link interactively"
  922. 'create-if-interactive)
  923. (const :tag "Only use existing" 'use-existing)
  924. (const :tag "Do not use ID to create link" nil)))
  925. (defcustom org-context-in-file-links t
  926. "Non-nil means, file links from `org-store-link' contain context.
  927. A search string will be added to the file name with :: as separator and
  928. used to find the context when the link is activated by the command
  929. `org-open-at-point'.
  930. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  931. negates this setting for the duration of the command."
  932. :group 'org-link-store
  933. :type 'boolean)
  934. (defcustom org-keep-stored-link-after-insertion nil
  935. "Non-nil means, keep link in list for entire session.
  936. The command `org-store-link' adds a link pointing to the current
  937. location to an internal list. These links accumulate during a session.
  938. The command `org-insert-link' can be used to insert links into any
  939. Org-mode file (offering completion for all stored links). When this
  940. option is nil, every link which has been inserted once using \\[org-insert-link]
  941. will be removed from the list, to make completing the unused links
  942. more efficient."
  943. :group 'org-link-store
  944. :type 'boolean)
  945. (defgroup org-link-follow nil
  946. "Options concerning following links in Org-mode."
  947. :tag "Org Follow Link"
  948. :group 'org-link)
  949. (defcustom org-link-translation-function nil
  950. "Function to translate links with different syntax to Org syntax.
  951. This can be used to translate links created for example by the Planner
  952. or emacs-wiki packages to Org syntax.
  953. The function must accept two parameters, a TYPE containing the link
  954. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  955. which is everything after the link protocol. It should return a cons
  956. with possibly modified values of type and path.
  957. Org contains a function for this, so if you set this variable to
  958. `org-translate-link-from-planner', you should be able follow many
  959. links created by planner."
  960. :group 'org-link-follow
  961. :type 'function)
  962. (defcustom org-follow-link-hook nil
  963. "Hook that is run after a link has been followed."
  964. :group 'org-link-follow
  965. :type 'hook)
  966. (defcustom org-tab-follows-link nil
  967. "Non-nil means, on links TAB will follow the link.
  968. Needs to be set before org.el is loaded.
  969. This really should not be used, it does not make sense, and the
  970. implementation is bad."
  971. :group 'org-link-follow
  972. :type 'boolean)
  973. (defcustom org-return-follows-link nil
  974. "Non-nil means, on links RET will follow the link.
  975. Needs to be set before org.el is loaded."
  976. :group 'org-link-follow
  977. :type 'boolean)
  978. (defcustom org-mouse-1-follows-link
  979. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  980. "Non-nil means, mouse-1 on a link will follow the link.
  981. A longer mouse click will still set point. Does not work on XEmacs.
  982. Needs to be set before org.el is loaded."
  983. :group 'org-link-follow
  984. :type 'boolean)
  985. (defcustom org-mark-ring-length 4
  986. "Number of different positions to be recorded in the ring
  987. Changing this requires a restart of Emacs to work correctly."
  988. :group 'org-link-follow
  989. :type 'integer)
  990. (defcustom org-link-frame-setup
  991. '((vm . vm-visit-folder-other-frame)
  992. (gnus . gnus-other-frame)
  993. (file . find-file-other-window))
  994. "Setup the frame configuration for following links.
  995. When following a link with Emacs, it may often be useful to display
  996. this link in another window or frame. This variable can be used to
  997. set this up for the different types of links.
  998. For VM, use any of
  999. `vm-visit-folder'
  1000. `vm-visit-folder-other-frame'
  1001. For Gnus, use any of
  1002. `gnus'
  1003. `gnus-other-frame'
  1004. `org-gnus-no-new-news'
  1005. For FILE, use any of
  1006. `find-file'
  1007. `find-file-other-window'
  1008. `find-file-other-frame'
  1009. For the calendar, use the variable `calendar-setup'.
  1010. For BBDB, it is currently only possible to display the matches in
  1011. another window."
  1012. :group 'org-link-follow
  1013. :type '(list
  1014. (cons (const vm)
  1015. (choice
  1016. (const vm-visit-folder)
  1017. (const vm-visit-folder-other-window)
  1018. (const vm-visit-folder-other-frame)))
  1019. (cons (const gnus)
  1020. (choice
  1021. (const gnus)
  1022. (const gnus-other-frame)
  1023. (const org-gnus-no-new-news)))
  1024. (cons (const file)
  1025. (choice
  1026. (const find-file)
  1027. (const find-file-other-window)
  1028. (const find-file-other-frame)))))
  1029. (defcustom org-display-internal-link-with-indirect-buffer nil
  1030. "Non-nil means, use indirect buffer to display infile links.
  1031. Activating internal links (from one location in a file to another location
  1032. in the same file) normally just jumps to the location. When the link is
  1033. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1034. another window. When this option is set, the other window actually displays
  1035. an indirect buffer clone of the current buffer, to avoid any visibility
  1036. changes to the current buffer."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-open-non-existing-files nil
  1040. "Non-nil means, `org-open-file' will open non-existing files.
  1041. When nil, an error will be generated."
  1042. :group 'org-link-follow
  1043. :type 'boolean)
  1044. (defcustom org-open-directory-means-index-dot-org nil
  1045. "Non-nil means, a link to a directory really means to index.org.
  1046. When nil, following a directory link will run dired or open a finder/explorer
  1047. window on that directory."
  1048. :group 'org-link-follow
  1049. :type 'boolean)
  1050. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1051. "Function and arguments to call for following mailto links.
  1052. This is a list with the first element being a lisp function, and the
  1053. remaining elements being arguments to the function. In string arguments,
  1054. %a will be replaced by the address, and %s will be replaced by the subject
  1055. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1056. :group 'org-link-follow
  1057. :type '(choice
  1058. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1059. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1060. (const :tag "message-mail" (message-mail "%a" "%s"))
  1061. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1062. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1063. "Non-nil means, ask for confirmation before executing shell links.
  1064. Shell links can be dangerous: just think about a link
  1065. [[shell:rm -rf ~/*][Google Search]]
  1066. This link would show up in your Org-mode document as \"Google Search\",
  1067. but really it would remove your entire home directory.
  1068. Therefore we advise against setting this variable to nil.
  1069. Just change it to `y-or-n-p' of you want to confirm with a
  1070. single keystroke rather than having to type \"yes\"."
  1071. :group 'org-link-follow
  1072. :type '(choice
  1073. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1074. (const :tag "with y-or-n (faster)" y-or-n-p)
  1075. (const :tag "no confirmation (dangerous)" nil)))
  1076. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1077. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1078. Elisp links can be dangerous: just think about a link
  1079. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1080. This link would show up in your Org-mode document as \"Google Search\",
  1081. but really it would remove your entire home directory.
  1082. Therefore we advise against setting this variable to nil.
  1083. Just change it to `y-or-n-p' of you want to confirm with a
  1084. single keystroke rather than having to type \"yes\"."
  1085. :group 'org-link-follow
  1086. :type '(choice
  1087. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1088. (const :tag "with y-or-n (faster)" y-or-n-p)
  1089. (const :tag "no confirmation (dangerous)" nil)))
  1090. (defconst org-file-apps-defaults-gnu
  1091. '((remote . emacs)
  1092. (system . mailcap)
  1093. (t . mailcap))
  1094. "Default file applications on a UNIX or GNU/Linux system.
  1095. See `org-file-apps'.")
  1096. (defconst org-file-apps-defaults-macosx
  1097. '((remote . emacs)
  1098. (t . "open %s")
  1099. (system . "open %s")
  1100. ("ps.gz" . "gv %s")
  1101. ("eps.gz" . "gv %s")
  1102. ("dvi" . "xdvi %s")
  1103. ("fig" . "xfig %s"))
  1104. "Default file applications on a MacOS X system.
  1105. The system \"open\" is known as a default, but we use X11 applications
  1106. for some files for which the OS does not have a good default.
  1107. See `org-file-apps'.")
  1108. (defconst org-file-apps-defaults-windowsnt
  1109. (list
  1110. '(remote . emacs)
  1111. (cons t
  1112. (list (if (featurep 'xemacs)
  1113. 'mswindows-shell-execute
  1114. 'w32-shell-execute)
  1115. "open" 'file))
  1116. (cons 'system
  1117. (list (if (featurep 'xemacs)
  1118. 'mswindows-shell-execute
  1119. 'w32-shell-execute)
  1120. "open" 'file)))
  1121. "Default file applications on a Windows NT system.
  1122. The system \"open\" is used for most files.
  1123. See `org-file-apps'.")
  1124. (defcustom org-file-apps
  1125. '(
  1126. (auto-mode . emacs)
  1127. ("\\.x?html?\\'" . default)
  1128. ("\\.pdf\\'" . default)
  1129. )
  1130. "External applications for opening `file:path' items in a document.
  1131. Org-mode uses system defaults for different file types, but
  1132. you can use this variable to set the application for a given file
  1133. extension. The entries in this list are cons cells where the car identifies
  1134. files and the cdr the corresponding command. Possible values for the
  1135. file identifier are
  1136. \"regex\" Regular expression matched against the file name. For backward
  1137. compatibility, this can also be a string with only alphanumeric
  1138. characters, which is then interpreted as an extension.
  1139. `directory' Matches a directory
  1140. `remote' Matches a remote file, accessible through tramp or efs.
  1141. Remote files most likely should be visited through Emacs
  1142. because external applications cannot handle such paths.
  1143. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1144. so all files Emacs knows how to handle. Using this with
  1145. command `emacs' will open most files in Emacs. Beware that this
  1146. will also open html files inside Emacs, unless you add
  1147. (\"html\" . default) to the list as well.
  1148. t Default for files not matched by any of the other options.
  1149. `system' The system command to open files, like `open' on Windows
  1150. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1151. that will be selected if you call `C-c C-o' with a double
  1152. `C-u C-u' prefix.
  1153. Possible values for the command are:
  1154. `emacs' The file will be visited by the current Emacs process.
  1155. `default' Use the default application for this file type, which is the
  1156. association for t in the list, most likely in the system-specific
  1157. part.
  1158. This can be used to overrule an unwanted setting in the
  1159. system-specific variable.
  1160. `system' Use the system command for opening files, like \"open\".
  1161. This command is specified by the entry whose car is `system'.
  1162. Most likely, the system-specific version of this variable
  1163. does define this command, but you can overrule/replace it
  1164. here.
  1165. string A command to be executed by a shell; %s will be replaced
  1166. by the path to the file.
  1167. sexp A Lisp form which will be evaluated. The file path will
  1168. be available in the Lisp variable `file'.
  1169. For more examples, see the system specific constants
  1170. `org-file-apps-defaults-macosx'
  1171. `org-file-apps-defaults-windowsnt'
  1172. `org-file-apps-defaults-gnu'."
  1173. :group 'org-link-follow
  1174. :type '(repeat
  1175. (cons (choice :value ""
  1176. (string :tag "Extension")
  1177. (const :tag "System command to open files" system)
  1178. (const :tag "Default for unrecognized files" t)
  1179. (const :tag "Remote file" remote)
  1180. (const :tag "Links to a directory" directory)
  1181. (const :tag "Any files that have Emacs modes"
  1182. auto-mode))
  1183. (choice :value ""
  1184. (const :tag "Visit with Emacs" emacs)
  1185. (const :tag "Use default" default)
  1186. (const :tag "Use the system command" system)
  1187. (string :tag "Command")
  1188. (sexp :tag "Lisp form")))))
  1189. (defgroup org-refile nil
  1190. "Options concerning refiling entries in Org-mode."
  1191. :tag "Org Refile"
  1192. :group 'org)
  1193. (defcustom org-directory "~/org"
  1194. "Directory with org files.
  1195. This is just a default location to look for Org files. There is no need
  1196. at all to put your files into this directory. It is only used in the
  1197. following situations:
  1198. 1. When a remember template specifies a target file that is not an
  1199. absolute path. The path will then be interpreted relative to
  1200. `org-directory'
  1201. 2. When a remember note is filed away in an interactive way (when exiting the
  1202. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1203. with `org-directory' as the default path."
  1204. :group 'org-refile
  1205. :group 'org-remember
  1206. :type 'directory)
  1207. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1208. "Default target for storing notes.
  1209. Used by the hooks for remember.el. This can be a string, or nil to mean
  1210. the value of `remember-data-file'.
  1211. You can set this on a per-template basis with the variable
  1212. `org-remember-templates'."
  1213. :group 'org-refile
  1214. :group 'org-remember
  1215. :type '(choice
  1216. (const :tag "Default from remember-data-file" nil)
  1217. file))
  1218. (defcustom org-goto-interface 'outline
  1219. "The default interface to be used for `org-goto'.
  1220. Allowed values are:
  1221. outline The interface shows an outline of the relevant file
  1222. and the correct heading is found by moving through
  1223. the outline or by searching with incremental search.
  1224. outline-path-completion Headlines in the current buffer are offered via
  1225. completion. This is the interface also used by
  1226. the refile command."
  1227. :group 'org-refile
  1228. :type '(choice
  1229. (const :tag "Outline" outline)
  1230. (const :tag "Outline-path-completion" outline-path-completion)))
  1231. (defcustom org-goto-max-level 5
  1232. "Maximum level to be considered when running org-goto with refile interface."
  1233. :group 'org-refile
  1234. :type 'number)
  1235. (defcustom org-reverse-note-order nil
  1236. "Non-nil means, store new notes at the beginning of a file or entry.
  1237. When nil, new notes will be filed to the end of a file or entry.
  1238. This can also be a list with cons cells of regular expressions that
  1239. are matched against file names, and values."
  1240. :group 'org-remember
  1241. :group 'org-refile
  1242. :type '(choice
  1243. (const :tag "Reverse always" t)
  1244. (const :tag "Reverse never" nil)
  1245. (repeat :tag "By file name regexp"
  1246. (cons regexp boolean))))
  1247. (defcustom org-refile-targets nil
  1248. "Targets for refiling entries with \\[org-refile].
  1249. This is list of cons cells. Each cell contains:
  1250. - a specification of the files to be considered, either a list of files,
  1251. or a symbol whose function or variable value will be used to retrieve
  1252. a file name or a list of file names. If you use `org-agenda-files' for
  1253. that, all agenda files will be scanned for targets. Nil means, consider
  1254. headings in the current buffer.
  1255. - A specification of how to find candidate refile targets. This may be
  1256. any of:
  1257. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1258. This tag has to be present in all target headlines, inheritance will
  1259. not be considered.
  1260. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1261. todo keyword.
  1262. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1263. headlines that are refiling targets.
  1264. - a cons cell (:level . N). Any headline of level N is considered a target.
  1265. Note that, when `org-odd-levels-only' is set, level corresponds to
  1266. order in hierarchy, not to the number of stars.
  1267. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1268. Note that, when `org-odd-levels-only' is set, level corresponds to
  1269. order in hierarchy, not to the number of stars.
  1270. When this variable is nil, all top-level headlines in the current buffer
  1271. are used, equivalent to the value `((nil . (:level . 1))'."
  1272. :group 'org-refile
  1273. :type '(repeat
  1274. (cons
  1275. (choice :value org-agenda-files
  1276. (const :tag "All agenda files" org-agenda-files)
  1277. (const :tag "Current buffer" nil)
  1278. (function) (variable) (file))
  1279. (choice :tag "Identify target headline by"
  1280. (cons :tag "Specific tag" (const :value :tag) (string))
  1281. (cons :tag "TODO keyword" (const :value :todo) (string))
  1282. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1283. (cons :tag "Level number" (const :value :level) (integer))
  1284. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1285. (defcustom org-refile-use-outline-path nil
  1286. "Non-nil means, provide refile targets as paths.
  1287. So a level 3 headline will be available as level1/level2/level3.
  1288. When the value is `file', also include the file name (without directory)
  1289. into the path. When `full-file-path', include the full file path."
  1290. :group 'org-refile
  1291. :type '(choice
  1292. (const :tag "Not" nil)
  1293. (const :tag "Yes" t)
  1294. (const :tag "Start with file name" file)
  1295. (const :tag "Start with full file path" full-file-path)))
  1296. (defcustom org-outline-path-complete-in-steps t
  1297. "Non-nil means, complete the outline path in hierarchical steps.
  1298. When Org-mode uses the refile interface to select an outline path
  1299. \(see variable `org-refile-use-outline-path'), the completion of
  1300. the path can be done is a single go, or if can be done in steps down
  1301. the headline hierarchy. Going in steps is probably the best if you
  1302. do not use a special completion package like `ido' or `icicles'.
  1303. However, when using these packages, going in one step can be very
  1304. fast, while still showing the whole path to the entry."
  1305. :group 'org-refile
  1306. :type 'boolean)
  1307. (defgroup org-todo nil
  1308. "Options concerning TODO items in Org-mode."
  1309. :tag "Org TODO"
  1310. :group 'org)
  1311. (defgroup org-progress nil
  1312. "Options concerning Progress logging in Org-mode."
  1313. :tag "Org Progress"
  1314. :group 'org-time)
  1315. (defvar org-todo-interpretation-widgets
  1316. '(
  1317. (:tag "Sequence (cycling hits every state)" sequence)
  1318. (:tag "Type (cycling directly to DONE)" type))
  1319. "The available interpretation symbols for customizing
  1320. `org-todo-keywords'.
  1321. Interested libraries should add to this list.")
  1322. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1323. "List of TODO entry keyword sequences and their interpretation.
  1324. \\<org-mode-map>This is a list of sequences.
  1325. Each sequence starts with a symbol, either `sequence' or `type',
  1326. indicating if the keywords should be interpreted as a sequence of
  1327. action steps, or as different types of TODO items. The first
  1328. keywords are states requiring action - these states will select a headline
  1329. for inclusion into the global TODO list Org-mode produces. If one of
  1330. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1331. signify that no further action is necessary. If \"|\" is not found,
  1332. the last keyword is treated as the only DONE state of the sequence.
  1333. The command \\[org-todo] cycles an entry through these states, and one
  1334. additional state where no keyword is present. For details about this
  1335. cycling, see the manual.
  1336. TODO keywords and interpretation can also be set on a per-file basis with
  1337. the special #+SEQ_TODO and #+TYP_TODO lines.
  1338. Each keyword can optionally specify a character for fast state selection
  1339. \(in combination with the variable `org-use-fast-todo-selection')
  1340. and specifiers for state change logging, using the same syntax
  1341. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1342. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1343. indicates to record a time stamp each time this state is selected.
  1344. Each keyword may also specify if a timestamp or a note should be
  1345. recorded when entering or leaving the state, by adding additional
  1346. characters in the parenthesis after the keyword. This looks like this:
  1347. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1348. record only the time of the state change. With X and Y being either
  1349. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1350. Y when leaving the state if and only if the *target* state does not
  1351. define X. You may omit any of the fast-selection key or X or /Y,
  1352. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1353. For backward compatibility, this variable may also be just a list
  1354. of keywords - in this case the interpretation (sequence or type) will be
  1355. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1356. :group 'org-todo
  1357. :group 'org-keywords
  1358. :type '(choice
  1359. (repeat :tag "Old syntax, just keywords"
  1360. (string :tag "Keyword"))
  1361. (repeat :tag "New syntax"
  1362. (cons
  1363. (choice
  1364. :tag "Interpretation"
  1365. ;;Quick and dirty way to see
  1366. ;;`org-todo-interpretations'. This takes the
  1367. ;;place of item arguments
  1368. :convert-widget
  1369. (lambda (widget)
  1370. (widget-put widget
  1371. :args (mapcar
  1372. #'(lambda (x)
  1373. (widget-convert
  1374. (cons 'const x)))
  1375. org-todo-interpretation-widgets))
  1376. widget))
  1377. (repeat
  1378. (string :tag "Keyword"))))))
  1379. (defvar org-todo-keywords-1 nil
  1380. "All TODO and DONE keywords active in a buffer.")
  1381. (make-variable-buffer-local 'org-todo-keywords-1)
  1382. (defvar org-todo-keywords-for-agenda nil)
  1383. (defvar org-done-keywords-for-agenda nil)
  1384. (defvar org-todo-keyword-alist-for-agenda nil)
  1385. (defvar org-tag-alist-for-agenda nil)
  1386. (defvar org-agenda-contributing-files nil)
  1387. (defvar org-not-done-keywords nil)
  1388. (make-variable-buffer-local 'org-not-done-keywords)
  1389. (defvar org-done-keywords nil)
  1390. (make-variable-buffer-local 'org-done-keywords)
  1391. (defvar org-todo-heads nil)
  1392. (make-variable-buffer-local 'org-todo-heads)
  1393. (defvar org-todo-sets nil)
  1394. (make-variable-buffer-local 'org-todo-sets)
  1395. (defvar org-todo-log-states nil)
  1396. (make-variable-buffer-local 'org-todo-log-states)
  1397. (defvar org-todo-kwd-alist nil)
  1398. (make-variable-buffer-local 'org-todo-kwd-alist)
  1399. (defvar org-todo-key-alist nil)
  1400. (make-variable-buffer-local 'org-todo-key-alist)
  1401. (defvar org-todo-key-trigger nil)
  1402. (make-variable-buffer-local 'org-todo-key-trigger)
  1403. (defcustom org-todo-interpretation 'sequence
  1404. "Controls how TODO keywords are interpreted.
  1405. This variable is in principle obsolete and is only used for
  1406. backward compatibility, if the interpretation of todo keywords is
  1407. not given already in `org-todo-keywords'. See that variable for
  1408. more information."
  1409. :group 'org-todo
  1410. :group 'org-keywords
  1411. :type '(choice (const sequence)
  1412. (const type)))
  1413. (defcustom org-use-fast-todo-selection t
  1414. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1415. This variable describes if and under what circumstances the cycling
  1416. mechanism for TODO keywords will be replaced by a single-key, direct
  1417. selection scheme.
  1418. When nil, fast selection is never used.
  1419. When the symbol `prefix', it will be used when `org-todo' is called with
  1420. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1421. in an agenda buffer.
  1422. When t, fast selection is used by default. In this case, the prefix
  1423. argument forces cycling instead.
  1424. In all cases, the special interface is only used if access keys have actually
  1425. been assigned by the user, i.e. if keywords in the configuration are followed
  1426. by a letter in parenthesis, like TODO(t)."
  1427. :group 'org-todo
  1428. :type '(choice
  1429. (const :tag "Never" nil)
  1430. (const :tag "By default" t)
  1431. (const :tag "Only with C-u C-c C-t" prefix)))
  1432. (defcustom org-provide-todo-statistics t
  1433. "Non-nil means, update todo statistics after insert and toggle.
  1434. When this is set, todo statistics is updated in the parent of the current
  1435. entry each time a todo state is changed."
  1436. :group 'org-todo
  1437. :type 'boolean)
  1438. (defcustom org-after-todo-state-change-hook nil
  1439. "Hook which is run after the state of a TODO item was changed.
  1440. The new state (a string with a TODO keyword, or nil) is available in the
  1441. Lisp variable `state'."
  1442. :group 'org-todo
  1443. :type 'hook)
  1444. (defvar org-blocker-hook nil
  1445. "Hook for functions that are allowed to block a state change.
  1446. Each function gets as its single argument a property list, see
  1447. `org-trigger-hook' for more information about this list.
  1448. If any of the functions in this hook returns nil, the state change
  1449. is blocked.")
  1450. (defvar org-trigger-hook nil
  1451. "Hook for functions that are triggered by a state change.
  1452. Each function gets as its single argument a property list with at least
  1453. the following elements:
  1454. (:type type-of-change :position pos-at-entry-start
  1455. :from old-state :to new-state)
  1456. Depending on the type, more properties may be present.
  1457. This mechanism is currently implemented for:
  1458. TODO state changes
  1459. ------------------
  1460. :type todo-state-change
  1461. :from previous state (keyword as a string), or nil, or a symbol
  1462. 'todo' or 'done', to indicate the general type of state.
  1463. :to new state, like in :from")
  1464. (defcustom org-enforce-todo-dependencies nil
  1465. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1466. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1467. be blocked if any prior sibling is not yet done.
  1468. This variable needs to be set before org.el is loaded, and you need to
  1469. restart Emacs after a change to make the change effective. The only way
  1470. to change is while Emacs is running is through the customize interface."
  1471. :set (lambda (var val)
  1472. (set var val)
  1473. (if val
  1474. (add-hook 'org-blocker-hook
  1475. 'org-block-todo-from-children-or-siblings)
  1476. (remove-hook 'org-blocker-hook
  1477. 'org-block-todo-from-children-or-siblings)))
  1478. :group 'org-todo
  1479. :type 'boolean)
  1480. (defcustom org-enforce-todo-checkbox-dependencies nil
  1481. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1482. When this is nil, checkboxes have no influence on switching TODO states.
  1483. When non-nil, you first need to check off all check boxes before the TODO
  1484. entry can be switched to DONE.
  1485. This variable needs to be set before org.el is loaded, and you need to
  1486. restart Emacs after a change to make the change effective. The only way
  1487. to change is while Emacs is running is through the customize interface."
  1488. :set (lambda (var val)
  1489. (set var val)
  1490. (if val
  1491. (add-hook 'org-blocker-hook
  1492. 'org-block-todo-from-checkboxes)
  1493. (remove-hook 'org-blocker-hook
  1494. 'org-block-todo-from-checkboxes)))
  1495. :group 'org-todo
  1496. :type 'boolean)
  1497. (defcustom org-todo-state-tags-triggers nil
  1498. "Tag changes that should be triggered by TODO state changes.
  1499. This is a list. Each entry is
  1500. (state-change (tag . flag) .......)
  1501. State-change can be a string with a state, and empty string to indicate the
  1502. state that has no TODO keyword, or it can be one of the symbols `todo'
  1503. or `done', meaning any not-done or done state, respectively."
  1504. :group 'org-todo
  1505. :group 'org-tags
  1506. :type '(repeat
  1507. (cons (choice :tag "When changing to"
  1508. (const :tag "Not-done state" todo)
  1509. (const :tag "Done state" done)
  1510. (string :tag "State"))
  1511. (repeat
  1512. (cons :tag "Tag action"
  1513. (string :tag "Tag")
  1514. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1515. (defcustom org-log-done nil
  1516. "Information to record when a task moves to the DONE state.
  1517. Possible values are:
  1518. nil Don't add anything, just change the keyword
  1519. time Add a time stamp to the task
  1520. note Prompt a closing note and add it with template `org-log-note-headings'
  1521. This option can also be set with on a per-file-basis with
  1522. #+STARTUP: nologdone
  1523. #+STARTUP: logdone
  1524. #+STARTUP: lognotedone
  1525. You can have local logging settings for a subtree by setting the LOGGING
  1526. property to one or more of these keywords."
  1527. :group 'org-todo
  1528. :group 'org-progress
  1529. :type '(choice
  1530. (const :tag "No logging" nil)
  1531. (const :tag "Record CLOSED timestamp" time)
  1532. (const :tag "Record CLOSED timestamp with closing note." note)))
  1533. ;; Normalize old uses of org-log-done.
  1534. (cond
  1535. ((eq org-log-done t) (setq org-log-done 'time))
  1536. ((and (listp org-log-done) (memq 'done org-log-done))
  1537. (setq org-log-done 'note)))
  1538. (defcustom org-log-note-clock-out nil
  1539. "Non-nil means, record a note when clocking out of an item.
  1540. This can also be configured on a per-file basis by adding one of
  1541. the following lines anywhere in the buffer:
  1542. #+STARTUP: lognoteclock-out
  1543. #+STARTUP: nolognoteclock-out"
  1544. :group 'org-todo
  1545. :group 'org-progress
  1546. :type 'boolean)
  1547. (defcustom org-log-done-with-time t
  1548. "Non-nil means, the CLOSED time stamp will contain date and time.
  1549. When nil, only the date will be recorded."
  1550. :group 'org-progress
  1551. :type 'boolean)
  1552. (defcustom org-log-note-headings
  1553. '((done . "CLOSING NOTE %t")
  1554. (state . "State %-12s from %-12S %t")
  1555. (note . "Note taken on %t")
  1556. (clock-out . ""))
  1557. "Headings for notes added to entries.
  1558. The value is an alist, with the car being a symbol indicating the note
  1559. context, and the cdr is the heading to be used. The heading may also be the
  1560. empty string.
  1561. %t in the heading will be replaced by a time stamp.
  1562. %s will be replaced by the new TODO state, in double quotes.
  1563. %S will be replaced by the old TODO state, in double quotes.
  1564. %u will be replaced by the user name.
  1565. %U will be replaced by the full user name."
  1566. :group 'org-todo
  1567. :group 'org-progress
  1568. :type '(list :greedy t
  1569. (cons (const :tag "Heading when closing an item" done) string)
  1570. (cons (const :tag
  1571. "Heading when changing todo state (todo sequence only)"
  1572. state) string)
  1573. (cons (const :tag "Heading when just taking a note" note) string)
  1574. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1575. (unless (assq 'note org-log-note-headings)
  1576. (push '(note . "%t") org-log-note-headings))
  1577. (defcustom org-log-into-drawer nil
  1578. "Non-nil means, insert state change notes and time stamps into a drawer.
  1579. When nil, state changes notes will be inserted after the headline and
  1580. any scheduling and clock lines, but not inside a drawer.
  1581. The value of this variable should be the name of the drawer to use.
  1582. LOGBOOK is proposed at the default drawer for this purpose, you can
  1583. also set this to a string to define the drawer of your choice.
  1584. A value of t is also allowed, representing \"LOGBOOK\".
  1585. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1586. will be ognored."
  1587. :group 'org-todo
  1588. :group 'org-progress
  1589. :type '(choice
  1590. (const :tag "Not into a drawer" nil)
  1591. (const :tag "LOGBOOK" t)
  1592. (string :tag "Other")))
  1593. (if (fboundp 'defvaralias)
  1594. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1595. (defcustom org-log-state-notes-insert-after-drawers nil
  1596. "Non-nil means, insert state change notes after any drawers in entry.
  1597. Only the drawers that *immediately* follow the headline and the
  1598. deadline/scheduled line are skipped.
  1599. When nil, insert notes right after the heading and perhaps the line
  1600. with deadline/scheduling if present.
  1601. This variable will have no effect if `org-log-into-drawer' is
  1602. set."
  1603. :group 'org-todo
  1604. :group 'org-progress
  1605. :type 'boolean)
  1606. (defcustom org-log-states-order-reversed t
  1607. "Non-nil means, the latest state change note will be directly after heading.
  1608. When nil, the notes will be orderer according to time."
  1609. :group 'org-todo
  1610. :group 'org-progress
  1611. :type 'boolean)
  1612. (defcustom org-log-repeat 'time
  1613. "Non-nil means, record moving through the DONE state when triggering repeat.
  1614. An auto-repeating tasks is immediately switched back to TODO when marked
  1615. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1616. the TODO keyword definition, or recording a closing note by setting
  1617. `org-log-done', there will be no record of the task moving through DONE.
  1618. This variable forces taking a note anyway. Possible values are:
  1619. nil Don't force a record
  1620. time Record a time stamp
  1621. note Record a note
  1622. This option can also be set with on a per-file-basis with
  1623. #+STARTUP: logrepeat
  1624. #+STARTUP: lognoterepeat
  1625. #+STARTUP: nologrepeat
  1626. You can have local logging settings for a subtree by setting the LOGGING
  1627. property to one or more of these keywords."
  1628. :group 'org-todo
  1629. :group 'org-progress
  1630. :type '(choice
  1631. (const :tag "Don't force a record" nil)
  1632. (const :tag "Force recording the DONE state" time)
  1633. (const :tag "Force recording a note with the DONE state" note)))
  1634. (defgroup org-priorities nil
  1635. "Priorities in Org-mode."
  1636. :tag "Org Priorities"
  1637. :group 'org-todo)
  1638. (defcustom org-highest-priority ?A
  1639. "The highest priority of TODO items. A character like ?A, ?B etc.
  1640. Must have a smaller ASCII number than `org-lowest-priority'."
  1641. :group 'org-priorities
  1642. :type 'character)
  1643. (defcustom org-lowest-priority ?C
  1644. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1645. Must have a larger ASCII number than `org-highest-priority'."
  1646. :group 'org-priorities
  1647. :type 'character)
  1648. (defcustom org-default-priority ?B
  1649. "The default priority of TODO items.
  1650. This is the priority an item get if no explicit priority is given."
  1651. :group 'org-priorities
  1652. :type 'character)
  1653. (defcustom org-priority-start-cycle-with-default t
  1654. "Non-nil means, start with default priority when starting to cycle.
  1655. When this is nil, the first step in the cycle will be (depending on the
  1656. command used) one higher or lower that the default priority."
  1657. :group 'org-priorities
  1658. :type 'boolean)
  1659. (defgroup org-time nil
  1660. "Options concerning time stamps and deadlines in Org-mode."
  1661. :tag "Org Time"
  1662. :group 'org)
  1663. (defcustom org-insert-labeled-timestamps-at-point nil
  1664. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1665. When nil, these labeled time stamps are forces into the second line of an
  1666. entry, just after the headline. When scheduling from the global TODO list,
  1667. the time stamp will always be forced into the second line."
  1668. :group 'org-time
  1669. :type 'boolean)
  1670. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1671. "Formats for `format-time-string' which are used for time stamps.
  1672. It is not recommended to change this constant.")
  1673. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1674. "Number of minutes to round time stamps to.
  1675. These are two values, the first applies when first creating a time stamp.
  1676. The second applies when changing it with the commands `S-up' and `S-down'.
  1677. When changing the time stamp, this means that it will change in steps
  1678. of N minutes, as given by the second value.
  1679. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1680. numbers should be factors of 60, so for example 5, 10, 15.
  1681. When this is larger than 1, you can still force an exact time-stamp by using
  1682. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1683. and by using a prefix arg to `S-up/down' to specify the exact number
  1684. of minutes to shift."
  1685. :group 'org-time
  1686. :get '(lambda (var) ; Make sure all entries have 5 elements
  1687. (if (integerp (default-value var))
  1688. (list (default-value var) 5)
  1689. (default-value var)))
  1690. :type '(list
  1691. (integer :tag "when inserting times")
  1692. (integer :tag "when modifying times")))
  1693. ;; Normalize old customizations of this variable.
  1694. (when (integerp org-time-stamp-rounding-minutes)
  1695. (setq org-time-stamp-rounding-minutes
  1696. (list org-time-stamp-rounding-minutes
  1697. org-time-stamp-rounding-minutes)))
  1698. (defcustom org-display-custom-times nil
  1699. "Non-nil means, overlay custom formats over all time stamps.
  1700. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1701. To turn this on on a per-file basis, insert anywhere in the file:
  1702. #+STARTUP: customtime"
  1703. :group 'org-time
  1704. :set 'set-default
  1705. :type 'sexp)
  1706. (make-variable-buffer-local 'org-display-custom-times)
  1707. (defcustom org-time-stamp-custom-formats
  1708. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1709. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1710. These are overlayed over the default ISO format if the variable
  1711. `org-display-custom-times' is set. Time like %H:%M should be at the
  1712. end of the second format."
  1713. :group 'org-time
  1714. :type 'sexp)
  1715. (defun org-time-stamp-format (&optional long inactive)
  1716. "Get the right format for a time string."
  1717. (let ((f (if long (cdr org-time-stamp-formats)
  1718. (car org-time-stamp-formats))))
  1719. (if inactive
  1720. (concat "[" (substring f 1 -1) "]")
  1721. f)))
  1722. (defcustom org-time-clocksum-format "%d:%02d"
  1723. "The format string used when creating CLOCKSUM lines, or when
  1724. org-mode generates a time duration."
  1725. :group 'org-time
  1726. :type 'string)
  1727. (defcustom org-deadline-warning-days 14
  1728. "No. of days before expiration during which a deadline becomes active.
  1729. This variable governs the display in sparse trees and in the agenda.
  1730. When 0 or negative, it means use this number (the absolute value of it)
  1731. even if a deadline has a different individual lead time specified."
  1732. :group 'org-time
  1733. :group 'org-agenda-daily/weekly
  1734. :type 'number)
  1735. (defcustom org-read-date-prefer-future t
  1736. "Non-nil means, assume future for incomplete date input from user.
  1737. This affects the following situations:
  1738. 1. The user gives a day, but no month.
  1739. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1740. read this as the third of *next* month. However, if you enter \"17\",
  1741. it will be considered as *this* month.
  1742. 2. The user gives a month but not a year.
  1743. For example, if it is april and you enter \"feb 2\", this will be read
  1744. as feb 2, *next* year. \"May 5\", however, will be this year.
  1745. Currently this does not work for ISO week specifications.
  1746. When this option is nil, the current month and year will always be used
  1747. as defaults."
  1748. :group 'org-time
  1749. :type 'boolean)
  1750. (defcustom org-read-date-display-live t
  1751. "Non-nil means, display current interpretation of date prompt live.
  1752. This display will be in an overlay, in the minibuffer."
  1753. :group 'org-time
  1754. :type 'boolean)
  1755. (defcustom org-read-date-popup-calendar t
  1756. "Non-nil means, pop up a calendar when prompting for a date.
  1757. In the calendar, the date can be selected with mouse-1. However, the
  1758. minibuffer will also be active, and you can simply enter the date as well.
  1759. When nil, only the minibuffer will be available."
  1760. :group 'org-time
  1761. :type 'boolean)
  1762. (if (fboundp 'defvaralias)
  1763. (defvaralias 'org-popup-calendar-for-date-prompt
  1764. 'org-read-date-popup-calendar))
  1765. (defcustom org-extend-today-until 0
  1766. "The hour when your day really ends. Must be an integer.
  1767. This has influence for the following applications:
  1768. - When switching the agenda to \"today\". It it is still earlier than
  1769. the time given here, the day recognized as TODAY is actually yesterday.
  1770. - When a date is read from the user and it is still before the time given
  1771. here, the current date and time will be assumed to be yesterday, 23:59.
  1772. Also, timestamps inserted in remember templates follow this rule.
  1773. IMPORTANT: This is a feature whose implementation is and likely will
  1774. remain incomplete. Really, it is only here because past midnight seems to
  1775. be the favorite working time of John Wiegley :-)"
  1776. :group 'org-time
  1777. :type 'number)
  1778. (defcustom org-edit-timestamp-down-means-later nil
  1779. "Non-nil means, S-down will increase the time in a time stamp.
  1780. When nil, S-up will increase."
  1781. :group 'org-time
  1782. :type 'boolean)
  1783. (defcustom org-calendar-follow-timestamp-change t
  1784. "Non-nil means, make the calendar window follow timestamp changes.
  1785. When a timestamp is modified and the calendar window is visible, it will be
  1786. moved to the new date."
  1787. :group 'org-time
  1788. :type 'boolean)
  1789. (defgroup org-tags nil
  1790. "Options concerning tags in Org-mode."
  1791. :tag "Org Tags"
  1792. :group 'org)
  1793. (defcustom org-tag-alist nil
  1794. "List of tags allowed in Org-mode files.
  1795. When this list is nil, Org-mode will base TAG input on what is already in the
  1796. buffer.
  1797. The value of this variable is an alist, the car of each entry must be a
  1798. keyword as a string, the cdr may be a character that is used to select
  1799. that tag through the fast-tag-selection interface.
  1800. See the manual for details."
  1801. :group 'org-tags
  1802. :type '(repeat
  1803. (choice
  1804. (cons (string :tag "Tag name")
  1805. (character :tag "Access char"))
  1806. (const :tag "Start radio group" (:startgroup))
  1807. (const :tag "End radio group" (:endgroup)))))
  1808. (defvar org-file-tags nil
  1809. "List of tags that can be inherited by all entries in the file.
  1810. The tags will be inherited if the variable `org-use-tag-inheritance'
  1811. says they should be.
  1812. This variable is populated from #+TAG lines.")
  1813. (defcustom org-use-fast-tag-selection 'auto
  1814. "Non-nil means, use fast tag selection scheme.
  1815. This is a special interface to select and deselect tags with single keys.
  1816. When nil, fast selection is never used.
  1817. When the symbol `auto', fast selection is used if and only if selection
  1818. characters for tags have been configured, either through the variable
  1819. `org-tag-alist' or through a #+TAGS line in the buffer.
  1820. When t, fast selection is always used and selection keys are assigned
  1821. automatically if necessary."
  1822. :group 'org-tags
  1823. :type '(choice
  1824. (const :tag "Always" t)
  1825. (const :tag "Never" nil)
  1826. (const :tag "When selection characters are configured" 'auto)))
  1827. (defcustom org-fast-tag-selection-single-key nil
  1828. "Non-nil means, fast tag selection exits after first change.
  1829. When nil, you have to press RET to exit it.
  1830. During fast tag selection, you can toggle this flag with `C-c'.
  1831. This variable can also have the value `expert'. In this case, the window
  1832. displaying the tags menu is not even shown, until you press C-c again."
  1833. :group 'org-tags
  1834. :type '(choice
  1835. (const :tag "No" nil)
  1836. (const :tag "Yes" t)
  1837. (const :tag "Expert" expert)))
  1838. (defvar org-fast-tag-selection-include-todo nil
  1839. "Non-nil means, fast tags selection interface will also offer TODO states.
  1840. This is an undocumented feature, you should not rely on it.")
  1841. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1842. "The column to which tags should be indented in a headline.
  1843. If this number is positive, it specifies the column. If it is negative,
  1844. it means that the tags should be flushright to that column. For example,
  1845. -80 works well for a normal 80 character screen."
  1846. :group 'org-tags
  1847. :type 'integer)
  1848. (defcustom org-auto-align-tags t
  1849. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1850. These operations change the length of a headline and therefore shift
  1851. the tags around. With this options turned on, after each such operation
  1852. the tags are again aligned to `org-tags-column'."
  1853. :group 'org-tags
  1854. :type 'boolean)
  1855. (defcustom org-use-tag-inheritance t
  1856. "Non-nil means, tags in levels apply also for sublevels.
  1857. When nil, only the tags directly given in a specific line apply there.
  1858. This may also be a list of tags that should be inherited, or a regexp that
  1859. matches tags that should be inherited. Additional control is possible
  1860. with the variable `org-tags-exclude-from-inheritance' which gives an
  1861. explicit list of tags to be excluded from inheritance., even if the value of
  1862. `org-use-tag-inheritance' would select it for inheritance.
  1863. If this option is t, a match early-on in a tree can lead to a large
  1864. number of matches in the subtree when constructing the agenda or creating
  1865. a sparse tree. If you only want to see the first match in a tree during
  1866. a search, check out the variable `org-tags-match-list-sublevels'."
  1867. :group 'org-tags
  1868. :type '(choice
  1869. (const :tag "Not" nil)
  1870. (const :tag "Always" t)
  1871. (repeat :tag "Specific tags" (string :tag "Tag"))
  1872. (regexp :tag "Tags matched by regexp")))
  1873. (defcustom org-tags-exclude-from-inheritance nil
  1874. "List of tags that should never be inherited.
  1875. This is a way to exclude a few tags from inheritance. For way to do
  1876. the opposite, to actively allow inheritance for selected tags,
  1877. see the variable `org-use-tag-inheritance'."
  1878. :group 'org-tags
  1879. :type '(repeat (string :tag "Tag")))
  1880. (defun org-tag-inherit-p (tag)
  1881. "Check if TAG is one that should be inherited."
  1882. (cond
  1883. ((member tag org-tags-exclude-from-inheritance) nil)
  1884. ((eq org-use-tag-inheritance t) t)
  1885. ((not org-use-tag-inheritance) nil)
  1886. ((stringp org-use-tag-inheritance)
  1887. (string-match org-use-tag-inheritance tag))
  1888. ((listp org-use-tag-inheritance)
  1889. (member tag org-use-tag-inheritance))
  1890. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1891. (defcustom org-tags-match-list-sublevels t
  1892. "Non-nil means list also sublevels of headlines matching tag search.
  1893. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1894. the sublevels of a headline matching a tag search often also match
  1895. the same search. Listing all of them can create very long lists.
  1896. Setting this variable to nil causes subtrees of a match to be skipped.
  1897. This option is off by default, because inheritance in on. If you turn
  1898. inheritance off, you very likely want to turn this option on.
  1899. As a special case, if the tag search is restricted to TODO items, the
  1900. value of this variable is ignored and sublevels are always checked, to
  1901. make sure all corresponding TODO items find their way into the list.
  1902. This variable is semi-obsolete and probably should always be true. It
  1903. is better to limit inheritance to certain tags using the variables
  1904. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1905. :group 'org-tags
  1906. :type 'boolean)
  1907. (defvar org-tags-history nil
  1908. "History of minibuffer reads for tags.")
  1909. (defvar org-last-tags-completion-table nil
  1910. "The last used completion table for tags.")
  1911. (defvar org-after-tags-change-hook nil
  1912. "Hook that is run after the tags in a line have changed.")
  1913. (defgroup org-properties nil
  1914. "Options concerning properties in Org-mode."
  1915. :tag "Org Properties"
  1916. :group 'org)
  1917. (defcustom org-property-format "%-10s %s"
  1918. "How property key/value pairs should be formatted by `indent-line'.
  1919. When `indent-line' hits a property definition, it will format the line
  1920. according to this format, mainly to make sure that the values are
  1921. lined-up with respect to each other."
  1922. :group 'org-properties
  1923. :type 'string)
  1924. (defcustom org-use-property-inheritance nil
  1925. "Non-nil means, properties apply also for sublevels.
  1926. This setting is chiefly used during property searches. Turning it on can
  1927. cause significant overhead when doing a search, which is why it is not
  1928. on by default.
  1929. When nil, only the properties directly given in the current entry count.
  1930. When t, every property is inherited. The value may also be a list of
  1931. properties that should have inheritance, or a regular expression matching
  1932. properties that should be inherited.
  1933. However, note that some special properties use inheritance under special
  1934. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1935. and the properties ending in \"_ALL\" when they are used as descriptor
  1936. for valid values of a property.
  1937. Note for programmers:
  1938. When querying an entry with `org-entry-get', you can control if inheritance
  1939. should be used. By default, `org-entry-get' looks only at the local
  1940. properties. You can request inheritance by setting the inherit argument
  1941. to t (to force inheritance) or to `selective' (to respect the setting
  1942. in this variable)."
  1943. :group 'org-properties
  1944. :type '(choice
  1945. (const :tag "Not" nil)
  1946. (const :tag "Always" t)
  1947. (repeat :tag "Specific properties" (string :tag "Property"))
  1948. (regexp :tag "Properties matched by regexp")))
  1949. (defun org-property-inherit-p (property)
  1950. "Check if PROPERTY is one that should be inherited."
  1951. (cond
  1952. ((eq org-use-property-inheritance t) t)
  1953. ((not org-use-property-inheritance) nil)
  1954. ((stringp org-use-property-inheritance)
  1955. (string-match org-use-property-inheritance property))
  1956. ((listp org-use-property-inheritance)
  1957. (member property org-use-property-inheritance))
  1958. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1959. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1960. "The default column format, if no other format has been defined.
  1961. This variable can be set on the per-file basis by inserting a line
  1962. #+COLUMNS: %25ITEM ....."
  1963. :group 'org-properties
  1964. :type 'string)
  1965. (defcustom org-columns-ellipses ".."
  1966. "The ellipses to be used when a field in column view is truncated.
  1967. When this is the empty string, as many characters as possible are shown,
  1968. but then there will be no visual indication that the field has been truncated.
  1969. When this is a string of length N, the last N characters of a truncated
  1970. field are replaced by this string. If the column is narrower than the
  1971. ellipses string, only part of the ellipses string will be shown."
  1972. :group 'org-properties
  1973. :type 'string)
  1974. (defcustom org-columns-modify-value-for-display-function nil
  1975. "Function that modifies values for display in column view.
  1976. For example, it can be used to cut out a certain part from a time stamp.
  1977. The function must take 2 arguments:
  1978. column-title The title of the column (*not* the property name)
  1979. value The value that should be modified.
  1980. The function should return the value that should be displayed,
  1981. or nil if the normal value should be used."
  1982. :group 'org-properties
  1983. :type 'function)
  1984. (defcustom org-effort-property "Effort"
  1985. "The property that is being used to keep track of effort estimates.
  1986. Effort estimates given in this property need to have the format H:MM."
  1987. :group 'org-properties
  1988. :group 'org-progress
  1989. :type '(string :tag "Property"))
  1990. (defconst org-global-properties-fixed
  1991. '(("VISIBILITY_ALL" . "folded children content all"))
  1992. "List of property/value pairs that can be inherited by any entry.
  1993. These are fixed values, for the preset properties.")
  1994. (defcustom org-global-properties nil
  1995. "List of property/value pairs that can be inherited by any entry.
  1996. You can set buffer-local values for the same purpose in the variable
  1997. `org-file-properties' this by adding lines like
  1998. #+PROPERTY: NAME VALUE"
  1999. :group 'org-properties
  2000. :type '(repeat
  2001. (cons (string :tag "Property")
  2002. (string :tag "Value"))))
  2003. (defvar org-file-properties nil
  2004. "List of property/value pairs that can be inherited by any entry.
  2005. Valid for the current buffer.
  2006. This variable is populated from #+PROPERTY lines.")
  2007. (make-variable-buffer-local 'org-file-properties)
  2008. (defgroup org-agenda nil
  2009. "Options concerning agenda views in Org-mode."
  2010. :tag "Org Agenda"
  2011. :group 'org)
  2012. (defvar org-category nil
  2013. "Variable used by org files to set a category for agenda display.
  2014. Such files should use a file variable to set it, for example
  2015. # -*- mode: org; org-category: \"ELisp\"
  2016. or contain a special line
  2017. #+CATEGORY: ELisp
  2018. If the file does not specify a category, then file's base name
  2019. is used instead.")
  2020. (make-variable-buffer-local 'org-category)
  2021. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2022. (defcustom org-agenda-files nil
  2023. "The files to be used for agenda display.
  2024. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2025. \\[org-remove-file]. You can also use customize to edit the list.
  2026. If an entry is a directory, all files in that directory that are matched by
  2027. `org-agenda-file-regexp' will be part of the file list.
  2028. If the value of the variable is not a list but a single file name, then
  2029. the list of agenda files is actually stored and maintained in that file, one
  2030. agenda file per line."
  2031. :group 'org-agenda
  2032. :type '(choice
  2033. (repeat :tag "List of files and directories" file)
  2034. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2035. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2036. "Regular expression to match files for `org-agenda-files'.
  2037. If any element in the list in that variable contains a directory instead
  2038. of a normal file, all files in that directory that are matched by this
  2039. regular expression will be included."
  2040. :group 'org-agenda
  2041. :type 'regexp)
  2042. (defcustom org-agenda-text-search-extra-files nil
  2043. "List of extra files to be searched by text search commands.
  2044. These files will be search in addition to the agenda files by the
  2045. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2046. Note that these files will only be searched for text search commands,
  2047. not for the other agenda views like todo lists, tag searches or the weekly
  2048. agenda. This variable is intended to list notes and possibly archive files
  2049. that should also be searched by these two commands.
  2050. In fact, if the first element in the list is the symbol `agenda-archives',
  2051. than all archive files of all agenda files will be added to the search
  2052. scope."
  2053. :group 'org-agenda
  2054. :type '(set :greedy t
  2055. (const :tag "Agenda Archives" agenda-archives)
  2056. (repeat :inline t (file))))
  2057. (if (fboundp 'defvaralias)
  2058. (defvaralias 'org-agenda-multi-occur-extra-files
  2059. 'org-agenda-text-search-extra-files))
  2060. (defcustom org-agenda-skip-unavailable-files nil
  2061. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2062. A nil value means to remove them, after a query, from the list."
  2063. :group 'org-agenda
  2064. :type 'boolean)
  2065. (defcustom org-calendar-to-agenda-key [?c]
  2066. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2067. The command `org-calendar-goto-agenda' will be bound to this key. The
  2068. default is the character `c' because then `c' can be used to switch back and
  2069. forth between agenda and calendar."
  2070. :group 'org-agenda
  2071. :type 'sexp)
  2072. (defcustom org-calendar-agenda-action-key [?k]
  2073. "The key to be installed in `calendar-mode-map' for agenda-action.
  2074. The command `org-agenda-action' will be bound to this key. The
  2075. default is the character `k' because we use the same key in the agenda."
  2076. :group 'org-agenda
  2077. :type 'sexp)
  2078. (eval-after-load "calendar"
  2079. '(progn
  2080. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2081. 'org-calendar-goto-agenda)
  2082. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2083. 'org-agenda-action)))
  2084. (defgroup org-latex nil
  2085. "Options for embedding LaTeX code into Org-mode."
  2086. :tag "Org LaTeX"
  2087. :group 'org)
  2088. (defcustom org-format-latex-options
  2089. '(:foreground default :background default :scale 1.0
  2090. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2091. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2092. "Options for creating images from LaTeX fragments.
  2093. This is a property list with the following properties:
  2094. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2095. `default' means use the foreground of the default face.
  2096. :background the background color, or \"Transparent\".
  2097. `default' means use the background of the default face.
  2098. :scale a scaling factor for the size of the images.
  2099. :html-foreground, :html-background, :html-scale
  2100. the same numbers for HTML export.
  2101. :matchers a list indicating which matchers should be used to
  2102. find LaTeX fragments. Valid members of this list are:
  2103. \"begin\" find environments
  2104. \"$1\" find single characters surrounded by $.$
  2105. \"$\" find math expressions surrounded by $...$
  2106. \"$$\" find math expressions surrounded by $$....$$
  2107. \"\\(\" find math expressions surrounded by \\(...\\)
  2108. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2109. :group 'org-latex
  2110. :type 'plist)
  2111. (defcustom org-format-latex-header "\\documentclass{article}
  2112. \\usepackage{fullpage} % do not remove
  2113. \\usepackage{amssymb}
  2114. \\usepackage[usenames]{color}
  2115. \\usepackage{amsmath}
  2116. \\usepackage{latexsym}
  2117. \\usepackage[mathscr]{eucal}
  2118. \\pagestyle{empty} % do not remove"
  2119. "The document header used for processing LaTeX fragments."
  2120. :group 'org-latex
  2121. :type 'string)
  2122. (defgroup org-font-lock nil
  2123. "Font-lock settings for highlighting in Org-mode."
  2124. :tag "Org Font Lock"
  2125. :group 'org)
  2126. (defcustom org-level-color-stars-only nil
  2127. "Non-nil means fontify only the stars in each headline.
  2128. When nil, the entire headline is fontified.
  2129. Changing it requires restart of `font-lock-mode' to become effective
  2130. also in regions already fontified."
  2131. :group 'org-font-lock
  2132. :type 'boolean)
  2133. (defcustom org-hide-leading-stars nil
  2134. "Non-nil means, hide the first N-1 stars in a headline.
  2135. This works by using the face `org-hide' for these stars. This
  2136. face is white for a light background, and black for a dark
  2137. background. You may have to customize the face `org-hide' to
  2138. make this work.
  2139. Changing it requires restart of `font-lock-mode' to become effective
  2140. also in regions already fontified.
  2141. You may also set this on a per-file basis by adding one of the following
  2142. lines to the buffer:
  2143. #+STARTUP: hidestars
  2144. #+STARTUP: showstars"
  2145. :group 'org-font-lock
  2146. :type 'boolean)
  2147. (defcustom org-fontify-done-headline nil
  2148. "Non-nil means, change the face of a headline if it is marked DONE.
  2149. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2150. When this is non-nil, the headline after the keyword is set to the
  2151. `org-headline-done' as an additional indication."
  2152. :group 'org-font-lock
  2153. :type 'boolean)
  2154. (defcustom org-fontify-emphasized-text t
  2155. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2156. Changing this variable requires a restart of Emacs to take effect."
  2157. :group 'org-font-lock
  2158. :type 'boolean)
  2159. (defcustom org-highlight-latex-fragments-and-specials nil
  2160. "Non-nil means, fontify what is treated specially by the exporters."
  2161. :group 'org-font-lock
  2162. :type 'boolean)
  2163. (defcustom org-hide-emphasis-markers nil
  2164. "Non-nil mean font-lock should hide the emphasis marker characters."
  2165. :group 'org-font-lock
  2166. :type 'boolean)
  2167. (defvar org-emph-re nil
  2168. "Regular expression for matching emphasis.")
  2169. (defvar org-verbatim-re nil
  2170. "Regular expression for matching verbatim text.")
  2171. (defvar org-emphasis-regexp-components) ; defined just below
  2172. (defvar org-emphasis-alist) ; defined just below
  2173. (defun org-set-emph-re (var val)
  2174. "Set variable and compute the emphasis regular expression."
  2175. (set var val)
  2176. (when (and (boundp 'org-emphasis-alist)
  2177. (boundp 'org-emphasis-regexp-components)
  2178. org-emphasis-alist org-emphasis-regexp-components)
  2179. (let* ((e org-emphasis-regexp-components)
  2180. (pre (car e))
  2181. (post (nth 1 e))
  2182. (border (nth 2 e))
  2183. (body (nth 3 e))
  2184. (nl (nth 4 e))
  2185. (body1 (concat body "*?"))
  2186. (markers (mapconcat 'car org-emphasis-alist ""))
  2187. (vmarkers (mapconcat
  2188. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2189. org-emphasis-alist "")))
  2190. ;; make sure special characters appear at the right position in the class
  2191. (if (string-match "\\^" markers)
  2192. (setq markers (concat (replace-match "" t t markers) "^")))
  2193. (if (string-match "-" markers)
  2194. (setq markers (concat (replace-match "" t t markers) "-")))
  2195. (if (string-match "\\^" vmarkers)
  2196. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2197. (if (string-match "-" vmarkers)
  2198. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2199. (if (> nl 0)
  2200. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2201. (int-to-string nl) "\\}")))
  2202. ;; Make the regexp
  2203. (setq org-emph-re
  2204. (concat "\\([" pre "]\\|^\\)"
  2205. "\\("
  2206. "\\([" markers "]\\)"
  2207. "\\("
  2208. "[^" border "]\\|"
  2209. "[^" border "]"
  2210. body1
  2211. "[^" border "]"
  2212. "\\)"
  2213. "\\3\\)"
  2214. "\\([" post "]\\|$\\)"))
  2215. (setq org-verbatim-re
  2216. (concat "\\([" pre "]\\|^\\)"
  2217. "\\("
  2218. "\\([" vmarkers "]\\)"
  2219. "\\("
  2220. "[^" border "]\\|"
  2221. "[^" border "]"
  2222. body1
  2223. "[^" border "]"
  2224. "\\)"
  2225. "\\3\\)"
  2226. "\\([" post "]\\|$\\)")))))
  2227. (defcustom org-emphasis-regexp-components
  2228. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2229. "Components used to build the regular expression for emphasis.
  2230. This is a list with 6 entries. Terminology: In an emphasis string
  2231. like \" *strong word* \", we call the initial space PREMATCH, the final
  2232. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2233. and \"trong wor\" is the body. The different components in this variable
  2234. specify what is allowed/forbidden in each part:
  2235. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2236. post Chars allowed as postmatch. End of line will be allowed too.
  2237. border The chars *forbidden* as border characters.
  2238. body-regexp A regexp like \".\" to match a body character. Don't use
  2239. non-shy groups here, and don't allow newline here.
  2240. newline The maximum number of newlines allowed in an emphasis exp.
  2241. Use customize to modify this, or restart Emacs after changing it."
  2242. :group 'org-font-lock
  2243. :set 'org-set-emph-re
  2244. :type '(list
  2245. (sexp :tag "Allowed chars in pre ")
  2246. (sexp :tag "Allowed chars in post ")
  2247. (sexp :tag "Forbidden chars in border ")
  2248. (sexp :tag "Regexp for body ")
  2249. (integer :tag "number of newlines allowed")
  2250. (option (boolean :tag "Please ignore this button"))))
  2251. (defcustom org-emphasis-alist
  2252. `(("*" bold "<b>" "</b>")
  2253. ("/" italic "<i>" "</i>")
  2254. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2255. ("=" org-code "<code>" "</code>" verbatim)
  2256. ("~" org-verbatim "<code>" "</code>" verbatim)
  2257. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2258. "<del>" "</del>")
  2259. )
  2260. "Special syntax for emphasized text.
  2261. Text starting and ending with a special character will be emphasized, for
  2262. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2263. characters, the face to be used by font-lock for highlighting in Org-mode
  2264. Emacs buffers, and the HTML tags to be used for this.
  2265. Use customize to modify this, or restart Emacs after changing it."
  2266. :group 'org-font-lock
  2267. :set 'org-set-emph-re
  2268. :type '(repeat
  2269. (list
  2270. (string :tag "Marker character")
  2271. (choice
  2272. (face :tag "Font-lock-face")
  2273. (plist :tag "Face property list"))
  2274. (string :tag "HTML start tag")
  2275. (string :tag "HTML end tag")
  2276. (option (const verbatim)))))
  2277. ;;; Miscellaneous options
  2278. (defgroup org-completion nil
  2279. "Completion in Org-mode."
  2280. :tag "Org Completion"
  2281. :group 'org)
  2282. (defcustom org-completion-use-ido nil
  2283. "Non-nil means, use ido completion wherever possible.
  2284. Note that `ido-mode' must be active for this variable to be relevant.
  2285. If you decide to turn this variable on, you might well want to turn off
  2286. `org-outline-path-complete-in-steps'."
  2287. :group 'org-completion
  2288. :type 'boolean)
  2289. (defcustom org-completion-fallback-command 'hippie-expand
  2290. "The expansion command called by \\[org-complete] in normal context.
  2291. Normal means, no org-mode-specific context."
  2292. :group 'org-completion
  2293. :type 'function)
  2294. ;;; Functions and variables from ther packages
  2295. ;; Declared here to avoid compiler warnings
  2296. ;; XEmacs only
  2297. (defvar outline-mode-menu-heading)
  2298. (defvar outline-mode-menu-show)
  2299. (defvar outline-mode-menu-hide)
  2300. (defvar zmacs-regions) ; XEmacs regions
  2301. ;; Emacs only
  2302. (defvar mark-active)
  2303. ;; Various packages
  2304. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2305. (declare-function calendar-forward-day "cal-move" (arg))
  2306. (declare-function calendar-goto-date "cal-move" (date))
  2307. (declare-function calendar-goto-today "cal-move" ())
  2308. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2309. (defvar calc-embedded-close-formula)
  2310. (defvar calc-embedded-open-formula)
  2311. (declare-function cdlatex-tab "ext:cdlatex" ())
  2312. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2313. (defvar font-lock-unfontify-region-function)
  2314. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2315. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2316. (defvar iswitchb-temp-buflist)
  2317. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2318. (defvar org-agenda-tags-todo-honor-ignore-options)
  2319. (declare-function org-agenda-skip "org-agenda" ())
  2320. (declare-function org-format-agenda-item "org-agenda"
  2321. (extra txt &optional category tags dotime noprefix remove-re))
  2322. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2323. (declare-function org-agenda-change-all-lines "org-agenda"
  2324. (newhead hdmarker &optional fixface just-this))
  2325. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2326. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2327. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2328. (beg end))
  2329. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2330. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2331. "org-agenda" (&optional end))
  2332. (declare-function parse-time-string "parse-time" (string))
  2333. (declare-function remember "remember" (&optional initial))
  2334. (declare-function remember-buffer-desc "remember" ())
  2335. (declare-function remember-finalize "remember" ())
  2336. (defvar remember-save-after-remembering)
  2337. (defvar remember-data-file)
  2338. (defvar remember-register)
  2339. (defvar remember-buffer)
  2340. (defvar remember-handler-functions)
  2341. (defvar remember-annotation-functions)
  2342. (defvar texmathp-why)
  2343. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2344. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2345. (defvar w3m-current-url)
  2346. (defvar w3m-current-title)
  2347. (defvar org-latex-regexps)
  2348. ;;; Autoload and prepare some org modules
  2349. ;; Some table stuff that needs to be defined here, because it is used
  2350. ;; by the functions setting up org-mode or checking for table context.
  2351. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2352. "Detects an org-type or table-type table.")
  2353. (defconst org-table-line-regexp "^[ \t]*|"
  2354. "Detects an org-type table line.")
  2355. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2356. "Detects an org-type table line.")
  2357. (defconst org-table-hline-regexp "^[ \t]*|-"
  2358. "Detects an org-type table hline.")
  2359. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2360. "Detects a table-type table hline.")
  2361. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2362. "Searching from within a table (any type) this finds the first line
  2363. outside the table.")
  2364. ;; Autoload the functions in org-table.el that are needed by functions here.
  2365. (eval-and-compile
  2366. (org-autoload "org-table"
  2367. '(org-table-align org-table-begin org-table-blank-field
  2368. org-table-convert org-table-convert-region org-table-copy-down
  2369. org-table-copy-region org-table-create
  2370. org-table-create-or-convert-from-region
  2371. org-table-create-with-table.el org-table-current-dline
  2372. org-table-cut-region org-table-delete-column org-table-edit-field
  2373. org-table-edit-formulas org-table-end org-table-eval-formula
  2374. org-table-export org-table-field-info
  2375. org-table-get-stored-formulas org-table-goto-column
  2376. org-table-hline-and-move org-table-import org-table-insert-column
  2377. org-table-insert-hline org-table-insert-row org-table-iterate
  2378. org-table-justify-field-maybe org-table-kill-row
  2379. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2380. org-table-move-column org-table-move-column-left
  2381. org-table-move-column-right org-table-move-row
  2382. org-table-move-row-down org-table-move-row-up
  2383. org-table-next-field org-table-next-row org-table-paste-rectangle
  2384. org-table-previous-field org-table-recalculate
  2385. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2386. org-table-toggle-coordinate-overlays
  2387. org-table-toggle-formula-debugger org-table-wrap-region
  2388. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2389. (defun org-at-table-p (&optional table-type)
  2390. "Return t if the cursor is inside an org-type table.
  2391. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2392. (if org-enable-table-editor
  2393. (save-excursion
  2394. (beginning-of-line 1)
  2395. (looking-at (if table-type org-table-any-line-regexp
  2396. org-table-line-regexp)))
  2397. nil))
  2398. (defsubst org-table-p () (org-at-table-p))
  2399. (defun org-at-table.el-p ()
  2400. "Return t if and only if we are at a table.el table."
  2401. (and (org-at-table-p 'any)
  2402. (save-excursion
  2403. (goto-char (org-table-begin 'any))
  2404. (looking-at org-table1-hline-regexp))))
  2405. (defun org-table-recognize-table.el ()
  2406. "If there is a table.el table nearby, recognize it and move into it."
  2407. (if org-table-tab-recognizes-table.el
  2408. (if (org-at-table.el-p)
  2409. (progn
  2410. (beginning-of-line 1)
  2411. (if (looking-at org-table-dataline-regexp)
  2412. nil
  2413. (if (looking-at org-table1-hline-regexp)
  2414. (progn
  2415. (beginning-of-line 2)
  2416. (if (looking-at org-table-any-border-regexp)
  2417. (beginning-of-line -1)))))
  2418. (if (re-search-forward "|" (org-table-end t) t)
  2419. (progn
  2420. (require 'table)
  2421. (if (table--at-cell-p (point))
  2422. t
  2423. (message "recognizing table.el table...")
  2424. (table-recognize-table)
  2425. (message "recognizing table.el table...done")))
  2426. (error "This should not happen..."))
  2427. t)
  2428. nil)
  2429. nil))
  2430. (defun org-at-table-hline-p ()
  2431. "Return t if the cursor is inside a hline in a table."
  2432. (if org-enable-table-editor
  2433. (save-excursion
  2434. (beginning-of-line 1)
  2435. (looking-at org-table-hline-regexp))
  2436. nil))
  2437. (defvar org-table-clean-did-remove-column nil)
  2438. (defun org-table-map-tables (function)
  2439. "Apply FUNCTION to the start of all tables in the buffer."
  2440. (save-excursion
  2441. (save-restriction
  2442. (widen)
  2443. (goto-char (point-min))
  2444. (while (re-search-forward org-table-any-line-regexp nil t)
  2445. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2446. (beginning-of-line 1)
  2447. (if (looking-at org-table-line-regexp)
  2448. (save-excursion (funcall function)))
  2449. (re-search-forward org-table-any-border-regexp nil 1))))
  2450. (message "Mapping tables: done"))
  2451. ;; Declare and autoload functions from org-exp.el
  2452. (declare-function org-default-export-plist "org-exp")
  2453. (declare-function org-infile-export-plist "org-exp")
  2454. (declare-function org-get-current-options "org-exp")
  2455. (eval-and-compile
  2456. (org-autoload "org-exp"
  2457. '(org-export org-export-as-ascii org-export-visible
  2458. org-insert-export-options-template org-export-as-html-and-open
  2459. org-export-as-html-batch org-export-as-html-to-buffer
  2460. org-replace-region-by-html org-export-region-as-html
  2461. org-export-as-html org-export-icalendar-this-file
  2462. org-export-icalendar-all-agenda-files
  2463. org-table-clean-before-export
  2464. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2465. ;; Declare and autoload functions from org-agenda.el
  2466. (eval-and-compile
  2467. (org-autoload "org-agenda"
  2468. '(org-agenda org-agenda-list org-search-view
  2469. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2470. org-diary org-agenda-to-appt
  2471. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2472. ;; Autoload org-remember
  2473. (eval-and-compile
  2474. (org-autoload "org-remember"
  2475. '(org-remember-insinuate org-remember-annotation
  2476. org-remember-apply-template org-remember org-remember-handler)))
  2477. ;; Autoload org-clock.el
  2478. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2479. (beg end))
  2480. (declare-function org-clock-update-mode-line "org-clock" ())
  2481. (defvar org-clock-start-time)
  2482. (defvar org-clock-marker (make-marker)
  2483. "Marker recording the last clock-in.")
  2484. (eval-and-compile
  2485. (org-autoload
  2486. "org-clock"
  2487. '(org-clock-in org-clock-out org-clock-cancel
  2488. org-clock-goto org-clock-sum org-clock-display
  2489. org-clock-remove-overlays org-clock-report
  2490. org-clocktable-shift org-dblock-write:clocktable
  2491. org-get-clocktable)))
  2492. (defun org-clock-update-time-maybe ()
  2493. "If this is a CLOCK line, update it and return t.
  2494. Otherwise, return nil."
  2495. (interactive)
  2496. (save-excursion
  2497. (beginning-of-line 1)
  2498. (skip-chars-forward " \t")
  2499. (when (looking-at org-clock-string)
  2500. (let ((re (concat "[ \t]*" org-clock-string
  2501. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2502. "\\([ \t]*=>.*\\)?\\)?"))
  2503. ts te h m s neg)
  2504. (cond
  2505. ((not (looking-at re))
  2506. nil)
  2507. ((not (match-end 2))
  2508. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2509. (> org-clock-marker (point))
  2510. (<= org-clock-marker (point-at-eol)))
  2511. ;; The clock is running here
  2512. (setq org-clock-start-time
  2513. (apply 'encode-time
  2514. (org-parse-time-string (match-string 1))))
  2515. (org-clock-update-mode-line)))
  2516. (t
  2517. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2518. (end-of-line 1)
  2519. (setq ts (match-string 1)
  2520. te (match-string 3))
  2521. (setq s (- (time-to-seconds
  2522. (apply 'encode-time (org-parse-time-string te)))
  2523. (time-to-seconds
  2524. (apply 'encode-time (org-parse-time-string ts))))
  2525. neg (< s 0)
  2526. s (abs s)
  2527. h (floor (/ s 3600))
  2528. s (- s (* 3600 h))
  2529. m (floor (/ s 60))
  2530. s (- s (* 60 s)))
  2531. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2532. t))))))
  2533. (defun org-check-running-clock ()
  2534. "Check if the current buffer contains the running clock.
  2535. If yes, offer to stop it and to save the buffer with the changes."
  2536. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2537. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2538. (buffer-name))))
  2539. (org-clock-out)
  2540. (when (y-or-n-p "Save changed buffer?")
  2541. (save-buffer))))
  2542. (defun org-clocktable-try-shift (dir n)
  2543. "Check if this line starts a clock table, if yes, shift the time block."
  2544. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2545. (org-clocktable-shift dir n)))
  2546. ;; Autoload org-timer.el
  2547. ;(declare-function org-timer "org-timer")
  2548. (eval-and-compile
  2549. (org-autoload
  2550. "org-timer"
  2551. '(org-timer-start org-timer org-timer-item
  2552. org-timer-change-times-in-region)))
  2553. ;; Autoload archiving code
  2554. ;; The stuff that is needed for cycling and tags has to be defined here.
  2555. (defgroup org-archive nil
  2556. "Options concerning archiving in Org-mode."
  2557. :tag "Org Archive"
  2558. :group 'org-structure)
  2559. (defcustom org-archive-location "%s_archive::"
  2560. "The location where subtrees should be archived.
  2561. The value of this variable is a string, consisting of two parts,
  2562. separated by a double-colon. The first part is a filename and
  2563. the second part is a headline.
  2564. When the filename is omitted, archiving happens in the same file.
  2565. %s in the filename will be replaced by the current file
  2566. name (without the directory part). Archiving to a different file
  2567. is useful to keep archived entries from contributing to the
  2568. Org-mode Agenda.
  2569. The archived entries will be filed as subtrees of the specified
  2570. headline. When the headline is omitted, the subtrees are simply
  2571. filed away at the end of the file, as top-level entries. Also in
  2572. the heading you can use %s to represent the file name, this can be
  2573. useful when using the same archive for a number of different files.
  2574. Here are a few examples:
  2575. \"%s_archive::\"
  2576. If the current file is Projects.org, archive in file
  2577. Projects.org_archive, as top-level trees. This is the default.
  2578. \"::* Archived Tasks\"
  2579. Archive in the current file, under the top-level headline
  2580. \"* Archived Tasks\".
  2581. \"~/org/archive.org::\"
  2582. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2583. \"~/org/archive.org::From %s\"
  2584. Archive in file ~/org/archive.org (absolute path), und headlines
  2585. \"From FILENAME\" where file name is the current file name.
  2586. \"basement::** Finished Tasks\"
  2587. Archive in file ./basement (relative path), as level 3 trees
  2588. below the level 2 heading \"** Finished Tasks\".
  2589. You may set this option on a per-file basis by adding to the buffer a
  2590. line like
  2591. #+ARCHIVE: basement::** Finished Tasks
  2592. You may also define it locally for a subtree by setting an ARCHIVE property
  2593. in the entry. If such a property is found in an entry, or anywhere up
  2594. the hierarchy, it will be used."
  2595. :group 'org-archive
  2596. :type 'string)
  2597. (defcustom org-archive-tag "ARCHIVE"
  2598. "The tag that marks a subtree as archived.
  2599. An archived subtree does not open during visibility cycling, and does
  2600. not contribute to the agenda listings.
  2601. After changing this, font-lock must be restarted in the relevant buffers to
  2602. get the proper fontification."
  2603. :group 'org-archive
  2604. :group 'org-keywords
  2605. :type 'string)
  2606. (defcustom org-agenda-skip-archived-trees t
  2607. "Non-nil means, the agenda will skip any items located in archived trees.
  2608. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2609. variable is no longer recommended, you should leave it at the value t.
  2610. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2611. :group 'org-archive
  2612. :group 'org-agenda-skip
  2613. :type 'boolean)
  2614. (defcustom org-cycle-open-archived-trees nil
  2615. "Non-nil means, `org-cycle' will open archived trees.
  2616. An archived tree is a tree marked with the tag ARCHIVE.
  2617. When nil, archived trees will stay folded. You can still open them with
  2618. normal outline commands like `show-all', but not with the cycling commands."
  2619. :group 'org-archive
  2620. :group 'org-cycle
  2621. :type 'boolean)
  2622. (defcustom org-sparse-tree-open-archived-trees nil
  2623. "Non-nil means sparse tree construction shows matches in archived trees.
  2624. When nil, matches in these trees are highlighted, but the trees are kept in
  2625. collapsed state."
  2626. :group 'org-archive
  2627. :group 'org-sparse-trees
  2628. :type 'boolean)
  2629. (defun org-cycle-hide-archived-subtrees (state)
  2630. "Re-hide all archived subtrees after a visibility state change."
  2631. (when (and (not org-cycle-open-archived-trees)
  2632. (not (memq state '(overview folded))))
  2633. (save-excursion
  2634. (let* ((globalp (memq state '(contents all)))
  2635. (beg (if globalp (point-min) (point)))
  2636. (end (if globalp (point-max) (org-end-of-subtree t))))
  2637. (org-hide-archived-subtrees beg end)
  2638. (goto-char beg)
  2639. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2640. (message "%s" (substitute-command-keys
  2641. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2642. (defun org-force-cycle-archived ()
  2643. "Cycle subtree even if it is archived."
  2644. (interactive)
  2645. (setq this-command 'org-cycle)
  2646. (let ((org-cycle-open-archived-trees t))
  2647. (call-interactively 'org-cycle)))
  2648. (defun org-hide-archived-subtrees (beg end)
  2649. "Re-hide all archived subtrees after a visibility state change."
  2650. (save-excursion
  2651. (let* ((re (concat ":" org-archive-tag ":")))
  2652. (goto-char beg)
  2653. (while (re-search-forward re end t)
  2654. (and (org-on-heading-p) (hide-subtree))
  2655. (org-end-of-subtree t)))))
  2656. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2657. (eval-and-compile
  2658. (org-autoload "org-archive"
  2659. '(org-add-archive-files org-archive-subtree
  2660. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2661. ;; Autoload Column View Code
  2662. (declare-function org-columns-number-to-string "org-colview")
  2663. (declare-function org-columns-get-format-and-top-level "org-colview")
  2664. (declare-function org-columns-compute "org-colview")
  2665. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2666. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2667. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2668. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2669. ;; Autoload ID code
  2670. (declare-function org-id-store-link "org-id")
  2671. (declare-function org-id-locations-load "org-id")
  2672. (declare-function org-id-locations-save "org-id")
  2673. (defvar org-id-track-globally)
  2674. (org-autoload "org-id"
  2675. '(org-id-get-create org-id-new org-id-copy org-id-get
  2676. org-id-get-with-outline-path-completion
  2677. org-id-get-with-outline-drilling
  2678. org-id-goto org-id-find org-id-store-link))
  2679. ;; Autoload Plotting Code
  2680. (org-autoload "org-plot"
  2681. '(org-plot/gnuplot))
  2682. ;;; Variables for pre-computed regular expressions, all buffer local
  2683. (defvar org-drawer-regexp nil
  2684. "Matches first line of a hidden block.")
  2685. (make-variable-buffer-local 'org-drawer-regexp)
  2686. (defvar org-todo-regexp nil
  2687. "Matches any of the TODO state keywords.")
  2688. (make-variable-buffer-local 'org-todo-regexp)
  2689. (defvar org-not-done-regexp nil
  2690. "Matches any of the TODO state keywords except the last one.")
  2691. (make-variable-buffer-local 'org-not-done-regexp)
  2692. (defvar org-todo-line-regexp nil
  2693. "Matches a headline and puts TODO state into group 2 if present.")
  2694. (make-variable-buffer-local 'org-todo-line-regexp)
  2695. (defvar org-complex-heading-regexp nil
  2696. "Matches a headline and puts everything into groups:
  2697. group 1: the stars
  2698. group 2: The todo keyword, maybe
  2699. group 3: Priority cookie
  2700. group 4: True headline
  2701. group 5: Tags")
  2702. (make-variable-buffer-local 'org-complex-heading-regexp)
  2703. (defvar org-todo-line-tags-regexp nil
  2704. "Matches a headline and puts TODO state into group 2 if present.
  2705. Also put tags into group 4 if tags are present.")
  2706. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2707. (defvar org-nl-done-regexp nil
  2708. "Matches newline followed by a headline with the DONE keyword.")
  2709. (make-variable-buffer-local 'org-nl-done-regexp)
  2710. (defvar org-looking-at-done-regexp nil
  2711. "Matches the DONE keyword a point.")
  2712. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2713. (defvar org-ds-keyword-length 12
  2714. "Maximum length of the Deadline and SCHEDULED keywords.")
  2715. (make-variable-buffer-local 'org-ds-keyword-length)
  2716. (defvar org-deadline-regexp nil
  2717. "Matches the DEADLINE keyword.")
  2718. (make-variable-buffer-local 'org-deadline-regexp)
  2719. (defvar org-deadline-time-regexp nil
  2720. "Matches the DEADLINE keyword together with a time stamp.")
  2721. (make-variable-buffer-local 'org-deadline-time-regexp)
  2722. (defvar org-deadline-line-regexp nil
  2723. "Matches the DEADLINE keyword and the rest of the line.")
  2724. (make-variable-buffer-local 'org-deadline-line-regexp)
  2725. (defvar org-scheduled-regexp nil
  2726. "Matches the SCHEDULED keyword.")
  2727. (make-variable-buffer-local 'org-scheduled-regexp)
  2728. (defvar org-scheduled-time-regexp nil
  2729. "Matches the SCHEDULED keyword together with a time stamp.")
  2730. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2731. (defvar org-closed-time-regexp nil
  2732. "Matches the CLOSED keyword together with a time stamp.")
  2733. (make-variable-buffer-local 'org-closed-time-regexp)
  2734. (defvar org-keyword-time-regexp nil
  2735. "Matches any of the 4 keywords, together with the time stamp.")
  2736. (make-variable-buffer-local 'org-keyword-time-regexp)
  2737. (defvar org-keyword-time-not-clock-regexp nil
  2738. "Matches any of the 3 keywords, together with the time stamp.")
  2739. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2740. (defvar org-maybe-keyword-time-regexp nil
  2741. "Matches a timestamp, possibly preceeded by a keyword.")
  2742. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2743. (defvar org-planning-or-clock-line-re nil
  2744. "Matches a line with planning or clock info.")
  2745. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2746. (defconst org-plain-time-of-day-regexp
  2747. (concat
  2748. "\\(\\<[012]?[0-9]"
  2749. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2750. "\\(--?"
  2751. "\\(\\<[012]?[0-9]"
  2752. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2753. "\\)?")
  2754. "Regular expression to match a plain time or time range.
  2755. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2756. groups carry important information:
  2757. 0 the full match
  2758. 1 the first time, range or not
  2759. 8 the second time, if it is a range.")
  2760. (defconst org-plain-time-extension-regexp
  2761. (concat
  2762. "\\(\\<[012]?[0-9]"
  2763. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2764. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2765. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2766. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2767. groups carry important information:
  2768. 0 the full match
  2769. 7 hours of duration
  2770. 9 minutes of duration")
  2771. (defconst org-stamp-time-of-day-regexp
  2772. (concat
  2773. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2774. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2775. "\\(--?"
  2776. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2777. "Regular expression to match a timestamp time or time range.
  2778. After a match, the following groups carry important information:
  2779. 0 the full match
  2780. 1 date plus weekday, for backreferencing to make sure both times on same day
  2781. 2 the first time, range or not
  2782. 4 the second time, if it is a range.")
  2783. (defconst org-startup-options
  2784. '(("fold" org-startup-folded t)
  2785. ("overview" org-startup-folded t)
  2786. ("nofold" org-startup-folded nil)
  2787. ("showall" org-startup-folded nil)
  2788. ("content" org-startup-folded content)
  2789. ("hidestars" org-hide-leading-stars t)
  2790. ("showstars" org-hide-leading-stars nil)
  2791. ("odd" org-odd-levels-only t)
  2792. ("oddeven" org-odd-levels-only nil)
  2793. ("align" org-startup-align-all-tables t)
  2794. ("noalign" org-startup-align-all-tables nil)
  2795. ("customtime" org-display-custom-times t)
  2796. ("logdone" org-log-done time)
  2797. ("lognotedone" org-log-done note)
  2798. ("nologdone" org-log-done nil)
  2799. ("lognoteclock-out" org-log-note-clock-out t)
  2800. ("nolognoteclock-out" org-log-note-clock-out nil)
  2801. ("logrepeat" org-log-repeat state)
  2802. ("lognoterepeat" org-log-repeat note)
  2803. ("nologrepeat" org-log-repeat nil)
  2804. ("fninline" org-footnote-define-inline t)
  2805. ("nofninline" org-footnote-define-inline nil)
  2806. ("fnlocal" org-footnote-section nil)
  2807. ("fnauto" org-footnote-auto-label t)
  2808. ("fnprompt" org-footnote-auto-label nil)
  2809. ("fnconfirm" org-footnote-auto-label confirm)
  2810. ("fnplain" org-footnote-auto-label plain)
  2811. ("constcgs" constants-unit-system cgs)
  2812. ("constSI" constants-unit-system SI))
  2813. "Variable associated with STARTUP options for org-mode.
  2814. Each element is a list of three items: The startup options as written
  2815. in the #+STARTUP line, the corresponding variable, and the value to
  2816. set this variable to if the option is found. An optional forth element PUSH
  2817. means to push this value onto the list in the variable.")
  2818. (defun org-set-regexps-and-options ()
  2819. "Precompute regular expressions for current buffer."
  2820. (when (org-mode-p)
  2821. (org-set-local 'org-todo-kwd-alist nil)
  2822. (org-set-local 'org-todo-key-alist nil)
  2823. (org-set-local 'org-todo-key-trigger nil)
  2824. (org-set-local 'org-todo-keywords-1 nil)
  2825. (org-set-local 'org-done-keywords nil)
  2826. (org-set-local 'org-todo-heads nil)
  2827. (org-set-local 'org-todo-sets nil)
  2828. (org-set-local 'org-todo-log-states nil)
  2829. (org-set-local 'org-file-properties nil)
  2830. (org-set-local 'org-file-tags nil)
  2831. (let ((re (org-make-options-regexp
  2832. '("CATEGORY" "TODO" "COLUMNS"
  2833. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2834. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2835. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2836. (splitre "[ \t]+")
  2837. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2838. tail sep kws1 prio props ftags drawers
  2839. ext-setup-or-nil setup-contents (start 0))
  2840. (save-excursion
  2841. (save-restriction
  2842. (widen)
  2843. (goto-char (point-min))
  2844. (while (or (and ext-setup-or-nil
  2845. (string-match re ext-setup-or-nil start)
  2846. (setq start (match-end 0)))
  2847. (and (setq ext-setup-or-nil nil start 0)
  2848. (re-search-forward re nil t)))
  2849. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2850. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2851. (cond
  2852. ((equal key "CATEGORY")
  2853. (if (string-match "[ \t]+$" value)
  2854. (setq value (replace-match "" t t value)))
  2855. (setq cat value))
  2856. ((member key '("SEQ_TODO" "TODO"))
  2857. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2858. ((equal key "TYP_TODO")
  2859. (push (cons 'type (org-split-string value splitre)) kwds))
  2860. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2861. ;; general TODO-like setup
  2862. (push (cons (intern (downcase (match-string 1 key)))
  2863. (org-split-string value splitre)) kwds))
  2864. ((equal key "TAGS")
  2865. (setq tags (append tags (org-split-string value splitre))))
  2866. ((equal key "COLUMNS")
  2867. (org-set-local 'org-columns-default-format value))
  2868. ((equal key "LINK")
  2869. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2870. (push (cons (match-string 1 value)
  2871. (org-trim (match-string 2 value)))
  2872. links)))
  2873. ((equal key "PRIORITIES")
  2874. (setq prio (org-split-string value " +")))
  2875. ((equal key "PROPERTY")
  2876. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2877. (push (cons (match-string 1 value) (match-string 2 value))
  2878. props)))
  2879. ((equal key "FILETAGS")
  2880. (when (string-match "\\S-" value)
  2881. (setq ftags
  2882. (append
  2883. ftags
  2884. (apply 'append
  2885. (mapcar (lambda (x) (org-split-string x ":"))
  2886. (org-split-string value)))))))
  2887. ((equal key "DRAWERS")
  2888. (setq drawers (org-split-string value splitre)))
  2889. ((equal key "CONSTANTS")
  2890. (setq const (append const (org-split-string value splitre))))
  2891. ((equal key "STARTUP")
  2892. (let ((opts (org-split-string value splitre))
  2893. l var val)
  2894. (while (setq l (pop opts))
  2895. (when (setq l (assoc l org-startup-options))
  2896. (setq var (nth 1 l) val (nth 2 l))
  2897. (if (not (nth 3 l))
  2898. (set (make-local-variable var) val)
  2899. (if (not (listp (symbol-value var)))
  2900. (set (make-local-variable var) nil))
  2901. (set (make-local-variable var) (symbol-value var))
  2902. (add-to-list var val))))))
  2903. ((equal key "ARCHIVE")
  2904. (string-match " *$" value)
  2905. (setq arch (replace-match "" t t value))
  2906. (remove-text-properties 0 (length arch)
  2907. '(face t fontified t) arch))
  2908. ((equal key "SETUPFILE")
  2909. (setq setup-contents (org-file-contents
  2910. (expand-file-name
  2911. (org-remove-double-quotes value))
  2912. 'noerror))
  2913. (if (not ext-setup-or-nil)
  2914. (setq ext-setup-or-nil setup-contents start 0)
  2915. (setq ext-setup-or-nil
  2916. (concat (substring ext-setup-or-nil 0 start)
  2917. "\n" setup-contents "\n"
  2918. (substring ext-setup-or-nil start)))))
  2919. ))))
  2920. (when cat
  2921. (org-set-local 'org-category (intern cat))
  2922. (push (cons "CATEGORY" cat) props))
  2923. (when prio
  2924. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2925. (setq prio (mapcar 'string-to-char prio))
  2926. (org-set-local 'org-highest-priority (nth 0 prio))
  2927. (org-set-local 'org-lowest-priority (nth 1 prio))
  2928. (org-set-local 'org-default-priority (nth 2 prio)))
  2929. (and props (org-set-local 'org-file-properties (nreverse props)))
  2930. (and ftags (org-set-local 'org-file-tags ftags))
  2931. (and drawers (org-set-local 'org-drawers drawers))
  2932. (and arch (org-set-local 'org-archive-location arch))
  2933. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2934. ;; Process the TODO keywords
  2935. (unless kwds
  2936. ;; Use the global values as if they had been given locally.
  2937. (setq kwds (default-value 'org-todo-keywords))
  2938. (if (stringp (car kwds))
  2939. (setq kwds (list (cons org-todo-interpretation
  2940. (default-value 'org-todo-keywords)))))
  2941. (setq kwds (reverse kwds)))
  2942. (setq kwds (nreverse kwds))
  2943. (let (inter kws kw)
  2944. (while (setq kws (pop kwds))
  2945. (let ((kws (or
  2946. (run-hook-with-args-until-success
  2947. 'org-todo-setup-filter-hook kws)
  2948. kws)))
  2949. (setq inter (pop kws) sep (member "|" kws)
  2950. kws0 (delete "|" (copy-sequence kws))
  2951. kwsa nil
  2952. kws1 (mapcar
  2953. (lambda (x)
  2954. ;; 1 2
  2955. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2956. (progn
  2957. (setq kw (match-string 1 x)
  2958. key (and (match-end 2) (match-string 2 x))
  2959. log (org-extract-log-state-settings x))
  2960. (push (cons kw (and key (string-to-char key))) kwsa)
  2961. (and log (push log org-todo-log-states))
  2962. kw)
  2963. (error "Invalid TODO keyword %s" x)))
  2964. kws0)
  2965. kwsa (if kwsa (append '((:startgroup))
  2966. (nreverse kwsa)
  2967. '((:endgroup))))
  2968. hw (car kws1)
  2969. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2970. tail (list inter hw (car dws) (org-last dws))))
  2971. (add-to-list 'org-todo-heads hw 'append)
  2972. (push kws1 org-todo-sets)
  2973. (setq org-done-keywords (append org-done-keywords dws nil))
  2974. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2975. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2976. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2977. (setq org-todo-sets (nreverse org-todo-sets)
  2978. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2979. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2980. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2981. ;; Process the constants
  2982. (when const
  2983. (let (e cst)
  2984. (while (setq e (pop const))
  2985. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2986. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2987. (setq org-table-formula-constants-local cst)))
  2988. ;; Process the tags.
  2989. (when tags
  2990. (let (e tgs)
  2991. (while (setq e (pop tags))
  2992. (cond
  2993. ((equal e "{") (push '(:startgroup) tgs))
  2994. ((equal e "}") (push '(:endgroup) tgs))
  2995. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2996. (push (cons (match-string 1 e)
  2997. (string-to-char (match-string 2 e)))
  2998. tgs))
  2999. (t (push (list e) tgs))))
  3000. (org-set-local 'org-tag-alist nil)
  3001. (while (setq e (pop tgs))
  3002. (or (and (stringp (car e))
  3003. (assoc (car e) org-tag-alist))
  3004. (push e org-tag-alist)))))
  3005. ;; Compute the regular expressions and other local variables
  3006. (if (not org-done-keywords)
  3007. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3008. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3009. (length org-scheduled-string)
  3010. (length org-clock-string)
  3011. (length org-closed-string)))
  3012. org-drawer-regexp
  3013. (concat "^[ \t]*:\\("
  3014. (mapconcat 'regexp-quote org-drawers "\\|")
  3015. "\\):[ \t]*$")
  3016. org-not-done-keywords
  3017. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3018. org-todo-regexp
  3019. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3020. "\\|") "\\)\\>")
  3021. org-not-done-regexp
  3022. (concat "\\<\\("
  3023. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3024. "\\)\\>")
  3025. org-todo-line-regexp
  3026. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3027. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3028. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3029. org-complex-heading-regexp
  3030. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3031. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3032. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3033. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3034. org-nl-done-regexp
  3035. (concat "\n\\*+[ \t]+"
  3036. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3037. "\\)" "\\>")
  3038. org-todo-line-tags-regexp
  3039. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3040. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3041. (org-re
  3042. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3043. org-looking-at-done-regexp
  3044. (concat "^" "\\(?:"
  3045. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3046. "\\>")
  3047. org-deadline-regexp (concat "\\<" org-deadline-string)
  3048. org-deadline-time-regexp
  3049. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3050. org-deadline-line-regexp
  3051. (concat "\\<\\(" org-deadline-string "\\).*")
  3052. org-scheduled-regexp
  3053. (concat "\\<" org-scheduled-string)
  3054. org-scheduled-time-regexp
  3055. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3056. org-closed-time-regexp
  3057. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3058. org-keyword-time-regexp
  3059. (concat "\\<\\(" org-scheduled-string
  3060. "\\|" org-deadline-string
  3061. "\\|" org-closed-string
  3062. "\\|" org-clock-string "\\)"
  3063. " *[[<]\\([^]>]+\\)[]>]")
  3064. org-keyword-time-not-clock-regexp
  3065. (concat "\\<\\(" org-scheduled-string
  3066. "\\|" org-deadline-string
  3067. "\\|" org-closed-string
  3068. "\\)"
  3069. " *[[<]\\([^]>]+\\)[]>]")
  3070. org-maybe-keyword-time-regexp
  3071. (concat "\\(\\<\\(" org-scheduled-string
  3072. "\\|" org-deadline-string
  3073. "\\|" org-closed-string
  3074. "\\|" org-clock-string "\\)\\)?"
  3075. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3076. org-planning-or-clock-line-re
  3077. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3078. "\\|" org-deadline-string
  3079. "\\|" org-closed-string "\\|" org-clock-string
  3080. "\\)\\>\\)")
  3081. )
  3082. (org-compute-latex-and-specials-regexp)
  3083. (org-set-font-lock-defaults))))
  3084. (defun org-file-contents (file &optional noerror)
  3085. "Return the contents of FILE, as a string."
  3086. (if (or (not file)
  3087. (not (file-readable-p file)))
  3088. (if noerror
  3089. (progn
  3090. (message "Cannot read file %s" file)
  3091. (ding) (sit-for 2)
  3092. "")
  3093. (error "Cannot read file %s" file))
  3094. (with-temp-buffer
  3095. (insert-file-contents file)
  3096. (buffer-string))))
  3097. (defun org-extract-log-state-settings (x)
  3098. "Extract the log state setting from a TODO keyword string.
  3099. This will extract info from a string like \"WAIT(w@/!)\"."
  3100. (let (kw key log1 log2)
  3101. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3102. (setq kw (match-string 1 x)
  3103. key (and (match-end 2) (match-string 2 x))
  3104. log1 (and (match-end 3) (match-string 3 x))
  3105. log2 (and (match-end 4) (match-string 4 x)))
  3106. (and (or log1 log2)
  3107. (list kw
  3108. (and log1 (if (equal log1 "!") 'time 'note))
  3109. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3110. (defun org-remove-keyword-keys (list)
  3111. "Remove a pair of parenthesis at the end of each string in LIST."
  3112. (mapcar (lambda (x)
  3113. (if (string-match "(.*)$" x)
  3114. (substring x 0 (match-beginning 0))
  3115. x))
  3116. list))
  3117. ;; FIXME: this could be done much better, using second characters etc.
  3118. (defun org-assign-fast-keys (alist)
  3119. "Assign fast keys to a keyword-key alist.
  3120. Respect keys that are already there."
  3121. (let (new e k c c1 c2 (char ?a))
  3122. (while (setq e (pop alist))
  3123. (cond
  3124. ((equal e '(:startgroup)) (push e new))
  3125. ((equal e '(:endgroup)) (push e new))
  3126. (t
  3127. (setq k (car e) c2 nil)
  3128. (if (cdr e)
  3129. (setq c (cdr e))
  3130. ;; automatically assign a character.
  3131. (setq c1 (string-to-char
  3132. (downcase (substring
  3133. k (if (= (string-to-char k) ?@) 1 0)))))
  3134. (if (or (rassoc c1 new) (rassoc c1 alist))
  3135. (while (or (rassoc char new) (rassoc char alist))
  3136. (setq char (1+ char)))
  3137. (setq c2 c1))
  3138. (setq c (or c2 char)))
  3139. (push (cons k c) new))))
  3140. (nreverse new)))
  3141. ;;; Some variables used in various places
  3142. (defvar org-window-configuration nil
  3143. "Used in various places to store a window configuration.")
  3144. (defvar org-finish-function nil
  3145. "Function to be called when `C-c C-c' is used.
  3146. This is for getting out of special buffers like remember.")
  3147. ;; FIXME: Occasionally check by commenting these, to make sure
  3148. ;; no other functions uses these, forgetting to let-bind them.
  3149. (defvar entry)
  3150. (defvar state)
  3151. (defvar last-state)
  3152. (defvar date)
  3153. (defvar description)
  3154. ;; Defined somewhere in this file, but used before definition.
  3155. (defvar org-html-entities)
  3156. (defvar org-struct-menu)
  3157. (defvar org-org-menu)
  3158. (defvar org-tbl-menu)
  3159. (defvar org-agenda-keymap)
  3160. ;;;; Define the Org-mode
  3161. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3162. (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."))
  3163. ;; We use a before-change function to check if a table might need
  3164. ;; an update.
  3165. (defvar org-table-may-need-update t
  3166. "Indicates that a table might need an update.
  3167. This variable is set by `org-before-change-function'.
  3168. `org-table-align' sets it back to nil.")
  3169. (defun org-before-change-function (beg end)
  3170. "Every change indicates that a table might need an update."
  3171. (setq org-table-may-need-update t))
  3172. (defvar org-mode-map)
  3173. (defvar org-mode-hook nil
  3174. "Mode hook for Org-mode, run after the mode was turned on.")
  3175. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3176. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3177. (defvar org-table-buffer-is-an nil)
  3178. (defconst org-outline-regexp "\\*+ ")
  3179. ;;;###autoload
  3180. (define-derived-mode org-mode outline-mode "Org"
  3181. "Outline-based notes management and organizer, alias
  3182. \"Carsten's outline-mode for keeping track of everything.\"
  3183. Org-mode develops organizational tasks around a NOTES file which
  3184. contains information about projects as plain text. Org-mode is
  3185. implemented on top of outline-mode, which is ideal to keep the content
  3186. of large files well structured. It supports ToDo items, deadlines and
  3187. time stamps, which magically appear in the diary listing of the Emacs
  3188. calendar. Tables are easily created with a built-in table editor.
  3189. Plain text URL-like links connect to websites, emails (VM), Usenet
  3190. messages (Gnus), BBDB entries, and any files related to the project.
  3191. For printing and sharing of notes, an Org-mode file (or a part of it)
  3192. can be exported as a structured ASCII or HTML file.
  3193. The following commands are available:
  3194. \\{org-mode-map}"
  3195. ;; Get rid of Outline menus, they are not needed
  3196. ;; Need to do this here because define-derived-mode sets up
  3197. ;; the keymap so late. Still, it is a waste to call this each time
  3198. ;; we switch another buffer into org-mode.
  3199. (if (featurep 'xemacs)
  3200. (when (boundp 'outline-mode-menu-heading)
  3201. ;; Assume this is Greg's port, it used easymenu
  3202. (easy-menu-remove outline-mode-menu-heading)
  3203. (easy-menu-remove outline-mode-menu-show)
  3204. (easy-menu-remove outline-mode-menu-hide))
  3205. (define-key org-mode-map [menu-bar headings] 'undefined)
  3206. (define-key org-mode-map [menu-bar hide] 'undefined)
  3207. (define-key org-mode-map [menu-bar show] 'undefined))
  3208. (org-load-modules-maybe)
  3209. (easy-menu-add org-org-menu)
  3210. (easy-menu-add org-tbl-menu)
  3211. (org-install-agenda-files-menu)
  3212. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3213. (org-add-to-invisibility-spec '(org-cwidth))
  3214. (when (featurep 'xemacs)
  3215. (org-set-local 'line-move-ignore-invisible t))
  3216. (org-set-local 'outline-regexp org-outline-regexp)
  3217. (org-set-local 'outline-level 'org-outline-level)
  3218. (when (and org-ellipsis
  3219. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3220. (fboundp 'make-glyph-code))
  3221. (unless org-display-table
  3222. (setq org-display-table (make-display-table)))
  3223. (set-display-table-slot
  3224. org-display-table 4
  3225. (vconcat (mapcar
  3226. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3227. org-ellipsis)))
  3228. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3229. (setq buffer-display-table org-display-table))
  3230. (org-set-regexps-and-options)
  3231. (when (and org-tag-faces (not org-tags-special-faces-re))
  3232. ;; tag faces set outside customize.... force initialization.
  3233. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3234. ;; Calc embedded
  3235. (org-set-local 'calc-embedded-open-mode "# ")
  3236. (modify-syntax-entry ?# "<")
  3237. (modify-syntax-entry ?@ "w")
  3238. (if org-startup-truncated (setq truncate-lines t))
  3239. (org-set-local 'font-lock-unfontify-region-function
  3240. 'org-unfontify-region)
  3241. ;; Activate before-change-function
  3242. (org-set-local 'org-table-may-need-update t)
  3243. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3244. 'local)
  3245. ;; Check for running clock before killing a buffer
  3246. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3247. ;; Paragraphs and auto-filling
  3248. (org-set-autofill-regexps)
  3249. (setq indent-line-function 'org-indent-line-function)
  3250. (org-update-radio-target-regexp)
  3251. ;; Make sure dependence stuff works reliably, even for users who set it
  3252. ;; too late :-(
  3253. (if org-enforce-todo-dependencies
  3254. (add-hook 'org-blocker-hook
  3255. 'org-block-todo-from-children-or-siblings)
  3256. (remove-hook 'org-blocker-hook
  3257. 'org-block-todo-from-children-or-siblings))
  3258. (if org-enforce-todo-checkbox-dependencies
  3259. (add-hook 'org-blocker-hook
  3260. 'org-block-todo-from-checkboxes)
  3261. (remove-hook 'org-blocker-hook
  3262. 'org-block-todo-from-checkboxes))
  3263. ;; Comment characters
  3264. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3265. (org-set-local 'comment-padding " ")
  3266. ;; Align options lines
  3267. (org-set-local
  3268. 'align-mode-rules-list
  3269. '((org-in-buffer-settings
  3270. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3271. (modes . '(org-mode)))))
  3272. ;; Imenu
  3273. (org-set-local 'imenu-create-index-function
  3274. 'org-imenu-get-tree)
  3275. ;; Make isearch reveal context
  3276. (if (or (featurep 'xemacs)
  3277. (not (boundp 'outline-isearch-open-invisible-function)))
  3278. ;; Emacs 21 and XEmacs make use of the hook
  3279. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3280. ;; Emacs 22 deals with this through a special variable
  3281. (org-set-local 'outline-isearch-open-invisible-function
  3282. (lambda (&rest ignore) (org-show-context 'isearch))))
  3283. ;; If empty file that did not turn on org-mode automatically, make it to.
  3284. (if (and org-insert-mode-line-in-empty-file
  3285. (interactive-p)
  3286. (= (point-min) (point-max)))
  3287. (insert "# -*- mode: org -*-\n\n"))
  3288. (unless org-inhibit-startup
  3289. (when org-startup-align-all-tables
  3290. (let ((bmp (buffer-modified-p)))
  3291. (org-table-map-tables 'org-table-align)
  3292. (set-buffer-modified-p bmp)))
  3293. (org-set-startup-visibility)))
  3294. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3295. (defun org-current-time ()
  3296. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3297. (if (> (car org-time-stamp-rounding-minutes) 1)
  3298. (let ((r (car org-time-stamp-rounding-minutes))
  3299. (time (decode-time)))
  3300. (apply 'encode-time
  3301. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3302. (nthcdr 2 time))))
  3303. (current-time)))
  3304. ;;;; Font-Lock stuff, including the activators
  3305. (defvar org-mouse-map (make-sparse-keymap))
  3306. (org-defkey org-mouse-map
  3307. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3308. (org-defkey org-mouse-map
  3309. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3310. (when org-mouse-1-follows-link
  3311. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3312. (when org-tab-follows-link
  3313. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3314. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3315. (require 'font-lock)
  3316. (defconst org-non-link-chars "]\t\n\r<>")
  3317. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3318. "shell" "elisp"))
  3319. (defvar org-link-types-re nil
  3320. "Matches a link that has a url-like prefix like \"http:\"")
  3321. (defvar org-link-re-with-space nil
  3322. "Matches a link with spaces, optional angular brackets around it.")
  3323. (defvar org-link-re-with-space2 nil
  3324. "Matches a link with spaces, optional angular brackets around it.")
  3325. (defvar org-link-re-with-space3 nil
  3326. "Matches a link with spaces, only for internal part in bracket links.")
  3327. (defvar org-angle-link-re nil
  3328. "Matches link with angular brackets, spaces are allowed.")
  3329. (defvar org-plain-link-re nil
  3330. "Matches plain link, without spaces.")
  3331. (defvar org-bracket-link-regexp nil
  3332. "Matches a link in double brackets.")
  3333. (defvar org-bracket-link-analytic-regexp nil
  3334. "Regular expression used to analyze links.
  3335. Here is what the match groups contain after a match:
  3336. 1: http:
  3337. 2: http
  3338. 3: path
  3339. 4: [desc]
  3340. 5: desc")
  3341. (defvar org-bracket-link-analytic-regexp++ nil
  3342. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3343. (defvar org-any-link-re nil
  3344. "Regular expression matching any link.")
  3345. (defun org-make-link-regexps ()
  3346. "Update the link regular expressions.
  3347. This should be called after the variable `org-link-types' has changed."
  3348. (setq org-link-types-re
  3349. (concat
  3350. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3351. org-link-re-with-space
  3352. (concat
  3353. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3354. "\\([^" org-non-link-chars " ]"
  3355. "[^" org-non-link-chars "]*"
  3356. "[^" org-non-link-chars " ]\\)>?")
  3357. org-link-re-with-space2
  3358. (concat
  3359. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3360. "\\([^" org-non-link-chars " ]"
  3361. "[^\t\n\r]*"
  3362. "[^" org-non-link-chars " ]\\)>?")
  3363. org-link-re-with-space3
  3364. (concat
  3365. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3366. "\\([^" org-non-link-chars " ]"
  3367. "[^\t\n\r]*\\)")
  3368. org-angle-link-re
  3369. (concat
  3370. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3371. "\\([^" org-non-link-chars " ]"
  3372. "[^" org-non-link-chars "]*"
  3373. "\\)>")
  3374. org-plain-link-re
  3375. (concat
  3376. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3377. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3378. org-bracket-link-regexp
  3379. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3380. org-bracket-link-analytic-regexp
  3381. (concat
  3382. "\\[\\["
  3383. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3384. "\\([^]]+\\)"
  3385. "\\]"
  3386. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3387. "\\]")
  3388. org-bracket-link-analytic-regexp++
  3389. (concat
  3390. "\\[\\["
  3391. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3392. "\\([^]]+\\)"
  3393. "\\]"
  3394. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3395. "\\]")
  3396. org-any-link-re
  3397. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3398. org-angle-link-re "\\)\\|\\("
  3399. org-plain-link-re "\\)")))
  3400. (org-make-link-regexps)
  3401. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3402. "Regular expression for fast time stamp matching.")
  3403. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3404. "Regular expression for fast time stamp matching.")
  3405. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3406. "Regular expression matching time strings for analysis.
  3407. This one does not require the space after the date, so it can be used
  3408. on a string that terminates immediately after the date.")
  3409. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3410. "Regular expression matching time strings for analysis.")
  3411. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3412. "Regular expression matching time stamps, with groups.")
  3413. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3414. "Regular expression matching time stamps (also [..]), with groups.")
  3415. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3416. "Regular expression matching a time stamp range.")
  3417. (defconst org-tr-regexp-both
  3418. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3419. "Regular expression matching a time stamp range.")
  3420. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3421. org-ts-regexp "\\)?")
  3422. "Regular expression matching a time stamp or time stamp range.")
  3423. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3424. org-ts-regexp-both "\\)?")
  3425. "Regular expression matching a time stamp or time stamp range.
  3426. The time stamps may be either active or inactive.")
  3427. (defvar org-emph-face nil)
  3428. (defun org-do-emphasis-faces (limit)
  3429. "Run through the buffer and add overlays to links."
  3430. (let (rtn)
  3431. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3432. (if (not (= (char-after (match-beginning 3))
  3433. (char-after (match-beginning 4))))
  3434. (progn
  3435. (setq rtn t)
  3436. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3437. 'face
  3438. (nth 1 (assoc (match-string 3)
  3439. org-emphasis-alist)))
  3440. (add-text-properties (match-beginning 2) (match-end 2)
  3441. '(font-lock-multiline t))
  3442. (when org-hide-emphasis-markers
  3443. (add-text-properties (match-end 4) (match-beginning 5)
  3444. '(invisible org-link))
  3445. (add-text-properties (match-beginning 3) (match-end 3)
  3446. '(invisible org-link)))))
  3447. (backward-char 1))
  3448. rtn))
  3449. (defun org-emphasize (&optional char)
  3450. "Insert or change an emphasis, i.e. a font like bold or italic.
  3451. If there is an active region, change that region to a new emphasis.
  3452. If there is no region, just insert the marker characters and position
  3453. the cursor between them.
  3454. CHAR should be either the marker character, or the first character of the
  3455. HTML tag associated with that emphasis. If CHAR is a space, the means
  3456. to remove the emphasis of the selected region.
  3457. If char is not given (for example in an interactive call) it
  3458. will be prompted for."
  3459. (interactive)
  3460. (let ((eal org-emphasis-alist) e det
  3461. (erc org-emphasis-regexp-components)
  3462. (prompt "")
  3463. (string "") beg end move tag c s)
  3464. (if (org-region-active-p)
  3465. (setq beg (region-beginning) end (region-end)
  3466. string (buffer-substring beg end))
  3467. (setq move t))
  3468. (while (setq e (pop eal))
  3469. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3470. c (aref tag 0))
  3471. (push (cons c (string-to-char (car e))) det)
  3472. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3473. (substring tag 1)))))
  3474. (setq det (nreverse det))
  3475. (unless char
  3476. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3477. (setq char (read-char-exclusive)))
  3478. (setq char (or (cdr (assoc char det)) char))
  3479. (if (equal char ?\ )
  3480. (setq s "" move nil)
  3481. (unless (assoc (char-to-string char) org-emphasis-alist)
  3482. (error "No such emphasis marker: \"%c\"" char))
  3483. (setq s (char-to-string char)))
  3484. (while (and (> (length string) 1)
  3485. (equal (substring string 0 1) (substring string -1))
  3486. (assoc (substring string 0 1) org-emphasis-alist))
  3487. (setq string (substring string 1 -1)))
  3488. (setq string (concat s string s))
  3489. (if beg (delete-region beg end))
  3490. (unless (or (bolp)
  3491. (string-match (concat "[" (nth 0 erc) "\n]")
  3492. (char-to-string (char-before (point)))))
  3493. (insert " "))
  3494. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3495. (char-to-string (char-after (point))))
  3496. (insert " ") (backward-char 1))
  3497. (insert string)
  3498. (and move (backward-char 1))))
  3499. (defconst org-nonsticky-props
  3500. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3501. (defun org-activate-plain-links (limit)
  3502. "Run through the buffer and add overlays to links."
  3503. (catch 'exit
  3504. (let (f)
  3505. (while (re-search-forward org-plain-link-re limit t)
  3506. (setq f (get-text-property (match-beginning 0) 'face))
  3507. (if (or (eq f 'org-tag)
  3508. (and (listp f) (memq 'org-tag f)))
  3509. nil
  3510. (add-text-properties (match-beginning 0) (match-end 0)
  3511. (list 'mouse-face 'highlight
  3512. 'rear-nonsticky org-nonsticky-props
  3513. 'keymap org-mouse-map
  3514. ))
  3515. (throw 'exit t))))))
  3516. (defun org-activate-code (limit)
  3517. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3518. (progn
  3519. (remove-text-properties (match-beginning 0) (match-end 0)
  3520. '(display t invisible t intangible t))
  3521. t)))
  3522. (defun org-activate-angle-links (limit)
  3523. "Run through the buffer and add overlays to links."
  3524. (if (re-search-forward org-angle-link-re limit t)
  3525. (progn
  3526. (add-text-properties (match-beginning 0) (match-end 0)
  3527. (list 'mouse-face 'highlight
  3528. 'rear-nonsticky org-nonsticky-props
  3529. 'keymap org-mouse-map
  3530. ))
  3531. t)))
  3532. (defun org-activate-footnote-links (limit)
  3533. "Run through the buffer and add overlays to links."
  3534. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3535. limit t)
  3536. (progn
  3537. (add-text-properties (match-beginning 2) (match-end 2)
  3538. (list 'mouse-face 'highlight
  3539. 'rear-nonsticky org-nonsticky-props
  3540. 'keymap org-mouse-map
  3541. 'help-echo
  3542. (if (= (point-at-bol) (match-beginning 2))
  3543. "Footnote definition"
  3544. "Footnote reference")
  3545. ))
  3546. t)))
  3547. (defun org-activate-bracket-links (limit)
  3548. "Run through the buffer and add overlays to bracketed links."
  3549. (if (re-search-forward org-bracket-link-regexp limit t)
  3550. (let* ((help (concat "LINK: "
  3551. (org-match-string-no-properties 1)))
  3552. ;; FIXME: above we should remove the escapes.
  3553. ;; but that requires another match, protecting match data,
  3554. ;; a lot of overhead for font-lock.
  3555. (ip (org-maybe-intangible
  3556. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3557. 'keymap org-mouse-map 'mouse-face 'highlight
  3558. 'font-lock-multiline t 'help-echo help)))
  3559. (vp (list 'rear-nonsticky org-nonsticky-props
  3560. 'keymap org-mouse-map 'mouse-face 'highlight
  3561. ' font-lock-multiline t 'help-echo help)))
  3562. ;; We need to remove the invisible property here. Table narrowing
  3563. ;; may have made some of this invisible.
  3564. (remove-text-properties (match-beginning 0) (match-end 0)
  3565. '(invisible nil))
  3566. (if (match-end 3)
  3567. (progn
  3568. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3569. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3570. (add-text-properties (match-end 3) (match-end 0) ip))
  3571. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3572. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3573. (add-text-properties (match-end 1) (match-end 0) ip))
  3574. t)))
  3575. (defun org-activate-dates (limit)
  3576. "Run through the buffer and add overlays to dates."
  3577. (if (re-search-forward org-tsr-regexp-both limit t)
  3578. (progn
  3579. (add-text-properties (match-beginning 0) (match-end 0)
  3580. (list 'mouse-face 'highlight
  3581. 'rear-nonsticky org-nonsticky-props
  3582. 'keymap org-mouse-map))
  3583. (when org-display-custom-times
  3584. (if (match-end 3)
  3585. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3586. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3587. t)))
  3588. (defvar org-target-link-regexp nil
  3589. "Regular expression matching radio targets in plain text.")
  3590. (make-variable-buffer-local 'org-target-link-regexp)
  3591. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3592. "Regular expression matching a link target.")
  3593. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3594. "Regular expression matching a radio target.")
  3595. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3596. "Regular expression matching any target.")
  3597. (defun org-activate-target-links (limit)
  3598. "Run through the buffer and add overlays to target matches."
  3599. (when org-target-link-regexp
  3600. (let ((case-fold-search t))
  3601. (if (re-search-forward org-target-link-regexp limit t)
  3602. (progn
  3603. (add-text-properties (match-beginning 0) (match-end 0)
  3604. (list 'mouse-face 'highlight
  3605. 'rear-nonsticky org-nonsticky-props
  3606. 'keymap org-mouse-map
  3607. 'help-echo "Radio target link"
  3608. 'org-linked-text t))
  3609. t)))))
  3610. (defun org-update-radio-target-regexp ()
  3611. "Find all radio targets in this file and update the regular expression."
  3612. (interactive)
  3613. (when (memq 'radio org-activate-links)
  3614. (setq org-target-link-regexp
  3615. (org-make-target-link-regexp (org-all-targets 'radio)))
  3616. (org-restart-font-lock)))
  3617. (defun org-hide-wide-columns (limit)
  3618. (let (s e)
  3619. (setq s (text-property-any (point) (or limit (point-max))
  3620. 'org-cwidth t))
  3621. (when s
  3622. (setq e (next-single-property-change s 'org-cwidth))
  3623. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3624. (goto-char e)
  3625. t)))
  3626. (defvar org-latex-and-specials-regexp nil
  3627. "Regular expression for highlighting export special stuff.")
  3628. (defvar org-match-substring-regexp)
  3629. (defvar org-match-substring-with-braces-regexp)
  3630. (defvar org-export-html-special-string-regexps)
  3631. (defun org-compute-latex-and-specials-regexp ()
  3632. "Compute regular expression for stuff treated specially by exporters."
  3633. (if (not org-highlight-latex-fragments-and-specials)
  3634. (org-set-local 'org-latex-and-specials-regexp nil)
  3635. (require 'org-exp)
  3636. (let*
  3637. ((matchers (plist-get org-format-latex-options :matchers))
  3638. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3639. org-latex-regexps)))
  3640. (options (org-combine-plists (org-default-export-plist)
  3641. (org-infile-export-plist)))
  3642. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3643. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3644. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3645. (org-export-html-expand (plist-get options :expand-quoted-html))
  3646. (org-export-with-special-strings (plist-get options :special-strings))
  3647. (re-sub
  3648. (cond
  3649. ((equal org-export-with-sub-superscripts '{})
  3650. (list org-match-substring-with-braces-regexp))
  3651. (org-export-with-sub-superscripts
  3652. (list org-match-substring-regexp))
  3653. (t nil)))
  3654. (re-latex
  3655. (if org-export-with-LaTeX-fragments
  3656. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3657. (re-macros
  3658. (if org-export-with-TeX-macros
  3659. (list (concat "\\\\"
  3660. (regexp-opt
  3661. (append (mapcar 'car org-html-entities)
  3662. (if (boundp 'org-latex-entities)
  3663. org-latex-entities nil))
  3664. 'words))) ; FIXME
  3665. ))
  3666. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3667. (re-special (if org-export-with-special-strings
  3668. (mapcar (lambda (x) (car x))
  3669. org-export-html-special-string-regexps)))
  3670. (re-rest
  3671. (delq nil
  3672. (list
  3673. (if org-export-html-expand "@<[^>\n]+>")
  3674. ))))
  3675. (org-set-local
  3676. 'org-latex-and-specials-regexp
  3677. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3678. re-rest) "\\|")))))
  3679. (defun org-do-latex-and-special-faces (limit)
  3680. "Run through the buffer and add overlays to links."
  3681. (when org-latex-and-specials-regexp
  3682. (let (rtn d)
  3683. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3684. limit t))
  3685. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3686. 'face))
  3687. '(org-code org-verbatim underline)))
  3688. (progn
  3689. (setq rtn t
  3690. d (cond ((member (char-after (1+ (match-beginning 0)))
  3691. '(?_ ?^)) 1)
  3692. (t 0)))
  3693. (font-lock-prepend-text-property
  3694. (+ d (match-beginning 0)) (match-end 0)
  3695. 'face 'org-latex-and-export-specials)
  3696. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3697. '(font-lock-multiline t)))))
  3698. rtn)))
  3699. (defun org-restart-font-lock ()
  3700. "Restart font-lock-mode, to force refontification."
  3701. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3702. (font-lock-mode -1)
  3703. (font-lock-mode 1)))
  3704. (defun org-all-targets (&optional radio)
  3705. "Return a list of all targets in this file.
  3706. With optional argument RADIO, only find radio targets."
  3707. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3708. rtn)
  3709. (save-excursion
  3710. (goto-char (point-min))
  3711. (while (re-search-forward re nil t)
  3712. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3713. rtn)))
  3714. (defun org-make-target-link-regexp (targets)
  3715. "Make regular expression matching all strings in TARGETS.
  3716. The regular expression finds the targets also if there is a line break
  3717. between words."
  3718. (and targets
  3719. (concat
  3720. "\\<\\("
  3721. (mapconcat
  3722. (lambda (x)
  3723. (while (string-match " +" x)
  3724. (setq x (replace-match "\\s-+" t t x)))
  3725. x)
  3726. targets
  3727. "\\|")
  3728. "\\)\\>")))
  3729. (defun org-activate-tags (limit)
  3730. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3731. (progn
  3732. (add-text-properties (match-beginning 1) (match-end 1)
  3733. (list 'mouse-face 'highlight
  3734. 'rear-nonsticky org-nonsticky-props
  3735. 'keymap org-mouse-map))
  3736. t)))
  3737. (defun org-outline-level ()
  3738. (save-excursion
  3739. (looking-at outline-regexp)
  3740. (if (match-beginning 1)
  3741. (+ (org-get-string-indentation (match-string 1)) 1000)
  3742. (1- (- (match-end 0) (match-beginning 0))))))
  3743. (defvar org-font-lock-keywords nil)
  3744. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3745. "Regular expression matching a property line.")
  3746. (defvar org-font-lock-hook nil
  3747. "Functions to be called for special font lock stuff.")
  3748. (defun org-font-lock-hook (limit)
  3749. (run-hook-with-args 'org-font-lock-hook limit))
  3750. (defun org-set-font-lock-defaults ()
  3751. (let* ((em org-fontify-emphasized-text)
  3752. (lk org-activate-links)
  3753. (org-font-lock-extra-keywords
  3754. (list
  3755. ;; Call the hook
  3756. '(org-font-lock-hook)
  3757. ;; Headlines
  3758. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3759. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3760. ;; Table lines
  3761. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3762. (1 'org-table t))
  3763. ;; Table internals
  3764. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3765. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3766. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3767. ;; Drawers
  3768. (list org-drawer-regexp '(0 'org-special-keyword t))
  3769. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3770. ;; Properties
  3771. (list org-property-re
  3772. '(1 'org-special-keyword t)
  3773. '(3 'org-property-value t))
  3774. (if org-format-transports-properties-p
  3775. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3776. ;; Links
  3777. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3778. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3779. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3780. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3781. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3782. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3783. (if (memq 'footnote lk) '(org-activate-footnote-links
  3784. (2 'org-footnote t)))
  3785. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3786. '(org-hide-wide-columns (0 nil append))
  3787. ;; TODO lines
  3788. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3789. '(1 (org-get-todo-face 1) t))
  3790. ;; DONE
  3791. (if org-fontify-done-headline
  3792. (list (concat "^[*]+ +\\<\\("
  3793. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3794. "\\)\\(.*\\)")
  3795. '(2 'org-headline-done t))
  3796. nil)
  3797. ;; Priorities
  3798. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3799. ;; Tags
  3800. '(org-font-lock-add-tag-faces)
  3801. ;; Special keywords
  3802. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3803. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3804. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3805. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3806. ;; Emphasis
  3807. (if em
  3808. (if (featurep 'xemacs)
  3809. '(org-do-emphasis-faces (0 nil append))
  3810. '(org-do-emphasis-faces)))
  3811. ;; Checkboxes
  3812. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3813. 2 'bold prepend)
  3814. (if org-provide-checkbox-statistics
  3815. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3816. (0 (org-get-checkbox-statistics-face) t)))
  3817. ;; Description list items
  3818. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3819. 2 'bold prepend)
  3820. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3821. '(1 'org-archived prepend))
  3822. ;; Specials
  3823. '(org-do-latex-and-special-faces)
  3824. ;; Code
  3825. '(org-activate-code (1 'org-code t))
  3826. ;; COMMENT
  3827. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3828. "\\|" org-quote-string "\\)\\>")
  3829. '(1 'org-special-keyword t))
  3830. '("^#.*" (0 'font-lock-comment-face t))
  3831. )))
  3832. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3833. ;; Now set the full font-lock-keywords
  3834. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3835. (org-set-local 'font-lock-defaults
  3836. '(org-font-lock-keywords t nil nil backward-paragraph))
  3837. (kill-local-variable 'font-lock-keywords) nil))
  3838. (defvar org-m nil)
  3839. (defvar org-l nil)
  3840. (defvar org-f nil)
  3841. (defun org-get-level-face (n)
  3842. "Get the right face for match N in font-lock matching of headlines."
  3843. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3844. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3845. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3846. (cond
  3847. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3848. ((eq n 2) org-f)
  3849. (t (if org-level-color-stars-only nil org-f))))
  3850. (defun org-get-todo-face (kwd)
  3851. "Get the right face for a TODO keyword KWD.
  3852. If KWD is a number, get the corresponding match group."
  3853. (if (numberp kwd) (setq kwd (match-string kwd)))
  3854. (or (cdr (assoc kwd org-todo-keyword-faces))
  3855. (and (member kwd org-done-keywords) 'org-done)
  3856. 'org-todo))
  3857. (defun org-font-lock-add-tag-faces (limit)
  3858. "Add the special tag faces."
  3859. (when (and org-tag-faces org-tags-special-faces-re)
  3860. (while (re-search-forward org-tags-special-faces-re limit t)
  3861. (add-text-properties (match-beginning 1) (match-end 1)
  3862. (list 'face (org-get-tag-face 1)
  3863. 'font-lock-fontified t))
  3864. (backward-char 1))))
  3865. (defun org-get-tag-face (kwd)
  3866. "Get the right face for a TODO keyword KWD.
  3867. If KWD is a number, get the corresponding match group."
  3868. (if (numberp kwd) (setq kwd (match-string kwd)))
  3869. (or (cdr (assoc kwd org-tag-faces))
  3870. 'org-tag))
  3871. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3872. "Remove fontification and activation overlays from links."
  3873. (font-lock-default-unfontify-region beg end)
  3874. (let* ((buffer-undo-list t)
  3875. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3876. (inhibit-modification-hooks t)
  3877. deactivate-mark buffer-file-name buffer-file-truename)
  3878. (remove-text-properties beg end
  3879. '(mouse-face t keymap t org-linked-text t
  3880. invisible t intangible t))))
  3881. ;;;; Visibility cycling, including org-goto and indirect buffer
  3882. ;;; Cycling
  3883. (defvar org-cycle-global-status nil)
  3884. (make-variable-buffer-local 'org-cycle-global-status)
  3885. (defvar org-cycle-subtree-status nil)
  3886. (make-variable-buffer-local 'org-cycle-subtree-status)
  3887. ;;;###autoload
  3888. (defun org-cycle (&optional arg)
  3889. "Visibility cycling for Org-mode.
  3890. - When this function is called with a prefix argument, rotate the entire
  3891. buffer through 3 states (global cycling)
  3892. 1. OVERVIEW: Show only top-level headlines.
  3893. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3894. 3. SHOW ALL: Show everything.
  3895. When called with two C-u C-u prefixes, switch to the startup visibility,
  3896. determined by the variable `org-startup-folded', and by any VISIBILITY
  3897. properties in the buffer.
  3898. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3899. including drawers.
  3900. - When point is at the beginning of a headline, rotate the subtree started
  3901. by this line through 3 different states (local cycling)
  3902. 1. FOLDED: Only the main headline is shown.
  3903. 2. CHILDREN: The main headline and the direct children are shown.
  3904. From this state, you can move to one of the children
  3905. and zoom in further.
  3906. 3. SUBTREE: Show the entire subtree, including body text.
  3907. - When there is a numeric prefix, go up to a heading with level ARG, do
  3908. a `show-subtree' and return to the previous cursor position. If ARG
  3909. is negative, go up that many levels.
  3910. - When point is not at the beginning of a headline, execute the global
  3911. binding for TAB, which is re-indenting the line. See the option
  3912. `org-cycle-emulate-tab' for details.
  3913. - Special case: if point is at the beginning of the buffer and there is
  3914. no headline in line 1, this function will act as if called with prefix arg.
  3915. But only if also the variable `org-cycle-global-at-bob' is t."
  3916. (interactive "P")
  3917. (org-load-modules-maybe)
  3918. (let* ((outline-regexp
  3919. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3920. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3921. outline-regexp))
  3922. (bob-special (and org-cycle-global-at-bob (bobp)
  3923. (not (looking-at outline-regexp))))
  3924. (org-cycle-hook
  3925. (if bob-special
  3926. (delq 'org-optimize-window-after-visibility-change
  3927. (copy-sequence org-cycle-hook))
  3928. org-cycle-hook))
  3929. (pos (point)))
  3930. (if (or bob-special (equal arg '(4)))
  3931. ;; special case: use global cycling
  3932. (setq arg t))
  3933. (cond
  3934. ((equal arg '(16))
  3935. (org-set-startup-visibility)
  3936. (message "Startup visibility, plus VISIBILITY properties"))
  3937. ((equal arg '(64))
  3938. (show-all)
  3939. (message "Entire buffer visible, including drawers"))
  3940. ((org-at-table-p 'any)
  3941. ;; Enter the table or move to the next field in the table
  3942. (or (org-table-recognize-table.el)
  3943. (progn
  3944. (if arg (org-table-edit-field t)
  3945. (org-table-justify-field-maybe)
  3946. (call-interactively 'org-table-next-field)))))
  3947. ((eq arg t) ;; Global cycling
  3948. (cond
  3949. ((and (eq last-command this-command)
  3950. (eq org-cycle-global-status 'overview))
  3951. ;; We just created the overview - now do table of contents
  3952. ;; This can be slow in very large buffers, so indicate action
  3953. (message "CONTENTS...")
  3954. (org-content)
  3955. (message "CONTENTS...done")
  3956. (setq org-cycle-global-status 'contents)
  3957. (run-hook-with-args 'org-cycle-hook 'contents))
  3958. ((and (eq last-command this-command)
  3959. (eq org-cycle-global-status 'contents))
  3960. ;; We just showed the table of contents - now show everything
  3961. (show-all)
  3962. (message "SHOW ALL")
  3963. (setq org-cycle-global-status 'all)
  3964. (run-hook-with-args 'org-cycle-hook 'all))
  3965. (t
  3966. ;; Default action: go to overview
  3967. (org-overview)
  3968. (message "OVERVIEW")
  3969. (setq org-cycle-global-status 'overview)
  3970. (run-hook-with-args 'org-cycle-hook 'overview))))
  3971. ((and org-drawers org-drawer-regexp
  3972. (save-excursion
  3973. (beginning-of-line 1)
  3974. (looking-at org-drawer-regexp)))
  3975. ;; Toggle block visibility
  3976. (org-flag-drawer
  3977. (not (get-char-property (match-end 0) 'invisible))))
  3978. ((integerp arg)
  3979. ;; Show-subtree, ARG levels up from here.
  3980. (save-excursion
  3981. (org-back-to-heading)
  3982. (outline-up-heading (if (< arg 0) (- arg)
  3983. (- (funcall outline-level) arg)))
  3984. (org-show-subtree)))
  3985. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3986. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3987. ;; At a heading: rotate between three different views
  3988. (org-back-to-heading)
  3989. (let ((goal-column 0) eoh eol eos)
  3990. ;; First, some boundaries
  3991. (save-excursion
  3992. (org-back-to-heading)
  3993. (save-excursion
  3994. (beginning-of-line 2)
  3995. (while (and (not (eobp)) ;; this is like `next-line'
  3996. (get-char-property (1- (point)) 'invisible))
  3997. (beginning-of-line 2)) (setq eol (point)))
  3998. (outline-end-of-heading) (setq eoh (point))
  3999. (org-end-of-subtree t)
  4000. (unless (eobp)
  4001. (skip-chars-forward " \t\n")
  4002. (beginning-of-line 1) ; in case this is an item
  4003. )
  4004. (setq eos (1- (point))))
  4005. ;; Find out what to do next and set `this-command'
  4006. (cond
  4007. ((= eos eoh)
  4008. ;; Nothing is hidden behind this heading
  4009. (message "EMPTY ENTRY")
  4010. (setq org-cycle-subtree-status nil)
  4011. (save-excursion
  4012. (goto-char eos)
  4013. (outline-next-heading)
  4014. (if (org-invisible-p) (org-flag-heading nil))))
  4015. ((or (>= eol eos)
  4016. (not (string-match "\\S-" (buffer-substring eol eos))))
  4017. ;; Entire subtree is hidden in one line: open it
  4018. (org-show-entry)
  4019. (show-children)
  4020. (message "CHILDREN")
  4021. (save-excursion
  4022. (goto-char eos)
  4023. (outline-next-heading)
  4024. (if (org-invisible-p) (org-flag-heading nil)))
  4025. (setq org-cycle-subtree-status 'children)
  4026. (run-hook-with-args 'org-cycle-hook 'children))
  4027. ((and (eq last-command this-command)
  4028. (eq org-cycle-subtree-status 'children))
  4029. ;; We just showed the children, now show everything.
  4030. (org-show-subtree)
  4031. (message "SUBTREE")
  4032. (setq org-cycle-subtree-status 'subtree)
  4033. (run-hook-with-args 'org-cycle-hook 'subtree))
  4034. (t
  4035. ;; Default action: hide the subtree.
  4036. (hide-subtree)
  4037. (message "FOLDED")
  4038. (setq org-cycle-subtree-status 'folded)
  4039. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4040. ;; TAB emulation and template completion
  4041. (buffer-read-only (org-back-to-heading))
  4042. ((org-try-structure-completion))
  4043. ((org-try-cdlatex-tab))
  4044. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4045. (or (not (bolp))
  4046. (not (looking-at outline-regexp))))
  4047. (call-interactively (global-key-binding "\t")))
  4048. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4049. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4050. (or (and (eq org-cycle-emulate-tab 'white)
  4051. (= (match-end 0) (point-at-eol)))
  4052. (and (eq org-cycle-emulate-tab 'whitestart)
  4053. (>= (match-end 0) pos))))
  4054. t
  4055. (eq org-cycle-emulate-tab t))
  4056. (call-interactively (global-key-binding "\t")))
  4057. (t (save-excursion
  4058. (org-back-to-heading)
  4059. (org-cycle))))))
  4060. ;;;###autoload
  4061. (defun org-global-cycle (&optional arg)
  4062. "Cycle the global visibility. For details see `org-cycle'.
  4063. With C-u prefix arg, switch to startup visibility.
  4064. With a numeric prefix, show all headlines up to that level."
  4065. (interactive "P")
  4066. (let ((org-cycle-include-plain-lists
  4067. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4068. (cond
  4069. ((integerp arg)
  4070. (show-all)
  4071. (hide-sublevels arg)
  4072. (setq org-cycle-global-status 'contents))
  4073. ((equal arg '(4))
  4074. (org-set-startup-visibility)
  4075. (message "Startup visibility, plus VISIBILITY properties."))
  4076. (t
  4077. (org-cycle '(4))))))
  4078. (defun org-set-startup-visibility ()
  4079. "Set the visibility required by startup options and properties."
  4080. (cond
  4081. ((eq org-startup-folded t)
  4082. (org-cycle '(4)))
  4083. ((eq org-startup-folded 'content)
  4084. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4085. (org-cycle '(4)) (org-cycle '(4)))))
  4086. (org-set-visibility-according-to-property 'no-cleanup)
  4087. (org-cycle-hide-archived-subtrees 'all)
  4088. (org-cycle-hide-drawers 'all)
  4089. (org-cycle-show-empty-lines 'all))
  4090. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4091. "Switch subtree visibilities according to :VISIBILITY: property."
  4092. (interactive)
  4093. (let (org-show-entry-below state)
  4094. (save-excursion
  4095. (goto-char (point-min))
  4096. (while (re-search-forward
  4097. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4098. nil t)
  4099. (setq state (match-string 1))
  4100. (save-excursion
  4101. (org-back-to-heading t)
  4102. (hide-subtree)
  4103. (org-reveal)
  4104. (cond
  4105. ((equal state '("fold" "folded"))
  4106. (hide-subtree))
  4107. ((equal state "children")
  4108. (org-show-hidden-entry)
  4109. (show-children))
  4110. ((equal state "content")
  4111. (save-excursion
  4112. (save-restriction
  4113. (org-narrow-to-subtree)
  4114. (org-content))))
  4115. ((member state '("all" "showall"))
  4116. (show-subtree)))))
  4117. (unless no-cleanup
  4118. (org-cycle-hide-archived-subtrees 'all)
  4119. (org-cycle-hide-drawers 'all)
  4120. (org-cycle-show-empty-lines 'all)))))
  4121. (defun org-overview ()
  4122. "Switch to overview mode, showing only top-level headlines.
  4123. Really, this shows all headlines with level equal or greater than the level
  4124. of the first headline in the buffer. This is important, because if the
  4125. first headline is not level one, then (hide-sublevels 1) gives confusing
  4126. results."
  4127. (interactive)
  4128. (let ((level (save-excursion
  4129. (goto-char (point-min))
  4130. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4131. (progn
  4132. (goto-char (match-beginning 0))
  4133. (funcall outline-level))))))
  4134. (and level (hide-sublevels level))))
  4135. (defun org-content (&optional arg)
  4136. "Show all headlines in the buffer, like a table of contents.
  4137. With numerical argument N, show content up to level N."
  4138. (interactive "P")
  4139. (save-excursion
  4140. ;; Visit all headings and show their offspring
  4141. (and (integerp arg) (org-overview))
  4142. (goto-char (point-max))
  4143. (catch 'exit
  4144. (while (and (progn (condition-case nil
  4145. (outline-previous-visible-heading 1)
  4146. (error (goto-char (point-min))))
  4147. t)
  4148. (looking-at outline-regexp))
  4149. (if (integerp arg)
  4150. (show-children (1- arg))
  4151. (show-branches))
  4152. (if (bobp) (throw 'exit nil))))))
  4153. (defun org-optimize-window-after-visibility-change (state)
  4154. "Adjust the window after a change in outline visibility.
  4155. This function is the default value of the hook `org-cycle-hook'."
  4156. (when (get-buffer-window (current-buffer))
  4157. (cond
  4158. ((eq state 'content) nil)
  4159. ((eq state 'all) nil)
  4160. ((eq state 'folded) nil)
  4161. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4162. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4163. (defun org-compact-display-after-subtree-move ()
  4164. "Show a compacter version of the tree of the entry's parent."
  4165. (save-excursion
  4166. (if (org-up-heading-safe)
  4167. (progn
  4168. (hide-subtree)
  4169. (show-entry)
  4170. (show-children)
  4171. (org-cycle-show-empty-lines 'children)
  4172. (org-cycle-hide-drawers 'children))
  4173. (org-overview))))
  4174. (defun org-cycle-show-empty-lines (state)
  4175. "Show empty lines above all visible headlines.
  4176. The region to be covered depends on STATE when called through
  4177. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4178. entire buffer covered. Note that an empty line is only shown if there
  4179. are at least `org-cycle-separator-lines' empty lines before the headline."
  4180. (when (> org-cycle-separator-lines 0)
  4181. (save-excursion
  4182. (let* ((n org-cycle-separator-lines)
  4183. (re (cond
  4184. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4185. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4186. (t (let ((ns (number-to-string (- n 2))))
  4187. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4188. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4189. beg end)
  4190. (cond
  4191. ((memq state '(overview contents t))
  4192. (setq beg (point-min) end (point-max)))
  4193. ((memq state '(children folded))
  4194. (setq beg (point) end (progn (org-end-of-subtree t t)
  4195. (beginning-of-line 2)
  4196. (point)))))
  4197. (when beg
  4198. (goto-char beg)
  4199. (while (re-search-forward re end t)
  4200. (if (not (get-char-property (match-end 1) 'invisible))
  4201. (outline-flag-region
  4202. (match-beginning 1) (match-end 1) nil)))))))
  4203. ;; Never hide empty lines at the end of the file.
  4204. (save-excursion
  4205. (goto-char (point-max))
  4206. (outline-previous-heading)
  4207. (outline-end-of-heading)
  4208. (if (and (looking-at "[ \t\n]+")
  4209. (= (match-end 0) (point-max)))
  4210. (outline-flag-region (point) (match-end 0) nil))))
  4211. (defun org-show-empty-lines-in-parent ()
  4212. "Move to the parent and re-show empty lines before visible headlines."
  4213. (save-excursion
  4214. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4215. (org-cycle-show-empty-lines context))))
  4216. (defun org-cycle-hide-drawers (state)
  4217. "Re-hide all drawers after a visibility state change."
  4218. (when (and (org-mode-p)
  4219. (not (memq state '(overview folded))))
  4220. (save-excursion
  4221. (let* ((globalp (memq state '(contents all)))
  4222. (beg (if globalp (point-min) (point)))
  4223. (end (if globalp (point-max) (org-end-of-subtree t))))
  4224. (goto-char beg)
  4225. (while (re-search-forward org-drawer-regexp end t)
  4226. (org-flag-drawer t))))))
  4227. (defun org-flag-drawer (flag)
  4228. (save-excursion
  4229. (beginning-of-line 1)
  4230. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4231. (let ((b (match-end 0))
  4232. (outline-regexp org-outline-regexp))
  4233. (if (re-search-forward
  4234. "^[ \t]*:END:"
  4235. (save-excursion (outline-next-heading) (point)) t)
  4236. (outline-flag-region b (point-at-eol) flag)
  4237. (error ":END: line missing"))))))
  4238. (defun org-subtree-end-visible-p ()
  4239. "Is the end of the current subtree visible?"
  4240. (pos-visible-in-window-p
  4241. (save-excursion (org-end-of-subtree t) (point))))
  4242. (defun org-first-headline-recenter (&optional N)
  4243. "Move cursor to the first headline and recenter the headline.
  4244. Optional argument N means, put the headline into the Nth line of the window."
  4245. (goto-char (point-min))
  4246. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4247. (beginning-of-line)
  4248. (recenter (prefix-numeric-value N))))
  4249. ;;; Org-goto
  4250. (defvar org-goto-window-configuration nil)
  4251. (defvar org-goto-marker nil)
  4252. (defvar org-goto-map
  4253. (let ((map (make-sparse-keymap)))
  4254. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4255. (while (setq cmd (pop cmds))
  4256. (substitute-key-definition cmd cmd map global-map)))
  4257. (suppress-keymap map)
  4258. (org-defkey map "\C-m" 'org-goto-ret)
  4259. (org-defkey map [(return)] 'org-goto-ret)
  4260. (org-defkey map [(left)] 'org-goto-left)
  4261. (org-defkey map [(right)] 'org-goto-right)
  4262. (org-defkey map [(control ?g)] 'org-goto-quit)
  4263. (org-defkey map "\C-i" 'org-cycle)
  4264. (org-defkey map [(tab)] 'org-cycle)
  4265. (org-defkey map [(down)] 'outline-next-visible-heading)
  4266. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4267. (if org-goto-auto-isearch
  4268. (if (fboundp 'define-key-after)
  4269. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4270. nil)
  4271. (org-defkey map "q" 'org-goto-quit)
  4272. (org-defkey map "n" 'outline-next-visible-heading)
  4273. (org-defkey map "p" 'outline-previous-visible-heading)
  4274. (org-defkey map "f" 'outline-forward-same-level)
  4275. (org-defkey map "b" 'outline-backward-same-level)
  4276. (org-defkey map "u" 'outline-up-heading))
  4277. (org-defkey map "/" 'org-occur)
  4278. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4279. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4280. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4281. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4282. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4283. map))
  4284. (defconst org-goto-help
  4285. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4286. RET=jump to location [Q]uit and return to previous location
  4287. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4288. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4289. ;; FIXME: Docstring doe not mention both interfaces
  4290. (defun org-goto (&optional alternative-interface)
  4291. "Look up a different location in the current file, keeping current visibility.
  4292. When you want look-up or go to a different location in a document, the
  4293. fastest way is often to fold the entire buffer and then dive into the tree.
  4294. This method has the disadvantage, that the previous location will be folded,
  4295. which may not be what you want.
  4296. This command works around this by showing a copy of the current buffer
  4297. in an indirect buffer, in overview mode. You can dive into the tree in
  4298. that copy, use org-occur and incremental search to find a location.
  4299. When pressing RET or `Q', the command returns to the original buffer in
  4300. which the visibility is still unchanged. After RET is will also jump to
  4301. the location selected in the indirect buffer and expose the
  4302. the headline hierarchy above."
  4303. (interactive "P")
  4304. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4305. (org-refile-use-outline-path t)
  4306. (interface
  4307. (if (not alternative-interface)
  4308. org-goto-interface
  4309. (if (eq org-goto-interface 'outline)
  4310. 'outline-path-completion
  4311. 'outline)))
  4312. (org-goto-start-pos (point))
  4313. (selected-point
  4314. (if (eq interface 'outline)
  4315. (car (org-get-location (current-buffer) org-goto-help))
  4316. (nth 3 (org-refile-get-location "Goto: ")))))
  4317. (if selected-point
  4318. (progn
  4319. (org-mark-ring-push org-goto-start-pos)
  4320. (goto-char selected-point)
  4321. (if (or (org-invisible-p) (org-invisible-p2))
  4322. (org-show-context 'org-goto)))
  4323. (message "Quit"))))
  4324. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4325. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4326. (defvar org-goto-local-auto-isearch-map) ; defined below
  4327. (defun org-get-location (buf help)
  4328. "Let the user select a location in the Org-mode buffer BUF.
  4329. This function uses a recursive edit. It returns the selected position
  4330. or nil."
  4331. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4332. (isearch-hide-immediately nil)
  4333. (isearch-search-fun-function
  4334. (lambda () 'org-goto-local-search-headings))
  4335. (org-goto-selected-point org-goto-exit-command))
  4336. (save-excursion
  4337. (save-window-excursion
  4338. (delete-other-windows)
  4339. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4340. (switch-to-buffer
  4341. (condition-case nil
  4342. (make-indirect-buffer (current-buffer) "*org-goto*")
  4343. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4344. (with-output-to-temp-buffer "*Help*"
  4345. (princ help))
  4346. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4347. (setq buffer-read-only nil)
  4348. (let ((org-startup-truncated t)
  4349. (org-startup-folded nil)
  4350. (org-startup-align-all-tables nil))
  4351. (org-mode)
  4352. (org-overview))
  4353. (setq buffer-read-only t)
  4354. (if (and (boundp 'org-goto-start-pos)
  4355. (integer-or-marker-p org-goto-start-pos))
  4356. (let ((org-show-hierarchy-above t)
  4357. (org-show-siblings t)
  4358. (org-show-following-heading t))
  4359. (goto-char org-goto-start-pos)
  4360. (and (org-invisible-p) (org-show-context)))
  4361. (goto-char (point-min)))
  4362. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4363. (message "Select location and press RET")
  4364. (use-local-map org-goto-map)
  4365. (recursive-edit)
  4366. ))
  4367. (kill-buffer "*org-goto*")
  4368. (cons org-goto-selected-point org-goto-exit-command)))
  4369. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4370. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4371. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4372. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4373. (defun org-goto-local-search-headings (string bound noerror)
  4374. "Search and make sure that any matches are in headlines."
  4375. (catch 'return
  4376. (while (if isearch-forward
  4377. (search-forward string bound noerror)
  4378. (search-backward string bound noerror))
  4379. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4380. (and (member :headline context)
  4381. (not (member :tags context))))
  4382. (throw 'return (point))))))
  4383. (defun org-goto-local-auto-isearch ()
  4384. "Start isearch."
  4385. (interactive)
  4386. (goto-char (point-min))
  4387. (let ((keys (this-command-keys)))
  4388. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4389. (isearch-mode t)
  4390. (isearch-process-search-char (string-to-char keys)))))
  4391. (defun org-goto-ret (&optional arg)
  4392. "Finish `org-goto' by going to the new location."
  4393. (interactive "P")
  4394. (setq org-goto-selected-point (point)
  4395. org-goto-exit-command 'return)
  4396. (throw 'exit nil))
  4397. (defun org-goto-left ()
  4398. "Finish `org-goto' by going to the new location."
  4399. (interactive)
  4400. (if (org-on-heading-p)
  4401. (progn
  4402. (beginning-of-line 1)
  4403. (setq org-goto-selected-point (point)
  4404. org-goto-exit-command 'left)
  4405. (throw 'exit nil))
  4406. (error "Not on a heading")))
  4407. (defun org-goto-right ()
  4408. "Finish `org-goto' by going to the new location."
  4409. (interactive)
  4410. (if (org-on-heading-p)
  4411. (progn
  4412. (setq org-goto-selected-point (point)
  4413. org-goto-exit-command 'right)
  4414. (throw 'exit nil))
  4415. (error "Not on a heading")))
  4416. (defun org-goto-quit ()
  4417. "Finish `org-goto' without cursor motion."
  4418. (interactive)
  4419. (setq org-goto-selected-point nil)
  4420. (setq org-goto-exit-command 'quit)
  4421. (throw 'exit nil))
  4422. ;;; Indirect buffer display of subtrees
  4423. (defvar org-indirect-dedicated-frame nil
  4424. "This is the frame being used for indirect tree display.")
  4425. (defvar org-last-indirect-buffer nil)
  4426. (defun org-tree-to-indirect-buffer (&optional arg)
  4427. "Create indirect buffer and narrow it to current subtree.
  4428. With numerical prefix ARG, go up to this level and then take that tree.
  4429. If ARG is negative, go up that many levels.
  4430. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4431. indirect buffer previously made with this command, to avoid proliferation of
  4432. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4433. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4434. is kept so that you can work with several indirect buffers at the same time.
  4435. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4436. requests that a new frame be made for the new buffer, so that the dedicated
  4437. frame is not changed."
  4438. (interactive "P")
  4439. (let ((cbuf (current-buffer))
  4440. (cwin (selected-window))
  4441. (pos (point))
  4442. beg end level heading ibuf)
  4443. (save-excursion
  4444. (org-back-to-heading t)
  4445. (when (numberp arg)
  4446. (setq level (org-outline-level))
  4447. (if (< arg 0) (setq arg (+ level arg)))
  4448. (while (> (setq level (org-outline-level)) arg)
  4449. (outline-up-heading 1 t)))
  4450. (setq beg (point)
  4451. heading (org-get-heading))
  4452. (org-end-of-subtree t) (setq end (point)))
  4453. (if (and (buffer-live-p org-last-indirect-buffer)
  4454. (not (eq org-indirect-buffer-display 'new-frame))
  4455. (not arg))
  4456. (kill-buffer org-last-indirect-buffer))
  4457. (setq ibuf (org-get-indirect-buffer cbuf)
  4458. org-last-indirect-buffer ibuf)
  4459. (cond
  4460. ((or (eq org-indirect-buffer-display 'new-frame)
  4461. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4462. (select-frame (make-frame))
  4463. (delete-other-windows)
  4464. (switch-to-buffer ibuf)
  4465. (org-set-frame-title heading))
  4466. ((eq org-indirect-buffer-display 'dedicated-frame)
  4467. (raise-frame
  4468. (select-frame (or (and org-indirect-dedicated-frame
  4469. (frame-live-p org-indirect-dedicated-frame)
  4470. org-indirect-dedicated-frame)
  4471. (setq org-indirect-dedicated-frame (make-frame)))))
  4472. (delete-other-windows)
  4473. (switch-to-buffer ibuf)
  4474. (org-set-frame-title (concat "Indirect: " heading)))
  4475. ((eq org-indirect-buffer-display 'current-window)
  4476. (switch-to-buffer ibuf))
  4477. ((eq org-indirect-buffer-display 'other-window)
  4478. (pop-to-buffer ibuf))
  4479. (t (error "Invalid value.")))
  4480. (if (featurep 'xemacs)
  4481. (save-excursion (org-mode) (turn-on-font-lock)))
  4482. (narrow-to-region beg end)
  4483. (show-all)
  4484. (goto-char pos)
  4485. (and (window-live-p cwin) (select-window cwin))))
  4486. (defun org-get-indirect-buffer (&optional buffer)
  4487. (setq buffer (or buffer (current-buffer)))
  4488. (let ((n 1) (base (buffer-name buffer)) bname)
  4489. (while (buffer-live-p
  4490. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4491. (setq n (1+ n)))
  4492. (condition-case nil
  4493. (make-indirect-buffer buffer bname 'clone)
  4494. (error (make-indirect-buffer buffer bname)))))
  4495. (defun org-set-frame-title (title)
  4496. "Set the title of the current frame to the string TITLE."
  4497. ;; FIXME: how to name a single frame in XEmacs???
  4498. (unless (featurep 'xemacs)
  4499. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4500. ;;;; Structure editing
  4501. ;;; Inserting headlines
  4502. (defun org-previous-line-empty-p ()
  4503. (save-excursion
  4504. (and (not (bobp))
  4505. (or (beginning-of-line 0) t)
  4506. (save-match-data
  4507. (looking-at "[ \t]*$")))))
  4508. (defun org-insert-heading (&optional force-heading)
  4509. "Insert a new heading or item with same depth at point.
  4510. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4511. If point is at the beginning of a headline, insert a sibling before the
  4512. current headline. If point is not at the beginning, do not split the line,
  4513. but create the new headline after the current line."
  4514. (interactive "P")
  4515. (if (= (buffer-size) 0)
  4516. (insert "\n* ")
  4517. (when (or force-heading (not (org-insert-item)))
  4518. (let* ((empty-line-p nil)
  4519. (head (save-excursion
  4520. (condition-case nil
  4521. (progn
  4522. (org-back-to-heading)
  4523. (setq empty-line-p (org-previous-line-empty-p))
  4524. (match-string 0))
  4525. (error "*"))))
  4526. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4527. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4528. pos hide-previous previous-pos)
  4529. (cond
  4530. ((and (org-on-heading-p) (bolp)
  4531. (or (bobp)
  4532. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4533. ;; insert before the current line
  4534. (open-line (if blank 2 1)))
  4535. ((and (bolp)
  4536. (or (bobp)
  4537. (save-excursion
  4538. (backward-char 1) (not (org-invisible-p)))))
  4539. ;; insert right here
  4540. nil)
  4541. (t
  4542. ;; somewhere in the line
  4543. (save-excursion
  4544. (setq previous-pos (point-at-bol))
  4545. (end-of-line)
  4546. (setq hide-previous (org-invisible-p)))
  4547. (and org-insert-heading-respect-content (org-show-subtree))
  4548. (let ((split
  4549. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4550. (save-excursion
  4551. (let ((p (point)))
  4552. (goto-char (point-at-bol))
  4553. (and (looking-at org-complex-heading-regexp)
  4554. (> p (match-beginning 4)))))))
  4555. tags pos)
  4556. (cond
  4557. (org-insert-heading-respect-content
  4558. (org-end-of-subtree nil t)
  4559. (or (bolp) (newline))
  4560. (or (org-previous-line-empty-p)
  4561. (and blank (newline)))
  4562. (open-line 1))
  4563. ((org-on-heading-p)
  4564. (when hide-previous
  4565. (show-children)
  4566. (org-show-entry))
  4567. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4568. (setq tags (and (match-end 2) (match-string 2)))
  4569. (and (match-end 1)
  4570. (delete-region (match-beginning 1) (match-end 1)))
  4571. (setq pos (point-at-bol))
  4572. (or split (end-of-line 1))
  4573. (delete-horizontal-space)
  4574. (newline (if blank 2 1))
  4575. (when tags
  4576. (save-excursion
  4577. (goto-char pos)
  4578. (end-of-line 1)
  4579. (insert " " tags)
  4580. (org-set-tags nil 'align))))
  4581. (t
  4582. (or split (end-of-line 1))
  4583. (newline (if blank 2 1)))))))
  4584. (insert head) (just-one-space)
  4585. (setq pos (point))
  4586. (end-of-line 1)
  4587. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4588. (when (and org-insert-heading-respect-content hide-previous)
  4589. (save-excursion
  4590. (goto-char previous-pos)
  4591. (hide-subtree)))
  4592. (run-hooks 'org-insert-heading-hook)))))
  4593. (defun org-get-heading (&optional no-tags)
  4594. "Return the heading of the current entry, without the stars."
  4595. (save-excursion
  4596. (org-back-to-heading t)
  4597. (if (looking-at
  4598. (if no-tags
  4599. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4600. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4601. (match-string 1) "")))
  4602. (defun org-heading-components ()
  4603. "Return the components of the current heading.
  4604. This is a list with the following elements:
  4605. - the level as an integer
  4606. - the reduced level, different if `org-odd-levels-only' is set.
  4607. - the TODO keyword, or nil
  4608. - the priority character, like ?A, or nil if no priority is given
  4609. - the headline text itself, or the tags string if no headline text
  4610. - the tags string, or nil."
  4611. (save-excursion
  4612. (org-back-to-heading t)
  4613. (if (looking-at org-complex-heading-regexp)
  4614. (list (length (match-string 1))
  4615. (org-reduced-level (length (match-string 1)))
  4616. (org-match-string-no-properties 2)
  4617. (and (match-end 3) (aref (match-string 3) 2))
  4618. (org-match-string-no-properties 4)
  4619. (org-match-string-no-properties 5)))))
  4620. (defun org-insert-heading-after-current ()
  4621. "Insert a new heading with same level as current, after current subtree."
  4622. (interactive)
  4623. (org-back-to-heading)
  4624. (org-insert-heading)
  4625. (org-move-subtree-down)
  4626. (end-of-line 1))
  4627. (defun org-insert-heading-respect-content ()
  4628. (interactive)
  4629. (let ((org-insert-heading-respect-content t))
  4630. (org-insert-heading t)))
  4631. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4632. (interactive "P")
  4633. (let ((org-insert-heading-respect-content t))
  4634. (org-insert-todo-heading force-state t)))
  4635. (defun org-insert-todo-heading (arg &optional force-heading)
  4636. "Insert a new heading with the same level and TODO state as current heading.
  4637. If the heading has no TODO state, or if the state is DONE, use the first
  4638. state (TODO by default). Also with prefix arg, force first state."
  4639. (interactive "P")
  4640. (when (or force-heading (not (org-insert-item 'checkbox)))
  4641. (org-insert-heading force-heading)
  4642. (save-excursion
  4643. (org-back-to-heading)
  4644. (outline-previous-heading)
  4645. (looking-at org-todo-line-regexp))
  4646. (let*
  4647. ((new-mark-x
  4648. (if (or arg
  4649. (not (match-beginning 2))
  4650. (member (match-string 2) org-done-keywords))
  4651. (car org-todo-keywords-1)
  4652. (match-string 2)))
  4653. (new-mark
  4654. (or
  4655. (run-hook-with-args-until-success
  4656. 'org-todo-get-default-hook new-mark-x nil)
  4657. new-mark-x)))
  4658. (insert new-mark " "))
  4659. (when org-provide-todo-statistics
  4660. (org-update-parent-todo-statistics))))
  4661. (defun org-insert-subheading (arg)
  4662. "Insert a new subheading and demote it.
  4663. Works for outline headings and for plain lists alike."
  4664. (interactive "P")
  4665. (org-insert-heading arg)
  4666. (cond
  4667. ((org-on-heading-p) (org-do-demote))
  4668. ((org-at-item-p) (org-indent-item 1))))
  4669. (defun org-insert-todo-subheading (arg)
  4670. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4671. Works for outline headings and for plain lists alike."
  4672. (interactive "P")
  4673. (org-insert-todo-heading arg)
  4674. (cond
  4675. ((org-on-heading-p) (org-do-demote))
  4676. ((org-at-item-p) (org-indent-item 1))))
  4677. ;;; Promotion and Demotion
  4678. (defun org-promote-subtree ()
  4679. "Promote the entire subtree.
  4680. See also `org-promote'."
  4681. (interactive)
  4682. (save-excursion
  4683. (org-map-tree 'org-promote))
  4684. (org-fix-position-after-promote))
  4685. (defun org-demote-subtree ()
  4686. "Demote the entire subtree. See `org-demote'.
  4687. See also `org-promote'."
  4688. (interactive)
  4689. (save-excursion
  4690. (org-map-tree 'org-demote))
  4691. (org-fix-position-after-promote))
  4692. (defun org-do-promote ()
  4693. "Promote the current heading higher up the tree.
  4694. If the region is active in `transient-mark-mode', promote all headings
  4695. in the region."
  4696. (interactive)
  4697. (save-excursion
  4698. (if (org-region-active-p)
  4699. (org-map-region 'org-promote (region-beginning) (region-end))
  4700. (org-promote)))
  4701. (org-fix-position-after-promote))
  4702. (defun org-do-demote ()
  4703. "Demote the current heading lower down the tree.
  4704. If the region is active in `transient-mark-mode', demote all headings
  4705. in the region."
  4706. (interactive)
  4707. (save-excursion
  4708. (if (org-region-active-p)
  4709. (org-map-region 'org-demote (region-beginning) (region-end))
  4710. (org-demote)))
  4711. (org-fix-position-after-promote))
  4712. (defun org-fix-position-after-promote ()
  4713. "Make sure that after pro/demotion cursor position is right."
  4714. (let ((pos (point)))
  4715. (when (save-excursion
  4716. (beginning-of-line 1)
  4717. (looking-at org-todo-line-regexp)
  4718. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4719. (cond ((eobp) (insert " "))
  4720. ((eolp) (insert " "))
  4721. ((equal (char-after) ?\ ) (forward-char 1))))))
  4722. (defun org-reduced-level (l)
  4723. "Compute the effective level of a heading.
  4724. This takes into account the setting of `org-odd-levels-only'."
  4725. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4726. (defun org-get-valid-level (level &optional change)
  4727. "Rectify a level change under the influence of `org-odd-levels-only'
  4728. LEVEL is a current level, CHANGE is by how much the level should be
  4729. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4730. even level numbers will become the next higher odd number."
  4731. (if org-odd-levels-only
  4732. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4733. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4734. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4735. (max 1 (+ level change))))
  4736. (if (boundp 'define-obsolete-function-alias)
  4737. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4738. (define-obsolete-function-alias 'org-get-legal-level
  4739. 'org-get-valid-level)
  4740. (define-obsolete-function-alias 'org-get-legal-level
  4741. 'org-get-valid-level "23.1")))
  4742. (defun org-promote ()
  4743. "Promote the current heading higher up the tree.
  4744. If the region is active in `transient-mark-mode', promote all headings
  4745. in the region."
  4746. (org-back-to-heading t)
  4747. (let* ((level (save-match-data (funcall outline-level)))
  4748. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4749. (diff (abs (- level (length up-head) -1))))
  4750. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4751. (replace-match up-head nil t)
  4752. ;; Fixup tag positioning
  4753. (and org-auto-align-tags (org-set-tags nil t))
  4754. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4755. (defun org-demote ()
  4756. "Demote the current heading lower down the tree.
  4757. If the region is active in `transient-mark-mode', demote all headings
  4758. in the region."
  4759. (org-back-to-heading t)
  4760. (let* ((level (save-match-data (funcall outline-level)))
  4761. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4762. (diff (abs (- level (length down-head) -1))))
  4763. (replace-match down-head nil t)
  4764. ;; Fixup tag positioning
  4765. (and org-auto-align-tags (org-set-tags nil t))
  4766. (if org-adapt-indentation (org-fixup-indentation diff))))
  4767. (defun org-map-tree (fun)
  4768. "Call FUN for every heading underneath the current one."
  4769. (org-back-to-heading)
  4770. (let ((level (funcall outline-level)))
  4771. (save-excursion
  4772. (funcall fun)
  4773. (while (and (progn
  4774. (outline-next-heading)
  4775. (> (funcall outline-level) level))
  4776. (not (eobp)))
  4777. (funcall fun)))))
  4778. (defun org-map-region (fun beg end)
  4779. "Call FUN for every heading between BEG and END."
  4780. (let ((org-ignore-region t))
  4781. (save-excursion
  4782. (setq end (copy-marker end))
  4783. (goto-char beg)
  4784. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4785. (< (point) end))
  4786. (funcall fun))
  4787. (while (and (progn
  4788. (outline-next-heading)
  4789. (< (point) end))
  4790. (not (eobp)))
  4791. (funcall fun)))))
  4792. (defun org-fixup-indentation (diff)
  4793. "Change the indentation in the current entry by DIFF
  4794. However, if any line in the current entry has no indentation, or if it
  4795. would end up with no indentation after the change, nothing at all is done."
  4796. (save-excursion
  4797. (let ((end (save-excursion (outline-next-heading)
  4798. (point-marker)))
  4799. (prohibit (if (> diff 0)
  4800. "^\\S-"
  4801. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4802. col)
  4803. (unless (save-excursion (end-of-line 1)
  4804. (re-search-forward prohibit end t))
  4805. (while (and (< (point) end)
  4806. (re-search-forward "^[ \t]+" end t))
  4807. (goto-char (match-end 0))
  4808. (setq col (current-column))
  4809. (if (< diff 0) (replace-match ""))
  4810. (org-indent-to-column (+ diff col))))
  4811. (move-marker end nil))))
  4812. (defun org-convert-to-odd-levels ()
  4813. "Convert an org-mode file with all levels allowed to one with odd levels.
  4814. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4815. level 5 etc."
  4816. (interactive)
  4817. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4818. (let ((org-odd-levels-only nil) n)
  4819. (save-excursion
  4820. (goto-char (point-min))
  4821. (while (re-search-forward "^\\*\\*+ " nil t)
  4822. (setq n (- (length (match-string 0)) 2))
  4823. (while (>= (setq n (1- n)) 0)
  4824. (org-demote))
  4825. (end-of-line 1))))))
  4826. (defun org-convert-to-oddeven-levels ()
  4827. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4828. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4829. section with an even level, conversion would destroy the structure of the file. An error
  4830. is signaled in this case."
  4831. (interactive)
  4832. (goto-char (point-min))
  4833. ;; First check if there are no even levels
  4834. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4835. (org-show-context t)
  4836. (error "Not all levels are odd in this file. Conversion not possible."))
  4837. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4838. (let ((org-odd-levels-only nil) n)
  4839. (save-excursion
  4840. (goto-char (point-min))
  4841. (while (re-search-forward "^\\*\\*+ " nil t)
  4842. (setq n (/ (1- (length (match-string 0))) 2))
  4843. (while (>= (setq n (1- n)) 0)
  4844. (org-promote))
  4845. (end-of-line 1))))))
  4846. (defun org-tr-level (n)
  4847. "Make N odd if required."
  4848. (if org-odd-levels-only (1+ (/ n 2)) n))
  4849. ;;; Vertical tree motion, cutting and pasting of subtrees
  4850. (defun org-move-subtree-up (&optional arg)
  4851. "Move the current subtree up past ARG headlines of the same level."
  4852. (interactive "p")
  4853. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4854. (defun org-move-subtree-down (&optional arg)
  4855. "Move the current subtree down past ARG headlines of the same level."
  4856. (interactive "p")
  4857. (setq arg (prefix-numeric-value arg))
  4858. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4859. 'outline-get-last-sibling))
  4860. (ins-point (make-marker))
  4861. (cnt (abs arg))
  4862. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4863. ;; Select the tree
  4864. (org-back-to-heading)
  4865. (setq beg0 (point))
  4866. (save-excursion
  4867. (setq ne-beg (org-back-over-empty-lines))
  4868. (setq beg (point)))
  4869. (save-match-data
  4870. (save-excursion (outline-end-of-heading)
  4871. (setq folded (org-invisible-p)))
  4872. (outline-end-of-subtree))
  4873. (outline-next-heading)
  4874. (setq ne-end (org-back-over-empty-lines))
  4875. (setq end (point))
  4876. (goto-char beg0)
  4877. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4878. ;; include less whitespace
  4879. (save-excursion
  4880. (goto-char beg)
  4881. (forward-line (- ne-beg ne-end))
  4882. (setq beg (point))))
  4883. ;; Find insertion point, with error handling
  4884. (while (> cnt 0)
  4885. (or (and (funcall movfunc) (looking-at outline-regexp))
  4886. (progn (goto-char beg0)
  4887. (error "Cannot move past superior level or buffer limit")))
  4888. (setq cnt (1- cnt)))
  4889. (if (> arg 0)
  4890. ;; Moving forward - still need to move over subtree
  4891. (progn (org-end-of-subtree t t)
  4892. (save-excursion
  4893. (org-back-over-empty-lines)
  4894. (or (bolp) (newline)))))
  4895. (setq ne-ins (org-back-over-empty-lines))
  4896. (move-marker ins-point (point))
  4897. (setq txt (buffer-substring beg end))
  4898. (org-save-markers-in-region beg end)
  4899. (delete-region beg end)
  4900. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4901. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4902. (let ((bbb (point)))
  4903. (insert-before-markers txt)
  4904. (org-reinstall-markers-in-region bbb)
  4905. (move-marker ins-point bbb))
  4906. (or (bolp) (insert "\n"))
  4907. (setq ins-end (point))
  4908. (goto-char ins-point)
  4909. (org-skip-whitespace)
  4910. (when (and (< arg 0)
  4911. (org-first-sibling-p)
  4912. (> ne-ins ne-beg))
  4913. ;; Move whitespace back to beginning
  4914. (save-excursion
  4915. (goto-char ins-end)
  4916. (let ((kill-whole-line t))
  4917. (kill-line (- ne-ins ne-beg)) (point)))
  4918. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4919. (move-marker ins-point nil)
  4920. (org-compact-display-after-subtree-move)
  4921. (org-show-empty-lines-in-parent)
  4922. (unless folded
  4923. (org-show-entry)
  4924. (show-children)
  4925. (org-cycle-hide-drawers 'children))))
  4926. (defvar org-subtree-clip ""
  4927. "Clipboard for cut and paste of subtrees.
  4928. This is actually only a copy of the kill, because we use the normal kill
  4929. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4930. (defvar org-subtree-clip-folded nil
  4931. "Was the last copied subtree folded?
  4932. This is used to fold the tree back after pasting.")
  4933. (defun org-cut-subtree (&optional n)
  4934. "Cut the current subtree into the clipboard.
  4935. With prefix arg N, cut this many sequential subtrees.
  4936. This is a short-hand for marking the subtree and then cutting it."
  4937. (interactive "p")
  4938. (org-copy-subtree n 'cut))
  4939. (defun org-copy-subtree (&optional n cut force-store-markers)
  4940. "Cut the current subtree into the clipboard.
  4941. With prefix arg N, cut this many sequential subtrees.
  4942. This is a short-hand for marking the subtree and then copying it.
  4943. If CUT is non-nil, actually cut the subtree.
  4944. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4945. of some markers in the region, even if CUT is non-nil. This is
  4946. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4947. (interactive "p")
  4948. (let (beg end folded (beg0 (point)))
  4949. (if (interactive-p)
  4950. (org-back-to-heading nil) ; take what looks like a subtree
  4951. (org-back-to-heading t)) ; take what is really there
  4952. (org-back-over-empty-lines)
  4953. (setq beg (point))
  4954. (skip-chars-forward " \t\r\n")
  4955. (save-match-data
  4956. (save-excursion (outline-end-of-heading)
  4957. (setq folded (org-invisible-p)))
  4958. (condition-case nil
  4959. (outline-forward-same-level (1- n))
  4960. (error nil))
  4961. (org-end-of-subtree t t))
  4962. (org-back-over-empty-lines)
  4963. (setq end (point))
  4964. (goto-char beg0)
  4965. (when (> end beg)
  4966. (setq org-subtree-clip-folded folded)
  4967. (when (or cut force-store-markers)
  4968. (org-save-markers-in-region beg end))
  4969. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4970. (setq org-subtree-clip (current-kill 0))
  4971. (message "%s: Subtree(s) with %d characters"
  4972. (if cut "Cut" "Copied")
  4973. (length org-subtree-clip)))))
  4974. (defun org-paste-subtree (&optional level tree for-yank)
  4975. "Paste the clipboard as a subtree, with modification of headline level.
  4976. The entire subtree is promoted or demoted in order to match a new headline
  4977. level.
  4978. If the cursor is at the beginning of a headline, the same level as
  4979. that headline is used to paste the tree
  4980. If not, the new level is derived from the *visible* headings
  4981. before and after the insertion point, and taken to be the inferior headline
  4982. level of the two. So if the previous visible heading is level 3 and the
  4983. next is level 4 (or vice versa), level 4 will be used for insertion.
  4984. This makes sure that the subtree remains an independent subtree and does
  4985. not swallow low level entries.
  4986. You can also force a different level, either by using a numeric prefix
  4987. argument, or by inserting the heading marker by hand. For example, if the
  4988. cursor is after \"*****\", then the tree will be shifted to level 5.
  4989. If optional TREE is given, use this text instead of the kill ring.
  4990. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4991. move back over whitespace before inserting, and move point to the end of
  4992. the inserted text when done."
  4993. (interactive "P")
  4994. (unless (org-kill-is-subtree-p tree)
  4995. (error "%s"
  4996. (substitute-command-keys
  4997. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4998. (let* ((visp (not (org-invisible-p)))
  4999. (txt (or tree (and kill-ring (current-kill 0))))
  5000. (^re (concat "^\\(" outline-regexp "\\)"))
  5001. (re (concat "\\(" outline-regexp "\\)"))
  5002. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5003. (old-level (if (string-match ^re txt)
  5004. (- (match-end 0) (match-beginning 0) 1)
  5005. -1))
  5006. (force-level (cond (level (prefix-numeric-value level))
  5007. ((and (looking-at "[ \t]*$")
  5008. (string-match
  5009. ^re_ (buffer-substring
  5010. (point-at-bol) (point))))
  5011. (- (match-end 1) (match-beginning 1)))
  5012. ((and (bolp)
  5013. (looking-at org-outline-regexp))
  5014. (- (match-end 0) (point) 1))
  5015. (t nil)))
  5016. (previous-level (save-excursion
  5017. (condition-case nil
  5018. (progn
  5019. (outline-previous-visible-heading 1)
  5020. (if (looking-at re)
  5021. (- (match-end 0) (match-beginning 0) 1)
  5022. 1))
  5023. (error 1))))
  5024. (next-level (save-excursion
  5025. (condition-case nil
  5026. (progn
  5027. (or (looking-at outline-regexp)
  5028. (outline-next-visible-heading 1))
  5029. (if (looking-at re)
  5030. (- (match-end 0) (match-beginning 0) 1)
  5031. 1))
  5032. (error 1))))
  5033. (new-level (or force-level (max previous-level next-level)))
  5034. (shift (if (or (= old-level -1)
  5035. (= new-level -1)
  5036. (= old-level new-level))
  5037. 0
  5038. (- new-level old-level)))
  5039. (delta (if (> shift 0) -1 1))
  5040. (func (if (> shift 0) 'org-demote 'org-promote))
  5041. (org-odd-levels-only nil)
  5042. beg end newend)
  5043. ;; Remove the forced level indicator
  5044. (if force-level
  5045. (delete-region (point-at-bol) (point)))
  5046. ;; Paste
  5047. (beginning-of-line 1)
  5048. (unless for-yank (org-back-over-empty-lines))
  5049. (setq beg (point))
  5050. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5051. (insert-before-markers txt)
  5052. (unless (string-match "\n\\'" txt) (insert "\n"))
  5053. (setq newend (point))
  5054. (org-reinstall-markers-in-region beg)
  5055. (setq end (point))
  5056. (goto-char beg)
  5057. (skip-chars-forward " \t\n\r")
  5058. (setq beg (point))
  5059. (if (and (org-invisible-p) visp)
  5060. (save-excursion (outline-show-heading)))
  5061. ;; Shift if necessary
  5062. (unless (= shift 0)
  5063. (save-restriction
  5064. (narrow-to-region beg end)
  5065. (while (not (= shift 0))
  5066. (org-map-region func (point-min) (point-max))
  5067. (setq shift (+ delta shift)))
  5068. (goto-char (point-min))
  5069. (setq newend (point-max))))
  5070. (when (or (interactive-p) for-yank)
  5071. (message "Clipboard pasted as level %d subtree" new-level))
  5072. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5073. kill-ring
  5074. (eq org-subtree-clip (current-kill 0))
  5075. org-subtree-clip-folded)
  5076. ;; The tree was folded before it was killed/copied
  5077. (hide-subtree))
  5078. (and for-yank (goto-char newend))))
  5079. (defun org-kill-is-subtree-p (&optional txt)
  5080. "Check if the current kill is an outline subtree, or a set of trees.
  5081. Returns nil if kill does not start with a headline, or if the first
  5082. headline level is not the largest headline level in the tree.
  5083. So this will actually accept several entries of equal levels as well,
  5084. which is OK for `org-paste-subtree'.
  5085. If optional TXT is given, check this string instead of the current kill."
  5086. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5087. (start-level (and kill
  5088. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5089. org-outline-regexp "\\)")
  5090. kill)
  5091. (- (match-end 2) (match-beginning 2) 1)))
  5092. (re (concat "^" org-outline-regexp))
  5093. (start (1+ (or (match-beginning 2) -1))))
  5094. (if (not start-level)
  5095. (progn
  5096. nil) ;; does not even start with a heading
  5097. (catch 'exit
  5098. (while (setq start (string-match re kill (1+ start)))
  5099. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5100. (throw 'exit nil)))
  5101. t))))
  5102. (defvar org-markers-to-move nil
  5103. "Markers that should be moved with a cut-and-paste operation.
  5104. Those markers are stored together with their positions relative to
  5105. the start of the region.")
  5106. (defun org-save-markers-in-region (beg end)
  5107. "Check markers in region.
  5108. If these markers are between BEG and END, record their position relative
  5109. to BEG, so that after moving the block of text, we can put the markers back
  5110. into place.
  5111. This function gets called just before an entry or tree gets cut from the
  5112. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5113. called immediately, to move the markers with the entries."
  5114. (setq org-markers-to-move nil)
  5115. (when (featurep 'org-clock)
  5116. (org-clock-save-markers-for-cut-and-paste beg end))
  5117. (when (featurep 'org-agenda)
  5118. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5119. (defun org-check-and-save-marker (marker beg end)
  5120. "Check if MARKER is between BEG and END.
  5121. If yes, remember the marker and the distance to BEG."
  5122. (when (and (marker-buffer marker)
  5123. (equal (marker-buffer marker) (current-buffer)))
  5124. (if (and (>= marker beg) (< marker end))
  5125. (push (cons marker (- marker beg)) org-markers-to-move))))
  5126. (defun org-reinstall-markers-in-region (beg)
  5127. "Move all remembered markers to their position relative to BEG."
  5128. (mapc (lambda (x)
  5129. (move-marker (car x) (+ beg (cdr x))))
  5130. org-markers-to-move)
  5131. (setq org-markers-to-move nil))
  5132. (defun org-narrow-to-subtree ()
  5133. "Narrow buffer to the current subtree."
  5134. (interactive)
  5135. (save-excursion
  5136. (save-match-data
  5137. (narrow-to-region
  5138. (progn (org-back-to-heading) (point))
  5139. (progn (org-end-of-subtree t) (point))))))
  5140. ;;; Outline Sorting
  5141. (defun org-sort (with-case)
  5142. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5143. Optional argument WITH-CASE means sort case-sensitively.
  5144. With a double prefix argument, also remove duplicate entries."
  5145. (interactive "P")
  5146. (if (org-at-table-p)
  5147. (org-call-with-arg 'org-table-sort-lines with-case)
  5148. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5149. (defun org-sort-remove-invisible (s)
  5150. (remove-text-properties 0 (length s) org-rm-props s)
  5151. (while (string-match org-bracket-link-regexp s)
  5152. (setq s (replace-match (if (match-end 2)
  5153. (match-string 3 s)
  5154. (match-string 1 s)) t t s)))
  5155. s)
  5156. (defvar org-priority-regexp) ; defined later in the file
  5157. (defun org-sort-entries-or-items
  5158. (&optional with-case sorting-type getkey-func compare-func property)
  5159. "Sort entries on a certain level of an outline tree.
  5160. If there is an active region, the entries in the region are sorted.
  5161. Else, if the cursor is before the first entry, sort the top-level items.
  5162. Else, the children of the entry at point are sorted.
  5163. Sorting can be alphabetically, numerically, and by date/time as given by
  5164. the first time stamp in the entry. The command prompts for the sorting
  5165. type unless it has been given to the function through the SORTING-TYPE
  5166. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5167. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5168. called with point at the beginning of the record. It must return either
  5169. a string or a number that should serve as the sorting key for that record.
  5170. Comparing entries ignores case by default. However, with an optional argument
  5171. WITH-CASE, the sorting considers case as well."
  5172. (interactive "P")
  5173. (let ((case-func (if with-case 'identity 'downcase))
  5174. start beg end stars re re2
  5175. txt what tmp plain-list-p)
  5176. ;; Find beginning and end of region to sort
  5177. (cond
  5178. ((org-region-active-p)
  5179. ;; we will sort the region
  5180. (setq end (region-end)
  5181. what "region")
  5182. (goto-char (region-beginning))
  5183. (if (not (org-on-heading-p)) (outline-next-heading))
  5184. (setq start (point)))
  5185. ((org-at-item-p)
  5186. ;; we will sort this plain list
  5187. (org-beginning-of-item-list) (setq start (point))
  5188. (org-end-of-item-list) (setq end (point))
  5189. (goto-char start)
  5190. (setq plain-list-p t
  5191. what "plain list"))
  5192. ((or (org-on-heading-p)
  5193. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5194. ;; we will sort the children of the current headline
  5195. (org-back-to-heading)
  5196. (setq start (point)
  5197. end (progn (org-end-of-subtree t t)
  5198. (org-back-over-empty-lines)
  5199. (point))
  5200. what "children")
  5201. (goto-char start)
  5202. (show-subtree)
  5203. (outline-next-heading))
  5204. (t
  5205. ;; we will sort the top-level entries in this file
  5206. (goto-char (point-min))
  5207. (or (org-on-heading-p) (outline-next-heading))
  5208. (setq start (point) end (point-max) what "top-level")
  5209. (goto-char start)
  5210. (show-all)))
  5211. (setq beg (point))
  5212. (if (>= beg end) (error "Nothing to sort"))
  5213. (unless plain-list-p
  5214. (looking-at "\\(\\*+\\)")
  5215. (setq stars (match-string 1)
  5216. re (concat "^" (regexp-quote stars) " +")
  5217. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5218. txt (buffer-substring beg end))
  5219. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5220. (if (and (not (equal stars "*")) (string-match re2 txt))
  5221. (error "Region to sort contains a level above the first entry")))
  5222. (unless sorting-type
  5223. (message
  5224. (if plain-list-p
  5225. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5226. "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:")
  5227. what)
  5228. (setq sorting-type (read-char-exclusive))
  5229. (and (= (downcase sorting-type) ?f)
  5230. (setq getkey-func
  5231. (org-ido-completing-read "Sort using function: "
  5232. obarray 'fboundp t nil nil))
  5233. (setq getkey-func (intern getkey-func)))
  5234. (and (= (downcase sorting-type) ?r)
  5235. (setq property
  5236. (org-ido-completing-read "Property: "
  5237. (mapcar 'list (org-buffer-property-keys t))
  5238. nil t))))
  5239. (message "Sorting entries...")
  5240. (save-restriction
  5241. (narrow-to-region start end)
  5242. (let ((dcst (downcase sorting-type))
  5243. (now (current-time)))
  5244. (sort-subr
  5245. (/= dcst sorting-type)
  5246. ;; This function moves to the beginning character of the "record" to
  5247. ;; be sorted.
  5248. (if plain-list-p
  5249. (lambda nil
  5250. (if (org-at-item-p) t (goto-char (point-max))))
  5251. (lambda nil
  5252. (if (re-search-forward re nil t)
  5253. (goto-char (match-beginning 0))
  5254. (goto-char (point-max)))))
  5255. ;; This function moves to the last character of the "record" being
  5256. ;; sorted.
  5257. (if plain-list-p
  5258. 'org-end-of-item
  5259. (lambda nil
  5260. (save-match-data
  5261. (condition-case nil
  5262. (outline-forward-same-level 1)
  5263. (error
  5264. (goto-char (point-max)))))))
  5265. ;; This function returns the value that gets sorted against.
  5266. (if plain-list-p
  5267. (lambda nil
  5268. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5269. (cond
  5270. ((= dcst ?n)
  5271. (string-to-number (buffer-substring (match-end 0)
  5272. (point-at-eol))))
  5273. ((= dcst ?a)
  5274. (buffer-substring (match-end 0) (point-at-eol)))
  5275. ((= dcst ?t)
  5276. (if (re-search-forward org-ts-regexp
  5277. (point-at-eol) t)
  5278. (org-time-string-to-time (match-string 0))
  5279. now))
  5280. ((= dcst ?f)
  5281. (if getkey-func
  5282. (progn
  5283. (setq tmp (funcall getkey-func))
  5284. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5285. tmp)
  5286. (error "Invalid key function `%s'" getkey-func)))
  5287. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5288. (lambda nil
  5289. (cond
  5290. ((= dcst ?n)
  5291. (if (looking-at org-complex-heading-regexp)
  5292. (string-to-number (match-string 4))
  5293. nil))
  5294. ((= dcst ?a)
  5295. (if (looking-at org-complex-heading-regexp)
  5296. (funcall case-func (match-string 4))
  5297. nil))
  5298. ((= dcst ?t)
  5299. (if (re-search-forward org-ts-regexp
  5300. (save-excursion
  5301. (forward-line 2)
  5302. (point)) t)
  5303. (org-time-string-to-time (match-string 0))
  5304. now))
  5305. ((= dcst ?p)
  5306. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5307. (string-to-char (match-string 2))
  5308. org-default-priority))
  5309. ((= dcst ?r)
  5310. (or (org-entry-get nil property) ""))
  5311. ((= dcst ?o)
  5312. (if (looking-at org-complex-heading-regexp)
  5313. (- 9999 (length (member (match-string 2)
  5314. org-todo-keywords-1)))))
  5315. ((= dcst ?f)
  5316. (if getkey-func
  5317. (progn
  5318. (setq tmp (funcall getkey-func))
  5319. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5320. tmp)
  5321. (error "Invalid key function `%s'" getkey-func)))
  5322. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5323. nil
  5324. (cond
  5325. ((= dcst ?a) 'string<)
  5326. ((= dcst ?t) 'time-less-p)
  5327. ((= dcst ?f) compare-func)
  5328. (t nil)))))
  5329. (message "Sorting entries...done")))
  5330. (defun org-do-sort (table what &optional with-case sorting-type)
  5331. "Sort TABLE of WHAT according to SORTING-TYPE.
  5332. The user will be prompted for the SORTING-TYPE if the call to this
  5333. function does not specify it. WHAT is only for the prompt, to indicate
  5334. what is being sorted. The sorting key will be extracted from
  5335. the car of the elements of the table.
  5336. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5337. (unless sorting-type
  5338. (message
  5339. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5340. what)
  5341. (setq sorting-type (read-char-exclusive)))
  5342. (let ((dcst (downcase sorting-type))
  5343. extractfun comparefun)
  5344. ;; Define the appropriate functions
  5345. (cond
  5346. ((= dcst ?n)
  5347. (setq extractfun 'string-to-number
  5348. comparefun (if (= dcst sorting-type) '< '>)))
  5349. ((= dcst ?a)
  5350. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5351. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5352. comparefun (if (= dcst sorting-type)
  5353. 'string<
  5354. (lambda (a b) (and (not (string< a b))
  5355. (not (string= a b)))))))
  5356. ((= dcst ?t)
  5357. (setq extractfun
  5358. (lambda (x)
  5359. (if (or (string-match org-ts-regexp x)
  5360. (string-match org-ts-regexp-both x))
  5361. (time-to-seconds
  5362. (org-time-string-to-time (match-string 0 x)))
  5363. 0))
  5364. comparefun (if (= dcst sorting-type) '< '>)))
  5365. (t (error "Invalid sorting type `%c'" sorting-type)))
  5366. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5367. table)
  5368. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5369. ;;; Editing source examples
  5370. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5371. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5372. (defvar org-edit-src-force-single-line nil)
  5373. (defvar org-edit-src-from-org-mode nil)
  5374. (defvar org-edit-src-picture nil)
  5375. (define-minor-mode org-exit-edit-mode
  5376. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5377. (defun org-edit-src-code ()
  5378. "Edit the source code example at point.
  5379. An indirect buffer is created, and that buffer is then narrowed to the
  5380. example at point and switched to the correct language mode. When done,
  5381. exit by killing the buffer with \\[org-edit-src-exit]."
  5382. (interactive)
  5383. (let ((line (org-current-line))
  5384. (case-fold-search t)
  5385. (msg (substitute-command-keys
  5386. "Edit, then exit with C-c ' (C-c and single quote)"))
  5387. (info (org-edit-src-find-region-and-lang))
  5388. (org-mode-p (eq major-mode 'org-mode))
  5389. beg end lang lang-f single lfmt)
  5390. (if (not info)
  5391. nil
  5392. (setq beg (nth 0 info)
  5393. end (nth 1 info)
  5394. lang (nth 2 info)
  5395. single (nth 3 info)
  5396. lfmt (nth 4 info)
  5397. lang-f (intern (concat lang "-mode")))
  5398. (unless (functionp lang-f)
  5399. (error "No such language mode: %s" lang-f))
  5400. (goto-line line)
  5401. (if (get-buffer "*Org Edit Src Example*")
  5402. (kill-buffer "*Org Edit Src Example*"))
  5403. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5404. "*Org Edit Src Example*"))
  5405. (narrow-to-region beg end)
  5406. (remove-text-properties beg end '(display nil invisible nil
  5407. intangible nil))
  5408. (let ((org-inhibit-startup t))
  5409. (funcall lang-f))
  5410. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5411. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5412. (when lfmt
  5413. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5414. (when org-mode-p
  5415. (goto-char (point-min))
  5416. (while (re-search-forward "^," nil t)
  5417. (replace-match "")))
  5418. (goto-line line)
  5419. (org-exit-edit-mode)
  5420. (org-set-local 'header-line-format msg)
  5421. (message "%s" msg)
  5422. t)))
  5423. (defun org-edit-fixed-width-region ()
  5424. "Edit the fixed-width ascii drawing at point.
  5425. This must be a region where each line starts with a colon followed by
  5426. a space character.
  5427. An indirect buffer is created, and that buffer is then narrowed to the
  5428. example at point and switched to artist-mode. When done,
  5429. exit by killing the buffer with \\[org-edit-src-exit]."
  5430. (interactive)
  5431. (let ((line (org-current-line))
  5432. (case-fold-search t)
  5433. (msg (substitute-command-keys
  5434. "Edit, then exit with C-c ' (C-c and single quote)"))
  5435. (org-mode-p (eq major-mode 'org-mode))
  5436. beg end)
  5437. (beginning-of-line 1)
  5438. (if (looking-at "[ \t]*[^:\n \t]")
  5439. nil
  5440. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5441. (setq beg (point) end beg)
  5442. (save-excursion
  5443. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5444. (setq beg (point-at-bol 2))
  5445. (setq beg (point))))
  5446. (save-excursion
  5447. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5448. (setq end (1- (match-beginning 0)))
  5449. (setq end (point))))
  5450. (goto-line line))
  5451. (if (get-buffer "*Org Edit Picture*")
  5452. (kill-buffer "*Org Edit Picture*"))
  5453. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5454. "*Org Edit Picture*"))
  5455. (narrow-to-region beg end)
  5456. (remove-text-properties beg end '(display nil invisible nil
  5457. intangible nil))
  5458. (when (fboundp 'font-lock-unfontify-region)
  5459. (font-lock-unfontify-region (point-min) (point-max)))
  5460. (cond
  5461. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5462. (fundamental-mode)
  5463. (artist-mode 1))
  5464. (t (funcall org-edit-fixed-width-region-mode)))
  5465. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5466. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5467. (set (make-local-variable 'org-edit-src-picture) t)
  5468. (goto-char (point-min))
  5469. (while (re-search-forward "^[ \t]*: ?" nil t)
  5470. (replace-match ""))
  5471. (goto-line line)
  5472. (org-exit-edit-mode)
  5473. (org-set-local 'header-line-format msg)
  5474. (message "%s" msg)
  5475. t)))
  5476. (defun org-edit-src-find-region-and-lang ()
  5477. "Find the region and language for a local edit.
  5478. Return a list with beginning and end of the region, a string representing
  5479. the language, a switch telling of the content should be in a single line."
  5480. (let ((re-list
  5481. (append
  5482. org-edit-src-region-extra
  5483. '(
  5484. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5485. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5486. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5487. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5488. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5489. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5490. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5491. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5492. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5493. ("^#\\+html:" "\n" "html" single-line)
  5494. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5495. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5496. ("^#\\+latex:" "\n" "latex" single-line)
  5497. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5498. ("^#\\+ascii:" "\n" "ascii" single-line)
  5499. )))
  5500. (pos (point))
  5501. re1 re2 single beg end lang lfmt match-re1)
  5502. (catch 'exit
  5503. (while (setq entry (pop re-list))
  5504. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5505. single (nth 3 entry))
  5506. (save-excursion
  5507. (if (or (looking-at re1)
  5508. (re-search-backward re1 nil t))
  5509. (progn
  5510. (setq match-re1 (match-string 0))
  5511. (setq beg (match-end 0)
  5512. lang (org-edit-src-get-lang lang)
  5513. lfmt (org-edit-src-get-label-format match-re1))
  5514. (if (and (re-search-forward re2 nil t)
  5515. (>= (match-end 0) pos))
  5516. (throw 'exit (list beg (match-beginning 0)
  5517. lang single lfmt))))
  5518. (if (or (looking-at re2)
  5519. (re-search-forward re2 nil t))
  5520. (progn
  5521. (setq end (match-beginning 0))
  5522. (if (and (re-search-backward re1 nil t)
  5523. (<= (match-beginning 0) pos))
  5524. (progn
  5525. (setq lfmt (org-edit-src-get-label-format
  5526. (match-string 0)))
  5527. (throw 'exit
  5528. (list (match-end 0) end
  5529. (org-edit-src-get-lang lang)
  5530. single lfmt))))))))))))
  5531. (defun org-edit-src-get-lang (lang)
  5532. "Extract the src language."
  5533. (let ((m (match-string 0)))
  5534. (cond
  5535. ((stringp lang) lang)
  5536. ((integerp lang) (match-string lang))
  5537. ((and (eq lang 'lang)
  5538. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5539. (match-string 1 m))
  5540. ((and (eq lang 'style)
  5541. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5542. (match-string 1 m))
  5543. (t "fundamental"))))
  5544. (defun org-edit-src-get-label-format (s)
  5545. "Extract the label format."
  5546. (save-match-data
  5547. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5548. (match-string 1 s))))
  5549. (defun org-edit-src-exit ()
  5550. "Exit special edit and protect problematic lines."
  5551. (interactive)
  5552. (unless (buffer-base-buffer (current-buffer))
  5553. (error "This is not an indirect buffer, something is wrong..."))
  5554. (unless (> (point-min) 1)
  5555. (error "This buffer is not narrowed, something is wrong..."))
  5556. (goto-char (point-min))
  5557. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5558. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5559. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5560. (goto-char (point-min))
  5561. (while (re-search-forward "\n" nil t)
  5562. (replace-match " "))
  5563. (goto-char (point-min))
  5564. (if (looking-at "\\s-*") (replace-match " "))
  5565. (if (re-search-forward "\\s-+\\'" nil t)
  5566. (replace-match "")))
  5567. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5568. (goto-char (point-min))
  5569. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5570. (replace-match ",\\1"))
  5571. (when font-lock-mode
  5572. (font-lock-unfontify-region (point-min) (point-max)))
  5573. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5574. (when (org-bound-and-true-p org-edit-src-picture)
  5575. (untabify (point-min) (point-max))
  5576. (goto-char (point-min))
  5577. (while (re-search-forward "^" nil t)
  5578. (replace-match ": "))
  5579. (when font-lock-mode
  5580. (font-lock-unfontify-region (point-min) (point-max)))
  5581. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5582. (kill-buffer (current-buffer))
  5583. (and (org-mode-p) (org-restart-font-lock)))
  5584. ;;; The orgstruct minor mode
  5585. ;; Define a minor mode which can be used in other modes in order to
  5586. ;; integrate the org-mode structure editing commands.
  5587. ;; This is really a hack, because the org-mode structure commands use
  5588. ;; keys which normally belong to the major mode. Here is how it
  5589. ;; works: The minor mode defines all the keys necessary to operate the
  5590. ;; structure commands, but wraps the commands into a function which
  5591. ;; tests if the cursor is currently at a headline or a plain list
  5592. ;; item. If that is the case, the structure command is used,
  5593. ;; temporarily setting many Org-mode variables like regular
  5594. ;; expressions for filling etc. However, when any of those keys is
  5595. ;; used at a different location, function uses `key-binding' to look
  5596. ;; up if the key has an associated command in another currently active
  5597. ;; keymap (minor modes, major mode, global), and executes that
  5598. ;; command. There might be problems if any of the keys is otherwise
  5599. ;; used as a prefix key.
  5600. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5601. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5602. ;; addresses this by checking explicitly for both bindings.
  5603. (defvar orgstruct-mode-map (make-sparse-keymap)
  5604. "Keymap for the minor `orgstruct-mode'.")
  5605. (defvar org-local-vars nil
  5606. "List of local variables, for use by `orgstruct-mode'")
  5607. ;;;###autoload
  5608. (define-minor-mode orgstruct-mode
  5609. "Toggle the minor more `orgstruct-mode'.
  5610. This mode is for using Org-mode structure commands in other modes.
  5611. The following key behave as if Org-mode was active, if the cursor
  5612. is on a headline, or on a plain list item (both in the definition
  5613. of Org-mode).
  5614. M-up Move entry/item up
  5615. M-down Move entry/item down
  5616. M-left Promote
  5617. M-right Demote
  5618. M-S-up Move entry/item up
  5619. M-S-down Move entry/item down
  5620. M-S-left Promote subtree
  5621. M-S-right Demote subtree
  5622. M-q Fill paragraph and items like in Org-mode
  5623. C-c ^ Sort entries
  5624. C-c - Cycle list bullet
  5625. TAB Cycle item visibility
  5626. M-RET Insert new heading/item
  5627. S-M-RET Insert new TODO heading / Checkbox item
  5628. C-c C-c Set tags / toggle checkbox"
  5629. nil " OrgStruct" nil
  5630. (org-load-modules-maybe)
  5631. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5632. ;;;###autoload
  5633. (defun turn-on-orgstruct ()
  5634. "Unconditionally turn on `orgstruct-mode'."
  5635. (orgstruct-mode 1))
  5636. ;;;###autoload
  5637. (defun turn-on-orgstruct++ ()
  5638. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5639. In addition to setting orgstruct-mode, this also exports all indentation and
  5640. autofilling variables from org-mode into the buffer. Note that turning
  5641. off orgstruct-mode will *not* remove these additional settings."
  5642. (orgstruct-mode 1)
  5643. (let (var val)
  5644. (mapc
  5645. (lambda (x)
  5646. (when (string-match
  5647. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5648. (symbol-name (car x)))
  5649. (setq var (car x) val (nth 1 x))
  5650. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5651. org-local-vars)))
  5652. (defun orgstruct-error ()
  5653. "Error when there is no default binding for a structure key."
  5654. (interactive)
  5655. (error "This key has no function outside structure elements"))
  5656. (defun orgstruct-setup ()
  5657. "Setup orgstruct keymaps."
  5658. (let ((nfunc 0)
  5659. (bindings
  5660. (list
  5661. '([(meta up)] org-metaup)
  5662. '([(meta down)] org-metadown)
  5663. '([(meta left)] org-metaleft)
  5664. '([(meta right)] org-metaright)
  5665. '([(meta shift up)] org-shiftmetaup)
  5666. '([(meta shift down)] org-shiftmetadown)
  5667. '([(meta shift left)] org-shiftmetaleft)
  5668. '([(meta shift right)] org-shiftmetaright)
  5669. '([(shift up)] org-shiftup)
  5670. '([(shift down)] org-shiftdown)
  5671. '([(shift left)] org-shiftleft)
  5672. '([(shift right)] org-shiftright)
  5673. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5674. '("\M-q" fill-paragraph)
  5675. '("\C-c^" org-sort)
  5676. '("\C-c-" org-cycle-list-bullet)))
  5677. elt key fun cmd)
  5678. (while (setq elt (pop bindings))
  5679. (setq nfunc (1+ nfunc))
  5680. (setq key (org-key (car elt))
  5681. fun (nth 1 elt)
  5682. cmd (orgstruct-make-binding fun nfunc key))
  5683. (org-defkey orgstruct-mode-map key cmd))
  5684. ;; Special treatment needed for TAB and RET
  5685. (org-defkey orgstruct-mode-map [(tab)]
  5686. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5687. (org-defkey orgstruct-mode-map "\C-i"
  5688. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5689. (org-defkey orgstruct-mode-map "\M-\C-m"
  5690. (orgstruct-make-binding 'org-insert-heading 105
  5691. "\M-\C-m" [(meta return)]))
  5692. (org-defkey orgstruct-mode-map [(meta return)]
  5693. (orgstruct-make-binding 'org-insert-heading 106
  5694. [(meta return)] "\M-\C-m"))
  5695. (org-defkey orgstruct-mode-map [(shift meta return)]
  5696. (orgstruct-make-binding 'org-insert-todo-heading 107
  5697. [(meta return)] "\M-\C-m"))
  5698. (unless org-local-vars
  5699. (setq org-local-vars (org-get-local-variables)))
  5700. t))
  5701. (defun orgstruct-make-binding (fun n &rest keys)
  5702. "Create a function for binding in the structure minor mode.
  5703. FUN is the command to call inside a table. N is used to create a unique
  5704. command name. KEYS are keys that should be checked in for a command
  5705. to execute outside of tables."
  5706. (eval
  5707. (list 'defun
  5708. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5709. '(arg)
  5710. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5711. "Outside of structure, run the binding of `"
  5712. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5713. "'.")
  5714. '(interactive "p")
  5715. (list 'if
  5716. '(org-context-p 'headline 'item)
  5717. (list 'org-run-like-in-org-mode (list 'quote fun))
  5718. (list 'let '(orgstruct-mode)
  5719. (list 'call-interactively
  5720. (append '(or)
  5721. (mapcar (lambda (k)
  5722. (list 'key-binding k))
  5723. keys)
  5724. '('orgstruct-error))))))))
  5725. (defun org-context-p (&rest contexts)
  5726. "Check if local context is any of CONTEXTS.
  5727. Possible values in the list of contexts are `table', `headline', and `item'."
  5728. (let ((pos (point)))
  5729. (goto-char (point-at-bol))
  5730. (prog1 (or (and (memq 'table contexts)
  5731. (looking-at "[ \t]*|"))
  5732. (and (memq 'headline contexts)
  5733. ;;????????? (looking-at "\\*+"))
  5734. (looking-at outline-regexp))
  5735. (and (memq 'item contexts)
  5736. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5737. (goto-char pos))))
  5738. (defun org-get-local-variables ()
  5739. "Return a list of all local variables in an org-mode buffer."
  5740. (let (varlist)
  5741. (with-current-buffer (get-buffer-create "*Org tmp*")
  5742. (erase-buffer)
  5743. (org-mode)
  5744. (setq varlist (buffer-local-variables)))
  5745. (kill-buffer "*Org tmp*")
  5746. (delq nil
  5747. (mapcar
  5748. (lambda (x)
  5749. (setq x
  5750. (if (symbolp x)
  5751. (list x)
  5752. (list (car x) (list 'quote (cdr x)))))
  5753. (if (string-match
  5754. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5755. (symbol-name (car x)))
  5756. x nil))
  5757. varlist))))
  5758. ;;;###autoload
  5759. (defun org-run-like-in-org-mode (cmd)
  5760. (org-load-modules-maybe)
  5761. (unless org-local-vars
  5762. (setq org-local-vars (org-get-local-variables)))
  5763. (eval (list 'let org-local-vars
  5764. (list 'call-interactively (list 'quote cmd)))))
  5765. ;;;; Archiving
  5766. (defun org-get-category (&optional pos)
  5767. "Get the category applying to position POS."
  5768. (get-text-property (or pos (point)) 'org-category))
  5769. (defun org-refresh-category-properties ()
  5770. "Refresh category text properties in the buffer."
  5771. (let ((def-cat (cond
  5772. ((null org-category)
  5773. (if buffer-file-name
  5774. (file-name-sans-extension
  5775. (file-name-nondirectory buffer-file-name))
  5776. "???"))
  5777. ((symbolp org-category) (symbol-name org-category))
  5778. (t org-category)))
  5779. beg end cat pos optionp)
  5780. (org-unmodified
  5781. (save-excursion
  5782. (save-restriction
  5783. (widen)
  5784. (goto-char (point-min))
  5785. (put-text-property (point) (point-max) 'org-category def-cat)
  5786. (while (re-search-forward
  5787. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5788. (setq pos (match-end 0)
  5789. optionp (equal (char-after (match-beginning 0)) ?#)
  5790. cat (org-trim (match-string 2)))
  5791. (if optionp
  5792. (setq beg (point-at-bol) end (point-max))
  5793. (org-back-to-heading t)
  5794. (setq beg (point) end (org-end-of-subtree t t)))
  5795. (put-text-property beg end 'org-category cat)
  5796. (goto-char pos)))))))
  5797. ;;;; Link Stuff
  5798. ;;; Link abbreviations
  5799. (defun org-link-expand-abbrev (link)
  5800. "Apply replacements as defined in `org-link-abbrev-alist."
  5801. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5802. (let* ((key (match-string 1 link))
  5803. (as (or (assoc key org-link-abbrev-alist-local)
  5804. (assoc key org-link-abbrev-alist)))
  5805. (tag (and (match-end 2) (match-string 3 link)))
  5806. rpl)
  5807. (if (not as)
  5808. link
  5809. (setq rpl (cdr as))
  5810. (cond
  5811. ((symbolp rpl) (funcall rpl tag))
  5812. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5813. ((string-match "%h" rpl)
  5814. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5815. (t (concat rpl tag)))))
  5816. link))
  5817. ;;; Storing and inserting links
  5818. (defvar org-insert-link-history nil
  5819. "Minibuffer history for links inserted with `org-insert-link'.")
  5820. (defvar org-stored-links nil
  5821. "Contains the links stored with `org-store-link'.")
  5822. (defvar org-store-link-plist nil
  5823. "Plist with info about the most recently link created with `org-store-link'.")
  5824. (defvar org-link-protocols nil
  5825. "Link protocols added to Org-mode using `org-add-link-type'.")
  5826. (defvar org-store-link-functions nil
  5827. "List of functions that are called to create and store a link.
  5828. Each function will be called in turn until one returns a non-nil
  5829. value. Each function should check if it is responsible for creating
  5830. this link (for example by looking at the major mode).
  5831. If not, it must exit and return nil.
  5832. If yes, it should return a non-nil value after a calling
  5833. `org-store-link-props' with a list of properties and values.
  5834. Special properties are:
  5835. :type The link prefix. like \"http\". This must be given.
  5836. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5837. This is obligatory as well.
  5838. :description Optional default description for the second pair
  5839. of brackets in an Org-mode link. The user can still change
  5840. this when inserting this link into an Org-mode buffer.
  5841. In addition to these, any additional properties can be specified
  5842. and then used in remember templates.")
  5843. (defun org-add-link-type (type &optional follow export)
  5844. "Add TYPE to the list of `org-link-types'.
  5845. Re-compute all regular expressions depending on `org-link-types'
  5846. FOLLOW and EXPORT are two functions.
  5847. FOLLOW should take the link path as the single argument and do whatever
  5848. is necessary to follow the link, for example find a file or display
  5849. a mail message.
  5850. EXPORT should format the link path for export to one of the export formats.
  5851. It should be a function accepting three arguments:
  5852. path the path of the link, the text after the prefix (like \"http:\")
  5853. desc the description of the link, if any, nil if there was no description
  5854. format the export format, a symbol like `html' or `latex'.
  5855. The function may use the FORMAT information to return different values
  5856. depending on the format. The return value will be put literally into
  5857. the exported file.
  5858. Org-mode has a built-in default for exporting links. If you are happy with
  5859. this default, there is no need to define an export function for the link
  5860. type. For a simple example of an export function, see `org-bbdb.el'."
  5861. (add-to-list 'org-link-types type t)
  5862. (org-make-link-regexps)
  5863. (if (assoc type org-link-protocols)
  5864. (setcdr (assoc type org-link-protocols) (list follow export))
  5865. (push (list type follow export) org-link-protocols)))
  5866. ;;;###autoload
  5867. (defun org-store-link (arg)
  5868. "\\<org-mode-map>Store an org-link to the current location.
  5869. This link is added to `org-stored-links' and can later be inserted
  5870. into an org-buffer with \\[org-insert-link].
  5871. For some link types, a prefix arg is interpreted:
  5872. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5873. For file links, arg negates `org-context-in-file-links'."
  5874. (interactive "P")
  5875. (org-load-modules-maybe)
  5876. (setq org-store-link-plist nil) ; reset
  5877. (let (link cpltxt desc description search txt)
  5878. (cond
  5879. ((run-hook-with-args-until-success 'org-store-link-functions)
  5880. (setq link (plist-get org-store-link-plist :link)
  5881. desc (or (plist-get org-store-link-plist :description) link)))
  5882. ((equal (buffer-name) "*Org Edit Src Example*")
  5883. (let (label gc)
  5884. (while (or (not label)
  5885. (save-excursion
  5886. (save-restriction
  5887. (widen)
  5888. (goto-char (point-min))
  5889. (re-search-forward
  5890. (regexp-quote (format org-coderef-label-format label))
  5891. nil t))))
  5892. (when label (message "Label exists already") (sit-for 2))
  5893. (setq label (read-string "Code line label: " label)))
  5894. (end-of-line 1)
  5895. (setq link (format org-coderef-label-format label))
  5896. (setq gc (- 79 (length link)))
  5897. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5898. (insert link)
  5899. (setq link (concat "(" label ")") desc nil)))
  5900. ((eq major-mode 'calendar-mode)
  5901. (let ((cd (calendar-cursor-to-date)))
  5902. (setq link
  5903. (format-time-string
  5904. (car org-time-stamp-formats)
  5905. (apply 'encode-time
  5906. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5907. nil nil nil))))
  5908. (org-store-link-props :type "calendar" :date cd)))
  5909. ((eq major-mode 'w3-mode)
  5910. (setq cpltxt (url-view-url t)
  5911. link (org-make-link cpltxt))
  5912. (org-store-link-props :type "w3" :url (url-view-url t)))
  5913. ((eq major-mode 'w3m-mode)
  5914. (setq cpltxt (or w3m-current-title w3m-current-url)
  5915. link (org-make-link w3m-current-url))
  5916. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5917. ((setq search (run-hook-with-args-until-success
  5918. 'org-create-file-search-functions))
  5919. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5920. "::" search))
  5921. (setq cpltxt (or description link)))
  5922. ((eq major-mode 'image-mode)
  5923. (setq cpltxt (concat "file:"
  5924. (abbreviate-file-name buffer-file-name))
  5925. link (org-make-link cpltxt))
  5926. (org-store-link-props :type "image" :file buffer-file-name))
  5927. ((eq major-mode 'dired-mode)
  5928. ;; link to the file in the current line
  5929. (setq cpltxt (concat "file:"
  5930. (abbreviate-file-name
  5931. (expand-file-name
  5932. (dired-get-filename nil t))))
  5933. link (org-make-link cpltxt)))
  5934. ((and buffer-file-name (org-mode-p))
  5935. (cond
  5936. ((org-in-regexp "<<\\(.*?\\)>>")
  5937. (setq cpltxt
  5938. (concat "file:"
  5939. (abbreviate-file-name buffer-file-name)
  5940. "::" (match-string 1))
  5941. link (org-make-link cpltxt)))
  5942. ((and (featurep 'org-id)
  5943. (or (eq org-link-to-org-use-id t)
  5944. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5945. (interactive-p))
  5946. (and org-link-to-org-use-id
  5947. (condition-case nil
  5948. (org-entry-get nil "ID")
  5949. (error nil)))))
  5950. ;; We can make a link using the ID.
  5951. (setq link (condition-case nil
  5952. (prog1 (org-id-store-link)
  5953. (setq desc (plist-get org-store-link-plist
  5954. :description)))
  5955. (error
  5956. ;; probably before first headline, link to file only
  5957. (concat "file:"
  5958. (abbreviate-file-name buffer-file-name))))))
  5959. (t
  5960. ;; Just link to current headline
  5961. (setq cpltxt (concat "file:"
  5962. (abbreviate-file-name buffer-file-name)))
  5963. ;; Add a context search string
  5964. (when (org-xor org-context-in-file-links arg)
  5965. (setq txt (cond
  5966. ((org-on-heading-p) nil)
  5967. ((org-region-active-p)
  5968. (buffer-substring (region-beginning) (region-end)))
  5969. (t nil)))
  5970. (when (or (null txt) (string-match "\\S-" txt))
  5971. (setq cpltxt
  5972. (concat cpltxt "::"
  5973. (condition-case nil
  5974. (org-make-org-heading-search-string txt)
  5975. (error "")))
  5976. desc "NONE")))
  5977. (if (string-match "::\\'" cpltxt)
  5978. (setq cpltxt (substring cpltxt 0 -2)))
  5979. (setq link (org-make-link cpltxt)))))
  5980. ((buffer-file-name (buffer-base-buffer))
  5981. ;; Just link to this file here.
  5982. (setq cpltxt (concat "file:"
  5983. (abbreviate-file-name
  5984. (buffer-file-name (buffer-base-buffer)))))
  5985. ;; Add a context string
  5986. (when (org-xor org-context-in-file-links arg)
  5987. (setq txt (if (org-region-active-p)
  5988. (buffer-substring (region-beginning) (region-end))
  5989. (buffer-substring (point-at-bol) (point-at-eol))))
  5990. ;; Only use search option if there is some text.
  5991. (when (string-match "\\S-" txt)
  5992. (setq cpltxt
  5993. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5994. desc "NONE")))
  5995. (setq link (org-make-link cpltxt)))
  5996. ((interactive-p)
  5997. (error "Cannot link to a buffer which is not visiting a file"))
  5998. (t (setq link nil)))
  5999. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6000. (setq link (or link cpltxt)
  6001. desc (or desc cpltxt))
  6002. (if (equal desc "NONE") (setq desc nil))
  6003. (if (and (interactive-p) link)
  6004. (progn
  6005. (setq org-stored-links
  6006. (cons (list link desc) org-stored-links))
  6007. (message "Stored: %s" (or desc link)))
  6008. (and link (org-make-link-string link desc)))))
  6009. (defun org-store-link-props (&rest plist)
  6010. "Store link properties, extract names and addresses."
  6011. (let (x adr)
  6012. (when (setq x (plist-get plist :from))
  6013. (setq adr (mail-extract-address-components x))
  6014. (setq plist (plist-put plist :fromname (car adr)))
  6015. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6016. (when (setq x (plist-get plist :to))
  6017. (setq adr (mail-extract-address-components x))
  6018. (setq plist (plist-put plist :toname (car adr)))
  6019. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6020. (let ((from (plist-get plist :from))
  6021. (to (plist-get plist :to)))
  6022. (when (and from to org-from-is-user-regexp)
  6023. (setq plist
  6024. (plist-put plist :fromto
  6025. (if (string-match org-from-is-user-regexp from)
  6026. (concat "to %t")
  6027. (concat "from %f"))))))
  6028. (setq org-store-link-plist plist))
  6029. (defun org-add-link-props (&rest plist)
  6030. "Add these properties to the link property list."
  6031. (let (key value)
  6032. (while plist
  6033. (setq key (pop plist) value (pop plist))
  6034. (setq org-store-link-plist
  6035. (plist-put org-store-link-plist key value)))))
  6036. (defun org-email-link-description (&optional fmt)
  6037. "Return the description part of an email link.
  6038. This takes information from `org-store-link-plist' and formats it
  6039. according to FMT (default from `org-email-link-description-format')."
  6040. (setq fmt (or fmt org-email-link-description-format))
  6041. (let* ((p org-store-link-plist)
  6042. (to (plist-get p :toaddress))
  6043. (from (plist-get p :fromaddress))
  6044. (table
  6045. (list
  6046. (cons "%c" (plist-get p :fromto))
  6047. (cons "%F" (plist-get p :from))
  6048. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6049. (cons "%T" (plist-get p :to))
  6050. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6051. (cons "%s" (plist-get p :subject))
  6052. (cons "%m" (plist-get p :message-id)))))
  6053. (when (string-match "%c" fmt)
  6054. ;; Check if the user wrote this message
  6055. (if (and org-from-is-user-regexp from to
  6056. (save-match-data (string-match org-from-is-user-regexp from)))
  6057. (setq fmt (replace-match "to %t" t t fmt))
  6058. (setq fmt (replace-match "from %f" t t fmt))))
  6059. (org-replace-escapes fmt table)))
  6060. (defun org-make-org-heading-search-string (&optional string heading)
  6061. "Make search string for STRING or current headline."
  6062. (interactive)
  6063. (let ((s (or string (org-get-heading))))
  6064. (unless (and string (not heading))
  6065. ;; We are using a headline, clean up garbage in there.
  6066. (if (string-match org-todo-regexp s)
  6067. (setq s (replace-match "" t t s)))
  6068. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6069. (setq s (replace-match "" t t s)))
  6070. (setq s (org-trim s))
  6071. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6072. org-comment-string "\\)") s)
  6073. (setq s (replace-match "" t t s)))
  6074. (while (string-match org-ts-regexp s)
  6075. (setq s (replace-match "" t t s))))
  6076. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6077. (setq s (replace-match " " t t s)))
  6078. (or string (setq s (concat "*" s))) ; Add * for headlines
  6079. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6080. (defun org-make-link (&rest strings)
  6081. "Concatenate STRINGS."
  6082. (apply 'concat strings))
  6083. (defun org-make-link-string (link &optional description)
  6084. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6085. (unless (string-match "\\S-" link)
  6086. (error "Empty link"))
  6087. (when (stringp description)
  6088. ;; Remove brackets from the description, they are fatal.
  6089. (while (string-match "\\[" description)
  6090. (setq description (replace-match "{" t t description)))
  6091. (while (string-match "\\]" description)
  6092. (setq description (replace-match "}" t t description))))
  6093. (when (equal (org-link-escape link) description)
  6094. ;; No description needed, it is identical
  6095. (setq description nil))
  6096. (when (and (not description)
  6097. (not (equal link (org-link-escape link))))
  6098. (setq description (org-extract-attributes link)))
  6099. (concat "[[" (org-link-escape link) "]"
  6100. (if description (concat "[" description "]") "")
  6101. "]"))
  6102. (defconst org-link-escape-chars
  6103. '((?\ . "%20")
  6104. (?\[ . "%5B")
  6105. (?\] . "%5D")
  6106. (?\340 . "%E0") ; `a
  6107. (?\342 . "%E2") ; ^a
  6108. (?\347 . "%E7") ; ,c
  6109. (?\350 . "%E8") ; `e
  6110. (?\351 . "%E9") ; 'e
  6111. (?\352 . "%EA") ; ^e
  6112. (?\356 . "%EE") ; ^i
  6113. (?\364 . "%F4") ; ^o
  6114. (?\371 . "%F9") ; `u
  6115. (?\373 . "%FB") ; ^u
  6116. (?\; . "%3B")
  6117. (?? . "%3F")
  6118. (?= . "%3D")
  6119. (?+ . "%2B")
  6120. )
  6121. "Association list of escapes for some characters problematic in links.
  6122. This is the list that is used for internal purposes.")
  6123. (defconst org-link-escape-chars-browser
  6124. '((?\ . "%20")) ; 32 for the SPC char
  6125. "Association list of escapes for some characters problematic in links.
  6126. This is the list that is used before handing over to the browser.")
  6127. (defun org-link-escape (text &optional table)
  6128. "Escape characters in TEXT that are problematic for links."
  6129. (setq table (or table org-link-escape-chars))
  6130. (when text
  6131. (let ((re (mapconcat (lambda (x) (regexp-quote
  6132. (char-to-string (car x))))
  6133. table "\\|")))
  6134. (while (string-match re text)
  6135. (setq text
  6136. (replace-match
  6137. (cdr (assoc (string-to-char (match-string 0 text))
  6138. table))
  6139. t t text)))
  6140. text)))
  6141. (defun org-link-unescape (text &optional table)
  6142. "Reverse the action of `org-link-escape'."
  6143. (setq table (or table org-link-escape-chars))
  6144. (when text
  6145. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6146. table "\\|")))
  6147. (while (string-match re text)
  6148. (setq text
  6149. (replace-match
  6150. (char-to-string (car (rassoc (match-string 0 text) table)))
  6151. t t text)))
  6152. text)))
  6153. (defun org-xor (a b)
  6154. "Exclusive or."
  6155. (if a (not b) b))
  6156. (defun org-fixup-message-id-for-http (s)
  6157. "Replace special characters in a message id, so it can be used in an http query."
  6158. (while (string-match "<" s)
  6159. (setq s (replace-match "%3C" t t s)))
  6160. (while (string-match ">" s)
  6161. (setq s (replace-match "%3E" t t s)))
  6162. (while (string-match "@" s)
  6163. (setq s (replace-match "%40" t t s)))
  6164. s)
  6165. ;;;###autoload
  6166. (defun org-insert-link-global ()
  6167. "Insert a link like Org-mode does.
  6168. This command can be called in any mode to insert a link in Org-mode syntax."
  6169. (interactive)
  6170. (org-load-modules-maybe)
  6171. (org-run-like-in-org-mode 'org-insert-link))
  6172. (defun org-insert-link (&optional complete-file link-location)
  6173. "Insert a link. At the prompt, enter the link.
  6174. Completion can be used to insert any of the link protocol prefixes like
  6175. http or ftp in use.
  6176. The history can be used to select a link previously stored with
  6177. `org-store-link'. When the empty string is entered (i.e. if you just
  6178. press RET at the prompt), the link defaults to the most recently
  6179. stored link. As SPC triggers completion in the minibuffer, you need to
  6180. use M-SPC or C-q SPC to force the insertion of a space character.
  6181. You will also be prompted for a description, and if one is given, it will
  6182. be displayed in the buffer instead of the link.
  6183. If there is already a link at point, this command will allow you to edit link
  6184. and description parts.
  6185. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6186. be selected using completion. The path to the file will be relative to the
  6187. current directory if the file is in the current directory or a subdirectory.
  6188. Otherwise, the link will be the absolute path as completed in the minibuffer
  6189. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6190. option `org-link-file-path-type'.
  6191. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6192. the current directory or below.
  6193. With three \\[universal-argument] prefixes, negate the meaning of
  6194. `org-keep-stored-link-after-insertion'.
  6195. If `org-make-link-description-function' is non-nil, this function will be
  6196. called with the link target, and the result will be the default
  6197. link description.
  6198. If the LINK-LOCATION parameter is non-nil, this value will be
  6199. used as the link location instead of reading one interactively."
  6200. (interactive "P")
  6201. (let* ((wcf (current-window-configuration))
  6202. (region (if (org-region-active-p)
  6203. (buffer-substring (region-beginning) (region-end))))
  6204. (remove (and region (list (region-beginning) (region-end))))
  6205. (desc region)
  6206. tmphist ; byte-compile incorrectly complains about this
  6207. (link link-location)
  6208. entry file)
  6209. (cond
  6210. (link-location) ; specified by arg, just use it.
  6211. ((org-in-regexp org-bracket-link-regexp 1)
  6212. ;; We do have a link at point, and we are going to edit it.
  6213. (setq remove (list (match-beginning 0) (match-end 0)))
  6214. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6215. (setq link (read-string "Link: "
  6216. (org-link-unescape
  6217. (org-match-string-no-properties 1)))))
  6218. ((or (org-in-regexp org-angle-link-re)
  6219. (org-in-regexp org-plain-link-re))
  6220. ;; Convert to bracket link
  6221. (setq remove (list (match-beginning 0) (match-end 0))
  6222. link (read-string "Link: "
  6223. (org-remove-angle-brackets (match-string 0)))))
  6224. ((member complete-file '((4) (16)))
  6225. ;; Completing read for file names.
  6226. (setq file (read-file-name "File: "))
  6227. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6228. (pwd1 (file-name-as-directory (abbreviate-file-name
  6229. (expand-file-name ".")))))
  6230. (cond
  6231. ((equal complete-file '(16))
  6232. (setq link (org-make-link
  6233. "file:"
  6234. (abbreviate-file-name (expand-file-name file)))))
  6235. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6236. (setq link (org-make-link "file:" (match-string 1 file))))
  6237. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6238. (expand-file-name file))
  6239. (setq link (org-make-link
  6240. "file:" (match-string 1 (expand-file-name file)))))
  6241. (t (setq link (org-make-link "file:" file))))))
  6242. (t
  6243. ;; Read link, with completion for stored links.
  6244. (with-output-to-temp-buffer "*Org Links*"
  6245. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6246. (when org-stored-links
  6247. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6248. (princ (mapconcat
  6249. (lambda (x)
  6250. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6251. (reverse org-stored-links) "\n"))))
  6252. (let ((cw (selected-window)))
  6253. (select-window (get-buffer-window "*Org Links*"))
  6254. (setq truncate-lines t)
  6255. (org-fit-window-to-buffer)
  6256. (select-window cw))
  6257. ;; Fake a link history, containing the stored links.
  6258. (setq tmphist (append (mapcar 'car org-stored-links)
  6259. org-insert-link-history))
  6260. (unwind-protect
  6261. (setq link
  6262. (let ((org-completion-use-ido nil))
  6263. (org-completing-read
  6264. "Link: "
  6265. (append
  6266. (mapcar (lambda (x) (list (concat (car x) ":")))
  6267. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6268. (mapcar (lambda (x) (list (concat x ":")))
  6269. org-link-types))
  6270. nil nil nil
  6271. 'tmphist
  6272. (or (car (car org-stored-links))))))
  6273. (set-window-configuration wcf)
  6274. (kill-buffer "*Org Links*"))
  6275. (setq entry (assoc link org-stored-links))
  6276. (or entry (push link org-insert-link-history))
  6277. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6278. (not org-keep-stored-link-after-insertion))
  6279. (setq org-stored-links (delq (assoc link org-stored-links)
  6280. org-stored-links)))
  6281. (setq desc (or desc (nth 1 entry)))))
  6282. (if (string-match org-plain-link-re link)
  6283. ;; URL-like link, normalize the use of angular brackets.
  6284. (setq link (org-make-link (org-remove-angle-brackets link))))
  6285. ;; Check if we are linking to the current file with a search option
  6286. ;; If yes, simplify the link by using only the search option.
  6287. (when (and buffer-file-name
  6288. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6289. (let* ((path (match-string 1 link))
  6290. (case-fold-search nil)
  6291. (search (match-string 2 link)))
  6292. (save-match-data
  6293. (if (equal (file-truename buffer-file-name) (file-truename path))
  6294. ;; We are linking to this same file, with a search option
  6295. (setq link search)))))
  6296. ;; Check if we can/should use a relative path. If yes, simplify the link
  6297. (when (string-match "^file:\\(.*\\)" link)
  6298. (let* ((path (match-string 1 link))
  6299. (origpath path)
  6300. (case-fold-search nil))
  6301. (cond
  6302. ((or (eq org-link-file-path-type 'absolute)
  6303. (equal complete-file '(16)))
  6304. (setq path (abbreviate-file-name (expand-file-name path))))
  6305. ((eq org-link-file-path-type 'noabbrev)
  6306. (setq path (expand-file-name path)))
  6307. ((eq org-link-file-path-type 'relative)
  6308. (setq path (file-relative-name path)))
  6309. (t
  6310. (save-match-data
  6311. (if (string-match (concat "^" (regexp-quote
  6312. (file-name-as-directory
  6313. (expand-file-name "."))))
  6314. (expand-file-name path))
  6315. ;; We are linking a file with relative path name.
  6316. (setq path (substring (expand-file-name path)
  6317. (match-end 0)))
  6318. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6319. (setq link (concat "file:" path))
  6320. (if (equal desc origpath)
  6321. (setq desc path))))
  6322. (if org-make-link-description-function
  6323. (setq desc (funcall org-make-link-description-function link desc)))
  6324. (setq desc (read-string "Description: " desc))
  6325. (unless (string-match "\\S-" desc) (setq desc nil))
  6326. (if remove (apply 'delete-region remove))
  6327. (insert (org-make-link-string link desc))))
  6328. (defun org-completing-read (&rest args)
  6329. "Completing-read with SPACE being a normal character."
  6330. (let ((minibuffer-local-completion-map
  6331. (copy-keymap minibuffer-local-completion-map)))
  6332. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6333. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6334. (apply 'org-ido-completing-read args)))
  6335. (defun org-ido-completing-read (&rest args)
  6336. "Completing-read using `ido-mode' speedups if available"
  6337. (if (and org-completion-use-ido
  6338. (fboundp 'ido-completing-read)
  6339. (boundp 'ido-mode) ido-mode
  6340. (listp (second args)))
  6341. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6342. (apply 'completing-read args)))
  6343. (defun org-extract-attributes (s)
  6344. "Extract the attributes cookie from a string and set as text property."
  6345. (let (a attr (start 0) key value)
  6346. (save-match-data
  6347. (when (string-match "{{\\([^}]+\\)}}$" s)
  6348. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6349. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6350. (setq key (match-string 1 a) value (match-string 2 a)
  6351. start (match-end 0)
  6352. attr (plist-put attr (intern key) value))))
  6353. (org-add-props s nil 'org-attr attr))
  6354. s))
  6355. (defun org-attributes-to-string (plist)
  6356. "Format a property list into an HTML attribute list."
  6357. (let ((s "") key value)
  6358. (while plist
  6359. (setq key (pop plist) value (pop plist))
  6360. (and value
  6361. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6362. s))
  6363. ;;; Opening/following a link
  6364. (defvar org-link-search-failed nil)
  6365. (defun org-next-link ()
  6366. "Move forward to the next link.
  6367. If the link is in hidden text, expose it."
  6368. (interactive)
  6369. (when (and org-link-search-failed (eq this-command last-command))
  6370. (goto-char (point-min))
  6371. (message "Link search wrapped back to beginning of buffer"))
  6372. (setq org-link-search-failed nil)
  6373. (let* ((pos (point))
  6374. (ct (org-context))
  6375. (a (assoc :link ct)))
  6376. (if a (goto-char (nth 2 a)))
  6377. (if (re-search-forward org-any-link-re nil t)
  6378. (progn
  6379. (goto-char (match-beginning 0))
  6380. (if (org-invisible-p) (org-show-context)))
  6381. (goto-char pos)
  6382. (setq org-link-search-failed t)
  6383. (error "No further link found"))))
  6384. (defun org-previous-link ()
  6385. "Move backward to the previous link.
  6386. If the link is in hidden text, expose it."
  6387. (interactive)
  6388. (when (and org-link-search-failed (eq this-command last-command))
  6389. (goto-char (point-max))
  6390. (message "Link search wrapped back to end of buffer"))
  6391. (setq org-link-search-failed nil)
  6392. (let* ((pos (point))
  6393. (ct (org-context))
  6394. (a (assoc :link ct)))
  6395. (if a (goto-char (nth 1 a)))
  6396. (if (re-search-backward org-any-link-re nil t)
  6397. (progn
  6398. (goto-char (match-beginning 0))
  6399. (if (org-invisible-p) (org-show-context)))
  6400. (goto-char pos)
  6401. (setq org-link-search-failed t)
  6402. (error "No further link found"))))
  6403. (defun org-translate-link (s)
  6404. "Translate a link string if a translation function has been defined."
  6405. (if (and org-link-translation-function
  6406. (fboundp org-link-translation-function)
  6407. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6408. (progn
  6409. (setq s (funcall org-link-translation-function
  6410. (match-string 1) (match-string 2)))
  6411. (concat (car s) ":" (cdr s)))
  6412. s))
  6413. (defun org-translate-link-from-planner (type path)
  6414. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6415. This is still an experimental function, your mileage may vary."
  6416. (cond
  6417. ((member type '("http" "https" "news" "ftp"))
  6418. ;; standard Internet links are the same.
  6419. nil)
  6420. ((and (equal type "irc") (string-match "^//" path))
  6421. ;; Planner has two / at the beginning of an irc link, we have 1.
  6422. ;; We should have zero, actually....
  6423. (setq path (substring path 1)))
  6424. ((and (equal type "lisp") (string-match "^/" path))
  6425. ;; Planner has a slash, we do not.
  6426. (setq type "elisp" path (substring path 1)))
  6427. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6428. ;; A typical message link. Planner has the id after the fina slash,
  6429. ;; we separate it with a hash mark
  6430. (setq path (concat (match-string 1 path) "#"
  6431. (org-remove-angle-brackets (match-string 2 path)))))
  6432. )
  6433. (cons type path))
  6434. (defun org-find-file-at-mouse (ev)
  6435. "Open file link or URL at mouse."
  6436. (interactive "e")
  6437. (mouse-set-point ev)
  6438. (org-open-at-point 'in-emacs))
  6439. (defun org-open-at-mouse (ev)
  6440. "Open file link or URL at mouse."
  6441. (interactive "e")
  6442. (mouse-set-point ev)
  6443. (if (eq major-mode 'org-agenda-mode)
  6444. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6445. (org-open-at-point))
  6446. (defvar org-window-config-before-follow-link nil
  6447. "The window configuration before following a link.
  6448. This is saved in case the need arises to restore it.")
  6449. (defvar org-open-link-marker (make-marker)
  6450. "Marker pointing to the location where `org-open-at-point; was called.")
  6451. ;;;###autoload
  6452. (defun org-open-at-point-global ()
  6453. "Follow a link like Org-mode does.
  6454. This command can be called in any mode to follow a link that has
  6455. Org-mode syntax."
  6456. (interactive)
  6457. (org-run-like-in-org-mode 'org-open-at-point))
  6458. ;;;###autoload
  6459. (defun org-open-link-from-string (s &optional arg)
  6460. "Open a link in the string S, as if it was in Org-mode."
  6461. (interactive "sLink: \nP")
  6462. (with-temp-buffer
  6463. (let ((org-inhibit-startup t))
  6464. (org-mode)
  6465. (insert s)
  6466. (goto-char (point-min))
  6467. (org-open-at-point arg))))
  6468. (defun org-open-at-point (&optional in-emacs)
  6469. "Open link at or after point.
  6470. If there is no link at point, this function will search forward up to
  6471. the end of the current subtree.
  6472. Normally, files will be opened by an appropriate application. If the
  6473. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6474. With a double prefix argument, try to open outside of Emacs, in the
  6475. application the system uses for this file type."
  6476. (interactive "P")
  6477. (org-load-modules-maybe)
  6478. (move-marker org-open-link-marker (point))
  6479. (setq org-window-config-before-follow-link (current-window-configuration))
  6480. (org-remove-occur-highlights nil nil t)
  6481. (cond
  6482. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6483. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6484. (org-footnote-action))
  6485. (t
  6486. (let (type path link line search (pos (point)))
  6487. (catch 'match
  6488. (save-excursion
  6489. (skip-chars-forward "^]\n\r")
  6490. (when (org-in-regexp org-bracket-link-regexp)
  6491. (setq link (org-extract-attributes
  6492. (org-link-unescape (org-match-string-no-properties 1))))
  6493. (while (string-match " *\n *" link)
  6494. (setq link (replace-match " " t t link)))
  6495. (setq link (org-link-expand-abbrev link))
  6496. (cond
  6497. ((or (file-name-absolute-p link)
  6498. (string-match "^\\.\\.?/" link))
  6499. (setq type "file" path link))
  6500. ((string-match org-link-re-with-space3 link)
  6501. (setq type (match-string 1 link) path (match-string 2 link)))
  6502. (t (setq type "thisfile" path link)))
  6503. (throw 'match t)))
  6504. (when (get-text-property (point) 'org-linked-text)
  6505. (setq type "thisfile"
  6506. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6507. (1+ (point)) (point))
  6508. path (buffer-substring
  6509. (previous-single-property-change pos 'org-linked-text)
  6510. (next-single-property-change pos 'org-linked-text)))
  6511. (throw 'match t))
  6512. (save-excursion
  6513. (when (or (org-in-regexp org-angle-link-re)
  6514. (org-in-regexp org-plain-link-re))
  6515. (setq type (match-string 1) path (match-string 2))
  6516. (throw 'match t)))
  6517. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6518. (setq type "tree-match"
  6519. path (match-string 1))
  6520. (throw 'match t))
  6521. (save-excursion
  6522. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6523. (setq type "tags"
  6524. path (match-string 1))
  6525. (while (string-match ":" path)
  6526. (setq path (replace-match "+" t t path)))
  6527. (throw 'match t))))
  6528. (unless path
  6529. (error "No link found"))
  6530. ;; Remove any trailing spaces in path
  6531. (if (string-match " +\\'" path)
  6532. (setq path (replace-match "" t t path)))
  6533. (if (and org-link-translation-function
  6534. (fboundp org-link-translation-function))
  6535. ;; Check if we need to translate the link
  6536. (let ((tmp (funcall org-link-translation-function type path)))
  6537. (setq type (car tmp) path (cdr tmp))))
  6538. (cond
  6539. ((assoc type org-link-protocols)
  6540. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6541. ((equal type "mailto")
  6542. (let ((cmd (car org-link-mailto-program))
  6543. (args (cdr org-link-mailto-program)) args1
  6544. (address path) (subject "") a)
  6545. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6546. (setq address (match-string 1 path)
  6547. subject (org-link-escape (match-string 2 path))))
  6548. (while args
  6549. (cond
  6550. ((not (stringp (car args))) (push (pop args) args1))
  6551. (t (setq a (pop args))
  6552. (if (string-match "%a" a)
  6553. (setq a (replace-match address t t a)))
  6554. (if (string-match "%s" a)
  6555. (setq a (replace-match subject t t a)))
  6556. (push a args1))))
  6557. (apply cmd (nreverse args1))))
  6558. ((member type '("http" "https" "ftp" "news"))
  6559. (browse-url (concat type ":" (org-link-escape
  6560. path org-link-escape-chars-browser))))
  6561. ((member type '("message"))
  6562. (browse-url (concat type ":" path)))
  6563. ((string= type "tags")
  6564. (org-tags-view in-emacs path))
  6565. ((string= type "thisfile")
  6566. (if in-emacs
  6567. (switch-to-buffer-other-window
  6568. (org-get-buffer-for-internal-link (current-buffer)))
  6569. (org-mark-ring-push))
  6570. (let ((cmd `(org-link-search
  6571. ,path
  6572. ,(cond ((equal in-emacs '(4)) 'occur)
  6573. ((equal in-emacs '(16)) 'org-occur)
  6574. (t nil))
  6575. ,pos)))
  6576. (condition-case nil (eval cmd)
  6577. (error (progn (widen) (eval cmd))))))
  6578. ((string= type "tree-match")
  6579. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6580. ((string= type "file")
  6581. (if (string-match "::\\([0-9]+\\)\\'" path)
  6582. (setq line (string-to-number (match-string 1 path))
  6583. path (substring path 0 (match-beginning 0)))
  6584. (if (string-match "::\\(.+\\)\\'" path)
  6585. (setq search (match-string 1 path)
  6586. path (substring path 0 (match-beginning 0)))))
  6587. (if (string-match "[*?{]" (file-name-nondirectory path))
  6588. (dired path)
  6589. (org-open-file path in-emacs line search)))
  6590. ((string= type "news")
  6591. (require 'org-gnus)
  6592. (org-gnus-follow-link path))
  6593. ((string= type "shell")
  6594. (let ((cmd path))
  6595. (if (or (not org-confirm-shell-link-function)
  6596. (funcall org-confirm-shell-link-function
  6597. (format "Execute \"%s\" in shell? "
  6598. (org-add-props cmd nil
  6599. 'face 'org-warning))))
  6600. (progn
  6601. (message "Executing %s" cmd)
  6602. (shell-command cmd))
  6603. (error "Abort"))))
  6604. ((string= type "elisp")
  6605. (let ((cmd path))
  6606. (if (or (not org-confirm-elisp-link-function)
  6607. (funcall org-confirm-elisp-link-function
  6608. (format "Execute \"%s\" as elisp? "
  6609. (org-add-props cmd nil
  6610. 'face 'org-warning))))
  6611. (message "%s => %s" cmd
  6612. (if (equal (string-to-char cmd) ?\()
  6613. (eval (read cmd))
  6614. (call-interactively (read cmd))))
  6615. (error "Abort"))))
  6616. (t
  6617. (browse-url-at-point))))))
  6618. (move-marker org-open-link-marker nil)
  6619. (run-hook-with-args 'org-follow-link-hook))
  6620. ;;;; Time estimates
  6621. (defun org-get-effort (&optional pom)
  6622. "Get the effort estimate for the current entry."
  6623. (org-entry-get pom org-effort-property))
  6624. ;;; File search
  6625. (defvar org-create-file-search-functions nil
  6626. "List of functions to construct the right search string for a file link.
  6627. These functions are called in turn with point at the location to
  6628. which the link should point.
  6629. A function in the hook should first test if it would like to
  6630. handle this file type, for example by checking the major-mode or
  6631. the file extension. If it decides not to handle this file, it
  6632. should just return nil to give other functions a chance. If it
  6633. does handle the file, it must return the search string to be used
  6634. when following the link. The search string will be part of the
  6635. file link, given after a double colon, and `org-open-at-point'
  6636. will automatically search for it. If special measures must be
  6637. taken to make the search successful, another function should be
  6638. added to the companion hook `org-execute-file-search-functions',
  6639. which see.
  6640. A function in this hook may also use `setq' to set the variable
  6641. `description' to provide a suggestion for the descriptive text to
  6642. be used for this link when it gets inserted into an Org-mode
  6643. buffer with \\[org-insert-link].")
  6644. (defvar org-execute-file-search-functions nil
  6645. "List of functions to execute a file search triggered by a link.
  6646. Functions added to this hook must accept a single argument, the
  6647. search string that was part of the file link, the part after the
  6648. double colon. The function must first check if it would like to
  6649. handle this search, for example by checking the major-mode or the
  6650. file extension. If it decides not to handle this search, it
  6651. should just return nil to give other functions a chance. If it
  6652. does handle the search, it must return a non-nil value to keep
  6653. other functions from trying.
  6654. Each function can access the current prefix argument through the
  6655. variable `current-prefix-argument'. Note that a single prefix is
  6656. used to force opening a link in Emacs, so it may be good to only
  6657. use a numeric or double prefix to guide the search function.
  6658. In case this is needed, a function in this hook can also restore
  6659. the window configuration before `org-open-at-point' was called using:
  6660. (set-window-configuration org-window-config-before-follow-link)")
  6661. (defun org-link-search (s &optional type avoid-pos)
  6662. "Search for a link search option.
  6663. If S is surrounded by forward slashes, it is interpreted as a
  6664. regular expression. In org-mode files, this will create an `org-occur'
  6665. sparse tree. In ordinary files, `occur' will be used to list matches.
  6666. If the current buffer is in `dired-mode', grep will be used to search
  6667. in all files. If AVOID-POS is given, ignore matches near that position."
  6668. (let ((case-fold-search t)
  6669. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6670. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6671. (append '(("") (" ") ("\t") ("\n"))
  6672. org-emphasis-alist)
  6673. "\\|") "\\)"))
  6674. (pos (point))
  6675. (pre nil) (post nil)
  6676. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6677. (cond
  6678. ;; First check if there are any special
  6679. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6680. ;; Now try the builtin stuff
  6681. ((save-excursion
  6682. (goto-char (point-min))
  6683. (and
  6684. (re-search-forward
  6685. (concat "<<" (regexp-quote s0) ">>") nil t)
  6686. (setq type 'dedicated
  6687. pos (match-beginning 0))))
  6688. ;; There is an exact target for this
  6689. (goto-char pos))
  6690. ((and (string-match "^(\\(.*\\))$" s0)
  6691. (save-excursion
  6692. (goto-char (point-min))
  6693. (and
  6694. (re-search-forward
  6695. (concat "[^[]" (regexp-quote
  6696. (format org-coderef-label-format
  6697. (match-string 1 s0))))
  6698. nil t)
  6699. (setq type 'dedicated
  6700. pos (1+ (match-beginning 0))))))
  6701. ;; There is a coderef target for this
  6702. (goto-char pos))
  6703. ((string-match "^/\\(.*\\)/$" s)
  6704. ;; A regular expression
  6705. (cond
  6706. ((org-mode-p)
  6707. (org-occur (match-string 1 s)))
  6708. ;;((eq major-mode 'dired-mode)
  6709. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6710. (t (org-do-occur (match-string 1 s)))))
  6711. (t
  6712. ;; A normal search strings
  6713. (when (equal (string-to-char s) ?*)
  6714. ;; Anchor on headlines, post may include tags.
  6715. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6716. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6717. s (substring s 1)))
  6718. (remove-text-properties
  6719. 0 (length s)
  6720. '(face nil mouse-face nil keymap nil fontified nil) s)
  6721. ;; Make a series of regular expressions to find a match
  6722. (setq words (org-split-string s "[ \n\r\t]+")
  6723. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6724. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6725. "\\)" markers)
  6726. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6727. re2a (concat "[ \t\r\n]" re2a_)
  6728. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6729. re4 (concat "[^a-zA-Z_]" re4_)
  6730. re1 (concat pre re2 post)
  6731. re3 (concat pre (if pre re4_ re4) post)
  6732. re5 (concat pre ".*" re4)
  6733. re2 (concat pre re2)
  6734. re2a (concat pre (if pre re2a_ re2a))
  6735. re4 (concat pre (if pre re4_ re4))
  6736. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6737. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6738. re5 "\\)"
  6739. ))
  6740. (cond
  6741. ((eq type 'org-occur) (org-occur reall))
  6742. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6743. (t (goto-char (point-min))
  6744. (setq type 'fuzzy)
  6745. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6746. (org-search-not-self 1 re1 nil t)
  6747. (org-search-not-self 1 re2 nil t)
  6748. (org-search-not-self 1 re2a nil t)
  6749. (org-search-not-self 1 re3 nil t)
  6750. (org-search-not-self 1 re4 nil t)
  6751. (org-search-not-self 1 re5 nil t)
  6752. )
  6753. (goto-char (match-beginning 1))
  6754. (goto-char pos)
  6755. (error "No match")))))
  6756. (t
  6757. ;; Normal string-search
  6758. (goto-char (point-min))
  6759. (if (search-forward s nil t)
  6760. (goto-char (match-beginning 0))
  6761. (error "No match"))))
  6762. (and (org-mode-p) (org-show-context 'link-search))
  6763. type))
  6764. (defun org-search-not-self (group &rest args)
  6765. "Execute `re-search-forward', but only accept matches that do not
  6766. enclose the position of `org-open-link-marker'."
  6767. (let ((m org-open-link-marker))
  6768. (catch 'exit
  6769. (while (apply 're-search-forward args)
  6770. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6771. (goto-char (match-end group))
  6772. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6773. (> (match-beginning 0) (marker-position m))
  6774. (< (match-end 0) (marker-position m)))
  6775. (save-match-data
  6776. (or (not (org-in-regexp
  6777. org-bracket-link-analytic-regexp 1))
  6778. (not (match-end 4)) ; no description
  6779. (and (<= (match-beginning 4) (point))
  6780. (>= (match-end 4) (point))))))
  6781. (throw 'exit (point))))))))
  6782. (defun org-get-buffer-for-internal-link (buffer)
  6783. "Return a buffer to be used for displaying the link target of internal links."
  6784. (cond
  6785. ((not org-display-internal-link-with-indirect-buffer)
  6786. buffer)
  6787. ((string-match "(Clone)$" (buffer-name buffer))
  6788. (message "Buffer is already a clone, not making another one")
  6789. ;; we also do not modify visibility in this case
  6790. buffer)
  6791. (t ; make a new indirect buffer for displaying the link
  6792. (let* ((bn (buffer-name buffer))
  6793. (ibn (concat bn "(Clone)"))
  6794. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6795. (with-current-buffer ib (org-overview))
  6796. ib))))
  6797. (defun org-do-occur (regexp &optional cleanup)
  6798. "Call the Emacs command `occur'.
  6799. If CLEANUP is non-nil, remove the printout of the regular expression
  6800. in the *Occur* buffer. This is useful if the regex is long and not useful
  6801. to read."
  6802. (occur regexp)
  6803. (when cleanup
  6804. (let ((cwin (selected-window)) win beg end)
  6805. (when (setq win (get-buffer-window "*Occur*"))
  6806. (select-window win))
  6807. (goto-char (point-min))
  6808. (when (re-search-forward "match[a-z]+" nil t)
  6809. (setq beg (match-end 0))
  6810. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6811. (setq end (1- (match-beginning 0)))))
  6812. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6813. (goto-char (point-min))
  6814. (select-window cwin))))
  6815. ;;; The mark ring for links jumps
  6816. (defvar org-mark-ring nil
  6817. "Mark ring for positions before jumps in Org-mode.")
  6818. (defvar org-mark-ring-last-goto nil
  6819. "Last position in the mark ring used to go back.")
  6820. ;; Fill and close the ring
  6821. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6822. (loop for i from 1 to org-mark-ring-length do
  6823. (push (make-marker) org-mark-ring))
  6824. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6825. org-mark-ring)
  6826. (defun org-mark-ring-push (&optional pos buffer)
  6827. "Put the current position or POS into the mark ring and rotate it."
  6828. (interactive)
  6829. (setq pos (or pos (point)))
  6830. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6831. (move-marker (car org-mark-ring)
  6832. (or pos (point))
  6833. (or buffer (current-buffer)))
  6834. (message "%s"
  6835. (substitute-command-keys
  6836. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6837. (defun org-mark-ring-goto (&optional n)
  6838. "Jump to the previous position in the mark ring.
  6839. With prefix arg N, jump back that many stored positions. When
  6840. called several times in succession, walk through the entire ring.
  6841. Org-mode commands jumping to a different position in the current file,
  6842. or to another Org-mode file, automatically push the old position
  6843. onto the ring."
  6844. (interactive "p")
  6845. (let (p m)
  6846. (if (eq last-command this-command)
  6847. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6848. (setq p org-mark-ring))
  6849. (setq org-mark-ring-last-goto p)
  6850. (setq m (car p))
  6851. (switch-to-buffer (marker-buffer m))
  6852. (goto-char m)
  6853. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6854. (defun org-remove-angle-brackets (s)
  6855. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6856. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6857. s)
  6858. (defun org-add-angle-brackets (s)
  6859. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6860. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6861. s)
  6862. (defun org-remove-double-quotes (s)
  6863. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6864. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6865. s)
  6866. ;;; Following specific links
  6867. (defun org-follow-timestamp-link ()
  6868. (cond
  6869. ((org-at-date-range-p t)
  6870. (let ((org-agenda-start-on-weekday)
  6871. (t1 (match-string 1))
  6872. (t2 (match-string 2)))
  6873. (setq t1 (time-to-days (org-time-string-to-time t1))
  6874. t2 (time-to-days (org-time-string-to-time t2)))
  6875. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6876. ((org-at-timestamp-p t)
  6877. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6878. (substring (match-string 1) 0 10)))
  6879. 1))
  6880. (t (error "This should not happen"))))
  6881. ;;; Following file links
  6882. (defvar org-wait nil)
  6883. (defun org-open-file (path &optional in-emacs line search)
  6884. "Open the file at PATH.
  6885. First, this expands any special file name abbreviations. Then the
  6886. configuration variable `org-file-apps' is checked if it contains an
  6887. entry for this file type, and if yes, the corresponding command is launched.
  6888. If no application is found, Emacs simply visits the file.
  6889. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6890. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6891. and o use an external application to visit the file.
  6892. Optional LINE specifies a line to go to, optional SEARCH a string to
  6893. search for. If LINE or SEARCH is given, the file will always be
  6894. opened in Emacs.
  6895. If the file does not exist, an error is thrown."
  6896. (setq in-emacs (or in-emacs line search))
  6897. (let* ((file (if (equal path "")
  6898. buffer-file-name
  6899. (substitute-in-file-name (expand-file-name path))))
  6900. (apps (append org-file-apps (org-default-apps)))
  6901. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6902. (dirp (if remp nil (file-directory-p file)))
  6903. (file (if (and dirp org-open-directory-means-index-dot-org)
  6904. (concat (file-name-as-directory file) "index.org")
  6905. file))
  6906. (a-m-a-p (assq 'auto-mode apps))
  6907. (dfile (downcase file))
  6908. (old-buffer (current-buffer))
  6909. (old-pos (point))
  6910. (old-mode major-mode)
  6911. ext cmd)
  6912. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6913. (setq ext (match-string 1 dfile))
  6914. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6915. (setq ext (match-string 1 dfile))))
  6916. (cond
  6917. ((equal in-emacs '(16))
  6918. (setq cmd (cdr (assoc 'system apps))))
  6919. (in-emacs (setq cmd 'emacs))
  6920. (t
  6921. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6922. (and dirp (cdr (assoc 'directory apps)))
  6923. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6924. 'string-match)
  6925. (cdr (assoc ext apps))
  6926. (cdr (assoc t apps))))))
  6927. (when (eq cmd 'system)
  6928. (setq cmd (cdr (assoc 'system apps))))
  6929. (when (eq cmd 'default)
  6930. (setq cmd (cdr (assoc t apps))))
  6931. (when (eq cmd 'mailcap)
  6932. (require 'mailcap)
  6933. (mailcap-parse-mailcaps)
  6934. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6935. (command (mailcap-mime-info mime-type)))
  6936. (if (stringp command)
  6937. (setq cmd command)
  6938. (setq cmd 'emacs))))
  6939. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6940. (not (file-exists-p file))
  6941. (not org-open-non-existing-files))
  6942. (error "No such file: %s" file))
  6943. (cond
  6944. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6945. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6946. (while (string-match "['\"]%s['\"]" cmd)
  6947. (setq cmd (replace-match "%s" t t cmd)))
  6948. (while (string-match "%s" cmd)
  6949. (setq cmd (replace-match
  6950. (save-match-data
  6951. (shell-quote-argument
  6952. (convert-standard-filename file)))
  6953. t t cmd)))
  6954. (save-window-excursion
  6955. (start-process-shell-command cmd nil cmd)
  6956. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6957. ))
  6958. ((or (stringp cmd)
  6959. (eq cmd 'emacs))
  6960. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6961. (widen)
  6962. (if line (goto-line line)
  6963. (if search (org-link-search search))))
  6964. ((consp cmd)
  6965. (let ((file (convert-standard-filename file)))
  6966. (eval cmd)))
  6967. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6968. (and (org-mode-p) (eq old-mode 'org-mode)
  6969. (or (not (equal old-buffer (current-buffer)))
  6970. (not (equal old-pos (point))))
  6971. (org-mark-ring-push old-pos old-buffer))))
  6972. (defun org-default-apps ()
  6973. "Return the default applications for this operating system."
  6974. (cond
  6975. ((eq system-type 'darwin)
  6976. org-file-apps-defaults-macosx)
  6977. ((eq system-type 'windows-nt)
  6978. org-file-apps-defaults-windowsnt)
  6979. (t org-file-apps-defaults-gnu)))
  6980. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6981. "Convert extensions to regular expressions in the cars of LIST.
  6982. Also, weed out any non-string entries, because the return value is used
  6983. only for regexp matching.
  6984. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6985. point to the symbol `emacs', indicating that the file should
  6986. be opened in Emacs."
  6987. (append
  6988. (delq nil
  6989. (mapcar (lambda (x)
  6990. (if (not (stringp (car x)))
  6991. nil
  6992. (if (string-match "\\W" (car x))
  6993. x
  6994. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6995. list))
  6996. (if add-auto-mode
  6997. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6998. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6999. (defun org-file-remote-p (file)
  7000. "Test whether FILE specifies a location on a remote system.
  7001. Return non-nil if the location is indeed remote.
  7002. For example, the filename \"/user@host:/foo\" specifies a location
  7003. on the system \"/user@host:\"."
  7004. (cond ((fboundp 'file-remote-p)
  7005. (file-remote-p file))
  7006. ((fboundp 'tramp-handle-file-remote-p)
  7007. (tramp-handle-file-remote-p file))
  7008. ((and (boundp 'ange-ftp-name-format)
  7009. (string-match (car ange-ftp-name-format) file))
  7010. t)
  7011. (t nil)))
  7012. ;;;; Refiling
  7013. (defun org-get-org-file ()
  7014. "Read a filename, with default directory `org-directory'."
  7015. (let ((default (or org-default-notes-file remember-data-file)))
  7016. (read-file-name (format "File name [%s]: " default)
  7017. (file-name-as-directory org-directory)
  7018. default)))
  7019. (defun org-notes-order-reversed-p ()
  7020. "Check if the current file should receive notes in reversed order."
  7021. (cond
  7022. ((not org-reverse-note-order) nil)
  7023. ((eq t org-reverse-note-order) t)
  7024. ((not (listp org-reverse-note-order)) nil)
  7025. (t (catch 'exit
  7026. (let ((all org-reverse-note-order)
  7027. entry)
  7028. (while (setq entry (pop all))
  7029. (if (string-match (car entry) buffer-file-name)
  7030. (throw 'exit (cdr entry))))
  7031. nil)))))
  7032. (defvar org-refile-target-table nil
  7033. "The list of refile targets, created by `org-refile'.")
  7034. (defvar org-agenda-new-buffers nil
  7035. "Buffers created to visit agenda files.")
  7036. (defun org-get-refile-targets (&optional default-buffer)
  7037. "Produce a table with refile targets."
  7038. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7039. targets txt re files f desc descre fast-path-p level)
  7040. (message "Getting targets...")
  7041. (with-current-buffer (or default-buffer (current-buffer))
  7042. (while (setq entry (pop entries))
  7043. (setq files (car entry) desc (cdr entry))
  7044. (setq fast-path-p nil)
  7045. (cond
  7046. ((null files) (setq files (list (current-buffer))))
  7047. ((eq files 'org-agenda-files)
  7048. (setq files (org-agenda-files 'unrestricted)))
  7049. ((and (symbolp files) (fboundp files))
  7050. (setq files (funcall files)))
  7051. ((and (symbolp files) (boundp files))
  7052. (setq files (symbol-value files))))
  7053. (if (stringp files) (setq files (list files)))
  7054. (cond
  7055. ((eq (car desc) :tag)
  7056. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7057. ((eq (car desc) :todo)
  7058. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7059. ((eq (car desc) :regexp)
  7060. (setq descre (cdr desc)))
  7061. ((eq (car desc) :level)
  7062. (setq descre (concat "^\\*\\{" (number-to-string
  7063. (if org-odd-levels-only
  7064. (1- (* 2 (cdr desc)))
  7065. (cdr desc)))
  7066. "\\}[ \t]")))
  7067. ((eq (car desc) :maxlevel)
  7068. (setq fast-path-p t)
  7069. (setq descre (concat "^\\*\\{1," (number-to-string
  7070. (if org-odd-levels-only
  7071. (1- (* 2 (cdr desc)))
  7072. (cdr desc)))
  7073. "\\}[ \t]")))
  7074. (t (error "Bad refiling target description %s" desc)))
  7075. (while (setq f (pop files))
  7076. (save-excursion
  7077. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7078. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7079. (setq f (expand-file-name f))
  7080. (save-excursion
  7081. (save-restriction
  7082. (widen)
  7083. (goto-char (point-min))
  7084. (while (re-search-forward descre nil t)
  7085. (goto-char (point-at-bol))
  7086. (when (looking-at org-complex-heading-regexp)
  7087. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7088. txt (org-link-display-format (match-string 4))
  7089. re (concat "^" (regexp-quote
  7090. (buffer-substring (match-beginning 1)
  7091. (match-end 4)))))
  7092. (if (match-end 5) (setq re (concat re "[ \t]+"
  7093. (regexp-quote
  7094. (match-string 5)))))
  7095. (setq re (concat re "[ \t]*$"))
  7096. (when org-refile-use-outline-path
  7097. (setq txt (mapconcat 'org-protect-slash
  7098. (append
  7099. (if (eq org-refile-use-outline-path 'file)
  7100. (list (file-name-nondirectory
  7101. (buffer-file-name (buffer-base-buffer))))
  7102. (if (eq org-refile-use-outline-path 'full-file-path)
  7103. (list (buffer-file-name (buffer-base-buffer)))))
  7104. (org-get-outline-path fast-path-p level txt)
  7105. (list txt))
  7106. "/")))
  7107. (push (list txt f re (point)) targets))
  7108. (goto-char (point-at-eol))))))))
  7109. (message "Getting targets...done")
  7110. (nreverse targets))))
  7111. (defun org-protect-slash (s)
  7112. (while (string-match "/" s)
  7113. (setq s (replace-match "\\" t t s)))
  7114. s)
  7115. (defvar org-olpa (make-vector 20 nil))
  7116. (defun org-get-outline-path (&optional fastp level heading)
  7117. "Return the outline path to the current entry, as a list."
  7118. (if fastp
  7119. (progn
  7120. (if (> level 19)
  7121. (error "Outline path failure, more than 19 levels."))
  7122. (loop for i from level upto 19 do
  7123. (aset org-olpa i nil))
  7124. (prog1
  7125. (delq nil (append org-olpa nil))
  7126. (aset org-olpa level heading)))
  7127. (let (rtn)
  7128. (save-excursion
  7129. (while (org-up-heading-safe)
  7130. (when (looking-at org-complex-heading-regexp)
  7131. (push (org-match-string-no-properties 4) rtn)))
  7132. rtn))))
  7133. (defvar org-refile-history nil
  7134. "History for refiling operations.")
  7135. (defun org-refile (&optional goto default-buffer)
  7136. "Move the entry at point to another heading.
  7137. The list of target headings is compiled using the information in
  7138. `org-refile-targets', which see. This list is created before each use
  7139. and will therefore always be up-to-date.
  7140. At the target location, the entry is filed as a subitem of the target heading.
  7141. Depending on `org-reverse-note-order', the new subitem will either be the
  7142. first or the last subitem.
  7143. If there is an active region, all entries in that region will be moved.
  7144. However, the region must fulfil the requirement that the first heading
  7145. is the first one sets the top-level of the moved text - at most siblings
  7146. below it are allowed.
  7147. With prefix arg GOTO, the command will only visit the target location,
  7148. not actually move anything.
  7149. With a double prefix `C-u C-u', go to the location where the last refiling
  7150. operation has put the subtree."
  7151. (interactive "P")
  7152. (let* ((cbuf (current-buffer))
  7153. (regionp (org-region-active-p))
  7154. (region-start (and regionp (region-beginning)))
  7155. (region-end (and regionp (region-end)))
  7156. (region-length (and regionp (- region-end region-start)))
  7157. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7158. pos it nbuf file re level reversed)
  7159. (when regionp (goto-char region-start)
  7160. (unless (org-kill-is-subtree-p
  7161. (buffer-substring region-start region-end))
  7162. (error "The region is not a (sequence of) subtree(s)")))
  7163. (if (equal goto '(16))
  7164. (org-refile-goto-last-stored)
  7165. (when (setq it (org-refile-get-location
  7166. (if goto "Goto: " "Refile to: ") default-buffer))
  7167. (setq file (nth 1 it)
  7168. re (nth 2 it)
  7169. pos (nth 3 it))
  7170. (if (and (equal (buffer-file-name) file)
  7171. (if regionp
  7172. (and (>= pos region-start)
  7173. (<= pos region-end))
  7174. (and (>= pos (point))
  7175. (< pos (save-excursion
  7176. (org-end-of-subtree t t))))))
  7177. (error "Cannot refile to position inside the tree or region"))
  7178. (setq nbuf (or (find-buffer-visiting file)
  7179. (find-file-noselect file)))
  7180. (if goto
  7181. (progn
  7182. (switch-to-buffer nbuf)
  7183. (goto-char pos)
  7184. (org-show-context 'org-goto))
  7185. (if regionp
  7186. (progn
  7187. (kill-new (buffer-substring region-start region-end))
  7188. (org-save-markers-in-region region-start region-end))
  7189. (org-copy-subtree 1 nil t))
  7190. (save-excursion
  7191. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7192. (find-file-noselect file))))
  7193. (setq reversed (org-notes-order-reversed-p))
  7194. (save-excursion
  7195. (save-restriction
  7196. (widen)
  7197. (goto-char pos)
  7198. (looking-at outline-regexp)
  7199. (setq level (org-get-valid-level (funcall outline-level) 1))
  7200. (goto-char
  7201. (if reversed
  7202. (or (outline-next-heading) (point-max))
  7203. (or (save-excursion (outline-get-next-sibling))
  7204. (org-end-of-subtree t t)
  7205. (point-max))))
  7206. (if (not (bolp)) (newline))
  7207. (bookmark-set "org-refile-last-stored")
  7208. (org-paste-subtree level))))
  7209. (if regionp
  7210. (delete-region (point) (+ (point) region-length))
  7211. (org-cut-subtree))
  7212. (setq org-markers-to-move nil)
  7213. (message "Refiled to \"%s\"" (car it)))))))
  7214. (defun org-refile-goto-last-stored ()
  7215. "Go to the location where the last refile was stored."
  7216. (interactive)
  7217. (bookmark-jump "org-refile-last-stored")
  7218. (message "This is the location of the last refile"))
  7219. (defun org-refile-get-location (&optional prompt default-buffer)
  7220. "Prompt the user for a refile location, using PROMPT."
  7221. (let ((org-refile-targets org-refile-targets)
  7222. (org-refile-use-outline-path org-refile-use-outline-path))
  7223. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7224. (unless org-refile-target-table
  7225. (error "No refile targets"))
  7226. (let* ((cbuf (current-buffer))
  7227. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7228. (cfunc (if (and org-refile-use-outline-path
  7229. org-outline-path-complete-in-steps)
  7230. 'org-olpath-completing-read
  7231. 'org-ido-completing-read))
  7232. (extra (if org-refile-use-outline-path "/" ""))
  7233. (filename (and cfn (expand-file-name cfn)))
  7234. (tbl (mapcar
  7235. (lambda (x)
  7236. (if (not (equal filename (nth 1 x)))
  7237. (cons (concat (car x) extra " ("
  7238. (file-name-nondirectory (nth 1 x)) ")")
  7239. (cdr x))
  7240. (cons (concat (car x) extra) (cdr x))))
  7241. org-refile-target-table))
  7242. (completion-ignore-case t))
  7243. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7244. tbl)))
  7245. (defun org-olpath-completing-read (prompt collection &rest args)
  7246. "Read an outline path like a file name."
  7247. (let ((thetable collection)
  7248. (org-completion-use-ido nil)) ; does not work with ido.
  7249. (apply
  7250. 'org-ido-completing-read prompt
  7251. (lambda (string predicate &optional flag)
  7252. (let (rtn r f (l (length string)))
  7253. (cond
  7254. ((eq flag nil)
  7255. ;; try completion
  7256. (try-completion string thetable))
  7257. ((eq flag t)
  7258. ;; all-completions
  7259. (setq rtn (all-completions string thetable predicate))
  7260. (mapcar
  7261. (lambda (x)
  7262. (setq r (substring x l))
  7263. (if (string-match " ([^)]*)$" x)
  7264. (setq f (match-string 0 x))
  7265. (setq f ""))
  7266. (if (string-match "/" r)
  7267. (concat string (substring r 0 (match-end 0)) f)
  7268. x))
  7269. rtn))
  7270. ((eq flag 'lambda)
  7271. ;; exact match?
  7272. (assoc string thetable)))
  7273. ))
  7274. args)))
  7275. ;;;; Dynamic blocks
  7276. (defun org-find-dblock (name)
  7277. "Find the first dynamic block with name NAME in the buffer.
  7278. If not found, stay at current position and return nil."
  7279. (let (pos)
  7280. (save-excursion
  7281. (goto-char (point-min))
  7282. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7283. nil t)
  7284. (match-beginning 0))))
  7285. (if pos (goto-char pos))
  7286. pos))
  7287. (defconst org-dblock-start-re
  7288. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7289. "Matches the startline of a dynamic block, with parameters.")
  7290. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7291. "Matches the end of a dynamic block.")
  7292. (defun org-create-dblock (plist)
  7293. "Create a dynamic block section, with parameters taken from PLIST.
  7294. PLIST must contain a :name entry which is used as name of the block."
  7295. (unless (bolp) (newline))
  7296. (let ((name (plist-get plist :name)))
  7297. (insert "#+BEGIN: " name)
  7298. (while plist
  7299. (if (eq (car plist) :name)
  7300. (setq plist (cddr plist))
  7301. (insert " " (prin1-to-string (pop plist)))))
  7302. (insert "\n\n#+END:\n")
  7303. (beginning-of-line -2)))
  7304. (defun org-prepare-dblock ()
  7305. "Prepare dynamic block for refresh.
  7306. This empties the block, puts the cursor at the insert position and returns
  7307. the property list including an extra property :name with the block name."
  7308. (unless (looking-at org-dblock-start-re)
  7309. (error "Not at a dynamic block"))
  7310. (let* ((begdel (1+ (match-end 0)))
  7311. (name (org-no-properties (match-string 1)))
  7312. (params (append (list :name name)
  7313. (read (concat "(" (match-string 3) ")")))))
  7314. (unless (re-search-forward org-dblock-end-re nil t)
  7315. (error "Dynamic block not terminated"))
  7316. (setq params
  7317. (append params
  7318. (list :content (buffer-substring
  7319. begdel (match-beginning 0)))))
  7320. (delete-region begdel (match-beginning 0))
  7321. (goto-char begdel)
  7322. (open-line 1)
  7323. params))
  7324. (defun org-map-dblocks (&optional command)
  7325. "Apply COMMAND to all dynamic blocks in the current buffer.
  7326. If COMMAND is not given, use `org-update-dblock'."
  7327. (let ((cmd (or command 'org-update-dblock))
  7328. pos)
  7329. (save-excursion
  7330. (goto-char (point-min))
  7331. (while (re-search-forward org-dblock-start-re nil t)
  7332. (goto-char (setq pos (match-beginning 0)))
  7333. (condition-case nil
  7334. (funcall cmd)
  7335. (error (message "Error during update of dynamic block")))
  7336. (goto-char pos)
  7337. (unless (re-search-forward org-dblock-end-re nil t)
  7338. (error "Dynamic block not terminated"))))))
  7339. (defun org-dblock-update (&optional arg)
  7340. "User command for updating dynamic blocks.
  7341. Update the dynamic block at point. With prefix ARG, update all dynamic
  7342. blocks in the buffer."
  7343. (interactive "P")
  7344. (if arg
  7345. (org-update-all-dblocks)
  7346. (or (looking-at org-dblock-start-re)
  7347. (org-beginning-of-dblock))
  7348. (org-update-dblock)))
  7349. (defun org-update-dblock ()
  7350. "Update the dynamic block at point
  7351. This means to empty the block, parse for parameters and then call
  7352. the correct writing function."
  7353. (save-window-excursion
  7354. (let* ((pos (point))
  7355. (line (org-current-line))
  7356. (params (org-prepare-dblock))
  7357. (name (plist-get params :name))
  7358. (cmd (intern (concat "org-dblock-write:" name))))
  7359. (message "Updating dynamic block `%s' at line %d..." name line)
  7360. (funcall cmd params)
  7361. (message "Updating dynamic block `%s' at line %d...done" name line)
  7362. (goto-char pos))))
  7363. (defun org-beginning-of-dblock ()
  7364. "Find the beginning of the dynamic block at point.
  7365. Error if there is no such block at point."
  7366. (let ((pos (point))
  7367. beg)
  7368. (end-of-line 1)
  7369. (if (and (re-search-backward org-dblock-start-re nil t)
  7370. (setq beg (match-beginning 0))
  7371. (re-search-forward org-dblock-end-re nil t)
  7372. (> (match-end 0) pos))
  7373. (goto-char beg)
  7374. (goto-char pos)
  7375. (error "Not in a dynamic block"))))
  7376. (defun org-update-all-dblocks ()
  7377. "Update all dynamic blocks in the buffer.
  7378. This function can be used in a hook."
  7379. (when (org-mode-p)
  7380. (org-map-dblocks 'org-update-dblock)))
  7381. ;;;; Completion
  7382. (defconst org-additional-option-like-keywords
  7383. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7384. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7385. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7386. "BEGIN_QUOTE" "END_QUOTE"
  7387. "BEGIN_VERSE" "END_VERSE"
  7388. "BEGIN_SRC" "END_SRC"
  7389. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7390. (defcustom org-structure-template-alist
  7391. '(
  7392. ("s" "#+begin_src ?\n\n#+end_src"
  7393. "<src lang=\"?\">\n\n</src>")
  7394. ("e" "#+begin_example\n?\n#+end_example"
  7395. "<example>\n?\n</example>")
  7396. ("q" "#+begin_quote\n?\n#+end_quote"
  7397. "<quote>\n?\n</quote>")
  7398. ("v" "#+begin_verse\n?\n#+end_verse"
  7399. "<verse>\n?\n/verse>")
  7400. ("l" "#+begin_latex\n?\n#+end_latex"
  7401. "<literal style=\"latex\">\n?\n</literal>")
  7402. ("L" "#+latex: "
  7403. "<literal style=\"latex\">?</literal>")
  7404. ("h" "#+begin_html\n?\n#+end_html"
  7405. "<literal style=\"html\">\n?\n</literal>")
  7406. ("H" "#+html: "
  7407. "<literal style=\"html\">?</literal>")
  7408. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7409. ("A" "#+ascii: ")
  7410. ("i" "#+include %file ?"
  7411. "<include file=%file markup=\"?\">")
  7412. )
  7413. "Structure completion elements.
  7414. This is a list of abbreviation keys and values. The value gets inserted
  7415. it you type @samp{.} followed by the key and then the completion key,
  7416. usually `M-TAB'. %file will be replaced by a file name after prompting
  7417. for the file using completion.
  7418. There are two templates for each key, the first uses the original Org syntax,
  7419. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7420. the default when the /org-mtags.el/ module has been loaded. See also the
  7421. variable `org-mtags-prefer-muse-templates'.
  7422. This is an experimental feature, it is undecided if it is going to stay in."
  7423. :group 'org-completion
  7424. :type '(repeat
  7425. (string :tag "Key")
  7426. (string :tag "Template")
  7427. (string :tag "Muse Template")))
  7428. (defun org-try-structure-completion ()
  7429. "Try to complete a structure template before point.
  7430. This looks for strings like \"<e\" on an otherwise empty line and
  7431. expands them."
  7432. (let ((l (buffer-substring (point-at-bol) (point)))
  7433. a)
  7434. (when (and (looking-at "[ \t]*$")
  7435. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7436. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7437. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7438. (match-beginning 1)) a)
  7439. t)))
  7440. (defun org-complete-expand-structure-template (start cell)
  7441. "Expand a structure template."
  7442. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7443. (rpl (nth (if musep 2 1) cell)))
  7444. (delete-region start (point))
  7445. (when (string-match "\\`#\\+" rpl)
  7446. (cond
  7447. ((bolp))
  7448. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7449. (delete-region (point-at-bol) (point)))
  7450. (t (newline))))
  7451. (setq start (point))
  7452. (if (string-match "%file" rpl)
  7453. (setq rpl (replace-match
  7454. (concat
  7455. "\""
  7456. (save-match-data
  7457. (abbreviate-file-name (read-file-name "Include file: ")))
  7458. "\"")
  7459. t t rpl)))
  7460. (insert rpl)
  7461. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7462. (defun org-complete (&optional arg)
  7463. "Perform completion on word at point.
  7464. At the beginning of a headline, this completes TODO keywords as given in
  7465. `org-todo-keywords'.
  7466. If the current word is preceded by a backslash, completes the TeX symbols
  7467. that are supported for HTML support.
  7468. If the current word is preceded by \"#+\", completes special words for
  7469. setting file options.
  7470. In the line after \"#+STARTUP:, complete valid keywords.\"
  7471. At all other locations, this simply calls the value of
  7472. `org-completion-fallback-command'."
  7473. (interactive "P")
  7474. (org-without-partial-completion
  7475. (catch 'exit
  7476. (let* ((a nil)
  7477. (end (point))
  7478. (beg1 (save-excursion
  7479. (skip-chars-backward (org-re "[:alnum:]_@"))
  7480. (point)))
  7481. (beg (save-excursion
  7482. (skip-chars-backward "a-zA-Z0-9_:$")
  7483. (point)))
  7484. (confirm (lambda (x) (stringp (car x))))
  7485. (searchhead (equal (char-before beg) ?*))
  7486. (struct
  7487. (when (and (member (char-before beg1) '(?. ?<))
  7488. (setq a (assoc (buffer-substring beg1 (point))
  7489. org-structure-template-alist)))
  7490. (org-complete-expand-structure-template (1- beg1) a)
  7491. (throw 'exit t)))
  7492. (tag (and (equal (char-before beg1) ?:)
  7493. (equal (char-after (point-at-bol)) ?*)))
  7494. (prop (and (equal (char-before beg1) ?:)
  7495. (not (equal (char-after (point-at-bol)) ?*))))
  7496. (texp (equal (char-before beg) ?\\))
  7497. (link (equal (char-before beg) ?\[))
  7498. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7499. beg)
  7500. "#+"))
  7501. (startup (string-match "^#\\+STARTUP:.*"
  7502. (buffer-substring (point-at-bol) (point))))
  7503. (completion-ignore-case opt)
  7504. (type nil)
  7505. (tbl nil)
  7506. (table (cond
  7507. (opt
  7508. (setq type :opt)
  7509. (require 'org-exp)
  7510. (append
  7511. (mapcar
  7512. (lambda (x)
  7513. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7514. (cons (match-string 2 x) (match-string 1 x)))
  7515. (org-split-string (org-get-current-options) "\n"))
  7516. (mapcar 'list org-additional-option-like-keywords)))
  7517. (startup
  7518. (setq type :startup)
  7519. org-startup-options)
  7520. (link (append org-link-abbrev-alist-local
  7521. org-link-abbrev-alist))
  7522. (texp
  7523. (setq type :tex)
  7524. org-html-entities)
  7525. ((string-match "\\`\\*+[ \t]+\\'"
  7526. (buffer-substring (point-at-bol) beg))
  7527. (setq type :todo)
  7528. (mapcar 'list org-todo-keywords-1))
  7529. (searchhead
  7530. (setq type :searchhead)
  7531. (save-excursion
  7532. (goto-char (point-min))
  7533. (while (re-search-forward org-todo-line-regexp nil t)
  7534. (push (list
  7535. (org-make-org-heading-search-string
  7536. (match-string 3) t))
  7537. tbl)))
  7538. tbl)
  7539. (tag (setq type :tag beg beg1)
  7540. (or org-tag-alist (org-get-buffer-tags)))
  7541. (prop (setq type :prop beg beg1)
  7542. (mapcar 'list (org-buffer-property-keys nil t t)))
  7543. (t (progn
  7544. (call-interactively org-completion-fallback-command)
  7545. (throw 'exit nil)))))
  7546. (pattern (buffer-substring-no-properties beg end))
  7547. (completion (try-completion pattern table confirm)))
  7548. (cond ((eq completion t)
  7549. (if (not (assoc (upcase pattern) table))
  7550. (message "Already complete")
  7551. (if (and (equal type :opt)
  7552. (not (member (car (assoc (upcase pattern) table))
  7553. org-additional-option-like-keywords)))
  7554. (insert (substring (cdr (assoc (upcase pattern) table))
  7555. (length pattern)))
  7556. (if (memq type '(:tag :prop)) (insert ":")))))
  7557. ((null completion)
  7558. (message "Can't find completion for \"%s\"" pattern)
  7559. (ding))
  7560. ((not (string= pattern completion))
  7561. (delete-region beg end)
  7562. (if (string-match " +$" completion)
  7563. (setq completion (replace-match "" t t completion)))
  7564. (insert completion)
  7565. (if (get-buffer-window "*Completions*")
  7566. (delete-window (get-buffer-window "*Completions*")))
  7567. (if (assoc completion table)
  7568. (if (eq type :todo) (insert " ")
  7569. (if (memq type '(:tag :prop)) (insert ":"))))
  7570. (if (and (equal type :opt) (assoc completion table))
  7571. (message "%s" (substitute-command-keys
  7572. "Press \\[org-complete] again to insert example settings"))))
  7573. (t
  7574. (message "Making completion list...")
  7575. (let ((list (sort (all-completions pattern table confirm)
  7576. 'string<)))
  7577. (with-output-to-temp-buffer "*Completions*"
  7578. (condition-case nil
  7579. ;; Protection needed for XEmacs and emacs 21
  7580. (display-completion-list list pattern)
  7581. (error (display-completion-list list)))))
  7582. (message "Making completion list...%s" "done")))))))
  7583. ;;;; TODO, DEADLINE, Comments
  7584. (defun org-toggle-comment ()
  7585. "Change the COMMENT state of an entry."
  7586. (interactive)
  7587. (save-excursion
  7588. (org-back-to-heading)
  7589. (let (case-fold-search)
  7590. (if (looking-at (concat outline-regexp
  7591. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7592. (replace-match "" t t nil 1)
  7593. (if (looking-at outline-regexp)
  7594. (progn
  7595. (goto-char (match-end 0))
  7596. (insert org-comment-string " ")))))))
  7597. (defvar org-last-todo-state-is-todo nil
  7598. "This is non-nil when the last TODO state change led to a TODO state.
  7599. If the last change removed the TODO tag or switched to DONE, then
  7600. this is nil.")
  7601. (defvar org-setting-tags nil) ; dynamically skipped
  7602. (defun org-parse-local-options (string var)
  7603. "Parse STRING for startup setting relevant for variable VAR."
  7604. (let ((rtn (symbol-value var))
  7605. e opts)
  7606. (save-match-data
  7607. (if (or (not string) (not (string-match "\\S-" string)))
  7608. rtn
  7609. (setq opts (delq nil (mapcar (lambda (x)
  7610. (setq e (assoc x org-startup-options))
  7611. (if (eq (nth 1 e) var) e nil))
  7612. (org-split-string string "[ \t]+"))))
  7613. (if (not opts)
  7614. rtn
  7615. (setq rtn nil)
  7616. (while (setq e (pop opts))
  7617. (if (not (nth 3 e))
  7618. (setq rtn (nth 2 e))
  7619. (if (not (listp rtn)) (setq rtn nil))
  7620. (push (nth 2 e) rtn)))
  7621. rtn)))))
  7622. (defvar org-todo-setup-filter-hook nil
  7623. "Hook for functions that pre-filter todo specs.
  7624. Each function takes a todo spec and returns either `nil' or the spec
  7625. transformed into canonical form." )
  7626. (defvar org-todo-get-default-hook nil
  7627. "Hook for functions that get a default item for todo.
  7628. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7629. `nil' or a string to be used for the todo mark." )
  7630. (defvar org-agenda-headline-snapshot-before-repeat)
  7631. (defun org-todo (&optional arg)
  7632. "Change the TODO state of an item.
  7633. The state of an item is given by a keyword at the start of the heading,
  7634. like
  7635. *** TODO Write paper
  7636. *** DONE Call mom
  7637. The different keywords are specified in the variable `org-todo-keywords'.
  7638. By default the available states are \"TODO\" and \"DONE\".
  7639. So for this example: when the item starts with TODO, it is changed to DONE.
  7640. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7641. DONE are present, add TODO at the beginning of the heading.
  7642. With C-u prefix arg, use completion to determine the new state.
  7643. With numeric prefix arg, switch to that state.
  7644. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7645. With a tripple C-u prefix, circumvent any state blocking.
  7646. For calling through lisp, arg is also interpreted in the following way:
  7647. 'none -> empty state
  7648. \"\"(empty string) -> switch to empty state
  7649. 'done -> switch to DONE
  7650. 'nextset -> switch to the next set of keywords
  7651. 'previousset -> switch to the previous set of keywords
  7652. \"WAITING\" -> switch to the specified keyword, but only if it
  7653. really is a member of `org-todo-keywords'."
  7654. (interactive "P")
  7655. (if (equal arg '(16)) (setq arg 'nextset))
  7656. (let ((org-blocker-hook org-blocker-hook))
  7657. (when (equal arg '(64))
  7658. (setq arg nil org-blocker-hook nil))
  7659. (save-excursion
  7660. (catch 'exit
  7661. (org-back-to-heading)
  7662. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7663. (or (looking-at (concat " +" org-todo-regexp " *"))
  7664. (looking-at " *"))
  7665. (let* ((match-data (match-data))
  7666. (startpos (point-at-bol))
  7667. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7668. (org-log-done org-log-done)
  7669. (org-log-repeat org-log-repeat)
  7670. (org-todo-log-states org-todo-log-states)
  7671. (this (match-string 1))
  7672. (hl-pos (match-beginning 0))
  7673. (head (org-get-todo-sequence-head this))
  7674. (ass (assoc head org-todo-kwd-alist))
  7675. (interpret (nth 1 ass))
  7676. (done-word (nth 3 ass))
  7677. (final-done-word (nth 4 ass))
  7678. (last-state (or this ""))
  7679. (completion-ignore-case t)
  7680. (member (member this org-todo-keywords-1))
  7681. (tail (cdr member))
  7682. (state (cond
  7683. ((and org-todo-key-trigger
  7684. (or (and (equal arg '(4))
  7685. (eq org-use-fast-todo-selection 'prefix))
  7686. (and (not arg) org-use-fast-todo-selection
  7687. (not (eq org-use-fast-todo-selection
  7688. 'prefix)))))
  7689. ;; Use fast selection
  7690. (org-fast-todo-selection))
  7691. ((and (equal arg '(4))
  7692. (or (not org-use-fast-todo-selection)
  7693. (not org-todo-key-trigger)))
  7694. ;; Read a state with completion
  7695. (org-ido-completing-read
  7696. "State: " (mapcar (lambda(x) (list x))
  7697. org-todo-keywords-1)
  7698. nil t))
  7699. ((eq arg 'right)
  7700. (if this
  7701. (if tail (car tail) nil)
  7702. (car org-todo-keywords-1)))
  7703. ((eq arg 'left)
  7704. (if (equal member org-todo-keywords-1)
  7705. nil
  7706. (if this
  7707. (nth (- (length org-todo-keywords-1)
  7708. (length tail) 2)
  7709. org-todo-keywords-1)
  7710. (org-last org-todo-keywords-1))))
  7711. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7712. (setq arg nil))) ; hack to fall back to cycling
  7713. (arg
  7714. ;; user or caller requests a specific state
  7715. (cond
  7716. ((equal arg "") nil)
  7717. ((eq arg 'none) nil)
  7718. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7719. ((eq arg 'nextset)
  7720. (or (car (cdr (member head org-todo-heads)))
  7721. (car org-todo-heads)))
  7722. ((eq arg 'previousset)
  7723. (let ((org-todo-heads (reverse org-todo-heads)))
  7724. (or (car (cdr (member head org-todo-heads)))
  7725. (car org-todo-heads))))
  7726. ((car (member arg org-todo-keywords-1)))
  7727. ((nth (1- (prefix-numeric-value arg))
  7728. org-todo-keywords-1))))
  7729. ((null member) (or head (car org-todo-keywords-1)))
  7730. ((equal this final-done-word) nil) ;; -> make empty
  7731. ((null tail) nil) ;; -> first entry
  7732. ((memq interpret '(type priority))
  7733. (if (eq this-command last-command)
  7734. (car tail)
  7735. (if (> (length tail) 0)
  7736. (or done-word (car org-done-keywords))
  7737. nil)))
  7738. (t
  7739. (car tail))))
  7740. (state (or
  7741. (run-hook-with-args-until-success
  7742. 'org-todo-get-default-hook state last-state)
  7743. state))
  7744. (next (if state (concat " " state " ") " "))
  7745. (change-plist (list :type 'todo-state-change :from this :to state
  7746. :position startpos))
  7747. dolog now-done-p)
  7748. (when org-blocker-hook
  7749. (setq org-last-todo-state-is-todo
  7750. (not (member this org-done-keywords)))
  7751. (unless (save-excursion
  7752. (save-match-data
  7753. (run-hook-with-args-until-failure
  7754. 'org-blocker-hook change-plist)))
  7755. (if (interactive-p)
  7756. (error "TODO state change from %s to %s blocked" this state)
  7757. ;; fail silently
  7758. (message "TODO state change from %s to %s blocked" this state)
  7759. (throw 'exit nil))))
  7760. (store-match-data match-data)
  7761. (replace-match next t t)
  7762. (unless (pos-visible-in-window-p hl-pos)
  7763. (message "TODO state changed to %s" (org-trim next)))
  7764. (unless head
  7765. (setq head (org-get-todo-sequence-head state)
  7766. ass (assoc head org-todo-kwd-alist)
  7767. interpret (nth 1 ass)
  7768. done-word (nth 3 ass)
  7769. final-done-word (nth 4 ass)))
  7770. (when (memq arg '(nextset previousset))
  7771. (message "Keyword-Set %d/%d: %s"
  7772. (- (length org-todo-sets) -1
  7773. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7774. (length org-todo-sets)
  7775. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7776. (setq org-last-todo-state-is-todo
  7777. (not (member state org-done-keywords)))
  7778. (setq now-done-p (and (member state org-done-keywords)
  7779. (not (member this org-done-keywords))))
  7780. (and logging (org-local-logging logging))
  7781. (when (and (or org-todo-log-states org-log-done)
  7782. (not (memq arg '(nextset previousset))))
  7783. ;; we need to look at recording a time and note
  7784. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7785. (nth 2 (assoc this org-todo-log-states))))
  7786. (when (and state
  7787. (member state org-not-done-keywords)
  7788. (not (member this org-not-done-keywords)))
  7789. ;; This is now a todo state and was not one before
  7790. ;; If there was a CLOSED time stamp, get rid of it.
  7791. (org-add-planning-info nil nil 'closed))
  7792. (when (and now-done-p org-log-done)
  7793. ;; It is now done, and it was not done before
  7794. (org-add-planning-info 'closed (org-current-time))
  7795. (if (and (not dolog) (eq 'note org-log-done))
  7796. (org-add-log-setup 'done state this 'findpos 'note)))
  7797. (when (and state dolog)
  7798. ;; This is a non-nil state, and we need to log it
  7799. (org-add-log-setup 'state state this 'findpos dolog)))
  7800. ;; Fixup tag positioning
  7801. (org-todo-trigger-tag-changes state)
  7802. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7803. (when org-provide-todo-statistics
  7804. (org-update-parent-todo-statistics))
  7805. (run-hooks 'org-after-todo-state-change-hook)
  7806. (if (and arg (not (member state org-done-keywords)))
  7807. (setq head (org-get-todo-sequence-head state)))
  7808. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7809. ;; Do we need to trigger a repeat?
  7810. (when now-done-p
  7811. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7812. ;; This is for the agenda, take a snapshot of the headline.
  7813. (save-match-data
  7814. (setq org-agenda-headline-snapshot-before-repeat
  7815. (org-get-heading))))
  7816. (org-auto-repeat-maybe state))
  7817. ;; Fixup cursor location if close to the keyword
  7818. (if (and (outline-on-heading-p)
  7819. (not (bolp))
  7820. (save-excursion (beginning-of-line 1)
  7821. (looking-at org-todo-line-regexp))
  7822. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7823. (progn
  7824. (goto-char (or (match-end 2) (match-end 1)))
  7825. (just-one-space)))
  7826. (when org-trigger-hook
  7827. (save-excursion
  7828. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7829. (defun org-block-todo-from-children-or-siblings (change-plist)
  7830. "Block turning an entry into a TODO, using the hierarchy.
  7831. This checks whether the current task should be blocked from state
  7832. changes. Such blocking occurs when:
  7833. 1. The task has children which are not all in a completed state.
  7834. 2. A task has a parent with the property :ORDERED:, and there
  7835. are siblings prior to the current task with incomplete
  7836. status."
  7837. (catch 'dont-block
  7838. ;; If this is not a todo state change, or if this entry is already DONE,
  7839. ;; do not block
  7840. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7841. (member (plist-get change-plist :from)
  7842. (cons 'done org-done-keywords))
  7843. (member (plist-get change-plist :to)
  7844. (cons 'todo org-not-done-keywords)))
  7845. (throw 'dont-block t))
  7846. ;; If this task has children, and any are undone, it's blocked
  7847. (save-excursion
  7848. (org-back-to-heading t)
  7849. (let ((this-level (funcall outline-level)))
  7850. (outline-next-heading)
  7851. (let ((child-level (funcall outline-level)))
  7852. (while (and (not (eobp))
  7853. (> child-level this-level))
  7854. ;; this todo has children, check whether they are all
  7855. ;; completed
  7856. (if (and (not (org-entry-is-done-p))
  7857. (org-entry-is-todo-p))
  7858. (throw 'dont-block nil))
  7859. (outline-next-heading)
  7860. (setq child-level (funcall outline-level))))))
  7861. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7862. ;; any previous siblings are undone, it's blocked
  7863. (save-excursion
  7864. (org-back-to-heading t)
  7865. (when (save-excursion
  7866. (ignore-errors
  7867. (outline-up-heading 1)
  7868. (org-entry-get (point) "ORDERED")))
  7869. (let* ((this-level (funcall outline-level))
  7870. (current-level this-level))
  7871. (while (and (not (bobp))
  7872. (= current-level this-level))
  7873. (outline-previous-heading)
  7874. (setq current-level (funcall outline-level))
  7875. (if (= current-level this-level)
  7876. ;; this todo has children, check whether they are all
  7877. ;; completed
  7878. (if (and (not (org-entry-is-done-p))
  7879. (org-entry-is-todo-p))
  7880. (throw 'dont-block nil)))))))
  7881. t)) ; don't block
  7882. (defun org-toggle-ordered-property ()
  7883. "Toggle the ORDERED property of the current entry."
  7884. (interactive)
  7885. (save-excursion
  7886. (org-back-to-heading)
  7887. (if (org-entry-get nil "ORDERED")
  7888. (progn
  7889. (org-delete-property "ORDERED")
  7890. (message "Subtasks can be completed in arbitrary order or parallel"))
  7891. (org-entry-put nil "ORDERED" "t")
  7892. (message "Subtasks must be completed in sequence"))))
  7893. (defun org-block-todo-from-checkboxes (change-plist)
  7894. "Block turning an entry into a TODO, using checkboxes.
  7895. This checks whether the current task should be blocked from state
  7896. changes because there are uncheckd boxes in this entry."
  7897. (catch 'dont-block
  7898. ;; If this is not a todo state change, or if this entry is already DONE,
  7899. ;; do not block
  7900. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7901. (member (plist-get change-plist :from)
  7902. (cons 'done org-done-keywords))
  7903. (member (plist-get change-plist :to)
  7904. (cons 'todo org-not-done-keywords)))
  7905. (throw 'dont-block t))
  7906. ;; If this task has checkboxes that are not checked, it's blocked
  7907. (save-excursion
  7908. (org-back-to-heading t)
  7909. (let ((beg (point)) end)
  7910. (outline-next-heading)
  7911. (setq end (point))
  7912. (goto-char beg)
  7913. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7914. end t)
  7915. (throw 'dont-block nil))))
  7916. t)) ; do not block
  7917. (defun org-update-parent-todo-statistics ()
  7918. "Update any statistics cookie in the parent of the current headline."
  7919. (interactive)
  7920. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7921. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  7922. (catch 'exit
  7923. (save-excursion
  7924. (setq level (org-up-heading-safe))
  7925. (unless level
  7926. (throw 'exit nil))
  7927. (while (re-search-forward box-re (point-at-eol) t)
  7928. (setq cnt-all 0 cnt-done 0 cookie-present t)
  7929. (setq is-percent (match-end 2))
  7930. (save-match-data
  7931. (unless (outline-next-heading) (throw 'exit nil))
  7932. (while (looking-at org-todo-line-regexp)
  7933. (setq kwd (match-string 2))
  7934. (and kwd (setq cnt-all (1+ cnt-all)))
  7935. (and (member kwd org-done-keywords)
  7936. (setq cnt-done (1+ cnt-done)))
  7937. (condition-case nil
  7938. (org-forward-same-level 1)
  7939. (error (end-of-line 1)))))
  7940. (replace-match
  7941. (if is-percent
  7942. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7943. (format "[%d/%d]" cnt-done cnt-all))))
  7944. (when cookie-present
  7945. (run-hook-with-args 'org-after-todo-statistics-hook
  7946. cnt-done (- cnt-all cnt-done)))))))
  7947. (defvar org-after-todo-statistics-hook nil
  7948. "Hook that is called after a TODO statistics cookie has been updated.
  7949. Each function is called with two arguments: the number of not-done entries
  7950. and the number of done entries.
  7951. For example, the following function, when added to this hook, will switch
  7952. an entry to DONE when all children are done, and back to TODO when new
  7953. entries are set to a TODO status. Note that this hook is only called
  7954. when there is a statistics cookie in the headline!
  7955. (defun org-summary-todo (n-done n-not-done)
  7956. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7957. (let (org-log-done org-log-states) ; turn off logging
  7958. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7959. ")
  7960. (defun org-todo-trigger-tag-changes (state)
  7961. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7962. (let ((l org-todo-state-tags-triggers)
  7963. changes)
  7964. (when (or (not state) (equal state ""))
  7965. (setq changes (append changes (cdr (assoc "" l)))))
  7966. (when (and (stringp state) (> (length state) 0))
  7967. (setq changes (append changes (cdr (assoc state l)))))
  7968. (when (member state org-not-done-keywords)
  7969. (setq changes (append changes (cdr (assoc 'todo l)))))
  7970. (when (member state org-done-keywords)
  7971. (setq changes (append changes (cdr (assoc 'done l)))))
  7972. (dolist (c changes)
  7973. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7974. (defun org-local-logging (value)
  7975. "Get logging settings from a property VALUE."
  7976. (let* (words w a)
  7977. ;; directly set the variables, they are already local.
  7978. (setq org-log-done nil
  7979. org-log-repeat nil
  7980. org-todo-log-states nil)
  7981. (setq words (org-split-string value))
  7982. (while (setq w (pop words))
  7983. (cond
  7984. ((setq a (assoc w org-startup-options))
  7985. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7986. (set (nth 1 a) (nth 2 a))))
  7987. ((setq a (org-extract-log-state-settings w))
  7988. (and (member (car a) org-todo-keywords-1)
  7989. (push a org-todo-log-states)))))))
  7990. (defun org-get-todo-sequence-head (kwd)
  7991. "Return the head of the TODO sequence to which KWD belongs.
  7992. If KWD is not set, check if there is a text property remembering the
  7993. right sequence."
  7994. (let (p)
  7995. (cond
  7996. ((not kwd)
  7997. (or (get-text-property (point-at-bol) 'org-todo-head)
  7998. (progn
  7999. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8000. nil (point-at-eol)))
  8001. (get-text-property p 'org-todo-head))))
  8002. ((not (member kwd org-todo-keywords-1))
  8003. (car org-todo-keywords-1))
  8004. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8005. (defun org-fast-todo-selection ()
  8006. "Fast TODO keyword selection with single keys.
  8007. Returns the new TODO keyword, or nil if no state change should occur."
  8008. (let* ((fulltable org-todo-key-alist)
  8009. (done-keywords org-done-keywords) ;; needed for the faces.
  8010. (maxlen (apply 'max (mapcar
  8011. (lambda (x)
  8012. (if (stringp (car x)) (string-width (car x)) 0))
  8013. fulltable)))
  8014. (expert nil)
  8015. (fwidth (+ maxlen 3 1 3))
  8016. (ncol (/ (- (window-width) 4) fwidth))
  8017. tg cnt e c tbl
  8018. groups ingroup)
  8019. (save-excursion
  8020. (save-window-excursion
  8021. (if expert
  8022. (set-buffer (get-buffer-create " *Org todo*"))
  8023. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8024. (erase-buffer)
  8025. (org-set-local 'org-done-keywords done-keywords)
  8026. (setq tbl fulltable cnt 0)
  8027. (while (setq e (pop tbl))
  8028. (cond
  8029. ((equal e '(:startgroup))
  8030. (push '() groups) (setq ingroup t)
  8031. (when (not (= cnt 0))
  8032. (setq cnt 0)
  8033. (insert "\n"))
  8034. (insert "{ "))
  8035. ((equal e '(:endgroup))
  8036. (setq ingroup nil cnt 0)
  8037. (insert "}\n"))
  8038. (t
  8039. (setq tg (car e) c (cdr e))
  8040. (if ingroup (push tg (car groups)))
  8041. (setq tg (org-add-props tg nil 'face
  8042. (org-get-todo-face tg)))
  8043. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8044. (insert "[" c "] " tg (make-string
  8045. (- fwidth 4 (length tg)) ?\ ))
  8046. (when (= (setq cnt (1+ cnt)) ncol)
  8047. (insert "\n")
  8048. (if ingroup (insert " "))
  8049. (setq cnt 0)))))
  8050. (insert "\n")
  8051. (goto-char (point-min))
  8052. (if (not expert) (org-fit-window-to-buffer))
  8053. (message "[a-z..]:Set [SPC]:clear")
  8054. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8055. (cond
  8056. ((or (= c ?\C-g)
  8057. (and (= c ?q) (not (rassoc c fulltable))))
  8058. (setq quit-flag t))
  8059. ((= c ?\ ) nil)
  8060. ((setq e (rassoc c fulltable) tg (car e))
  8061. tg)
  8062. (t (setq quit-flag t)))))))
  8063. (defun org-entry-is-todo-p ()
  8064. (member (org-get-todo-state) org-not-done-keywords))
  8065. (defun org-entry-is-done-p ()
  8066. (member (org-get-todo-state) org-done-keywords))
  8067. (defun org-get-todo-state ()
  8068. (save-excursion
  8069. (org-back-to-heading t)
  8070. (and (looking-at org-todo-line-regexp)
  8071. (match-end 2)
  8072. (match-string 2))))
  8073. (defun org-at-date-range-p (&optional inactive-ok)
  8074. "Is the cursor inside a date range?"
  8075. (interactive)
  8076. (save-excursion
  8077. (catch 'exit
  8078. (let ((pos (point)))
  8079. (skip-chars-backward "^[<\r\n")
  8080. (skip-chars-backward "<[")
  8081. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8082. (>= (match-end 0) pos)
  8083. (throw 'exit t))
  8084. (skip-chars-backward "^<[\r\n")
  8085. (skip-chars-backward "<[")
  8086. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8087. (>= (match-end 0) pos)
  8088. (throw 'exit t)))
  8089. nil)))
  8090. (defun org-get-repeat ()
  8091. "Check if there is a deadline/schedule with repeater in this entry."
  8092. (save-match-data
  8093. (save-excursion
  8094. (org-back-to-heading t)
  8095. (if (re-search-forward
  8096. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8097. (match-string 1)))))
  8098. (defvar org-last-changed-timestamp)
  8099. (defvar org-last-inserted-timestamp)
  8100. (defvar org-log-post-message)
  8101. (defvar org-log-note-purpose)
  8102. (defvar org-log-note-how)
  8103. (defvar org-log-note-extra)
  8104. (defun org-auto-repeat-maybe (done-word)
  8105. "Check if the current headline contains a repeated deadline/schedule.
  8106. If yes, set TODO state back to what it was and change the base date
  8107. of repeating deadline/scheduled time stamps to new date.
  8108. This function is run automatically after each state change to a DONE state."
  8109. ;; last-state is dynamically scoped into this function
  8110. (let* ((repeat (org-get-repeat))
  8111. (aa (assoc last-state org-todo-kwd-alist))
  8112. (interpret (nth 1 aa))
  8113. (head (nth 2 aa))
  8114. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8115. (msg "Entry repeats: ")
  8116. (org-log-done nil)
  8117. (org-todo-log-states nil)
  8118. (nshiftmax 10) (nshift 0)
  8119. re type n what ts time)
  8120. (when repeat
  8121. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8122. (org-todo (if (eq interpret 'type) last-state head))
  8123. (when org-log-repeat
  8124. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8125. (memq 'org-add-log-note post-command-hook))
  8126. ;; OK, we are already setup for some record
  8127. (if (eq org-log-repeat 'note)
  8128. ;; make sure we take a note, not only a time stamp
  8129. (setq org-log-note-how 'note))
  8130. ;; Set up for taking a record
  8131. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8132. last-state
  8133. 'findpos org-log-repeat)))
  8134. (org-back-to-heading t)
  8135. (org-add-planning-info nil nil 'closed)
  8136. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8137. org-deadline-time-regexp "\\)\\|\\("
  8138. org-ts-regexp "\\)"))
  8139. (while (re-search-forward
  8140. re (save-excursion (outline-next-heading) (point)) t)
  8141. (setq type (if (match-end 1) org-scheduled-string
  8142. (if (match-end 3) org-deadline-string "Plain:"))
  8143. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8144. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8145. (setq n (string-to-number (match-string 2 ts))
  8146. what (match-string 3 ts))
  8147. (if (equal what "w") (setq n (* n 7) what "d"))
  8148. ;; Preparation, see if we need to modify the start date for the change
  8149. (when (match-end 1)
  8150. (setq time (save-match-data (org-time-string-to-time ts)))
  8151. (cond
  8152. ((equal (match-string 1 ts) ".")
  8153. ;; Shift starting date to today
  8154. (org-timestamp-change
  8155. (- (time-to-days (current-time)) (time-to-days time))
  8156. 'day))
  8157. ((equal (match-string 1 ts) "+")
  8158. (while (or (= nshift 0)
  8159. (<= (time-to-days time) (time-to-days (current-time))))
  8160. (when (= (incf nshift) nshiftmax)
  8161. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8162. (error "Abort")))
  8163. (org-timestamp-change n (cdr (assoc what whata)))
  8164. (org-at-timestamp-p t)
  8165. (setq ts (match-string 1))
  8166. (setq time (save-match-data (org-time-string-to-time ts))))
  8167. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8168. ;; rematch, so that we have everything in place for the real shift
  8169. (org-at-timestamp-p t)
  8170. (setq ts (match-string 1))
  8171. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8172. (org-timestamp-change n (cdr (assoc what whata)))
  8173. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8174. (setq org-log-post-message msg)
  8175. (message "%s" msg))))
  8176. (defun org-show-todo-tree (arg)
  8177. "Make a compact tree which shows all headlines marked with TODO.
  8178. The tree will show the lines where the regexp matches, and all higher
  8179. headlines above the match.
  8180. With a \\[universal-argument] prefix, also show the DONE entries.
  8181. With a numeric prefix N, construct a sparse tree for the Nth element
  8182. of `org-todo-keywords-1'."
  8183. (interactive "P")
  8184. (let ((case-fold-search nil)
  8185. (kwd-re
  8186. (cond ((null arg) org-not-done-regexp)
  8187. ((equal arg '(4))
  8188. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8189. (mapcar 'list org-todo-keywords-1))))
  8190. (concat "\\("
  8191. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8192. "\\)\\>")))
  8193. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8194. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8195. org-todo-keywords-1)))
  8196. (t (error "Invalid prefix argument: %s" arg)))))
  8197. (message "%d TODO entries found"
  8198. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8199. (defun org-deadline (&optional remove time)
  8200. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8201. With argument REMOVE, remove any deadline from the item.
  8202. When TIME is set, it should be an internal time specification, and the
  8203. scheduling will use the corresponding date."
  8204. (interactive "P")
  8205. (if remove
  8206. (progn
  8207. (org-remove-timestamp-with-keyword org-deadline-string)
  8208. (message "Item no longer has a deadline."))
  8209. (if (org-get-repeat)
  8210. (error "Cannot change deadline on task with repeater, please do that by hand")
  8211. (org-add-planning-info 'deadline time 'closed)
  8212. (message "Deadline on %s" org-last-inserted-timestamp))))
  8213. (defun org-schedule (&optional remove time)
  8214. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8215. With argument REMOVE, remove any scheduling date from the item.
  8216. When TIME is set, it should be an internal time specification, and the
  8217. scheduling will use the corresponding date."
  8218. (interactive "P")
  8219. (if remove
  8220. (progn
  8221. (org-remove-timestamp-with-keyword org-scheduled-string)
  8222. (message "Item is no longer scheduled."))
  8223. (if (org-get-repeat)
  8224. (error "Cannot reschedule task with repeater, please do that by hand")
  8225. (org-add-planning-info 'scheduled time 'closed)
  8226. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8227. (defun org-get-scheduled-time (pom &optional inherit)
  8228. "Get the scheduled time as a time tuple, of a format suitable
  8229. for calling org-schedule with, or if there is no scheduling,
  8230. returns nil."
  8231. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8232. (when time
  8233. (apply 'encode-time (org-parse-time-string time)))))
  8234. (defun org-get-deadline-time (pom &optional inherit)
  8235. "Get the deadine as a time tuple, of a format suitable for
  8236. calling org-deadlin with, or if there is no scheduling, returns
  8237. nil."
  8238. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8239. (when time
  8240. (apply 'encode-time (org-parse-time-string time)))))
  8241. (defun org-remove-timestamp-with-keyword (keyword)
  8242. "Remove all time stamps with KEYWORD in the current entry."
  8243. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8244. beg)
  8245. (save-excursion
  8246. (org-back-to-heading t)
  8247. (setq beg (point))
  8248. (org-end-of-subtree t t)
  8249. (while (re-search-backward re beg t)
  8250. (replace-match "")
  8251. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8252. (equal (char-before) ?\ ))
  8253. (backward-delete-char 1)
  8254. (if (string-match "^[ \t]*$" (buffer-substring
  8255. (point-at-bol) (point-at-eol)))
  8256. (delete-region (point-at-bol)
  8257. (min (point-max) (1+ (point-at-eol))))))))))
  8258. (defun org-add-planning-info (what &optional time &rest remove)
  8259. "Insert new timestamp with keyword in the line directly after the headline.
  8260. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8261. If non is given, the user is prompted for a date.
  8262. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8263. be removed."
  8264. (interactive)
  8265. (let (org-time-was-given org-end-time-was-given ts
  8266. end default-time default-input)
  8267. (when (and (not time) (memq what '(scheduled deadline)))
  8268. ;; Try to get a default date/time from existing timestamp
  8269. (save-excursion
  8270. (org-back-to-heading t)
  8271. (setq end (save-excursion (outline-next-heading) (point)))
  8272. (when (re-search-forward (if (eq what 'scheduled)
  8273. org-scheduled-time-regexp
  8274. org-deadline-time-regexp)
  8275. end t)
  8276. (setq ts (match-string 1)
  8277. default-time
  8278. (apply 'encode-time (org-parse-time-string ts))
  8279. default-input (and ts (org-get-compact-tod ts))))))
  8280. (when what
  8281. ;; If necessary, get the time from the user
  8282. (setq time (or time (org-read-date nil 'to-time nil nil
  8283. default-time default-input))))
  8284. (when (and org-insert-labeled-timestamps-at-point
  8285. (member what '(scheduled deadline)))
  8286. (insert
  8287. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8288. (org-insert-time-stamp time org-time-was-given
  8289. nil nil nil (list org-end-time-was-given))
  8290. (setq what nil))
  8291. (save-excursion
  8292. (save-restriction
  8293. (let (col list elt ts buffer-invisibility-spec)
  8294. (org-back-to-heading t)
  8295. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8296. (goto-char (match-end 1))
  8297. (setq col (current-column))
  8298. (goto-char (match-end 0))
  8299. (if (eobp) (insert "\n") (forward-char 1))
  8300. (if (and (not (looking-at outline-regexp))
  8301. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8302. "[^\r\n]*"))
  8303. (not (equal (match-string 1) org-clock-string)))
  8304. (narrow-to-region (match-beginning 0) (match-end 0))
  8305. (insert-before-markers "\n")
  8306. (backward-char 1)
  8307. (narrow-to-region (point) (point))
  8308. (and org-adapt-indentation (org-indent-to-column col)))
  8309. ;; Check if we have to remove something.
  8310. (setq list (cons what remove))
  8311. (while list
  8312. (setq elt (pop list))
  8313. (goto-char (point-min))
  8314. (when (or (and (eq elt 'scheduled)
  8315. (re-search-forward org-scheduled-time-regexp nil t))
  8316. (and (eq elt 'deadline)
  8317. (re-search-forward org-deadline-time-regexp nil t))
  8318. (and (eq elt 'closed)
  8319. (re-search-forward org-closed-time-regexp nil t)))
  8320. (replace-match "")
  8321. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8322. (if (looking-at " +") (replace-match ""))))
  8323. (goto-char (point-max))
  8324. (when what
  8325. (insert
  8326. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8327. (cond ((eq what 'scheduled) org-scheduled-string)
  8328. ((eq what 'deadline) org-deadline-string)
  8329. ((eq what 'closed) org-closed-string))
  8330. " ")
  8331. (setq ts (org-insert-time-stamp
  8332. time
  8333. (or org-time-was-given
  8334. (and (eq what 'closed) org-log-done-with-time))
  8335. (eq what 'closed)
  8336. nil nil (list org-end-time-was-given)))
  8337. (end-of-line 1))
  8338. (goto-char (point-min))
  8339. (widen)
  8340. (if (and (looking-at "[ \t]+\n")
  8341. (equal (char-before) ?\n))
  8342. (delete-region (1- (point)) (point-at-eol)))
  8343. ts)))))
  8344. (defvar org-log-note-marker (make-marker))
  8345. (defvar org-log-note-purpose nil)
  8346. (defvar org-log-note-state nil)
  8347. (defvar org-log-note-previous-state nil)
  8348. (defvar org-log-note-how nil)
  8349. (defvar org-log-note-extra nil)
  8350. (defvar org-log-note-window-configuration nil)
  8351. (defvar org-log-note-return-to (make-marker))
  8352. (defvar org-log-post-message nil
  8353. "Message to be displayed after a log note has been stored.
  8354. The auto-repeater uses this.")
  8355. (defun org-add-note ()
  8356. "Add a note to the current entry.
  8357. This is done in the same way as adding a state change note."
  8358. (interactive)
  8359. (org-add-log-setup 'note nil nil 'findpos nil))
  8360. (defvar org-property-end-re)
  8361. (defun org-add-log-setup (&optional purpose state prev-state
  8362. findpos how &optional extra)
  8363. "Set up the post command hook to take a note.
  8364. If this is about to TODO state change, the new state is expected in STATE.
  8365. When FINDPOS is non-nil, find the correct position for the note in
  8366. the current entry. If not, assume that it can be inserted at point.
  8367. HOW is an indicator what kind of note should be created.
  8368. EXTRA is additional text that will be inserted into the notes buffer."
  8369. (let ((drawer (cond ((stringp org-log-into-drawer)
  8370. org-log-into-drawer)
  8371. (org-log-into-drawer "LOGBOOK")
  8372. (t nil))))
  8373. (save-restriction
  8374. (save-excursion
  8375. (when findpos
  8376. (org-back-to-heading t)
  8377. (narrow-to-region (point) (save-excursion
  8378. (outline-next-heading) (point)))
  8379. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8380. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8381. "[^\r\n]*\\)?"))
  8382. (goto-char (match-end 0))
  8383. (cond
  8384. (drawer
  8385. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8386. nil t)
  8387. (progn
  8388. (goto-char (match-end 0))
  8389. (or org-log-states-order-reversed
  8390. (and (re-search-forward org-property-end-re nil t)
  8391. (goto-char (1- (match-beginning 0))))))
  8392. (insert "\n:" drawer ":\n:END:")
  8393. (beginning-of-line 0)
  8394. (org-indent-line-function)
  8395. (beginning-of-line 2)
  8396. (org-indent-line-function)
  8397. (end-of-line 0)))
  8398. ((and org-log-state-notes-insert-after-drawers
  8399. (save-excursion
  8400. (forward-line) (looking-at org-drawer-regexp)))
  8401. (forward-line)
  8402. (while (looking-at org-drawer-regexp)
  8403. (goto-char (match-end 0))
  8404. (re-search-forward org-property-end-re (point-max) t)
  8405. (forward-line))
  8406. (forward-line -1)))
  8407. (unless org-log-states-order-reversed
  8408. (and (= (char-after) ?\n) (forward-char 1))
  8409. (org-skip-over-state-notes)
  8410. (skip-chars-backward " \t\n\r")))
  8411. (move-marker org-log-note-marker (point))
  8412. (setq org-log-note-purpose purpose
  8413. org-log-note-state state
  8414. org-log-note-previous-state prev-state
  8415. org-log-note-how how
  8416. org-log-note-extra extra)
  8417. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8418. (defun org-skip-over-state-notes ()
  8419. "Skip past the list of State notes in an entry."
  8420. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8421. (while (looking-at "[ \t]*- State")
  8422. (condition-case nil
  8423. (org-next-item)
  8424. (error (org-end-of-item)))))
  8425. (defun org-add-log-note (&optional purpose)
  8426. "Pop up a window for taking a note, and add this note later at point."
  8427. (remove-hook 'post-command-hook 'org-add-log-note)
  8428. (setq org-log-note-window-configuration (current-window-configuration))
  8429. (delete-other-windows)
  8430. (move-marker org-log-note-return-to (point))
  8431. (switch-to-buffer (marker-buffer org-log-note-marker))
  8432. (goto-char org-log-note-marker)
  8433. (org-switch-to-buffer-other-window "*Org Note*")
  8434. (erase-buffer)
  8435. (if (memq org-log-note-how '(time state))
  8436. (let (current-prefix-arg) (org-store-log-note))
  8437. (let ((org-inhibit-startup t)) (org-mode))
  8438. (insert (format "# Insert note for %s.
  8439. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8440. (cond
  8441. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8442. ((eq org-log-note-purpose 'done) "closed todo item")
  8443. ((eq org-log-note-purpose 'state)
  8444. (format "state change from \"%s\" to \"%s\""
  8445. (or org-log-note-previous-state "")
  8446. (or org-log-note-state "")))
  8447. ((eq org-log-note-purpose 'note)
  8448. "this entry")
  8449. (t (error "This should not happen")))))
  8450. (if org-log-note-extra (insert org-log-note-extra))
  8451. (org-set-local 'org-finish-function 'org-store-log-note)))
  8452. (defvar org-note-abort nil) ; dynamically scoped
  8453. (defun org-store-log-note ()
  8454. "Finish taking a log note, and insert it to where it belongs."
  8455. (let ((txt (buffer-string))
  8456. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8457. lines ind)
  8458. (kill-buffer (current-buffer))
  8459. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8460. (setq txt (replace-match "" t t txt)))
  8461. (if (string-match "\\s-+\\'" txt)
  8462. (setq txt (replace-match "" t t txt)))
  8463. (setq lines (org-split-string txt "\n"))
  8464. (when (and note (string-match "\\S-" note))
  8465. (setq note
  8466. (org-replace-escapes
  8467. note
  8468. (list (cons "%u" (user-login-name))
  8469. (cons "%U" user-full-name)
  8470. (cons "%t" (format-time-string
  8471. (org-time-stamp-format 'long 'inactive)
  8472. (current-time)))
  8473. (cons "%s" (if org-log-note-state
  8474. (concat "\"" org-log-note-state "\"")
  8475. ""))
  8476. (cons "%S" (if org-log-note-previous-state
  8477. (concat "\"" org-log-note-previous-state "\"")
  8478. "\"\"")))))
  8479. (if lines (setq note (concat note " \\\\")))
  8480. (push note lines))
  8481. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8482. (when lines
  8483. (save-excursion
  8484. (set-buffer (marker-buffer org-log-note-marker))
  8485. (save-excursion
  8486. (goto-char org-log-note-marker)
  8487. (move-marker org-log-note-marker nil)
  8488. (end-of-line 1)
  8489. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8490. (insert "- " (pop lines))
  8491. (org-indent-line-function)
  8492. (beginning-of-line 1)
  8493. (looking-at "[ \t]*")
  8494. (setq ind (concat (match-string 0) " "))
  8495. (end-of-line 1)
  8496. (while lines (insert "\n" ind (pop lines)))
  8497. (message "Note stored")
  8498. (org-back-to-heading t)
  8499. (org-cycle-hide-drawers 'children)))))
  8500. (set-window-configuration org-log-note-window-configuration)
  8501. (with-current-buffer (marker-buffer org-log-note-return-to)
  8502. (goto-char org-log-note-return-to))
  8503. (move-marker org-log-note-return-to nil)
  8504. (and org-log-post-message (message "%s" org-log-post-message)))
  8505. (defun org-sparse-tree (&optional arg)
  8506. "Create a sparse tree, prompt for the details.
  8507. This command can create sparse trees. You first need to select the type
  8508. of match used to create the tree:
  8509. t Show entries with a specific TODO keyword.
  8510. T Show entries selected by a tags match.
  8511. p Enter a property name and its value (both with completion on existing
  8512. names/values) and show entries with that property.
  8513. r Show entries matching a regular expression
  8514. d Show deadlines due within `org-deadline-warning-days'."
  8515. (interactive "P")
  8516. (let (ans kwd value)
  8517. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8518. (setq ans (read-char-exclusive))
  8519. (cond
  8520. ((equal ans ?d)
  8521. (call-interactively 'org-check-deadlines))
  8522. ((equal ans ?b)
  8523. (call-interactively 'org-check-before-date))
  8524. ((equal ans ?t)
  8525. (org-show-todo-tree '(4)))
  8526. ((equal ans ?T)
  8527. (call-interactively 'org-tags-sparse-tree))
  8528. ((member ans '(?p ?P))
  8529. (setq kwd (org-ido-completing-read "Property: "
  8530. (mapcar 'list (org-buffer-property-keys))))
  8531. (setq value (org-ido-completing-read "Value: "
  8532. (mapcar 'list (org-property-values kwd))))
  8533. (unless (string-match "\\`{.*}\\'" value)
  8534. (setq value (concat "\"" value "\"")))
  8535. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8536. ((member ans '(?r ?R ?/))
  8537. (call-interactively 'org-occur))
  8538. (t (error "No such sparse tree command \"%c\"" ans)))))
  8539. (defvar org-occur-highlights nil
  8540. "List of overlays used for occur matches.")
  8541. (make-variable-buffer-local 'org-occur-highlights)
  8542. (defvar org-occur-parameters nil
  8543. "Parameters of the active org-occur calls.
  8544. This is a list, each call to org-occur pushes as cons cell,
  8545. containing the regular expression and the callback, onto the list.
  8546. The list can contain several entries if `org-occur' has been called
  8547. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8548. will only contain one set of parameters. When the highlights are
  8549. removed (for example with `C-c C-c', or with the next edit (depending
  8550. on `org-remove-highlights-with-change'), this variable is emptied
  8551. as well.")
  8552. (make-variable-buffer-local 'org-occur-parameters)
  8553. (defun org-occur (regexp &optional keep-previous callback)
  8554. "Make a compact tree which shows all matches of REGEXP.
  8555. The tree will show the lines where the regexp matches, and all higher
  8556. headlines above the match. It will also show the heading after the match,
  8557. to make sure editing the matching entry is easy.
  8558. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8559. call to `org-occur' will be kept, to allow stacking of calls to this
  8560. command.
  8561. If CALLBACK is non-nil, it is a function which is called to confirm
  8562. that the match should indeed be shown."
  8563. (interactive "sRegexp: \nP")
  8564. (unless keep-previous
  8565. (org-remove-occur-highlights nil nil t))
  8566. (push (cons regexp callback) org-occur-parameters)
  8567. (let ((cnt 0))
  8568. (save-excursion
  8569. (goto-char (point-min))
  8570. (if (or (not keep-previous) ; do not want to keep
  8571. (not org-occur-highlights)) ; no previous matches
  8572. ;; hide everything
  8573. (org-overview))
  8574. (while (re-search-forward regexp nil t)
  8575. (when (or (not callback)
  8576. (save-match-data (funcall callback)))
  8577. (setq cnt (1+ cnt))
  8578. (when org-highlight-sparse-tree-matches
  8579. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8580. (org-show-context 'occur-tree))))
  8581. (when org-remove-highlights-with-change
  8582. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8583. nil 'local))
  8584. (unless org-sparse-tree-open-archived-trees
  8585. (org-hide-archived-subtrees (point-min) (point-max)))
  8586. (run-hooks 'org-occur-hook)
  8587. (if (interactive-p)
  8588. (message "%d match(es) for regexp %s" cnt regexp))
  8589. cnt))
  8590. (defun org-show-context (&optional key)
  8591. "Make sure point and context and visible.
  8592. How much context is shown depends upon the variables
  8593. `org-show-hierarchy-above', `org-show-following-heading'. and
  8594. `org-show-siblings'."
  8595. (let ((heading-p (org-on-heading-p t))
  8596. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8597. (following-p (org-get-alist-option org-show-following-heading key))
  8598. (entry-p (org-get-alist-option org-show-entry-below key))
  8599. (siblings-p (org-get-alist-option org-show-siblings key)))
  8600. (catch 'exit
  8601. ;; Show heading or entry text
  8602. (if (and heading-p (not entry-p))
  8603. (org-flag-heading nil) ; only show the heading
  8604. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8605. (org-show-hidden-entry))) ; show entire entry
  8606. (when following-p
  8607. ;; Show next sibling, or heading below text
  8608. (save-excursion
  8609. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8610. (org-flag-heading nil))))
  8611. (when siblings-p (org-show-siblings))
  8612. (when hierarchy-p
  8613. ;; show all higher headings, possibly with siblings
  8614. (save-excursion
  8615. (while (and (condition-case nil
  8616. (progn (org-up-heading-all 1) t)
  8617. (error nil))
  8618. (not (bobp)))
  8619. (org-flag-heading nil)
  8620. (when siblings-p (org-show-siblings))))))))
  8621. (defun org-reveal (&optional siblings)
  8622. "Show current entry, hierarchy above it, and the following headline.
  8623. This can be used to show a consistent set of context around locations
  8624. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8625. not t for the search context.
  8626. With optional argument SIBLINGS, on each level of the hierarchy all
  8627. siblings are shown. This repairs the tree structure to what it would
  8628. look like when opened with hierarchical calls to `org-cycle'."
  8629. (interactive "P")
  8630. (let ((org-show-hierarchy-above t)
  8631. (org-show-following-heading t)
  8632. (org-show-siblings (if siblings t org-show-siblings)))
  8633. (org-show-context nil)))
  8634. (defun org-highlight-new-match (beg end)
  8635. "Highlight from BEG to END and mark the highlight is an occur headline."
  8636. (let ((ov (org-make-overlay beg end)))
  8637. (org-overlay-put ov 'face 'secondary-selection)
  8638. (push ov org-occur-highlights)))
  8639. (defun org-remove-occur-highlights (&optional beg end noremove)
  8640. "Remove the occur highlights from the buffer.
  8641. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8642. from the `before-change-functions' in the current buffer."
  8643. (interactive)
  8644. (unless org-inhibit-highlight-removal
  8645. (mapc 'org-delete-overlay org-occur-highlights)
  8646. (setq org-occur-highlights nil)
  8647. (setq org-occur-parameters nil)
  8648. (unless noremove
  8649. (remove-hook 'before-change-functions
  8650. 'org-remove-occur-highlights 'local))))
  8651. ;;;; Priorities
  8652. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8653. "Regular expression matching the priority indicator.")
  8654. (defvar org-remove-priority-next-time nil)
  8655. (defun org-priority-up ()
  8656. "Increase the priority of the current item."
  8657. (interactive)
  8658. (org-priority 'up))
  8659. (defun org-priority-down ()
  8660. "Decrease the priority of the current item."
  8661. (interactive)
  8662. (org-priority 'down))
  8663. (defun org-priority (&optional action)
  8664. "Change the priority of an item by ARG.
  8665. ACTION can be `set', `up', `down', or a character."
  8666. (interactive)
  8667. (setq action (or action 'set))
  8668. (let (current new news have remove)
  8669. (save-excursion
  8670. (org-back-to-heading)
  8671. (if (looking-at org-priority-regexp)
  8672. (setq current (string-to-char (match-string 2))
  8673. have t)
  8674. (setq current org-default-priority))
  8675. (cond
  8676. ((or (eq action 'set)
  8677. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8678. (if (not (eq action 'set))
  8679. (setq new action)
  8680. (message "Priority %c-%c, SPC to remove: "
  8681. org-highest-priority org-lowest-priority)
  8682. (setq new (read-char-exclusive)))
  8683. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8684. (= (upcase org-lowest-priority) org-lowest-priority))
  8685. (setq new (upcase new)))
  8686. (cond ((equal new ?\ ) (setq remove t))
  8687. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8688. (error "Priority must be between `%c' and `%c'"
  8689. org-highest-priority org-lowest-priority))))
  8690. ((eq action 'up)
  8691. (if (and (not have) (eq last-command this-command))
  8692. (setq new org-lowest-priority)
  8693. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8694. org-default-priority (1- current)))))
  8695. ((eq action 'down)
  8696. (if (and (not have) (eq last-command this-command))
  8697. (setq new org-highest-priority)
  8698. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8699. org-default-priority (1+ current)))))
  8700. (t (error "Invalid action")))
  8701. (if (or (< (upcase new) org-highest-priority)
  8702. (> (upcase new) org-lowest-priority))
  8703. (setq remove t))
  8704. (setq news (format "%c" new))
  8705. (if have
  8706. (if remove
  8707. (replace-match "" t t nil 1)
  8708. (replace-match news t t nil 2))
  8709. (if remove
  8710. (error "No priority cookie found in line")
  8711. (looking-at org-todo-line-regexp)
  8712. (if (match-end 2)
  8713. (progn
  8714. (goto-char (match-end 2))
  8715. (insert " [#" news "]"))
  8716. (goto-char (match-beginning 3))
  8717. (insert "[#" news "] ")))))
  8718. (org-preserve-lc (org-set-tags nil 'align))
  8719. (if remove
  8720. (message "Priority removed")
  8721. (message "Priority of current item set to %s" news))))
  8722. (defun org-get-priority (s)
  8723. "Find priority cookie and return priority."
  8724. (save-match-data
  8725. (if (not (string-match org-priority-regexp s))
  8726. (* 1000 (- org-lowest-priority org-default-priority))
  8727. (* 1000 (- org-lowest-priority
  8728. (string-to-char (match-string 2 s)))))))
  8729. ;;;; Tags
  8730. (defvar org-agenda-archives-mode)
  8731. (defun org-scan-tags (action matcher &optional todo-only)
  8732. "Scan headline tags with inheritance and produce output ACTION.
  8733. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8734. or `agenda' to produce an entry list for an agenda view. It can also be
  8735. a Lisp form or a function that should be called at each matched headline, in
  8736. this case the return value is a list of all return values from these calls.
  8737. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8738. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8739. only lines with a TODO keyword are included in the output."
  8740. (require 'org-agenda)
  8741. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8742. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8743. (org-re
  8744. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8745. (props (list 'face 'default
  8746. 'done-face 'org-done
  8747. 'undone-face 'default
  8748. 'mouse-face 'highlight
  8749. 'org-not-done-regexp org-not-done-regexp
  8750. 'org-todo-regexp org-todo-regexp
  8751. 'keymap org-agenda-keymap
  8752. 'help-echo
  8753. (format "mouse-2 or RET jump to org file %s"
  8754. (abbreviate-file-name
  8755. (or (buffer-file-name (buffer-base-buffer))
  8756. (buffer-name (buffer-base-buffer)))))))
  8757. (case-fold-search nil)
  8758. lspos tags tags-list
  8759. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8760. (llast 0) rtn rtn1 level category i txt
  8761. todo marker entry priority)
  8762. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8763. (setq action (list 'lambda nil action)))
  8764. (save-excursion
  8765. (goto-char (point-min))
  8766. (when (eq action 'sparse-tree)
  8767. (org-overview)
  8768. (org-remove-occur-highlights))
  8769. (while (re-search-forward re nil t)
  8770. (catch :skip
  8771. (setq todo (if (match-end 1) (match-string 2))
  8772. tags (if (match-end 4) (match-string 4)))
  8773. (goto-char (setq lspos (1+ (match-beginning 0))))
  8774. (setq level (org-reduced-level (funcall outline-level))
  8775. category (org-get-category))
  8776. (setq i llast llast level)
  8777. ;; remove tag lists from same and sublevels
  8778. (while (>= i level)
  8779. (when (setq entry (assoc i tags-alist))
  8780. (setq tags-alist (delete entry tags-alist)))
  8781. (setq i (1- i)))
  8782. ;; add the next tags
  8783. (when tags
  8784. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8785. tags-alist
  8786. (cons (cons level tags) tags-alist)))
  8787. ;; compile tags for current headline
  8788. (setq tags-list
  8789. (if org-use-tag-inheritance
  8790. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8791. tags))
  8792. (when org-use-tag-inheritance
  8793. (setcdr (car tags-alist)
  8794. (mapcar (lambda (x)
  8795. (setq x (copy-sequence x))
  8796. (org-add-prop-inherited x))
  8797. (cdar tags-alist))))
  8798. (when (and tags org-use-tag-inheritance
  8799. (not (eq t org-use-tag-inheritance)))
  8800. ;; selective inheritance, remove uninherited ones
  8801. (setcdr (car tags-alist)
  8802. (org-remove-uniherited-tags (cdar tags-alist))))
  8803. (when (and (or (not todo-only)
  8804. (and (member todo org-not-done-keywords)
  8805. (or (not org-agenda-tags-todo-honor-ignore-options)
  8806. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8807. (let ((case-fold-search t)) (eval matcher))
  8808. (or
  8809. (not (member org-archive-tag tags-list))
  8810. ;; we have an archive tag, should we use this anyway?
  8811. (or (not org-agenda-skip-archived-trees)
  8812. (and (eq action 'agenda) org-agenda-archives-mode))))
  8813. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8814. ;; select this headline
  8815. (cond
  8816. ((eq action 'sparse-tree)
  8817. (and org-highlight-sparse-tree-matches
  8818. (org-get-heading) (match-end 0)
  8819. (org-highlight-new-match
  8820. (match-beginning 0) (match-beginning 1)))
  8821. (org-show-context 'tags-tree))
  8822. ((eq action 'agenda)
  8823. (setq txt (org-format-agenda-item
  8824. ""
  8825. (concat
  8826. (if org-tags-match-list-sublevels
  8827. (make-string (1- level) ?.) "")
  8828. (org-get-heading))
  8829. category (org-get-tags-at))
  8830. priority (org-get-priority txt))
  8831. (goto-char lspos)
  8832. (setq marker (org-agenda-new-marker))
  8833. (org-add-props txt props
  8834. 'org-marker marker 'org-hd-marker marker 'org-category category
  8835. 'todo-state todo
  8836. 'priority priority 'type "tagsmatch")
  8837. (push txt rtn))
  8838. ((functionp action)
  8839. (save-excursion
  8840. (setq rtn1 (funcall action))
  8841. (push rtn1 rtn))
  8842. (goto-char (point-at-eol)))
  8843. (t (error "Invalid action")))
  8844. ;; if we are to skip sublevels, jump to end of subtree
  8845. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8846. (when (and (eq action 'sparse-tree)
  8847. (not org-sparse-tree-open-archived-trees))
  8848. (org-hide-archived-subtrees (point-min) (point-max)))
  8849. (nreverse rtn)))
  8850. (defun org-remove-uniherited-tags (tags)
  8851. "Remove all tags that are not inherited from the list TAGS."
  8852. (cond
  8853. ((eq org-use-tag-inheritance t)
  8854. (if org-tags-exclude-from-inheritance
  8855. (org-delete-all org-tags-exclude-from-inheritance tags)
  8856. tags))
  8857. ((not org-use-tag-inheritance) nil)
  8858. ((stringp org-use-tag-inheritance)
  8859. (delq nil (mapcar
  8860. (lambda (x)
  8861. (if (and (string-match org-use-tag-inheritance x)
  8862. (not (member x org-tags-exclude-from-inheritance)))
  8863. x nil))
  8864. tags)))
  8865. ((listp org-use-tag-inheritance)
  8866. (delq nil (mapcar
  8867. (lambda (x)
  8868. (if (member x org-use-tag-inheritance) x nil))
  8869. tags)))))
  8870. (defvar todo-only) ;; dynamically scoped
  8871. (defun org-tags-sparse-tree (&optional todo-only match)
  8872. "Create a sparse tree according to tags string MATCH.
  8873. MATCH can contain positive and negative selection of tags, like
  8874. \"+WORK+URGENT-WITHBOSS\".
  8875. If optional argument TODO-ONLY is non-nil, only select lines that are
  8876. also TODO lines."
  8877. (interactive "P")
  8878. (org-prepare-agenda-buffers (list (current-buffer)))
  8879. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8880. (defvar org-cached-props nil)
  8881. (defun org-cached-entry-get (pom property)
  8882. (if (or (eq t org-use-property-inheritance)
  8883. (and (stringp org-use-property-inheritance)
  8884. (string-match org-use-property-inheritance property))
  8885. (and (listp org-use-property-inheritance)
  8886. (member property org-use-property-inheritance)))
  8887. ;; Caching is not possible, check it directly
  8888. (org-entry-get pom property 'inherit)
  8889. ;; Get all properties, so that we can do complicated checks easily
  8890. (cdr (assoc property (or org-cached-props
  8891. (setq org-cached-props
  8892. (org-entry-properties pom)))))))
  8893. (defun org-global-tags-completion-table (&optional files)
  8894. "Return the list of all tags in all agenda buffer/files."
  8895. (save-excursion
  8896. (org-uniquify
  8897. (delq nil
  8898. (apply 'append
  8899. (mapcar
  8900. (lambda (file)
  8901. (set-buffer (find-file-noselect file))
  8902. (append (org-get-buffer-tags)
  8903. (mapcar (lambda (x) (if (stringp (car-safe x))
  8904. (list (car-safe x)) nil))
  8905. org-tag-alist)))
  8906. (if (and files (car files))
  8907. files
  8908. (org-agenda-files))))))))
  8909. (defun org-make-tags-matcher (match)
  8910. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8911. ;; todo-only is scoped dynamically into this function, and the function
  8912. ;; may change it if the matcher asks for it.
  8913. (unless match
  8914. ;; Get a new match request, with completion
  8915. (let ((org-last-tags-completion-table
  8916. (org-global-tags-completion-table)))
  8917. (setq match (org-completing-read
  8918. "Match: " 'org-tags-completion-function nil nil nil
  8919. 'org-tags-history))))
  8920. ;; Parse the string and create a lisp form
  8921. (let ((match0 match)
  8922. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8923. minus tag mm
  8924. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8925. orterms term orlist re-p str-p level-p level-op time-p
  8926. prop-p pn pv po cat-p gv rest)
  8927. (if (string-match "/+" match)
  8928. ;; match contains also a todo-matching request
  8929. (progn
  8930. (setq tagsmatch (substring match 0 (match-beginning 0))
  8931. todomatch (substring match (match-end 0)))
  8932. (if (string-match "^!" todomatch)
  8933. (setq todo-only t todomatch (substring todomatch 1)))
  8934. (if (string-match "^\\s-*$" todomatch)
  8935. (setq todomatch nil)))
  8936. ;; only matching tags
  8937. (setq tagsmatch match todomatch nil))
  8938. ;; Make the tags matcher
  8939. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8940. (setq tagsmatcher t)
  8941. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8942. (while (setq term (pop orterms))
  8943. (while (and (equal (substring term -1) "\\") orterms)
  8944. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8945. (while (string-match re term)
  8946. (setq rest (substring term (match-end 0))
  8947. minus (and (match-end 1)
  8948. (equal (match-string 1 term) "-"))
  8949. tag (match-string 2 term)
  8950. re-p (equal (string-to-char tag) ?{)
  8951. level-p (match-end 4)
  8952. prop-p (match-end 5)
  8953. mm (cond
  8954. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8955. (level-p
  8956. (setq level-op (org-op-to-function (match-string 3 term)))
  8957. `(,level-op level ,(string-to-number
  8958. (match-string 4 term))))
  8959. (prop-p
  8960. (setq pn (match-string 5 term)
  8961. po (match-string 6 term)
  8962. pv (match-string 7 term)
  8963. cat-p (equal pn "CATEGORY")
  8964. re-p (equal (string-to-char pv) ?{)
  8965. str-p (equal (string-to-char pv) ?\")
  8966. time-p (save-match-data
  8967. (string-match "^\"[[<].*[]>]\"$" pv))
  8968. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8969. (if time-p (setq pv (org-matcher-time pv)))
  8970. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8971. (cond
  8972. ((equal pn "CATEGORY")
  8973. (setq gv '(get-text-property (point) 'org-category)))
  8974. ((equal pn "TODO")
  8975. (setq gv 'todo))
  8976. (t
  8977. (setq gv `(org-cached-entry-get nil ,pn))))
  8978. (if re-p
  8979. (if (eq po 'org<>)
  8980. `(not (string-match ,pv (or ,gv "")))
  8981. `(string-match ,pv (or ,gv "")))
  8982. (if str-p
  8983. `(,po (or ,gv "") ,pv)
  8984. `(,po (string-to-number (or ,gv ""))
  8985. ,(string-to-number pv) ))))
  8986. (t `(member ,(downcase tag) tags-list)))
  8987. mm (if minus (list 'not mm) mm)
  8988. term rest)
  8989. (push mm tagsmatcher))
  8990. (push (if (> (length tagsmatcher) 1)
  8991. (cons 'and tagsmatcher)
  8992. (car tagsmatcher))
  8993. orlist)
  8994. (setq tagsmatcher nil))
  8995. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8996. (setq tagsmatcher
  8997. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8998. ;; Make the todo matcher
  8999. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9000. (setq todomatcher t)
  9001. (setq orterms (org-split-string todomatch "|") orlist nil)
  9002. (while (setq term (pop orterms))
  9003. (while (string-match re term)
  9004. (setq minus (and (match-end 1)
  9005. (equal (match-string 1 term) "-"))
  9006. kwd (match-string 2 term)
  9007. re-p (equal (string-to-char kwd) ?{)
  9008. term (substring term (match-end 0))
  9009. mm (if re-p
  9010. `(string-match ,(substring kwd 1 -1) todo)
  9011. (list 'equal 'todo kwd))
  9012. mm (if minus (list 'not mm) mm))
  9013. (push mm todomatcher))
  9014. (push (if (> (length todomatcher) 1)
  9015. (cons 'and todomatcher)
  9016. (car todomatcher))
  9017. orlist)
  9018. (setq todomatcher nil))
  9019. (setq todomatcher (if (> (length orlist) 1)
  9020. (cons 'or orlist) (car orlist))))
  9021. ;; Return the string and lisp forms of the matcher
  9022. (setq matcher (if todomatcher
  9023. (list 'and tagsmatcher todomatcher)
  9024. tagsmatcher))
  9025. (cons match0 matcher)))
  9026. (defun org-op-to-function (op &optional stringp)
  9027. "Turn an operator into the appropriate function."
  9028. (setq op
  9029. (cond
  9030. ((equal op "<" ) '(< string< org-time<))
  9031. ((equal op ">" ) '(> org-string> org-time>))
  9032. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9033. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9034. ((member op '("=" "==")) '(= string= org-time=))
  9035. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9036. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9037. (defun org<> (a b) (not (= a b)))
  9038. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9039. (defun org-string>= (a b) (not (string< a b)))
  9040. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9041. (defun org-string<> (a b) (not (string= a b)))
  9042. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9043. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9044. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9045. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> 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) (org<> a b)))
  9048. (defun org-2ft (s)
  9049. "Convert S to a floating point time.
  9050. If S is already a number, just return it. If it is a string, parse
  9051. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9052. (cond
  9053. ((numberp s) s)
  9054. ((stringp s)
  9055. (condition-case nil
  9056. (float-time (apply 'encode-time (org-parse-time-string s)))
  9057. (error 0.)))
  9058. (t 0.)))
  9059. (defun org-time-today ()
  9060. "Time in seconds today at 0:00.
  9061. Returns the float number of seconds since the beginning of the
  9062. epoch to the beginning of today (00:00)."
  9063. (float-time (apply 'encode-time
  9064. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9065. (defun org-matcher-time (s)
  9066. "Interpret a time comparison value."
  9067. (save-match-data
  9068. (cond
  9069. ((string= s "<now>") (float-time))
  9070. ((string= s "<today>") (org-time-today))
  9071. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9072. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9073. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9074. (+ (org-time-today)
  9075. (* (string-to-number (match-string 1 s))
  9076. (cdr (assoc (match-string 2 s)
  9077. '(("d" . 86400.0) ("w" . 604800.0)
  9078. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9079. (t (org-2ft s)))))
  9080. (defun org-match-any-p (re list)
  9081. "Does re match any element of list?"
  9082. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9083. (delq nil list))
  9084. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9085. (defvar org-tags-overlay (org-make-overlay 1 1))
  9086. (org-detach-overlay org-tags-overlay)
  9087. (defun org-get-local-tags-at (&optional pos)
  9088. "Get a list of tags defined in the current headline."
  9089. (org-get-tags-at pos 'local))
  9090. (defun org-get-local-tags ()
  9091. "Get a list of tags defined in the current headline."
  9092. (org-get-tags-at nil 'local))
  9093. (defun org-get-tags-at (&optional pos local)
  9094. "Get a list of all headline tags applicable at POS.
  9095. POS defaults to point. If tags are inherited, the list contains
  9096. the targets in the same sequence as the headlines appear, i.e.
  9097. the tags of the current headline come last.
  9098. When LOCAL is non-nil, only return tags from the current headline,
  9099. ignore inherited ones."
  9100. (interactive)
  9101. (let (tags ltags lastpos parent)
  9102. (save-excursion
  9103. (save-restriction
  9104. (widen)
  9105. (goto-char (or pos (point)))
  9106. (save-match-data
  9107. (catch 'done
  9108. (condition-case nil
  9109. (progn
  9110. (org-back-to-heading t)
  9111. (while (not (equal lastpos (point)))
  9112. (setq lastpos (point))
  9113. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9114. (setq ltags (org-split-string
  9115. (org-match-string-no-properties 1) ":"))
  9116. (when parent
  9117. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9118. (setq tags (append
  9119. (if parent
  9120. (org-remove-uniherited-tags ltags)
  9121. ltags)
  9122. tags)))
  9123. (or org-use-tag-inheritance (throw 'done t))
  9124. (if local (throw 'done t))
  9125. (org-up-heading-all 1)
  9126. (setq parent t)))
  9127. (error nil)))))
  9128. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9129. (defun org-add-prop-inherited (s)
  9130. (add-text-properties 0 (length s) '(inherited t) s)
  9131. s)
  9132. (defun org-toggle-tag (tag &optional onoff)
  9133. "Toggle the tag TAG for the current line.
  9134. If ONOFF is `on' or `off', don't toggle but set to this state."
  9135. (let (res current)
  9136. (save-excursion
  9137. (org-back-to-heading t)
  9138. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9139. (point-at-eol) t)
  9140. (progn
  9141. (setq current (match-string 1))
  9142. (replace-match ""))
  9143. (setq current ""))
  9144. (setq current (nreverse (org-split-string current ":")))
  9145. (cond
  9146. ((eq onoff 'on)
  9147. (setq res t)
  9148. (or (member tag current) (push tag current)))
  9149. ((eq onoff 'off)
  9150. (or (not (member tag current)) (setq current (delete tag current))))
  9151. (t (if (member tag current)
  9152. (setq current (delete tag current))
  9153. (setq res t)
  9154. (push tag current))))
  9155. (end-of-line 1)
  9156. (if current
  9157. (progn
  9158. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9159. (org-set-tags nil t))
  9160. (delete-horizontal-space))
  9161. (run-hooks 'org-after-tags-change-hook))
  9162. res))
  9163. (defun org-align-tags-here (to-col)
  9164. ;; Assumes that this is a headline
  9165. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9166. (beginning-of-line 1)
  9167. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9168. (< pos (match-beginning 2)))
  9169. (progn
  9170. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9171. (goto-char (match-beginning 1))
  9172. (insert " ")
  9173. (delete-region (point) (1+ (match-beginning 2)))
  9174. (setq ncol (max (1+ (current-column))
  9175. (1+ col)
  9176. (if (> to-col 0)
  9177. to-col
  9178. (- (abs to-col) tags-l))))
  9179. (setq p (point))
  9180. (insert (make-string (- ncol (current-column)) ?\ ))
  9181. (setq ncol (current-column))
  9182. (when indent-tabs-mode (tabify p (point-at-eol)))
  9183. (org-move-to-column (min ncol col) t))
  9184. (goto-char pos))))
  9185. (defun org-set-tags-command (&optional arg just-align)
  9186. "Call the set-tags command for the current entry."
  9187. (interactive "P")
  9188. (if (org-on-heading-p)
  9189. (org-set-tags arg just-align)
  9190. (save-excursion
  9191. (org-back-to-heading t)
  9192. (org-set-tags arg just-align))))
  9193. (defun org-set-tags (&optional arg just-align)
  9194. "Set the tags for the current headline.
  9195. With prefix ARG, realign all tags in headings in the current buffer."
  9196. (interactive "P")
  9197. (let* ((re (concat "^" outline-regexp))
  9198. (current (org-get-tags-string))
  9199. (col (current-column))
  9200. (org-setting-tags t)
  9201. table current-tags inherited-tags ; computed below when needed
  9202. tags p0 c0 c1 rpl)
  9203. (if arg
  9204. (save-excursion
  9205. (goto-char (point-min))
  9206. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9207. (while (re-search-forward re nil t)
  9208. (org-set-tags nil t)
  9209. (end-of-line 1)))
  9210. (message "All tags realigned to column %d" org-tags-column))
  9211. (if just-align
  9212. (setq tags current)
  9213. ;; Get a new set of tags from the user
  9214. (save-excursion
  9215. (setq table (or org-tag-alist (org-get-buffer-tags))
  9216. org-last-tags-completion-table table
  9217. current-tags (org-split-string current ":")
  9218. inherited-tags (nreverse
  9219. (nthcdr (length current-tags)
  9220. (nreverse (org-get-tags-at))))
  9221. tags
  9222. (if (or (eq t org-use-fast-tag-selection)
  9223. (and org-use-fast-tag-selection
  9224. (delq nil (mapcar 'cdr table))))
  9225. (org-fast-tag-selection
  9226. current-tags inherited-tags table
  9227. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9228. (let ((org-add-colon-after-tag-completion t))
  9229. (org-trim
  9230. (org-without-partial-completion
  9231. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9232. nil nil current 'org-tags-history)))))))
  9233. (while (string-match "[-+&]+" tags)
  9234. ;; No boolean logic, just a list
  9235. (setq tags (replace-match ":" t t tags))))
  9236. (if (string-match "\\`[\t ]*\\'" tags)
  9237. (setq tags "")
  9238. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9239. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9240. ;; Insert new tags at the correct column
  9241. (beginning-of-line 1)
  9242. (cond
  9243. ((and (equal current "") (equal tags "")))
  9244. ((re-search-forward
  9245. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9246. (point-at-eol) t)
  9247. (if (equal tags "")
  9248. (setq rpl "")
  9249. (goto-char (match-beginning 0))
  9250. (setq c0 (current-column) p0 (point)
  9251. c1 (max (1+ c0) (if (> org-tags-column 0)
  9252. org-tags-column
  9253. (- (- org-tags-column) (length tags))))
  9254. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9255. (replace-match rpl t t)
  9256. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9257. tags)
  9258. (t (error "Tags alignment failed")))
  9259. (org-move-to-column col)
  9260. (unless just-align
  9261. (run-hooks 'org-after-tags-change-hook)))))
  9262. (defun org-change-tag-in-region (beg end tag off)
  9263. "Add or remove TAG for each entry in the region.
  9264. This works in the agenda, and also in an org-mode buffer."
  9265. (interactive
  9266. (list (region-beginning) (region-end)
  9267. (let ((org-last-tags-completion-table
  9268. (if (org-mode-p)
  9269. (org-get-buffer-tags)
  9270. (org-global-tags-completion-table))))
  9271. (org-ido-completing-read
  9272. "Tag: " 'org-tags-completion-function nil nil nil
  9273. 'org-tags-history))
  9274. (progn
  9275. (message "[s]et or [r]emove? ")
  9276. (equal (read-char-exclusive) ?r))))
  9277. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9278. (let ((agendap (equal major-mode 'org-agenda-mode))
  9279. l1 l2 m buf pos newhead (cnt 0))
  9280. (goto-char end)
  9281. (setq l2 (1- (org-current-line)))
  9282. (goto-char beg)
  9283. (setq l1 (org-current-line))
  9284. (loop for l from l1 to l2 do
  9285. (goto-line l)
  9286. (setq m (get-text-property (point) 'org-hd-marker))
  9287. (when (or (and (org-mode-p) (org-on-heading-p))
  9288. (and agendap m))
  9289. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9290. pos (if agendap m (point)))
  9291. (with-current-buffer buf
  9292. (save-excursion
  9293. (save-restriction
  9294. (goto-char pos)
  9295. (setq cnt (1+ cnt))
  9296. (org-toggle-tag tag (if off 'off 'on))
  9297. (setq newhead (org-get-heading)))))
  9298. (and agendap (org-agenda-change-all-lines newhead m))))
  9299. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9300. (defun org-tags-completion-function (string predicate &optional flag)
  9301. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9302. (confirm (lambda (x) (stringp (car x)))))
  9303. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9304. (setq s1 (match-string 1 string)
  9305. s2 (match-string 2 string))
  9306. (setq s1 "" s2 string))
  9307. (cond
  9308. ((eq flag nil)
  9309. ;; try completion
  9310. (setq rtn (try-completion s2 ctable confirm))
  9311. (if (stringp rtn)
  9312. (setq rtn
  9313. (concat s1 s2 (substring rtn (length s2))
  9314. (if (and org-add-colon-after-tag-completion
  9315. (assoc rtn ctable))
  9316. ":" ""))))
  9317. rtn)
  9318. ((eq flag t)
  9319. ;; all-completions
  9320. (all-completions s2 ctable confirm)
  9321. )
  9322. ((eq flag 'lambda)
  9323. ;; exact match?
  9324. (assoc s2 ctable)))
  9325. ))
  9326. (defun org-fast-tag-insert (kwd tags face &optional end)
  9327. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9328. (insert (format "%-12s" (concat kwd ":"))
  9329. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9330. (or end "")))
  9331. (defun org-fast-tag-show-exit (flag)
  9332. (save-excursion
  9333. (goto-line 3)
  9334. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9335. (replace-match ""))
  9336. (when flag
  9337. (end-of-line 1)
  9338. (org-move-to-column (- (window-width) 19) t)
  9339. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9340. (defun org-set-current-tags-overlay (current prefix)
  9341. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9342. (if (featurep 'xemacs)
  9343. (org-overlay-display org-tags-overlay (concat prefix s)
  9344. 'secondary-selection)
  9345. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9346. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9347. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9348. "Fast tag selection with single keys.
  9349. CURRENT is the current list of tags in the headline, INHERITED is the
  9350. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9351. possibly with grouping information. TODO-TABLE is a similar table with
  9352. TODO keywords, should these have keys assigned to them.
  9353. If the keys are nil, a-z are automatically assigned.
  9354. Returns the new tags string, or nil to not change the current settings."
  9355. (let* ((fulltable (append table todo-table))
  9356. (maxlen (apply 'max (mapcar
  9357. (lambda (x)
  9358. (if (stringp (car x)) (string-width (car x)) 0))
  9359. fulltable)))
  9360. (buf (current-buffer))
  9361. (expert (eq org-fast-tag-selection-single-key 'expert))
  9362. (buffer-tags nil)
  9363. (fwidth (+ maxlen 3 1 3))
  9364. (ncol (/ (- (window-width) 4) fwidth))
  9365. (i-face 'org-done)
  9366. (c-face 'org-todo)
  9367. tg cnt e c char c1 c2 ntable tbl rtn
  9368. ov-start ov-end ov-prefix
  9369. (exit-after-next org-fast-tag-selection-single-key)
  9370. (done-keywords org-done-keywords)
  9371. groups ingroup)
  9372. (save-excursion
  9373. (beginning-of-line 1)
  9374. (if (looking-at
  9375. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9376. (setq ov-start (match-beginning 1)
  9377. ov-end (match-end 1)
  9378. ov-prefix "")
  9379. (setq ov-start (1- (point-at-eol))
  9380. ov-end (1+ ov-start))
  9381. (skip-chars-forward "^\n\r")
  9382. (setq ov-prefix
  9383. (concat
  9384. (buffer-substring (1- (point)) (point))
  9385. (if (> (current-column) org-tags-column)
  9386. " "
  9387. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9388. (org-move-overlay org-tags-overlay ov-start ov-end)
  9389. (save-window-excursion
  9390. (if expert
  9391. (set-buffer (get-buffer-create " *Org tags*"))
  9392. (delete-other-windows)
  9393. (split-window-vertically)
  9394. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9395. (erase-buffer)
  9396. (org-set-local 'org-done-keywords done-keywords)
  9397. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9398. (org-fast-tag-insert "Current" current c-face "\n\n")
  9399. (org-fast-tag-show-exit exit-after-next)
  9400. (org-set-current-tags-overlay current ov-prefix)
  9401. (setq tbl fulltable char ?a cnt 0)
  9402. (while (setq e (pop tbl))
  9403. (cond
  9404. ((equal e '(:startgroup))
  9405. (push '() groups) (setq ingroup t)
  9406. (when (not (= cnt 0))
  9407. (setq cnt 0)
  9408. (insert "\n"))
  9409. (insert "{ "))
  9410. ((equal e '(:endgroup))
  9411. (setq ingroup nil cnt 0)
  9412. (insert "}\n"))
  9413. (t
  9414. (setq tg (car e) c2 nil)
  9415. (if (cdr e)
  9416. (setq c (cdr e))
  9417. ;; automatically assign a character.
  9418. (setq c1 (string-to-char
  9419. (downcase (substring
  9420. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9421. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9422. (while (or (rassoc char ntable) (rassoc char table))
  9423. (setq char (1+ char)))
  9424. (setq c2 c1))
  9425. (setq c (or c2 char)))
  9426. (if ingroup (push tg (car groups)))
  9427. (setq tg (org-add-props tg nil 'face
  9428. (cond
  9429. ((not (assoc tg table))
  9430. (org-get-todo-face tg))
  9431. ((member tg current) c-face)
  9432. ((member tg inherited) i-face)
  9433. (t nil))))
  9434. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9435. (insert "[" c "] " tg (make-string
  9436. (- fwidth 4 (length tg)) ?\ ))
  9437. (push (cons tg c) ntable)
  9438. (when (= (setq cnt (1+ cnt)) ncol)
  9439. (insert "\n")
  9440. (if ingroup (insert " "))
  9441. (setq cnt 0)))))
  9442. (setq ntable (nreverse ntable))
  9443. (insert "\n")
  9444. (goto-char (point-min))
  9445. (if (not expert) (org-fit-window-to-buffer))
  9446. (setq rtn
  9447. (catch 'exit
  9448. (while t
  9449. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9450. (if groups " [!] no groups" " [!]groups")
  9451. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9452. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9453. (cond
  9454. ((= c ?\r) (throw 'exit t))
  9455. ((= c ?!)
  9456. (setq groups (not groups))
  9457. (goto-char (point-min))
  9458. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9459. ((= c ?\C-c)
  9460. (if (not expert)
  9461. (org-fast-tag-show-exit
  9462. (setq exit-after-next (not exit-after-next)))
  9463. (setq expert nil)
  9464. (delete-other-windows)
  9465. (split-window-vertically)
  9466. (org-switch-to-buffer-other-window " *Org tags*")
  9467. (org-fit-window-to-buffer)))
  9468. ((or (= c ?\C-g)
  9469. (and (= c ?q) (not (rassoc c ntable))))
  9470. (org-detach-overlay org-tags-overlay)
  9471. (setq quit-flag t))
  9472. ((= c ?\ )
  9473. (setq current nil)
  9474. (if exit-after-next (setq exit-after-next 'now)))
  9475. ((= c ?\t)
  9476. (condition-case nil
  9477. (setq tg (org-ido-completing-read
  9478. "Tag: "
  9479. (or buffer-tags
  9480. (with-current-buffer buf
  9481. (org-get-buffer-tags)))))
  9482. (quit (setq tg "")))
  9483. (when (string-match "\\S-" tg)
  9484. (add-to-list 'buffer-tags (list tg))
  9485. (if (member tg current)
  9486. (setq current (delete tg current))
  9487. (push tg current)))
  9488. (if exit-after-next (setq exit-after-next 'now)))
  9489. ((setq e (rassoc c todo-table) tg (car e))
  9490. (with-current-buffer buf
  9491. (save-excursion (org-todo tg)))
  9492. (if exit-after-next (setq exit-after-next 'now)))
  9493. ((setq e (rassoc c ntable) tg (car e))
  9494. (if (member tg current)
  9495. (setq current (delete tg current))
  9496. (loop for g in groups do
  9497. (if (member tg g)
  9498. (mapc (lambda (x)
  9499. (setq current (delete x current)))
  9500. g)))
  9501. (push tg current))
  9502. (if exit-after-next (setq exit-after-next 'now))))
  9503. ;; Create a sorted list
  9504. (setq current
  9505. (sort current
  9506. (lambda (a b)
  9507. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9508. (if (eq exit-after-next 'now) (throw 'exit t))
  9509. (goto-char (point-min))
  9510. (beginning-of-line 2)
  9511. (delete-region (point) (point-at-eol))
  9512. (org-fast-tag-insert "Current" current c-face)
  9513. (org-set-current-tags-overlay current ov-prefix)
  9514. (while (re-search-forward
  9515. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9516. (setq tg (match-string 1))
  9517. (add-text-properties
  9518. (match-beginning 1) (match-end 1)
  9519. (list 'face
  9520. (cond
  9521. ((member tg current) c-face)
  9522. ((member tg inherited) i-face)
  9523. (t (get-text-property (match-beginning 1) 'face))))))
  9524. (goto-char (point-min)))))
  9525. (org-detach-overlay org-tags-overlay)
  9526. (if rtn
  9527. (mapconcat 'identity current ":")
  9528. nil))))
  9529. (defun org-get-tags-string ()
  9530. "Get the TAGS string in the current headline."
  9531. (unless (org-on-heading-p t)
  9532. (error "Not on a heading"))
  9533. (save-excursion
  9534. (beginning-of-line 1)
  9535. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9536. (org-match-string-no-properties 1)
  9537. "")))
  9538. (defun org-get-tags ()
  9539. "Get the list of tags specified in the current headline."
  9540. (org-split-string (org-get-tags-string) ":"))
  9541. (defun org-get-buffer-tags ()
  9542. "Get a table of all tags used in the buffer, for completion."
  9543. (let (tags)
  9544. (save-excursion
  9545. (goto-char (point-min))
  9546. (while (re-search-forward
  9547. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9548. (when (equal (char-after (point-at-bol 0)) ?*)
  9549. (mapc (lambda (x) (add-to-list 'tags x))
  9550. (org-split-string (org-match-string-no-properties 1) ":")))))
  9551. (mapcar 'list tags)))
  9552. ;;;; The mapping API
  9553. ;;;###autoload
  9554. (defun org-map-entries (func &optional match scope &rest skip)
  9555. "Call FUNC at each headline selected by MATCH in SCOPE.
  9556. FUNC is a function or a lisp form. The function will be called without
  9557. arguments, with the cursor positioned at the beginning of the headline.
  9558. The return values of all calls to the function will be collected and
  9559. returned as a list.
  9560. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9561. Only headlines that are matched by this query will be considered during
  9562. the iteration. When MATCH is nil or t, all headlines will be
  9563. visited by the iteration.
  9564. SCOPE determines the scope of this command. It can be any of:
  9565. nil The current buffer, respecting the restriction if any
  9566. tree The subtree started with the entry at point
  9567. file The current buffer, without restriction
  9568. file-with-archives
  9569. The current buffer, and any archives associated with it
  9570. agenda All agenda files
  9571. agenda-with-archives
  9572. All agenda files with any archive files associated with them
  9573. \(file1 file2 ...)
  9574. If this is a list, all files in the list will be scanned
  9575. The remaining args are treated as settings for the skipping facilities of
  9576. the scanner. The following items can be given here:
  9577. archive skip trees with the archive tag.
  9578. comment skip trees with the COMMENT keyword
  9579. function or Emacs Lisp form:
  9580. will be used as value for `org-agenda-skip-function', so whenever
  9581. the the function returns t, FUNC will not be called for that
  9582. entry and search will continue from the point where the
  9583. function leaves it."
  9584. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9585. (org-agenda-skip-archived-trees (memq 'archive skip))
  9586. (org-agenda-skip-comment-trees (memq 'comment skip))
  9587. (org-agenda-skip-function
  9588. (car (org-delete-all '(comment archive) skip)))
  9589. (org-tags-match-list-sublevels t)
  9590. matcher file res
  9591. org-todo-keywords-for-agenda
  9592. org-done-keywords-for-agenda
  9593. org-todo-keyword-alist-for-agenda
  9594. org-tag-alist-for-agenda)
  9595. (cond
  9596. ((eq match t) (setq matcher t))
  9597. ((eq match nil) (setq matcher t))
  9598. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9599. (save-excursion
  9600. (save-restriction
  9601. (when (eq scope 'tree)
  9602. (org-back-to-heading t)
  9603. (org-narrow-to-subtree)
  9604. (setq scope nil))
  9605. (if (not scope)
  9606. (progn
  9607. (org-prepare-agenda-buffers
  9608. (list (buffer-file-name (current-buffer))))
  9609. (setq res (org-scan-tags func matcher)))
  9610. ;; Get the right scope
  9611. (cond
  9612. ((and scope (listp scope) (symbolp (car scope)))
  9613. (setq scope (eval scope)))
  9614. ((eq scope 'agenda)
  9615. (setq scope (org-agenda-files t)))
  9616. ((eq scope 'agenda-with-archives)
  9617. (setq scope (org-agenda-files t))
  9618. (setq scope (org-add-archive-files scope)))
  9619. ((eq scope 'file)
  9620. (setq scope (list (buffer-file-name))))
  9621. ((eq scope 'file-with-archives)
  9622. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9623. (org-prepare-agenda-buffers scope)
  9624. (while (setq file (pop scope))
  9625. (with-current-buffer (org-find-base-buffer-visiting file)
  9626. (save-excursion
  9627. (save-restriction
  9628. (widen)
  9629. (goto-char (point-min))
  9630. (setq res (append res (org-scan-tags func matcher))))))))))
  9631. res))
  9632. ;;;; Properties
  9633. ;;; Setting and retrieving properties
  9634. (defconst org-special-properties
  9635. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9636. "TIMESTAMP" "TIMESTAMP_IA")
  9637. "The special properties valid in Org-mode.
  9638. These are properties that are not defined in the property drawer,
  9639. but in some other way.")
  9640. (defconst org-default-properties
  9641. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9642. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9643. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9644. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9645. "Some properties that are used by Org-mode for various purposes.
  9646. Being in this list makes sure that they are offered for completion.")
  9647. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9648. "Regular expression matching the first line of a property drawer.")
  9649. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9650. "Regular expression matching the first line of a property drawer.")
  9651. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9652. "Regular expression matching the first line of a property drawer.")
  9653. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9654. "Regular expression matching the first line of a property drawer.")
  9655. (defconst org-property-drawer-re
  9656. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9657. org-property-end-re "\\)\n?")
  9658. "Matches an entire property drawer.")
  9659. (defconst org-clock-drawer-re
  9660. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9661. org-property-end-re "\\)\n?")
  9662. "Matches an entire clock drawer.")
  9663. (defun org-property-action ()
  9664. "Do an action on properties."
  9665. (interactive)
  9666. (let (c)
  9667. (org-at-property-p)
  9668. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9669. (setq c (read-char-exclusive))
  9670. (cond
  9671. ((equal c ?s)
  9672. (call-interactively 'org-set-property))
  9673. ((equal c ?d)
  9674. (call-interactively 'org-delete-property))
  9675. ((equal c ?D)
  9676. (call-interactively 'org-delete-property-globally))
  9677. ((equal c ?c)
  9678. (call-interactively 'org-compute-property-at-point))
  9679. (t (error "No such property action %c" c)))))
  9680. (defun org-at-property-p ()
  9681. "Is the cursor in a property line?"
  9682. ;; FIXME: Does not check if we are actually in the drawer.
  9683. ;; FIXME: also returns true on any drawers.....
  9684. ;; This is used by C-c C-c for property action.
  9685. (save-excursion
  9686. (beginning-of-line 1)
  9687. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9688. (defun org-get-property-block (&optional beg end force)
  9689. "Return the (beg . end) range of the body of the property drawer.
  9690. BEG and END can be beginning and end of subtree, if not given
  9691. they will be found.
  9692. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9693. (catch 'exit
  9694. (save-excursion
  9695. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9696. (end (or end (progn (outline-next-heading) (point)))))
  9697. (goto-char beg)
  9698. (if (re-search-forward org-property-start-re end t)
  9699. (setq beg (1+ (match-end 0)))
  9700. (if force
  9701. (save-excursion
  9702. (org-insert-property-drawer)
  9703. (setq end (progn (outline-next-heading) (point))))
  9704. (throw 'exit nil))
  9705. (goto-char beg)
  9706. (if (re-search-forward org-property-start-re end t)
  9707. (setq beg (1+ (match-end 0)))))
  9708. (if (re-search-forward org-property-end-re end t)
  9709. (setq end (match-beginning 0))
  9710. (or force (throw 'exit nil))
  9711. (goto-char beg)
  9712. (setq end beg)
  9713. (org-indent-line-function)
  9714. (insert ":END:\n"))
  9715. (cons beg end)))))
  9716. (defun org-entry-properties (&optional pom which)
  9717. "Get all properties of the entry at point-or-marker POM.
  9718. This includes the TODO keyword, the tags, time strings for deadline,
  9719. scheduled, and clocking, and any additional properties defined in the
  9720. entry. The return value is an alist, keys may occur multiple times
  9721. if the property key was used several times.
  9722. POM may also be nil, in which case the current entry is used.
  9723. If WHICH is nil or `all', get all properties. If WHICH is
  9724. `special' or `standard', only get that subclass."
  9725. (setq which (or which 'all))
  9726. (org-with-point-at pom
  9727. (let ((clockstr (substring org-clock-string 0 -1))
  9728. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9729. beg end range props sum-props key value string clocksum)
  9730. (save-excursion
  9731. (when (condition-case nil
  9732. (and (org-mode-p) (org-back-to-heading t))
  9733. (error nil))
  9734. (setq beg (point))
  9735. (setq sum-props (get-text-property (point) 'org-summaries))
  9736. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9737. (outline-next-heading)
  9738. (setq end (point))
  9739. (when (memq which '(all special))
  9740. ;; Get the special properties, like TODO and tags
  9741. (goto-char beg)
  9742. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9743. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9744. (when (looking-at org-priority-regexp)
  9745. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9746. (when (and (setq value (org-get-tags-string))
  9747. (string-match "\\S-" value))
  9748. (push (cons "TAGS" value) props))
  9749. (when (setq value (org-get-tags-at))
  9750. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9751. props))
  9752. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9753. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9754. string (if (equal key clockstr)
  9755. (org-no-properties
  9756. (org-trim
  9757. (buffer-substring
  9758. (match-beginning 3) (goto-char (point-at-eol)))))
  9759. (substring (org-match-string-no-properties 3) 1 -1)))
  9760. (unless key
  9761. (if (= (char-after (match-beginning 3)) ?\[)
  9762. (setq key "TIMESTAMP_IA")
  9763. (setq key "TIMESTAMP")))
  9764. (when (or (equal key clockstr) (not (assoc key props)))
  9765. (push (cons key string) props)))
  9766. )
  9767. (when (memq which '(all standard))
  9768. ;; Get the standard properties, like :PORP: ...
  9769. (setq range (org-get-property-block beg end))
  9770. (when range
  9771. (goto-char (car range))
  9772. (while (re-search-forward
  9773. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9774. (cdr range) t)
  9775. (setq key (org-match-string-no-properties 1)
  9776. value (org-trim (or (org-match-string-no-properties 2) "")))
  9777. (unless (member key excluded)
  9778. (push (cons key (or value "")) props)))))
  9779. (if clocksum
  9780. (push (cons "CLOCKSUM"
  9781. (org-columns-number-to-string (/ (float clocksum) 60.)
  9782. 'add_times))
  9783. props))
  9784. (unless (assoc "CATEGORY" props)
  9785. (setq value (or (org-get-category)
  9786. (progn (org-refresh-category-properties)
  9787. (org-get-category))))
  9788. (push (cons "CATEGORY" value) props))
  9789. (append sum-props (nreverse props)))))))
  9790. (defun org-entry-get (pom property &optional inherit)
  9791. "Get value of PROPERTY for entry at point-or-marker POM.
  9792. If INHERIT is non-nil and the entry does not have the property,
  9793. then also check higher levels of the hierarchy.
  9794. If INHERIT is the symbol `selective', use inheritance only if the setting
  9795. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9796. If the property is present but empty, the return value is the empty string.
  9797. If the property is not present at all, nil is returned."
  9798. (org-with-point-at pom
  9799. (if (and inherit (if (eq inherit 'selective)
  9800. (org-property-inherit-p property)
  9801. t))
  9802. (org-entry-get-with-inheritance property)
  9803. (if (member property org-special-properties)
  9804. ;; We need a special property. Use brute force, get all properties.
  9805. (cdr (assoc property (org-entry-properties nil 'special)))
  9806. (let ((range (org-get-property-block)))
  9807. (if (and range
  9808. (goto-char (car range))
  9809. (re-search-forward
  9810. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9811. (cdr range) t))
  9812. ;; Found the property, return it.
  9813. (if (match-end 1)
  9814. (org-match-string-no-properties 1)
  9815. "")))))))
  9816. (defun org-property-or-variable-value (var &optional inherit)
  9817. "Check if there is a property fixing the value of VAR.
  9818. If yes, return this value. If not, return the current value of the variable."
  9819. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9820. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9821. (read prop)
  9822. (symbol-value var))))
  9823. (defun org-entry-delete (pom property)
  9824. "Delete the property PROPERTY from entry at point-or-marker POM."
  9825. (org-with-point-at pom
  9826. (if (member property org-special-properties)
  9827. nil ; cannot delete these properties.
  9828. (let ((range (org-get-property-block)))
  9829. (if (and range
  9830. (goto-char (car range))
  9831. (re-search-forward
  9832. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9833. (cdr range) t))
  9834. (progn
  9835. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9836. t)
  9837. nil)))))
  9838. ;; Multi-values properties are properties that contain multiple values
  9839. ;; These values are assumed to be single words, separated by whitespace.
  9840. (defun org-entry-add-to-multivalued-property (pom property value)
  9841. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9842. (let* ((old (org-entry-get pom property))
  9843. (values (and old (org-split-string old "[ \t]"))))
  9844. (setq value (org-entry-protect-space value))
  9845. (unless (member value values)
  9846. (setq values (cons value values))
  9847. (org-entry-put pom property
  9848. (mapconcat 'identity values " ")))))
  9849. (defun org-entry-remove-from-multivalued-property (pom property value)
  9850. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9851. (let* ((old (org-entry-get pom property))
  9852. (values (and old (org-split-string old "[ \t]"))))
  9853. (setq value (org-entry-protect-space value))
  9854. (when (member value values)
  9855. (setq values (delete value values))
  9856. (org-entry-put pom property
  9857. (mapconcat 'identity values " ")))))
  9858. (defun org-entry-member-in-multivalued-property (pom property value)
  9859. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9860. (let* ((old (org-entry-get pom property))
  9861. (values (and old (org-split-string old "[ \t]"))))
  9862. (setq value (org-entry-protect-space value))
  9863. (member value values)))
  9864. (defun org-entry-get-multivalued-property (pom property)
  9865. "Return a list of values in a multivalued property."
  9866. (let* ((value (org-entry-get pom property))
  9867. (values (and value (org-split-string value "[ \t]"))))
  9868. (mapcar 'org-entry-restore-space values)))
  9869. (defun org-entry-put-multivalued-property (pom property &rest values)
  9870. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9871. VALUES should be a list of strings. Spaces will be protected."
  9872. (org-entry-put pom property
  9873. (mapconcat 'org-entry-protect-space values " "))
  9874. (let* ((value (org-entry-get pom property))
  9875. (values (and value (org-split-string value "[ \t]"))))
  9876. (mapcar 'org-entry-restore-space values)))
  9877. (defun org-entry-protect-space (s)
  9878. "Protect spaces and newline in string S."
  9879. (while (string-match " " s)
  9880. (setq s (replace-match "%20" t t s)))
  9881. (while (string-match "\n" s)
  9882. (setq s (replace-match "%0A" t t s)))
  9883. s)
  9884. (defun org-entry-restore-space (s)
  9885. "Restore spaces and newline in string S."
  9886. (while (string-match "%20" s)
  9887. (setq s (replace-match " " t t s)))
  9888. (while (string-match "%0A" s)
  9889. (setq s (replace-match "\n" t t s)))
  9890. s)
  9891. (defvar org-entry-property-inherited-from (make-marker)
  9892. "Marker pointing to the entry from where a property was inherited.
  9893. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9894. location of the entry where the inheritance search matched. If there was
  9895. no match, the marker will point nowhere.
  9896. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9897. is set.")
  9898. (defun org-entry-get-with-inheritance (property)
  9899. "Get entry property, and search higher levels if not present."
  9900. (move-marker org-entry-property-inherited-from nil)
  9901. (let (tmp)
  9902. (save-excursion
  9903. (save-restriction
  9904. (widen)
  9905. (catch 'ex
  9906. (while t
  9907. (when (setq tmp (org-entry-get nil property))
  9908. (org-back-to-heading t)
  9909. (move-marker org-entry-property-inherited-from (point))
  9910. (throw 'ex tmp))
  9911. (or (org-up-heading-safe) (throw 'ex nil)))))
  9912. (or tmp
  9913. (cdr (assoc property org-file-properties))
  9914. (cdr (assoc property org-global-properties))
  9915. (cdr (assoc property org-global-properties-fixed))))))
  9916. (defun org-entry-put (pom property value)
  9917. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9918. (org-with-point-at pom
  9919. (org-back-to-heading t)
  9920. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9921. range)
  9922. (cond
  9923. ((equal property "TODO")
  9924. (when (and (stringp value) (string-match "\\S-" value)
  9925. (not (member value org-todo-keywords-1)))
  9926. (error "\"%s\" is not a valid TODO state" value))
  9927. (if (or (not value)
  9928. (not (string-match "\\S-" value)))
  9929. (setq value 'none))
  9930. (org-todo value)
  9931. (org-set-tags nil 'align))
  9932. ((equal property "PRIORITY")
  9933. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9934. (string-to-char value) ?\ ))
  9935. (org-set-tags nil 'align))
  9936. ((equal property "SCHEDULED")
  9937. (if (re-search-forward org-scheduled-time-regexp end t)
  9938. (cond
  9939. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9940. ((eq value 'later) (org-timestamp-change 1 'day))
  9941. (t (call-interactively 'org-schedule)))
  9942. (call-interactively 'org-schedule)))
  9943. ((equal property "DEADLINE")
  9944. (if (re-search-forward org-deadline-time-regexp end t)
  9945. (cond
  9946. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9947. ((eq value 'later) (org-timestamp-change 1 'day))
  9948. (t (call-interactively 'org-deadline)))
  9949. (call-interactively 'org-deadline)))
  9950. ((member property org-special-properties)
  9951. (error "The %s property can not yet be set with `org-entry-put'"
  9952. property))
  9953. (t ; a non-special property
  9954. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9955. (setq range (org-get-property-block beg end 'force))
  9956. (goto-char (car range))
  9957. (if (re-search-forward
  9958. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9959. (progn
  9960. (delete-region (match-beginning 1) (match-end 1))
  9961. (goto-char (match-beginning 1)))
  9962. (goto-char (cdr range))
  9963. (insert "\n")
  9964. (backward-char 1)
  9965. (org-indent-line-function)
  9966. (insert ":" property ":"))
  9967. (and value (insert " " value))
  9968. (org-indent-line-function)))))))
  9969. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9970. "Get all property keys in the current buffer.
  9971. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9972. like tags and TODO state.
  9973. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9974. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9975. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9976. formats in the current buffer."
  9977. (let (rtn range cfmt s p)
  9978. (save-excursion
  9979. (save-restriction
  9980. (widen)
  9981. (goto-char (point-min))
  9982. (while (re-search-forward org-property-start-re nil t)
  9983. (setq range (org-get-property-block))
  9984. (goto-char (car range))
  9985. (while (re-search-forward
  9986. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9987. (cdr range) t)
  9988. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9989. (outline-next-heading))))
  9990. (when include-specials
  9991. (setq rtn (append org-special-properties rtn)))
  9992. (when include-defaults
  9993. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9994. (when include-columns
  9995. (save-excursion
  9996. (save-restriction
  9997. (widen)
  9998. (goto-char (point-min))
  9999. (while (re-search-forward
  10000. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10001. nil t)
  10002. (setq cfmt (match-string 2) s 0)
  10003. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10004. cfmt s)
  10005. (setq s (match-end 0)
  10006. p (match-string 1 cfmt))
  10007. (unless (or (equal p "ITEM")
  10008. (member p org-special-properties))
  10009. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10010. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10011. (defun org-property-values (key)
  10012. "Return a list of all values of property KEY."
  10013. (save-excursion
  10014. (save-restriction
  10015. (widen)
  10016. (goto-char (point-min))
  10017. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10018. values)
  10019. (while (re-search-forward re nil t)
  10020. (add-to-list 'values (org-trim (match-string 1))))
  10021. (delete "" values)))))
  10022. (defun org-insert-property-drawer ()
  10023. "Insert a property drawer into the current entry."
  10024. (interactive)
  10025. (org-back-to-heading t)
  10026. (looking-at outline-regexp)
  10027. (let ((indent (- (match-end 0)(match-beginning 0)))
  10028. (beg (point))
  10029. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10030. end hiddenp)
  10031. (outline-next-heading)
  10032. (setq end (point))
  10033. (goto-char beg)
  10034. (while (re-search-forward re end t))
  10035. (setq hiddenp (org-invisible-p))
  10036. (end-of-line 1)
  10037. (and (equal (char-after) ?\n) (forward-char 1))
  10038. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10039. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10040. ;; just skip this line
  10041. (beginning-of-line 2)
  10042. ;; Drawer start, find the end
  10043. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10044. (beginning-of-line 1)))
  10045. (org-skip-over-state-notes)
  10046. (skip-chars-backward " \t\n\r")
  10047. (if (eq (char-before) ?*) (forward-char 1))
  10048. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10049. (beginning-of-line 0)
  10050. (org-indent-to-column indent)
  10051. (beginning-of-line 2)
  10052. (org-indent-to-column indent)
  10053. (beginning-of-line 0)
  10054. (if hiddenp
  10055. (save-excursion
  10056. (org-back-to-heading t)
  10057. (hide-entry))
  10058. (org-flag-drawer t))))
  10059. (defun org-set-property (property value)
  10060. "In the current entry, set PROPERTY to VALUE.
  10061. When called interactively, this will prompt for a property name, offering
  10062. completion on existing and default properties. And then it will prompt
  10063. for a value, offering completion either on allowed values (via an inherited
  10064. xxx_ALL property) or on existing values in other instances of this property
  10065. in the current file."
  10066. (interactive
  10067. (let* ((completion-ignore-case t)
  10068. (keys (org-buffer-property-keys nil t t))
  10069. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10070. (prop (if (member prop0 keys)
  10071. prop0
  10072. (or (cdr (assoc (downcase prop0)
  10073. (mapcar (lambda (x) (cons (downcase x) x))
  10074. keys)))
  10075. prop0)))
  10076. (cur (org-entry-get nil prop))
  10077. (allowed (org-property-get-allowed-values nil prop 'table))
  10078. (existing (mapcar 'list (org-property-values prop)))
  10079. (val (if allowed
  10080. (org-completing-read "Value: " allowed nil 'req-match)
  10081. (org-completing-read
  10082. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10083. (concat "[" cur "]") "")
  10084. ": ")
  10085. existing nil nil "" nil cur))))
  10086. (list prop (if (equal val "") cur val))))
  10087. (unless (equal (org-entry-get nil property) value)
  10088. (org-entry-put nil property value)))
  10089. (defun org-delete-property (property)
  10090. "In the current entry, delete PROPERTY."
  10091. (interactive
  10092. (let* ((completion-ignore-case t)
  10093. (prop (org-ido-completing-read
  10094. "Property: " (org-entry-properties nil 'standard))))
  10095. (list prop)))
  10096. (message "Property %s %s" property
  10097. (if (org-entry-delete nil property)
  10098. "deleted"
  10099. "was not present in the entry")))
  10100. (defun org-delete-property-globally (property)
  10101. "Remove PROPERTY globally, from all entries."
  10102. (interactive
  10103. (let* ((completion-ignore-case t)
  10104. (prop (org-ido-completing-read
  10105. "Globally remove property: "
  10106. (mapcar 'list (org-buffer-property-keys)))))
  10107. (list prop)))
  10108. (save-excursion
  10109. (save-restriction
  10110. (widen)
  10111. (goto-char (point-min))
  10112. (let ((cnt 0))
  10113. (while (re-search-forward
  10114. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10115. nil t)
  10116. (setq cnt (1+ cnt))
  10117. (replace-match ""))
  10118. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10119. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10120. (defun org-compute-property-at-point ()
  10121. "Compute the property at point.
  10122. This looks for an enclosing column format, extracts the operator and
  10123. then applies it to the property in the column format's scope."
  10124. (interactive)
  10125. (unless (org-at-property-p)
  10126. (error "Not at a property"))
  10127. (let ((prop (org-match-string-no-properties 2)))
  10128. (org-columns-get-format-and-top-level)
  10129. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10130. (error "No operator defined for property %s" prop))
  10131. (org-columns-compute prop)))
  10132. (defun org-property-get-allowed-values (pom property &optional table)
  10133. "Get allowed values for the property PROPERTY.
  10134. When TABLE is non-nil, return an alist that can directly be used for
  10135. completion."
  10136. (let (vals)
  10137. (cond
  10138. ((equal property "TODO")
  10139. (setq vals (org-with-point-at pom
  10140. (append org-todo-keywords-1 '("")))))
  10141. ((equal property "PRIORITY")
  10142. (let ((n org-lowest-priority))
  10143. (while (>= n org-highest-priority)
  10144. (push (char-to-string n) vals)
  10145. (setq n (1- n)))))
  10146. ((member property org-special-properties))
  10147. (t
  10148. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10149. (when (and vals (string-match "\\S-" vals))
  10150. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10151. (setq vals (mapcar (lambda (x)
  10152. (cond ((stringp x) x)
  10153. ((numberp x) (number-to-string x))
  10154. ((symbolp x) (symbol-name x))
  10155. (t "???")))
  10156. vals)))))
  10157. (if table (mapcar 'list vals) vals)))
  10158. (defun org-property-previous-allowed-value (&optional previous)
  10159. "Switch to the next allowed value for this property."
  10160. (interactive)
  10161. (org-property-next-allowed-value t))
  10162. (defun org-property-next-allowed-value (&optional previous)
  10163. "Switch to the next allowed value for this property."
  10164. (interactive)
  10165. (unless (org-at-property-p)
  10166. (error "Not at a property"))
  10167. (let* ((key (match-string 2))
  10168. (value (match-string 3))
  10169. (allowed (or (org-property-get-allowed-values (point) key)
  10170. (and (member value '("[ ]" "[-]" "[X]"))
  10171. '("[ ]" "[X]"))))
  10172. nval)
  10173. (unless allowed
  10174. (error "Allowed values for this property have not been defined"))
  10175. (if previous (setq allowed (reverse allowed)))
  10176. (if (member value allowed)
  10177. (setq nval (car (cdr (member value allowed)))))
  10178. (setq nval (or nval (car allowed)))
  10179. (if (equal nval value)
  10180. (error "Only one allowed value for this property"))
  10181. (org-at-property-p)
  10182. (replace-match (concat " :" key ": " nval) t t)
  10183. (org-indent-line-function)
  10184. (beginning-of-line 1)
  10185. (skip-chars-forward " \t")))
  10186. (defun org-find-entry-with-id (ident)
  10187. "Locate the entry that contains the ID property with exact value IDENT.
  10188. IDENT can be a string, a symbol or a number, this function will search for
  10189. the string representation of it.
  10190. Return the position where this entry starts, or nil if there is no such entry."
  10191. (interactive "sID: ")
  10192. (let ((id (cond
  10193. ((stringp ident) ident)
  10194. ((symbol-name ident) (symbol-name ident))
  10195. ((numberp ident) (number-to-string ident))
  10196. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10197. (case-fold-search nil))
  10198. (save-excursion
  10199. (save-restriction
  10200. (widen)
  10201. (goto-char (point-min))
  10202. (when (re-search-forward
  10203. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10204. nil t)
  10205. (org-back-to-heading)
  10206. (point))))))
  10207. ;;;; Timestamps
  10208. (defvar org-last-changed-timestamp nil)
  10209. (defvar org-last-inserted-timestamp nil
  10210. "The last time stamp inserted with `org-insert-time-stamp'.")
  10211. (defvar org-time-was-given) ; dynamically scoped parameter
  10212. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10213. (defvar org-ts-what) ; dynamically scoped parameter
  10214. (defun org-time-stamp (arg &optional inactive)
  10215. "Prompt for a date/time and insert a time stamp.
  10216. If the user specifies a time like HH:MM, or if this command is called
  10217. with a prefix argument, the time stamp will contain date and time.
  10218. Otherwise, only the date will be included. All parts of a date not
  10219. specified by the user will be filled in from the current date/time.
  10220. So if you press just return without typing anything, the time stamp
  10221. will represent the current date/time. If there is already a timestamp
  10222. at the cursor, it will be modified."
  10223. (interactive "P")
  10224. (let* ((ts nil)
  10225. (default-time
  10226. ;; Default time is either today, or, when entering a range,
  10227. ;; the range start.
  10228. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10229. (save-excursion
  10230. (re-search-backward
  10231. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10232. (- (point) 20) t)))
  10233. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10234. (current-time)))
  10235. (default-input (and ts (org-get-compact-tod ts)))
  10236. org-time-was-given org-end-time-was-given time)
  10237. (cond
  10238. ((and (org-at-timestamp-p t)
  10239. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10240. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10241. (insert "--")
  10242. (setq time (let ((this-command this-command))
  10243. (org-read-date arg 'totime nil nil
  10244. default-time default-input)))
  10245. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10246. ((org-at-timestamp-p t)
  10247. (setq time (let ((this-command this-command))
  10248. (org-read-date arg 'totime nil nil default-time default-input)))
  10249. (when (org-at-timestamp-p t) ; just to get the match data
  10250. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10251. (replace-match "")
  10252. (setq org-last-changed-timestamp
  10253. (org-insert-time-stamp
  10254. time (or org-time-was-given arg)
  10255. inactive nil nil (list org-end-time-was-given))))
  10256. (message "Timestamp updated"))
  10257. (t
  10258. (setq time (let ((this-command this-command))
  10259. (org-read-date arg 'totime nil nil default-time default-input)))
  10260. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10261. nil nil (list org-end-time-was-given))))))
  10262. ;; FIXME: can we use this for something else, like computing time differences?
  10263. (defun org-get-compact-tod (s)
  10264. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10265. (let* ((t1 (match-string 1 s))
  10266. (h1 (string-to-number (match-string 2 s)))
  10267. (m1 (string-to-number (match-string 3 s)))
  10268. (t2 (and (match-end 4) (match-string 5 s)))
  10269. (h2 (and t2 (string-to-number (match-string 6 s))))
  10270. (m2 (and t2 (string-to-number (match-string 7 s))))
  10271. dh dm)
  10272. (if (not t2)
  10273. t1
  10274. (setq dh (- h2 h1) dm (- m2 m1))
  10275. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10276. (concat t1 "+" (number-to-string dh)
  10277. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10278. (defun org-time-stamp-inactive (&optional arg)
  10279. "Insert an inactive time stamp.
  10280. An inactive time stamp is enclosed in square brackets instead of angle
  10281. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10282. does not link to the calendar and cannot be changed with the S-cursor keys.
  10283. So these are more for recording a certain time/date."
  10284. (interactive "P")
  10285. (org-time-stamp arg 'inactive))
  10286. (defvar org-date-ovl (org-make-overlay 1 1))
  10287. (org-overlay-put org-date-ovl 'face 'org-warning)
  10288. (org-detach-overlay org-date-ovl)
  10289. (defvar org-ans1) ; dynamically scoped parameter
  10290. (defvar org-ans2) ; dynamically scoped parameter
  10291. (defvar org-plain-time-of-day-regexp) ; defined below
  10292. (defvar org-overriding-default-time nil) ; dynamically scoped
  10293. (defvar org-read-date-overlay nil)
  10294. (defvar org-dcst nil) ; dynamically scoped
  10295. (defun org-read-date (&optional with-time to-time from-string prompt
  10296. default-time default-input)
  10297. "Read a date, possibly a time, and make things smooth for the user.
  10298. The prompt will suggest to enter an ISO date, but you can also enter anything
  10299. which will at least partially be understood by `parse-time-string'.
  10300. Unrecognized parts of the date will default to the current day, month, year,
  10301. hour and minute. If this command is called to replace a timestamp at point,
  10302. of to enter the second timestamp of a range, the default time is taken from the
  10303. existing stamp. For example,
  10304. 3-2-5 --> 2003-02-05
  10305. feb 15 --> currentyear-02-15
  10306. sep 12 9 --> 2009-09-12
  10307. 12:45 --> today 12:45
  10308. 22 sept 0:34 --> currentyear-09-22 0:34
  10309. 12 --> currentyear-currentmonth-12
  10310. Fri --> nearest Friday (today or later)
  10311. etc.
  10312. Furthermore you can specify a relative date by giving, as the *first* thing
  10313. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10314. change in days weeks, months, years.
  10315. With a single plus or minus, the date is relative to today. With a double
  10316. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10317. +4d --> four days from today
  10318. +4 --> same as above
  10319. +2w --> two weeks from today
  10320. ++5 --> five days from default date
  10321. The function understands only English month and weekday abbreviations,
  10322. but this can be configured with the variables `parse-time-months' and
  10323. `parse-time-weekdays'.
  10324. While prompting, a calendar is popped up - you can also select the
  10325. date with the mouse (button 1). The calendar shows a period of three
  10326. months. To scroll it to other months, use the keys `>' and `<'.
  10327. If you don't like the calendar, turn it off with
  10328. \(setq org-read-date-popup-calendar nil)
  10329. With optional argument TO-TIME, the date will immediately be converted
  10330. to an internal time.
  10331. With an optional argument WITH-TIME, the prompt will suggest to also
  10332. insert a time. Note that when WITH-TIME is not set, you can still
  10333. enter a time, and this function will inform the calling routine about
  10334. this change. The calling routine may then choose to change the format
  10335. used to insert the time stamp into the buffer to include the time.
  10336. With optional argument FROM-STRING, read from this string instead from
  10337. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10338. the time/date that is used for everything that is not specified by the
  10339. user."
  10340. (require 'parse-time)
  10341. (let* ((org-time-stamp-rounding-minutes
  10342. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10343. (org-dcst org-display-custom-times)
  10344. (ct (org-current-time))
  10345. (def (or org-overriding-default-time default-time ct))
  10346. (defdecode (decode-time def))
  10347. (dummy (progn
  10348. (when (< (nth 2 defdecode) org-extend-today-until)
  10349. (setcar (nthcdr 2 defdecode) -1)
  10350. (setcar (nthcdr 1 defdecode) 59)
  10351. (setq def (apply 'encode-time defdecode)
  10352. defdecode (decode-time def)))))
  10353. (calendar-move-hook nil)
  10354. (calendar-view-diary-initially-flag nil)
  10355. (view-diary-entries-initially nil)
  10356. (calendar-view-holidays-initially-flag nil)
  10357. (view-calendar-holidays-initially nil)
  10358. (timestr (format-time-string
  10359. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10360. (prompt (concat (if prompt (concat prompt " ") "")
  10361. (format "Date+time [%s]: " timestr)))
  10362. ans (org-ans0 "") org-ans1 org-ans2 final)
  10363. (cond
  10364. (from-string (setq ans from-string))
  10365. (org-read-date-popup-calendar
  10366. (save-excursion
  10367. (save-window-excursion
  10368. (calendar)
  10369. (calendar-forward-day (- (time-to-days def)
  10370. (calendar-absolute-from-gregorian
  10371. (calendar-current-date))))
  10372. (org-eval-in-calendar nil t)
  10373. (let* ((old-map (current-local-map))
  10374. (map (copy-keymap calendar-mode-map))
  10375. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10376. (org-defkey map (kbd "RET") 'org-calendar-select)
  10377. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10378. 'org-calendar-select-mouse)
  10379. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10380. 'org-calendar-select-mouse)
  10381. (org-defkey minibuffer-local-map [(meta shift left)]
  10382. (lambda () (interactive)
  10383. (org-eval-in-calendar '(calendar-backward-month 1))))
  10384. (org-defkey minibuffer-local-map [(meta shift right)]
  10385. (lambda () (interactive)
  10386. (org-eval-in-calendar '(calendar-forward-month 1))))
  10387. (org-defkey minibuffer-local-map [(meta shift up)]
  10388. (lambda () (interactive)
  10389. (org-eval-in-calendar '(calendar-backward-year 1))))
  10390. (org-defkey minibuffer-local-map [(meta shift down)]
  10391. (lambda () (interactive)
  10392. (org-eval-in-calendar '(calendar-forward-year 1))))
  10393. (org-defkey minibuffer-local-map [(shift up)]
  10394. (lambda () (interactive)
  10395. (org-eval-in-calendar '(calendar-backward-week 1))))
  10396. (org-defkey minibuffer-local-map [(shift down)]
  10397. (lambda () (interactive)
  10398. (org-eval-in-calendar '(calendar-forward-week 1))))
  10399. (org-defkey minibuffer-local-map [(shift left)]
  10400. (lambda () (interactive)
  10401. (org-eval-in-calendar '(calendar-backward-day 1))))
  10402. (org-defkey minibuffer-local-map [(shift right)]
  10403. (lambda () (interactive)
  10404. (org-eval-in-calendar '(calendar-forward-day 1))))
  10405. (org-defkey minibuffer-local-map ">"
  10406. (lambda () (interactive)
  10407. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10408. (org-defkey minibuffer-local-map "<"
  10409. (lambda () (interactive)
  10410. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10411. (unwind-protect
  10412. (progn
  10413. (use-local-map map)
  10414. (add-hook 'post-command-hook 'org-read-date-display)
  10415. (setq org-ans0 (read-string prompt default-input nil nil))
  10416. ;; org-ans0: from prompt
  10417. ;; org-ans1: from mouse click
  10418. ;; org-ans2: from calendar motion
  10419. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10420. (remove-hook 'post-command-hook 'org-read-date-display)
  10421. (use-local-map old-map)
  10422. (when org-read-date-overlay
  10423. (org-delete-overlay org-read-date-overlay)
  10424. (setq org-read-date-overlay nil)))))))
  10425. (t ; Naked prompt only
  10426. (unwind-protect
  10427. (setq ans (read-string prompt default-input nil timestr))
  10428. (when org-read-date-overlay
  10429. (org-delete-overlay org-read-date-overlay)
  10430. (setq org-read-date-overlay nil)))))
  10431. (setq final (org-read-date-analyze ans def defdecode))
  10432. (if to-time
  10433. (apply 'encode-time final)
  10434. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10435. (format "%04d-%02d-%02d %02d:%02d"
  10436. (nth 5 final) (nth 4 final) (nth 3 final)
  10437. (nth 2 final) (nth 1 final))
  10438. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10439. (defvar def)
  10440. (defvar defdecode)
  10441. (defvar with-time)
  10442. (defun org-read-date-display ()
  10443. "Display the current date prompt interpretation in the minibuffer."
  10444. (when org-read-date-display-live
  10445. (when org-read-date-overlay
  10446. (org-delete-overlay org-read-date-overlay))
  10447. (let ((p (point)))
  10448. (end-of-line 1)
  10449. (while (not (equal (buffer-substring
  10450. (max (point-min) (- (point) 4)) (point))
  10451. " "))
  10452. (insert " "))
  10453. (goto-char p))
  10454. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10455. " " (or org-ans1 org-ans2)))
  10456. (org-end-time-was-given nil)
  10457. (f (org-read-date-analyze ans def defdecode))
  10458. (fmts (if org-dcst
  10459. org-time-stamp-custom-formats
  10460. org-time-stamp-formats))
  10461. (fmt (if (or with-time
  10462. (and (boundp 'org-time-was-given) org-time-was-given))
  10463. (cdr fmts)
  10464. (car fmts)))
  10465. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10466. (when (and org-end-time-was-given
  10467. (string-match org-plain-time-of-day-regexp txt))
  10468. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10469. org-end-time-was-given
  10470. (substring txt (match-end 0)))))
  10471. (setq org-read-date-overlay
  10472. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10473. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10474. (defun org-read-date-analyze (ans def defdecode)
  10475. "Analyse the combined answer of the date prompt."
  10476. ;; FIXME: cleanup and comment
  10477. (let (delta deltan deltaw deltadef year month day
  10478. hour minute second wday pm h2 m2 tl wday1
  10479. iso-year iso-weekday iso-week iso-year iso-date)
  10480. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10481. (setq ans "+0"))
  10482. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10483. (setq ans (replace-match "" t t ans)
  10484. deltan (car delta)
  10485. deltaw (nth 1 delta)
  10486. deltadef (nth 2 delta)))
  10487. ;; Check if there is an iso week date in there
  10488. ;; If yes, sore the info and postpone interpreting it until the rest
  10489. ;; of the parsing is done
  10490. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10491. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10492. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10493. iso-week (string-to-number (match-string 2 ans)))
  10494. (setq ans (replace-match "" t t ans)))
  10495. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10496. (when (string-match
  10497. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10498. (setq year (if (match-end 2)
  10499. (string-to-number (match-string 2 ans))
  10500. (string-to-number (format-time-string "%Y")))
  10501. month (string-to-number (match-string 3 ans))
  10502. day (string-to-number (match-string 4 ans)))
  10503. (if (< year 100) (setq year (+ 2000 year)))
  10504. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10505. t nil ans)))
  10506. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10507. ;; If there is a time with am/pm, and *no* time without it, we convert
  10508. ;; so that matching will be successful.
  10509. (loop for i from 1 to 2 do ; twice, for end time as well
  10510. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10511. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10512. (setq hour (string-to-number (match-string 1 ans))
  10513. minute (if (match-end 3)
  10514. (string-to-number (match-string 3 ans))
  10515. 0)
  10516. pm (equal ?p
  10517. (string-to-char (downcase (match-string 4 ans)))))
  10518. (if (and (= hour 12) (not pm))
  10519. (setq hour 0)
  10520. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10521. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10522. t t ans))))
  10523. ;; Check if a time range is given as a duration
  10524. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10525. (setq hour (string-to-number (match-string 1 ans))
  10526. h2 (+ hour (string-to-number (match-string 3 ans)))
  10527. minute (string-to-number (match-string 2 ans))
  10528. m2 (+ minute (if (match-end 5) (string-to-number
  10529. (match-string 5 ans))0)))
  10530. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10531. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10532. t t ans)))
  10533. ;; Check if there is a time range
  10534. (when (boundp 'org-end-time-was-given)
  10535. (setq org-time-was-given nil)
  10536. (when (and (string-match org-plain-time-of-day-regexp ans)
  10537. (match-end 8))
  10538. (setq org-end-time-was-given (match-string 8 ans))
  10539. (setq ans (concat (substring ans 0 (match-beginning 7))
  10540. (substring ans (match-end 7))))))
  10541. (setq tl (parse-time-string ans)
  10542. day (or (nth 3 tl) (nth 3 defdecode))
  10543. month (or (nth 4 tl)
  10544. (if (and org-read-date-prefer-future
  10545. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10546. (1+ (nth 4 defdecode))
  10547. (nth 4 defdecode)))
  10548. year (or (nth 5 tl)
  10549. (if (and org-read-date-prefer-future
  10550. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10551. (1+ (nth 5 defdecode))
  10552. (nth 5 defdecode)))
  10553. hour (or (nth 2 tl) (nth 2 defdecode))
  10554. minute (or (nth 1 tl) (nth 1 defdecode))
  10555. second (or (nth 0 tl) 0)
  10556. wday (nth 6 tl))
  10557. ;; Special date definitions below
  10558. (cond
  10559. (iso-week
  10560. ;; There was an iso week
  10561. (setq year (or iso-year year)
  10562. day (or iso-weekday wday 1)
  10563. wday nil ; to make sure that the trigger below does not match
  10564. iso-date (calendar-gregorian-from-absolute
  10565. (calendar-absolute-from-iso
  10566. (list iso-week day year))))
  10567. ; FIXME: Should we also push ISO weeks into the future?
  10568. ; (when (and org-read-date-prefer-future
  10569. ; (not iso-year)
  10570. ; (< (calendar-absolute-from-gregorian iso-date)
  10571. ; (time-to-days (current-time))))
  10572. ; (setq year (1+ year)
  10573. ; iso-date (calendar-gregorian-from-absolute
  10574. ; (calendar-absolute-from-iso
  10575. ; (list iso-week day year)))))
  10576. (setq month (car iso-date)
  10577. year (nth 2 iso-date)
  10578. day (nth 1 iso-date)))
  10579. (deltan
  10580. (unless deltadef
  10581. (let ((now (decode-time (current-time))))
  10582. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10583. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10584. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10585. ((equal deltaw "m") (setq month (+ month deltan)))
  10586. ((equal deltaw "y") (setq year (+ year deltan)))))
  10587. ((and wday (not (nth 3 tl)))
  10588. ;; Weekday was given, but no day, so pick that day in the week
  10589. ;; on or after the derived date.
  10590. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10591. (unless (equal wday wday1)
  10592. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10593. (if (and (boundp 'org-time-was-given)
  10594. (nth 2 tl))
  10595. (setq org-time-was-given t))
  10596. (if (< year 100) (setq year (+ 2000 year)))
  10597. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10598. (list second minute hour day month year)))
  10599. (defvar parse-time-weekdays)
  10600. (defun org-read-date-get-relative (s today default)
  10601. "Check string S for special relative date string.
  10602. TODAY and DEFAULT are internal times, for today and for a default.
  10603. Return shift list (N what def-flag)
  10604. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10605. N is the number of WHATs to shift.
  10606. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10607. the DEFAULT date rather than TODAY."
  10608. (when (and
  10609. (string-match
  10610. (concat
  10611. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10612. "\\([0-9]+\\)?"
  10613. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10614. "\\([ \t]\\|$\\)") s)
  10615. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10616. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10617. (string-to-char (substring (match-string 1 s) -1))
  10618. ?+))
  10619. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10620. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10621. (what (if (match-end 3) (match-string 3 s) "d"))
  10622. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10623. (date (if rel default today))
  10624. (wday (nth 6 (decode-time date)))
  10625. delta)
  10626. (if wday1
  10627. (progn
  10628. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10629. (if (= dir ?-) (setq delta (- delta 7)))
  10630. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10631. (list delta "d" rel))
  10632. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10633. (defun org-eval-in-calendar (form &optional keepdate)
  10634. "Eval FORM in the calendar window and return to current window.
  10635. Also, store the cursor date in variable org-ans2."
  10636. (let ((sw (selected-window)))
  10637. (select-window (get-buffer-window "*Calendar*"))
  10638. (eval form)
  10639. (when (and (not keepdate) (calendar-cursor-to-date))
  10640. (let* ((date (calendar-cursor-to-date))
  10641. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10642. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10643. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10644. (select-window sw)))
  10645. (defun org-calendar-select ()
  10646. "Return to `org-read-date' with the date currently selected.
  10647. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10648. (interactive)
  10649. (when (calendar-cursor-to-date)
  10650. (let* ((date (calendar-cursor-to-date))
  10651. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10652. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10653. (if (active-minibuffer-window) (exit-minibuffer))))
  10654. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10655. "Insert a date stamp for the date given by the internal TIME.
  10656. WITH-HM means, use the stamp format that includes the time of the day.
  10657. INACTIVE means use square brackets instead of angular ones, so that the
  10658. stamp will not contribute to the agenda.
  10659. PRE and POST are optional strings to be inserted before and after the
  10660. stamp.
  10661. The command returns the inserted time stamp."
  10662. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10663. stamp)
  10664. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10665. (insert-before-markers (or pre ""))
  10666. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10667. (when (listp extra)
  10668. (setq extra (car extra))
  10669. (if (and (stringp extra)
  10670. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10671. (setq extra (format "-%02d:%02d"
  10672. (string-to-number (match-string 1 extra))
  10673. (string-to-number (match-string 2 extra))))
  10674. (setq extra nil)))
  10675. (when extra
  10676. (backward-char 1)
  10677. (insert-before-markers extra)
  10678. (forward-char 1))
  10679. (insert-before-markers (or post ""))
  10680. (setq org-last-inserted-timestamp stamp)))
  10681. (defun org-toggle-time-stamp-overlays ()
  10682. "Toggle the use of custom time stamp formats."
  10683. (interactive)
  10684. (setq org-display-custom-times (not org-display-custom-times))
  10685. (unless org-display-custom-times
  10686. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10687. (while (setq p (next-single-property-change p 'display))
  10688. (if (and (get-text-property p 'display)
  10689. (eq (get-text-property p 'face) 'org-date))
  10690. (remove-text-properties
  10691. p (setq p (next-single-property-change p 'display))
  10692. '(display t))))
  10693. (set-buffer-modified-p bmp)))
  10694. (if (featurep 'xemacs)
  10695. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10696. (org-restart-font-lock)
  10697. (setq org-table-may-need-update t)
  10698. (if org-display-custom-times
  10699. (message "Time stamps are overlayed with custom format")
  10700. (message "Time stamp overlays removed")))
  10701. (defun org-display-custom-time (beg end)
  10702. "Overlay modified time stamp format over timestamp between BEG and END."
  10703. (let* ((ts (buffer-substring beg end))
  10704. t1 w1 with-hm tf time str w2 (off 0))
  10705. (save-match-data
  10706. (setq t1 (org-parse-time-string ts t))
  10707. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10708. (setq off (- (match-end 0) (match-beginning 0)))))
  10709. (setq end (- end off))
  10710. (setq w1 (- end beg)
  10711. with-hm (and (nth 1 t1) (nth 2 t1))
  10712. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10713. time (org-fix-decoded-time t1)
  10714. str (org-add-props
  10715. (format-time-string
  10716. (substring tf 1 -1) (apply 'encode-time time))
  10717. nil 'mouse-face 'highlight)
  10718. w2 (length str))
  10719. (if (not (= w2 w1))
  10720. (add-text-properties (1+ beg) (+ 2 beg)
  10721. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10722. (if (featurep 'xemacs)
  10723. (progn
  10724. (put-text-property beg end 'invisible t)
  10725. (put-text-property beg end 'end-glyph (make-glyph str)))
  10726. (put-text-property beg end 'display str))))
  10727. (defun org-translate-time (string)
  10728. "Translate all timestamps in STRING to custom format.
  10729. But do this only if the variable `org-display-custom-times' is set."
  10730. (when org-display-custom-times
  10731. (save-match-data
  10732. (let* ((start 0)
  10733. (re org-ts-regexp-both)
  10734. t1 with-hm inactive tf time str beg end)
  10735. (while (setq start (string-match re string start))
  10736. (setq beg (match-beginning 0)
  10737. end (match-end 0)
  10738. t1 (save-match-data
  10739. (org-parse-time-string (substring string beg end) t))
  10740. with-hm (and (nth 1 t1) (nth 2 t1))
  10741. inactive (equal (substring string beg (1+ beg)) "[")
  10742. tf (funcall (if with-hm 'cdr 'car)
  10743. org-time-stamp-custom-formats)
  10744. time (org-fix-decoded-time t1)
  10745. str (format-time-string
  10746. (concat
  10747. (if inactive "[" "<") (substring tf 1 -1)
  10748. (if inactive "]" ">"))
  10749. (apply 'encode-time time))
  10750. string (replace-match str t t string)
  10751. start (+ start (length str)))))))
  10752. string)
  10753. (defun org-fix-decoded-time (time)
  10754. "Set 0 instead of nil for the first 6 elements of time.
  10755. Don't touch the rest."
  10756. (let ((n 0))
  10757. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10758. (defun org-days-to-time (timestamp-string)
  10759. "Difference between TIMESTAMP-STRING and now in days."
  10760. (- (time-to-days (org-time-string-to-time timestamp-string))
  10761. (time-to-days (current-time))))
  10762. (defun org-deadline-close (timestamp-string &optional ndays)
  10763. "Is the time in TIMESTAMP-STRING close to the current date?"
  10764. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10765. (and (< (org-days-to-time timestamp-string) ndays)
  10766. (not (org-entry-is-done-p))))
  10767. (defun org-get-wdays (ts)
  10768. "Get the deadline lead time appropriate for timestring TS."
  10769. (cond
  10770. ((<= org-deadline-warning-days 0)
  10771. ;; 0 or negative, enforce this value no matter what
  10772. (- org-deadline-warning-days))
  10773. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10774. ;; lead time is specified.
  10775. (floor (* (string-to-number (match-string 1 ts))
  10776. (cdr (assoc (match-string 2 ts)
  10777. '(("d" . 1) ("w" . 7)
  10778. ("m" . 30.4) ("y" . 365.25)))))))
  10779. ;; go for the default.
  10780. (t org-deadline-warning-days)))
  10781. (defun org-calendar-select-mouse (ev)
  10782. "Return to `org-read-date' with the date currently selected.
  10783. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10784. (interactive "e")
  10785. (mouse-set-point ev)
  10786. (when (calendar-cursor-to-date)
  10787. (let* ((date (calendar-cursor-to-date))
  10788. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10789. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10790. (if (active-minibuffer-window) (exit-minibuffer))))
  10791. (defun org-check-deadlines (ndays)
  10792. "Check if there are any deadlines due or past due.
  10793. A deadline is considered due if it happens within `org-deadline-warning-days'
  10794. days from today's date. If the deadline appears in an entry marked DONE,
  10795. it is not shown. The prefix arg NDAYS can be used to test that many
  10796. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10797. (interactive "P")
  10798. (let* ((org-warn-days
  10799. (cond
  10800. ((equal ndays '(4)) 100000)
  10801. (ndays (prefix-numeric-value ndays))
  10802. (t (abs org-deadline-warning-days))))
  10803. (case-fold-search nil)
  10804. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10805. (callback
  10806. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10807. (message "%d deadlines past-due or due within %d days"
  10808. (org-occur regexp nil callback)
  10809. org-warn-days)))
  10810. (defun org-check-before-date (date)
  10811. "Check if there are deadlines or scheduled entries before DATE."
  10812. (interactive (list (org-read-date)))
  10813. (let ((case-fold-search nil)
  10814. (regexp (concat "\\<\\(" org-deadline-string
  10815. "\\|" org-scheduled-string
  10816. "\\) *<\\([^>]+\\)>"))
  10817. (callback
  10818. (lambda () (time-less-p
  10819. (org-time-string-to-time (match-string 2))
  10820. (org-time-string-to-time date)))))
  10821. (message "%d entries before %s"
  10822. (org-occur regexp nil callback) date)))
  10823. (defun org-evaluate-time-range (&optional to-buffer)
  10824. "Evaluate a time range by computing the difference between start and end.
  10825. Normally the result is just printed in the echo area, but with prefix arg
  10826. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10827. If the time range is actually in a table, the result is inserted into the
  10828. next column.
  10829. For time difference computation, a year is assumed to be exactly 365
  10830. days in order to avoid rounding problems."
  10831. (interactive "P")
  10832. (or
  10833. (org-clock-update-time-maybe)
  10834. (save-excursion
  10835. (unless (org-at-date-range-p t)
  10836. (goto-char (point-at-bol))
  10837. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10838. (if (not (org-at-date-range-p t))
  10839. (error "Not at a time-stamp range, and none found in current line")))
  10840. (let* ((ts1 (match-string 1))
  10841. (ts2 (match-string 2))
  10842. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10843. (match-end (match-end 0))
  10844. (time1 (org-time-string-to-time ts1))
  10845. (time2 (org-time-string-to-time ts2))
  10846. (t1 (time-to-seconds time1))
  10847. (t2 (time-to-seconds time2))
  10848. (diff (abs (- t2 t1)))
  10849. (negative (< (- t2 t1) 0))
  10850. ;; (ys (floor (* 365 24 60 60)))
  10851. (ds (* 24 60 60))
  10852. (hs (* 60 60))
  10853. (fy "%dy %dd %02d:%02d")
  10854. (fy1 "%dy %dd")
  10855. (fd "%dd %02d:%02d")
  10856. (fd1 "%dd")
  10857. (fh "%02d:%02d")
  10858. y d h m align)
  10859. (if havetime
  10860. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10861. y 0
  10862. d (floor (/ diff ds)) diff (mod diff ds)
  10863. h (floor (/ diff hs)) diff (mod diff hs)
  10864. m (floor (/ diff 60)))
  10865. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10866. y 0
  10867. d (floor (+ (/ diff ds) 0.5))
  10868. h 0 m 0))
  10869. (if (not to-buffer)
  10870. (message "%s" (org-make-tdiff-string y d h m))
  10871. (if (org-at-table-p)
  10872. (progn
  10873. (goto-char match-end)
  10874. (setq align t)
  10875. (and (looking-at " *|") (goto-char (match-end 0))))
  10876. (goto-char match-end))
  10877. (if (looking-at
  10878. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10879. (replace-match ""))
  10880. (if negative (insert " -"))
  10881. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10882. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10883. (insert " " (format fh h m))))
  10884. (if align (org-table-align))
  10885. (message "Time difference inserted")))))
  10886. (defun org-make-tdiff-string (y d h m)
  10887. (let ((fmt "")
  10888. (l nil))
  10889. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10890. l (push y l)))
  10891. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10892. l (push d l)))
  10893. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10894. l (push h l)))
  10895. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10896. l (push m l)))
  10897. (apply 'format fmt (nreverse l))))
  10898. (defun org-time-string-to-time (s)
  10899. (apply 'encode-time (org-parse-time-string s)))
  10900. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10901. "Convert a time stamp to an absolute day number.
  10902. If there is a specifyer for a cyclic time stamp, get the closest date to
  10903. DAYNR.
  10904. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10905. (cond
  10906. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10907. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10908. daynr
  10909. (+ daynr 1000)))
  10910. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10911. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10912. (time-to-days (current-time))) (match-string 0 s)
  10913. prefer show-all))
  10914. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10915. (defun org-days-to-iso-week (days)
  10916. "Return the iso week number."
  10917. (require 'cal-iso)
  10918. (car (calendar-iso-from-absolute days)))
  10919. (defun org-small-year-to-year (year)
  10920. "Convert 2-digit years into 4-digit years.
  10921. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10922. The year 2000 cannot be abbreviated. Any year larger than 99
  10923. is returned unchanged."
  10924. (if (< year 38)
  10925. (setq year (+ 2000 year))
  10926. (if (< year 100)
  10927. (setq year (+ 1900 year))))
  10928. year)
  10929. (defun org-time-from-absolute (d)
  10930. "Return the time corresponding to date D.
  10931. D may be an absolute day number, or a calendar-type list (month day year)."
  10932. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10933. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10934. (defun org-calendar-holiday ()
  10935. "List of holidays, for Diary display in Org-mode."
  10936. (require 'holidays)
  10937. (let ((hl (funcall
  10938. (if (fboundp 'calendar-check-holidays)
  10939. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10940. (if hl (mapconcat 'identity hl "; "))))
  10941. (defun org-diary-sexp-entry (sexp entry date)
  10942. "Process a SEXP diary ENTRY for DATE."
  10943. (require 'diary-lib)
  10944. (let ((result (if calendar-debug-sexp
  10945. (let ((stack-trace-on-error t))
  10946. (eval (car (read-from-string sexp))))
  10947. (condition-case nil
  10948. (eval (car (read-from-string sexp)))
  10949. (error
  10950. (beep)
  10951. (message "Bad sexp at line %d in %s: %s"
  10952. (org-current-line)
  10953. (buffer-file-name) sexp)
  10954. (sleep-for 2))))))
  10955. (cond ((stringp result) result)
  10956. ((and (consp result)
  10957. (stringp (cdr result))) (cdr result))
  10958. (result entry)
  10959. (t nil))))
  10960. (defun org-diary-to-ical-string (frombuf)
  10961. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10962. This uses the icalendar.el library."
  10963. (let* ((tmpdir (if (featurep 'xemacs)
  10964. (temp-directory)
  10965. temporary-file-directory))
  10966. (tmpfile (make-temp-name
  10967. (expand-file-name "orgics" tmpdir)))
  10968. buf rtn b e)
  10969. (save-excursion
  10970. (set-buffer frombuf)
  10971. (icalendar-export-region (point-min) (point-max) tmpfile)
  10972. (setq buf (find-buffer-visiting tmpfile))
  10973. (set-buffer buf)
  10974. (goto-char (point-min))
  10975. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10976. (setq b (match-beginning 0)))
  10977. (goto-char (point-max))
  10978. (if (re-search-backward "^END:VEVENT" nil t)
  10979. (setq e (match-end 0)))
  10980. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10981. (kill-buffer buf)
  10982. (delete-file tmpfile)
  10983. rtn))
  10984. (defun org-closest-date (start current change prefer show-all)
  10985. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10986. When PREFER is `past' return a date that is either CURRENT or past.
  10987. When PREFER is `future', return a date that is either CURRENT or future.
  10988. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10989. ;; Make the proper lists from the dates
  10990. (catch 'exit
  10991. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10992. dn dw sday cday n1 n2 n0
  10993. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10994. (setq start (org-date-to-gregorian start)
  10995. current (org-date-to-gregorian
  10996. (if show-all
  10997. current
  10998. (time-to-days (current-time))))
  10999. sday (calendar-absolute-from-gregorian start)
  11000. cday (calendar-absolute-from-gregorian current))
  11001. (if (<= cday sday) (throw 'exit sday))
  11002. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11003. (setq dn (string-to-number (match-string 1 change))
  11004. dw (cdr (assoc (match-string 2 change) a1)))
  11005. (error "Invalid change specifyer: %s" change))
  11006. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11007. (cond
  11008. ((eq dw 'day)
  11009. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11010. n2 (+ n1 dn)))
  11011. ((eq dw 'year)
  11012. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11013. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11014. (setq date1 (list m d y1)
  11015. n1 (calendar-absolute-from-gregorian date1)
  11016. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11017. n2 (calendar-absolute-from-gregorian date2)))
  11018. ((eq dw 'month)
  11019. ;; approx number of month between the two dates
  11020. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11021. ;; How often does dn fit in there?
  11022. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11023. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11024. m (+ m nm)
  11025. ny (floor (/ m 12))
  11026. y (+ y ny)
  11027. m (- m (* ny 12)))
  11028. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11029. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11030. (setq m2 (+ m dn) y2 y)
  11031. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11032. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11033. (while (<= n2 cday)
  11034. (setq n1 n2 m m2 y y2)
  11035. (setq m2 (+ m dn) y2 y)
  11036. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11037. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11038. ;; Make sure n1 is the earlier date
  11039. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11040. (if show-all
  11041. (cond
  11042. ((eq prefer 'past) n1)
  11043. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11044. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11045. (cond
  11046. ((eq prefer 'past) n1)
  11047. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11048. (t (if (= cday n1) n1 n2)))))))
  11049. (defun org-date-to-gregorian (date)
  11050. "Turn any specification of DATE into a gregorian date for the calendar."
  11051. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11052. ((and (listp date) (= (length date) 3)) date)
  11053. ((stringp date)
  11054. (setq date (org-parse-time-string date))
  11055. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11056. ((listp date)
  11057. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11058. (defun org-parse-time-string (s &optional nodefault)
  11059. "Parse the standard Org-mode time string.
  11060. This should be a lot faster than the normal `parse-time-string'.
  11061. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11062. hour and minute fields will be nil if not given."
  11063. (if (string-match org-ts-regexp0 s)
  11064. (list 0
  11065. (if (or (match-beginning 8) (not nodefault))
  11066. (string-to-number (or (match-string 8 s) "0")))
  11067. (if (or (match-beginning 7) (not nodefault))
  11068. (string-to-number (or (match-string 7 s) "0")))
  11069. (string-to-number (match-string 4 s))
  11070. (string-to-number (match-string 3 s))
  11071. (string-to-number (match-string 2 s))
  11072. nil nil nil)
  11073. (make-list 9 0)))
  11074. (defun org-timestamp-up (&optional arg)
  11075. "Increase the date item at the cursor by one.
  11076. If the cursor is on the year, change the year. If it is on the month or
  11077. the day, change that.
  11078. With prefix ARG, change by that many units."
  11079. (interactive "p")
  11080. (org-timestamp-change (prefix-numeric-value arg)))
  11081. (defun org-timestamp-down (&optional arg)
  11082. "Decrease the date item at the cursor by one.
  11083. If the cursor is on the year, change the year. If it is on the month or
  11084. the day, change that.
  11085. With prefix ARG, change by that many units."
  11086. (interactive "p")
  11087. (org-timestamp-change (- (prefix-numeric-value arg))))
  11088. (defun org-timestamp-up-day (&optional arg)
  11089. "Increase the date in the time stamp by one day.
  11090. With prefix ARG, change that many days."
  11091. (interactive "p")
  11092. (if (and (not (org-at-timestamp-p t))
  11093. (org-on-heading-p))
  11094. (org-todo 'up)
  11095. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11096. (defun org-timestamp-down-day (&optional arg)
  11097. "Decrease the date in the time stamp by one day.
  11098. With prefix ARG, change that many days."
  11099. (interactive "p")
  11100. (if (and (not (org-at-timestamp-p t))
  11101. (org-on-heading-p))
  11102. (org-todo 'down)
  11103. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11104. (defun org-at-timestamp-p (&optional inactive-ok)
  11105. "Determine if the cursor is in or at a timestamp."
  11106. (interactive)
  11107. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11108. (pos (point))
  11109. (ans (or (looking-at tsr)
  11110. (save-excursion
  11111. (skip-chars-backward "^[<\n\r\t")
  11112. (if (> (point) (point-min)) (backward-char 1))
  11113. (and (looking-at tsr)
  11114. (> (- (match-end 0) pos) -1))))))
  11115. (and ans
  11116. (boundp 'org-ts-what)
  11117. (setq org-ts-what
  11118. (cond
  11119. ((= pos (match-beginning 0)) 'bracket)
  11120. ((= pos (1- (match-end 0))) 'bracket)
  11121. ((org-pos-in-match-range pos 2) 'year)
  11122. ((org-pos-in-match-range pos 3) 'month)
  11123. ((org-pos-in-match-range pos 7) 'hour)
  11124. ((org-pos-in-match-range pos 8) 'minute)
  11125. ((or (org-pos-in-match-range pos 4)
  11126. (org-pos-in-match-range pos 5)) 'day)
  11127. ((and (> pos (or (match-end 8) (match-end 5)))
  11128. (< pos (match-end 0)))
  11129. (- pos (or (match-end 8) (match-end 5))))
  11130. (t 'day))))
  11131. ans))
  11132. (defun org-toggle-timestamp-type ()
  11133. "Toggle the type (<active> or [inactive]) of a time stamp."
  11134. (interactive)
  11135. (when (org-at-timestamp-p t)
  11136. (let ((beg (match-beginning 0)) (end (match-end 0))
  11137. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11138. (save-excursion
  11139. (goto-char beg)
  11140. (while (re-search-forward "[][<>]" end t)
  11141. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11142. t t)))
  11143. (message "Timestamp is now %sactive"
  11144. (if (equal (char-after beg) ?<) "" "in")))))
  11145. (defun org-timestamp-change (n &optional what)
  11146. "Change the date in the time stamp at point.
  11147. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11148. `year', `minute', `second'. If WHAT is not given, the cursor position
  11149. in the timestamp determines what will be changed."
  11150. (let ((pos (point))
  11151. with-hm inactive
  11152. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11153. org-ts-what
  11154. extra rem
  11155. ts time time0)
  11156. (if (not (org-at-timestamp-p t))
  11157. (error "Not at a timestamp"))
  11158. (if (and (not what) (eq org-ts-what 'bracket))
  11159. (org-toggle-timestamp-type)
  11160. (if (and (not what) (not (eq org-ts-what 'day))
  11161. org-display-custom-times
  11162. (get-text-property (point) 'display)
  11163. (not (get-text-property (1- (point)) 'display)))
  11164. (setq org-ts-what 'day))
  11165. (setq org-ts-what (or what org-ts-what)
  11166. inactive (= (char-after (match-beginning 0)) ?\[)
  11167. ts (match-string 0))
  11168. (replace-match "")
  11169. (if (string-match
  11170. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11171. ts)
  11172. (setq extra (match-string 1 ts)))
  11173. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11174. (setq with-hm t))
  11175. (setq time0 (org-parse-time-string ts))
  11176. (when (and (eq org-ts-what 'minute)
  11177. (eq current-prefix-arg nil))
  11178. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11179. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11180. (setcar (cdr time0) (+ (nth 1 time0)
  11181. (if (> n 0) (- rem) (- dm rem))))))
  11182. (setq time
  11183. (encode-time (or (car time0) 0)
  11184. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11185. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11186. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11187. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11188. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11189. (nthcdr 6 time0)))
  11190. (when (integerp org-ts-what)
  11191. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11192. (if (eq what 'calendar)
  11193. (let ((cal-date (org-get-date-from-calendar)))
  11194. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11195. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11196. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11197. (setcar time0 (or (car time0) 0))
  11198. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11199. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11200. (setq time (apply 'encode-time time0))))
  11201. (setq org-last-changed-timestamp
  11202. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11203. (org-clock-update-time-maybe)
  11204. (goto-char pos)
  11205. ;; Try to recenter the calendar window, if any
  11206. (if (and org-calendar-follow-timestamp-change
  11207. (get-buffer-window "*Calendar*" t)
  11208. (memq org-ts-what '(day month year)))
  11209. (org-recenter-calendar (time-to-days time))))))
  11210. (defun org-modify-ts-extra (s pos n dm)
  11211. "Change the different parts of the lead-time and repeat fields in timestamp."
  11212. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11213. ng h m new rem)
  11214. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11215. (cond
  11216. ((or (org-pos-in-match-range pos 2)
  11217. (org-pos-in-match-range pos 3))
  11218. (setq m (string-to-number (match-string 3 s))
  11219. h (string-to-number (match-string 2 s)))
  11220. (if (org-pos-in-match-range pos 2)
  11221. (setq h (+ h n))
  11222. (setq n (* dm (org-no-warnings (signum n))))
  11223. (when (not (= 0 (setq rem (% m dm))))
  11224. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11225. (setq m (+ m n)))
  11226. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11227. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11228. (setq h (min 24 (max 0 h)))
  11229. (setq ng 1 new (format "-%02d:%02d" h m)))
  11230. ((org-pos-in-match-range pos 6)
  11231. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11232. ((org-pos-in-match-range pos 5)
  11233. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11234. ((org-pos-in-match-range pos 9)
  11235. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11236. ((org-pos-in-match-range pos 8)
  11237. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11238. (when ng
  11239. (setq s (concat
  11240. (substring s 0 (match-beginning ng))
  11241. new
  11242. (substring s (match-end ng))))))
  11243. s))
  11244. (defun org-recenter-calendar (date)
  11245. "If the calendar is visible, recenter it to DATE."
  11246. (let* ((win (selected-window))
  11247. (cwin (get-buffer-window "*Calendar*" t))
  11248. (calendar-move-hook nil))
  11249. (when cwin
  11250. (select-window cwin)
  11251. (calendar-goto-date (if (listp date) date
  11252. (calendar-gregorian-from-absolute date)))
  11253. (select-window win))))
  11254. (defun org-goto-calendar (&optional arg)
  11255. "Go to the Emacs calendar at the current date.
  11256. If there is a time stamp in the current line, go to that date.
  11257. A prefix ARG can be used to force the current date."
  11258. (interactive "P")
  11259. (let ((tsr org-ts-regexp) diff
  11260. (calendar-move-hook nil)
  11261. (calendar-view-holidays-initially-flag nil)
  11262. (view-calendar-holidays-initially nil)
  11263. (calendar-view-diary-initially-flag nil)
  11264. (view-diary-entries-initially nil))
  11265. (if (or (org-at-timestamp-p)
  11266. (save-excursion
  11267. (beginning-of-line 1)
  11268. (looking-at (concat ".*" tsr))))
  11269. (let ((d1 (time-to-days (current-time)))
  11270. (d2 (time-to-days
  11271. (org-time-string-to-time (match-string 1)))))
  11272. (setq diff (- d2 d1))))
  11273. (calendar)
  11274. (calendar-goto-today)
  11275. (if (and diff (not arg)) (calendar-forward-day diff))))
  11276. (defun org-get-date-from-calendar ()
  11277. "Return a list (month day year) of date at point in calendar."
  11278. (with-current-buffer "*Calendar*"
  11279. (save-match-data
  11280. (calendar-cursor-to-date))))
  11281. (defun org-date-from-calendar ()
  11282. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11283. If there is already a time stamp at the cursor position, update it."
  11284. (interactive)
  11285. (if (org-at-timestamp-p t)
  11286. (org-timestamp-change 0 'calendar)
  11287. (let ((cal-date (org-get-date-from-calendar)))
  11288. (org-insert-time-stamp
  11289. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11290. (defun org-minutes-to-hh:mm-string (m)
  11291. "Compute H:MM from a number of minutes."
  11292. (let ((h (/ m 60)))
  11293. (setq m (- m (* 60 h)))
  11294. (format org-time-clocksum-format h m)))
  11295. (defun org-hh:mm-string-to-minutes (s)
  11296. "Convert a string H:MM to a number of minutes."
  11297. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11298. (+ (* (string-to-number (match-string 1 s)) 60)
  11299. (string-to-number (match-string 2 s)))
  11300. 0))
  11301. ;;;; Files
  11302. (defun org-save-all-org-buffers ()
  11303. "Save all Org-mode buffers without user confirmation."
  11304. (interactive)
  11305. (message "Saving all Org-mode buffers...")
  11306. (save-some-buffers t 'org-mode-p)
  11307. (when (featurep 'org-id) (org-id-locations-save))
  11308. (message "Saving all Org-mode buffers... done"))
  11309. (defun org-revert-all-org-buffers ()
  11310. "Revert all Org-mode buffers.
  11311. Prompt for confirmation when there are unsaved changes.
  11312. Be sure you know what you are doing before letting this function
  11313. overwrite your changes.
  11314. This function is useful in a setup where one tracks org files
  11315. with a version control system, to revert on one machine after pulling
  11316. changes from another. I believe the procedure must be like this:
  11317. 1. M-x org-save-all-org-buffers
  11318. 2. Pull changes from the other machine, resolve conflicts
  11319. 3. M-x org-revert-all-org-buffers"
  11320. (interactive)
  11321. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11322. (error "Abort"))
  11323. (save-excursion
  11324. (save-window-excursion
  11325. (mapc
  11326. (lambda (b)
  11327. (when (and (with-current-buffer b (org-mode-p))
  11328. (with-current-buffer b buffer-file-name))
  11329. (switch-to-buffer b)
  11330. (revert-buffer t 'no-confirm)))
  11331. (buffer-list))
  11332. (when (and (featurep 'org-id) org-id-track-globally)
  11333. (org-id-locations-load)))))
  11334. ;;;; Agenda files
  11335. ;;;###autoload
  11336. (defun org-iswitchb (&optional arg)
  11337. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11338. With a prefix argument, restrict available to files.
  11339. With two prefix arguments, restrict available buffers to agenda files.
  11340. Due to some yet unresolved reason, the global function
  11341. `iswitchb-mode' needs to be active for this function to work."
  11342. (interactive "P")
  11343. (require 'iswitchb)
  11344. (let ((enabled iswitchb-mode) blist)
  11345. (or enabled (iswitchb-mode 1))
  11346. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11347. ((equal arg '(16)) (org-buffer-list 'agenda))
  11348. (t (org-buffer-list))))
  11349. (unwind-protect
  11350. (let ((iswitchb-make-buflist-hook
  11351. (lambda ()
  11352. (setq iswitchb-temp-buflist
  11353. (mapcar 'buffer-name blist)))))
  11354. (switch-to-buffer
  11355. (iswitchb-read-buffer
  11356. "Switch-to: " nil t))
  11357. (or enabled (iswitchb-mode -1))))))
  11358. ;;;###autoload
  11359. (defun org-ido-switchb (&optional arg)
  11360. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11361. With a prefix argument, restrict available to files.
  11362. With two prefix arguments, restrict available buffers to agenda files."
  11363. (interactive "P")
  11364. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11365. ((equal arg '(16)) (org-buffer-list 'agenda))
  11366. (t (org-buffer-list)))))
  11367. (switch-to-buffer
  11368. (org-ido-completing-read "Org buffer: "
  11369. (mapcar 'buffer-name blist)
  11370. nil t))))
  11371. (defun org-buffer-list (&optional predicate exclude-tmp)
  11372. "Return a list of Org buffers.
  11373. PREDICATE can be `export', `files' or `agenda'.
  11374. export restrict the list to Export buffers.
  11375. files restrict the list to buffers visiting Org files.
  11376. agenda restrict the list to buffers visiting agenda files.
  11377. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11378. (let* ((bfn nil)
  11379. (agenda-files (and (eq predicate 'agenda)
  11380. (mapcar 'file-truename (org-agenda-files t))))
  11381. (filter
  11382. (cond
  11383. ((eq predicate 'files)
  11384. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11385. ((eq predicate 'export)
  11386. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11387. ((eq predicate 'agenda)
  11388. (lambda (b)
  11389. (with-current-buffer b
  11390. (and (eq major-mode 'org-mode)
  11391. (setq bfn (buffer-file-name b))
  11392. (member (file-truename bfn) agenda-files)))))
  11393. (t (lambda (b) (with-current-buffer b
  11394. (or (eq major-mode 'org-mode)
  11395. (string-match "\*Org .*Export"
  11396. (buffer-name b)))))))))
  11397. (delq nil
  11398. (mapcar
  11399. (lambda(b)
  11400. (if (and (funcall filter b)
  11401. (or (not exclude-tmp)
  11402. (not (string-match "tmp" (buffer-name b)))))
  11403. b
  11404. nil))
  11405. (buffer-list)))))
  11406. (defun org-agenda-files (&optional unrestricted archives)
  11407. "Get the list of agenda files.
  11408. Optional UNRESTRICTED means return the full list even if a restriction
  11409. is currently in place.
  11410. When ARCHIVES is t, include all archive files hat are really being
  11411. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11412. `org-agenda-archives-mode' is t."
  11413. (let ((files
  11414. (cond
  11415. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11416. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11417. ((listp org-agenda-files) org-agenda-files)
  11418. (t (error "Invalid value of `org-agenda-files'")))))
  11419. (setq files (apply 'append
  11420. (mapcar (lambda (f)
  11421. (if (file-directory-p f)
  11422. (directory-files
  11423. f t org-agenda-file-regexp)
  11424. (list f)))
  11425. files)))
  11426. (when org-agenda-skip-unavailable-files
  11427. (setq files (delq nil
  11428. (mapcar (function
  11429. (lambda (file)
  11430. (and (file-readable-p file) file)))
  11431. files))))
  11432. (when (or (eq archives t)
  11433. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11434. (setq files (org-add-archive-files files)))
  11435. files))
  11436. (defun org-edit-agenda-file-list ()
  11437. "Edit the list of agenda files.
  11438. Depending on setup, this either uses customize to edit the variable
  11439. `org-agenda-files', or it visits the file that is holding the list. In the
  11440. latter case, the buffer is set up in a way that saving it automatically kills
  11441. the buffer and restores the previous window configuration."
  11442. (interactive)
  11443. (if (stringp org-agenda-files)
  11444. (let ((cw (current-window-configuration)))
  11445. (find-file org-agenda-files)
  11446. (org-set-local 'org-window-configuration cw)
  11447. (org-add-hook 'after-save-hook
  11448. (lambda ()
  11449. (set-window-configuration
  11450. (prog1 org-window-configuration
  11451. (kill-buffer (current-buffer))))
  11452. (org-install-agenda-files-menu)
  11453. (message "New agenda file list installed"))
  11454. nil 'local)
  11455. (message "%s" (substitute-command-keys
  11456. "Edit list and finish with \\[save-buffer]")))
  11457. (customize-variable 'org-agenda-files)))
  11458. (defun org-store-new-agenda-file-list (list)
  11459. "Set new value for the agenda file list and save it correctly."
  11460. (if (stringp org-agenda-files)
  11461. (let ((f org-agenda-files) b)
  11462. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11463. (with-temp-file f
  11464. (insert (mapconcat 'identity list "\n") "\n")))
  11465. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11466. (setq org-agenda-files list)
  11467. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11468. (defun org-read-agenda-file-list ()
  11469. "Read the list of agenda files from a file."
  11470. (when (file-directory-p org-agenda-files)
  11471. (error "`org-agenda-files' cannot be a single directory"))
  11472. (when (stringp org-agenda-files)
  11473. (with-temp-buffer
  11474. (insert-file-contents org-agenda-files)
  11475. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11476. ;;;###autoload
  11477. (defun org-cycle-agenda-files ()
  11478. "Cycle through the files in `org-agenda-files'.
  11479. If the current buffer visits an agenda file, find the next one in the list.
  11480. If the current buffer does not, find the first agenda file."
  11481. (interactive)
  11482. (let* ((fs (org-agenda-files t))
  11483. (files (append fs (list (car fs))))
  11484. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11485. file)
  11486. (unless files (error "No agenda files"))
  11487. (catch 'exit
  11488. (while (setq file (pop files))
  11489. (if (equal (file-truename file) tcf)
  11490. (when (car files)
  11491. (find-file (car files))
  11492. (throw 'exit t))))
  11493. (find-file (car fs)))
  11494. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11495. (defun org-agenda-file-to-front (&optional to-end)
  11496. "Move/add the current file to the top of the agenda file list.
  11497. If the file is not present in the list, it is added to the front. If it is
  11498. present, it is moved there. With optional argument TO-END, add/move to the
  11499. end of the list."
  11500. (interactive "P")
  11501. (let ((org-agenda-skip-unavailable-files nil)
  11502. (file-alist (mapcar (lambda (x)
  11503. (cons (file-truename x) x))
  11504. (org-agenda-files t)))
  11505. (ctf (file-truename buffer-file-name))
  11506. x had)
  11507. (setq x (assoc ctf file-alist) had x)
  11508. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11509. (if to-end
  11510. (setq file-alist (append (delq x file-alist) (list x)))
  11511. (setq file-alist (cons x (delq x file-alist))))
  11512. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11513. (org-install-agenda-files-menu)
  11514. (message "File %s to %s of agenda file list"
  11515. (if had "moved" "added") (if to-end "end" "front"))))
  11516. (defun org-remove-file (&optional file)
  11517. "Remove current file from the list of files in variable `org-agenda-files'.
  11518. These are the files which are being checked for agenda entries.
  11519. Optional argument FILE means, use this file instead of the current."
  11520. (interactive)
  11521. (let* ((org-agenda-skip-unavailable-files nil)
  11522. (file (or file buffer-file-name))
  11523. (true-file (file-truename file))
  11524. (afile (abbreviate-file-name file))
  11525. (files (delq nil (mapcar
  11526. (lambda (x)
  11527. (if (equal true-file
  11528. (file-truename x))
  11529. nil x))
  11530. (org-agenda-files t)))))
  11531. (if (not (= (length files) (length (org-agenda-files t))))
  11532. (progn
  11533. (org-store-new-agenda-file-list files)
  11534. (org-install-agenda-files-menu)
  11535. (message "Removed file: %s" afile))
  11536. (message "File was not in list: %s (not removed)" afile))))
  11537. (defun org-file-menu-entry (file)
  11538. (vector file (list 'find-file file) t))
  11539. (defun org-check-agenda-file (file)
  11540. "Make sure FILE exists. If not, ask user what to do."
  11541. (when (not (file-exists-p file))
  11542. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11543. (abbreviate-file-name file))
  11544. (let ((r (downcase (read-char-exclusive))))
  11545. (cond
  11546. ((equal r ?r)
  11547. (org-remove-file file)
  11548. (throw 'nextfile t))
  11549. (t (error "Abort"))))))
  11550. (defun org-get-agenda-file-buffer (file)
  11551. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11552. it to the list of buffers which might be released later."
  11553. (let ((buf (org-find-base-buffer-visiting file)))
  11554. (if buf
  11555. buf ; just return it
  11556. ;; Make a new buffer and remember it
  11557. (setq buf (find-file-noselect file))
  11558. (if buf (push buf org-agenda-new-buffers))
  11559. buf)))
  11560. (defun org-release-buffers (blist)
  11561. "Release all buffers in list, asking the user for confirmation when needed.
  11562. When a buffer is unmodified, it is just killed. When modified, it is saved
  11563. \(if the user agrees) and then killed."
  11564. (let (buf file)
  11565. (while (setq buf (pop blist))
  11566. (setq file (buffer-file-name buf))
  11567. (when (and (buffer-modified-p buf)
  11568. file
  11569. (y-or-n-p (format "Save file %s? " file)))
  11570. (with-current-buffer buf (save-buffer)))
  11571. (kill-buffer buf))))
  11572. (defun org-prepare-agenda-buffers (files)
  11573. "Create buffers for all agenda files, protect archived trees and comments."
  11574. (interactive)
  11575. (let ((pa '(:org-archived t))
  11576. (pc '(:org-comment t))
  11577. (pall '(:org-archived t :org-comment t))
  11578. (inhibit-read-only t)
  11579. (rea (concat ":" org-archive-tag ":"))
  11580. bmp file re)
  11581. (save-excursion
  11582. (save-restriction
  11583. (while (setq file (pop files))
  11584. (if (bufferp file)
  11585. (set-buffer file)
  11586. (org-check-agenda-file file)
  11587. (set-buffer (org-get-agenda-file-buffer file)))
  11588. (widen)
  11589. (setq bmp (buffer-modified-p))
  11590. (org-refresh-category-properties)
  11591. (setq org-todo-keywords-for-agenda
  11592. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11593. (setq org-done-keywords-for-agenda
  11594. (append org-done-keywords-for-agenda org-done-keywords))
  11595. (setq org-todo-keyword-alist-for-agenda
  11596. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11597. (setq org-tag-alist-for-agenda
  11598. (append org-tag-alist-for-agenda org-tag-alist))
  11599. (save-excursion
  11600. (remove-text-properties (point-min) (point-max) pall)
  11601. (when org-agenda-skip-archived-trees
  11602. (goto-char (point-min))
  11603. (while (re-search-forward rea nil t)
  11604. (if (org-on-heading-p t)
  11605. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11606. (goto-char (point-min))
  11607. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11608. (while (re-search-forward re nil t)
  11609. (add-text-properties
  11610. (match-beginning 0) (org-end-of-subtree t) pc)))
  11611. (set-buffer-modified-p bmp))))
  11612. (setq org-todo-keyword-alist-for-agenda
  11613. (org-uniquify org-todo-keyword-alist-for-agenda)
  11614. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11615. ;;;; Embedded LaTeX
  11616. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11617. "Keymap for the minor `org-cdlatex-mode'.")
  11618. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11619. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11620. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11621. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11622. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11623. (defvar org-cdlatex-texmathp-advice-is-done nil
  11624. "Flag remembering if we have applied the advice to texmathp already.")
  11625. (define-minor-mode org-cdlatex-mode
  11626. "Toggle the minor `org-cdlatex-mode'.
  11627. This mode supports entering LaTeX environment and math in LaTeX fragments
  11628. in Org-mode.
  11629. \\{org-cdlatex-mode-map}"
  11630. nil " OCDL" nil
  11631. (when org-cdlatex-mode (require 'cdlatex))
  11632. (unless org-cdlatex-texmathp-advice-is-done
  11633. (setq org-cdlatex-texmathp-advice-is-done t)
  11634. (defadvice texmathp (around org-math-always-on activate)
  11635. "Always return t in org-mode buffers.
  11636. This is because we want to insert math symbols without dollars even outside
  11637. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11638. an embedded LaTeX fragment, let texmathp do its job.
  11639. \\[org-cdlatex-mode-map]"
  11640. (interactive)
  11641. (let (p)
  11642. (cond
  11643. ((not (org-mode-p)) ad-do-it)
  11644. ((eq this-command 'cdlatex-math-symbol)
  11645. (setq ad-return-value t
  11646. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11647. (t
  11648. (let ((p (org-inside-LaTeX-fragment-p)))
  11649. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11650. (setq ad-return-value t
  11651. texmathp-why '("Org-mode embedded math" . 0))
  11652. (if p ad-do-it)))))))))
  11653. (defun turn-on-org-cdlatex ()
  11654. "Unconditionally turn on `org-cdlatex-mode'."
  11655. (org-cdlatex-mode 1))
  11656. (defun org-inside-LaTeX-fragment-p ()
  11657. "Test if point is inside a LaTeX fragment.
  11658. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11659. sequence appearing also before point.
  11660. Even though the matchers for math are configurable, this function assumes
  11661. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11662. delimiters are skipped when they have been removed by customization.
  11663. The return value is nil, or a cons cell with the delimiter and
  11664. and the position of this delimiter.
  11665. This function does a reasonably good job, but can locally be fooled by
  11666. for example currency specifications. For example it will assume being in
  11667. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11668. fragments that are properly closed, but during editing, we have to live
  11669. with the uncertainty caused by missing closing delimiters. This function
  11670. looks only before point, not after."
  11671. (catch 'exit
  11672. (let ((pos (point))
  11673. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11674. (lim (progn
  11675. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11676. (point)))
  11677. dd-on str (start 0) m re)
  11678. (goto-char pos)
  11679. (when dodollar
  11680. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11681. re (nth 1 (assoc "$" org-latex-regexps)))
  11682. (while (string-match re str start)
  11683. (cond
  11684. ((= (match-end 0) (length str))
  11685. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11686. ((= (match-end 0) (- (length str) 5))
  11687. (throw 'exit nil))
  11688. (t (setq start (match-end 0))))))
  11689. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11690. (goto-char pos)
  11691. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11692. (and (match-beginning 2) (throw 'exit nil))
  11693. ;; count $$
  11694. (while (re-search-backward "\\$\\$" lim t)
  11695. (setq dd-on (not dd-on)))
  11696. (goto-char pos)
  11697. (if dd-on (cons "$$" m))))))
  11698. (defun org-try-cdlatex-tab ()
  11699. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11700. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11701. - inside a LaTeX fragment, or
  11702. - after the first word in a line, where an abbreviation expansion could
  11703. insert a LaTeX environment."
  11704. (when org-cdlatex-mode
  11705. (cond
  11706. ((save-excursion
  11707. (skip-chars-backward "a-zA-Z0-9*")
  11708. (skip-chars-backward " \t")
  11709. (bolp))
  11710. (cdlatex-tab) t)
  11711. ((org-inside-LaTeX-fragment-p)
  11712. (cdlatex-tab) t)
  11713. (t nil))))
  11714. (defun org-cdlatex-underscore-caret (&optional arg)
  11715. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11716. Revert to the normal definition outside of these fragments."
  11717. (interactive "P")
  11718. (if (org-inside-LaTeX-fragment-p)
  11719. (call-interactively 'cdlatex-sub-superscript)
  11720. (let (org-cdlatex-mode)
  11721. (call-interactively (key-binding (vector last-input-event))))))
  11722. (defun org-cdlatex-math-modify (&optional arg)
  11723. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11724. Revert to the normal definition outside of these fragments."
  11725. (interactive "P")
  11726. (if (org-inside-LaTeX-fragment-p)
  11727. (call-interactively 'cdlatex-math-modify)
  11728. (let (org-cdlatex-mode)
  11729. (call-interactively (key-binding (vector last-input-event))))))
  11730. (defvar org-latex-fragment-image-overlays nil
  11731. "List of overlays carrying the images of latex fragments.")
  11732. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11733. (defun org-remove-latex-fragment-image-overlays ()
  11734. "Remove all overlays with LaTeX fragment images in current buffer."
  11735. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11736. (setq org-latex-fragment-image-overlays nil))
  11737. (defun org-preview-latex-fragment (&optional subtree)
  11738. "Preview the LaTeX fragment at point, or all locally or globally.
  11739. If the cursor is in a LaTeX fragment, create the image and overlay
  11740. it over the source code. If there is no fragment at point, display
  11741. all fragments in the current text, from one headline to the next. With
  11742. prefix SUBTREE, display all fragments in the current subtree. With a
  11743. double prefix `C-u C-u', or when the cursor is before the first headline,
  11744. display all fragments in the buffer.
  11745. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11746. (interactive "P")
  11747. (org-remove-latex-fragment-image-overlays)
  11748. (save-excursion
  11749. (save-restriction
  11750. (let (beg end at msg)
  11751. (cond
  11752. ((or (equal subtree '(16))
  11753. (not (save-excursion
  11754. (re-search-backward (concat "^" outline-regexp) nil t))))
  11755. (setq beg (point-min) end (point-max)
  11756. msg "Creating images for buffer...%s"))
  11757. ((equal subtree '(4))
  11758. (org-back-to-heading)
  11759. (setq beg (point) end (org-end-of-subtree t)
  11760. msg "Creating images for subtree...%s"))
  11761. (t
  11762. (if (setq at (org-inside-LaTeX-fragment-p))
  11763. (goto-char (max (point-min) (- (cdr at) 2)))
  11764. (org-back-to-heading))
  11765. (setq beg (point) end (progn (outline-next-heading) (point))
  11766. msg (if at "Creating image...%s"
  11767. "Creating images for entry...%s"))))
  11768. (message msg "")
  11769. (narrow-to-region beg end)
  11770. (goto-char beg)
  11771. (org-format-latex
  11772. (concat "ltxpng/" (file-name-sans-extension
  11773. (file-name-nondirectory
  11774. buffer-file-name)))
  11775. default-directory 'overlays msg at 'forbuffer)
  11776. (message msg "done. Use `C-c C-c' to remove images.")))))
  11777. (defvar org-latex-regexps
  11778. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11779. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11780. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11781. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11782. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11783. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11784. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11785. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11786. "Regular expressions for matching embedded LaTeX.")
  11787. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11788. "Replace LaTeX fragments with links to an image, and produce images."
  11789. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11790. (let* ((prefixnodir (file-name-nondirectory prefix))
  11791. (absprefix (expand-file-name prefix dir))
  11792. (todir (file-name-directory absprefix))
  11793. (opt org-format-latex-options)
  11794. (matchers (plist-get opt :matchers))
  11795. (re-list org-latex-regexps)
  11796. (cnt 0) txt link beg end re e checkdir
  11797. m n block linkfile movefile ov)
  11798. ;; Check if there are old images files with this prefix, and remove them
  11799. (when (file-directory-p todir)
  11800. (mapc 'delete-file
  11801. (directory-files
  11802. todir 'full
  11803. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11804. ;; Check the different regular expressions
  11805. (while (setq e (pop re-list))
  11806. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11807. block (if (nth 3 e) "\n\n" ""))
  11808. (when (member m matchers)
  11809. (goto-char (point-min))
  11810. (while (re-search-forward re nil t)
  11811. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11812. (not (get-text-property (match-beginning n)
  11813. 'org-protected)))
  11814. (setq txt (match-string n)
  11815. beg (match-beginning n) end (match-end n)
  11816. cnt (1+ cnt)
  11817. linkfile (format "%s_%04d.png" prefix cnt)
  11818. movefile (format "%s_%04d.png" absprefix cnt)
  11819. link (concat block "[[file:" linkfile "]]" block))
  11820. (if msg (message msg cnt))
  11821. (goto-char beg)
  11822. (unless checkdir ; make sure the directory exists
  11823. (setq checkdir t)
  11824. (or (file-directory-p todir) (make-directory todir)))
  11825. (org-create-formula-image
  11826. txt movefile opt forbuffer)
  11827. (if overlays
  11828. (progn
  11829. (setq ov (org-make-overlay beg end))
  11830. (if (featurep 'xemacs)
  11831. (progn
  11832. (org-overlay-put ov 'invisible t)
  11833. (org-overlay-put
  11834. ov 'end-glyph
  11835. (make-glyph (vector 'png :file movefile))))
  11836. (org-overlay-put
  11837. ov 'display
  11838. (list 'image :type 'png :file movefile :ascent 'center)))
  11839. (push ov org-latex-fragment-image-overlays)
  11840. (goto-char end))
  11841. (delete-region beg end)
  11842. (insert link))))))))
  11843. ;; This function borrows from Ganesh Swami's latex2png.el
  11844. (defun org-create-formula-image (string tofile options buffer)
  11845. (let* ((tmpdir (if (featurep 'xemacs)
  11846. (temp-directory)
  11847. temporary-file-directory))
  11848. (texfilebase (make-temp-name
  11849. (expand-file-name "orgtex" tmpdir)))
  11850. (texfile (concat texfilebase ".tex"))
  11851. (dvifile (concat texfilebase ".dvi"))
  11852. (pngfile (concat texfilebase ".png"))
  11853. (fnh (if (featurep 'xemacs)
  11854. (font-height (get-face-font 'default))
  11855. (face-attribute 'default :height nil)))
  11856. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11857. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11858. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11859. "Black"))
  11860. (bg (or (plist-get options (if buffer :background :html-background))
  11861. "Transparent")))
  11862. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11863. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11864. (with-temp-file texfile
  11865. (insert org-format-latex-header
  11866. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11867. (let ((dir default-directory))
  11868. (condition-case nil
  11869. (progn
  11870. (cd tmpdir)
  11871. (call-process "latex" nil nil nil texfile))
  11872. (error nil))
  11873. (cd dir))
  11874. (if (not (file-exists-p dvifile))
  11875. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11876. (condition-case nil
  11877. (call-process "dvipng" nil nil nil
  11878. "-E" "-fg" fg "-bg" bg
  11879. "-D" dpi
  11880. ;;"-x" scale "-y" scale
  11881. "-T" "tight"
  11882. "-o" pngfile
  11883. dvifile)
  11884. (error nil))
  11885. (if (not (file-exists-p pngfile))
  11886. (progn (message "Failed to create png file from %s" texfile) nil)
  11887. ;; Use the requested file name and clean up
  11888. (copy-file pngfile tofile 'replace)
  11889. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11890. (delete-file (concat texfilebase e)))
  11891. pngfile))))
  11892. (defun org-dvipng-color (attr)
  11893. "Return an rgb color specification for dvipng."
  11894. (apply 'format "rgb %s %s %s"
  11895. (mapcar 'org-normalize-color
  11896. (color-values (face-attribute 'default attr nil)))))
  11897. (defun org-normalize-color (value)
  11898. "Return string to be used as color value for an RGB component."
  11899. (format "%g" (/ value 65535.0)))
  11900. ;;;; Key bindings
  11901. ;; Make `C-c C-x' a prefix key
  11902. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11903. ;; TAB key with modifiers
  11904. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11905. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11906. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11907. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11908. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11909. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11910. ;; The following line is necessary under Suse GNU/Linux
  11911. (unless (featurep 'xemacs)
  11912. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11913. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11914. (define-key org-mode-map [backtab] 'org-shifttab)
  11915. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11916. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11917. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11918. ;; Cursor keys with modifiers
  11919. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11920. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11921. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11922. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11923. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11924. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11925. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11926. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11927. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11928. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11929. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11930. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11931. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11932. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11933. ;;; Extra keys for tty access.
  11934. ;; We only set them when really needed because otherwise the
  11935. ;; menus don't show the simple keys
  11936. (when (or org-use-extra-keys
  11937. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11938. (not window-system))
  11939. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11940. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11941. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11942. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11943. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11944. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11945. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11946. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11947. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11948. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11949. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11950. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11951. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11952. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11953. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11954. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11955. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11956. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11957. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11958. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11959. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11960. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11961. ;; All the other keys
  11962. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11963. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11964. (if (boundp 'narrow-map)
  11965. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11966. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11967. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11968. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11969. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11970. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11971. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11972. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11973. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11974. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11975. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11976. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11977. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11978. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11979. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11980. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11981. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11982. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11983. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11984. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11985. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11986. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11987. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11988. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11989. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11990. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11991. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11992. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11993. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11994. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11995. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11996. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11997. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11998. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11999. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12000. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12001. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12002. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12003. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12004. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12005. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12006. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12007. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12008. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12009. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12010. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12011. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12012. (org-defkey org-mode-map "\C-m" 'org-return)
  12013. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12014. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12015. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12016. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12017. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12018. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12019. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12020. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12021. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12022. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12023. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12024. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12025. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12026. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12027. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12028. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12029. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12030. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12031. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12032. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12033. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12034. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12035. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12036. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12037. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12038. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12039. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12040. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12041. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12042. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12043. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12044. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12045. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12046. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12047. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12048. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12049. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12050. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12051. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12052. (when (featurep 'xemacs)
  12053. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12054. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12055. (defun org-self-insert-command (N)
  12056. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12057. If the cursor is in a table looking at whitespace, the whitespace is
  12058. overwritten, and the table is not marked as requiring realignment."
  12059. (interactive "p")
  12060. (if (and (org-table-p)
  12061. (progn
  12062. ;; check if we blank the field, and if that triggers align
  12063. (and (featurep 'org-table) org-table-auto-blank-field
  12064. (member last-command
  12065. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12066. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12067. ;; got extra space, this field does not determine column width
  12068. (let (org-table-may-need-update) (org-table-blank-field))
  12069. ;; no extra space, this field may determine column width
  12070. (org-table-blank-field)))
  12071. t)
  12072. (eq N 1)
  12073. (looking-at "[^|\n]* |"))
  12074. (let (org-table-may-need-update)
  12075. (goto-char (1- (match-end 0)))
  12076. (delete-backward-char 1)
  12077. (goto-char (match-beginning 0))
  12078. (self-insert-command N))
  12079. (setq org-table-may-need-update t)
  12080. (self-insert-command N)
  12081. (org-fix-tags-on-the-fly)))
  12082. (defun org-fix-tags-on-the-fly ()
  12083. (when (and (equal (char-after (point-at-bol)) ?*)
  12084. (org-on-heading-p))
  12085. (org-align-tags-here org-tags-column)))
  12086. (defun org-delete-backward-char (N)
  12087. "Like `delete-backward-char', insert whitespace at field end in tables.
  12088. When deleting backwards, in tables this function will insert whitespace in
  12089. front of the next \"|\" separator, to keep the table aligned. The table will
  12090. still be marked for re-alignment if the field did fill the entire column,
  12091. because, in this case the deletion might narrow the column."
  12092. (interactive "p")
  12093. (if (and (org-table-p)
  12094. (eq N 1)
  12095. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12096. (looking-at ".*?|"))
  12097. (let ((pos (point))
  12098. (noalign (looking-at "[^|\n\r]* |"))
  12099. (c org-table-may-need-update))
  12100. (backward-delete-char N)
  12101. (skip-chars-forward "^|")
  12102. (insert " ")
  12103. (goto-char (1- pos))
  12104. ;; noalign: if there were two spaces at the end, this field
  12105. ;; does not determine the width of the column.
  12106. (if noalign (setq org-table-may-need-update c)))
  12107. (backward-delete-char N)
  12108. (org-fix-tags-on-the-fly)))
  12109. (defun org-delete-char (N)
  12110. "Like `delete-char', but insert whitespace at field end in tables.
  12111. When deleting characters, in tables this function will insert whitespace in
  12112. front of the next \"|\" separator, to keep the table aligned. The table will
  12113. still be marked for re-alignment if the field did fill the entire column,
  12114. because, in this case the deletion might narrow the column."
  12115. (interactive "p")
  12116. (if (and (org-table-p)
  12117. (not (bolp))
  12118. (not (= (char-after) ?|))
  12119. (eq N 1))
  12120. (if (looking-at ".*?|")
  12121. (let ((pos (point))
  12122. (noalign (looking-at "[^|\n\r]* |"))
  12123. (c org-table-may-need-update))
  12124. (replace-match (concat
  12125. (substring (match-string 0) 1 -1)
  12126. " |"))
  12127. (goto-char pos)
  12128. ;; noalign: if there were two spaces at the end, this field
  12129. ;; does not determine the width of the column.
  12130. (if noalign (setq org-table-may-need-update c)))
  12131. (delete-char N))
  12132. (delete-char N)
  12133. (org-fix-tags-on-the-fly)))
  12134. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12135. (put 'org-self-insert-command 'delete-selection t)
  12136. (put 'orgtbl-self-insert-command 'delete-selection t)
  12137. (put 'org-delete-char 'delete-selection 'supersede)
  12138. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12139. (put 'org-yank 'delete-selection 'yank)
  12140. ;; Make `flyspell-mode' delay after some commands
  12141. (put 'org-self-insert-command 'flyspell-delayed t)
  12142. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12143. (put 'org-delete-char 'flyspell-delayed t)
  12144. (put 'org-delete-backward-char 'flyspell-delayed t)
  12145. ;; Make pabbrev-mode expand after org-mode commands
  12146. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12147. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12148. ;; How to do this: Measure non-white length of current string
  12149. ;; If equal to column width, we should realign.
  12150. (defun org-remap (map &rest commands)
  12151. "In MAP, remap the functions given in COMMANDS.
  12152. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12153. (let (new old)
  12154. (while commands
  12155. (setq old (pop commands) new (pop commands))
  12156. (if (fboundp 'command-remapping)
  12157. (org-defkey map (vector 'remap old) new)
  12158. (substitute-key-definition old new map global-map)))))
  12159. (when (eq org-enable-table-editor 'optimized)
  12160. ;; If the user wants maximum table support, we need to hijack
  12161. ;; some standard editing functions
  12162. (org-remap org-mode-map
  12163. 'self-insert-command 'org-self-insert-command
  12164. 'delete-char 'org-delete-char
  12165. 'delete-backward-char 'org-delete-backward-char)
  12166. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12167. (defvar org-ctrl-c-ctrl-c-hook nil
  12168. "Hook for functions attaching themselves to `C-c C-c'.
  12169. This can be used to add additional functionality to the C-c C-c key which
  12170. executes context-dependent commands.
  12171. Each function will be called with no arguments. The function must check
  12172. if the context is appropriate for it to act. If yes, it should do its
  12173. thing and then return a non-nil value. If the context is wrong,
  12174. just do nothing.")
  12175. (defvar org-metaleft-hook nil
  12176. "Hook for functions attaching themselves to `M-left'.
  12177. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12178. (defvar org-metaright-hook nil
  12179. "Hook for functions attaching themselves to `M-right'.
  12180. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12181. (defvar org-metaup-hook nil
  12182. "Hook for functions attaching themselves to `M-up'.
  12183. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12184. (defvar org-metadown-hook nil
  12185. "Hook for functions attaching themselves to `M-down'.
  12186. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12187. (defvar org-shiftmetaleft-hook nil
  12188. "Hook for functions attaching themselves to `M-S-left'.
  12189. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12190. (defvar org-shiftmetaright-hook nil
  12191. "Hook for functions attaching themselves to `M-S-right'.
  12192. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12193. (defvar org-shiftmetaup-hook nil
  12194. "Hook for functions attaching themselves to `M-S-up'.
  12195. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12196. (defvar org-shiftmetadown-hook nil
  12197. "Hook for functions attaching themselves to `M-S-down'.
  12198. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12199. (defvar org-metareturn-hook nil
  12200. "Hook for functions attaching themselves to `M-RET'.
  12201. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12202. (defun org-modifier-cursor-error ()
  12203. "Throw an error, a modified cursor command was applied in wrong context."
  12204. (error "This command is active in special context like tables, headlines or items"))
  12205. (defun org-shiftselect-error ()
  12206. "Throw an error because Shift-Cursor command was applied in wrong context."
  12207. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12208. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12209. (error "This command works only in special context like headlines or timestamps.")))
  12210. (defun org-call-for-shift-select (cmd)
  12211. (let ((this-command-keys-shift-translated t))
  12212. (call-interactively cmd)))
  12213. (defun org-shifttab (&optional arg)
  12214. "Global visibility cycling or move to previous table field.
  12215. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12216. on context.
  12217. See the individual commands for more information."
  12218. (interactive "P")
  12219. (cond
  12220. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12221. ((integerp arg)
  12222. (message "Content view to level: %d" arg)
  12223. (org-content (prefix-numeric-value arg))
  12224. (setq org-cycle-global-status 'overview))
  12225. (t (call-interactively 'org-global-cycle))))
  12226. (defun org-shiftmetaleft ()
  12227. "Promote subtree or delete table column.
  12228. Calls `org-promote-subtree', `org-outdent-item',
  12229. or `org-table-delete-column', depending on context.
  12230. See the individual commands for more information."
  12231. (interactive)
  12232. (cond
  12233. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12234. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12235. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12236. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12237. (t (org-modifier-cursor-error))))
  12238. (defun org-shiftmetaright ()
  12239. "Demote subtree or insert table column.
  12240. Calls `org-demote-subtree', `org-indent-item',
  12241. or `org-table-insert-column', depending on context.
  12242. See the individual commands for more information."
  12243. (interactive)
  12244. (cond
  12245. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12246. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12247. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12248. ((org-at-item-p) (call-interactively 'org-indent-item))
  12249. (t (org-modifier-cursor-error))))
  12250. (defun org-shiftmetaup (&optional arg)
  12251. "Move subtree up or kill table row.
  12252. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12253. `org-move-item-up' depending on context. See the individual commands
  12254. for more information."
  12255. (interactive "P")
  12256. (cond
  12257. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12258. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12259. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12260. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12261. (t (org-modifier-cursor-error))))
  12262. (defun org-shiftmetadown (&optional arg)
  12263. "Move subtree down or insert table row.
  12264. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12265. `org-move-item-down', depending on context. See the individual
  12266. commands for more information."
  12267. (interactive "P")
  12268. (cond
  12269. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12270. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12271. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12272. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12273. (t (org-modifier-cursor-error))))
  12274. (defun org-metaleft (&optional arg)
  12275. "Promote heading or move table column to left.
  12276. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12277. With no specific context, calls the Emacs default `backward-word'.
  12278. See the individual commands for more information."
  12279. (interactive "P")
  12280. (cond
  12281. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12282. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12283. ((or (org-on-heading-p) (org-region-active-p))
  12284. (call-interactively 'org-do-promote))
  12285. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12286. (t (call-interactively 'backward-word))))
  12287. (defun org-metaright (&optional arg)
  12288. "Demote subtree or move table column to right.
  12289. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12290. With no specific context, calls the Emacs default `forward-word'.
  12291. See the individual commands for more information."
  12292. (interactive "P")
  12293. (cond
  12294. ((run-hook-with-args-until-success 'org-metaright-hook))
  12295. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12296. ((or (org-on-heading-p) (org-region-active-p))
  12297. (call-interactively 'org-do-demote))
  12298. ((org-at-item-p) (call-interactively 'org-indent-item))
  12299. (t (call-interactively 'forward-word))))
  12300. (defun org-metaup (&optional arg)
  12301. "Move subtree up or move table row up.
  12302. Calls `org-move-subtree-up' or `org-table-move-row' or
  12303. `org-move-item-up', depending on context. See the individual commands
  12304. for more information."
  12305. (interactive "P")
  12306. (cond
  12307. ((run-hook-with-args-until-success 'org-metaup-hook))
  12308. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12309. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12310. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12311. (t (transpose-lines 1) (beginning-of-line -1))))
  12312. (defun org-metadown (&optional arg)
  12313. "Move subtree down or move table row down.
  12314. Calls `org-move-subtree-down' or `org-table-move-row' or
  12315. `org-move-item-down', depending on context. See the individual
  12316. commands for more information."
  12317. (interactive "P")
  12318. (cond
  12319. ((run-hook-with-args-until-success 'org-metadown-hook))
  12320. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12321. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12322. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12323. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12324. (defun org-shiftup (&optional arg)
  12325. "Increase item in timestamp or increase priority of current headline.
  12326. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12327. depending on context. See the individual commands for more information."
  12328. (interactive "P")
  12329. (cond
  12330. ((and org-support-shift-select (org-region-active-p))
  12331. (org-call-for-shift-select 'previous-line))
  12332. ((org-at-timestamp-p t)
  12333. (call-interactively (if org-edit-timestamp-down-means-later
  12334. 'org-timestamp-down 'org-timestamp-up)))
  12335. ((and (not (eq org-support-shift-select 'always))
  12336. (org-on-heading-p))
  12337. (call-interactively 'org-priority-up))
  12338. ((and (not org-support-shift-select) (org-at-item-p))
  12339. (call-interactively 'org-previous-item))
  12340. ((org-clocktable-try-shift 'up arg))
  12341. (org-support-shift-select
  12342. (org-call-for-shift-select 'previous-line))
  12343. (t (org-shiftselect-error))))
  12344. (defun org-shiftdown (&optional arg)
  12345. "Decrease item in timestamp or decrease priority of current headline.
  12346. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12347. depending on context. See the individual commands for more information."
  12348. (interactive "P")
  12349. (cond
  12350. ((and org-support-shift-select (org-region-active-p))
  12351. (org-call-for-shift-select 'next-line))
  12352. ((org-at-timestamp-p t)
  12353. (call-interactively (if org-edit-timestamp-down-means-later
  12354. 'org-timestamp-up 'org-timestamp-down)))
  12355. ((and (not (eq org-support-shift-select 'always))
  12356. (org-on-heading-p))
  12357. (call-interactively 'org-priority-down))
  12358. ((and (not org-support-shift-select) (org-at-item-p))
  12359. (call-interactively 'org-next-item))
  12360. ((org-clocktable-try-shift 'down arg))
  12361. (org-support-shift-select
  12362. (org-call-for-shift-select 'next-line))
  12363. (t (org-shiftselect-error))))
  12364. (defun org-shiftright (&optional arg)
  12365. "Cycle the thing at point or in the current line, depending on context.
  12366. Depending on context, this does one of the following:
  12367. - switch a timestamp at point one day into the future
  12368. - on a headline, switch to the next TODO keyword.
  12369. - on an item, switch entire list to the next bullet type
  12370. - on a property line, switch to the next allowed value
  12371. - on a clocktable definition line, move time block into the future"
  12372. (interactive "P")
  12373. (cond
  12374. ((and org-support-shift-select (org-region-active-p))
  12375. (org-call-for-shift-select 'forward-char))
  12376. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12377. ((and (not (eq org-support-shift-select 'always))
  12378. (org-on-heading-p))
  12379. (org-call-with-arg 'org-todo 'right))
  12380. ((or (and org-support-shift-select
  12381. (not (eq org-support-shift-select 'always))
  12382. (org-at-item-bullet-p))
  12383. (and (not org-support-shift-select) (org-at-item-p)))
  12384. (org-call-with-arg 'org-cycle-list-bullet nil))
  12385. ((and (not (eq org-support-shift-select 'always))
  12386. (org-at-property-p))
  12387. (call-interactively 'org-property-next-allowed-value))
  12388. ((org-clocktable-try-shift 'right arg))
  12389. (org-support-shift-select
  12390. (org-call-for-shift-select 'forward-char))
  12391. (t (org-shiftselect-error))))
  12392. (defun org-shiftleft (&optional arg)
  12393. "Cycle the thing at point or in the current line, depending on context.
  12394. Depending on context, this does one of the following:
  12395. - switch a timestamp at point one day into the past
  12396. - on a headline, switch to the previous TODO keyword.
  12397. - on an item, switch entire list to the previous bullet type
  12398. - on a property line, switch to the previous allowed value
  12399. - on a clocktable definition line, move time block into the past"
  12400. (interactive "P")
  12401. (cond
  12402. ((and org-support-shift-select (org-region-active-p))
  12403. (org-call-for-shift-select 'backward-char))
  12404. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12405. ((and (not (eq org-support-shift-select 'always))
  12406. (org-on-heading-p))
  12407. (org-call-with-arg 'org-todo 'left))
  12408. ((or (and org-support-shift-select
  12409. (not (eq org-support-shift-select 'always))
  12410. (org-at-item-bullet-p))
  12411. (and (not org-support-shift-select) (org-at-item-p)))
  12412. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12413. ((and (not (eq org-support-shift-select 'always))
  12414. (org-at-property-p))
  12415. (call-interactively 'org-property-previous-allowed-value))
  12416. ((org-clocktable-try-shift 'left arg))
  12417. (org-support-shift-select
  12418. (org-call-for-shift-select 'backward-char))
  12419. (t (org-shiftselect-error))))
  12420. (defun org-shiftcontrolright ()
  12421. "Switch to next TODO set."
  12422. (interactive)
  12423. (cond
  12424. ((and org-support-shift-select (org-region-active-p))
  12425. (org-call-for-shift-select 'forward-word))
  12426. ((and (not (eq org-support-shift-select 'always))
  12427. (org-on-heading-p))
  12428. (org-call-with-arg 'org-todo 'nextset))
  12429. (org-support-shift-select
  12430. (org-call-for-shift-select 'forward-word))
  12431. (t (org-shiftselect-error))))
  12432. (defun org-shiftcontrolleft ()
  12433. "Switch to previous TODO set."
  12434. (interactive)
  12435. (cond
  12436. ((and org-support-shift-select (org-region-active-p))
  12437. (org-call-for-shift-select 'backward-word))
  12438. ((and (not (eq org-support-shift-select 'always))
  12439. (org-on-heading-p))
  12440. (org-call-with-arg 'org-todo 'previousset))
  12441. (org-support-shift-select
  12442. (org-call-for-shift-select 'backward-word))
  12443. (t (org-shiftselect-error))))
  12444. (defun org-ctrl-c-ret ()
  12445. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12446. (interactive)
  12447. (cond
  12448. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12449. (t (call-interactively 'org-insert-heading))))
  12450. (defun org-copy-special ()
  12451. "Copy region in table or copy current subtree.
  12452. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12453. See the individual commands for more information."
  12454. (interactive)
  12455. (call-interactively
  12456. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12457. (defun org-cut-special ()
  12458. "Cut region in table or cut current subtree.
  12459. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12460. See the individual commands for more information."
  12461. (interactive)
  12462. (call-interactively
  12463. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12464. (defun org-paste-special (arg)
  12465. "Paste rectangular region into table, or past subtree relative to level.
  12466. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12467. See the individual commands for more information."
  12468. (interactive "P")
  12469. (if (org-at-table-p)
  12470. (org-table-paste-rectangle)
  12471. (org-paste-subtree arg)))
  12472. (defun org-edit-special ()
  12473. "Call a special editor for the stuff at point.
  12474. When at a table, call the formula editor with `org-table-edit-formulas'.
  12475. When at the first line of an src example, call `org-edit-src-code'.
  12476. When in an #+include line, visit the include file. Otherwise call
  12477. `ffap' to visit the file at point."
  12478. (interactive)
  12479. (cond
  12480. ((org-at-table-p)
  12481. (call-interactively 'org-table-edit-formulas))
  12482. ((save-excursion
  12483. (beginning-of-line 1)
  12484. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12485. (find-file (org-trim (match-string 1))))
  12486. ((org-edit-src-code))
  12487. ((org-edit-fixed-width-region))
  12488. (t (call-interactively 'ffap))))
  12489. (defun org-ctrl-c-ctrl-c (&optional arg)
  12490. "Set tags in headline, or update according to changed information at point.
  12491. This command does many different things, depending on context:
  12492. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12493. this is what we do.
  12494. - If the cursor is in a headline, prompt for tags and insert them
  12495. into the current line, aligned to `org-tags-column'. When called
  12496. with prefix arg, realign all tags in the current buffer.
  12497. - If the cursor is in one of the special #+KEYWORD lines, this
  12498. triggers scanning the buffer for these lines and updating the
  12499. information.
  12500. - If the cursor is inside a table, realign the table. This command
  12501. works even if the automatic table editor has been turned off.
  12502. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12503. the entire table.
  12504. - If the cursor is at a footnote reference or definition, jump to
  12505. the corresponding definition or references, respectively.
  12506. - If the cursor is a the beginning of a dynamic block, update it.
  12507. - If the cursor is inside a table created by the table.el package,
  12508. activate that table.
  12509. - If the current buffer is a remember buffer, close note and file
  12510. it. A prefix argument of 1 files to the default location
  12511. without further interaction. A prefix argument of 2 files to
  12512. the currently clocking task.
  12513. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12514. links in this buffer.
  12515. - If the cursor is on a numbered item in a plain list, renumber the
  12516. ordered list.
  12517. - If the cursor is on a checkbox, toggle it."
  12518. (interactive "P")
  12519. (let ((org-enable-table-editor t))
  12520. (cond
  12521. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12522. org-occur-highlights
  12523. org-latex-fragment-image-overlays)
  12524. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12525. (org-remove-occur-highlights)
  12526. (org-remove-latex-fragment-image-overlays)
  12527. (message "Temporary highlights/overlays removed from current buffer"))
  12528. ((and (local-variable-p 'org-finish-function (current-buffer))
  12529. (fboundp org-finish-function))
  12530. (funcall org-finish-function))
  12531. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12532. ((org-at-property-p)
  12533. (call-interactively 'org-property-action))
  12534. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12535. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12536. ((org-at-table.el-p)
  12537. (require 'table)
  12538. (beginning-of-line 1)
  12539. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12540. (call-interactively 'table-recognize-table))
  12541. ((org-at-table-p)
  12542. (org-table-maybe-eval-formula)
  12543. (if arg
  12544. (call-interactively 'org-table-recalculate)
  12545. (org-table-maybe-recalculate-line))
  12546. (call-interactively 'org-table-align))
  12547. ((or (org-footnote-at-reference-p)
  12548. (org-footnote-at-definition-p))
  12549. (call-interactively 'org-footnote-action))
  12550. ((org-at-item-checkbox-p)
  12551. (call-interactively 'org-toggle-checkbox))
  12552. ((org-at-item-p)
  12553. (if arg
  12554. (call-interactively 'org-toggle-checkbox)
  12555. (call-interactively 'org-maybe-renumber-ordered-list)))
  12556. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12557. ;; Dynamic block
  12558. (beginning-of-line 1)
  12559. (save-excursion (org-update-dblock)))
  12560. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12561. (cond
  12562. ((equal (match-string 1) "TBLFM")
  12563. ;; Recalculate the table before this line
  12564. (save-excursion
  12565. (beginning-of-line 1)
  12566. (skip-chars-backward " \r\n\t")
  12567. (if (org-at-table-p)
  12568. (org-call-with-arg 'org-table-recalculate t))))
  12569. (t
  12570. ; (org-set-regexps-and-options)
  12571. ; (org-restart-font-lock)
  12572. (let ((org-inhibit-startup t)) (org-mode-restart))
  12573. (message "Local setup has been refreshed"))))
  12574. (t (error "C-c C-c can do nothing useful at this location.")))))
  12575. (defun org-mode-restart ()
  12576. "Restart Org-mode, to scan again for special lines.
  12577. Also updates the keyword regular expressions."
  12578. (interactive)
  12579. (org-mode)
  12580. (message "Org-mode restarted"))
  12581. (defun org-kill-note-or-show-branches ()
  12582. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12583. (interactive)
  12584. (if (not org-finish-function)
  12585. (call-interactively 'show-branches)
  12586. (let ((org-note-abort t))
  12587. (funcall org-finish-function))))
  12588. (defun org-return (&optional indent)
  12589. "Goto next table row or insert a newline.
  12590. Calls `org-table-next-row' or `newline', depending on context.
  12591. See the individual commands for more information."
  12592. (interactive)
  12593. (cond
  12594. ((bobp) (if indent (newline-and-indent) (newline)))
  12595. ((org-at-table-p)
  12596. (org-table-justify-field-maybe)
  12597. (call-interactively 'org-table-next-row))
  12598. ((and org-return-follows-link
  12599. (eq (get-text-property (point) 'face) 'org-link))
  12600. (call-interactively 'org-open-at-point))
  12601. ((and (org-at-heading-p)
  12602. (looking-at
  12603. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12604. (org-show-entry)
  12605. (end-of-line 1)
  12606. (newline))
  12607. (t (if indent (newline-and-indent) (newline)))))
  12608. (defun org-return-indent ()
  12609. "Goto next table row or insert a newline and indent.
  12610. Calls `org-table-next-row' or `newline-and-indent', depending on
  12611. context. See the individual commands for more information."
  12612. (interactive)
  12613. (org-return t))
  12614. (defun org-ctrl-c-star ()
  12615. "Compute table, or change heading status of lines.
  12616. Calls `org-table-recalculate' or `org-toggle-heading',
  12617. depending on context."
  12618. (interactive)
  12619. (cond
  12620. ((org-at-table-p)
  12621. (call-interactively 'org-table-recalculate))
  12622. (t
  12623. ;; Convert all lines in region to list items
  12624. (call-interactively 'org-toggle-heading))))
  12625. (defun org-ctrl-c-minus ()
  12626. "Insert separator line in table or modify bullet status of line.
  12627. Also turns a plain line or a region of lines into list items.
  12628. Calls `org-table-insert-hline', `org-toggle-item', or
  12629. `org-cycle-list-bullet', depending on context."
  12630. (interactive)
  12631. (cond
  12632. ((org-at-table-p)
  12633. (call-interactively 'org-table-insert-hline))
  12634. ((org-region-active-p)
  12635. (call-interactively 'org-toggle-item))
  12636. ((org-in-item-p)
  12637. (call-interactively 'org-cycle-list-bullet))
  12638. (t
  12639. (call-interactively 'org-toggle-item))))
  12640. (defun org-toggle-item ()
  12641. "Convert headings or normal lines to items, items to normal lines.
  12642. If there is no active region, only the current line is considered.
  12643. If the first line in the region is a headline, convert all headlines to items.
  12644. If the first line in the region is an item, convert all items to normal lines.
  12645. If the first line is normal text, add an item bullet to each line."
  12646. (interactive)
  12647. (let (l2 l beg end)
  12648. (if (org-region-active-p)
  12649. (setq beg (region-beginning) end (region-end))
  12650. (setq beg (point-at-bol)
  12651. end (min (1+ (point-at-eol)) (point-max))))
  12652. (save-excursion
  12653. (goto-char end)
  12654. (setq l2 (org-current-line))
  12655. (goto-char beg)
  12656. (beginning-of-line 1)
  12657. (setq l (1- (org-current-line)))
  12658. (if (org-at-item-p)
  12659. ;; We already have items, de-itemize
  12660. (while (< (setq l (1+ l)) l2)
  12661. (when (org-at-item-p)
  12662. (goto-char (match-beginning 2))
  12663. (delete-region (match-beginning 2) (match-end 2))
  12664. (and (looking-at "[ \t]+") (replace-match "")))
  12665. (beginning-of-line 2))
  12666. (if (org-on-heading-p)
  12667. ;; Headings, convert to items
  12668. (while (< (setq l (1+ l)) l2)
  12669. (if (looking-at org-outline-regexp)
  12670. (replace-match "- " t t))
  12671. (beginning-of-line 2))
  12672. ;; normal lines, turn them into items
  12673. (while (< (setq l (1+ l)) l2)
  12674. (unless (org-at-item-p)
  12675. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12676. (replace-match "\\1- \\2")))
  12677. (beginning-of-line 2)))))))
  12678. (defun org-toggle-heading (&optional nstars)
  12679. "Convert headings to normal text, or items or text to headings.
  12680. If there is no active region, only the current line is considered.
  12681. If the first line is a heading, remove the stars from all headlines
  12682. in the region.
  12683. If the first line is a plain list item, turn all plain list items into
  12684. headings.
  12685. If the first line is a normal line, turn each and every line in the region
  12686. into a heading.
  12687. When converting a line into a heading, the number of stars is chosen
  12688. such that the lines become children of the current entry. However, when
  12689. a prefix argument is given, its value determines the number of stars to add."
  12690. (interactive "P")
  12691. (let (l2 l itemp beg end)
  12692. (if (org-region-active-p)
  12693. (setq beg (region-beginning) end (region-end))
  12694. (setq beg (point-at-bol)
  12695. end (min (1+ (point-at-eol)) (point-max))))
  12696. (save-excursion
  12697. (goto-char end)
  12698. (setq l2 (org-current-line))
  12699. (goto-char beg)
  12700. (beginning-of-line 1)
  12701. (setq l (1- (org-current-line)))
  12702. (if (org-on-heading-p)
  12703. ;; We already have headlines, de-star them
  12704. (while (< (setq l (1+ l)) l2)
  12705. (when (org-on-heading-p t)
  12706. (and (looking-at outline-regexp) (replace-match "")))
  12707. (beginning-of-line 2))
  12708. (setq itemp (org-at-item-p))
  12709. (let* ((stars
  12710. (if nstars
  12711. (make-string (prefix-numeric-value current-prefix-arg)
  12712. ?*)
  12713. (save-excursion
  12714. (re-search-backward org-complex-heading-regexp nil t)
  12715. (or (match-string 1) "*"))))
  12716. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12717. (rpl (concat stars add-stars " ")))
  12718. (while (< (setq l (1+ l)) l2)
  12719. (if itemp
  12720. (and (org-at-item-p) (replace-match rpl t t))
  12721. (unless (org-on-heading-p)
  12722. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12723. (replace-match (concat rpl (match-string 2))))))
  12724. (beginning-of-line 2)))))))
  12725. (defun org-meta-return (&optional arg)
  12726. "Insert a new heading or wrap a region in a table.
  12727. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12728. See the individual commands for more information."
  12729. (interactive "P")
  12730. (cond
  12731. ((run-hook-with-args-until-success 'org-metareturn-hook))
  12732. ((org-at-table-p)
  12733. (call-interactively 'org-table-wrap-region))
  12734. (t (call-interactively 'org-insert-heading))))
  12735. ;;; Menu entries
  12736. ;; Define the Org-mode menus
  12737. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12738. '("Tbl"
  12739. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12740. ["Next Field" org-cycle (org-at-table-p)]
  12741. ["Previous Field" org-shifttab (org-at-table-p)]
  12742. ["Next Row" org-return (org-at-table-p)]
  12743. "--"
  12744. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12745. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12746. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12747. "--"
  12748. ("Column"
  12749. ["Move Column Left" org-metaleft (org-at-table-p)]
  12750. ["Move Column Right" org-metaright (org-at-table-p)]
  12751. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12752. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12753. ("Row"
  12754. ["Move Row Up" org-metaup (org-at-table-p)]
  12755. ["Move Row Down" org-metadown (org-at-table-p)]
  12756. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12757. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12758. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12759. "--"
  12760. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12761. ("Rectangle"
  12762. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12763. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12764. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12765. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12766. "--"
  12767. ("Calculate"
  12768. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12769. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12770. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12771. "--"
  12772. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12773. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12774. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12775. "--"
  12776. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12777. "--"
  12778. ["Sum Column/Rectangle" org-table-sum
  12779. (or (org-at-table-p) (org-region-active-p))]
  12780. ["Which Column?" org-table-current-column (org-at-table-p)])
  12781. ["Debug Formulas"
  12782. org-table-toggle-formula-debugger
  12783. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12784. ["Show Col/Row Numbers"
  12785. org-table-toggle-coordinate-overlays
  12786. :style toggle
  12787. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12788. "--"
  12789. ["Create" org-table-create (and (not (org-at-table-p))
  12790. org-enable-table-editor)]
  12791. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12792. ["Import from File" org-table-import (not (org-at-table-p))]
  12793. ["Export to File" org-table-export (org-at-table-p)]
  12794. "--"
  12795. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12796. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12797. '("Org"
  12798. ("Show/Hide"
  12799. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12800. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12801. ["Sparse Tree..." org-sparse-tree t]
  12802. ["Reveal Context" org-reveal t]
  12803. ["Show All" show-all t]
  12804. "--"
  12805. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12806. "--"
  12807. ["New Heading" org-insert-heading t]
  12808. ("Navigate Headings"
  12809. ["Up" outline-up-heading t]
  12810. ["Next" outline-next-visible-heading t]
  12811. ["Previous" outline-previous-visible-heading t]
  12812. ["Next Same Level" outline-forward-same-level t]
  12813. ["Previous Same Level" outline-backward-same-level t]
  12814. "--"
  12815. ["Jump" org-goto t])
  12816. ("Edit Structure"
  12817. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12818. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12819. "--"
  12820. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12821. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12822. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12823. "--"
  12824. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12825. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12826. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12827. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12828. "--"
  12829. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12830. "--"
  12831. ["Convert to odd levels" org-convert-to-odd-levels t]
  12832. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12833. ("Editing"
  12834. ["Emphasis..." org-emphasize t]
  12835. ["Edit Source Example" org-edit-special t]
  12836. "--"
  12837. ["Footnote new/jump" org-footnote-action t]
  12838. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12839. ("Archive"
  12840. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12841. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12842. ; :active t :keys "C-u C-c C-x C-a"]
  12843. ["Sparse trees open ARCHIVE trees"
  12844. (setq org-sparse-tree-open-archived-trees
  12845. (not org-sparse-tree-open-archived-trees))
  12846. :style toggle :selected org-sparse-tree-open-archived-trees]
  12847. ["Cycling opens ARCHIVE trees"
  12848. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12849. :style toggle :selected org-cycle-open-archived-trees]
  12850. "--"
  12851. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12852. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12853. ; ["Check and Move Children" (org-archive-subtree '(4))
  12854. ; :active t :keys "C-u C-c C-x C-s"]
  12855. )
  12856. "--"
  12857. ("TODO Lists"
  12858. ["TODO/DONE/-" org-todo t]
  12859. ("Select keyword"
  12860. ["Next keyword" org-shiftright (org-on-heading-p)]
  12861. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12862. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12863. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12864. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12865. ["Show TODO Tree" org-show-todo-tree t]
  12866. ["Global TODO list" org-todo-list t]
  12867. "--"
  12868. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12869. :selected org-enforce-todo-dependencies :style toggle :active t]
  12870. "Settings for tree at point"
  12871. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12872. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12873. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12874. ["Do Children parallel" org-toggle-ordered-property :style radio
  12875. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12876. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12877. "--"
  12878. ["Set Priority" org-priority t]
  12879. ["Priority Up" org-shiftup t]
  12880. ["Priority Down" org-shiftdown t])
  12881. ("TAGS and Properties"
  12882. ["Set Tags" org-set-tags-command t]
  12883. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12884. "--"
  12885. ["Set property" org-set-property t]
  12886. ["Column view of properties" org-columns t]
  12887. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12888. ("Dates and Scheduling"
  12889. ["Timestamp" org-time-stamp t]
  12890. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12891. ("Change Date"
  12892. ["1 Day Later" org-shiftright t]
  12893. ["1 Day Earlier" org-shiftleft t]
  12894. ["1 ... Later" org-shiftup t]
  12895. ["1 ... Earlier" org-shiftdown t])
  12896. ["Compute Time Range" org-evaluate-time-range t]
  12897. ["Schedule Item" org-schedule t]
  12898. ["Deadline" org-deadline t]
  12899. "--"
  12900. ["Custom time format" org-toggle-time-stamp-overlays
  12901. :style radio :selected org-display-custom-times]
  12902. "--"
  12903. ["Goto Calendar" org-goto-calendar t]
  12904. ["Date from Calendar" org-date-from-calendar t]
  12905. "--"
  12906. ["Start/Restart Timer" org-timer-start t]
  12907. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12908. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12909. ["Insert Timer String" org-timer t]
  12910. ["Insert Timer Item" org-timer-item t])
  12911. ("Logging work"
  12912. ["Clock in" org-clock-in t]
  12913. ["Clock out" org-clock-out t]
  12914. ["Clock cancel" org-clock-cancel t]
  12915. ["Goto running clock" org-clock-goto t]
  12916. ["Display times" org-clock-display t]
  12917. ["Create clock table" org-clock-report t]
  12918. "--"
  12919. ["Record DONE time"
  12920. (progn (setq org-log-done (not org-log-done))
  12921. (message "Switching to %s will %s record a timestamp"
  12922. (car org-done-keywords)
  12923. (if org-log-done "automatically" "not")))
  12924. :style toggle :selected org-log-done])
  12925. "--"
  12926. ["Agenda Command..." org-agenda t]
  12927. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12928. ("File List for Agenda")
  12929. ("Special views current file"
  12930. ["TODO Tree" org-show-todo-tree t]
  12931. ["Check Deadlines" org-check-deadlines t]
  12932. ["Timeline" org-timeline t]
  12933. ["Tags Tree" org-tags-sparse-tree t])
  12934. "--"
  12935. ("Hyperlinks"
  12936. ["Store Link (Global)" org-store-link t]
  12937. ["Insert Link" org-insert-link t]
  12938. ["Follow Link" org-open-at-point t]
  12939. "--"
  12940. ["Next link" org-next-link t]
  12941. ["Previous link" org-previous-link t]
  12942. "--"
  12943. ["Descriptive Links"
  12944. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12945. :style radio
  12946. :selected (member '(org-link) buffer-invisibility-spec)]
  12947. ["Literal Links"
  12948. (progn
  12949. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12950. :style radio
  12951. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12952. "--"
  12953. ["Export/Publish..." org-export t]
  12954. ("LaTeX"
  12955. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12956. :selected org-cdlatex-mode]
  12957. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12958. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12959. ["Modify math symbol" org-cdlatex-math-modify
  12960. (org-inside-LaTeX-fragment-p)]
  12961. ["Export LaTeX fragments as images"
  12962. (if (featurep 'org-exp)
  12963. (setq org-export-with-LaTeX-fragments
  12964. (not org-export-with-LaTeX-fragments))
  12965. (require 'org-exp))
  12966. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12967. org-export-with-LaTeX-fragments)])
  12968. "--"
  12969. ("Documentation"
  12970. ["Show Version" org-version t]
  12971. ["Info Documentation" org-info t])
  12972. ("Customize"
  12973. ["Browse Org Group" org-customize t]
  12974. "--"
  12975. ["Expand This Menu" org-create-customize-menu
  12976. (fboundp 'customize-menu-create)])
  12977. "--"
  12978. ["Refresh setup" org-mode-restart t]
  12979. ))
  12980. (defun org-info (&optional node)
  12981. "Read documentation for Org-mode in the info system.
  12982. With optional NODE, go directly to that node."
  12983. (interactive)
  12984. (info (format "(org)%s" (or node ""))))
  12985. (defun org-install-agenda-files-menu ()
  12986. (let ((bl (buffer-list)))
  12987. (save-excursion
  12988. (while bl
  12989. (set-buffer (pop bl))
  12990. (if (org-mode-p) (setq bl nil)))
  12991. (when (org-mode-p)
  12992. (easy-menu-change
  12993. '("Org") "File List for Agenda"
  12994. (append
  12995. (list
  12996. ["Edit File List" (org-edit-agenda-file-list) t]
  12997. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12998. ["Remove Current File from List" org-remove-file t]
  12999. ["Cycle through agenda files" org-cycle-agenda-files t]
  13000. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13001. "--")
  13002. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13003. ;;;; Documentation
  13004. ;;;###autoload
  13005. (defun org-require-autoloaded-modules ()
  13006. (interactive)
  13007. (mapc 'require
  13008. '(org-agenda org-archive org-attach org-clock org-colview
  13009. org-exp org-id org-export-latex org-publish
  13010. org-remember org-table org-timer)))
  13011. ;;;###autoload
  13012. (defun org-customize ()
  13013. "Call the customize function with org as argument."
  13014. (interactive)
  13015. (org-load-modules-maybe)
  13016. (org-require-autoloaded-modules)
  13017. (customize-browse 'org))
  13018. (defun org-create-customize-menu ()
  13019. "Create a full customization menu for Org-mode, insert it into the menu."
  13020. (interactive)
  13021. (org-load-modules-maybe)
  13022. (org-require-autoloaded-modules)
  13023. (if (fboundp 'customize-menu-create)
  13024. (progn
  13025. (easy-menu-change
  13026. '("Org") "Customize"
  13027. `(["Browse Org group" org-customize t]
  13028. "--"
  13029. ,(customize-menu-create 'org)
  13030. ["Set" Custom-set t]
  13031. ["Save" Custom-save t]
  13032. ["Reset to Current" Custom-reset-current t]
  13033. ["Reset to Saved" Custom-reset-saved t]
  13034. ["Reset to Standard Settings" Custom-reset-standard t]))
  13035. (message "\"Org\"-menu now contains full customization menu"))
  13036. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13037. ;;;; Miscellaneous stuff
  13038. ;;; Generally useful functions
  13039. (defun org-find-text-property-in-string (prop s)
  13040. "Return the first non-nil value of property PROP in string S."
  13041. (or (get-text-property 0 prop s)
  13042. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13043. prop s)))
  13044. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13045. "Display the given MESSAGE as a warning."
  13046. (if (fboundp 'display-warning)
  13047. (display-warning 'org message
  13048. (if (featurep 'xemacs)
  13049. 'warning
  13050. :warning))
  13051. (let ((buf (get-buffer-create "*Org warnings*")))
  13052. (with-current-buffer buf
  13053. (goto-char (point-max))
  13054. (insert "Warning (Org): " message)
  13055. (unless (bolp)
  13056. (newline)))
  13057. (display-buffer buf)
  13058. (sit-for 0))))
  13059. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13060. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13061. (if (and marker (marker-buffer marker)
  13062. (buffer-live-p (marker-buffer marker)))
  13063. (progn
  13064. (switch-to-buffer (marker-buffer marker))
  13065. (if (or (> marker (point-max)) (< marker (point-min)))
  13066. (widen))
  13067. (goto-char marker)
  13068. (org-show-context 'org-goto))
  13069. (if bookmark
  13070. (bookmark-jump bookmark)
  13071. (error "Cannot find location"))))
  13072. (defun org-quote-csv-field (s)
  13073. "Quote field for inclusion in CSV material."
  13074. (if (string-match "[\",]" s)
  13075. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13076. s))
  13077. (defun org-plist-delete (plist property)
  13078. "Delete PROPERTY from PLIST.
  13079. This is in contrast to merely setting it to 0."
  13080. (let (p)
  13081. (while plist
  13082. (if (not (eq property (car plist)))
  13083. (setq p (plist-put p (car plist) (nth 1 plist))))
  13084. (setq plist (cddr plist)))
  13085. p))
  13086. (defun org-force-self-insert (N)
  13087. "Needed to enforce self-insert under remapping."
  13088. (interactive "p")
  13089. (self-insert-command N))
  13090. (defun org-string-width (s)
  13091. "Compute width of string, ignoring invisible characters.
  13092. This ignores character with invisibility property `org-link', and also
  13093. characters with property `org-cwidth', because these will become invisible
  13094. upon the next fontification round."
  13095. (let (b l)
  13096. (when (or (eq t buffer-invisibility-spec)
  13097. (assq 'org-link buffer-invisibility-spec))
  13098. (while (setq b (text-property-any 0 (length s)
  13099. 'invisible 'org-link s))
  13100. (setq s (concat (substring s 0 b)
  13101. (substring s (or (next-single-property-change
  13102. b 'invisible s) (length s)))))))
  13103. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13104. (setq s (concat (substring s 0 b)
  13105. (substring s (or (next-single-property-change
  13106. b 'org-cwidth s) (length s))))))
  13107. (setq l (string-width s) b -1)
  13108. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13109. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13110. l))
  13111. (defun org-get-indentation (&optional line)
  13112. "Get the indentation of the current line, interpreting tabs.
  13113. When LINE is given, assume it represents a line and compute its indentation."
  13114. (if line
  13115. (if (string-match "^ *" (org-remove-tabs line))
  13116. (match-end 0))
  13117. (save-excursion
  13118. (beginning-of-line 1)
  13119. (skip-chars-forward " \t")
  13120. (current-column))))
  13121. (defun org-remove-tabs (s &optional width)
  13122. "Replace tabulators in S with spaces.
  13123. Assumes that s is a single line, starting in column 0."
  13124. (setq width (or width tab-width))
  13125. (while (string-match "\t" s)
  13126. (setq s (replace-match
  13127. (make-string
  13128. (- (* width (/ (+ (match-beginning 0) width) width))
  13129. (match-beginning 0)) ?\ )
  13130. t t s)))
  13131. s)
  13132. (defun org-fix-indentation (line ind)
  13133. "Fix indentation in LINE.
  13134. IND is a cons cell with target and minimum indentation.
  13135. If the current indentation in LINE is smaller than the minimum,
  13136. leave it alone. If it is larger than ind, set it to the target."
  13137. (let* ((l (org-remove-tabs line))
  13138. (i (org-get-indentation l))
  13139. (i1 (car ind)) (i2 (cdr ind)))
  13140. (if (>= i i2) (setq l (substring line i2)))
  13141. (if (> i1 0)
  13142. (concat (make-string i1 ?\ ) l)
  13143. l)))
  13144. (defun org-base-buffer (buffer)
  13145. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13146. (if (not buffer)
  13147. buffer
  13148. (or (buffer-base-buffer buffer)
  13149. buffer)))
  13150. (defun org-trim (s)
  13151. "Remove whitespace at beginning and end of string."
  13152. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13153. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13154. s)
  13155. (defun org-wrap (string &optional width lines)
  13156. "Wrap string to either a number of lines, or a width in characters.
  13157. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13158. that costs. If there is a word longer than WIDTH, the text is actually
  13159. wrapped to the length of that word.
  13160. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13161. many lines, whatever width that takes.
  13162. The return value is a list of lines, without newlines at the end."
  13163. (let* ((words (org-split-string string "[ \t\n]+"))
  13164. (maxword (apply 'max (mapcar 'org-string-width words)))
  13165. w ll)
  13166. (cond (width
  13167. (org-do-wrap words (max maxword width)))
  13168. (lines
  13169. (setq w maxword)
  13170. (setq ll (org-do-wrap words maxword))
  13171. (if (<= (length ll) lines)
  13172. ll
  13173. (setq ll words)
  13174. (while (> (length ll) lines)
  13175. (setq w (1+ w))
  13176. (setq ll (org-do-wrap words w)))
  13177. ll))
  13178. (t (error "Cannot wrap this")))))
  13179. (defun org-do-wrap (words width)
  13180. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13181. (let (lines line)
  13182. (while words
  13183. (setq line (pop words))
  13184. (while (and words (< (+ (length line) (length (car words))) width))
  13185. (setq line (concat line " " (pop words))))
  13186. (setq lines (push line lines)))
  13187. (nreverse lines)))
  13188. (defun org-split-string (string &optional separators)
  13189. "Splits STRING into substrings at SEPARATORS.
  13190. No empty strings are returned if there are matches at the beginning
  13191. and end of string."
  13192. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13193. (start 0)
  13194. notfirst
  13195. (list nil))
  13196. (while (and (string-match rexp string
  13197. (if (and notfirst
  13198. (= start (match-beginning 0))
  13199. (< start (length string)))
  13200. (1+ start) start))
  13201. (< (match-beginning 0) (length string)))
  13202. (setq notfirst t)
  13203. (or (eq (match-beginning 0) 0)
  13204. (and (eq (match-beginning 0) (match-end 0))
  13205. (eq (match-beginning 0) start))
  13206. (setq list
  13207. (cons (substring string start (match-beginning 0))
  13208. list)))
  13209. (setq start (match-end 0)))
  13210. (or (eq start (length string))
  13211. (setq list
  13212. (cons (substring string start)
  13213. list)))
  13214. (nreverse list)))
  13215. (defun org-context ()
  13216. "Return a list of contexts of the current cursor position.
  13217. If several contexts apply, all are returned.
  13218. Each context entry is a list with a symbol naming the context, and
  13219. two positions indicating start and end of the context. Possible
  13220. contexts are:
  13221. :headline anywhere in a headline
  13222. :headline-stars on the leading stars in a headline
  13223. :todo-keyword on a TODO keyword (including DONE) in a headline
  13224. :tags on the TAGS in a headline
  13225. :priority on the priority cookie in a headline
  13226. :item on the first line of a plain list item
  13227. :item-bullet on the bullet/number of a plain list item
  13228. :checkbox on the checkbox in a plain list item
  13229. :table in an org-mode table
  13230. :table-special on a special filed in a table
  13231. :table-table in a table.el table
  13232. :link on a hyperlink
  13233. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13234. :target on a <<target>>
  13235. :radio-target on a <<<radio-target>>>
  13236. :latex-fragment on a LaTeX fragment
  13237. :latex-preview on a LaTeX fragment with overlayed preview image
  13238. This function expects the position to be visible because it uses font-lock
  13239. faces as a help to recognize the following contexts: :table-special, :link,
  13240. and :keyword."
  13241. (let* ((f (get-text-property (point) 'face))
  13242. (faces (if (listp f) f (list f)))
  13243. (p (point)) clist o)
  13244. ;; First the large context
  13245. (cond
  13246. ((org-on-heading-p t)
  13247. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13248. (when (progn
  13249. (beginning-of-line 1)
  13250. (looking-at org-todo-line-tags-regexp))
  13251. (push (org-point-in-group p 1 :headline-stars) clist)
  13252. (push (org-point-in-group p 2 :todo-keyword) clist)
  13253. (push (org-point-in-group p 4 :tags) clist))
  13254. (goto-char p)
  13255. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13256. (if (looking-at "\\[#[A-Z0-9]\\]")
  13257. (push (org-point-in-group p 0 :priority) clist)))
  13258. ((org-at-item-p)
  13259. (push (org-point-in-group p 2 :item-bullet) clist)
  13260. (push (list :item (point-at-bol)
  13261. (save-excursion (org-end-of-item) (point)))
  13262. clist)
  13263. (and (org-at-item-checkbox-p)
  13264. (push (org-point-in-group p 0 :checkbox) clist)))
  13265. ((org-at-table-p)
  13266. (push (list :table (org-table-begin) (org-table-end)) clist)
  13267. (if (memq 'org-formula faces)
  13268. (push (list :table-special
  13269. (previous-single-property-change p 'face)
  13270. (next-single-property-change p 'face)) clist)))
  13271. ((org-at-table-p 'any)
  13272. (push (list :table-table) clist)))
  13273. (goto-char p)
  13274. ;; Now the small context
  13275. (cond
  13276. ((org-at-timestamp-p)
  13277. (push (org-point-in-group p 0 :timestamp) clist))
  13278. ((memq 'org-link faces)
  13279. (push (list :link
  13280. (previous-single-property-change p 'face)
  13281. (next-single-property-change p 'face)) clist))
  13282. ((memq 'org-special-keyword faces)
  13283. (push (list :keyword
  13284. (previous-single-property-change p 'face)
  13285. (next-single-property-change p 'face)) clist))
  13286. ((org-on-target-p)
  13287. (push (org-point-in-group p 0 :target) clist)
  13288. (goto-char (1- (match-beginning 0)))
  13289. (if (looking-at org-radio-target-regexp)
  13290. (push (org-point-in-group p 0 :radio-target) clist))
  13291. (goto-char p))
  13292. ((setq o (car (delq nil
  13293. (mapcar
  13294. (lambda (x)
  13295. (if (memq x org-latex-fragment-image-overlays) x))
  13296. (org-overlays-at (point))))))
  13297. (push (list :latex-fragment
  13298. (org-overlay-start o) (org-overlay-end o)) clist)
  13299. (push (list :latex-preview
  13300. (org-overlay-start o) (org-overlay-end o)) clist))
  13301. ((org-inside-LaTeX-fragment-p)
  13302. ;; FIXME: positions wrong.
  13303. (push (list :latex-fragment (point) (point)) clist)))
  13304. (setq clist (nreverse (delq nil clist)))
  13305. clist))
  13306. ;; FIXME: Compare with at-regexp-p Do we need both?
  13307. (defun org-in-regexp (re &optional nlines visually)
  13308. "Check if point is inside a match of regexp.
  13309. Normally only the current line is checked, but you can include NLINES extra
  13310. lines both before and after point into the search.
  13311. If VISUALLY is set, require that the cursor is not after the match but
  13312. really on, so that the block visually is on the match."
  13313. (catch 'exit
  13314. (let ((pos (point))
  13315. (eol (point-at-eol (+ 1 (or nlines 0))))
  13316. (inc (if visually 1 0)))
  13317. (save-excursion
  13318. (beginning-of-line (- 1 (or nlines 0)))
  13319. (while (re-search-forward re eol t)
  13320. (if (and (<= (match-beginning 0) pos)
  13321. (>= (+ inc (match-end 0)) pos))
  13322. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13323. (defun org-at-regexp-p (regexp)
  13324. "Is point inside a match of REGEXP in the current line?"
  13325. (catch 'exit
  13326. (save-excursion
  13327. (let ((pos (point)) (end (point-at-eol)))
  13328. (beginning-of-line 1)
  13329. (while (re-search-forward regexp end t)
  13330. (if (and (<= (match-beginning 0) pos)
  13331. (>= (match-end 0) pos))
  13332. (throw 'exit t)))
  13333. nil))))
  13334. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13335. "Call `multi-occur' with buffers for all agenda files."
  13336. (interactive "sOrg-files matching: \np")
  13337. (let* ((files (org-agenda-files))
  13338. (tnames (mapcar 'file-truename files))
  13339. (extra org-agenda-text-search-extra-files)
  13340. f)
  13341. (when (eq (car extra) 'agenda-archives)
  13342. (setq extra (cdr extra))
  13343. (setq files (org-add-archive-files files)))
  13344. (while (setq f (pop extra))
  13345. (unless (member (file-truename f) tnames)
  13346. (add-to-list 'files f 'append)
  13347. (add-to-list 'tnames (file-truename f) 'append)))
  13348. (multi-occur
  13349. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13350. regexp)))
  13351. (if (boundp 'occur-mode-find-occurrence-hook)
  13352. ;; Emacs 23
  13353. (add-hook 'occur-mode-find-occurrence-hook
  13354. (lambda ()
  13355. (when (org-mode-p)
  13356. (org-reveal))))
  13357. ;; Emacs 22
  13358. (defadvice occur-mode-goto-occurrence
  13359. (after org-occur-reveal activate)
  13360. (and (org-mode-p) (org-reveal)))
  13361. (defadvice occur-mode-goto-occurrence-other-window
  13362. (after org-occur-reveal activate)
  13363. (and (org-mode-p) (org-reveal)))
  13364. (defadvice occur-mode-display-occurrence
  13365. (after org-occur-reveal activate)
  13366. (when (org-mode-p)
  13367. (let ((pos (occur-mode-find-occurrence)))
  13368. (with-current-buffer (marker-buffer pos)
  13369. (save-excursion
  13370. (goto-char pos)
  13371. (org-reveal)))))))
  13372. (defun org-uniquify (list)
  13373. "Remove duplicate elements from LIST."
  13374. (let (res)
  13375. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13376. res))
  13377. (defun org-delete-all (elts list)
  13378. "Remove all elements in ELTS from LIST."
  13379. (while elts
  13380. (setq list (delete (pop elts) list)))
  13381. list)
  13382. (defun org-back-over-empty-lines ()
  13383. "Move backwards over whitespace, to the beginning of the first empty line.
  13384. Returns the number of empty lines passed."
  13385. (let ((pos (point)))
  13386. (skip-chars-backward " \t\n\r")
  13387. (beginning-of-line 2)
  13388. (goto-char (min (point) pos))
  13389. (count-lines (point) pos)))
  13390. (defun org-skip-whitespace ()
  13391. (skip-chars-forward " \t\n\r"))
  13392. (defun org-point-in-group (point group &optional context)
  13393. "Check if POINT is in match-group GROUP.
  13394. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13395. match. If the match group does ot exist or point is not inside it,
  13396. return nil."
  13397. (and (match-beginning group)
  13398. (>= point (match-beginning group))
  13399. (<= point (match-end group))
  13400. (if context
  13401. (list context (match-beginning group) (match-end group))
  13402. t)))
  13403. (defun org-switch-to-buffer-other-window (&rest args)
  13404. "Switch to buffer in a second window on the current frame.
  13405. In particular, do not allow pop-up frames."
  13406. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13407. special-display-function)
  13408. (apply 'switch-to-buffer-other-window args)))
  13409. (defun org-combine-plists (&rest plists)
  13410. "Create a single property list from all plists in PLISTS.
  13411. The process starts by copying the first list, and then setting properties
  13412. from the other lists. Settings in the last list are the most significant
  13413. ones and overrule settings in the other lists."
  13414. (let ((rtn (copy-sequence (pop plists)))
  13415. p v ls)
  13416. (while plists
  13417. (setq ls (pop plists))
  13418. (while ls
  13419. (setq p (pop ls) v (pop ls))
  13420. (setq rtn (plist-put rtn p v))))
  13421. rtn))
  13422. (defun org-move-line-down (arg)
  13423. "Move the current line down. With prefix argument, move it past ARG lines."
  13424. (interactive "p")
  13425. (let ((col (current-column))
  13426. beg end pos)
  13427. (beginning-of-line 1) (setq beg (point))
  13428. (beginning-of-line 2) (setq end (point))
  13429. (beginning-of-line (+ 1 arg))
  13430. (setq pos (move-marker (make-marker) (point)))
  13431. (insert (delete-and-extract-region beg end))
  13432. (goto-char pos)
  13433. (org-move-to-column col)))
  13434. (defun org-move-line-up (arg)
  13435. "Move the current line up. With prefix argument, move it past ARG lines."
  13436. (interactive "p")
  13437. (let ((col (current-column))
  13438. beg end pos)
  13439. (beginning-of-line 1) (setq beg (point))
  13440. (beginning-of-line 2) (setq end (point))
  13441. (beginning-of-line (- arg))
  13442. (setq pos (move-marker (make-marker) (point)))
  13443. (insert (delete-and-extract-region beg end))
  13444. (goto-char pos)
  13445. (org-move-to-column col)))
  13446. (defun org-replace-escapes (string table)
  13447. "Replace %-escapes in STRING with values in TABLE.
  13448. TABLE is an association list with keys like \"%a\" and string values.
  13449. The sequences in STRING may contain normal field width and padding information,
  13450. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13451. so values can contain further %-escapes if they are define later in TABLE."
  13452. (let ((case-fold-search nil)
  13453. e re rpl)
  13454. (while (setq e (pop table))
  13455. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13456. (while (string-match re string)
  13457. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13458. (cdr e)))
  13459. (setq string (replace-match rpl t t string))))
  13460. string))
  13461. (defun org-sublist (list start end)
  13462. "Return a section of LIST, from START to END.
  13463. Counting starts at 1."
  13464. (let (rtn (c start))
  13465. (setq list (nthcdr (1- start) list))
  13466. (while (and list (<= c end))
  13467. (push (pop list) rtn)
  13468. (setq c (1+ c)))
  13469. (nreverse rtn)))
  13470. (defun org-find-base-buffer-visiting (file)
  13471. "Like `find-buffer-visiting' but alway return the base buffer and
  13472. not an indirect buffer."
  13473. (let ((buf (find-buffer-visiting file)))
  13474. (if buf
  13475. (or (buffer-base-buffer buf) buf)
  13476. nil)))
  13477. (defun org-image-file-name-regexp (&optional extensions)
  13478. "Return regexp matching the file names of images.
  13479. If EXTENSIONS is given, only match these."
  13480. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13481. (image-file-name-regexp)
  13482. (let ((image-file-name-extensions
  13483. (or extensions
  13484. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13485. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13486. (concat "\\."
  13487. (regexp-opt (nconc (mapcar 'upcase
  13488. image-file-name-extensions)
  13489. image-file-name-extensions)
  13490. t)
  13491. "\\'"))))
  13492. (defun org-file-image-p (file &optional extensions)
  13493. "Return non-nil if FILE is an image."
  13494. (save-match-data
  13495. (string-match (org-image-file-name-regexp extensions) file)))
  13496. (defun org-get-cursor-date ()
  13497. "Return the date at cursor in as a time.
  13498. This works in the calendar and in the agenda, anywhere else it just
  13499. returns the current time."
  13500. (let (date day defd)
  13501. (cond
  13502. ((eq major-mode 'calendar-mode)
  13503. (setq date (calendar-cursor-to-date)
  13504. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13505. ((eq major-mode 'org-agenda-mode)
  13506. (setq day (get-text-property (point) 'day))
  13507. (if day
  13508. (setq date (calendar-gregorian-from-absolute day)
  13509. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13510. (nth 2 date))))))
  13511. (or defd (current-time))))
  13512. (defvar org-agenda-action-marker (make-marker)
  13513. "Marker pointing to the entry for the next agenda action.")
  13514. (defun org-mark-entry-for-agenda-action ()
  13515. "Mark the current entry as target of an agenda action.
  13516. Agenda actions are actions executed from the agenda with the key `k',
  13517. which make use of the date at the cursor."
  13518. (interactive)
  13519. (move-marker org-agenda-action-marker
  13520. (save-excursion (org-back-to-heading t) (point))
  13521. (current-buffer))
  13522. (message
  13523. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13524. ;;; Paragraph filling stuff.
  13525. ;; We want this to be just right, so use the full arsenal.
  13526. (defun org-indent-line-function ()
  13527. "Indent line like previous, but further if previous was headline or item."
  13528. (interactive)
  13529. (let* ((pos (point))
  13530. (itemp (org-at-item-p))
  13531. column bpos bcol tpos tcol bullet btype bullet-type)
  13532. ;; Find the previous relevant line
  13533. (beginning-of-line 1)
  13534. (cond
  13535. ((looking-at "#") (setq column 0))
  13536. ((looking-at "\\*+ ") (setq column 0))
  13537. ((and (looking-at "[ \t]*:END:")
  13538. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13539. (save-excursion
  13540. (goto-char (1- (match-beginning 1)))
  13541. (setq column (current-column))))
  13542. (t
  13543. (beginning-of-line 0)
  13544. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13545. (not (looking-at "[ \t]*:END:"))
  13546. (not (looking-at org-drawer-regexp)))
  13547. (beginning-of-line 0))
  13548. (cond
  13549. ((looking-at "\\*+[ \t]+")
  13550. (if (not org-adapt-indentation)
  13551. (setq column 0)
  13552. (goto-char (match-end 0))
  13553. (setq column (current-column))))
  13554. ((looking-at org-drawer-regexp)
  13555. (goto-char (1- (match-beginning 1)))
  13556. (setq column (current-column)))
  13557. ((looking-at "\\([ \t]*\\):END:")
  13558. (goto-char (match-end 1))
  13559. (setq column (current-column)))
  13560. ((org-in-item-p)
  13561. (org-beginning-of-item)
  13562. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13563. (setq bpos (match-beginning 1) tpos (match-end 0)
  13564. bcol (progn (goto-char bpos) (current-column))
  13565. tcol (progn (goto-char tpos) (current-column))
  13566. bullet (match-string 1)
  13567. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13568. (if (> tcol (+ bcol org-description-max-indent))
  13569. (setq tcol (+ bcol 5)))
  13570. (if (not itemp)
  13571. (setq column tcol)
  13572. (goto-char pos)
  13573. (beginning-of-line 1)
  13574. (if (looking-at "\\S-")
  13575. (progn
  13576. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13577. (setq bullet (match-string 1)
  13578. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13579. (setq column (if (equal btype bullet-type) bcol tcol)))
  13580. (setq column (org-get-indentation)))))
  13581. (t (setq column (org-get-indentation))))))
  13582. (goto-char pos)
  13583. (if (<= (current-column) (current-indentation))
  13584. (org-indent-line-to column)
  13585. (save-excursion (org-indent-line-to column)))
  13586. (setq column (current-column))
  13587. (beginning-of-line 1)
  13588. (if (looking-at
  13589. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13590. (replace-match (concat "\\1" (format org-property-format
  13591. (match-string 2) (match-string 3)))
  13592. t nil))
  13593. (org-move-to-column column)))
  13594. (defun org-set-autofill-regexps ()
  13595. (interactive)
  13596. ;; In the paragraph separator we include headlines, because filling
  13597. ;; text in a line directly attached to a headline would otherwise
  13598. ;; fill the headline as well.
  13599. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13600. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13601. ;; The paragraph starter includes hand-formatted lists.
  13602. (org-set-local 'paragraph-start
  13603. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13604. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13605. ;; But only if the user has not turned off tables or fixed-width regions
  13606. (org-set-local
  13607. 'auto-fill-inhibit-regexp
  13608. (concat "\\*+ \\|#\\+"
  13609. "\\|[ \t]*" org-keyword-time-regexp
  13610. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13611. (concat
  13612. "\\|[ \t]*["
  13613. (if org-enable-table-editor "|" "")
  13614. (if org-enable-fixed-width-editor ":" "")
  13615. "]"))))
  13616. ;; We use our own fill-paragraph function, to make sure that tables
  13617. ;; and fixed-width regions are not wrapped. That function will pass
  13618. ;; through to `fill-paragraph' when appropriate.
  13619. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13620. ; Adaptive filling: To get full control, first make sure that
  13621. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13622. (org-set-local 'adaptive-fill-regexp "\000")
  13623. (org-set-local 'adaptive-fill-function
  13624. 'org-adaptive-fill-function)
  13625. (org-set-local
  13626. 'align-mode-rules-list
  13627. '((org-in-buffer-settings
  13628. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13629. (modes . '(org-mode))))))
  13630. (defun org-fill-paragraph (&optional justify)
  13631. "Re-align a table, pass through to fill-paragraph if no table."
  13632. (let ((table-p (org-at-table-p))
  13633. (table.el-p (org-at-table.el-p)))
  13634. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13635. (save-excursion (goto-char (point-at-bol))
  13636. (looking-at outline-regexp)))
  13637. t) ; skip headlines
  13638. (table.el-p t) ; skip table.el tables
  13639. (table-p (org-table-align) t) ; align org-mode tables
  13640. (t nil)))) ; call paragraph-fill
  13641. ;; For reference, this is the default value of adaptive-fill-regexp
  13642. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13643. (defun org-adaptive-fill-function ()
  13644. "Return a fill prefix for org-mode files.
  13645. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13646. work correctly."
  13647. (cond ((looking-at "#[ \t]+")
  13648. (match-string 0))
  13649. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13650. (save-excursion
  13651. (if (> (match-end 1) (+ (match-beginning 1)
  13652. org-description-max-indent))
  13653. (goto-char (+ (match-beginning 1) 5))
  13654. (goto-char (match-end 0)))
  13655. (make-string (current-column) ?\ )))
  13656. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13657. (save-excursion
  13658. (goto-char (match-end 0))
  13659. (make-string (current-column) ?\ )))
  13660. (t nil)))
  13661. ;;; Other stuff.
  13662. (defun org-toggle-fixed-width-section (arg)
  13663. "Toggle the fixed-width export.
  13664. If there is no active region, the QUOTE keyword at the current headline is
  13665. inserted or removed. When present, it causes the text between this headline
  13666. and the next to be exported as fixed-width text, and unmodified.
  13667. If there is an active region, this command adds or removes a colon as the
  13668. first character of this line. If the first character of a line is a colon,
  13669. this line is also exported in fixed-width font."
  13670. (interactive "P")
  13671. (let* ((cc 0)
  13672. (regionp (org-region-active-p))
  13673. (beg (if regionp (region-beginning) (point)))
  13674. (end (if regionp (region-end)))
  13675. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13676. (case-fold-search nil)
  13677. (re "[ \t]*\\(:\\)")
  13678. off)
  13679. (if regionp
  13680. (save-excursion
  13681. (goto-char beg)
  13682. (setq cc (current-column))
  13683. (beginning-of-line 1)
  13684. (setq off (looking-at re))
  13685. (while (> nlines 0)
  13686. (setq nlines (1- nlines))
  13687. (beginning-of-line 1)
  13688. (cond
  13689. (arg
  13690. (org-move-to-column cc t)
  13691. (insert ":\n")
  13692. (forward-line -1))
  13693. ((and off (looking-at re))
  13694. (replace-match "" t t nil 1))
  13695. ((not off) (org-move-to-column cc t) (insert ":")))
  13696. (forward-line 1)))
  13697. (save-excursion
  13698. (org-back-to-heading)
  13699. (if (looking-at (concat outline-regexp
  13700. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13701. (replace-match "" t t nil 1)
  13702. (if (looking-at outline-regexp)
  13703. (progn
  13704. (goto-char (match-end 0))
  13705. (insert org-quote-string " "))))))))
  13706. ;;;; Functions extending outline functionality
  13707. (defun org-beginning-of-line (&optional arg)
  13708. "Go to the beginning of the current line. If that is invisible, continue
  13709. to a visible line beginning. This makes the function of C-a more intuitive.
  13710. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13711. first attempt, and only move to after the tags when the cursor is already
  13712. beyond the end of the headline."
  13713. (interactive "P")
  13714. (let ((pos (point)) refpos)
  13715. (beginning-of-line 1)
  13716. (if (and arg (fboundp 'move-beginning-of-line))
  13717. (call-interactively 'move-beginning-of-line)
  13718. (if (bobp)
  13719. nil
  13720. (backward-char 1)
  13721. (if (org-invisible-p)
  13722. (while (and (not (bobp)) (org-invisible-p))
  13723. (backward-char 1)
  13724. (beginning-of-line 1))
  13725. (forward-char 1))))
  13726. (when org-special-ctrl-a/e
  13727. (cond
  13728. ((and (looking-at org-complex-heading-regexp)
  13729. (= (char-after (match-end 1)) ?\ ))
  13730. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13731. (point-at-eol)))
  13732. (goto-char
  13733. (if (eq org-special-ctrl-a/e t)
  13734. (cond ((> pos refpos) refpos)
  13735. ((= pos (point)) refpos)
  13736. (t (point)))
  13737. (cond ((> pos (point)) (point))
  13738. ((not (eq last-command this-command)) (point))
  13739. (t refpos)))))
  13740. ((org-at-item-p)
  13741. (goto-char
  13742. (if (eq org-special-ctrl-a/e t)
  13743. (cond ((> pos (match-end 4)) (match-end 4))
  13744. ((= pos (point)) (match-end 4))
  13745. (t (point)))
  13746. (cond ((> pos (point)) (point))
  13747. ((not (eq last-command this-command)) (point))
  13748. (t (match-end 4))))))))
  13749. (org-no-warnings
  13750. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13751. (defun org-end-of-line (&optional arg)
  13752. "Go to the end of the line.
  13753. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13754. first attempt, and only move to after the tags when the cursor is already
  13755. beyond the end of the headline."
  13756. (interactive "P")
  13757. (if (or (not org-special-ctrl-a/e)
  13758. (not (org-on-heading-p))
  13759. arg)
  13760. (call-interactively (if (fboundp 'move-end-of-line)
  13761. 'move-end-of-line
  13762. 'end-of-line))
  13763. (let ((pos (point)))
  13764. (beginning-of-line 1)
  13765. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13766. (if (eq org-special-ctrl-a/e t)
  13767. (if (or (< pos (match-beginning 1))
  13768. (= pos (match-end 0)))
  13769. (goto-char (match-beginning 1))
  13770. (goto-char (match-end 0)))
  13771. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13772. (goto-char (match-end 0))
  13773. (goto-char (match-beginning 1))))
  13774. (call-interactively (if (fboundp 'move-end-of-line)
  13775. 'move-end-of-line
  13776. 'end-of-line)))))
  13777. (org-no-warnings
  13778. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13779. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13780. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13781. (defun org-kill-line (&optional arg)
  13782. "Kill line, to tags or end of line."
  13783. (interactive "P")
  13784. (cond
  13785. ((or (not org-special-ctrl-k)
  13786. (bolp)
  13787. (not (org-on-heading-p)))
  13788. (call-interactively 'kill-line))
  13789. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13790. (kill-region (point) (match-beginning 1))
  13791. (org-set-tags nil t))
  13792. (t (kill-region (point) (point-at-eol)))))
  13793. (define-key org-mode-map "\C-k" 'org-kill-line)
  13794. (defun org-yank (&optional arg)
  13795. "Yank. If the kill is a subtree, treat it specially.
  13796. This command will look at the current kill and check if is a single
  13797. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13798. cursor is at the beginning of a line or after the stars of a currently
  13799. empty headline, then the yank is handled specially. How exactly depends
  13800. on the value of the following variables, both set by default.
  13801. org-yank-folded-subtrees
  13802. When set, the subtree(s) will be folded after insertion, but only
  13803. if doing so would now swallow text after the yanked text.
  13804. org-yank-adjusted-subtrees
  13805. When set, the subtree will be promoted or demoted in order to
  13806. fit into the local outline tree structure, which means that the level
  13807. will be adjusted so that it becomes the smaller one of the two
  13808. *visible* surrounding headings.
  13809. Any prefix to this command will cause `yank' to be called directly with
  13810. no special treatment. In particular, a simple `C-u' prefix will just
  13811. plainly yank the text as it is.
  13812. \[1] Basically, the test checks if the first non-white line is a heading
  13813. and if there are no other headings with fewer stars."
  13814. (interactive "P")
  13815. (setq this-command 'yank)
  13816. (if arg
  13817. (call-interactively 'yank)
  13818. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13819. (and (org-kill-is-subtree-p)
  13820. (or (bolp)
  13821. (and (looking-at "[ \t]*$")
  13822. (string-match
  13823. "\\`\\*+\\'"
  13824. (buffer-substring (point-at-bol) (point)))))))
  13825. swallowp)
  13826. (cond
  13827. ((and subtreep org-yank-folded-subtrees)
  13828. (let ((beg (point))
  13829. end)
  13830. (if (and subtreep org-yank-adjusted-subtrees)
  13831. (org-paste-subtree nil nil 'for-yank)
  13832. (call-interactively 'yank))
  13833. (setq end (point))
  13834. (goto-char beg)
  13835. (when (and (bolp) subtreep
  13836. (not (setq swallowp
  13837. (org-yank-folding-would-swallow-text beg end))))
  13838. (or (looking-at outline-regexp)
  13839. (re-search-forward (concat "^" outline-regexp) end t))
  13840. (while (and (< (point) end) (looking-at outline-regexp))
  13841. (hide-subtree)
  13842. (org-cycle-show-empty-lines 'folded)
  13843. (condition-case nil
  13844. (outline-forward-same-level 1)
  13845. (error (goto-char end)))))
  13846. (when swallowp
  13847. (message
  13848. "Yanked text not folded because that would swallow text"))
  13849. (goto-char end)
  13850. (skip-chars-forward " \t\n\r")
  13851. (beginning-of-line 1)
  13852. (push-mark beg 'nomsg)))
  13853. ((and subtreep org-yank-adjusted-subtrees)
  13854. (let ((beg (point-at-bol)))
  13855. (org-paste-subtree nil nil 'for-yank)
  13856. (push-mark beg 'nomsg)))
  13857. (t
  13858. (call-interactively 'yank))))))
  13859. (defun org-yank-folding-would-swallow-text (beg end)
  13860. "Would hide-subtree at BEG swallow any text after END?"
  13861. (let (level)
  13862. (save-excursion
  13863. (goto-char beg)
  13864. (when (or (looking-at outline-regexp)
  13865. (re-search-forward (concat "^" outline-regexp) end t))
  13866. (setq level (org-outline-level)))
  13867. (goto-char end)
  13868. (skip-chars-forward " \t\r\n\v\f")
  13869. (if (or (eobp)
  13870. (and (bolp) (looking-at org-outline-regexp)
  13871. (<= (org-outline-level) level)))
  13872. nil ; Nothing would be swallowed
  13873. t)))) ; something would swallow
  13874. (define-key org-mode-map "\C-y" 'org-yank)
  13875. (defun org-invisible-p ()
  13876. "Check if point is at a character currently not visible."
  13877. ;; Early versions of noutline don't have `outline-invisible-p'.
  13878. (if (fboundp 'outline-invisible-p)
  13879. (outline-invisible-p)
  13880. (get-char-property (point) 'invisible)))
  13881. (defun org-invisible-p2 ()
  13882. "Check if point is at a character currently not visible."
  13883. (save-excursion
  13884. (if (and (eolp) (not (bobp))) (backward-char 1))
  13885. ;; Early versions of noutline don't have `outline-invisible-p'.
  13886. (if (fboundp 'outline-invisible-p)
  13887. (outline-invisible-p)
  13888. (get-char-property (point) 'invisible))))
  13889. (defun org-back-to-heading (&optional invisible-ok)
  13890. "Call `outline-back-to-heading', but provide a better error message."
  13891. (condition-case nil
  13892. (outline-back-to-heading invisible-ok)
  13893. (error (error "Before first headline at position %d in buffer %s"
  13894. (point) (current-buffer)))))
  13895. (defun org-before-first-heading-p ()
  13896. "Before first heading?"
  13897. (save-excursion
  13898. (null (re-search-backward "^\\*+ " nil t))))
  13899. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13900. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13901. (defun org-at-heading-or-item-p ()
  13902. (or (org-on-heading-p) (org-at-item-p)))
  13903. (defun org-on-target-p ()
  13904. (or (org-in-regexp org-radio-target-regexp)
  13905. (org-in-regexp org-target-regexp)))
  13906. (defun org-up-heading-all (arg)
  13907. "Move to the heading line of which the present line is a subheading.
  13908. This function considers both visible and invisible heading lines.
  13909. With argument, move up ARG levels."
  13910. (if (fboundp 'outline-up-heading-all)
  13911. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13912. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13913. (defun org-up-heading-safe ()
  13914. "Move to the heading line of which the present line is a subheading.
  13915. This version will not throw an error. It will return the level of the
  13916. headline found, or nil if no higher level is found."
  13917. (let (start-level re)
  13918. (org-back-to-heading t)
  13919. (setq start-level (funcall outline-level))
  13920. (if (equal start-level 1)
  13921. nil
  13922. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13923. (if (re-search-backward re nil t)
  13924. (funcall outline-level)))))
  13925. (defun org-first-sibling-p ()
  13926. "Is this heading the first child of its parents?"
  13927. (interactive)
  13928. (let ((re (concat "^" outline-regexp))
  13929. level l)
  13930. (unless (org-at-heading-p t)
  13931. (error "Not at a heading"))
  13932. (setq level (funcall outline-level))
  13933. (save-excursion
  13934. (if (not (re-search-backward re nil t))
  13935. t
  13936. (setq l (funcall outline-level))
  13937. (< l level)))))
  13938. (defun org-goto-sibling (&optional previous)
  13939. "Goto the next sibling, even if it is invisible.
  13940. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13941. when a sibling was found. When none is found, return nil and don't
  13942. move point."
  13943. (let ((fun (if previous 're-search-backward 're-search-forward))
  13944. (pos (point))
  13945. (re (concat "^" outline-regexp))
  13946. level l)
  13947. (when (condition-case nil (org-back-to-heading t) (error nil))
  13948. (setq level (funcall outline-level))
  13949. (catch 'exit
  13950. (or previous (forward-char 1))
  13951. (while (funcall fun re nil t)
  13952. (setq l (funcall outline-level))
  13953. (when (< l level) (goto-char pos) (throw 'exit nil))
  13954. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13955. (goto-char pos)
  13956. nil))))
  13957. (defun org-show-siblings ()
  13958. "Show all siblings of the current headline."
  13959. (save-excursion
  13960. (while (org-goto-sibling) (org-flag-heading nil)))
  13961. (save-excursion
  13962. (while (org-goto-sibling 'previous)
  13963. (org-flag-heading nil))))
  13964. (defun org-show-hidden-entry ()
  13965. "Show an entry where even the heading is hidden."
  13966. (save-excursion
  13967. (org-show-entry)))
  13968. (defun org-flag-heading (flag &optional entry)
  13969. "Flag the current heading. FLAG non-nil means make invisible.
  13970. When ENTRY is non-nil, show the entire entry."
  13971. (save-excursion
  13972. (org-back-to-heading t)
  13973. ;; Check if we should show the entire entry
  13974. (if entry
  13975. (progn
  13976. (org-show-entry)
  13977. (save-excursion
  13978. (and (outline-next-heading)
  13979. (org-flag-heading nil))))
  13980. (outline-flag-region (max (point-min) (1- (point)))
  13981. (save-excursion (outline-end-of-heading) (point))
  13982. flag))))
  13983. (defun org-forward-same-level (arg)
  13984. "Move forward to the ARG'th subheading at same level as this one.
  13985. Stop at the first and last subheadings of a superior heading.
  13986. This is like outline-forward-same-level, but invisible headings are ok."
  13987. (interactive "p")
  13988. (org-back-to-heading t)
  13989. (while (> arg 0)
  13990. (let ((point-to-move-to (save-excursion
  13991. (org-get-next-sibling))))
  13992. (if point-to-move-to
  13993. (progn
  13994. (goto-char point-to-move-to)
  13995. (setq arg (1- arg)))
  13996. (progn
  13997. (setq arg 0)
  13998. (error "No following same-level heading"))))))
  13999. (defun org-get-next-sibling ()
  14000. "Move to next heading of the same level, and return point.
  14001. If there is no such heading, return nil.
  14002. This is like outline-next-sibling, but invisible headings are ok."
  14003. (let ((level (funcall outline-level)))
  14004. (outline-next-heading)
  14005. (while (and (not (eobp)) (> (funcall outline-level) level))
  14006. (outline-next-heading))
  14007. (if (or (eobp) (< (funcall outline-level) level))
  14008. nil
  14009. (point))))
  14010. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14011. ;; This is an exact copy of the original function, but it uses
  14012. ;; `org-back-to-heading', to make it work also in invisible
  14013. ;; trees. And is uses an invisible-OK argument.
  14014. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14015. (org-back-to-heading invisible-OK)
  14016. (let ((first t)
  14017. (level (funcall outline-level)))
  14018. (while (and (not (eobp))
  14019. (or first (> (funcall outline-level) level)))
  14020. (setq first nil)
  14021. (outline-next-heading))
  14022. (unless to-heading
  14023. (if (memq (preceding-char) '(?\n ?\^M))
  14024. (progn
  14025. ;; Go to end of line before heading
  14026. (forward-char -1)
  14027. (if (memq (preceding-char) '(?\n ?\^M))
  14028. ;; leave blank line before heading
  14029. (forward-char -1))))))
  14030. (point))
  14031. (defun org-show-subtree ()
  14032. "Show everything after this heading at deeper levels."
  14033. (outline-flag-region
  14034. (point)
  14035. (save-excursion
  14036. (outline-end-of-subtree) (outline-next-heading) (point))
  14037. nil))
  14038. (defun org-show-entry ()
  14039. "Show the body directly following this heading.
  14040. Show the heading too, if it is currently invisible."
  14041. (interactive)
  14042. (save-excursion
  14043. (condition-case nil
  14044. (progn
  14045. (org-back-to-heading t)
  14046. (outline-flag-region
  14047. (max (point-min) (1- (point)))
  14048. (save-excursion
  14049. (if (re-search-forward
  14050. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14051. (match-beginning 1)
  14052. (point-max)))
  14053. nil))
  14054. (error nil))))
  14055. (defun org-make-options-regexp (kwds &optional extra)
  14056. "Make a regular expression for keyword lines."
  14057. (concat
  14058. "^"
  14059. "#?[ \t]*\\+\\("
  14060. (mapconcat 'regexp-quote kwds "\\|")
  14061. (if extra (concat "\\|" extra))
  14062. "\\):[ \t]*"
  14063. "\\(.+\\)"))
  14064. ;; Make isearch reveal the necessary context
  14065. (defun org-isearch-end ()
  14066. "Reveal context after isearch exits."
  14067. (when isearch-success ; only if search was successful
  14068. (if (featurep 'xemacs)
  14069. ;; Under XEmacs, the hook is run in the correct place,
  14070. ;; we directly show the context.
  14071. (org-show-context 'isearch)
  14072. ;; In Emacs the hook runs *before* restoring the overlays.
  14073. ;; So we have to use a one-time post-command-hook to do this.
  14074. ;; (Emacs 22 has a special variable, see function `org-mode')
  14075. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14076. isearch-mode-end-hook-quit)
  14077. ;; Only when the isearch was not quitted.
  14078. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14079. 'append 'local)))))
  14080. (defun org-isearch-post-command ()
  14081. "Remove self from hook, and show context."
  14082. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14083. (org-show-context 'isearch))
  14084. ;;;; Integration with and fixes for other packages
  14085. ;;; Imenu support
  14086. (defvar org-imenu-markers nil
  14087. "All markers currently used by Imenu.")
  14088. (make-variable-buffer-local 'org-imenu-markers)
  14089. (defun org-imenu-new-marker (&optional pos)
  14090. "Return a new marker for use by Imenu, and remember the marker."
  14091. (let ((m (make-marker)))
  14092. (move-marker m (or pos (point)))
  14093. (push m org-imenu-markers)
  14094. m))
  14095. (defun org-imenu-get-tree ()
  14096. "Produce the index for Imenu."
  14097. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14098. (setq org-imenu-markers nil)
  14099. (let* ((n org-imenu-depth)
  14100. (re (concat "^" outline-regexp))
  14101. (subs (make-vector (1+ n) nil))
  14102. (last-level 0)
  14103. m level head)
  14104. (save-excursion
  14105. (save-restriction
  14106. (widen)
  14107. (goto-char (point-max))
  14108. (while (re-search-backward re nil t)
  14109. (setq level (org-reduced-level (funcall outline-level)))
  14110. (when (<= level n)
  14111. (looking-at org-complex-heading-regexp)
  14112. (setq head (org-link-display-format
  14113. (org-match-string-no-properties 4))
  14114. m (org-imenu-new-marker))
  14115. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14116. (if (>= level last-level)
  14117. (push (cons head m) (aref subs level))
  14118. (push (cons head (aref subs (1+ level))) (aref subs level))
  14119. (loop for i from (1+ level) to n do (aset subs i nil)))
  14120. (setq last-level level)))))
  14121. (aref subs 1)))
  14122. (eval-after-load "imenu"
  14123. '(progn
  14124. (add-hook 'imenu-after-jump-hook
  14125. (lambda ()
  14126. (if (eq major-mode 'org-mode)
  14127. (org-show-context 'org-goto))))))
  14128. (defun org-link-display-format (link)
  14129. "Replace a link with either the description, or the link target
  14130. if no description is present"
  14131. (save-match-data
  14132. (if (string-match org-bracket-link-analytic-regexp link)
  14133. (replace-match (or (match-string 5 link)
  14134. (concat (match-string 1 link)
  14135. (match-string 3 link)))
  14136. nil nil link)
  14137. link)))
  14138. ;; Speedbar support
  14139. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14140. "Overlay marking the agenda restriction line in speedbar.")
  14141. (org-overlay-put org-speedbar-restriction-lock-overlay
  14142. 'face 'org-agenda-restriction-lock)
  14143. (org-overlay-put org-speedbar-restriction-lock-overlay
  14144. 'help-echo "Agendas are currently limited to this item.")
  14145. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14146. (defun org-speedbar-set-agenda-restriction ()
  14147. "Restrict future agenda commands to the location at point in speedbar.
  14148. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14149. (interactive)
  14150. (require 'org-agenda)
  14151. (let (p m tp np dir txt)
  14152. (cond
  14153. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14154. 'org-imenu t))
  14155. (setq m (get-text-property p 'org-imenu-marker))
  14156. (save-excursion
  14157. (save-restriction
  14158. (set-buffer (marker-buffer m))
  14159. (goto-char m)
  14160. (org-agenda-set-restriction-lock 'subtree))))
  14161. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14162. 'speedbar-function 'speedbar-find-file))
  14163. (setq tp (previous-single-property-change
  14164. (1+ p) 'speedbar-function)
  14165. np (next-single-property-change
  14166. tp 'speedbar-function)
  14167. dir (speedbar-line-directory)
  14168. txt (buffer-substring-no-properties (or tp (point-min))
  14169. (or np (point-max))))
  14170. (save-excursion
  14171. (save-restriction
  14172. (set-buffer (find-file-noselect
  14173. (let ((default-directory dir))
  14174. (expand-file-name txt))))
  14175. (unless (org-mode-p)
  14176. (error "Cannot restrict to non-Org-mode file"))
  14177. (org-agenda-set-restriction-lock 'file))))
  14178. (t (error "Don't know how to restrict Org-mode's agenda")))
  14179. (org-move-overlay org-speedbar-restriction-lock-overlay
  14180. (point-at-bol) (point-at-eol))
  14181. (setq current-prefix-arg nil)
  14182. (org-agenda-maybe-redo)))
  14183. (eval-after-load "speedbar"
  14184. '(progn
  14185. (speedbar-add-supported-extension ".org")
  14186. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14187. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14188. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14189. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14190. (add-hook 'speedbar-visiting-tag-hook
  14191. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14192. ;;; Fixes and Hacks for problems with other packages
  14193. ;; Make flyspell not check words in links, to not mess up our keymap
  14194. (defun org-mode-flyspell-verify ()
  14195. "Don't let flyspell put overlays at active buttons."
  14196. (not (get-text-property (point) 'keymap)))
  14197. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14198. (eval-after-load "bookmark"
  14199. '(if (boundp 'bookmark-after-jump-hook)
  14200. ;; We can use the hook
  14201. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14202. ;; Hook not available, use advice
  14203. (defadvice bookmark-jump (after org-make-visible activate)
  14204. "Make the position visible."
  14205. (org-bookmark-jump-unhide))))
  14206. ;; Make sure saveplace show the location if it was hidden
  14207. (eval-after-load "saveplace"
  14208. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14209. "Make the position visible."
  14210. (org-bookmark-jump-unhide)))
  14211. (defun org-bookmark-jump-unhide ()
  14212. "Unhide the current position, to show the bookmark location."
  14213. (and (org-mode-p)
  14214. (or (org-invisible-p)
  14215. (save-excursion (goto-char (max (point-min) (1- (point))))
  14216. (org-invisible-p)))
  14217. (org-show-context 'bookmark-jump)))
  14218. ;; Make session.el ignore our circular variable
  14219. (eval-after-load "session"
  14220. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14221. ;;;; Experimental code
  14222. (defun org-closed-in-range ()
  14223. "Sparse tree of items closed in a certain time range.
  14224. Still experimental, may disappear in the future."
  14225. (interactive)
  14226. ;; Get the time interval from the user.
  14227. (let* ((time1 (time-to-seconds
  14228. (org-read-date nil 'to-time nil "Starting date: ")))
  14229. (time2 (time-to-seconds
  14230. (org-read-date nil 'to-time nil "End date:")))
  14231. ;; callback function
  14232. (callback (lambda ()
  14233. (let ((time
  14234. (time-to-seconds
  14235. (apply 'encode-time
  14236. (org-parse-time-string
  14237. (match-string 1))))))
  14238. ;; check if time in interval
  14239. (and (>= time time1) (<= time time2))))))
  14240. ;; make tree, check each match with the callback
  14241. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14242. ;;;; Finish up
  14243. (provide 'org)
  14244. (run-hooks 'org-load-hook)
  14245. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14246. ;;; org.el ends here