org.el 634 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023
  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.29trans
  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-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.29trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let* ((org-version org-version)
  96. (git-version)
  97. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  98. (if (file-exists-p (expand-file-name ".git" dir))
  99. (progn
  100. (shell-command (concat "cd " dir " && git describe --abbrev=4 HEAD"))
  101. (save-excursion
  102. (set-buffer "*Shell Command Output*")
  103. (goto-char (point-min))
  104. (replace-regexp "-" ".")
  105. (goto-char (point-min))
  106. (re-search-forward "[^\n]+")
  107. (setq git-version (match-string 0))
  108. (shell-command (concat "cd " dir " && git diff-index --name-only HEAD --"))
  109. (unless (eql 1 (point-max))
  110. (setq git-version (concat git-version ".dirty")))
  111. (setq org-version (concat org-version " (" git-version ")")))))
  112. (let ((version (format "Org-mode version %s" org-version)))
  113. (if here (insert version))
  114. (message version)
  115. version)))
  116. ;;; Compatibility constants
  117. ;;; The custom variables
  118. (defgroup org nil
  119. "Outline-based notes management and organizer."
  120. :tag "Org"
  121. :group 'outlines
  122. :group 'hypermedia
  123. :group 'calendar)
  124. (defcustom org-load-hook nil
  125. "Hook that is run after org.el has been loaded."
  126. :group 'org
  127. :type 'hook)
  128. (defvar org-modules) ; defined below
  129. (defvar org-modules-loaded nil
  130. "Have the modules been loaded already?")
  131. (defun org-load-modules-maybe (&optional force)
  132. "Load all extensions listed in `org-modules'."
  133. (when (or force (not org-modules-loaded))
  134. (mapc (lambda (ext)
  135. (condition-case nil (require ext)
  136. (error (message "Problems while trying to load feature `%s'" ext))))
  137. org-modules)
  138. (setq org-modules-loaded t)))
  139. (defun org-set-modules (var value)
  140. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  141. (set var value)
  142. (when (featurep 'org)
  143. (org-load-modules-maybe 'force)))
  144. (when (org-bound-and-true-p org-modules)
  145. (let ((a (member 'org-infojs org-modules)))
  146. (and a (setcar a 'org-jsinfo))))
  147. (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)
  148. "Modules that should always be loaded together with org.el.
  149. If a description starts with <C>, the file is not part of Emacs
  150. and loading it will require that you have downloaded and properly installed
  151. the org-mode distribution.
  152. You can also use this system to load external packages (i.e. neither Org
  153. core modules, not modules from the CONTRIB directory). Just add symbols
  154. to the end of the list. If the package is called org-xyz.el, then you need
  155. to add the symbol `xyz', and the package must have a call to
  156. (provide 'org-xyz)"
  157. :group 'org
  158. :set 'org-set-modules
  159. :type
  160. '(set :greedy t
  161. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  162. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  163. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  164. (const :tag " id: Global IDs for identifying entries" org-id)
  165. (const :tag " info: Links to Info nodes" org-info)
  166. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  167. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  168. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  169. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  170. (const :tag " mew Links to Mew folders/messages" org-mew)
  171. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  172. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  173. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  174. (const :tag " vm: Links to VM folders/messages" org-vm)
  175. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  176. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  177. (const :tag " mouse: Additional mouse support" org-mouse)
  178. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  179. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  180. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  181. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  182. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  183. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  184. (const :tag "C collector: Collect properties into tables" org-collector)
  185. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  186. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  187. (const :tag "C eval: Include command output as text" org-eval)
  188. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  189. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  190. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  191. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  192. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  193. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  194. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  195. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  196. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  197. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  198. (const :tag "C R: Computation using the R language" org-R)
  199. (const :tag "C registry: A registry for Org links" org-registry)
  200. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  201. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  202. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  203. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  204. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  205. (const :tag "C track: Keep up with Org development" org-track)
  206. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  207. (defcustom org-support-shift-select nil
  208. "Non-nil means, make shift-cursor commands select text when possible.
  209. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  210. selecting a region, or enlarge thusly regions started in this way.
  211. In Org-mode, in special contexts, these same keys are used for other
  212. purposes, important enough to compete with shift selection. Org tries
  213. to balance these needs by supporting `shift-select-mode' outside these
  214. special contexts, under control of this variable.
  215. The default of this variable is nil, to avoid confusing behavior. Shifted
  216. cursor keys will then execute Org commands in the following contexts:
  217. - on a headline, changing TODO state (left/right) and priority (up/down)
  218. - on a time stamp, changing the time
  219. - in a plain list item, changing the bullet type
  220. - in a property definition line, switching between allowed values
  221. - in the BEGIN line of a clock table (changing the time block).
  222. Outside these contexts, the commands will throw an error.
  223. When this variable is t and the cursor is not in a special context,
  224. Org-mode will support shift-selection for making and enlarging regions.
  225. To make this more effective, the bullet cycling will no longer happen
  226. anywhere in an item line, but only if the cursor is exactly on the bullet.
  227. If you set this variable to the symbol `always', then the keys
  228. will not be special in headlines, property lines, and item lines, to make
  229. shift selection work there as well. If this is what you want, you can
  230. use the following alternative commands: `C-c C-t' and `C-c ,' to
  231. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  232. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  233. edited by hand or in column view.
  234. However, when the cursor is on a timestamp, shift-cursor commands
  235. will still edit the time stamp - this is just too good to give up.
  236. XEmacs user should have this variable set to nil, because shift-select-mode
  237. is Emacs 23 only."
  238. :group 'org
  239. :type '(choice
  240. (const :tag "Never" nil)
  241. (const :tag "When outside special context" t)
  242. (const :tag "Everywhere except timestamps" always)))
  243. (defgroup org-startup nil
  244. "Options concerning startup of Org-mode."
  245. :tag "Org Startup"
  246. :group 'org)
  247. (defcustom org-startup-folded t
  248. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  249. This can also be configured on a per-file basis by adding one of
  250. the following lines anywhere in the buffer:
  251. #+STARTUP: fold
  252. #+STARTUP: nofold
  253. #+STARTUP: content"
  254. :group 'org-startup
  255. :type '(choice
  256. (const :tag "nofold: show all" nil)
  257. (const :tag "fold: overview" t)
  258. (const :tag "content: all headlines" content)))
  259. (defcustom org-startup-truncated t
  260. "Non-nil means, entering Org-mode will set `truncate-lines'.
  261. This is useful since some lines containing links can be very long and
  262. uninteresting. Also tables look terrible when wrapped."
  263. :group 'org-startup
  264. :type 'boolean)
  265. (defcustom org-startup-indented nil
  266. "Non-nil means, turn on `org-indent-mode' on startup.
  267. This can also be configured on a per-file basis by adding one of
  268. the following lines anywhere in the buffer:
  269. #+STARTUP: indent
  270. #+STARTUP: noindent"
  271. :group 'org-structure
  272. :type '(choice
  273. (const :tag "Not" nil)
  274. (const :tag "Globally (slow on startup in large files)" t)))
  275. (defcustom org-startup-align-all-tables nil
  276. "Non-nil means, align all tables when visiting a file.
  277. This is useful when the column width in tables is forced with <N> cookies
  278. in table fields. Such tables will look correct only after the first re-align.
  279. This can also be configured on a per-file basis by adding one of
  280. the following lines anywhere in the buffer:
  281. #+STARTUP: align
  282. #+STARTUP: noalign"
  283. :group 'org-startup
  284. :type 'boolean)
  285. (defcustom org-insert-mode-line-in-empty-file nil
  286. "Non-nil means insert the first line setting Org-mode in empty files.
  287. When the function `org-mode' is called interactively in an empty file, this
  288. normally means that the file name does not automatically trigger Org-mode.
  289. To ensure that the file will always be in Org-mode in the future, a
  290. line enforcing Org-mode will be inserted into the buffer, if this option
  291. has been set."
  292. :group 'org-startup
  293. :type 'boolean)
  294. (defcustom org-replace-disputed-keys nil
  295. "Non-nil means use alternative key bindings for some keys.
  296. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  297. These keys are also used by other packages like shift-selection-mode'
  298. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  299. If you want to use Org-mode together with one of these other modes,
  300. or more generally if you would like to move some Org-mode commands to
  301. other keys, set this variable and configure the keys with the variable
  302. `org-disputed-keys'.
  303. This option is only relevant at load-time of Org-mode, and must be set
  304. *before* org.el is loaded. Changing it requires a restart of Emacs to
  305. become effective."
  306. :group 'org-startup
  307. :type 'boolean)
  308. (defcustom org-use-extra-keys nil
  309. "Non-nil means use extra key sequence definitions for certain
  310. commands. This happens automatically if you run XEmacs or if
  311. window-system is nil. This variable lets you do the same
  312. manually. You must set it before loading org.
  313. Example: on Carbon Emacs 22 running graphically, with an external
  314. keyboard on a Powerbook, the default way of setting M-left might
  315. not work for either Alt or ESC. Setting this variable will make
  316. it work for ESC."
  317. :group 'org-startup
  318. :type 'boolean)
  319. (if (fboundp 'defvaralias)
  320. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  321. (defcustom org-disputed-keys
  322. '(([(shift up)] . [(meta p)])
  323. ([(shift down)] . [(meta n)])
  324. ([(shift left)] . [(meta -)])
  325. ([(shift right)] . [(meta +)])
  326. ([(control shift right)] . [(meta shift +)])
  327. ([(control shift left)] . [(meta shift -)]))
  328. "Keys for which Org-mode and other modes compete.
  329. This is an alist, cars are the default keys, second element specifies
  330. the alternative to use when `org-replace-disputed-keys' is t.
  331. Keys can be specified in any syntax supported by `define-key'.
  332. The value of this option takes effect only at Org-mode's startup,
  333. therefore you'll have to restart Emacs to apply it after changing."
  334. :group 'org-startup
  335. :type 'alist)
  336. (defun org-key (key)
  337. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  338. Or return the original if not disputed."
  339. (if org-replace-disputed-keys
  340. (let* ((nkey (key-description key))
  341. (x (org-find-if (lambda (x)
  342. (equal (key-description (car x)) nkey))
  343. org-disputed-keys)))
  344. (if x (cdr x) key))
  345. key))
  346. (defun org-find-if (predicate seq)
  347. (catch 'exit
  348. (while seq
  349. (if (funcall predicate (car seq))
  350. (throw 'exit (car seq))
  351. (pop seq)))))
  352. (defun org-defkey (keymap key def)
  353. "Define a key, possibly translated, as returned by `org-key'."
  354. (define-key keymap (org-key key) def))
  355. (defcustom org-ellipsis nil
  356. "The ellipsis to use in the Org-mode outline.
  357. When nil, just use the standard three dots. When a string, use that instead,
  358. When a face, use the standard 3 dots, but with the specified face.
  359. The change affects only Org-mode (which will then use its own display table).
  360. Changing this requires executing `M-x org-mode' in a buffer to become
  361. effective."
  362. :group 'org-startup
  363. :type '(choice (const :tag "Default" nil)
  364. (face :tag "Face" :value org-warning)
  365. (string :tag "String" :value "...#")))
  366. (defvar org-display-table nil
  367. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  368. (defgroup org-keywords nil
  369. "Keywords in Org-mode."
  370. :tag "Org Keywords"
  371. :group 'org)
  372. (defcustom org-deadline-string "DEADLINE:"
  373. "String to mark deadline entries.
  374. A deadline is this string, followed by a time stamp. Should be a word,
  375. terminated by a colon. You can insert a schedule keyword and
  376. a timestamp with \\[org-deadline].
  377. Changes become only effective after restarting Emacs."
  378. :group 'org-keywords
  379. :type 'string)
  380. (defcustom org-scheduled-string "SCHEDULED:"
  381. "String to mark scheduled TODO entries.
  382. A schedule is this string, followed by a time stamp. Should be a word,
  383. terminated by a colon. You can insert a schedule keyword and
  384. a timestamp with \\[org-schedule].
  385. Changes become only effective after restarting Emacs."
  386. :group 'org-keywords
  387. :type 'string)
  388. (defcustom org-closed-string "CLOSED:"
  389. "String used as the prefix for timestamps logging closing a TODO entry."
  390. :group 'org-keywords
  391. :type 'string)
  392. (defcustom org-clock-string "CLOCK:"
  393. "String used as prefix for timestamps clocking work hours on an item."
  394. :group 'org-keywords
  395. :type 'string)
  396. (defcustom org-comment-string "COMMENT"
  397. "Entries starting with this keyword will never be exported.
  398. An entry can be toggled between COMMENT and normal with
  399. \\[org-toggle-comment].
  400. Changes become only effective after restarting Emacs."
  401. :group 'org-keywords
  402. :type 'string)
  403. (defcustom org-quote-string "QUOTE"
  404. "Entries starting with this keyword will be exported in fixed-width font.
  405. Quoting applies only to the text in the entry following the headline, and does
  406. not extend beyond the next headline, even if that is lower level.
  407. An entry can be toggled between QUOTE and normal with
  408. \\[org-toggle-fixed-width-section]."
  409. :group 'org-keywords
  410. :type 'string)
  411. (defconst org-repeat-re
  412. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  413. "Regular expression for specifying repeated events.
  414. After a match, group 1 contains the repeat expression.")
  415. (defgroup org-structure nil
  416. "Options concerning the general structure of Org-mode files."
  417. :tag "Org Structure"
  418. :group 'org)
  419. (defgroup org-reveal-location nil
  420. "Options about how to make context of a location visible."
  421. :tag "Org Reveal Location"
  422. :group 'org-structure)
  423. (defconst org-context-choice
  424. '(choice
  425. (const :tag "Always" t)
  426. (const :tag "Never" nil)
  427. (repeat :greedy t :tag "Individual contexts"
  428. (cons
  429. (choice :tag "Context"
  430. (const agenda)
  431. (const org-goto)
  432. (const occur-tree)
  433. (const tags-tree)
  434. (const link-search)
  435. (const mark-goto)
  436. (const bookmark-jump)
  437. (const isearch)
  438. (const default))
  439. (boolean))))
  440. "Contexts for the reveal options.")
  441. (defcustom org-show-hierarchy-above '((default . t))
  442. "Non-nil means, show full hierarchy when revealing a location.
  443. Org-mode often shows locations in an org-mode file which might have
  444. been invisible before. When this is set, the hierarchy of headings
  445. above the exposed location is shown.
  446. Turning this off for example for sparse trees makes them very compact.
  447. Instead of t, this can also be an alist specifying this option for different
  448. contexts. Valid contexts are
  449. agenda when exposing an entry from the agenda
  450. org-goto when using the command `org-goto' on key C-c C-j
  451. occur-tree when using the command `org-occur' on key C-c /
  452. tags-tree when constructing a sparse tree based on tags matches
  453. link-search when exposing search matches associated with a link
  454. mark-goto when exposing the jump goal of a mark
  455. bookmark-jump when exposing a bookmark location
  456. isearch when exiting from an incremental search
  457. default default for all contexts not set explicitly"
  458. :group 'org-reveal-location
  459. :type org-context-choice)
  460. (defcustom org-show-following-heading '((default . nil))
  461. "Non-nil means, show following heading when revealing a location.
  462. Org-mode often shows locations in an org-mode file which might have
  463. been invisible before. When this is set, the heading following the
  464. match is shown.
  465. Turning this off for example for sparse trees makes them very compact,
  466. but makes it harder to edit the location of the match. In such a case,
  467. use the command \\[org-reveal] to show more context.
  468. Instead of t, this can also be an alist specifying this option for different
  469. contexts. See `org-show-hierarchy-above' for valid contexts."
  470. :group 'org-reveal-location
  471. :type org-context-choice)
  472. (defcustom org-show-siblings '((default . nil) (isearch t))
  473. "Non-nil means, show all sibling heading when revealing a location.
  474. Org-mode often shows locations in an org-mode file which might have
  475. been invisible before. When this is set, the sibling of the current entry
  476. heading are all made visible. If `org-show-hierarchy-above' is t,
  477. the same happens on each level of the hierarchy above the current entry.
  478. By default this is on for the isearch context, off for all other contexts.
  479. Turning this off for example for sparse trees makes them very compact,
  480. but makes it harder to edit the location of the match. In such a case,
  481. use the command \\[org-reveal] to show more context.
  482. Instead of t, this can also be an alist specifying this option for different
  483. contexts. See `org-show-hierarchy-above' for valid contexts."
  484. :group 'org-reveal-location
  485. :type org-context-choice)
  486. (defcustom org-show-entry-below '((default . nil))
  487. "Non-nil means, show the entry below a headline when revealing a location.
  488. Org-mode often shows locations in an org-mode file which might have
  489. been invisible before. When this is set, the text below the headline that is
  490. exposed is also shown.
  491. By default this is off for all contexts.
  492. Instead of t, this can also be an alist specifying this option for different
  493. contexts. See `org-show-hierarchy-above' for valid contexts."
  494. :group 'org-reveal-location
  495. :type org-context-choice)
  496. (defcustom org-indirect-buffer-display 'other-window
  497. "How should indirect tree buffers be displayed?
  498. This applies to indirect buffers created with the commands
  499. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  500. Valid values are:
  501. current-window Display in the current window
  502. other-window Just display in another window.
  503. dedicated-frame Create one new frame, and re-use it each time.
  504. new-frame Make a new frame each time. Note that in this case
  505. previously-made indirect buffers are kept, and you need to
  506. kill these buffers yourself."
  507. :group 'org-structure
  508. :group 'org-agenda-windows
  509. :type '(choice
  510. (const :tag "In current window" current-window)
  511. (const :tag "In current frame, other window" other-window)
  512. (const :tag "Each time a new frame" new-frame)
  513. (const :tag "One dedicated frame" dedicated-frame)))
  514. (defgroup org-cycle nil
  515. "Options concerning visibility cycling in Org-mode."
  516. :tag "Org Cycle"
  517. :group 'org-structure)
  518. (defcustom org-cycle-skip-children-state-if-no-children t
  519. "Non-nil means, skip CHILDREN state in entries that don't have any."
  520. :group 'org-cycle
  521. :type 'boolean)
  522. (defcustom org-cycle-max-level nil
  523. "Maximum level which should still be subject to visibility cycling.
  524. Levels higher than this will, for cycling, be treated as text, not a headline.
  525. When `org-odd-levels-only' is set, a value of N in this variable actually
  526. means 2N-1 stars as the limiting headline.
  527. When nil, cycle all levels.
  528. Note that the limiting level of cycling is also influenced by
  529. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  530. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  531. than its value."
  532. :group 'org-cycle
  533. :type '(choice
  534. (const :tag "No limit" nil)
  535. (integer :tag "Maximum level")))
  536. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  537. "Names of drawers. Drawers are not opened by cycling on the headline above.
  538. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  539. this:
  540. :DRAWERNAME:
  541. .....
  542. :END:
  543. The drawer \"PROPERTIES\" is special for capturing properties through
  544. the property API.
  545. Drawers can be defined on the per-file basis with a line like:
  546. #+DRAWERS: HIDDEN STATE PROPERTIES"
  547. :group 'org-structure
  548. :group 'org-cycle
  549. :type '(repeat (string :tag "Drawer Name")))
  550. (defcustom org-hide-block-startup nil
  551. "Non-nil means, , entering Org-mode will fold all blocks.
  552. This can also be set in on a per-file basis with
  553. #+STARTUP: hideblocks
  554. #+STARTUP: showblocks"
  555. :group 'org-startup
  556. :group 'org-cycle
  557. :type 'boolean)
  558. (defcustom org-cycle-global-at-bob nil
  559. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  560. This makes it possible to do global cycling without having to use S-TAB or
  561. C-u TAB. For this special case to work, the first line of the buffer
  562. must not be a headline - it may be empty or some other text. When used in
  563. this way, `org-cycle-hook' is disables temporarily, to make sure the
  564. cursor stays at the beginning of the buffer.
  565. When this option is nil, don't do anything special at the beginning
  566. of the buffer."
  567. :group 'org-cycle
  568. :type 'boolean)
  569. (defcustom org-cycle-emulate-tab t
  570. "Where should `org-cycle' emulate TAB.
  571. nil Never
  572. white Only in completely white lines
  573. whitestart Only at the beginning of lines, before the first non-white char
  574. t Everywhere except in headlines
  575. exc-hl-bol Everywhere except at the start of a headline
  576. If TAB is used in a place where it does not emulate TAB, the current subtree
  577. visibility is cycled."
  578. :group 'org-cycle
  579. :type '(choice (const :tag "Never" nil)
  580. (const :tag "Only in completely white lines" white)
  581. (const :tag "Before first char in a line" whitestart)
  582. (const :tag "Everywhere except in headlines" t)
  583. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  584. ))
  585. (defcustom org-cycle-separator-lines 2
  586. "Number of empty lines needed to keep an empty line between collapsed trees.
  587. If you leave an empty line between the end of a subtree and the following
  588. headline, this empty line is hidden when the subtree is folded.
  589. Org-mode will leave (exactly) one empty line visible if the number of
  590. empty lines is equal or larger to the number given in this variable.
  591. So the default 2 means, at least 2 empty lines after the end of a subtree
  592. are needed to produce free space between a collapsed subtree and the
  593. following headline.
  594. Special case: when 0, never leave empty lines in collapsed view."
  595. :group 'org-cycle
  596. :type 'integer)
  597. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  598. (defcustom org-pre-cycle-hook nil
  599. "Hook that is run before visibility cycling is happening.
  600. The function(s) in this hook must accept a single argument which indicates
  601. the new state that will be set right after running this hook. The
  602. argument is a symbol. Before a global state change, it can have the values
  603. `overview', `content', or `all'. Before a local state change, it can have
  604. the values `folded', `children', or `subtree'."
  605. :group 'org-cycle
  606. :type 'hook)
  607. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  608. org-cycle-hide-drawers
  609. org-cycle-show-empty-lines
  610. org-optimize-window-after-visibility-change)
  611. "Hook that is run after `org-cycle' has changed the buffer visibility.
  612. The function(s) in this hook must accept a single argument which indicates
  613. the new state that was set by the most recent `org-cycle' command. The
  614. argument is a symbol. After a global state change, it can have the values
  615. `overview', `content', or `all'. After a local state change, it can have
  616. the values `folded', `children', or `subtree'."
  617. :group 'org-cycle
  618. :type 'hook)
  619. (defgroup org-edit-structure nil
  620. "Options concerning structure editing in Org-mode."
  621. :tag "Org Edit Structure"
  622. :group 'org-structure)
  623. (defcustom org-odd-levels-only nil
  624. "Non-nil means, skip even levels and only use odd levels for the outline.
  625. This has the effect that two stars are being added/taken away in
  626. promotion/demotion commands. It also influences how levels are
  627. handled by the exporters.
  628. Changing it requires restart of `font-lock-mode' to become effective
  629. for fontification also in regions already fontified.
  630. You may also set this on a per-file basis by adding one of the following
  631. lines to the buffer:
  632. #+STARTUP: odd
  633. #+STARTUP: oddeven"
  634. :group 'org-edit-structure
  635. :group 'org-font-lock
  636. :type 'boolean)
  637. (defcustom org-adapt-indentation t
  638. "Non-nil means, adapt indentation to outline node level.
  639. When this variable is set, Org assumes that you write outlines by
  640. indenting text in each node to align with the headline (after the stars).
  641. The following issues are influenced by this variable:
  642. - When this is set and the *entire* text in an entry is indented, the
  643. indentation is increased by one space in a demotion command, and
  644. decreased by one in a promotion command. If any line in the entry
  645. body starts with text at column 0, indentation is not changed at all.
  646. - Property drawers and planning information is inserted indented when
  647. this variable s set. When nil, they will not be indented.
  648. - TAB indents a line relative to context. The lines below a headline
  649. will be indented when this variable is set.
  650. Note that this is all about true indentation, by adding and removing
  651. space characters. See also `org-indent.el' which does level-dependent
  652. indentation in a virtual way, i.e. at display time in Emacs."
  653. :group 'org-edit-structure
  654. :type 'boolean)
  655. (defcustom org-special-ctrl-a/e nil
  656. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  657. When t, `C-a' will bring back the cursor to the beginning of the
  658. headline text, i.e. after the stars and after a possible TODO keyword.
  659. In an item, this will be the position after the bullet.
  660. When the cursor is already at that position, another `C-a' will bring
  661. it to the beginning of the line.
  662. `C-e' will jump to the end of the headline, ignoring the presence of tags
  663. in the headline. A second `C-e' will then jump to the true end of the
  664. line, after any tags.
  665. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  666. going to the true line boundary first. Only a directly following, identical
  667. keypress will bring the cursor to the special positions.
  668. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  669. set separately."
  670. :group 'org-edit-structure
  671. :type '(choice
  672. (const :tag "off" nil)
  673. (const :tag "after stars/bullet and before tags first" t)
  674. (const :tag "true line boundary first" reversed)
  675. (cons :tag "Set C-a and C-e separately"
  676. (choice :tag "Special C-a"
  677. (const :tag "off" nil)
  678. (const :tag "after stars/bullet first" t)
  679. (const :tag "before stars/bullet first" reversed))
  680. (choice :tag "Special C-e"
  681. (const :tag "off" nil)
  682. (const :tag "before tags first" t)
  683. (const :tag "after tags first" reversed)))))
  684. (if (fboundp 'defvaralias)
  685. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  686. (defcustom org-special-ctrl-k nil
  687. "Non-nil means `C-k' will behave specially in headlines.
  688. When nil, `C-k' will call the default `kill-line' command.
  689. When t, the following will happen while the cursor is in the headline:
  690. - When the cursor is at the beginning of a headline, kill the entire
  691. line and possible the folded subtree below the line.
  692. - When in the middle of the headline text, kill the headline up to the tags.
  693. - When after the headline text, kill the tags."
  694. :group 'org-edit-structure
  695. :type 'boolean)
  696. (defcustom org-yank-folded-subtrees t
  697. "Non-nil means, when yanking subtrees, fold them.
  698. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  699. it starts with a heading and all other headings in it are either children
  700. or siblings, then fold all the subtrees. However, do this only if no
  701. text after the yank would be swallowed into a folded tree by this action."
  702. :group 'org-edit-structure
  703. :type 'boolean)
  704. (defcustom org-yank-adjusted-subtrees nil
  705. "Non-nil means, when yanking subtrees, adjust the level.
  706. With this setting, `org-paste-subtree' is used to insert the subtree, see
  707. this function for details."
  708. :group 'org-edit-structure
  709. :type 'boolean)
  710. (defcustom org-M-RET-may-split-line '((default . t))
  711. "Non-nil means, M-RET will split the line at the cursor position.
  712. When nil, it will go to the end of the line before making a
  713. new line.
  714. You may also set this option in a different way for different
  715. contexts. Valid contexts are:
  716. headline when creating a new headline
  717. item when creating a new item
  718. table in a table field
  719. default the value to be used for all contexts not explicitly
  720. customized"
  721. :group 'org-structure
  722. :group 'org-table
  723. :type '(choice
  724. (const :tag "Always" t)
  725. (const :tag "Never" nil)
  726. (repeat :greedy t :tag "Individual contexts"
  727. (cons
  728. (choice :tag "Context"
  729. (const headline)
  730. (const item)
  731. (const table)
  732. (const default))
  733. (boolean)))))
  734. (defcustom org-insert-heading-respect-content nil
  735. "Non-nil means, insert new headings after the current subtree.
  736. When nil, the new heading is created directly after the current line.
  737. The commands \\[org-insert-heading-respect-content] and
  738. \\[org-insert-todo-heading-respect-content] turn this variable on
  739. for the duration of the command."
  740. :group 'org-structure
  741. :type 'boolean)
  742. (defcustom org-blank-before-new-entry '((heading . auto)
  743. (plain-list-item . auto))
  744. "Should `org-insert-heading' leave a blank line before new heading/item?
  745. The value is an alist, with `heading' and `plain-list-item' as car,
  746. and a boolean flag as cdr. For plain lists, if the variable
  747. `org-empty-line-terminates-plain-lists' is set, the setting here
  748. is ignored and no empty line is inserted, to keep the list in tact."
  749. :group 'org-edit-structure
  750. :type '(list
  751. (cons (const heading)
  752. (choice (const :tag "Never" nil)
  753. (const :tag "Always" t)
  754. (const :tag "Auto" auto)))
  755. (cons (const plain-list-item)
  756. (choice (const :tag "Never" nil)
  757. (const :tag "Always" t)
  758. (const :tag "Auto" auto)))))
  759. (defcustom org-insert-heading-hook nil
  760. "Hook being run after inserting a new heading."
  761. :group 'org-edit-structure
  762. :type 'hook)
  763. (defcustom org-enable-fixed-width-editor t
  764. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  765. This currently only means, they are never auto-wrapped.
  766. When nil, such lines will be treated like ordinary lines.
  767. See also the QUOTE keyword."
  768. :group 'org-edit-structure
  769. :type 'boolean)
  770. (defcustom org-goto-auto-isearch t
  771. "Non-nil means, typing characters in org-goto starts incremental search."
  772. :group 'org-edit-structure
  773. :type 'boolean)
  774. (defgroup org-sparse-trees nil
  775. "Options concerning sparse trees in Org-mode."
  776. :tag "Org Sparse Trees"
  777. :group 'org-structure)
  778. (defcustom org-highlight-sparse-tree-matches t
  779. "Non-nil means, highlight all matches that define a sparse tree.
  780. The highlights will automatically disappear the next time the buffer is
  781. changed by an edit command."
  782. :group 'org-sparse-trees
  783. :type 'boolean)
  784. (defcustom org-remove-highlights-with-change t
  785. "Non-nil means, any change to the buffer will remove temporary highlights.
  786. Such highlights are created by `org-occur' and `org-clock-display'.
  787. When nil, `C-c C-c needs to be used to get rid of the highlights.
  788. The highlights created by `org-preview-latex-fragment' always need
  789. `C-c C-c' to be removed."
  790. :group 'org-sparse-trees
  791. :group 'org-time
  792. :type 'boolean)
  793. (defcustom org-occur-hook '(org-first-headline-recenter)
  794. "Hook that is run after `org-occur' has constructed a sparse tree.
  795. This can be used to recenter the window to show as much of the structure
  796. as possible."
  797. :group 'org-sparse-trees
  798. :type 'hook)
  799. (defgroup org-imenu-and-speedbar nil
  800. "Options concerning imenu and speedbar in Org-mode."
  801. :tag "Org Imenu and Speedbar"
  802. :group 'org-structure)
  803. (defcustom org-imenu-depth 2
  804. "The maximum level for Imenu access to Org-mode headlines.
  805. This also applied for speedbar access."
  806. :group 'org-imenu-and-speedbar
  807. :type 'integer)
  808. (defgroup org-table nil
  809. "Options concerning tables in Org-mode."
  810. :tag "Org Table"
  811. :group 'org)
  812. (defcustom org-enable-table-editor 'optimized
  813. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  814. When nil, such lines will be treated like ordinary lines.
  815. When equal to the symbol `optimized', the table editor will be optimized to
  816. do the following:
  817. - Automatic overwrite mode in front of whitespace in table fields.
  818. This makes the structure of the table stay in tact as long as the edited
  819. field does not exceed the column width.
  820. - Minimize the number of realigns. Normally, the table is aligned each time
  821. TAB or RET are pressed to move to another field. With optimization this
  822. happens only if changes to a field might have changed the column width.
  823. Optimization requires replacing the functions `self-insert-command',
  824. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  825. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  826. very good at guessing when a re-align will be necessary, but you can always
  827. force one with \\[org-ctrl-c-ctrl-c].
  828. If you would like to use the optimized version in Org-mode, but the
  829. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  830. This variable can be used to turn on and off the table editor during a session,
  831. but in order to toggle optimization, a restart is required.
  832. See also the variable `org-table-auto-blank-field'."
  833. :group 'org-table
  834. :type '(choice
  835. (const :tag "off" nil)
  836. (const :tag "on" t)
  837. (const :tag "on, optimized" optimized)))
  838. (defcustom org-self-insert-cluster-for-undo t
  839. "Non-nil means cluster self-insert commands for undo when possible.
  840. If this is set, then, like in the Emacs command loop, 20 consequtive
  841. characters will be undone together.
  842. This is configurable, because there is some impact on typing performance."
  843. :group 'org-table
  844. :type 'boolean)
  845. (defcustom org-table-tab-recognizes-table.el t
  846. "Non-nil means, TAB will automatically notice a table.el table.
  847. When it sees such a table, it moves point into it and - if necessary -
  848. calls `table-recognize-table'."
  849. :group 'org-table-editing
  850. :type 'boolean)
  851. (defgroup org-link nil
  852. "Options concerning links in Org-mode."
  853. :tag "Org Link"
  854. :group 'org)
  855. (defvar org-link-abbrev-alist-local nil
  856. "Buffer-local version of `org-link-abbrev-alist', which see.
  857. The value of this is taken from the #+LINK lines.")
  858. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  859. (defcustom org-link-abbrev-alist nil
  860. "Alist of link abbreviations.
  861. The car of each element is a string, to be replaced at the start of a link.
  862. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  863. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  864. [[linkkey:tag][description]]
  865. The 'linkkey' must be a word word, starting with a letter, followed
  866. by letters, numbers, '-' or '_'.
  867. If REPLACE is a string, the tag will simply be appended to create the link.
  868. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  869. the placeholder \"%h\" will cause a url-encoded version of the tag to
  870. be inserted at that point (see the function `url-hexify-string').
  871. REPLACE may also be a function that will be called with the tag as the
  872. only argument to create the link, which should be returned as a string.
  873. See the manual for examples."
  874. :group 'org-link
  875. :type '(repeat
  876. (cons
  877. (string :tag "Protocol")
  878. (choice
  879. (string :tag "Format")
  880. (function)))))
  881. (defcustom org-descriptive-links t
  882. "Non-nil means, hide link part and only show description of bracket links.
  883. Bracket links are like [[link][description]]. This variable sets the initial
  884. state in new org-mode buffers. The setting can then be toggled on a
  885. per-buffer basis from the Org->Hyperlinks menu."
  886. :group 'org-link
  887. :type 'boolean)
  888. (defcustom org-link-file-path-type 'adaptive
  889. "How the path name in file links should be stored.
  890. Valid values are:
  891. relative Relative to the current directory, i.e. the directory of the file
  892. into which the link is being inserted.
  893. absolute Absolute path, if possible with ~ for home directory.
  894. noabbrev Absolute path, no abbreviation of home directory.
  895. adaptive Use relative path for files in the current directory and sub-
  896. directories of it. For other files, use an absolute path."
  897. :group 'org-link
  898. :type '(choice
  899. (const relative)
  900. (const absolute)
  901. (const noabbrev)
  902. (const adaptive)))
  903. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  904. "Types of links that should be activated in Org-mode files.
  905. This is a list of symbols, each leading to the activation of a certain link
  906. type. In principle, it does not hurt to turn on most link types - there may
  907. be a small gain when turning off unused link types. The types are:
  908. bracket The recommended [[link][description]] or [[link]] links with hiding.
  909. angular Links in angular brackets that may contain whitespace like
  910. <bbdb:Carsten Dominik>.
  911. plain Plain links in normal text, no whitespace, like http://google.com.
  912. radio Text that is matched by a radio target, see manual for details.
  913. tag Tag settings in a headline (link to tag search).
  914. date Time stamps (link to calendar).
  915. footnote Footnote labels.
  916. Changing this variable requires a restart of Emacs to become effective."
  917. :group 'org-link
  918. :type '(set :greedy t
  919. (const :tag "Double bracket links (new style)" bracket)
  920. (const :tag "Angular bracket links (old style)" angular)
  921. (const :tag "Plain text links" plain)
  922. (const :tag "Radio target matches" radio)
  923. (const :tag "Tags" tag)
  924. (const :tag "Timestamps" date)
  925. (const :tag "Footnotes" footnote)))
  926. (defcustom org-make-link-description-function nil
  927. "Function to use to generate link descriptions from links. If
  928. nil the link location will be used. This function must take two
  929. parameters; the first is the link and the second the description
  930. org-insert-link has generated, and should return the description
  931. to use."
  932. :group 'org-link
  933. :type 'function)
  934. (defgroup org-link-store nil
  935. "Options concerning storing links in Org-mode."
  936. :tag "Org Store Link"
  937. :group 'org-link)
  938. (defcustom org-email-link-description-format "Email %c: %.30s"
  939. "Format of the description part of a link to an email or usenet message.
  940. The following %-escapes will be replaced by corresponding information:
  941. %F full \"From\" field
  942. %f name, taken from \"From\" field, address if no name
  943. %T full \"To\" field
  944. %t first name in \"To\" field, address if no name
  945. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  946. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  947. %s subject
  948. %m message-id.
  949. You may use normal field width specification between the % and the letter.
  950. This is for example useful to limit the length of the subject.
  951. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  952. :group 'org-link-store
  953. :type 'string)
  954. (defcustom org-from-is-user-regexp
  955. (let (r1 r2)
  956. (when (and user-mail-address (not (string= user-mail-address "")))
  957. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  958. (when (and user-full-name (not (string= user-full-name "")))
  959. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  960. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  961. "Regexp matched against the \"From:\" header of an email or usenet message.
  962. It should match if the message is from the user him/herself."
  963. :group 'org-link-store
  964. :type 'regexp)
  965. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  966. "Non-nil means, storing a link to an Org file will use entry IDs.
  967. Note that before this variable is even considered, org-id must be loaded,
  968. so please customize `org-modules' and turn it on.
  969. The variable can have the following values:
  970. t Create an ID if needed to make a link to the current entry.
  971. create-if-interactive
  972. If `org-store-link' is called directly (interactively, as a user
  973. command), do create an ID to support the link. But when doing the
  974. job for remember, only use the ID if it already exists. The
  975. purpose of this setting is to avoid proliferation of unwanted
  976. IDs, just because you happen to be in an Org file when you
  977. call `org-remember' that automatically and preemptively
  978. creates a link. If you do want to get an ID link in a remember
  979. template to an entry not having an ID, create it first by
  980. explicitly creating a link to it, using `C-c C-l' first.
  981. create-if-interactive-and-no-custom-id
  982. Like create-if-interactive, but do not create an ID if there is
  983. a CUSTOM_ID property defined in the entry. This is the default.
  984. use-existing
  985. Use existing ID, do not create one.
  986. nil Never use an ID to make a link, instead link using a text search for
  987. the headline text."
  988. :group 'org-link-store
  989. :type '(choice
  990. (const :tag "Create ID to make link" t)
  991. (const :tag "Create if storing link interactively"
  992. create-if-interactive)
  993. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  994. create-if-interactive-and-no-custom-id)
  995. (const :tag "Only use existing" use-existing)
  996. (const :tag "Do not use ID to create link" nil)))
  997. (defcustom org-context-in-file-links t
  998. "Non-nil means, file links from `org-store-link' contain context.
  999. A search string will be added to the file name with :: as separator and
  1000. used to find the context when the link is activated by the command
  1001. `org-open-at-point'.
  1002. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1003. negates this setting for the duration of the command."
  1004. :group 'org-link-store
  1005. :type 'boolean)
  1006. (defcustom org-keep-stored-link-after-insertion nil
  1007. "Non-nil means, keep link in list for entire session.
  1008. The command `org-store-link' adds a link pointing to the current
  1009. location to an internal list. These links accumulate during a session.
  1010. The command `org-insert-link' can be used to insert links into any
  1011. Org-mode file (offering completion for all stored links). When this
  1012. option is nil, every link which has been inserted once using \\[org-insert-link]
  1013. will be removed from the list, to make completing the unused links
  1014. more efficient."
  1015. :group 'org-link-store
  1016. :type 'boolean)
  1017. (defgroup org-link-follow nil
  1018. "Options concerning following links in Org-mode."
  1019. :tag "Org Follow Link"
  1020. :group 'org-link)
  1021. (defcustom org-link-translation-function nil
  1022. "Function to translate links with different syntax to Org syntax.
  1023. This can be used to translate links created for example by the Planner
  1024. or emacs-wiki packages to Org syntax.
  1025. The function must accept two parameters, a TYPE containing the link
  1026. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1027. which is everything after the link protocol. It should return a cons
  1028. with possibly modified values of type and path.
  1029. Org contains a function for this, so if you set this variable to
  1030. `org-translate-link-from-planner', you should be able follow many
  1031. links created by planner."
  1032. :group 'org-link-follow
  1033. :type 'function)
  1034. (defcustom org-follow-link-hook nil
  1035. "Hook that is run after a link has been followed."
  1036. :group 'org-link-follow
  1037. :type 'hook)
  1038. (defcustom org-tab-follows-link nil
  1039. "Non-nil means, on links TAB will follow the link.
  1040. Needs to be set before org.el is loaded.
  1041. This really should not be used, it does not make sense, and the
  1042. implementation is bad."
  1043. :group 'org-link-follow
  1044. :type 'boolean)
  1045. (defcustom org-return-follows-link nil
  1046. "Non-nil means, on links RET will follow the link.
  1047. Needs to be set before org.el is loaded."
  1048. :group 'org-link-follow
  1049. :type 'boolean)
  1050. (defcustom org-mouse-1-follows-link
  1051. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1052. "Non-nil means, mouse-1 on a link will follow the link.
  1053. A longer mouse click will still set point. Does not work on XEmacs.
  1054. Needs to be set before org.el is loaded."
  1055. :group 'org-link-follow
  1056. :type 'boolean)
  1057. (defcustom org-mark-ring-length 4
  1058. "Number of different positions to be recorded in the ring
  1059. Changing this requires a restart of Emacs to work correctly."
  1060. :group 'org-link-follow
  1061. :type 'integer)
  1062. (defcustom org-link-frame-setup
  1063. '((vm . vm-visit-folder-other-frame)
  1064. (gnus . gnus-other-frame)
  1065. (file . find-file-other-window))
  1066. "Setup the frame configuration for following links.
  1067. When following a link with Emacs, it may often be useful to display
  1068. this link in another window or frame. This variable can be used to
  1069. set this up for the different types of links.
  1070. For VM, use any of
  1071. `vm-visit-folder'
  1072. `vm-visit-folder-other-frame'
  1073. For Gnus, use any of
  1074. `gnus'
  1075. `gnus-other-frame'
  1076. `org-gnus-no-new-news'
  1077. For FILE, use any of
  1078. `find-file'
  1079. `find-file-other-window'
  1080. `find-file-other-frame'
  1081. For the calendar, use the variable `calendar-setup'.
  1082. For BBDB, it is currently only possible to display the matches in
  1083. another window."
  1084. :group 'org-link-follow
  1085. :type '(list
  1086. (cons (const vm)
  1087. (choice
  1088. (const vm-visit-folder)
  1089. (const vm-visit-folder-other-window)
  1090. (const vm-visit-folder-other-frame)))
  1091. (cons (const gnus)
  1092. (choice
  1093. (const gnus)
  1094. (const gnus-other-frame)
  1095. (const org-gnus-no-new-news)))
  1096. (cons (const file)
  1097. (choice
  1098. (const find-file)
  1099. (const find-file-other-window)
  1100. (const find-file-other-frame)))))
  1101. (defcustom org-display-internal-link-with-indirect-buffer nil
  1102. "Non-nil means, use indirect buffer to display infile links.
  1103. Activating internal links (from one location in a file to another location
  1104. in the same file) normally just jumps to the location. When the link is
  1105. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1106. another window. When this option is set, the other window actually displays
  1107. an indirect buffer clone of the current buffer, to avoid any visibility
  1108. changes to the current buffer."
  1109. :group 'org-link-follow
  1110. :type 'boolean)
  1111. (defcustom org-open-non-existing-files nil
  1112. "Non-nil means, `org-open-file' will open non-existing files.
  1113. When nil, an error will be generated.
  1114. This variable applies only to external applications because they
  1115. might choke on non-existing files. If the link is to a file that
  1116. will be openend in Emacs, the variable is ignored."
  1117. :group 'org-link-follow
  1118. :type 'boolean)
  1119. (defcustom org-open-directory-means-index-dot-org nil
  1120. "Non-nil means, a link to a directory really means to index.org.
  1121. When nil, following a directory link will run dired or open a finder/explorer
  1122. window on that directory."
  1123. :group 'org-link-follow
  1124. :type 'boolean)
  1125. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1126. "Function and arguments to call for following mailto links.
  1127. This is a list with the first element being a lisp function, and the
  1128. remaining elements being arguments to the function. In string arguments,
  1129. %a will be replaced by the address, and %s will be replaced by the subject
  1130. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1131. :group 'org-link-follow
  1132. :type '(choice
  1133. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1134. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1135. (const :tag "message-mail" (message-mail "%a" "%s"))
  1136. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1137. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1138. "Non-nil means, ask for confirmation before executing shell links.
  1139. Shell links can be dangerous: just think about a link
  1140. [[shell:rm -rf ~/*][Google Search]]
  1141. This link would show up in your Org-mode document as \"Google Search\",
  1142. but really it would remove your entire home directory.
  1143. Therefore we advise against setting this variable to nil.
  1144. Just change it to `y-or-n-p' if you want to confirm with a
  1145. single keystroke rather than having to type \"yes\"."
  1146. :group 'org-link-follow
  1147. :type '(choice
  1148. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1149. (const :tag "with y-or-n (faster)" y-or-n-p)
  1150. (const :tag "no confirmation (dangerous)" nil)))
  1151. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1152. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1153. Elisp links can be dangerous: just think about a link
  1154. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1155. This link would show up in your Org-mode document as \"Google Search\",
  1156. but really it would remove your entire home directory.
  1157. Therefore we advise against setting this variable to nil.
  1158. Just change it to `y-or-n-p' if you want to confirm with a
  1159. single keystroke rather than having to type \"yes\"."
  1160. :group 'org-link-follow
  1161. :type '(choice
  1162. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1163. (const :tag "with y-or-n (faster)" y-or-n-p)
  1164. (const :tag "no confirmation (dangerous)" nil)))
  1165. (defconst org-file-apps-defaults-gnu
  1166. '((remote . emacs)
  1167. (system . mailcap)
  1168. (t . mailcap))
  1169. "Default file applications on a UNIX or GNU/Linux system.
  1170. See `org-file-apps'.")
  1171. (defconst org-file-apps-defaults-macosx
  1172. '((remote . emacs)
  1173. (t . "open %s")
  1174. (system . "open %s")
  1175. ("ps.gz" . "gv %s")
  1176. ("eps.gz" . "gv %s")
  1177. ("dvi" . "xdvi %s")
  1178. ("fig" . "xfig %s"))
  1179. "Default file applications on a MacOS X system.
  1180. The system \"open\" is known as a default, but we use X11 applications
  1181. for some files for which the OS does not have a good default.
  1182. See `org-file-apps'.")
  1183. (defconst org-file-apps-defaults-windowsnt
  1184. (list
  1185. '(remote . emacs)
  1186. (cons t
  1187. (list (if (featurep 'xemacs)
  1188. 'mswindows-shell-execute
  1189. 'w32-shell-execute)
  1190. "open" 'file))
  1191. (cons 'system
  1192. (list (if (featurep 'xemacs)
  1193. 'mswindows-shell-execute
  1194. 'w32-shell-execute)
  1195. "open" 'file)))
  1196. "Default file applications on a Windows NT system.
  1197. The system \"open\" is used for most files.
  1198. See `org-file-apps'.")
  1199. (defcustom org-file-apps
  1200. '(
  1201. (auto-mode . emacs)
  1202. ("\\.x?html?\\'" . default)
  1203. ("\\.pdf\\'" . default)
  1204. )
  1205. "External applications for opening `file:path' items in a document.
  1206. Org-mode uses system defaults for different file types, but
  1207. you can use this variable to set the application for a given file
  1208. extension. The entries in this list are cons cells where the car identifies
  1209. files and the cdr the corresponding command. Possible values for the
  1210. file identifier are
  1211. \"regex\" Regular expression matched against the file name. For backward
  1212. compatibility, this can also be a string with only alphanumeric
  1213. characters, which is then interpreted as an extension.
  1214. `directory' Matches a directory
  1215. `remote' Matches a remote file, accessible through tramp or efs.
  1216. Remote files most likely should be visited through Emacs
  1217. because external applications cannot handle such paths.
  1218. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1219. so all files Emacs knows how to handle. Using this with
  1220. command `emacs' will open most files in Emacs. Beware that this
  1221. will also open html files inside Emacs, unless you add
  1222. (\"html\" . default) to the list as well.
  1223. t Default for files not matched by any of the other options.
  1224. `system' The system command to open files, like `open' on Windows
  1225. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1226. that will be selected if you call `C-c C-o' with a double
  1227. `C-u C-u' prefix.
  1228. Possible values for the command are:
  1229. `emacs' The file will be visited by the current Emacs process.
  1230. `default' Use the default application for this file type, which is the
  1231. association for t in the list, most likely in the system-specific
  1232. part.
  1233. This can be used to overrule an unwanted setting in the
  1234. system-specific variable.
  1235. `system' Use the system command for opening files, like \"open\".
  1236. This command is specified by the entry whose car is `system'.
  1237. Most likely, the system-specific version of this variable
  1238. does define this command, but you can overrule/replace it
  1239. here.
  1240. string A command to be executed by a shell; %s will be replaced
  1241. by the path to the file.
  1242. sexp A Lisp form which will be evaluated. The file path will
  1243. be available in the Lisp variable `file'.
  1244. For more examples, see the system specific constants
  1245. `org-file-apps-defaults-macosx'
  1246. `org-file-apps-defaults-windowsnt'
  1247. `org-file-apps-defaults-gnu'."
  1248. :group 'org-link-follow
  1249. :type '(repeat
  1250. (cons (choice :value ""
  1251. (string :tag "Extension")
  1252. (const :tag "System command to open files" system)
  1253. (const :tag "Default for unrecognized files" t)
  1254. (const :tag "Remote file" remote)
  1255. (const :tag "Links to a directory" directory)
  1256. (const :tag "Any files that have Emacs modes"
  1257. auto-mode))
  1258. (choice :value ""
  1259. (const :tag "Visit with Emacs" emacs)
  1260. (const :tag "Use default" default)
  1261. (const :tag "Use the system command" system)
  1262. (string :tag "Command")
  1263. (sexp :tag "Lisp form")))))
  1264. (defgroup org-refile nil
  1265. "Options concerning refiling entries in Org-mode."
  1266. :tag "Org Refile"
  1267. :group 'org)
  1268. (defcustom org-directory "~/org"
  1269. "Directory with org files.
  1270. This is just a default location to look for Org files. There is no need
  1271. at all to put your files into this directory. It is only used in the
  1272. following situations:
  1273. 1. When a remember template specifies a target file that is not an
  1274. absolute path. The path will then be interpreted relative to
  1275. `org-directory'
  1276. 2. When a remember note is filed away in an interactive way (when exiting the
  1277. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1278. with `org-directory' as the default path."
  1279. :group 'org-refile
  1280. :group 'org-remember
  1281. :type 'directory)
  1282. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1283. "Default target for storing notes.
  1284. Used by the hooks for remember.el. This can be a string, or nil to mean
  1285. the value of `remember-data-file'.
  1286. You can set this on a per-template basis with the variable
  1287. `org-remember-templates'."
  1288. :group 'org-refile
  1289. :group 'org-remember
  1290. :type '(choice
  1291. (const :tag "Default from remember-data-file" nil)
  1292. file))
  1293. (defcustom org-goto-interface 'outline
  1294. "The default interface to be used for `org-goto'.
  1295. Allowed values are:
  1296. outline The interface shows an outline of the relevant file
  1297. and the correct heading is found by moving through
  1298. the outline or by searching with incremental search.
  1299. outline-path-completion Headlines in the current buffer are offered via
  1300. completion. This is the interface also used by
  1301. the refile command."
  1302. :group 'org-refile
  1303. :type '(choice
  1304. (const :tag "Outline" outline)
  1305. (const :tag "Outline-path-completion" outline-path-completion)))
  1306. (defcustom org-goto-max-level 5
  1307. "Maximum level to be considered when running org-goto with refile interface."
  1308. :group 'org-refile
  1309. :type 'integer)
  1310. (defcustom org-reverse-note-order nil
  1311. "Non-nil means, store new notes at the beginning of a file or entry.
  1312. When nil, new notes will be filed to the end of a file or entry.
  1313. This can also be a list with cons cells of regular expressions that
  1314. are matched against file names, and values."
  1315. :group 'org-remember
  1316. :group 'org-refile
  1317. :type '(choice
  1318. (const :tag "Reverse always" t)
  1319. (const :tag "Reverse never" nil)
  1320. (repeat :tag "By file name regexp"
  1321. (cons regexp boolean))))
  1322. (defcustom org-refile-targets nil
  1323. "Targets for refiling entries with \\[org-refile].
  1324. This is list of cons cells. Each cell contains:
  1325. - a specification of the files to be considered, either a list of files,
  1326. or a symbol whose function or variable value will be used to retrieve
  1327. a file name or a list of file names. If you use `org-agenda-files' for
  1328. that, all agenda files will be scanned for targets. Nil means, consider
  1329. headings in the current buffer.
  1330. - A specification of how to find candidate refile targets. This may be
  1331. any of:
  1332. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1333. This tag has to be present in all target headlines, inheritance will
  1334. not be considered.
  1335. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1336. todo keyword.
  1337. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1338. headlines that are refiling targets.
  1339. - a cons cell (:level . N). Any headline of level N is considered a target.
  1340. Note that, when `org-odd-levels-only' is set, level corresponds to
  1341. order in hierarchy, not to the number of stars.
  1342. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1343. Note that, when `org-odd-levels-only' is set, level corresponds to
  1344. order in hierarchy, not to the number of stars.
  1345. You can set the variable `org-refile-target-verify-function' to a function
  1346. to verify each headline found by the simple critery above.
  1347. When this variable is nil, all top-level headlines in the current buffer
  1348. are used, equivalent to the value `((nil . (:level . 1))'."
  1349. :group 'org-refile
  1350. :type '(repeat
  1351. (cons
  1352. (choice :value org-agenda-files
  1353. (const :tag "All agenda files" org-agenda-files)
  1354. (const :tag "Current buffer" nil)
  1355. (function) (variable) (file))
  1356. (choice :tag "Identify target headline by"
  1357. (cons :tag "Specific tag" (const :value :tag) (string))
  1358. (cons :tag "TODO keyword" (const :value :todo) (string))
  1359. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1360. (cons :tag "Level number" (const :value :level) (integer))
  1361. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1362. (defcustom org-refile-target-verify-function nil
  1363. "Function to verify if the headline at point should be a refile target.
  1364. The function will be called without arguments, with point at the
  1365. beginning of the headline. It should return t and leave point
  1366. where it is if the headline is a valid target for refiling.
  1367. If the target should not be selected, the function must return nil.
  1368. In addition to this, it may move point to a place from where the search
  1369. should be continued. For example, the function may decide that the entire
  1370. subtree of the current entry should be excluded and move point to the end
  1371. of the subtree."
  1372. :group 'org-refile
  1373. :type 'function)
  1374. (defcustom org-refile-use-outline-path nil
  1375. "Non-nil means, provide refile targets as paths.
  1376. So a level 3 headline will be available as level1/level2/level3.
  1377. When the value is `file', also include the file name (without directory)
  1378. into the path. In this case, you can also stop the completion after
  1379. the file name, to get entries inserted as top level in the file.
  1380. When `full-file-path', include the full file path."
  1381. :group 'org-refile
  1382. :type '(choice
  1383. (const :tag "Not" nil)
  1384. (const :tag "Yes" t)
  1385. (const :tag "Start with file name" file)
  1386. (const :tag "Start with full file path" full-file-path)))
  1387. (defcustom org-outline-path-complete-in-steps t
  1388. "Non-nil means, complete the outline path in hierarchical steps.
  1389. When Org-mode uses the refile interface to select an outline path
  1390. \(see variable `org-refile-use-outline-path'), the completion of
  1391. the path can be done is a single go, or if can be done in steps down
  1392. the headline hierarchy. Going in steps is probably the best if you
  1393. do not use a special completion package like `ido' or `icicles'.
  1394. However, when using these packages, going in one step can be very
  1395. fast, while still showing the whole path to the entry."
  1396. :group 'org-refile
  1397. :type 'boolean)
  1398. (defcustom org-refile-allow-creating-parent-nodes nil
  1399. "Non-nil means, allow to create new nodes as refile targets.
  1400. New nodes are then created by adding \"/new node name\" to the completion
  1401. of an existing node. When the value of this variable is `confirm',
  1402. new node creation must be confirmed by the user (recommended)
  1403. When nil, the completion must match an existing entry.
  1404. Note that, if the new heading is not seen by the criteria
  1405. listed in `org-refile-targets', multiple instances of the same
  1406. heading would be created by trying again to file under the new
  1407. heading."
  1408. :group 'org-refile
  1409. :type '(choice
  1410. (const :tag "Never" nil)
  1411. (const :tag "Always" t)
  1412. (const :tag "Prompt for confirmation" confirm)))
  1413. (defgroup org-todo nil
  1414. "Options concerning TODO items in Org-mode."
  1415. :tag "Org TODO"
  1416. :group 'org)
  1417. (defgroup org-progress nil
  1418. "Options concerning Progress logging in Org-mode."
  1419. :tag "Org Progress"
  1420. :group 'org-time)
  1421. (defvar org-todo-interpretation-widgets
  1422. '(
  1423. (:tag "Sequence (cycling hits every state)" sequence)
  1424. (:tag "Type (cycling directly to DONE)" type))
  1425. "The available interpretation symbols for customizing
  1426. `org-todo-keywords'.
  1427. Interested libraries should add to this list.")
  1428. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1429. "List of TODO entry keyword sequences and their interpretation.
  1430. \\<org-mode-map>This is a list of sequences.
  1431. Each sequence starts with a symbol, either `sequence' or `type',
  1432. indicating if the keywords should be interpreted as a sequence of
  1433. action steps, or as different types of TODO items. The first
  1434. keywords are states requiring action - these states will select a headline
  1435. for inclusion into the global TODO list Org-mode produces. If one of
  1436. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1437. signify that no further action is necessary. If \"|\" is not found,
  1438. the last keyword is treated as the only DONE state of the sequence.
  1439. The command \\[org-todo] cycles an entry through these states, and one
  1440. additional state where no keyword is present. For details about this
  1441. cycling, see the manual.
  1442. TODO keywords and interpretation can also be set on a per-file basis with
  1443. the special #+SEQ_TODO and #+TYP_TODO lines.
  1444. Each keyword can optionally specify a character for fast state selection
  1445. \(in combination with the variable `org-use-fast-todo-selection')
  1446. and specifiers for state change logging, using the same syntax
  1447. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1448. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1449. indicates to record a time stamp each time this state is selected.
  1450. Each keyword may also specify if a timestamp or a note should be
  1451. recorded when entering or leaving the state, by adding additional
  1452. characters in the parenthesis after the keyword. This looks like this:
  1453. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1454. record only the time of the state change. With X and Y being either
  1455. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1456. Y when leaving the state if and only if the *target* state does not
  1457. define X. You may omit any of the fast-selection key or X or /Y,
  1458. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1459. For backward compatibility, this variable may also be just a list
  1460. of keywords - in this case the interpretation (sequence or type) will be
  1461. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1462. :group 'org-todo
  1463. :group 'org-keywords
  1464. :type '(choice
  1465. (repeat :tag "Old syntax, just keywords"
  1466. (string :tag "Keyword"))
  1467. (repeat :tag "New syntax"
  1468. (cons
  1469. (choice
  1470. :tag "Interpretation"
  1471. ;;Quick and dirty way to see
  1472. ;;`org-todo-interpretations'. This takes the
  1473. ;;place of item arguments
  1474. :convert-widget
  1475. (lambda (widget)
  1476. (widget-put widget
  1477. :args (mapcar
  1478. #'(lambda (x)
  1479. (widget-convert
  1480. (cons 'const x)))
  1481. org-todo-interpretation-widgets))
  1482. widget))
  1483. (repeat
  1484. (string :tag "Keyword"))))))
  1485. (defvar org-todo-keywords-1 nil
  1486. "All TODO and DONE keywords active in a buffer.")
  1487. (make-variable-buffer-local 'org-todo-keywords-1)
  1488. (defvar org-todo-keywords-for-agenda nil)
  1489. (defvar org-done-keywords-for-agenda nil)
  1490. (defvar org-todo-keyword-alist-for-agenda nil)
  1491. (defvar org-tag-alist-for-agenda nil)
  1492. (defvar org-agenda-contributing-files nil)
  1493. (defvar org-not-done-keywords nil)
  1494. (make-variable-buffer-local 'org-not-done-keywords)
  1495. (defvar org-done-keywords nil)
  1496. (make-variable-buffer-local 'org-done-keywords)
  1497. (defvar org-todo-heads nil)
  1498. (make-variable-buffer-local 'org-todo-heads)
  1499. (defvar org-todo-sets nil)
  1500. (make-variable-buffer-local 'org-todo-sets)
  1501. (defvar org-todo-log-states nil)
  1502. (make-variable-buffer-local 'org-todo-log-states)
  1503. (defvar org-todo-kwd-alist nil)
  1504. (make-variable-buffer-local 'org-todo-kwd-alist)
  1505. (defvar org-todo-key-alist nil)
  1506. (make-variable-buffer-local 'org-todo-key-alist)
  1507. (defvar org-todo-key-trigger nil)
  1508. (make-variable-buffer-local 'org-todo-key-trigger)
  1509. (defcustom org-todo-interpretation 'sequence
  1510. "Controls how TODO keywords are interpreted.
  1511. This variable is in principle obsolete and is only used for
  1512. backward compatibility, if the interpretation of todo keywords is
  1513. not given already in `org-todo-keywords'. See that variable for
  1514. more information."
  1515. :group 'org-todo
  1516. :group 'org-keywords
  1517. :type '(choice (const sequence)
  1518. (const type)))
  1519. (defcustom org-use-fast-todo-selection t
  1520. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1521. This variable describes if and under what circumstances the cycling
  1522. mechanism for TODO keywords will be replaced by a single-key, direct
  1523. selection scheme.
  1524. When nil, fast selection is never used.
  1525. When the symbol `prefix', it will be used when `org-todo' is called with
  1526. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1527. in an agenda buffer.
  1528. When t, fast selection is used by default. In this case, the prefix
  1529. argument forces cycling instead.
  1530. In all cases, the special interface is only used if access keys have actually
  1531. been assigned by the user, i.e. if keywords in the configuration are followed
  1532. by a letter in parenthesis, like TODO(t)."
  1533. :group 'org-todo
  1534. :type '(choice
  1535. (const :tag "Never" nil)
  1536. (const :tag "By default" t)
  1537. (const :tag "Only with C-u C-c C-t" prefix)))
  1538. (defcustom org-provide-todo-statistics t
  1539. "Non-nil means, update todo statistics after insert and toggle.
  1540. ALL-HEADLINES means update todo statistics by including headlines
  1541. with no TODO keyword as well, counting them as not done.
  1542. A list of TODO keywords means the same, but skip keywords that are
  1543. not in this list.
  1544. When this is set, todo statistics is updated in the parent of the
  1545. current entry each time a todo state is changed."
  1546. :group 'org-todo
  1547. :type '(choice
  1548. (const :tag "Yes, only for TODO entries" t)
  1549. (const :tag "Yes, including all entries" 'all-headlines)
  1550. (repeat :tag "Yes, for TODOs in this list"
  1551. (string :tag "TODO keyword"))
  1552. (other :tag "No TODO statistics" nil)))
  1553. (defcustom org-hierarchical-todo-statistics t
  1554. "Non-nil means, TODO statistics covers just direct children.
  1555. When nil, all entries in the subtree are considered.
  1556. This has only an effect if `org-provide-todo-statistics' is set.
  1557. To set this to nil for only a single subtree, use a COOKIE_DATA
  1558. property and include the word \"recursive\" into the value."
  1559. :group 'org-todo
  1560. :type 'boolean)
  1561. (defcustom org-after-todo-state-change-hook nil
  1562. "Hook which is run after the state of a TODO item was changed.
  1563. The new state (a string with a TODO keyword, or nil) is available in the
  1564. Lisp variable `state'."
  1565. :group 'org-todo
  1566. :type 'hook)
  1567. (defvar org-blocker-hook nil
  1568. "Hook for functions that are allowed to block a state change.
  1569. Each function gets as its single argument a property list, see
  1570. `org-trigger-hook' for more information about this list.
  1571. If any of the functions in this hook returns nil, the state change
  1572. is blocked.")
  1573. (defvar org-trigger-hook nil
  1574. "Hook for functions that are triggered by a state change.
  1575. Each function gets as its single argument a property list with at least
  1576. the following elements:
  1577. (:type type-of-change :position pos-at-entry-start
  1578. :from old-state :to new-state)
  1579. Depending on the type, more properties may be present.
  1580. This mechanism is currently implemented for:
  1581. TODO state changes
  1582. ------------------
  1583. :type todo-state-change
  1584. :from previous state (keyword as a string), or nil, or a symbol
  1585. 'todo' or 'done', to indicate the general type of state.
  1586. :to new state, like in :from")
  1587. (defcustom org-enforce-todo-dependencies nil
  1588. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1589. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1590. be blocked if any prior sibling is not yet done.
  1591. Finally, if the parent is blocked because of ordered siblings of its own,
  1592. the child will also be blocked.
  1593. This variable needs to be set before org.el is loaded, and you need to
  1594. restart Emacs after a change to make the change effective. The only way
  1595. to change is while Emacs is running is through the customize interface."
  1596. :set (lambda (var val)
  1597. (set var val)
  1598. (if val
  1599. (add-hook 'org-blocker-hook
  1600. 'org-block-todo-from-children-or-siblings-or-parent)
  1601. (remove-hook 'org-blocker-hook
  1602. 'org-block-todo-from-children-or-siblings-or-parent)))
  1603. :group 'org-todo
  1604. :type 'boolean)
  1605. (defcustom org-enforce-todo-checkbox-dependencies nil
  1606. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1607. When this is nil, checkboxes have no influence on switching TODO states.
  1608. When non-nil, you first need to check off all check boxes before the TODO
  1609. entry can be switched to DONE.
  1610. This variable needs to be set before org.el is loaded, and you need to
  1611. restart Emacs after a change to make the change effective. The only way
  1612. to change is while Emacs is running is through the customize interface."
  1613. :set (lambda (var val)
  1614. (set var val)
  1615. (if val
  1616. (add-hook 'org-blocker-hook
  1617. 'org-block-todo-from-checkboxes)
  1618. (remove-hook 'org-blocker-hook
  1619. 'org-block-todo-from-checkboxes)))
  1620. :group 'org-todo
  1621. :type 'boolean)
  1622. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1623. "Non-nil means, inserting a TODO heading is treated as state change.
  1624. So when the command \\[org-insert-todo-heading] is used, state change
  1625. logging will apply if appropriate. When nil, the new TODO item will
  1626. be inserted directly, and no logging will take place."
  1627. :group 'org-todo
  1628. :type 'boolean)
  1629. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1630. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1631. This is the default behavior. However, setting this to nil allows a
  1632. convenient way to select a TODO state and bypass any logging associated
  1633. with that."
  1634. :group 'org-todo
  1635. :type 'boolean)
  1636. (defcustom org-todo-state-tags-triggers nil
  1637. "Tag changes that should be triggered by TODO state changes.
  1638. This is a list. Each entry is
  1639. (state-change (tag . flag) .......)
  1640. State-change can be a string with a state, and empty string to indicate the
  1641. state that has no TODO keyword, or it can be one of the symbols `todo'
  1642. or `done', meaning any not-done or done state, respectively."
  1643. :group 'org-todo
  1644. :group 'org-tags
  1645. :type '(repeat
  1646. (cons (choice :tag "When changing to"
  1647. (const :tag "Not-done state" todo)
  1648. (const :tag "Done state" done)
  1649. (string :tag "State"))
  1650. (repeat
  1651. (cons :tag "Tag action"
  1652. (string :tag "Tag")
  1653. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1654. (defcustom org-log-done nil
  1655. "Information to record when a task moves to the DONE state.
  1656. Possible values are:
  1657. nil Don't add anything, just change the keyword
  1658. time Add a time stamp to the task
  1659. note Prompt a closing note and add it with template `org-log-note-headings'
  1660. This option can also be set with on a per-file-basis with
  1661. #+STARTUP: nologdone
  1662. #+STARTUP: logdone
  1663. #+STARTUP: lognotedone
  1664. You can have local logging settings for a subtree by setting the LOGGING
  1665. property to one or more of these keywords."
  1666. :group 'org-todo
  1667. :group 'org-progress
  1668. :type '(choice
  1669. (const :tag "No logging" nil)
  1670. (const :tag "Record CLOSED timestamp" time)
  1671. (const :tag "Record CLOSED timestamp with closing note." note)))
  1672. ;; Normalize old uses of org-log-done.
  1673. (cond
  1674. ((eq org-log-done t) (setq org-log-done 'time))
  1675. ((and (listp org-log-done) (memq 'done org-log-done))
  1676. (setq org-log-done 'note)))
  1677. (defcustom org-log-note-clock-out nil
  1678. "Non-nil means, record a note when clocking out of an item.
  1679. This can also be configured on a per-file basis by adding one of
  1680. the following lines anywhere in the buffer:
  1681. #+STARTUP: lognoteclock-out
  1682. #+STARTUP: nolognoteclock-out"
  1683. :group 'org-todo
  1684. :group 'org-progress
  1685. :type 'boolean)
  1686. (defcustom org-log-done-with-time t
  1687. "Non-nil means, the CLOSED time stamp will contain date and time.
  1688. When nil, only the date will be recorded."
  1689. :group 'org-progress
  1690. :type 'boolean)
  1691. (defcustom org-log-note-headings
  1692. '((done . "CLOSING NOTE %t")
  1693. (state . "State %-12s from %-12S %t")
  1694. (note . "Note taken on %t")
  1695. (clock-out . ""))
  1696. "Headings for notes added to entries.
  1697. The value is an alist, with the car being a symbol indicating the note
  1698. context, and the cdr is the heading to be used. The heading may also be the
  1699. empty string.
  1700. %t in the heading will be replaced by a time stamp.
  1701. %s will be replaced by the new TODO state, in double quotes.
  1702. %S will be replaced by the old TODO state, in double quotes.
  1703. %u will be replaced by the user name.
  1704. %U will be replaced by the full user name."
  1705. :group 'org-todo
  1706. :group 'org-progress
  1707. :type '(list :greedy t
  1708. (cons (const :tag "Heading when closing an item" done) string)
  1709. (cons (const :tag
  1710. "Heading when changing todo state (todo sequence only)"
  1711. state) string)
  1712. (cons (const :tag "Heading when just taking a note" note) string)
  1713. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1714. (unless (assq 'note org-log-note-headings)
  1715. (push '(note . "%t") org-log-note-headings))
  1716. (defcustom org-log-into-drawer nil
  1717. "Non-nil means, insert state change notes and time stamps into a drawer.
  1718. When nil, state changes notes will be inserted after the headline and
  1719. any scheduling and clock lines, but not inside a drawer.
  1720. The value of this variable should be the name of the drawer to use.
  1721. LOGBOOK is proposed at the default drawer for this purpose, you can
  1722. also set this to a string to define the drawer of your choice.
  1723. A value of t is also allowed, representing \"LOGBOOK\".
  1724. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1725. will be ignored.
  1726. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1727. a subtree."
  1728. :group 'org-todo
  1729. :group 'org-progress
  1730. :type '(choice
  1731. (const :tag "Not into a drawer" nil)
  1732. (const :tag "LOGBOOK" t)
  1733. (string :tag "Other")))
  1734. (if (fboundp 'defvaralias)
  1735. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1736. (defun org-log-into-drawer ()
  1737. "Return the value of `org-log-into-drawer', but let properties overrule.
  1738. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1739. used instead of the default value."
  1740. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1741. (cond
  1742. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1743. ((equal p "t") "LOGBOOK")
  1744. (t p))))
  1745. (defcustom org-log-state-notes-insert-after-drawers nil
  1746. "Non-nil means, insert state change notes after any drawers in entry.
  1747. Only the drawers that *immediately* follow the headline and the
  1748. deadline/scheduled line are skipped.
  1749. When nil, insert notes right after the heading and perhaps the line
  1750. with deadline/scheduling if present.
  1751. This variable will have no effect if `org-log-into-drawer' is
  1752. set."
  1753. :group 'org-todo
  1754. :group 'org-progress
  1755. :type 'boolean)
  1756. (defcustom org-log-states-order-reversed t
  1757. "Non-nil means, the latest state change note will be directly after heading.
  1758. When nil, the notes will be orderer according to time."
  1759. :group 'org-todo
  1760. :group 'org-progress
  1761. :type 'boolean)
  1762. (defcustom org-log-repeat 'time
  1763. "Non-nil means, record moving through the DONE state when triggering repeat.
  1764. An auto-repeating tasks is immediately switched back to TODO when marked
  1765. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1766. the TODO keyword definition, or recording a closing note by setting
  1767. `org-log-done', there will be no record of the task moving through DONE.
  1768. This variable forces taking a note anyway. Possible values are:
  1769. nil Don't force a record
  1770. time Record a time stamp
  1771. note Record a note
  1772. This option can also be set with on a per-file-basis with
  1773. #+STARTUP: logrepeat
  1774. #+STARTUP: lognoterepeat
  1775. #+STARTUP: nologrepeat
  1776. You can have local logging settings for a subtree by setting the LOGGING
  1777. property to one or more of these keywords."
  1778. :group 'org-todo
  1779. :group 'org-progress
  1780. :type '(choice
  1781. (const :tag "Don't force a record" nil)
  1782. (const :tag "Force recording the DONE state" time)
  1783. (const :tag "Force recording a note with the DONE state" note)))
  1784. (defgroup org-priorities nil
  1785. "Priorities in Org-mode."
  1786. :tag "Org Priorities"
  1787. :group 'org-todo)
  1788. (defcustom org-enable-priority-commands t
  1789. "Non-nil means, priority commands are active.
  1790. When nil, these commands will be disabled, so that you never accidentally
  1791. set a priority."
  1792. :group 'org-priorities
  1793. :type 'boolean)
  1794. (defcustom org-highest-priority ?A
  1795. "The highest priority of TODO items. A character like ?A, ?B etc.
  1796. Must have a smaller ASCII number than `org-lowest-priority'."
  1797. :group 'org-priorities
  1798. :type 'character)
  1799. (defcustom org-lowest-priority ?C
  1800. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1801. Must have a larger ASCII number than `org-highest-priority'."
  1802. :group 'org-priorities
  1803. :type 'character)
  1804. (defcustom org-default-priority ?B
  1805. "The default priority of TODO items.
  1806. This is the priority an item get if no explicit priority is given."
  1807. :group 'org-priorities
  1808. :type 'character)
  1809. (defcustom org-priority-start-cycle-with-default t
  1810. "Non-nil means, start with default priority when starting to cycle.
  1811. When this is nil, the first step in the cycle will be (depending on the
  1812. command used) one higher or lower that the default priority."
  1813. :group 'org-priorities
  1814. :type 'boolean)
  1815. (defgroup org-time nil
  1816. "Options concerning time stamps and deadlines in Org-mode."
  1817. :tag "Org Time"
  1818. :group 'org)
  1819. (defcustom org-insert-labeled-timestamps-at-point nil
  1820. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1821. When nil, these labeled time stamps are forces into the second line of an
  1822. entry, just after the headline. When scheduling from the global TODO list,
  1823. the time stamp will always be forced into the second line."
  1824. :group 'org-time
  1825. :type 'boolean)
  1826. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1827. "Formats for `format-time-string' which are used for time stamps.
  1828. It is not recommended to change this constant.")
  1829. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1830. "Number of minutes to round time stamps to.
  1831. These are two values, the first applies when first creating a time stamp.
  1832. The second applies when changing it with the commands `S-up' and `S-down'.
  1833. When changing the time stamp, this means that it will change in steps
  1834. of N minutes, as given by the second value.
  1835. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1836. numbers should be factors of 60, so for example 5, 10, 15.
  1837. When this is larger than 1, you can still force an exact time-stamp by using
  1838. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1839. and by using a prefix arg to `S-up/down' to specify the exact number
  1840. of minutes to shift."
  1841. :group 'org-time
  1842. :get '(lambda (var) ; Make sure all entries have 5 elements
  1843. (if (integerp (default-value var))
  1844. (list (default-value var) 5)
  1845. (default-value var)))
  1846. :type '(list
  1847. (integer :tag "when inserting times")
  1848. (integer :tag "when modifying times")))
  1849. ;; Normalize old customizations of this variable.
  1850. (when (integerp org-time-stamp-rounding-minutes)
  1851. (setq org-time-stamp-rounding-minutes
  1852. (list org-time-stamp-rounding-minutes
  1853. org-time-stamp-rounding-minutes)))
  1854. (defcustom org-display-custom-times nil
  1855. "Non-nil means, overlay custom formats over all time stamps.
  1856. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1857. To turn this on on a per-file basis, insert anywhere in the file:
  1858. #+STARTUP: customtime"
  1859. :group 'org-time
  1860. :set 'set-default
  1861. :type 'sexp)
  1862. (make-variable-buffer-local 'org-display-custom-times)
  1863. (defcustom org-time-stamp-custom-formats
  1864. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1865. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1866. These are overlayed over the default ISO format if the variable
  1867. `org-display-custom-times' is set. Time like %H:%M should be at the
  1868. end of the second format. The custom formats are also honored by export
  1869. commands, if custom time display is turned on at the time of export."
  1870. :group 'org-time
  1871. :type 'sexp)
  1872. (defun org-time-stamp-format (&optional long inactive)
  1873. "Get the right format for a time string."
  1874. (let ((f (if long (cdr org-time-stamp-formats)
  1875. (car org-time-stamp-formats))))
  1876. (if inactive
  1877. (concat "[" (substring f 1 -1) "]")
  1878. f)))
  1879. (defcustom org-time-clocksum-format "%d:%02d"
  1880. "The format string used when creating CLOCKSUM lines, or when
  1881. org-mode generates a time duration."
  1882. :group 'org-time
  1883. :type 'string)
  1884. (defcustom org-deadline-warning-days 14
  1885. "No. of days before expiration during which a deadline becomes active.
  1886. This variable governs the display in sparse trees and in the agenda.
  1887. When 0 or negative, it means use this number (the absolute value of it)
  1888. even if a deadline has a different individual lead time specified.
  1889. Custom commands can set this variable in the options section."
  1890. :group 'org-time
  1891. :group 'org-agenda-daily/weekly
  1892. :type 'integer)
  1893. (defcustom org-read-date-prefer-future t
  1894. "Non-nil means, assume future for incomplete date input from user.
  1895. This affects the following situations:
  1896. 1. The user gives a day, but no month.
  1897. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1898. read this as the third of *next* month. However, if you enter \"17\",
  1899. it will be considered as *this* month.
  1900. 2. The user gives a month but not a year.
  1901. For example, if it is april and you enter \"feb 2\", this will be read
  1902. as feb 2, *next* year. \"May 5\", however, will be this year.
  1903. Currently this does not work for ISO week specifications.
  1904. When this option is nil, the current month and year will always be used
  1905. as defaults."
  1906. :group 'org-time
  1907. :type 'boolean)
  1908. (defcustom org-read-date-display-live t
  1909. "Non-nil means, display current interpretation of date prompt live.
  1910. This display will be in an overlay, in the minibuffer."
  1911. :group 'org-time
  1912. :type 'boolean)
  1913. (defcustom org-read-date-popup-calendar t
  1914. "Non-nil means, pop up a calendar when prompting for a date.
  1915. In the calendar, the date can be selected with mouse-1. However, the
  1916. minibuffer will also be active, and you can simply enter the date as well.
  1917. When nil, only the minibuffer will be available."
  1918. :group 'org-time
  1919. :type 'boolean)
  1920. (if (fboundp 'defvaralias)
  1921. (defvaralias 'org-popup-calendar-for-date-prompt
  1922. 'org-read-date-popup-calendar))
  1923. (defcustom org-read-date-minibuffer-setup-hook nil
  1924. "Hook to be used to set up keys for the date/time interface.
  1925. Add key definitions to `minibuffer-local-map', which will be a temporary
  1926. copy."
  1927. :group 'org-time
  1928. :type 'hook)
  1929. (defcustom org-extend-today-until 0
  1930. "The hour when your day really ends. Must be an integer.
  1931. This has influence for the following applications:
  1932. - When switching the agenda to \"today\". It it is still earlier than
  1933. the time given here, the day recognized as TODAY is actually yesterday.
  1934. - When a date is read from the user and it is still before the time given
  1935. here, the current date and time will be assumed to be yesterday, 23:59.
  1936. Also, timestamps inserted in remember templates follow this rule.
  1937. IMPORTANT: This is a feature whose implementation is and likely will
  1938. remain incomplete. Really, it is only here because past midnight seems to
  1939. be the favorite working time of John Wiegley :-)"
  1940. :group 'org-time
  1941. :type 'integer)
  1942. (defcustom org-edit-timestamp-down-means-later nil
  1943. "Non-nil means, S-down will increase the time in a time stamp.
  1944. When nil, S-up will increase."
  1945. :group 'org-time
  1946. :type 'boolean)
  1947. (defcustom org-calendar-follow-timestamp-change t
  1948. "Non-nil means, make the calendar window follow timestamp changes.
  1949. When a timestamp is modified and the calendar window is visible, it will be
  1950. moved to the new date."
  1951. :group 'org-time
  1952. :type 'boolean)
  1953. (defgroup org-tags nil
  1954. "Options concerning tags in Org-mode."
  1955. :tag "Org Tags"
  1956. :group 'org)
  1957. (defcustom org-tag-alist nil
  1958. "List of tags allowed in Org-mode files.
  1959. When this list is nil, Org-mode will base TAG input on what is already in the
  1960. buffer.
  1961. The value of this variable is an alist, the car of each entry must be a
  1962. keyword as a string, the cdr may be a character that is used to select
  1963. that tag through the fast-tag-selection interface.
  1964. See the manual for details."
  1965. :group 'org-tags
  1966. :type '(repeat
  1967. (choice
  1968. (cons (string :tag "Tag name")
  1969. (character :tag "Access char"))
  1970. (const :tag "Start radio group" (:startgroup))
  1971. (const :tag "End radio group" (:endgroup))
  1972. (const :tag "New line" (:newline)))))
  1973. (defcustom org-tag-persistent-alist nil
  1974. "List of tags that will always appear in all Org-mode files.
  1975. This is in addition to any in buffer settings or customizations
  1976. of `org-tag-alist'.
  1977. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1978. The value of this variable is an alist, the car of each entry must be a
  1979. keyword as a string, the cdr may be a character that is used to select
  1980. that tag through the fast-tag-selection interface.
  1981. See the manual for details.
  1982. To disable these tags on a per-file basis, insert anywhere in the file:
  1983. #+STARTUP: noptag"
  1984. :group 'org-tags
  1985. :type '(repeat
  1986. (choice
  1987. (cons (string :tag "Tag name")
  1988. (character :tag "Access char"))
  1989. (const :tag "Start radio group" (:startgroup))
  1990. (const :tag "End radio group" (:endgroup))
  1991. (const :tag "New line" (:newline)))))
  1992. (defvar org-file-tags nil
  1993. "List of tags that can be inherited by all entries in the file.
  1994. The tags will be inherited if the variable `org-use-tag-inheritance'
  1995. says they should be.
  1996. This variable is populated from #+TAG lines.")
  1997. (defcustom org-use-fast-tag-selection 'auto
  1998. "Non-nil means, use fast tag selection scheme.
  1999. This is a special interface to select and deselect tags with single keys.
  2000. When nil, fast selection is never used.
  2001. When the symbol `auto', fast selection is used if and only if selection
  2002. characters for tags have been configured, either through the variable
  2003. `org-tag-alist' or through a #+TAGS line in the buffer.
  2004. When t, fast selection is always used and selection keys are assigned
  2005. automatically if necessary."
  2006. :group 'org-tags
  2007. :type '(choice
  2008. (const :tag "Always" t)
  2009. (const :tag "Never" nil)
  2010. (const :tag "When selection characters are configured" 'auto)))
  2011. (defcustom org-fast-tag-selection-single-key nil
  2012. "Non-nil means, fast tag selection exits after first change.
  2013. When nil, you have to press RET to exit it.
  2014. During fast tag selection, you can toggle this flag with `C-c'.
  2015. This variable can also have the value `expert'. In this case, the window
  2016. displaying the tags menu is not even shown, until you press C-c again."
  2017. :group 'org-tags
  2018. :type '(choice
  2019. (const :tag "No" nil)
  2020. (const :tag "Yes" t)
  2021. (const :tag "Expert" expert)))
  2022. (defvar org-fast-tag-selection-include-todo nil
  2023. "Non-nil means, fast tags selection interface will also offer TODO states.
  2024. This is an undocumented feature, you should not rely on it.")
  2025. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2026. "The column to which tags should be indented in a headline.
  2027. If this number is positive, it specifies the column. If it is negative,
  2028. it means that the tags should be flushright to that column. For example,
  2029. -80 works well for a normal 80 character screen."
  2030. :group 'org-tags
  2031. :type 'integer)
  2032. (defcustom org-auto-align-tags t
  2033. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2034. These operations change the length of a headline and therefore shift
  2035. the tags around. With this options turned on, after each such operation
  2036. the tags are again aligned to `org-tags-column'."
  2037. :group 'org-tags
  2038. :type 'boolean)
  2039. (defcustom org-use-tag-inheritance t
  2040. "Non-nil means, tags in levels apply also for sublevels.
  2041. When nil, only the tags directly given in a specific line apply there.
  2042. This may also be a list of tags that should be inherited, or a regexp that
  2043. matches tags that should be inherited. Additional control is possible
  2044. with the variable `org-tags-exclude-from-inheritance' which gives an
  2045. explicit list of tags to be excluded from inheritance., even if the value of
  2046. `org-use-tag-inheritance' would select it for inheritance.
  2047. If this option is t, a match early-on in a tree can lead to a large
  2048. number of matches in the subtree when constructing the agenda or creating
  2049. a sparse tree. If you only want to see the first match in a tree during
  2050. a search, check out the variable `org-tags-match-list-sublevels'."
  2051. :group 'org-tags
  2052. :type '(choice
  2053. (const :tag "Not" nil)
  2054. (const :tag "Always" t)
  2055. (repeat :tag "Specific tags" (string :tag "Tag"))
  2056. (regexp :tag "Tags matched by regexp")))
  2057. (defcustom org-tags-exclude-from-inheritance nil
  2058. "List of tags that should never be inherited.
  2059. This is a way to exclude a few tags from inheritance. For way to do
  2060. the opposite, to actively allow inheritance for selected tags,
  2061. see the variable `org-use-tag-inheritance'."
  2062. :group 'org-tags
  2063. :type '(repeat (string :tag "Tag")))
  2064. (defun org-tag-inherit-p (tag)
  2065. "Check if TAG is one that should be inherited."
  2066. (cond
  2067. ((member tag org-tags-exclude-from-inheritance) nil)
  2068. ((eq org-use-tag-inheritance t) t)
  2069. ((not org-use-tag-inheritance) nil)
  2070. ((stringp org-use-tag-inheritance)
  2071. (string-match org-use-tag-inheritance tag))
  2072. ((listp org-use-tag-inheritance)
  2073. (member tag org-use-tag-inheritance))
  2074. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2075. (defcustom org-tags-match-list-sublevels t
  2076. "Non-nil means list also sublevels of headlines matching a search.
  2077. This variable applies to tags/property searches, and also to stuck
  2078. projects because this search is based on a tags match as well.
  2079. When set to the symbol `indented', sublevels are indented with
  2080. leading dots.
  2081. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2082. the sublevels of a headline matching a tag search often also match
  2083. the same search. Listing all of them can create very long lists.
  2084. Setting this variable to nil causes subtrees of a match to be skipped.
  2085. This variable is semi-obsolete and probably should always be true. It
  2086. is better to limit inheritance to certain tags using the variables
  2087. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2088. :group 'org-tags
  2089. :type '(choice
  2090. (const :tag "No, don't list them" nil)
  2091. (const :tag "Yes, do list them" t)
  2092. (const :tag "List them, indented with leading dots" indented)))
  2093. (defcustom org-tags-sort-function nil
  2094. "When set, tags are sorted using this function as a comparator"
  2095. :group 'org-tags
  2096. :type '(choice
  2097. (const :tag "No sorting" nil)
  2098. (const :tag "Alphabetical" string<)
  2099. (const :tag "Reverse alphabetical" string>)
  2100. (function :tag "Custom function" nil)))
  2101. (defvar org-tags-history nil
  2102. "History of minibuffer reads for tags.")
  2103. (defvar org-last-tags-completion-table nil
  2104. "The last used completion table for tags.")
  2105. (defvar org-after-tags-change-hook nil
  2106. "Hook that is run after the tags in a line have changed.")
  2107. (defgroup org-properties nil
  2108. "Options concerning properties in Org-mode."
  2109. :tag "Org Properties"
  2110. :group 'org)
  2111. (defcustom org-property-format "%-10s %s"
  2112. "How property key/value pairs should be formatted by `indent-line'.
  2113. When `indent-line' hits a property definition, it will format the line
  2114. according to this format, mainly to make sure that the values are
  2115. lined-up with respect to each other."
  2116. :group 'org-properties
  2117. :type 'string)
  2118. (defcustom org-use-property-inheritance nil
  2119. "Non-nil means, properties apply also for sublevels.
  2120. This setting is chiefly used during property searches. Turning it on can
  2121. cause significant overhead when doing a search, which is why it is not
  2122. on by default.
  2123. When nil, only the properties directly given in the current entry count.
  2124. When t, every property is inherited. The value may also be a list of
  2125. properties that should have inheritance, or a regular expression matching
  2126. properties that should be inherited.
  2127. However, note that some special properties use inheritance under special
  2128. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2129. and the properties ending in \"_ALL\" when they are used as descriptor
  2130. for valid values of a property.
  2131. Note for programmers:
  2132. When querying an entry with `org-entry-get', you can control if inheritance
  2133. should be used. By default, `org-entry-get' looks only at the local
  2134. properties. You can request inheritance by setting the inherit argument
  2135. to t (to force inheritance) or to `selective' (to respect the setting
  2136. in this variable)."
  2137. :group 'org-properties
  2138. :type '(choice
  2139. (const :tag "Not" nil)
  2140. (const :tag "Always" t)
  2141. (repeat :tag "Specific properties" (string :tag "Property"))
  2142. (regexp :tag "Properties matched by regexp")))
  2143. (defun org-property-inherit-p (property)
  2144. "Check if PROPERTY is one that should be inherited."
  2145. (cond
  2146. ((eq org-use-property-inheritance t) t)
  2147. ((not org-use-property-inheritance) nil)
  2148. ((stringp org-use-property-inheritance)
  2149. (string-match org-use-property-inheritance property))
  2150. ((listp org-use-property-inheritance)
  2151. (member property org-use-property-inheritance))
  2152. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2153. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2154. "The default column format, if no other format has been defined.
  2155. This variable can be set on the per-file basis by inserting a line
  2156. #+COLUMNS: %25ITEM ....."
  2157. :group 'org-properties
  2158. :type 'string)
  2159. (defcustom org-columns-ellipses ".."
  2160. "The ellipses to be used when a field in column view is truncated.
  2161. When this is the empty string, as many characters as possible are shown,
  2162. but then there will be no visual indication that the field has been truncated.
  2163. When this is a string of length N, the last N characters of a truncated
  2164. field are replaced by this string. If the column is narrower than the
  2165. ellipses string, only part of the ellipses string will be shown."
  2166. :group 'org-properties
  2167. :type 'string)
  2168. (defcustom org-columns-modify-value-for-display-function nil
  2169. "Function that modifies values for display in column view.
  2170. For example, it can be used to cut out a certain part from a time stamp.
  2171. The function must take 2 arguments:
  2172. column-title The title of the column (*not* the property name)
  2173. value The value that should be modified.
  2174. The function should return the value that should be displayed,
  2175. or nil if the normal value should be used."
  2176. :group 'org-properties
  2177. :type 'function)
  2178. (defcustom org-effort-property "Effort"
  2179. "The property that is being used to keep track of effort estimates.
  2180. Effort estimates given in this property need to have the format H:MM."
  2181. :group 'org-properties
  2182. :group 'org-progress
  2183. :type '(string :tag "Property"))
  2184. (defconst org-global-properties-fixed
  2185. '(("VISIBILITY_ALL" . "folded children content all")
  2186. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2187. "List of property/value pairs that can be inherited by any entry.
  2188. These are fixed values, for the preset properties. The user variable
  2189. that can be used to add to this list is `org-global-properties'.
  2190. The entries in this list are cons cells where the car is a property
  2191. name and cdr is a string with the value. If the value represents
  2192. multiple items like an \"_ALL\" property, separate the items by
  2193. spaces.")
  2194. (defcustom org-global-properties nil
  2195. "List of property/value pairs that can be inherited by any entry.
  2196. This list will be combined with the constant `org-global-properties-fixed'.
  2197. The entries in this list are cons cells where the car is a property
  2198. name and cdr is a string with the value.
  2199. You can set buffer-local values for the same purpose in the variable
  2200. `org-file-properties' this by adding lines like
  2201. #+PROPERTY: NAME VALUE"
  2202. :group 'org-properties
  2203. :type '(repeat
  2204. (cons (string :tag "Property")
  2205. (string :tag "Value"))))
  2206. (defvar org-file-properties nil
  2207. "List of property/value pairs that can be inherited by any entry.
  2208. Valid for the current buffer.
  2209. This variable is populated from #+PROPERTY lines.")
  2210. (make-variable-buffer-local 'org-file-properties)
  2211. (defgroup org-agenda nil
  2212. "Options concerning agenda views in Org-mode."
  2213. :tag "Org Agenda"
  2214. :group 'org)
  2215. (defvar org-category nil
  2216. "Variable used by org files to set a category for agenda display.
  2217. Such files should use a file variable to set it, for example
  2218. # -*- mode: org; org-category: \"ELisp\"
  2219. or contain a special line
  2220. #+CATEGORY: ELisp
  2221. If the file does not specify a category, then file's base name
  2222. is used instead.")
  2223. (make-variable-buffer-local 'org-category)
  2224. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2225. (defcustom org-agenda-files nil
  2226. "The files to be used for agenda display.
  2227. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2228. \\[org-remove-file]. You can also use customize to edit the list.
  2229. If an entry is a directory, all files in that directory that are matched by
  2230. `org-agenda-file-regexp' will be part of the file list.
  2231. If the value of the variable is not a list but a single file name, then
  2232. the list of agenda files is actually stored and maintained in that file, one
  2233. agenda file per line."
  2234. :group 'org-agenda
  2235. :type '(choice
  2236. (repeat :tag "List of files and directories" file)
  2237. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2238. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2239. "Regular expression to match files for `org-agenda-files'.
  2240. If any element in the list in that variable contains a directory instead
  2241. of a normal file, all files in that directory that are matched by this
  2242. regular expression will be included."
  2243. :group 'org-agenda
  2244. :type 'regexp)
  2245. (defcustom org-agenda-text-search-extra-files nil
  2246. "List of extra files to be searched by text search commands.
  2247. These files will be search in addition to the agenda files by the
  2248. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2249. Note that these files will only be searched for text search commands,
  2250. not for the other agenda views like todo lists, tag searches or the weekly
  2251. agenda. This variable is intended to list notes and possibly archive files
  2252. that should also be searched by these two commands.
  2253. In fact, if the first element in the list is the symbol `agenda-archives',
  2254. than all archive files of all agenda files will be added to the search
  2255. scope."
  2256. :group 'org-agenda
  2257. :type '(set :greedy t
  2258. (const :tag "Agenda Archives" agenda-archives)
  2259. (repeat :inline t (file))))
  2260. (if (fboundp 'defvaralias)
  2261. (defvaralias 'org-agenda-multi-occur-extra-files
  2262. 'org-agenda-text-search-extra-files))
  2263. (defcustom org-agenda-skip-unavailable-files nil
  2264. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2265. A nil value means to remove them, after a query, from the list."
  2266. :group 'org-agenda
  2267. :type 'boolean)
  2268. (defcustom org-calendar-to-agenda-key [?c]
  2269. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2270. The command `org-calendar-goto-agenda' will be bound to this key. The
  2271. default is the character `c' because then `c' can be used to switch back and
  2272. forth between agenda and calendar."
  2273. :group 'org-agenda
  2274. :type 'sexp)
  2275. (defcustom org-calendar-agenda-action-key [?k]
  2276. "The key to be installed in `calendar-mode-map' for agenda-action.
  2277. The command `org-agenda-action' will be bound to this key. The
  2278. default is the character `k' because we use the same key in the agenda."
  2279. :group 'org-agenda
  2280. :type 'sexp)
  2281. (eval-after-load "calendar"
  2282. '(progn
  2283. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2284. 'org-calendar-goto-agenda)
  2285. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2286. 'org-agenda-action)))
  2287. (defgroup org-latex nil
  2288. "Options for embedding LaTeX code into Org-mode."
  2289. :tag "Org LaTeX"
  2290. :group 'org)
  2291. (defcustom org-format-latex-options
  2292. '(:foreground default :background default :scale 1.0
  2293. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2294. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2295. "Options for creating images from LaTeX fragments.
  2296. This is a property list with the following properties:
  2297. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2298. `default' means use the foreground of the default face.
  2299. :background the background color, or \"Transparent\".
  2300. `default' means use the background of the default face.
  2301. :scale a scaling factor for the size of the images.
  2302. :html-foreground, :html-background, :html-scale
  2303. the same numbers for HTML export.
  2304. :matchers a list indicating which matchers should be used to
  2305. find LaTeX fragments. Valid members of this list are:
  2306. \"begin\" find environments
  2307. \"$1\" find single characters surrounded by $.$
  2308. \"$\" find math expressions surrounded by $...$
  2309. \"$$\" find math expressions surrounded by $$....$$
  2310. \"\\(\" find math expressions surrounded by \\(...\\)
  2311. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2312. :group 'org-latex
  2313. :type 'plist)
  2314. (defcustom org-format-latex-header "\\documentclass{article}
  2315. \\usepackage{fullpage} % do not remove
  2316. \\usepackage{amssymb}
  2317. \\usepackage[usenames]{color}
  2318. \\usepackage{amsmath}
  2319. \\usepackage{latexsym}
  2320. \\usepackage[mathscr]{eucal}
  2321. \\pagestyle{empty} % do not remove"
  2322. "The document header used for processing LaTeX fragments."
  2323. :group 'org-latex
  2324. :type 'string)
  2325. (defgroup org-font-lock nil
  2326. "Font-lock settings for highlighting in Org-mode."
  2327. :tag "Org Font Lock"
  2328. :group 'org)
  2329. (defcustom org-level-color-stars-only nil
  2330. "Non-nil means fontify only the stars in each headline.
  2331. When nil, the entire headline is fontified.
  2332. Changing it requires restart of `font-lock-mode' to become effective
  2333. also in regions already fontified."
  2334. :group 'org-font-lock
  2335. :type 'boolean)
  2336. (defcustom org-hide-leading-stars nil
  2337. "Non-nil means, hide the first N-1 stars in a headline.
  2338. This works by using the face `org-hide' for these stars. This
  2339. face is white for a light background, and black for a dark
  2340. background. You may have to customize the face `org-hide' to
  2341. make this work.
  2342. Changing it requires restart of `font-lock-mode' to become effective
  2343. also in regions already fontified.
  2344. You may also set this on a per-file basis by adding one of the following
  2345. lines to the buffer:
  2346. #+STARTUP: hidestars
  2347. #+STARTUP: showstars"
  2348. :group 'org-font-lock
  2349. :type 'boolean)
  2350. (defcustom org-fontify-done-headline nil
  2351. "Non-nil means, change the face of a headline if it is marked DONE.
  2352. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2353. When this is non-nil, the headline after the keyword is set to the
  2354. `org-headline-done' as an additional indication."
  2355. :group 'org-font-lock
  2356. :type 'boolean)
  2357. (defcustom org-fontify-emphasized-text t
  2358. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2359. Changing this variable requires a restart of Emacs to take effect."
  2360. :group 'org-font-lock
  2361. :type 'boolean)
  2362. (defcustom org-fontify-whole-heading-line nil
  2363. "Non-nil means fontify the whole line for headings.
  2364. This is useful when setting a background color for the
  2365. org-leve-* faces."
  2366. :group 'org-font-lock
  2367. :type 'boolean)
  2368. (defcustom org-highlight-latex-fragments-and-specials nil
  2369. "Non-nil means, fontify what is treated specially by the exporters."
  2370. :group 'org-font-lock
  2371. :type 'boolean)
  2372. (defcustom org-hide-emphasis-markers nil
  2373. "Non-nil mean font-lock should hide the emphasis marker characters."
  2374. :group 'org-font-lock
  2375. :type 'boolean)
  2376. (defvar org-emph-re nil
  2377. "Regular expression for matching emphasis.")
  2378. (defvar org-verbatim-re nil
  2379. "Regular expression for matching verbatim text.")
  2380. (defvar org-emphasis-regexp-components) ; defined just below
  2381. (defvar org-emphasis-alist) ; defined just below
  2382. (defun org-set-emph-re (var val)
  2383. "Set variable and compute the emphasis regular expression."
  2384. (set var val)
  2385. (when (and (boundp 'org-emphasis-alist)
  2386. (boundp 'org-emphasis-regexp-components)
  2387. org-emphasis-alist org-emphasis-regexp-components)
  2388. (let* ((e org-emphasis-regexp-components)
  2389. (pre (car e))
  2390. (post (nth 1 e))
  2391. (border (nth 2 e))
  2392. (body (nth 3 e))
  2393. (nl (nth 4 e))
  2394. (body1 (concat body "*?"))
  2395. (markers (mapconcat 'car org-emphasis-alist ""))
  2396. (vmarkers (mapconcat
  2397. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2398. org-emphasis-alist "")))
  2399. ;; make sure special characters appear at the right position in the class
  2400. (if (string-match "\\^" markers)
  2401. (setq markers (concat (replace-match "" t t markers) "^")))
  2402. (if (string-match "-" markers)
  2403. (setq markers (concat (replace-match "" t t markers) "-")))
  2404. (if (string-match "\\^" vmarkers)
  2405. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2406. (if (string-match "-" vmarkers)
  2407. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2408. (if (> nl 0)
  2409. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2410. (int-to-string nl) "\\}")))
  2411. ;; Make the regexp
  2412. (setq org-emph-re
  2413. (concat "\\([" pre "]\\|^\\)"
  2414. "\\("
  2415. "\\([" markers "]\\)"
  2416. "\\("
  2417. "[^" border "]\\|"
  2418. "[^" border "]"
  2419. body1
  2420. "[^" border "]"
  2421. "\\)"
  2422. "\\3\\)"
  2423. "\\([" post "]\\|$\\)"))
  2424. (setq org-verbatim-re
  2425. (concat "\\([" pre "]\\|^\\)"
  2426. "\\("
  2427. "\\([" vmarkers "]\\)"
  2428. "\\("
  2429. "[^" border "]\\|"
  2430. "[^" border "]"
  2431. body1
  2432. "[^" border "]"
  2433. "\\)"
  2434. "\\3\\)"
  2435. "\\([" post "]\\|$\\)")))))
  2436. (defcustom org-emphasis-regexp-components
  2437. '(" \t('`\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2438. "Components used to build the regular expression for emphasis.
  2439. This is a list with 6 entries. Terminology: In an emphasis string
  2440. like \" *strong word* \", we call the initial space PREMATCH, the final
  2441. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2442. and \"trong wor\" is the body. The different components in this variable
  2443. specify what is allowed/forbidden in each part:
  2444. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2445. post Chars allowed as postmatch. End of line will be allowed too.
  2446. border The chars *forbidden* as border characters.
  2447. body-regexp A regexp like \".\" to match a body character. Don't use
  2448. non-shy groups here, and don't allow newline here.
  2449. newline The maximum number of newlines allowed in an emphasis exp.
  2450. Use customize to modify this, or restart Emacs after changing it."
  2451. :group 'org-font-lock
  2452. :set 'org-set-emph-re
  2453. :type '(list
  2454. (sexp :tag "Allowed chars in pre ")
  2455. (sexp :tag "Allowed chars in post ")
  2456. (sexp :tag "Forbidden chars in border ")
  2457. (sexp :tag "Regexp for body ")
  2458. (integer :tag "number of newlines allowed")
  2459. (option (boolean :tag "Please ignore this button"))))
  2460. (defcustom org-emphasis-alist
  2461. `(("*" bold "<b>" "</b>")
  2462. ("/" italic "<i>" "</i>")
  2463. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2464. ("=" org-code "<code>" "</code>" verbatim)
  2465. ("~" org-verbatim "<code>" "</code>" verbatim)
  2466. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2467. "<del>" "</del>")
  2468. )
  2469. "Special syntax for emphasized text.
  2470. Text starting and ending with a special character will be emphasized, for
  2471. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2472. characters, the face to be used by font-lock for highlighting in Org-mode
  2473. Emacs buffers, and the HTML tags to be used for this.
  2474. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2475. Use customize to modify this, or restart Emacs after changing it."
  2476. :group 'org-font-lock
  2477. :set 'org-set-emph-re
  2478. :type '(repeat
  2479. (list
  2480. (string :tag "Marker character")
  2481. (choice
  2482. (face :tag "Font-lock-face")
  2483. (plist :tag "Face property list"))
  2484. (string :tag "HTML start tag")
  2485. (string :tag "HTML end tag")
  2486. (option (const verbatim)))))
  2487. (defvar org-protecting-blocks
  2488. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2489. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2490. This is needed for font-lock setup.")
  2491. ;;; Miscellaneous options
  2492. (defgroup org-completion nil
  2493. "Completion in Org-mode."
  2494. :tag "Org Completion"
  2495. :group 'org)
  2496. (defcustom org-completion-use-ido nil
  2497. "Non-nil means, use ido completion wherever possible.
  2498. Note that `ido-mode' must be active for this variable to be relevant.
  2499. If you decide to turn this variable on, you might well want to turn off
  2500. `org-outline-path-complete-in-steps'.
  2501. See also `org-completion-use-iswitchb'."
  2502. :group 'org-completion
  2503. :type 'boolean)
  2504. (defcustom org-completion-use-iswitchb nil
  2505. "Non-nil means, use iswitchb completion wherever possible.
  2506. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2507. If you decide to turn this variable on, you might well want to turn off
  2508. `org-outline-path-complete-in-steps'.
  2509. Note that thi variable has only an effect if `org-completion-use-ido' is nil."
  2510. :group 'org-completion
  2511. :type 'boolean)
  2512. (defcustom org-completion-fallback-command 'hippie-expand
  2513. "The expansion command called by \\[org-complete] in normal context.
  2514. Normal means, no org-mode-specific context."
  2515. :group 'org-completion
  2516. :type 'function)
  2517. ;;; Functions and variables from ther packages
  2518. ;; Declared here to avoid compiler warnings
  2519. ;; XEmacs only
  2520. (defvar outline-mode-menu-heading)
  2521. (defvar outline-mode-menu-show)
  2522. (defvar outline-mode-menu-hide)
  2523. (defvar zmacs-regions) ; XEmacs regions
  2524. ;; Emacs only
  2525. (defvar mark-active)
  2526. ;; Various packages
  2527. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2528. (declare-function calendar-forward-day "cal-move" (arg))
  2529. (declare-function calendar-goto-date "cal-move" (date))
  2530. (declare-function calendar-goto-today "cal-move" ())
  2531. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2532. (defvar calc-embedded-close-formula)
  2533. (defvar calc-embedded-open-formula)
  2534. (declare-function cdlatex-tab "ext:cdlatex" ())
  2535. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2536. (defvar font-lock-unfontify-region-function)
  2537. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2538. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2539. (defvar iswitchb-temp-buflist)
  2540. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2541. (defvar org-agenda-tags-todo-honor-ignore-options)
  2542. (declare-function org-agenda-skip "org-agenda" ())
  2543. (declare-function org-format-agenda-item "org-agenda"
  2544. (extra txt &optional category tags dotime noprefix remove-re))
  2545. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2546. (declare-function org-agenda-change-all-lines "org-agenda"
  2547. (newhead hdmarker &optional fixface just-this))
  2548. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2549. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2550. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2551. (beg end))
  2552. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2553. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2554. "org-agenda" (&optional end))
  2555. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2556. (declare-function org-indent-mode "org-indent" (arg))
  2557. (declare-function parse-time-string "parse-time" (string))
  2558. (declare-function remember "remember" (&optional initial))
  2559. (declare-function remember-buffer-desc "remember" ())
  2560. (declare-function remember-finalize "remember" ())
  2561. (defvar remember-save-after-remembering)
  2562. (defvar remember-data-file)
  2563. (defvar remember-register)
  2564. (defvar remember-buffer)
  2565. (defvar remember-handler-functions)
  2566. (defvar remember-annotation-functions)
  2567. (defvar texmathp-why)
  2568. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2569. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2570. (defvar w3m-current-url)
  2571. (defvar w3m-current-title)
  2572. (defvar org-latex-regexps)
  2573. ;;; Autoload and prepare some org modules
  2574. ;; Some table stuff that needs to be defined here, because it is used
  2575. ;; by the functions setting up org-mode or checking for table context.
  2576. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2577. "Detects an org-type or table-type table.")
  2578. (defconst org-table-line-regexp "^[ \t]*|"
  2579. "Detects an org-type table line.")
  2580. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2581. "Detects an org-type table line.")
  2582. (defconst org-table-hline-regexp "^[ \t]*|-"
  2583. "Detects an org-type table hline.")
  2584. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2585. "Detects a table-type table hline.")
  2586. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2587. "Searching from within a table (any type) this finds the first line
  2588. outside the table.")
  2589. ;; Autoload the functions in org-table.el that are needed by functions here.
  2590. (eval-and-compile
  2591. (org-autoload "org-table"
  2592. '(org-table-align org-table-begin org-table-blank-field
  2593. org-table-convert org-table-convert-region org-table-copy-down
  2594. org-table-copy-region org-table-create
  2595. org-table-create-or-convert-from-region
  2596. org-table-create-with-table.el org-table-current-dline
  2597. org-table-cut-region org-table-delete-column org-table-edit-field
  2598. org-table-edit-formulas org-table-end org-table-eval-formula
  2599. org-table-export org-table-field-info
  2600. org-table-get-stored-formulas org-table-goto-column
  2601. org-table-hline-and-move org-table-import org-table-insert-column
  2602. org-table-insert-hline org-table-insert-row org-table-iterate
  2603. org-table-justify-field-maybe org-table-kill-row
  2604. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2605. org-table-move-column org-table-move-column-left
  2606. org-table-move-column-right org-table-move-row
  2607. org-table-move-row-down org-table-move-row-up
  2608. org-table-next-field org-table-next-row org-table-paste-rectangle
  2609. org-table-previous-field org-table-recalculate
  2610. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2611. org-table-toggle-coordinate-overlays
  2612. org-table-toggle-formula-debugger org-table-wrap-region
  2613. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2614. (defun org-at-table-p (&optional table-type)
  2615. "Return t if the cursor is inside an org-type table.
  2616. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2617. (if org-enable-table-editor
  2618. (save-excursion
  2619. (beginning-of-line 1)
  2620. (looking-at (if table-type org-table-any-line-regexp
  2621. org-table-line-regexp)))
  2622. nil))
  2623. (defsubst org-table-p () (org-at-table-p))
  2624. (defun org-at-table.el-p ()
  2625. "Return t if and only if we are at a table.el table."
  2626. (and (org-at-table-p 'any)
  2627. (save-excursion
  2628. (goto-char (org-table-begin 'any))
  2629. (looking-at org-table1-hline-regexp))))
  2630. (defun org-table-recognize-table.el ()
  2631. "If there is a table.el table nearby, recognize it and move into it."
  2632. (if org-table-tab-recognizes-table.el
  2633. (if (org-at-table.el-p)
  2634. (progn
  2635. (beginning-of-line 1)
  2636. (if (looking-at org-table-dataline-regexp)
  2637. nil
  2638. (if (looking-at org-table1-hline-regexp)
  2639. (progn
  2640. (beginning-of-line 2)
  2641. (if (looking-at org-table-any-border-regexp)
  2642. (beginning-of-line -1)))))
  2643. (if (re-search-forward "|" (org-table-end t) t)
  2644. (progn
  2645. (require 'table)
  2646. (if (table--at-cell-p (point))
  2647. t
  2648. (message "recognizing table.el table...")
  2649. (table-recognize-table)
  2650. (message "recognizing table.el table...done")))
  2651. (error "This should not happen..."))
  2652. t)
  2653. nil)
  2654. nil))
  2655. (defun org-at-table-hline-p ()
  2656. "Return t if the cursor is inside a hline in a table."
  2657. (if org-enable-table-editor
  2658. (save-excursion
  2659. (beginning-of-line 1)
  2660. (looking-at org-table-hline-regexp))
  2661. nil))
  2662. (defvar org-table-clean-did-remove-column nil)
  2663. (defun org-table-map-tables (function)
  2664. "Apply FUNCTION to the start of all tables in the buffer."
  2665. (save-excursion
  2666. (save-restriction
  2667. (widen)
  2668. (goto-char (point-min))
  2669. (while (re-search-forward org-table-any-line-regexp nil t)
  2670. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2671. (beginning-of-line 1)
  2672. (when (looking-at org-table-line-regexp)
  2673. (save-excursion (funcall function))
  2674. (or (looking-at org-table-line-regexp)
  2675. (forward-char 1)))
  2676. (re-search-forward org-table-any-border-regexp nil 1))))
  2677. (message "Mapping tables: done"))
  2678. ;; Declare and autoload functions from org-exp.el & Co
  2679. (declare-function org-default-export-plist "org-exp")
  2680. (declare-function org-infile-export-plist "org-exp")
  2681. (declare-function org-get-current-options "org-exp")
  2682. (eval-and-compile
  2683. (org-autoload "org-exp"
  2684. '(org-export org-export-visible
  2685. org-insert-export-options-template
  2686. org-table-clean-before-export))
  2687. (org-autoload "org-ascii"
  2688. '(org-export-as-ascii org-export-ascii-preprocess
  2689. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2690. org-export-region-as-ascii))
  2691. (org-autoload "org-html"
  2692. '(org-export-as-html-and-open
  2693. org-export-as-html-batch org-export-as-html-to-buffer
  2694. org-replace-region-by-html org-export-region-as-html
  2695. org-export-as-html))
  2696. (org-autoload "org-icalendar"
  2697. '(org-export-icalendar-this-file
  2698. org-export-icalendar-all-agenda-files
  2699. org-export-icalendar-combine-agenda-files))
  2700. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2701. ;; Declare and autoload functions from org-agenda.el
  2702. (eval-and-compile
  2703. (org-autoload "org-agenda"
  2704. '(org-agenda org-agenda-list org-search-view
  2705. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2706. org-diary org-agenda-to-appt
  2707. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2708. ;; Autoload org-remember
  2709. (eval-and-compile
  2710. (org-autoload "org-remember"
  2711. '(org-remember-insinuate org-remember-annotation
  2712. org-remember-apply-template org-remember org-remember-handler)))
  2713. ;; Autoload org-clock.el
  2714. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2715. (beg end))
  2716. (declare-function org-clock-update-mode-line "org-clock" ())
  2717. (defvar org-clock-start-time)
  2718. (defvar org-clock-marker (make-marker)
  2719. "Marker recording the last clock-in.")
  2720. (defvar org-clock-hd-marker (make-marker)
  2721. "Marker recording the last clock-in, but the headline position.")
  2722. (defun org-clock-is-active ()
  2723. "Return non-nil if clock is currently running.
  2724. The return value is actually the clock marker."
  2725. (marker-buffer org-clock-marker))
  2726. (eval-and-compile
  2727. (org-autoload
  2728. "org-clock"
  2729. '(org-clock-in org-clock-out org-clock-cancel
  2730. org-clock-goto org-clock-sum org-clock-display
  2731. org-clock-remove-overlays org-clock-report
  2732. org-clocktable-shift org-dblock-write:clocktable
  2733. org-get-clocktable)))
  2734. (defun org-clock-update-time-maybe ()
  2735. "If this is a CLOCK line, update it and return t.
  2736. Otherwise, return nil."
  2737. (interactive)
  2738. (save-excursion
  2739. (beginning-of-line 1)
  2740. (skip-chars-forward " \t")
  2741. (when (looking-at org-clock-string)
  2742. (let ((re (concat "[ \t]*" org-clock-string
  2743. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2744. "\\([ \t]*=>.*\\)?\\)?"))
  2745. ts te h m s neg)
  2746. (cond
  2747. ((not (looking-at re))
  2748. nil)
  2749. ((not (match-end 2))
  2750. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2751. (> org-clock-marker (point))
  2752. (<= org-clock-marker (point-at-eol)))
  2753. ;; The clock is running here
  2754. (setq org-clock-start-time
  2755. (apply 'encode-time
  2756. (org-parse-time-string (match-string 1))))
  2757. (org-clock-update-mode-line)))
  2758. (t
  2759. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2760. (end-of-line 1)
  2761. (setq ts (match-string 1)
  2762. te (match-string 3))
  2763. (setq s (- (time-to-seconds
  2764. (apply 'encode-time (org-parse-time-string te)))
  2765. (time-to-seconds
  2766. (apply 'encode-time (org-parse-time-string ts))))
  2767. neg (< s 0)
  2768. s (abs s)
  2769. h (floor (/ s 3600))
  2770. s (- s (* 3600 h))
  2771. m (floor (/ s 60))
  2772. s (- s (* 60 s)))
  2773. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2774. t))))))
  2775. (defun org-check-running-clock ()
  2776. "Check if the current buffer contains the running clock.
  2777. If yes, offer to stop it and to save the buffer with the changes."
  2778. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2779. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2780. (buffer-name))))
  2781. (org-clock-out)
  2782. (when (y-or-n-p "Save changed buffer?")
  2783. (save-buffer))))
  2784. (defun org-clocktable-try-shift (dir n)
  2785. "Check if this line starts a clock table, if yes, shift the time block."
  2786. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2787. (org-clocktable-shift dir n)))
  2788. ;; Autoload org-timer.el
  2789. (eval-and-compile
  2790. (org-autoload
  2791. "org-timer"
  2792. '(org-timer-start org-timer org-timer-item
  2793. org-timer-change-times-in-region
  2794. org-timer-set-timer
  2795. org-timer-reset-timers
  2796. org-timer-show-remaining-time)))
  2797. ;; Autoload org-feed.el
  2798. (eval-and-compile
  2799. (org-autoload
  2800. "org-feed"
  2801. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2802. ;; Autoload org-indent.el
  2803. (eval-and-compile
  2804. (org-autoload
  2805. "org-indent"
  2806. '(org-indent-mode)))
  2807. ;; Autoload archiving code
  2808. ;; The stuff that is needed for cycling and tags has to be defined here.
  2809. (defgroup org-archive nil
  2810. "Options concerning archiving in Org-mode."
  2811. :tag "Org Archive"
  2812. :group 'org-structure)
  2813. (defcustom org-archive-location "%s_archive::"
  2814. "The location where subtrees should be archived.
  2815. The value of this variable is a string, consisting of two parts,
  2816. separated by a double-colon. The first part is a filename and
  2817. the second part is a headline.
  2818. When the filename is omitted, archiving happens in the same file.
  2819. %s in the filename will be replaced by the current file
  2820. name (without the directory part). Archiving to a different file
  2821. is useful to keep archived entries from contributing to the
  2822. Org-mode Agenda.
  2823. The archived entries will be filed as subtrees of the specified
  2824. headline. When the headline is omitted, the subtrees are simply
  2825. filed away at the end of the file, as top-level entries. Also in
  2826. the heading you can use %s to represent the file name, this can be
  2827. useful when using the same archive for a number of different files.
  2828. Here are a few examples:
  2829. \"%s_archive::\"
  2830. If the current file is Projects.org, archive in file
  2831. Projects.org_archive, as top-level trees. This is the default.
  2832. \"::* Archived Tasks\"
  2833. Archive in the current file, under the top-level headline
  2834. \"* Archived Tasks\".
  2835. \"~/org/archive.org::\"
  2836. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2837. \"~/org/archive.org::From %s\"
  2838. Archive in file ~/org/archive.org (absolute path), und headlines
  2839. \"From FILENAME\" where file name is the current file name.
  2840. \"basement::** Finished Tasks\"
  2841. Archive in file ./basement (relative path), as level 3 trees
  2842. below the level 2 heading \"** Finished Tasks\".
  2843. You may set this option on a per-file basis by adding to the buffer a
  2844. line like
  2845. #+ARCHIVE: basement::** Finished Tasks
  2846. You may also define it locally for a subtree by setting an ARCHIVE property
  2847. in the entry. If such a property is found in an entry, or anywhere up
  2848. the hierarchy, it will be used."
  2849. :group 'org-archive
  2850. :type 'string)
  2851. (defcustom org-archive-tag "ARCHIVE"
  2852. "The tag that marks a subtree as archived.
  2853. An archived subtree does not open during visibility cycling, and does
  2854. not contribute to the agenda listings.
  2855. After changing this, font-lock must be restarted in the relevant buffers to
  2856. get the proper fontification."
  2857. :group 'org-archive
  2858. :group 'org-keywords
  2859. :type 'string)
  2860. (defcustom org-agenda-skip-archived-trees t
  2861. "Non-nil means, the agenda will skip any items located in archived trees.
  2862. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2863. variable is no longer recommended, you should leave it at the value t.
  2864. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2865. :group 'org-archive
  2866. :group 'org-agenda-skip
  2867. :type 'boolean)
  2868. (defcustom org-columns-skip-arrchived-trees t
  2869. "Non-nil means, irgnore archived trees when creating column view."
  2870. :group 'org-archive
  2871. :group 'org-properties
  2872. :type 'boolean)
  2873. (defcustom org-cycle-open-archived-trees nil
  2874. "Non-nil means, `org-cycle' will open archived trees.
  2875. An archived tree is a tree marked with the tag ARCHIVE.
  2876. When nil, archived trees will stay folded. You can still open them with
  2877. normal outline commands like `show-all', but not with the cycling commands."
  2878. :group 'org-archive
  2879. :group 'org-cycle
  2880. :type 'boolean)
  2881. (defcustom org-sparse-tree-open-archived-trees nil
  2882. "Non-nil means sparse tree construction shows matches in archived trees.
  2883. When nil, matches in these trees are highlighted, but the trees are kept in
  2884. collapsed state."
  2885. :group 'org-archive
  2886. :group 'org-sparse-trees
  2887. :type 'boolean)
  2888. (defun org-cycle-hide-archived-subtrees (state)
  2889. "Re-hide all archived subtrees after a visibility state change."
  2890. (when (and (not org-cycle-open-archived-trees)
  2891. (not (memq state '(overview folded))))
  2892. (save-excursion
  2893. (let* ((globalp (memq state '(contents all)))
  2894. (beg (if globalp (point-min) (point)))
  2895. (end (if globalp (point-max) (org-end-of-subtree t))))
  2896. (org-hide-archived-subtrees beg end)
  2897. (goto-char beg)
  2898. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2899. (message "%s" (substitute-command-keys
  2900. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2901. (defun org-force-cycle-archived ()
  2902. "Cycle subtree even if it is archived."
  2903. (interactive)
  2904. (setq this-command 'org-cycle)
  2905. (let ((org-cycle-open-archived-trees t))
  2906. (call-interactively 'org-cycle)))
  2907. (defun org-hide-archived-subtrees (beg end)
  2908. "Re-hide all archived subtrees after a visibility state change."
  2909. (save-excursion
  2910. (let* ((re (concat ":" org-archive-tag ":")))
  2911. (goto-char beg)
  2912. (while (re-search-forward re end t)
  2913. (and (org-on-heading-p) (hide-subtree))
  2914. (org-end-of-subtree t)))))
  2915. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2916. (eval-and-compile
  2917. (org-autoload "org-archive"
  2918. '(org-add-archive-files org-archive-subtree
  2919. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2920. ;; Autoload Column View Code
  2921. (declare-function org-columns-number-to-string "org-colview")
  2922. (declare-function org-columns-get-format-and-top-level "org-colview")
  2923. (declare-function org-columns-compute "org-colview")
  2924. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2925. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2926. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2927. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2928. ;; Autoload ID code
  2929. (declare-function org-id-store-link "org-id")
  2930. (declare-function org-id-locations-load "org-id")
  2931. (declare-function org-id-locations-save "org-id")
  2932. (defvar org-id-track-globally)
  2933. (org-autoload "org-id"
  2934. '(org-id-get-create org-id-new org-id-copy org-id-get
  2935. org-id-get-with-outline-path-completion
  2936. org-id-get-with-outline-drilling
  2937. org-id-goto org-id-find org-id-store-link))
  2938. ;; Autoload Plotting Code
  2939. (org-autoload "org-plot"
  2940. '(org-plot/gnuplot))
  2941. ;;; Variables for pre-computed regular expressions, all buffer local
  2942. (defvar org-drawer-regexp nil
  2943. "Matches first line of a hidden block.")
  2944. (make-variable-buffer-local 'org-drawer-regexp)
  2945. (defvar org-todo-regexp nil
  2946. "Matches any of the TODO state keywords.")
  2947. (make-variable-buffer-local 'org-todo-regexp)
  2948. (defvar org-not-done-regexp nil
  2949. "Matches any of the TODO state keywords except the last one.")
  2950. (make-variable-buffer-local 'org-not-done-regexp)
  2951. (defvar org-not-done-heading-regexp nil
  2952. "Matches a TODO headline that is not done.")
  2953. (make-variable-buffer-local 'org-not-done-regexp)
  2954. (defvar org-todo-line-regexp nil
  2955. "Matches a headline and puts TODO state into group 2 if present.")
  2956. (make-variable-buffer-local 'org-todo-line-regexp)
  2957. (defvar org-complex-heading-regexp nil
  2958. "Matches a headline and puts everything into groups:
  2959. group 1: the stars
  2960. group 2: The todo keyword, maybe
  2961. group 3: Priority cookie
  2962. group 4: True headline
  2963. group 5: Tags")
  2964. (make-variable-buffer-local 'org-complex-heading-regexp)
  2965. (defvar org-todo-line-tags-regexp nil
  2966. "Matches a headline and puts TODO state into group 2 if present.
  2967. Also put tags into group 4 if tags are present.")
  2968. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2969. (defvar org-nl-done-regexp nil
  2970. "Matches newline followed by a headline with the DONE keyword.")
  2971. (make-variable-buffer-local 'org-nl-done-regexp)
  2972. (defvar org-looking-at-done-regexp nil
  2973. "Matches the DONE keyword a point.")
  2974. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2975. (defvar org-ds-keyword-length 12
  2976. "Maximum length of the Deadline and SCHEDULED keywords.")
  2977. (make-variable-buffer-local 'org-ds-keyword-length)
  2978. (defvar org-deadline-regexp nil
  2979. "Matches the DEADLINE keyword.")
  2980. (make-variable-buffer-local 'org-deadline-regexp)
  2981. (defvar org-deadline-time-regexp nil
  2982. "Matches the DEADLINE keyword together with a time stamp.")
  2983. (make-variable-buffer-local 'org-deadline-time-regexp)
  2984. (defvar org-deadline-line-regexp nil
  2985. "Matches the DEADLINE keyword and the rest of the line.")
  2986. (make-variable-buffer-local 'org-deadline-line-regexp)
  2987. (defvar org-scheduled-regexp nil
  2988. "Matches the SCHEDULED keyword.")
  2989. (make-variable-buffer-local 'org-scheduled-regexp)
  2990. (defvar org-scheduled-time-regexp nil
  2991. "Matches the SCHEDULED keyword together with a time stamp.")
  2992. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2993. (defvar org-closed-time-regexp nil
  2994. "Matches the CLOSED keyword together with a time stamp.")
  2995. (make-variable-buffer-local 'org-closed-time-regexp)
  2996. (defvar org-keyword-time-regexp nil
  2997. "Matches any of the 4 keywords, together with the time stamp.")
  2998. (make-variable-buffer-local 'org-keyword-time-regexp)
  2999. (defvar org-keyword-time-not-clock-regexp nil
  3000. "Matches any of the 3 keywords, together with the time stamp.")
  3001. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3002. (defvar org-maybe-keyword-time-regexp nil
  3003. "Matches a timestamp, possibly preceeded by a keyword.")
  3004. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3005. (defvar org-planning-or-clock-line-re nil
  3006. "Matches a line with planning or clock info.")
  3007. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3008. (defconst org-plain-time-of-day-regexp
  3009. (concat
  3010. "\\(\\<[012]?[0-9]"
  3011. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3012. "\\(--?"
  3013. "\\(\\<[012]?[0-9]"
  3014. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3015. "\\)?")
  3016. "Regular expression to match a plain time or time range.
  3017. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3018. groups carry important information:
  3019. 0 the full match
  3020. 1 the first time, range or not
  3021. 8 the second time, if it is a range.")
  3022. (defconst org-plain-time-extension-regexp
  3023. (concat
  3024. "\\(\\<[012]?[0-9]"
  3025. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3026. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3027. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3028. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3029. groups carry important information:
  3030. 0 the full match
  3031. 7 hours of duration
  3032. 9 minutes of duration")
  3033. (defconst org-stamp-time-of-day-regexp
  3034. (concat
  3035. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3036. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3037. "\\(--?"
  3038. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3039. "Regular expression to match a timestamp time or time range.
  3040. After a match, the following groups carry important information:
  3041. 0 the full match
  3042. 1 date plus weekday, for backreferencing to make sure both times on same day
  3043. 2 the first time, range or not
  3044. 4 the second time, if it is a range.")
  3045. (defconst org-startup-options
  3046. '(("fold" org-startup-folded t)
  3047. ("overview" org-startup-folded t)
  3048. ("nofold" org-startup-folded nil)
  3049. ("showall" org-startup-folded nil)
  3050. ("content" org-startup-folded content)
  3051. ("indent" org-startup-indented t)
  3052. ("noindent" org-startup-indented nil)
  3053. ("hidestars" org-hide-leading-stars t)
  3054. ("showstars" org-hide-leading-stars nil)
  3055. ("odd" org-odd-levels-only t)
  3056. ("oddeven" org-odd-levels-only nil)
  3057. ("align" org-startup-align-all-tables t)
  3058. ("noalign" org-startup-align-all-tables nil)
  3059. ("customtime" org-display-custom-times t)
  3060. ("logdone" org-log-done time)
  3061. ("lognotedone" org-log-done note)
  3062. ("nologdone" org-log-done nil)
  3063. ("lognoteclock-out" org-log-note-clock-out t)
  3064. ("nolognoteclock-out" org-log-note-clock-out nil)
  3065. ("logrepeat" org-log-repeat state)
  3066. ("lognoterepeat" org-log-repeat note)
  3067. ("nologrepeat" org-log-repeat nil)
  3068. ("fninline" org-footnote-define-inline t)
  3069. ("nofninline" org-footnote-define-inline nil)
  3070. ("fnlocal" org-footnote-section nil)
  3071. ("fnauto" org-footnote-auto-label t)
  3072. ("fnprompt" org-footnote-auto-label nil)
  3073. ("fnconfirm" org-footnote-auto-label confirm)
  3074. ("fnplain" org-footnote-auto-label plain)
  3075. ("fnadjust" org-footnote-auto-adjust t)
  3076. ("nofnadjust" org-footnote-auto-adjust nil)
  3077. ("constcgs" constants-unit-system cgs)
  3078. ("constSI" constants-unit-system SI)
  3079. ("noptag" org-tag-persistent-alist nil)
  3080. ("hideblocks" org-hide-block-startup t)
  3081. ("nohideblocks" org-hide-block-startup nil))
  3082. "Variable associated with STARTUP options for org-mode.
  3083. Each element is a list of three items: The startup options as written
  3084. in the #+STARTUP line, the corresponding variable, and the value to
  3085. set this variable to if the option is found. An optional forth element PUSH
  3086. means to push this value onto the list in the variable.")
  3087. (defun org-set-regexps-and-options ()
  3088. "Precompute regular expressions for current buffer."
  3089. (when (org-mode-p)
  3090. (org-set-local 'org-todo-kwd-alist nil)
  3091. (org-set-local 'org-todo-key-alist nil)
  3092. (org-set-local 'org-todo-key-trigger nil)
  3093. (org-set-local 'org-todo-keywords-1 nil)
  3094. (org-set-local 'org-done-keywords nil)
  3095. (org-set-local 'org-todo-heads nil)
  3096. (org-set-local 'org-todo-sets nil)
  3097. (org-set-local 'org-todo-log-states nil)
  3098. (org-set-local 'org-file-properties nil)
  3099. (org-set-local 'org-file-tags nil)
  3100. (let ((re (org-make-options-regexp
  3101. '("CATEGORY" "TODO" "COLUMNS"
  3102. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3103. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3104. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3105. (splitre "[ \t]+")
  3106. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3107. tail sep kws1 prio props ftags drawers
  3108. ext-setup-or-nil setup-contents (start 0))
  3109. (save-excursion
  3110. (save-restriction
  3111. (widen)
  3112. (goto-char (point-min))
  3113. (while (or (and ext-setup-or-nil
  3114. (string-match re ext-setup-or-nil start)
  3115. (setq start (match-end 0)))
  3116. (and (setq ext-setup-or-nil nil start 0)
  3117. (re-search-forward re nil t)))
  3118. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3119. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3120. (cond
  3121. ((equal key "CATEGORY")
  3122. (if (string-match "[ \t]+$" value)
  3123. (setq value (replace-match "" t t value)))
  3124. (setq cat value))
  3125. ((member key '("SEQ_TODO" "TODO"))
  3126. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3127. ((equal key "TYP_TODO")
  3128. (push (cons 'type (org-split-string value splitre)) kwds))
  3129. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3130. ;; general TODO-like setup
  3131. (push (cons (intern (downcase (match-string 1 key)))
  3132. (org-split-string value splitre)) kwds))
  3133. ((equal key "TAGS")
  3134. (setq tags (append tags (if tags '("\\n") nil)
  3135. (org-split-string value splitre))))
  3136. ((equal key "COLUMNS")
  3137. (org-set-local 'org-columns-default-format value))
  3138. ((equal key "LINK")
  3139. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3140. (push (cons (match-string 1 value)
  3141. (org-trim (match-string 2 value)))
  3142. links)))
  3143. ((equal key "PRIORITIES")
  3144. (setq prio (org-split-string value " +")))
  3145. ((equal key "PROPERTY")
  3146. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3147. (push (cons (match-string 1 value) (match-string 2 value))
  3148. props)))
  3149. ((equal key "FILETAGS")
  3150. (when (string-match "\\S-" value)
  3151. (setq ftags
  3152. (append
  3153. ftags
  3154. (apply 'append
  3155. (mapcar (lambda (x) (org-split-string x ":"))
  3156. (org-split-string value)))))))
  3157. ((equal key "DRAWERS")
  3158. (setq drawers (org-split-string value splitre)))
  3159. ((equal key "CONSTANTS")
  3160. (setq const (append const (org-split-string value splitre))))
  3161. ((equal key "STARTUP")
  3162. (let ((opts (org-split-string value splitre))
  3163. l var val)
  3164. (while (setq l (pop opts))
  3165. (when (setq l (assoc l org-startup-options))
  3166. (setq var (nth 1 l) val (nth 2 l))
  3167. (if (not (nth 3 l))
  3168. (set (make-local-variable var) val)
  3169. (if (not (listp (symbol-value var)))
  3170. (set (make-local-variable var) nil))
  3171. (set (make-local-variable var) (symbol-value var))
  3172. (add-to-list var val))))))
  3173. ((equal key "ARCHIVE")
  3174. (string-match " *$" value)
  3175. (setq arch (replace-match "" t t value))
  3176. (remove-text-properties 0 (length arch)
  3177. '(face t fontified t) arch))
  3178. ((equal key "SETUPFILE")
  3179. (setq setup-contents (org-file-contents
  3180. (expand-file-name
  3181. (org-remove-double-quotes value))
  3182. 'noerror))
  3183. (if (not ext-setup-or-nil)
  3184. (setq ext-setup-or-nil setup-contents start 0)
  3185. (setq ext-setup-or-nil
  3186. (concat (substring ext-setup-or-nil 0 start)
  3187. "\n" setup-contents "\n"
  3188. (substring ext-setup-or-nil start)))))
  3189. ))))
  3190. (when cat
  3191. (org-set-local 'org-category (intern cat))
  3192. (push (cons "CATEGORY" cat) props))
  3193. (when prio
  3194. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3195. (setq prio (mapcar 'string-to-char prio))
  3196. (org-set-local 'org-highest-priority (nth 0 prio))
  3197. (org-set-local 'org-lowest-priority (nth 1 prio))
  3198. (org-set-local 'org-default-priority (nth 2 prio)))
  3199. (and props (org-set-local 'org-file-properties (nreverse props)))
  3200. (and ftags (org-set-local 'org-file-tags
  3201. (mapcar 'org-add-prop-inherited ftags)))
  3202. (and drawers (org-set-local 'org-drawers drawers))
  3203. (and arch (org-set-local 'org-archive-location arch))
  3204. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3205. ;; Process the TODO keywords
  3206. (unless kwds
  3207. ;; Use the global values as if they had been given locally.
  3208. (setq kwds (default-value 'org-todo-keywords))
  3209. (if (stringp (car kwds))
  3210. (setq kwds (list (cons org-todo-interpretation
  3211. (default-value 'org-todo-keywords)))))
  3212. (setq kwds (reverse kwds)))
  3213. (setq kwds (nreverse kwds))
  3214. (let (inter kws kw)
  3215. (while (setq kws (pop kwds))
  3216. (let ((kws (or
  3217. (run-hook-with-args-until-success
  3218. 'org-todo-setup-filter-hook kws)
  3219. kws)))
  3220. (setq inter (pop kws) sep (member "|" kws)
  3221. kws0 (delete "|" (copy-sequence kws))
  3222. kwsa nil
  3223. kws1 (mapcar
  3224. (lambda (x)
  3225. ;; 1 2
  3226. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3227. (progn
  3228. (setq kw (match-string 1 x)
  3229. key (and (match-end 2) (match-string 2 x))
  3230. log (org-extract-log-state-settings x))
  3231. (push (cons kw (and key (string-to-char key))) kwsa)
  3232. (and log (push log org-todo-log-states))
  3233. kw)
  3234. (error "Invalid TODO keyword %s" x)))
  3235. kws0)
  3236. kwsa (if kwsa (append '((:startgroup))
  3237. (nreverse kwsa)
  3238. '((:endgroup))))
  3239. hw (car kws1)
  3240. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3241. tail (list inter hw (car dws) (org-last dws))))
  3242. (add-to-list 'org-todo-heads hw 'append)
  3243. (push kws1 org-todo-sets)
  3244. (setq org-done-keywords (append org-done-keywords dws nil))
  3245. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3246. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3247. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3248. (setq org-todo-sets (nreverse org-todo-sets)
  3249. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3250. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3251. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3252. ;; Process the constants
  3253. (when const
  3254. (let (e cst)
  3255. (while (setq e (pop const))
  3256. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3257. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3258. (setq org-table-formula-constants-local cst)))
  3259. ;; Process the tags.
  3260. (when tags
  3261. (let (e tgs)
  3262. (while (setq e (pop tags))
  3263. (cond
  3264. ((equal e "{") (push '(:startgroup) tgs))
  3265. ((equal e "}") (push '(:endgroup) tgs))
  3266. ((equal e "\\n") (push '(:newline) tgs))
  3267. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3268. (push (cons (match-string 1 e)
  3269. (string-to-char (match-string 2 e)))
  3270. tgs))
  3271. (t (push (list e) tgs))))
  3272. (org-set-local 'org-tag-alist nil)
  3273. (while (setq e (pop tgs))
  3274. (or (and (stringp (car e))
  3275. (assoc (car e) org-tag-alist))
  3276. (push e org-tag-alist)))))
  3277. ;; Compute the regular expressions and other local variables
  3278. (if (not org-done-keywords)
  3279. (setq org-done-keywords (and org-todo-keywords-1
  3280. (list (org-last org-todo-keywords-1)))))
  3281. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3282. (length org-scheduled-string)
  3283. (length org-clock-string)
  3284. (length org-closed-string)))
  3285. org-drawer-regexp
  3286. (concat "^[ \t]*:\\("
  3287. (mapconcat 'regexp-quote org-drawers "\\|")
  3288. "\\):[ \t]*$")
  3289. org-not-done-keywords
  3290. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3291. org-todo-regexp
  3292. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3293. "\\|") "\\)\\>")
  3294. org-not-done-regexp
  3295. (concat "\\<\\("
  3296. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3297. "\\)\\>")
  3298. org-not-done-heading-regexp
  3299. (concat "^\\(\\*+\\)[ \t]+\\("
  3300. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3301. "\\)\\>")
  3302. org-todo-line-regexp
  3303. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3304. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3305. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3306. org-complex-heading-regexp
  3307. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3308. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3309. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3310. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3311. org-nl-done-regexp
  3312. (concat "\n\\*+[ \t]+"
  3313. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3314. "\\)" "\\>")
  3315. org-todo-line-tags-regexp
  3316. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3317. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3318. (org-re
  3319. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3320. org-looking-at-done-regexp
  3321. (concat "^" "\\(?:"
  3322. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3323. "\\>")
  3324. org-deadline-regexp (concat "\\<" org-deadline-string)
  3325. org-deadline-time-regexp
  3326. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3327. org-deadline-line-regexp
  3328. (concat "\\<\\(" org-deadline-string "\\).*")
  3329. org-scheduled-regexp
  3330. (concat "\\<" org-scheduled-string)
  3331. org-scheduled-time-regexp
  3332. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3333. org-closed-time-regexp
  3334. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3335. org-keyword-time-regexp
  3336. (concat "\\<\\(" org-scheduled-string
  3337. "\\|" org-deadline-string
  3338. "\\|" org-closed-string
  3339. "\\|" org-clock-string "\\)"
  3340. " *[[<]\\([^]>]+\\)[]>]")
  3341. org-keyword-time-not-clock-regexp
  3342. (concat "\\<\\(" org-scheduled-string
  3343. "\\|" org-deadline-string
  3344. "\\|" org-closed-string
  3345. "\\)"
  3346. " *[[<]\\([^]>]+\\)[]>]")
  3347. org-maybe-keyword-time-regexp
  3348. (concat "\\(\\<\\(" org-scheduled-string
  3349. "\\|" org-deadline-string
  3350. "\\|" org-closed-string
  3351. "\\|" org-clock-string "\\)\\)?"
  3352. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3353. org-planning-or-clock-line-re
  3354. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3355. "\\|" org-deadline-string
  3356. "\\|" org-closed-string "\\|" org-clock-string
  3357. "\\)\\>\\)")
  3358. )
  3359. (org-compute-latex-and-specials-regexp)
  3360. (org-set-font-lock-defaults))))
  3361. (defun org-file-contents (file &optional noerror)
  3362. "Return the contents of FILE, as a string."
  3363. (if (or (not file)
  3364. (not (file-readable-p file)))
  3365. (if noerror
  3366. (progn
  3367. (message "Cannot read file %s" file)
  3368. (ding) (sit-for 2)
  3369. "")
  3370. (error "Cannot read file %s" file))
  3371. (with-temp-buffer
  3372. (insert-file-contents file)
  3373. (buffer-string))))
  3374. (defun org-extract-log-state-settings (x)
  3375. "Extract the log state setting from a TODO keyword string.
  3376. This will extract info from a string like \"WAIT(w@/!)\"."
  3377. (let (kw key log1 log2)
  3378. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3379. (setq kw (match-string 1 x)
  3380. key (and (match-end 2) (match-string 2 x))
  3381. log1 (and (match-end 3) (match-string 3 x))
  3382. log2 (and (match-end 4) (match-string 4 x)))
  3383. (and (or log1 log2)
  3384. (list kw
  3385. (and log1 (if (equal log1 "!") 'time 'note))
  3386. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3387. (defun org-remove-keyword-keys (list)
  3388. "Remove a pair of parenthesis at the end of each string in LIST."
  3389. (mapcar (lambda (x)
  3390. (if (string-match "(.*)$" x)
  3391. (substring x 0 (match-beginning 0))
  3392. x))
  3393. list))
  3394. ;; FIXME: this could be done much better, using second characters etc.
  3395. (defun org-assign-fast-keys (alist)
  3396. "Assign fast keys to a keyword-key alist.
  3397. Respect keys that are already there."
  3398. (let (new e k c c1 c2 (char ?a))
  3399. (while (setq e (pop alist))
  3400. (cond
  3401. ((equal e '(:startgroup)) (push e new))
  3402. ((equal e '(:endgroup)) (push e new))
  3403. ((equal e '(:newline)) (push e new))
  3404. (t
  3405. (setq k (car e) c2 nil)
  3406. (if (cdr e)
  3407. (setq c (cdr e))
  3408. ;; automatically assign a character.
  3409. (setq c1 (string-to-char
  3410. (downcase (substring
  3411. k (if (= (string-to-char k) ?@) 1 0)))))
  3412. (if (or (rassoc c1 new) (rassoc c1 alist))
  3413. (while (or (rassoc char new) (rassoc char alist))
  3414. (setq char (1+ char)))
  3415. (setq c2 c1))
  3416. (setq c (or c2 char)))
  3417. (push (cons k c) new))))
  3418. (nreverse new)))
  3419. ;;; Some variables used in various places
  3420. (defvar org-window-configuration nil
  3421. "Used in various places to store a window configuration.")
  3422. (defvar org-finish-function nil
  3423. "Function to be called when `C-c C-c' is used.
  3424. This is for getting out of special buffers like remember.")
  3425. ;; FIXME: Occasionally check by commenting these, to make sure
  3426. ;; no other functions uses these, forgetting to let-bind them.
  3427. (defvar entry)
  3428. (defvar last-state)
  3429. (defvar date)
  3430. ;; Defined somewhere in this file, but used before definition.
  3431. (defvar org-html-entities)
  3432. (defvar org-struct-menu)
  3433. (defvar org-org-menu)
  3434. (defvar org-tbl-menu)
  3435. (defvar org-agenda-keymap)
  3436. ;;;; Define the Org-mode
  3437. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3438. (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."))
  3439. ;; We use a before-change function to check if a table might need
  3440. ;; an update.
  3441. (defvar org-table-may-need-update t
  3442. "Indicates that a table might need an update.
  3443. This variable is set by `org-before-change-function'.
  3444. `org-table-align' sets it back to nil.")
  3445. (defun org-before-change-function (beg end)
  3446. "Every change indicates that a table might need an update."
  3447. (setq org-table-may-need-update t))
  3448. (defvar org-mode-map)
  3449. (defvar org-mode-hook nil
  3450. "Mode hook for Org-mode, run after the mode was turned on.")
  3451. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3452. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3453. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3454. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3455. (defvar org-table-buffer-is-an nil)
  3456. (defconst org-outline-regexp "\\*+ ")
  3457. ;;;###autoload
  3458. (define-derived-mode org-mode outline-mode "Org"
  3459. "Outline-based notes management and organizer, alias
  3460. \"Carsten's outline-mode for keeping track of everything.\"
  3461. Org-mode develops organizational tasks around a NOTES file which
  3462. contains information about projects as plain text. Org-mode is
  3463. implemented on top of outline-mode, which is ideal to keep the content
  3464. of large files well structured. It supports ToDo items, deadlines and
  3465. time stamps, which magically appear in the diary listing of the Emacs
  3466. calendar. Tables are easily created with a built-in table editor.
  3467. Plain text URL-like links connect to websites, emails (VM), Usenet
  3468. messages (Gnus), BBDB entries, and any files related to the project.
  3469. For printing and sharing of notes, an Org-mode file (or a part of it)
  3470. can be exported as a structured ASCII or HTML file.
  3471. The following commands are available:
  3472. \\{org-mode-map}"
  3473. ;; Get rid of Outline menus, they are not needed
  3474. ;; Need to do this here because define-derived-mode sets up
  3475. ;; the keymap so late. Still, it is a waste to call this each time
  3476. ;; we switch another buffer into org-mode.
  3477. (if (featurep 'xemacs)
  3478. (when (boundp 'outline-mode-menu-heading)
  3479. ;; Assume this is Greg's port, it used easymenu
  3480. (easy-menu-remove outline-mode-menu-heading)
  3481. (easy-menu-remove outline-mode-menu-show)
  3482. (easy-menu-remove outline-mode-menu-hide))
  3483. (define-key org-mode-map [menu-bar headings] 'undefined)
  3484. (define-key org-mode-map [menu-bar hide] 'undefined)
  3485. (define-key org-mode-map [menu-bar show] 'undefined))
  3486. (org-load-modules-maybe)
  3487. (easy-menu-add org-org-menu)
  3488. (easy-menu-add org-tbl-menu)
  3489. (org-install-agenda-files-menu)
  3490. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3491. (org-add-to-invisibility-spec '(org-cwidth))
  3492. (org-add-to-invisibility-spec '(org-hide-block . t))
  3493. (when (featurep 'xemacs)
  3494. (org-set-local 'line-move-ignore-invisible t))
  3495. (org-set-local 'outline-regexp org-outline-regexp)
  3496. (org-set-local 'outline-level 'org-outline-level)
  3497. (when (and org-ellipsis
  3498. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3499. (fboundp 'make-glyph-code))
  3500. (unless org-display-table
  3501. (setq org-display-table (make-display-table)))
  3502. (set-display-table-slot
  3503. org-display-table 4
  3504. (vconcat (mapcar
  3505. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3506. org-ellipsis)))
  3507. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3508. (setq buffer-display-table org-display-table))
  3509. (org-set-regexps-and-options)
  3510. (when (and org-tag-faces (not org-tags-special-faces-re))
  3511. ;; tag faces set outside customize.... force initialization.
  3512. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3513. ;; Calc embedded
  3514. (org-set-local 'calc-embedded-open-mode "# ")
  3515. (modify-syntax-entry ?# "<")
  3516. (modify-syntax-entry ?@ "w")
  3517. (if org-startup-truncated (setq truncate-lines t))
  3518. (org-set-local 'font-lock-unfontify-region-function
  3519. 'org-unfontify-region)
  3520. ;; Activate before-change-function
  3521. (org-set-local 'org-table-may-need-update t)
  3522. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3523. 'local)
  3524. ;; Check for running clock before killing a buffer
  3525. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3526. ;; Paragraphs and auto-filling
  3527. (org-set-autofill-regexps)
  3528. (setq indent-line-function 'org-indent-line-function)
  3529. (org-update-radio-target-regexp)
  3530. ;; Make sure dependence stuff works reliably, even for users who set it
  3531. ;; too late :-(
  3532. (if org-enforce-todo-dependencies
  3533. (add-hook 'org-blocker-hook
  3534. 'org-block-todo-from-children-or-siblings-or-parent)
  3535. (remove-hook 'org-blocker-hook
  3536. 'org-block-todo-from-children-or-siblings-or-parent))
  3537. (if org-enforce-todo-checkbox-dependencies
  3538. (add-hook 'org-blocker-hook
  3539. 'org-block-todo-from-checkboxes)
  3540. (remove-hook 'org-blocker-hook
  3541. 'org-block-todo-from-checkboxes))
  3542. ;; Comment characters
  3543. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3544. (org-set-local 'comment-padding " ")
  3545. ;; Align options lines
  3546. (org-set-local
  3547. 'align-mode-rules-list
  3548. '((org-in-buffer-settings
  3549. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3550. (modes . '(org-mode)))))
  3551. ;; Imenu
  3552. (org-set-local 'imenu-create-index-function
  3553. 'org-imenu-get-tree)
  3554. ;; Make isearch reveal context
  3555. (if (or (featurep 'xemacs)
  3556. (not (boundp 'outline-isearch-open-invisible-function)))
  3557. ;; Emacs 21 and XEmacs make use of the hook
  3558. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3559. ;; Emacs 22 deals with this through a special variable
  3560. (org-set-local 'outline-isearch-open-invisible-function
  3561. (lambda (&rest ignore) (org-show-context 'isearch))))
  3562. ;; If empty file that did not turn on org-mode automatically, make it to.
  3563. (if (and org-insert-mode-line-in-empty-file
  3564. (interactive-p)
  3565. (= (point-min) (point-max)))
  3566. (insert "# -*- mode: org -*-\n\n"))
  3567. (unless org-inhibit-startup
  3568. (when org-startup-align-all-tables
  3569. (let ((bmp (buffer-modified-p)))
  3570. (org-table-map-tables 'org-table-align)
  3571. (set-buffer-modified-p bmp)))
  3572. (when org-startup-indented
  3573. (require 'org-indent)
  3574. (org-indent-mode 1))
  3575. (org-set-startup-visibility)))
  3576. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3577. (defun org-current-time ()
  3578. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3579. (if (> (car org-time-stamp-rounding-minutes) 1)
  3580. (let ((r (car org-time-stamp-rounding-minutes))
  3581. (time (decode-time)))
  3582. (apply 'encode-time
  3583. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3584. (nthcdr 2 time))))
  3585. (current-time)))
  3586. ;;;; Font-Lock stuff, including the activators
  3587. (defvar org-mouse-map (make-sparse-keymap))
  3588. (org-defkey org-mouse-map
  3589. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3590. (org-defkey org-mouse-map
  3591. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3592. (when org-mouse-1-follows-link
  3593. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3594. (when org-tab-follows-link
  3595. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3596. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3597. (require 'font-lock)
  3598. (defconst org-non-link-chars "]\t\n\r<>")
  3599. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3600. "shell" "elisp"))
  3601. (defvar org-link-types-re nil
  3602. "Matches a link that has a url-like prefix like \"http:\"")
  3603. (defvar org-link-re-with-space nil
  3604. "Matches a link with spaces, optional angular brackets around it.")
  3605. (defvar org-link-re-with-space2 nil
  3606. "Matches a link with spaces, optional angular brackets around it.")
  3607. (defvar org-link-re-with-space3 nil
  3608. "Matches a link with spaces, only for internal part in bracket links.")
  3609. (defvar org-angle-link-re nil
  3610. "Matches link with angular brackets, spaces are allowed.")
  3611. (defvar org-plain-link-re nil
  3612. "Matches plain link, without spaces.")
  3613. (defvar org-bracket-link-regexp nil
  3614. "Matches a link in double brackets.")
  3615. (defvar org-bracket-link-analytic-regexp nil
  3616. "Regular expression used to analyze links.
  3617. Here is what the match groups contain after a match:
  3618. 1: http:
  3619. 2: http
  3620. 3: path
  3621. 4: [desc]
  3622. 5: desc")
  3623. (defvar org-bracket-link-analytic-regexp++ nil
  3624. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3625. (defvar org-any-link-re nil
  3626. "Regular expression matching any link.")
  3627. (defun org-make-link-regexps ()
  3628. "Update the link regular expressions.
  3629. This should be called after the variable `org-link-types' has changed."
  3630. (setq org-link-types-re
  3631. (concat
  3632. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3633. org-link-re-with-space
  3634. (concat
  3635. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3636. "\\([^" org-non-link-chars " ]"
  3637. "[^" org-non-link-chars "]*"
  3638. "[^" org-non-link-chars " ]\\)>?")
  3639. org-link-re-with-space2
  3640. (concat
  3641. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3642. "\\([^" org-non-link-chars " ]"
  3643. "[^\t\n\r]*"
  3644. "[^" org-non-link-chars " ]\\)>?")
  3645. org-link-re-with-space3
  3646. (concat
  3647. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3648. "\\([^" org-non-link-chars " ]"
  3649. "[^\t\n\r]*\\)")
  3650. org-angle-link-re
  3651. (concat
  3652. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3653. "\\([^" org-non-link-chars " ]"
  3654. "[^" org-non-link-chars "]*"
  3655. "\\)>")
  3656. org-plain-link-re
  3657. (concat
  3658. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3659. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3660. org-bracket-link-regexp
  3661. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3662. org-bracket-link-analytic-regexp
  3663. (concat
  3664. "\\[\\["
  3665. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3666. "\\([^]]+\\)"
  3667. "\\]"
  3668. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3669. "\\]")
  3670. org-bracket-link-analytic-regexp++
  3671. (concat
  3672. "\\[\\["
  3673. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3674. "\\([^]]+\\)"
  3675. "\\]"
  3676. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3677. "\\]")
  3678. org-any-link-re
  3679. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3680. org-angle-link-re "\\)\\|\\("
  3681. org-plain-link-re "\\)")))
  3682. (org-make-link-regexps)
  3683. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3684. "Regular expression for fast time stamp matching.")
  3685. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3686. "Regular expression for fast time stamp matching.")
  3687. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3688. "Regular expression matching time strings for analysis.
  3689. This one does not require the space after the date, so it can be used
  3690. on a string that terminates immediately after the date.")
  3691. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3692. "Regular expression matching time strings for analysis.")
  3693. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3694. "Regular expression matching time stamps, with groups.")
  3695. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3696. "Regular expression matching time stamps (also [..]), with groups.")
  3697. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3698. "Regular expression matching a time stamp range.")
  3699. (defconst org-tr-regexp-both
  3700. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3701. "Regular expression matching a time stamp range.")
  3702. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3703. org-ts-regexp "\\)?")
  3704. "Regular expression matching a time stamp or time stamp range.")
  3705. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3706. org-ts-regexp-both "\\)?")
  3707. "Regular expression matching a time stamp or time stamp range.
  3708. The time stamps may be either active or inactive.")
  3709. (defvar org-emph-face nil)
  3710. (defun org-do-emphasis-faces (limit)
  3711. "Run through the buffer and add overlays to links."
  3712. (let (rtn a)
  3713. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3714. (if (not (= (char-after (match-beginning 3))
  3715. (char-after (match-beginning 4))))
  3716. (progn
  3717. (setq rtn t)
  3718. (setq a (assoc (match-string 3) org-emphasis-alist))
  3719. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3720. 'face
  3721. (nth 1 a))
  3722. (and (nth 4 a)
  3723. (org-remove-flyspell-overlays-in
  3724. (match-beginning 0) (match-end 0)))
  3725. (add-text-properties (match-beginning 2) (match-end 2)
  3726. '(font-lock-multiline t))
  3727. (when org-hide-emphasis-markers
  3728. (add-text-properties (match-end 4) (match-beginning 5)
  3729. '(invisible org-link))
  3730. (add-text-properties (match-beginning 3) (match-end 3)
  3731. '(invisible org-link)))))
  3732. (backward-char 1))
  3733. rtn))
  3734. (defun org-emphasize (&optional char)
  3735. "Insert or change an emphasis, i.e. a font like bold or italic.
  3736. If there is an active region, change that region to a new emphasis.
  3737. If there is no region, just insert the marker characters and position
  3738. the cursor between them.
  3739. CHAR should be either the marker character, or the first character of the
  3740. HTML tag associated with that emphasis. If CHAR is a space, the means
  3741. to remove the emphasis of the selected region.
  3742. If char is not given (for example in an interactive call) it
  3743. will be prompted for."
  3744. (interactive)
  3745. (let ((eal org-emphasis-alist) e det
  3746. (erc org-emphasis-regexp-components)
  3747. (prompt "")
  3748. (string "") beg end move tag c s)
  3749. (if (org-region-active-p)
  3750. (setq beg (region-beginning) end (region-end)
  3751. string (buffer-substring beg end))
  3752. (setq move t))
  3753. (while (setq e (pop eal))
  3754. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3755. c (aref tag 0))
  3756. (push (cons c (string-to-char (car e))) det)
  3757. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3758. (substring tag 1)))))
  3759. (setq det (nreverse det))
  3760. (unless char
  3761. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3762. (setq char (read-char-exclusive)))
  3763. (setq char (or (cdr (assoc char det)) char))
  3764. (if (equal char ?\ )
  3765. (setq s "" move nil)
  3766. (unless (assoc (char-to-string char) org-emphasis-alist)
  3767. (error "No such emphasis marker: \"%c\"" char))
  3768. (setq s (char-to-string char)))
  3769. (while (and (> (length string) 1)
  3770. (equal (substring string 0 1) (substring string -1))
  3771. (assoc (substring string 0 1) org-emphasis-alist))
  3772. (setq string (substring string 1 -1)))
  3773. (setq string (concat s string s))
  3774. (if beg (delete-region beg end))
  3775. (unless (or (bolp)
  3776. (string-match (concat "[" (nth 0 erc) "\n]")
  3777. (char-to-string (char-before (point)))))
  3778. (insert " "))
  3779. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3780. (char-to-string (char-after (point))))
  3781. (insert " ") (backward-char 1))
  3782. (insert string)
  3783. (and move (backward-char 1))))
  3784. (defconst org-nonsticky-props
  3785. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3786. (defsubst org-rear-nonsticky-at (pos)
  3787. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3788. (defun org-activate-plain-links (limit)
  3789. "Run through the buffer and add overlays to links."
  3790. (catch 'exit
  3791. (let (f)
  3792. (if (re-search-forward org-plain-link-re limit t)
  3793. (progn
  3794. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3795. (setq f (get-text-property (match-beginning 0) 'face))
  3796. (if (or (eq f 'org-tag)
  3797. (and (listp f) (memq 'org-tag f)))
  3798. nil
  3799. (add-text-properties (match-beginning 0) (match-end 0)
  3800. (list 'mouse-face 'highlight
  3801. 'keymap org-mouse-map))
  3802. (org-rear-nonsticky-at (match-end 0)))
  3803. t)))))
  3804. (defun org-activate-code (limit)
  3805. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3806. (progn
  3807. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3808. (remove-text-properties (match-beginning 0) (match-end 0)
  3809. '(display t invisible t intangible t))
  3810. t)))
  3811. (defun org-fontify-meta-lines-and-blocks (limit)
  3812. "Fontify #+ lines and blocks, in the correct ways."
  3813. (let ((case-fold-search t))
  3814. (if (re-search-forward
  3815. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3816. limit t)
  3817. (let ((beg (match-beginning 0))
  3818. (beg1 (line-beginning-position 2))
  3819. (dc1 (downcase (match-string 2)))
  3820. (dc3 (downcase (match-string 3)))
  3821. end end1 quoting)
  3822. (cond
  3823. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3824. ;; a single line of backend-specific content
  3825. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3826. (remove-text-properties (match-beginning 0) (match-end 0)
  3827. '(display t invisible t intangible t))
  3828. (add-text-properties (match-beginning 1) (match-end 3)
  3829. '(font-lock-fontified t face org-meta-line))
  3830. (add-text-properties (match-beginning 6) (match-end 6)
  3831. '(font-lock-fontified t face org-block))
  3832. t)
  3833. ((and (match-end 4) (equal dc3 "begin"))
  3834. ;; Truely a block
  3835. (setq quoting (member (downcase (match-string 5))
  3836. org-protecting-blocks))
  3837. (when (re-search-forward
  3838. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3839. nil t) ;; on purpose, we look further than LIMIT
  3840. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3841. (when quoting
  3842. (remove-text-properties beg end
  3843. '(display t invisible t intangible t)))
  3844. (add-text-properties
  3845. beg end
  3846. '(font-lock-fontified t font-lock-multiline t))
  3847. (add-text-properties beg beg1 '(face org-meta-line))
  3848. (add-text-properties end1 end '(face org-meta-line))
  3849. (when quoting
  3850. (add-text-properties beg1 end1 '(face org-block)))
  3851. t))
  3852. ((not (member (char-after beg) '(?\ ?\t)))
  3853. ;; just any other in-buffer setting, but not indented
  3854. (add-text-properties
  3855. beg (match-end 0)
  3856. '(font-lock-fontified t face org-meta-line))
  3857. t)
  3858. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
  3859. (and (match-end 4) (equal dc3 "attr")))
  3860. (add-text-properties
  3861. beg (match-end 0)
  3862. '(font-lock-fontified t face org-meta-line))
  3863. t)
  3864. (t nil))))))
  3865. (defun org-activate-angle-links (limit)
  3866. "Run through the buffer and add overlays to links."
  3867. (if (re-search-forward org-angle-link-re limit t)
  3868. (progn
  3869. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3870. (add-text-properties (match-beginning 0) (match-end 0)
  3871. (list 'mouse-face 'highlight
  3872. 'keymap org-mouse-map))
  3873. (org-rear-nonsticky-at (match-end 0))
  3874. t)))
  3875. (defun org-activate-footnote-links (limit)
  3876. "Run through the buffer and add overlays to links."
  3877. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3878. limit t)
  3879. (progn
  3880. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3881. (add-text-properties (match-beginning 2) (match-end 2)
  3882. (list 'mouse-face 'highlight
  3883. 'keymap org-mouse-map
  3884. 'help-echo
  3885. (if (= (point-at-bol) (match-beginning 2))
  3886. "Footnote definition"
  3887. "Footnote reference")
  3888. ))
  3889. (org-rear-nonsticky-at (match-end 2))
  3890. t)))
  3891. (defun org-activate-bracket-links (limit)
  3892. "Run through the buffer and add overlays to bracketed links."
  3893. (if (re-search-forward org-bracket-link-regexp limit t)
  3894. (let* ((help (concat "LINK: "
  3895. (org-match-string-no-properties 1)))
  3896. ;; FIXME: above we should remove the escapes.
  3897. ;; but that requires another match, protecting match data,
  3898. ;; a lot of overhead for font-lock.
  3899. (ip (org-maybe-intangible
  3900. (list 'invisible 'org-link
  3901. 'keymap org-mouse-map 'mouse-face 'highlight
  3902. 'font-lock-multiline t 'help-echo help)))
  3903. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3904. 'font-lock-multiline t 'help-echo help)))
  3905. ;; We need to remove the invisible property here. Table narrowing
  3906. ;; may have made some of this invisible.
  3907. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3908. (remove-text-properties (match-beginning 0) (match-end 0)
  3909. '(invisible nil))
  3910. (if (match-end 3)
  3911. (progn
  3912. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3913. (org-rear-nonsticky-at (match-beginning 3))
  3914. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3915. (org-rear-nonsticky-at (match-end 3))
  3916. (add-text-properties (match-end 3) (match-end 0) ip)
  3917. (org-rear-nonsticky-at (match-end 0)))
  3918. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3919. (org-rear-nonsticky-at (match-beginning 1))
  3920. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3921. (org-rear-nonsticky-at (match-end 1))
  3922. (add-text-properties (match-end 1) (match-end 0) ip)
  3923. (org-rear-nonsticky-at (match-end 0)))
  3924. t)))
  3925. (defun org-activate-dates (limit)
  3926. "Run through the buffer and add overlays to dates."
  3927. (if (re-search-forward org-tsr-regexp-both limit t)
  3928. (progn
  3929. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3930. (add-text-properties (match-beginning 0) (match-end 0)
  3931. (list 'mouse-face 'highlight
  3932. 'keymap org-mouse-map))
  3933. (org-rear-nonsticky-at (match-end 0))
  3934. (when org-display-custom-times
  3935. (if (match-end 3)
  3936. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3937. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3938. t)))
  3939. (defvar org-target-link-regexp nil
  3940. "Regular expression matching radio targets in plain text.")
  3941. (make-variable-buffer-local 'org-target-link-regexp)
  3942. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3943. "Regular expression matching a link target.")
  3944. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3945. "Regular expression matching a radio target.")
  3946. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3947. "Regular expression matching any target.")
  3948. (defun org-activate-target-links (limit)
  3949. "Run through the buffer and add overlays to target matches."
  3950. (when org-target-link-regexp
  3951. (let ((case-fold-search t))
  3952. (if (re-search-forward org-target-link-regexp limit t)
  3953. (progn
  3954. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3955. (add-text-properties (match-beginning 0) (match-end 0)
  3956. (list 'mouse-face 'highlight
  3957. 'keymap org-mouse-map
  3958. 'help-echo "Radio target link"
  3959. 'org-linked-text t))
  3960. (org-rear-nonsticky-at (match-end 0))
  3961. t)))))
  3962. (defun org-update-radio-target-regexp ()
  3963. "Find all radio targets in this file and update the regular expression."
  3964. (interactive)
  3965. (when (memq 'radio org-activate-links)
  3966. (setq org-target-link-regexp
  3967. (org-make-target-link-regexp (org-all-targets 'radio)))
  3968. (org-restart-font-lock)))
  3969. (defun org-hide-wide-columns (limit)
  3970. (let (s e)
  3971. (setq s (text-property-any (point) (or limit (point-max))
  3972. 'org-cwidth t))
  3973. (when s
  3974. (setq e (next-single-property-change s 'org-cwidth))
  3975. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3976. (goto-char e)
  3977. t)))
  3978. (defvar org-latex-and-specials-regexp nil
  3979. "Regular expression for highlighting export special stuff.")
  3980. (defvar org-match-substring-regexp)
  3981. (defvar org-match-substring-with-braces-regexp)
  3982. ;; This should be with the exporter code, but we also use if for font-locking
  3983. (defconst org-export-html-special-string-regexps
  3984. '(("\\\\-" . "&shy;")
  3985. ("---\\([^-]\\)" . "&mdash;\\1")
  3986. ("--\\([^-]\\)" . "&ndash;\\1")
  3987. ("\\.\\.\\." . "&hellip;"))
  3988. "Regular expressions for special string conversion.")
  3989. (defun org-compute-latex-and-specials-regexp ()
  3990. "Compute regular expression for stuff treated specially by exporters."
  3991. (if (not org-highlight-latex-fragments-and-specials)
  3992. (org-set-local 'org-latex-and-specials-regexp nil)
  3993. (require 'org-exp)
  3994. (let*
  3995. ((matchers (plist-get org-format-latex-options :matchers))
  3996. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3997. org-latex-regexps)))
  3998. (options (org-combine-plists (org-default-export-plist)
  3999. (org-infile-export-plist)))
  4000. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4001. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4002. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4003. (org-export-html-expand (plist-get options :expand-quoted-html))
  4004. (org-export-with-special-strings (plist-get options :special-strings))
  4005. (re-sub
  4006. (cond
  4007. ((equal org-export-with-sub-superscripts '{})
  4008. (list org-match-substring-with-braces-regexp))
  4009. (org-export-with-sub-superscripts
  4010. (list org-match-substring-regexp))
  4011. (t nil)))
  4012. (re-latex
  4013. (if org-export-with-LaTeX-fragments
  4014. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4015. (re-macros
  4016. (if org-export-with-TeX-macros
  4017. (list (concat "\\\\"
  4018. (regexp-opt
  4019. (append (mapcar 'car org-html-entities)
  4020. (if (boundp 'org-latex-entities)
  4021. (mapcar (lambda (x)
  4022. (or (car-safe x) x))
  4023. org-latex-entities)
  4024. nil))
  4025. 'words))) ; FIXME
  4026. ))
  4027. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4028. (re-special (if org-export-with-special-strings
  4029. (mapcar (lambda (x) (car x))
  4030. org-export-html-special-string-regexps)))
  4031. (re-rest
  4032. (delq nil
  4033. (list
  4034. (if org-export-html-expand "@<[^>\n]+>")
  4035. ))))
  4036. (org-set-local
  4037. 'org-latex-and-specials-regexp
  4038. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4039. re-rest) "\\|")))))
  4040. (defun org-do-latex-and-special-faces (limit)
  4041. "Run through the buffer and add overlays to links."
  4042. (when org-latex-and-specials-regexp
  4043. (let (rtn d)
  4044. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4045. limit t))
  4046. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4047. 'face))
  4048. '(org-code org-verbatim underline)))
  4049. (progn
  4050. (setq rtn t
  4051. d (cond ((member (char-after (1+ (match-beginning 0)))
  4052. '(?_ ?^)) 1)
  4053. (t 0)))
  4054. (font-lock-prepend-text-property
  4055. (+ d (match-beginning 0)) (match-end 0)
  4056. 'face 'org-latex-and-export-specials)
  4057. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4058. '(font-lock-multiline t)))))
  4059. rtn)))
  4060. (defun org-restart-font-lock ()
  4061. "Restart font-lock-mode, to force refontification."
  4062. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4063. (font-lock-mode -1)
  4064. (font-lock-mode 1)))
  4065. (defun org-all-targets (&optional radio)
  4066. "Return a list of all targets in this file.
  4067. With optional argument RADIO, only find radio targets."
  4068. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4069. rtn)
  4070. (save-excursion
  4071. (goto-char (point-min))
  4072. (while (re-search-forward re nil t)
  4073. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4074. rtn)))
  4075. (defun org-make-target-link-regexp (targets)
  4076. "Make regular expression matching all strings in TARGETS.
  4077. The regular expression finds the targets also if there is a line break
  4078. between words."
  4079. (and targets
  4080. (concat
  4081. "\\<\\("
  4082. (mapconcat
  4083. (lambda (x)
  4084. (while (string-match " +" x)
  4085. (setq x (replace-match "\\s-+" t t x)))
  4086. x)
  4087. targets
  4088. "\\|")
  4089. "\\)\\>")))
  4090. (defun org-activate-tags (limit)
  4091. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4092. (progn
  4093. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4094. (add-text-properties (match-beginning 1) (match-end 1)
  4095. (list 'mouse-face 'highlight
  4096. 'keymap org-mouse-map))
  4097. (org-rear-nonsticky-at (match-end 1))
  4098. t)))
  4099. (defun org-outline-level ()
  4100. (save-excursion
  4101. (looking-at outline-regexp)
  4102. (if (match-beginning 1)
  4103. (+ (org-get-string-indentation (match-string 1)) 1000)
  4104. (1- (- (match-end 0) (match-beginning 0))))))
  4105. (defvar org-font-lock-keywords nil)
  4106. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4107. "Regular expression matching a property line.")
  4108. (defvar org-font-lock-hook nil
  4109. "Functions to be called for special font lock stuff.")
  4110. (defun org-font-lock-hook (limit)
  4111. (run-hook-with-args 'org-font-lock-hook limit))
  4112. (defun org-set-font-lock-defaults ()
  4113. (let* ((em org-fontify-emphasized-text)
  4114. (lk org-activate-links)
  4115. (org-font-lock-extra-keywords
  4116. (list
  4117. ;; Call the hook
  4118. '(org-font-lock-hook)
  4119. ;; Headlines
  4120. `(,(if org-fontify-whole-heading-line
  4121. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4122. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4123. (1 (org-get-level-face 1))
  4124. (2 (org-get-level-face 2))
  4125. (3 (org-get-level-face 3)))
  4126. ;; Table lines
  4127. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4128. (1 'org-table t))
  4129. ;; Table internals
  4130. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4131. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4132. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4133. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4134. ;; Drawers
  4135. (list org-drawer-regexp '(0 'org-special-keyword t))
  4136. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4137. ;; Properties
  4138. (list org-property-re
  4139. '(1 'org-special-keyword t)
  4140. '(3 'org-property-value t))
  4141. ;; Links
  4142. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4143. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4144. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4145. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4146. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4147. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4148. (if (memq 'footnote lk) '(org-activate-footnote-links
  4149. (2 'org-footnote t)))
  4150. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4151. '(org-hide-wide-columns (0 nil append))
  4152. ;; TODO lines
  4153. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4154. '(1 (org-get-todo-face 1) t))
  4155. ;; DONE
  4156. (if org-fontify-done-headline
  4157. (list (concat "^[*]+ +\\<\\("
  4158. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4159. "\\)\\(.*\\)")
  4160. '(2 'org-headline-done t))
  4161. nil)
  4162. ;; Priorities
  4163. '(org-font-lock-add-priority-faces)
  4164. ;; Tags
  4165. '(org-font-lock-add-tag-faces)
  4166. ;; Special keywords
  4167. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4168. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4169. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4170. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4171. ;; Emphasis
  4172. (if em
  4173. (if (featurep 'xemacs)
  4174. '(org-do-emphasis-faces (0 nil append))
  4175. '(org-do-emphasis-faces)))
  4176. ;; Checkboxes
  4177. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4178. 2 'org-checkbox prepend)
  4179. (if org-provide-checkbox-statistics
  4180. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4181. (0 (org-get-checkbox-statistics-face) t)))
  4182. ;; Description list items
  4183. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4184. 2 'bold prepend)
  4185. ;; ARCHIVEd headings
  4186. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4187. '(1 'org-archived prepend))
  4188. ;; Specials
  4189. '(org-do-latex-and-special-faces)
  4190. ;; Code
  4191. '(org-activate-code (1 'org-code t))
  4192. ;; COMMENT
  4193. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4194. "\\|" org-quote-string "\\)\\>")
  4195. '(1 'org-special-keyword t))
  4196. '("^#.*" (0 'font-lock-comment-face t))
  4197. ;; Blocks and meta lines
  4198. '(org-fontify-meta-lines-and-blocks)
  4199. )))
  4200. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4201. ;; Now set the full font-lock-keywords
  4202. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4203. (org-set-local 'font-lock-defaults
  4204. '(org-font-lock-keywords t nil nil backward-paragraph))
  4205. (kill-local-variable 'font-lock-keywords) nil))
  4206. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4207. "Fontify string S like in Org-mode"
  4208. (with-temp-buffer
  4209. (insert s)
  4210. (let ((org-odd-levels-only odd-levels))
  4211. (org-mode)
  4212. (font-lock-fontify-buffer)
  4213. (buffer-string))))
  4214. (defvar org-m nil)
  4215. (defvar org-l nil)
  4216. (defvar org-f nil)
  4217. (defun org-get-level-face (n)
  4218. "Get the right face for match N in font-lock matching of headlines."
  4219. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4220. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4221. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4222. (cond
  4223. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4224. ((eq n 2) org-f)
  4225. (t (if org-level-color-stars-only nil org-f))))
  4226. (defun org-get-todo-face (kwd)
  4227. "Get the right face for a TODO keyword KWD.
  4228. If KWD is a number, get the corresponding match group."
  4229. (if (numberp kwd) (setq kwd (match-string kwd)))
  4230. (or (cdr (assoc kwd org-todo-keyword-faces))
  4231. (and (member kwd org-done-keywords) 'org-done)
  4232. 'org-todo))
  4233. (defun org-font-lock-add-tag-faces (limit)
  4234. "Add the special tag faces."
  4235. (when (and org-tag-faces org-tags-special-faces-re)
  4236. (while (re-search-forward org-tags-special-faces-re limit t)
  4237. (add-text-properties (match-beginning 1) (match-end 1)
  4238. (list 'face (org-get-tag-face 1)
  4239. 'font-lock-fontified t))
  4240. (backward-char 1))))
  4241. (defun org-font-lock-add-priority-faces (limit)
  4242. "Add the special priority faces."
  4243. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4244. (add-text-properties
  4245. (match-beginning 0) (match-end 0)
  4246. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4247. org-priority-faces))
  4248. 'org-special-keyword)
  4249. 'font-lock-fontified t))))
  4250. (defun org-get-tag-face (kwd)
  4251. "Get the right face for a TODO keyword KWD.
  4252. If KWD is a number, get the corresponding match group."
  4253. (if (numberp kwd) (setq kwd (match-string kwd)))
  4254. (or (cdr (assoc kwd org-tag-faces))
  4255. 'org-tag))
  4256. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4257. "Remove fontification and activation overlays from links."
  4258. (font-lock-default-unfontify-region beg end)
  4259. (let* ((buffer-undo-list t)
  4260. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4261. (inhibit-modification-hooks t)
  4262. deactivate-mark buffer-file-name buffer-file-truename)
  4263. (remove-text-properties beg end
  4264. '(mouse-face t keymap t org-linked-text t
  4265. invisible t intangible t
  4266. line-prefix t wrap-prefix t
  4267. org-no-flyspell t))))
  4268. ;;;; Visibility cycling, including org-goto and indirect buffer
  4269. ;;; Cycling
  4270. (defvar org-cycle-global-status nil)
  4271. (make-variable-buffer-local 'org-cycle-global-status)
  4272. (defvar org-cycle-subtree-status nil)
  4273. (make-variable-buffer-local 'org-cycle-subtree-status)
  4274. ;;;###autoload
  4275. (defvar org-inlinetask-min-level)
  4276. (defun org-cycle (&optional arg)
  4277. "TAB-action and visibility cycling for Org-mode.
  4278. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4279. is outine visibility cycling, but it also invokes other actions
  4280. in special contexts.
  4281. - When this function is called with a prefix argument, rotate the entire
  4282. buffer through 3 states (global cycling)
  4283. 1. OVERVIEW: Show only top-level headlines.
  4284. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4285. 3. SHOW ALL: Show everything.
  4286. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4287. determined by the variable `org-startup-folded', and by any VISIBILITY
  4288. properties in the buffer.
  4289. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4290. including any drawers.
  4291. - When inside a table, re-align the table and move to the next field.
  4292. - When point is at the beginning of a headline, rotate the subtree started
  4293. by this line through 3 different states (local cycling)
  4294. 1. FOLDED: Only the main headline is shown.
  4295. 2. CHILDREN: The main headline and the direct children are shown.
  4296. From this state, you can move to one of the children
  4297. and zoom in further.
  4298. 3. SUBTREE: Show the entire subtree, including body text.
  4299. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4300. - When there is a numeric prefix, go up to a heading with level ARG, do
  4301. a `show-subtree' and return to the previous cursor position. If ARG
  4302. is negative, go up that many levels.
  4303. - When point is not at the beginning of a headline, execute the global
  4304. binding for TAB, which is re-indenting the line. See the option
  4305. `org-cycle-emulate-tab' for details.
  4306. - Special case: if point is at the beginning of the buffer and there is
  4307. no headline in line 1, this function will act as if called with prefix arg.
  4308. But only if also the variable `org-cycle-global-at-bob' is t."
  4309. (interactive "P")
  4310. (org-load-modules-maybe)
  4311. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4312. (let* ((limit-level
  4313. (or org-cycle-max-level
  4314. (and (boundp 'org-inlinetask-min-level)
  4315. org-inlinetask-min-level
  4316. (1- org-inlinetask-min-level))))
  4317. (nstars (and limit-level
  4318. (if org-odd-levels-only
  4319. (and limit-level (1- (* limit-level 2)))
  4320. limit-level)))
  4321. (outline-regexp
  4322. (cond
  4323. ((not (org-mode-p)) outline-regexp)
  4324. ((or (eq org-cycle-include-plain-lists 'integrate)
  4325. (and org-cycle-include-plain-lists (org-at-item-p)))
  4326. (concat "\\(?:\\*"
  4327. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4328. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4329. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4330. (bob-special (and org-cycle-global-at-bob (bobp)
  4331. (not (looking-at outline-regexp))))
  4332. (org-cycle-hook
  4333. (if bob-special
  4334. (delq 'org-optimize-window-after-visibility-change
  4335. (copy-sequence org-cycle-hook))
  4336. org-cycle-hook))
  4337. (pos (point)))
  4338. (if (or bob-special (equal arg '(4)))
  4339. ;; special case: use global cycling
  4340. (setq arg t))
  4341. (cond
  4342. ((equal arg '(16))
  4343. (org-set-startup-visibility)
  4344. (message "Startup visibility, plus VISIBILITY properties"))
  4345. ((equal arg '(64))
  4346. (show-all)
  4347. (message "Entire buffer visible, including drawers"))
  4348. ((org-at-table-p 'any)
  4349. ;; Enter the table or move to the next field in the table
  4350. (or (org-table-recognize-table.el)
  4351. (progn
  4352. (if arg (org-table-edit-field t)
  4353. (org-table-justify-field-maybe)
  4354. (call-interactively 'org-table-next-field)))))
  4355. ((run-hook-with-args-until-success
  4356. 'org-tab-after-check-for-table-hook))
  4357. ((eq arg t) ;; Global cycling
  4358. (org-cycle-internal-global))
  4359. ((and org-drawers org-drawer-regexp
  4360. (save-excursion
  4361. (beginning-of-line 1)
  4362. (looking-at org-drawer-regexp)))
  4363. ;; Toggle block visibility
  4364. (org-flag-drawer
  4365. (not (get-char-property (match-end 0) 'invisible))))
  4366. ((integerp arg)
  4367. ;; Show-subtree, ARG levels up from here.
  4368. (save-excursion
  4369. (org-back-to-heading)
  4370. (outline-up-heading (if (< arg 0) (- arg)
  4371. (- (funcall outline-level) arg)))
  4372. (org-show-subtree)))
  4373. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4374. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4375. (org-cycle-internal-local))
  4376. ;; TAB emulation and template completion
  4377. (buffer-read-only (org-back-to-heading))
  4378. ((run-hook-with-args-until-success
  4379. 'org-tab-after-check-for-cycling-hook))
  4380. ((org-try-structure-completion))
  4381. ((org-try-cdlatex-tab))
  4382. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4383. (or (not (bolp))
  4384. (not (looking-at outline-regexp))))
  4385. (call-interactively (global-key-binding "\t")))
  4386. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4387. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4388. (or (and (eq org-cycle-emulate-tab 'white)
  4389. (= (match-end 0) (point-at-eol)))
  4390. (and (eq org-cycle-emulate-tab 'whitestart)
  4391. (>= (match-end 0) pos))))
  4392. t
  4393. (eq org-cycle-emulate-tab t))
  4394. (call-interactively (global-key-binding "\t")))
  4395. (t (save-excursion
  4396. (org-back-to-heading)
  4397. (org-cycle)))))))
  4398. (defun org-cycle-internal-global ()
  4399. "Do the global cycling action."
  4400. (cond
  4401. ((and (eq last-command this-command)
  4402. (eq org-cycle-global-status 'overview))
  4403. ;; We just created the overview - now do table of contents
  4404. ;; This can be slow in very large buffers, so indicate action
  4405. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4406. (message "CONTENTS...")
  4407. (org-content)
  4408. (message "CONTENTS...done")
  4409. (setq org-cycle-global-status 'contents)
  4410. (run-hook-with-args 'org-cycle-hook 'contents))
  4411. ((and (eq last-command this-command)
  4412. (eq org-cycle-global-status 'contents))
  4413. ;; We just showed the table of contents - now show everything
  4414. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4415. (show-all)
  4416. (message "SHOW ALL")
  4417. (setq org-cycle-global-status 'all)
  4418. (run-hook-with-args 'org-cycle-hook 'all))
  4419. (t
  4420. ;; Default action: go to overview
  4421. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4422. (org-overview)
  4423. (message "OVERVIEW")
  4424. (setq org-cycle-global-status 'overview)
  4425. (run-hook-with-args 'org-cycle-hook 'overview))))
  4426. (defun org-cycle-internal-local ()
  4427. "Do the local cycling action."
  4428. (org-back-to-heading)
  4429. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4430. ;; First, some boundaries
  4431. (save-excursion
  4432. (org-back-to-heading)
  4433. (setq level (funcall outline-level))
  4434. (save-excursion
  4435. (beginning-of-line 2)
  4436. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4437. ; XEmacs does not have `next-single-char-property-change'
  4438. ; I'm not sure about Emacs 21.
  4439. (while (and (not (eobp)) ;; this is like `next-line'
  4440. (get-char-property (1- (point)) 'invisible))
  4441. (beginning-of-line 2))
  4442. (while (and (not (eobp)) ;; this is like `next-line'
  4443. (get-char-property (1- (point)) 'invisible))
  4444. (goto-char (next-single-char-property-change (point) 'invisible))
  4445. ;;;??? (or (bolp) (beginning-of-line 2))))
  4446. (and (eolp) (beginning-of-line 2))))
  4447. (setq eol (point)))
  4448. (outline-end-of-heading) (setq eoh (point))
  4449. (save-excursion
  4450. (outline-next-heading)
  4451. (setq has-children (and (org-at-heading-p t)
  4452. (> (funcall outline-level) level))))
  4453. (org-end-of-subtree t)
  4454. (unless (eobp)
  4455. (skip-chars-forward " \t\n")
  4456. (beginning-of-line 1) ; in case this is an item
  4457. )
  4458. (setq eos (if (eobp) (point) (1- (point)))))
  4459. ;; Find out what to do next and set `this-command'
  4460. (cond
  4461. ((= eos eoh)
  4462. ;; Nothing is hidden behind this heading
  4463. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4464. (message "EMPTY ENTRY")
  4465. (setq org-cycle-subtree-status nil)
  4466. (save-excursion
  4467. (goto-char eos)
  4468. (outline-next-heading)
  4469. (if (org-invisible-p) (org-flag-heading nil))))
  4470. ((and (or (>= eol eos)
  4471. (not (string-match "\\S-" (buffer-substring eol eos))))
  4472. (or has-children
  4473. (not (setq children-skipped
  4474. org-cycle-skip-children-state-if-no-children))))
  4475. ;; Entire subtree is hidden in one line: children view
  4476. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4477. (org-show-entry)
  4478. (show-children)
  4479. (message "CHILDREN")
  4480. (save-excursion
  4481. (goto-char eos)
  4482. (outline-next-heading)
  4483. (if (org-invisible-p) (org-flag-heading nil)))
  4484. (setq org-cycle-subtree-status 'children)
  4485. (run-hook-with-args 'org-cycle-hook 'children))
  4486. ((or children-skipped
  4487. (and (eq last-command this-command)
  4488. (eq org-cycle-subtree-status 'children)))
  4489. ;; We just showed the children, or no children are there,
  4490. ;; now show everything.
  4491. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4492. (org-show-subtree)
  4493. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4494. (setq org-cycle-subtree-status 'subtree)
  4495. (run-hook-with-args 'org-cycle-hook 'subtree))
  4496. (t
  4497. ;; Default action: hide the subtree.
  4498. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4499. (hide-subtree)
  4500. (message "FOLDED")
  4501. (setq org-cycle-subtree-status 'folded)
  4502. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4503. ;;;###autoload
  4504. (defun org-global-cycle (&optional arg)
  4505. "Cycle the global visibility. For details see `org-cycle'.
  4506. With C-u prefix arg, switch to startup visibility.
  4507. With a numeric prefix, show all headlines up to that level."
  4508. (interactive "P")
  4509. (let ((org-cycle-include-plain-lists
  4510. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4511. (cond
  4512. ((integerp arg)
  4513. (show-all)
  4514. (hide-sublevels arg)
  4515. (setq org-cycle-global-status 'contents))
  4516. ((equal arg '(4))
  4517. (org-set-startup-visibility)
  4518. (message "Startup visibility, plus VISIBILITY properties."))
  4519. (t
  4520. (org-cycle '(4))))))
  4521. (defun org-set-startup-visibility ()
  4522. "Set the visibility required by startup options and properties."
  4523. (cond
  4524. ((eq org-startup-folded t)
  4525. (org-cycle '(4)))
  4526. ((eq org-startup-folded 'content)
  4527. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4528. (org-cycle '(4)) (org-cycle '(4)))))
  4529. (if org-hide-block-startup (org-hide-block-all))
  4530. (org-set-visibility-according-to-property 'no-cleanup)
  4531. (org-cycle-hide-archived-subtrees 'all)
  4532. (org-cycle-hide-drawers 'all)
  4533. (org-cycle-show-empty-lines 'all))
  4534. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4535. "Switch subtree visibilities according to :VISIBILITY: property."
  4536. (interactive)
  4537. (let (org-show-entry-below state)
  4538. (save-excursion
  4539. (goto-char (point-min))
  4540. (while (re-search-forward
  4541. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4542. nil t)
  4543. (setq state (match-string 1))
  4544. (save-excursion
  4545. (org-back-to-heading t)
  4546. (hide-subtree)
  4547. (org-reveal)
  4548. (cond
  4549. ((equal state '("fold" "folded"))
  4550. (hide-subtree))
  4551. ((equal state "children")
  4552. (org-show-hidden-entry)
  4553. (show-children))
  4554. ((equal state "content")
  4555. (save-excursion
  4556. (save-restriction
  4557. (org-narrow-to-subtree)
  4558. (org-content))))
  4559. ((member state '("all" "showall"))
  4560. (show-subtree)))))
  4561. (unless no-cleanup
  4562. (org-cycle-hide-archived-subtrees 'all)
  4563. (org-cycle-hide-drawers 'all)
  4564. (org-cycle-show-empty-lines 'all)))))
  4565. (defun org-overview ()
  4566. "Switch to overview mode, showing only top-level headlines.
  4567. Really, this shows all headlines with level equal or greater than the level
  4568. of the first headline in the buffer. This is important, because if the
  4569. first headline is not level one, then (hide-sublevels 1) gives confusing
  4570. results."
  4571. (interactive)
  4572. (let ((level (save-excursion
  4573. (goto-char (point-min))
  4574. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4575. (progn
  4576. (goto-char (match-beginning 0))
  4577. (funcall outline-level))))))
  4578. (and level (hide-sublevels level))))
  4579. (defun org-content (&optional arg)
  4580. "Show all headlines in the buffer, like a table of contents.
  4581. With numerical argument N, show content up to level N."
  4582. (interactive "P")
  4583. (save-excursion
  4584. ;; Visit all headings and show their offspring
  4585. (and (integerp arg) (org-overview))
  4586. (goto-char (point-max))
  4587. (catch 'exit
  4588. (while (and (progn (condition-case nil
  4589. (outline-previous-visible-heading 1)
  4590. (error (goto-char (point-min))))
  4591. t)
  4592. (looking-at outline-regexp))
  4593. (if (integerp arg)
  4594. (show-children (1- arg))
  4595. (show-branches))
  4596. (if (bobp) (throw 'exit nil))))))
  4597. (defun org-optimize-window-after-visibility-change (state)
  4598. "Adjust the window after a change in outline visibility.
  4599. This function is the default value of the hook `org-cycle-hook'."
  4600. (when (get-buffer-window (current-buffer))
  4601. (cond
  4602. ((eq state 'content) nil)
  4603. ((eq state 'all) nil)
  4604. ((eq state 'folded) nil)
  4605. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4606. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4607. ;; FIXME: no longer in use
  4608. (defun org-compact-display-after-subtree-move ()
  4609. "Show a compacter version of the tree of the entry's parent."
  4610. (save-excursion
  4611. (if (org-up-heading-safe)
  4612. (progn
  4613. (hide-subtree)
  4614. (show-entry)
  4615. (show-children)
  4616. (org-cycle-show-empty-lines 'children)
  4617. (org-cycle-hide-drawers 'children))
  4618. (org-overview))))
  4619. (defun org-remove-empty-overlays-at (pos)
  4620. "Remove outline overlays that do not contain non-white stuff."
  4621. (mapc
  4622. (lambda (o)
  4623. (and (eq 'outline (org-overlay-get o 'invisible))
  4624. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4625. (org-overlay-end o))))
  4626. (org-delete-overlay o)))
  4627. (org-overlays-at pos)))
  4628. (defun org-clean-visibility-after-subtree-move ()
  4629. "Fix visibility issues after moving a subtree."
  4630. ;; First, find a reasonable region to look at:
  4631. ;; Start two siblings above, end three below
  4632. (let* ((beg (save-excursion
  4633. (and (outline-get-last-sibling)
  4634. (outline-get-last-sibling))
  4635. (point)))
  4636. (end (save-excursion
  4637. (and (outline-get-next-sibling)
  4638. (outline-get-next-sibling)
  4639. (outline-get-next-sibling))
  4640. (if (org-at-heading-p)
  4641. (point-at-eol)
  4642. (point))))
  4643. (level (looking-at "\\*+"))
  4644. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4645. (save-excursion
  4646. (save-restriction
  4647. (narrow-to-region beg end)
  4648. (when re
  4649. ;; Properly fold already folded siblings
  4650. (goto-char (point-min))
  4651. (while (re-search-forward re nil t)
  4652. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4653. (hide-entry))))
  4654. (org-cycle-show-empty-lines 'overview)
  4655. (org-cycle-hide-drawers 'overview)))))
  4656. (defun org-cycle-show-empty-lines (state)
  4657. "Show empty lines above all visible headlines.
  4658. The region to be covered depends on STATE when called through
  4659. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4660. entire buffer covered. Note that an empty line is only shown if there
  4661. are at least `org-cycle-separator-lines' empty lines before the headline."
  4662. (when (> org-cycle-separator-lines 0)
  4663. (save-excursion
  4664. (let* ((n org-cycle-separator-lines)
  4665. (re (cond
  4666. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4667. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4668. (t (let ((ns (number-to-string (- n 2))))
  4669. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4670. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4671. beg end)
  4672. (cond
  4673. ((memq state '(overview contents t))
  4674. (setq beg (point-min) end (point-max)))
  4675. ((memq state '(children folded))
  4676. (setq beg (point) end (progn (org-end-of-subtree t t)
  4677. (beginning-of-line 2)
  4678. (point)))))
  4679. (when beg
  4680. (goto-char beg)
  4681. (while (re-search-forward re end t)
  4682. (if (not (get-char-property (match-end 1) 'invisible))
  4683. (outline-flag-region
  4684. (match-beginning 1) (match-end 1) nil)))))))
  4685. ;; Never hide empty lines at the end of the file.
  4686. (save-excursion
  4687. (goto-char (point-max))
  4688. (outline-previous-heading)
  4689. (outline-end-of-heading)
  4690. (if (and (looking-at "[ \t\n]+")
  4691. (= (match-end 0) (point-max)))
  4692. (outline-flag-region (point) (match-end 0) nil))))
  4693. (defun org-show-empty-lines-in-parent ()
  4694. "Move to the parent and re-show empty lines before visible headlines."
  4695. (save-excursion
  4696. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4697. (org-cycle-show-empty-lines context))))
  4698. (defun org-cycle-hide-drawers (state)
  4699. "Re-hide all drawers after a visibility state change."
  4700. (when (and (org-mode-p)
  4701. (not (memq state '(overview folded contents))))
  4702. (save-excursion
  4703. (let* ((globalp (memq state '(contents all)))
  4704. (beg (if globalp (point-min) (point)))
  4705. (end (if globalp (point-max)
  4706. (if (eq state 'children)
  4707. (save-excursion (outline-next-heading) (point))
  4708. (org-end-of-subtree t)))))
  4709. (goto-char beg)
  4710. (while (re-search-forward org-drawer-regexp end t)
  4711. (org-flag-drawer t))))))
  4712. (defun org-flag-drawer (flag)
  4713. (save-excursion
  4714. (beginning-of-line 1)
  4715. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4716. (let ((b (match-end 0))
  4717. (outline-regexp org-outline-regexp))
  4718. (if (re-search-forward
  4719. "^[ \t]*:END:"
  4720. (save-excursion (outline-next-heading) (point)) t)
  4721. (outline-flag-region b (point-at-eol) flag)
  4722. (error ":END: line missing"))))))
  4723. (defun org-subtree-end-visible-p ()
  4724. "Is the end of the current subtree visible?"
  4725. (pos-visible-in-window-p
  4726. (save-excursion (org-end-of-subtree t) (point))))
  4727. (defun org-first-headline-recenter (&optional N)
  4728. "Move cursor to the first headline and recenter the headline.
  4729. Optional argument N means, put the headline into the Nth line of the window."
  4730. (goto-char (point-min))
  4731. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4732. (beginning-of-line)
  4733. (recenter (prefix-numeric-value N))))
  4734. ;;; Folding of blocks
  4735. (defconst org-block-regexp
  4736. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4737. "Regular expression for hiding blocks.")
  4738. (defvar org-hide-block-overlays nil
  4739. "Overays hiding blocks.")
  4740. (make-variable-buffer-local 'org-hide-block-overlays)
  4741. (defun org-block-map (function &optional start end)
  4742. "Call func at the head of all source blocks in the current
  4743. buffer. Optional arguments START and END can be used to limit
  4744. the range."
  4745. (let ((start (or start (point-min)))
  4746. (end (or end (point-max))))
  4747. (save-excursion
  4748. (goto-char start)
  4749. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4750. (save-excursion
  4751. (save-match-data
  4752. (goto-char (match-beginning 0))
  4753. (funcall function)))))))
  4754. (defun org-hide-block-toggle-all ()
  4755. "Toggle the visibility of all blocks in the current buffer."
  4756. (org-block-map #'org-hide-block-toggle))
  4757. (defun org-hide-block-all ()
  4758. "Fold all blocks in the current buffer."
  4759. (interactive)
  4760. (org-show-block-all)
  4761. (org-block-map #'org-hide-block-toggle-maybe))
  4762. (defun org-show-block-all ()
  4763. "Unfold all blocks in the current buffer."
  4764. (mapc 'org-delete-overlay org-hide-block-overlays)
  4765. (setq org-hide-block-overlays nil))
  4766. (defun org-hide-block-toggle-maybe ()
  4767. "Toggle visibility of block at point."
  4768. (interactive)
  4769. (let ((case-fold-search t))
  4770. (if (save-excursion
  4771. (beginning-of-line 1)
  4772. (looking-at org-block-regexp))
  4773. (progn (org-hide-block-toggle)
  4774. t) ;; to signal that we took action
  4775. nil))) ;; to signal that we did not
  4776. (defun org-hide-block-toggle (&optional force)
  4777. "Toggle the visibility of the current block."
  4778. (interactive)
  4779. (save-excursion
  4780. (beginning-of-line)
  4781. (if (re-search-forward org-block-regexp nil t)
  4782. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4783. (end (match-end 0))
  4784. ov) ;; end of entire body
  4785. (if (memq t (mapcar (lambda (overlay)
  4786. (eq (org-overlay-get overlay 'invisible)
  4787. 'org-hide-block))
  4788. (org-overlays-at start)))
  4789. (if (or (not force) (eq force 'off))
  4790. (mapc (lambda (ov)
  4791. (when (member ov org-hide-block-overlays)
  4792. (setq org-hide-block-overlays
  4793. (delq ov org-hide-block-overlays)))
  4794. (when (eq (org-overlay-get ov 'invisible)
  4795. 'org-hide-block)
  4796. (org-delete-overlay ov)))
  4797. (org-overlays-at start)))
  4798. (setq ov (org-make-overlay start end))
  4799. (org-overlay-put ov 'invisible 'org-hide-block)
  4800. (push ov org-hide-block-overlays)))
  4801. (error "Not looking at a source block"))))
  4802. ;; org-tab-after-check-for-cycling-hook
  4803. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4804. ;; Remove overlays when changing major mode
  4805. (add-hook 'org-mode-hook
  4806. (lambda () (org-add-hook 'change-major-mode-hook
  4807. 'org-show-block-all 'append 'local)))
  4808. ;;; Org-goto
  4809. (defvar org-goto-window-configuration nil)
  4810. (defvar org-goto-marker nil)
  4811. (defvar org-goto-map
  4812. (let ((map (make-sparse-keymap)))
  4813. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4814. (while (setq cmd (pop cmds))
  4815. (substitute-key-definition cmd cmd map global-map)))
  4816. (suppress-keymap map)
  4817. (org-defkey map "\C-m" 'org-goto-ret)
  4818. (org-defkey map [(return)] 'org-goto-ret)
  4819. (org-defkey map [(left)] 'org-goto-left)
  4820. (org-defkey map [(right)] 'org-goto-right)
  4821. (org-defkey map [(control ?g)] 'org-goto-quit)
  4822. (org-defkey map "\C-i" 'org-cycle)
  4823. (org-defkey map [(tab)] 'org-cycle)
  4824. (org-defkey map [(down)] 'outline-next-visible-heading)
  4825. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4826. (if org-goto-auto-isearch
  4827. (if (fboundp 'define-key-after)
  4828. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4829. nil)
  4830. (org-defkey map "q" 'org-goto-quit)
  4831. (org-defkey map "n" 'outline-next-visible-heading)
  4832. (org-defkey map "p" 'outline-previous-visible-heading)
  4833. (org-defkey map "f" 'outline-forward-same-level)
  4834. (org-defkey map "b" 'outline-backward-same-level)
  4835. (org-defkey map "u" 'outline-up-heading))
  4836. (org-defkey map "/" 'org-occur)
  4837. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4838. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4839. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4840. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4841. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4842. map))
  4843. (defconst org-goto-help
  4844. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4845. RET=jump to location [Q]uit and return to previous location
  4846. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4847. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4848. ;; FIXME: Docstring doe not mention both interfaces
  4849. (defun org-goto (&optional alternative-interface)
  4850. "Look up a different location in the current file, keeping current visibility.
  4851. When you want look-up or go to a different location in a document, the
  4852. fastest way is often to fold the entire buffer and then dive into the tree.
  4853. This method has the disadvantage, that the previous location will be folded,
  4854. which may not be what you want.
  4855. This command works around this by showing a copy of the current buffer
  4856. in an indirect buffer, in overview mode. You can dive into the tree in
  4857. that copy, use org-occur and incremental search to find a location.
  4858. When pressing RET or `Q', the command returns to the original buffer in
  4859. which the visibility is still unchanged. After RET is will also jump to
  4860. the location selected in the indirect buffer and expose the
  4861. the headline hierarchy above."
  4862. (interactive "P")
  4863. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4864. (org-refile-use-outline-path t)
  4865. (org-refile-target-verify-function nil)
  4866. (interface
  4867. (if (not alternative-interface)
  4868. org-goto-interface
  4869. (if (eq org-goto-interface 'outline)
  4870. 'outline-path-completion
  4871. 'outline)))
  4872. (org-goto-start-pos (point))
  4873. (selected-point
  4874. (if (eq interface 'outline)
  4875. (car (org-get-location (current-buffer) org-goto-help))
  4876. (nth 3 (org-refile-get-location "Goto: ")))))
  4877. (if selected-point
  4878. (progn
  4879. (org-mark-ring-push org-goto-start-pos)
  4880. (goto-char selected-point)
  4881. (if (or (org-invisible-p) (org-invisible-p2))
  4882. (org-show-context 'org-goto)))
  4883. (message "Quit"))))
  4884. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4885. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4886. (defvar org-goto-local-auto-isearch-map) ; defined below
  4887. (defun org-get-location (buf help)
  4888. "Let the user select a location in the Org-mode buffer BUF.
  4889. This function uses a recursive edit. It returns the selected position
  4890. or nil."
  4891. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4892. (isearch-hide-immediately nil)
  4893. (isearch-search-fun-function
  4894. (lambda () 'org-goto-local-search-headings))
  4895. (org-goto-selected-point org-goto-exit-command))
  4896. (save-excursion
  4897. (save-window-excursion
  4898. (delete-other-windows)
  4899. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4900. (switch-to-buffer
  4901. (condition-case nil
  4902. (make-indirect-buffer (current-buffer) "*org-goto*")
  4903. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4904. (with-output-to-temp-buffer "*Help*"
  4905. (princ help))
  4906. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4907. (setq buffer-read-only nil)
  4908. (let ((org-startup-truncated t)
  4909. (org-startup-folded nil)
  4910. (org-startup-align-all-tables nil))
  4911. (org-mode)
  4912. (org-overview))
  4913. (setq buffer-read-only t)
  4914. (if (and (boundp 'org-goto-start-pos)
  4915. (integer-or-marker-p org-goto-start-pos))
  4916. (let ((org-show-hierarchy-above t)
  4917. (org-show-siblings t)
  4918. (org-show-following-heading t))
  4919. (goto-char org-goto-start-pos)
  4920. (and (org-invisible-p) (org-show-context)))
  4921. (goto-char (point-min)))
  4922. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4923. (message "Select location and press RET")
  4924. (use-local-map org-goto-map)
  4925. (recursive-edit)
  4926. ))
  4927. (kill-buffer "*org-goto*")
  4928. (cons org-goto-selected-point org-goto-exit-command)))
  4929. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4930. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4931. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4932. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4933. (defun org-goto-local-search-headings (string bound noerror)
  4934. "Search and make sure that any matches are in headlines."
  4935. (catch 'return
  4936. (while (if isearch-forward
  4937. (search-forward string bound noerror)
  4938. (search-backward string bound noerror))
  4939. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4940. (and (member :headline context)
  4941. (not (member :tags context))))
  4942. (throw 'return (point))))))
  4943. (defun org-goto-local-auto-isearch ()
  4944. "Start isearch."
  4945. (interactive)
  4946. (goto-char (point-min))
  4947. (let ((keys (this-command-keys)))
  4948. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4949. (isearch-mode t)
  4950. (isearch-process-search-char (string-to-char keys)))))
  4951. (defun org-goto-ret (&optional arg)
  4952. "Finish `org-goto' by going to the new location."
  4953. (interactive "P")
  4954. (setq org-goto-selected-point (point)
  4955. org-goto-exit-command 'return)
  4956. (throw 'exit nil))
  4957. (defun org-goto-left ()
  4958. "Finish `org-goto' by going to the new location."
  4959. (interactive)
  4960. (if (org-on-heading-p)
  4961. (progn
  4962. (beginning-of-line 1)
  4963. (setq org-goto-selected-point (point)
  4964. org-goto-exit-command 'left)
  4965. (throw 'exit nil))
  4966. (error "Not on a heading")))
  4967. (defun org-goto-right ()
  4968. "Finish `org-goto' by going to the new location."
  4969. (interactive)
  4970. (if (org-on-heading-p)
  4971. (progn
  4972. (setq org-goto-selected-point (point)
  4973. org-goto-exit-command 'right)
  4974. (throw 'exit nil))
  4975. (error "Not on a heading")))
  4976. (defun org-goto-quit ()
  4977. "Finish `org-goto' without cursor motion."
  4978. (interactive)
  4979. (setq org-goto-selected-point nil)
  4980. (setq org-goto-exit-command 'quit)
  4981. (throw 'exit nil))
  4982. ;;; Indirect buffer display of subtrees
  4983. (defvar org-indirect-dedicated-frame nil
  4984. "This is the frame being used for indirect tree display.")
  4985. (defvar org-last-indirect-buffer nil)
  4986. (defun org-tree-to-indirect-buffer (&optional arg)
  4987. "Create indirect buffer and narrow it to current subtree.
  4988. With numerical prefix ARG, go up to this level and then take that tree.
  4989. If ARG is negative, go up that many levels.
  4990. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4991. indirect buffer previously made with this command, to avoid proliferation of
  4992. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4993. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4994. is kept so that you can work with several indirect buffers at the same time.
  4995. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4996. requests that a new frame be made for the new buffer, so that the dedicated
  4997. frame is not changed."
  4998. (interactive "P")
  4999. (let ((cbuf (current-buffer))
  5000. (cwin (selected-window))
  5001. (pos (point))
  5002. beg end level heading ibuf)
  5003. (save-excursion
  5004. (org-back-to-heading t)
  5005. (when (numberp arg)
  5006. (setq level (org-outline-level))
  5007. (if (< arg 0) (setq arg (+ level arg)))
  5008. (while (> (setq level (org-outline-level)) arg)
  5009. (outline-up-heading 1 t)))
  5010. (setq beg (point)
  5011. heading (org-get-heading))
  5012. (org-end-of-subtree t) (setq end (point)))
  5013. (if (and (buffer-live-p org-last-indirect-buffer)
  5014. (not (eq org-indirect-buffer-display 'new-frame))
  5015. (not arg))
  5016. (kill-buffer org-last-indirect-buffer))
  5017. (setq ibuf (org-get-indirect-buffer cbuf)
  5018. org-last-indirect-buffer ibuf)
  5019. (cond
  5020. ((or (eq org-indirect-buffer-display 'new-frame)
  5021. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5022. (select-frame (make-frame))
  5023. (delete-other-windows)
  5024. (switch-to-buffer ibuf)
  5025. (org-set-frame-title heading))
  5026. ((eq org-indirect-buffer-display 'dedicated-frame)
  5027. (raise-frame
  5028. (select-frame (or (and org-indirect-dedicated-frame
  5029. (frame-live-p org-indirect-dedicated-frame)
  5030. org-indirect-dedicated-frame)
  5031. (setq org-indirect-dedicated-frame (make-frame)))))
  5032. (delete-other-windows)
  5033. (switch-to-buffer ibuf)
  5034. (org-set-frame-title (concat "Indirect: " heading)))
  5035. ((eq org-indirect-buffer-display 'current-window)
  5036. (switch-to-buffer ibuf))
  5037. ((eq org-indirect-buffer-display 'other-window)
  5038. (pop-to-buffer ibuf))
  5039. (t (error "Invalid value.")))
  5040. (if (featurep 'xemacs)
  5041. (save-excursion (org-mode) (turn-on-font-lock)))
  5042. (narrow-to-region beg end)
  5043. (show-all)
  5044. (goto-char pos)
  5045. (and (window-live-p cwin) (select-window cwin))))
  5046. (defun org-get-indirect-buffer (&optional buffer)
  5047. (setq buffer (or buffer (current-buffer)))
  5048. (let ((n 1) (base (buffer-name buffer)) bname)
  5049. (while (buffer-live-p
  5050. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5051. (setq n (1+ n)))
  5052. (condition-case nil
  5053. (make-indirect-buffer buffer bname 'clone)
  5054. (error (make-indirect-buffer buffer bname)))))
  5055. (defun org-set-frame-title (title)
  5056. "Set the title of the current frame to the string TITLE."
  5057. ;; FIXME: how to name a single frame in XEmacs???
  5058. (unless (featurep 'xemacs)
  5059. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5060. ;;;; Structure editing
  5061. ;;; Inserting headlines
  5062. (defun org-previous-line-empty-p ()
  5063. (save-excursion
  5064. (and (not (bobp))
  5065. (or (beginning-of-line 0) t)
  5066. (save-match-data
  5067. (looking-at "[ \t]*$")))))
  5068. (defun org-insert-heading (&optional force-heading)
  5069. "Insert a new heading or item with same depth at point.
  5070. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5071. If point is at the beginning of a headline, insert a sibling before the
  5072. current headline. If point is not at the beginning, do not split the line,
  5073. but create the new headline after the current line."
  5074. (interactive "P")
  5075. (if (= (buffer-size) 0)
  5076. (insert "\n* ")
  5077. (when (or force-heading (not (org-insert-item)))
  5078. (let* ((empty-line-p nil)
  5079. (head (save-excursion
  5080. (condition-case nil
  5081. (progn
  5082. (org-back-to-heading)
  5083. (setq empty-line-p (org-previous-line-empty-p))
  5084. (match-string 0))
  5085. (error "*"))))
  5086. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5087. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5088. pos hide-previous previous-pos)
  5089. (cond
  5090. ((and (org-on-heading-p) (bolp)
  5091. (or (bobp)
  5092. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5093. ;; insert before the current line
  5094. (open-line (if blank 2 1)))
  5095. ((and (bolp)
  5096. (not org-insert-heading-respect-content)
  5097. (or (bobp)
  5098. (save-excursion
  5099. (backward-char 1) (not (org-invisible-p)))))
  5100. ;; insert right here
  5101. nil)
  5102. (t
  5103. ;; somewhere in the line
  5104. (save-excursion
  5105. (setq previous-pos (point-at-bol))
  5106. (end-of-line)
  5107. (setq hide-previous (org-invisible-p)))
  5108. (and org-insert-heading-respect-content (org-show-subtree))
  5109. (let ((split
  5110. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5111. (save-excursion
  5112. (let ((p (point)))
  5113. (goto-char (point-at-bol))
  5114. (and (looking-at org-complex-heading-regexp)
  5115. (> p (match-beginning 4)))))))
  5116. tags pos)
  5117. (cond
  5118. (org-insert-heading-respect-content
  5119. (org-end-of-subtree nil t)
  5120. (or (bolp) (newline))
  5121. (or (org-previous-line-empty-p)
  5122. (and blank (newline)))
  5123. (open-line 1))
  5124. ((org-on-heading-p)
  5125. (when hide-previous
  5126. (show-children)
  5127. (org-show-entry))
  5128. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5129. (setq tags (and (match-end 2) (match-string 2)))
  5130. (and (match-end 1)
  5131. (delete-region (match-beginning 1) (match-end 1)))
  5132. (setq pos (point-at-bol))
  5133. (or split (end-of-line 1))
  5134. (delete-horizontal-space)
  5135. (newline (if blank 2 1))
  5136. (when tags
  5137. (save-excursion
  5138. (goto-char pos)
  5139. (end-of-line 1)
  5140. (insert " " tags)
  5141. (org-set-tags nil 'align))))
  5142. (t
  5143. (or split (end-of-line 1))
  5144. (newline (if blank 2 1)))))))
  5145. (insert head) (just-one-space)
  5146. (setq pos (point))
  5147. (end-of-line 1)
  5148. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5149. (when (and org-insert-heading-respect-content hide-previous)
  5150. (save-excursion
  5151. (goto-char previous-pos)
  5152. (hide-subtree)))
  5153. (run-hooks 'org-insert-heading-hook)))))
  5154. (defun org-get-heading (&optional no-tags)
  5155. "Return the heading of the current entry, without the stars."
  5156. (save-excursion
  5157. (org-back-to-heading t)
  5158. (if (looking-at
  5159. (if no-tags
  5160. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5161. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5162. (match-string 1) "")))
  5163. (defun org-heading-components ()
  5164. "Return the components of the current heading.
  5165. This is a list with the following elements:
  5166. - the level as an integer
  5167. - the reduced level, different if `org-odd-levels-only' is set.
  5168. - the TODO keyword, or nil
  5169. - the priority character, like ?A, or nil if no priority is given
  5170. - the headline text itself, or the tags string if no headline text
  5171. - the tags string, or nil."
  5172. (save-excursion
  5173. (org-back-to-heading t)
  5174. (if (looking-at org-complex-heading-regexp)
  5175. (list (length (match-string 1))
  5176. (org-reduced-level (length (match-string 1)))
  5177. (org-match-string-no-properties 2)
  5178. (and (match-end 3) (aref (match-string 3) 2))
  5179. (org-match-string-no-properties 4)
  5180. (org-match-string-no-properties 5)))))
  5181. (defun org-get-entry ()
  5182. "Get the entry text, after heading, entire subtree."
  5183. (save-excursion
  5184. (org-back-to-heading t)
  5185. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5186. (defun org-insert-heading-after-current ()
  5187. "Insert a new heading with same level as current, after current subtree."
  5188. (interactive)
  5189. (org-back-to-heading)
  5190. (org-insert-heading)
  5191. (org-move-subtree-down)
  5192. (end-of-line 1))
  5193. (defun org-insert-heading-respect-content ()
  5194. (interactive)
  5195. (let ((org-insert-heading-respect-content t))
  5196. (org-insert-heading t)))
  5197. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5198. (interactive "P")
  5199. (let ((org-insert-heading-respect-content t))
  5200. (org-insert-todo-heading force-state t)))
  5201. (defun org-insert-todo-heading (arg &optional force-heading)
  5202. "Insert a new heading with the same level and TODO state as current heading.
  5203. If the heading has no TODO state, or if the state is DONE, use the first
  5204. state (TODO by default). Also with prefix arg, force first state."
  5205. (interactive "P")
  5206. (when (or force-heading (not (org-insert-item 'checkbox)))
  5207. (org-insert-heading force-heading)
  5208. (save-excursion
  5209. (org-back-to-heading)
  5210. (outline-previous-heading)
  5211. (looking-at org-todo-line-regexp))
  5212. (let*
  5213. ((new-mark-x
  5214. (if (or arg
  5215. (not (match-beginning 2))
  5216. (member (match-string 2) org-done-keywords))
  5217. (car org-todo-keywords-1)
  5218. (match-string 2)))
  5219. (new-mark
  5220. (or
  5221. (run-hook-with-args-until-success
  5222. 'org-todo-get-default-hook new-mark-x nil)
  5223. new-mark-x)))
  5224. (beginning-of-line 1)
  5225. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5226. (if org-treat-insert-todo-heading-as-state-change
  5227. (org-todo new-mark)
  5228. (insert new-mark " "))))
  5229. (when org-provide-todo-statistics
  5230. (org-update-parent-todo-statistics))))
  5231. (defun org-insert-subheading (arg)
  5232. "Insert a new subheading and demote it.
  5233. Works for outline headings and for plain lists alike."
  5234. (interactive "P")
  5235. (org-insert-heading arg)
  5236. (cond
  5237. ((org-on-heading-p) (org-do-demote))
  5238. ((org-at-item-p) (org-indent-item 1))))
  5239. (defun org-insert-todo-subheading (arg)
  5240. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5241. Works for outline headings and for plain lists alike."
  5242. (interactive "P")
  5243. (org-insert-todo-heading arg)
  5244. (cond
  5245. ((org-on-heading-p) (org-do-demote))
  5246. ((org-at-item-p) (org-indent-item 1))))
  5247. ;;; Promotion and Demotion
  5248. (defvar org-after-demote-entry-hook nil
  5249. "Hook run after an entry has been demoted.
  5250. The cursor will be at the beginning of the entry.
  5251. When a subtree is being demoted, the hook will be called for each node.")
  5252. (defvar org-after-promote-entry-hook nil
  5253. "Hook run after an entry has been promoted.
  5254. The cursor will be at the beginning of the entry.
  5255. When a subtree is being promoted, the hook will be called for each node.")
  5256. (defun org-promote-subtree ()
  5257. "Promote the entire subtree.
  5258. See also `org-promote'."
  5259. (interactive)
  5260. (save-excursion
  5261. (org-map-tree 'org-promote))
  5262. (org-fix-position-after-promote))
  5263. (defun org-demote-subtree ()
  5264. "Demote the entire subtree. See `org-demote'.
  5265. See also `org-promote'."
  5266. (interactive)
  5267. (save-excursion
  5268. (org-map-tree 'org-demote))
  5269. (org-fix-position-after-promote))
  5270. (defun org-do-promote ()
  5271. "Promote the current heading higher up the tree.
  5272. If the region is active in `transient-mark-mode', promote all headings
  5273. in the region."
  5274. (interactive)
  5275. (save-excursion
  5276. (if (org-region-active-p)
  5277. (org-map-region 'org-promote (region-beginning) (region-end))
  5278. (org-promote)))
  5279. (org-fix-position-after-promote))
  5280. (defun org-do-demote ()
  5281. "Demote the current heading lower down the tree.
  5282. If the region is active in `transient-mark-mode', demote all headings
  5283. in the region."
  5284. (interactive)
  5285. (save-excursion
  5286. (if (org-region-active-p)
  5287. (org-map-region 'org-demote (region-beginning) (region-end))
  5288. (org-demote)))
  5289. (org-fix-position-after-promote))
  5290. (defun org-fix-position-after-promote ()
  5291. "Make sure that after pro/demotion cursor position is right."
  5292. (let ((pos (point)))
  5293. (when (save-excursion
  5294. (beginning-of-line 1)
  5295. (looking-at org-todo-line-regexp)
  5296. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5297. (cond ((eobp) (insert " "))
  5298. ((eolp) (insert " "))
  5299. ((equal (char-after) ?\ ) (forward-char 1))))))
  5300. (defun org-reduced-level (l)
  5301. "Compute the effective level of a heading.
  5302. This takes into account the setting of `org-odd-levels-only'."
  5303. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5304. (defun org-get-valid-level (level &optional change)
  5305. "Rectify a level change under the influence of `org-odd-levels-only'
  5306. LEVEL is a current level, CHANGE is by how much the level should be
  5307. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5308. even level numbers will become the next higher odd number."
  5309. (if org-odd-levels-only
  5310. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5311. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5312. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5313. (max 1 (+ level (or change 0)))))
  5314. (if (boundp 'define-obsolete-function-alias)
  5315. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5316. (define-obsolete-function-alias 'org-get-legal-level
  5317. 'org-get-valid-level)
  5318. (define-obsolete-function-alias 'org-get-legal-level
  5319. 'org-get-valid-level "23.1")))
  5320. (defun org-promote ()
  5321. "Promote the current heading higher up the tree.
  5322. If the region is active in `transient-mark-mode', promote all headings
  5323. in the region."
  5324. (org-back-to-heading t)
  5325. (let* ((level (save-match-data (funcall outline-level)))
  5326. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5327. (diff (abs (- level (length up-head) -1))))
  5328. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5329. (replace-match up-head nil t)
  5330. ;; Fixup tag positioning
  5331. (and org-auto-align-tags (org-set-tags nil t))
  5332. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5333. (run-hooks 'org-after-promote-entry-hook)))
  5334. (defun org-demote ()
  5335. "Demote the current heading lower down the tree.
  5336. If the region is active in `transient-mark-mode', demote all headings
  5337. in the region."
  5338. (org-back-to-heading t)
  5339. (let* ((level (save-match-data (funcall outline-level)))
  5340. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5341. (diff (abs (- level (length down-head) -1))))
  5342. (replace-match down-head nil t)
  5343. ;; Fixup tag positioning
  5344. (and org-auto-align-tags (org-set-tags nil t))
  5345. (if org-adapt-indentation (org-fixup-indentation diff))
  5346. (run-hooks 'org-after-demote-entry-hook)))
  5347. (defun org-map-tree (fun)
  5348. "Call FUN for every heading underneath the current one."
  5349. (org-back-to-heading)
  5350. (let ((level (funcall outline-level)))
  5351. (save-excursion
  5352. (funcall fun)
  5353. (while (and (progn
  5354. (outline-next-heading)
  5355. (> (funcall outline-level) level))
  5356. (not (eobp)))
  5357. (funcall fun)))))
  5358. (defun org-map-region (fun beg end)
  5359. "Call FUN for every heading between BEG and END."
  5360. (let ((org-ignore-region t))
  5361. (save-excursion
  5362. (setq end (copy-marker end))
  5363. (goto-char beg)
  5364. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5365. (< (point) end))
  5366. (funcall fun))
  5367. (while (and (progn
  5368. (outline-next-heading)
  5369. (< (point) end))
  5370. (not (eobp)))
  5371. (funcall fun)))))
  5372. (defun org-fixup-indentation (diff)
  5373. "Change the indentation in the current entry by DIFF
  5374. However, if any line in the current entry has no indentation, or if it
  5375. would end up with no indentation after the change, nothing at all is done."
  5376. (save-excursion
  5377. (let ((end (save-excursion (outline-next-heading)
  5378. (point-marker)))
  5379. (prohibit (if (> diff 0)
  5380. "^\\S-"
  5381. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5382. col)
  5383. (unless (save-excursion (end-of-line 1)
  5384. (re-search-forward prohibit end t))
  5385. (while (and (< (point) end)
  5386. (re-search-forward "^[ \t]+" end t))
  5387. (goto-char (match-end 0))
  5388. (setq col (current-column))
  5389. (if (< diff 0) (replace-match ""))
  5390. (org-indent-to-column (+ diff col))))
  5391. (move-marker end nil))))
  5392. (defun org-convert-to-odd-levels ()
  5393. "Convert an org-mode file with all levels allowed to one with odd levels.
  5394. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5395. level 5 etc."
  5396. (interactive)
  5397. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5398. (let ((org-odd-levels-only nil) n)
  5399. (save-excursion
  5400. (goto-char (point-min))
  5401. (while (re-search-forward "^\\*\\*+ " nil t)
  5402. (setq n (- (length (match-string 0)) 2))
  5403. (while (>= (setq n (1- n)) 0)
  5404. (org-demote))
  5405. (end-of-line 1))))))
  5406. (defun org-convert-to-oddeven-levels ()
  5407. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5408. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5409. section with an even level, conversion would destroy the structure of the file. An error
  5410. is signaled in this case."
  5411. (interactive)
  5412. (goto-char (point-min))
  5413. ;; First check if there are no even levels
  5414. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5415. (org-show-context t)
  5416. (error "Not all levels are odd in this file. Conversion not possible."))
  5417. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5418. (let ((org-odd-levels-only nil) n)
  5419. (save-excursion
  5420. (goto-char (point-min))
  5421. (while (re-search-forward "^\\*\\*+ " nil t)
  5422. (setq n (/ (1- (length (match-string 0))) 2))
  5423. (while (>= (setq n (1- n)) 0)
  5424. (org-promote))
  5425. (end-of-line 1))))))
  5426. (defun org-tr-level (n)
  5427. "Make N odd if required."
  5428. (if org-odd-levels-only (1+ (/ n 2)) n))
  5429. ;;; Vertical tree motion, cutting and pasting of subtrees
  5430. (defun org-move-subtree-up (&optional arg)
  5431. "Move the current subtree up past ARG headlines of the same level."
  5432. (interactive "p")
  5433. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5434. (defun org-move-subtree-down (&optional arg)
  5435. "Move the current subtree down past ARG headlines of the same level."
  5436. (interactive "p")
  5437. (setq arg (prefix-numeric-value arg))
  5438. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5439. 'outline-get-last-sibling))
  5440. (ins-point (make-marker))
  5441. (cnt (abs arg))
  5442. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5443. ;; Select the tree
  5444. (org-back-to-heading)
  5445. (setq beg0 (point))
  5446. (save-excursion
  5447. (setq ne-beg (org-back-over-empty-lines))
  5448. (setq beg (point)))
  5449. (save-match-data
  5450. (save-excursion (outline-end-of-heading)
  5451. (setq folded (org-invisible-p)))
  5452. (outline-end-of-subtree))
  5453. (outline-next-heading)
  5454. (setq ne-end (org-back-over-empty-lines))
  5455. (setq end (point))
  5456. (goto-char beg0)
  5457. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5458. ;; include less whitespace
  5459. (save-excursion
  5460. (goto-char beg)
  5461. (forward-line (- ne-beg ne-end))
  5462. (setq beg (point))))
  5463. ;; Find insertion point, with error handling
  5464. (while (> cnt 0)
  5465. (or (and (funcall movfunc) (looking-at outline-regexp))
  5466. (progn (goto-char beg0)
  5467. (error "Cannot move past superior level or buffer limit")))
  5468. (setq cnt (1- cnt)))
  5469. (if (> arg 0)
  5470. ;; Moving forward - still need to move over subtree
  5471. (progn (org-end-of-subtree t t)
  5472. (save-excursion
  5473. (org-back-over-empty-lines)
  5474. (or (bolp) (newline)))))
  5475. (setq ne-ins (org-back-over-empty-lines))
  5476. (move-marker ins-point (point))
  5477. (setq txt (buffer-substring beg end))
  5478. (org-save-markers-in-region beg end)
  5479. (delete-region beg end)
  5480. (org-remove-empty-overlays-at beg)
  5481. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5482. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5483. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5484. (let ((bbb (point)))
  5485. (insert-before-markers txt)
  5486. (org-reinstall-markers-in-region bbb)
  5487. (move-marker ins-point bbb))
  5488. (or (bolp) (insert "\n"))
  5489. (setq ins-end (point))
  5490. (goto-char ins-point)
  5491. (org-skip-whitespace)
  5492. (when (and (< arg 0)
  5493. (org-first-sibling-p)
  5494. (> ne-ins ne-beg))
  5495. ;; Move whitespace back to beginning
  5496. (save-excursion
  5497. (goto-char ins-end)
  5498. (let ((kill-whole-line t))
  5499. (kill-line (- ne-ins ne-beg)) (point)))
  5500. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5501. (move-marker ins-point nil)
  5502. (if folded
  5503. (hide-subtree)
  5504. (org-show-entry)
  5505. (show-children)
  5506. (org-cycle-hide-drawers 'children))
  5507. (org-clean-visibility-after-subtree-move)))
  5508. (defvar org-subtree-clip ""
  5509. "Clipboard for cut and paste of subtrees.
  5510. This is actually only a copy of the kill, because we use the normal kill
  5511. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5512. (defvar org-subtree-clip-folded nil
  5513. "Was the last copied subtree folded?
  5514. This is used to fold the tree back after pasting.")
  5515. (defun org-cut-subtree (&optional n)
  5516. "Cut the current subtree into the clipboard.
  5517. With prefix arg N, cut this many sequential subtrees.
  5518. This is a short-hand for marking the subtree and then cutting it."
  5519. (interactive "p")
  5520. (org-copy-subtree n 'cut))
  5521. (defun org-copy-subtree (&optional n cut force-store-markers)
  5522. "Cut the current subtree into the clipboard.
  5523. With prefix arg N, cut this many sequential subtrees.
  5524. This is a short-hand for marking the subtree and then copying it.
  5525. If CUT is non-nil, actually cut the subtree.
  5526. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5527. of some markers in the region, even if CUT is non-nil. This is
  5528. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5529. (interactive "p")
  5530. (let (beg end folded (beg0 (point)))
  5531. (if (interactive-p)
  5532. (org-back-to-heading nil) ; take what looks like a subtree
  5533. (org-back-to-heading t)) ; take what is really there
  5534. (org-back-over-empty-lines)
  5535. (setq beg (point))
  5536. (skip-chars-forward " \t\r\n")
  5537. (save-match-data
  5538. (save-excursion (outline-end-of-heading)
  5539. (setq folded (org-invisible-p)))
  5540. (condition-case nil
  5541. (org-forward-same-level (1- n) t)
  5542. (error nil))
  5543. (org-end-of-subtree t t))
  5544. (org-back-over-empty-lines)
  5545. (setq end (point))
  5546. (goto-char beg0)
  5547. (when (> end beg)
  5548. (setq org-subtree-clip-folded folded)
  5549. (when (or cut force-store-markers)
  5550. (org-save-markers-in-region beg end))
  5551. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5552. (setq org-subtree-clip (current-kill 0))
  5553. (message "%s: Subtree(s) with %d characters"
  5554. (if cut "Cut" "Copied")
  5555. (length org-subtree-clip)))))
  5556. (defun org-paste-subtree (&optional level tree for-yank)
  5557. "Paste the clipboard as a subtree, with modification of headline level.
  5558. The entire subtree is promoted or demoted in order to match a new headline
  5559. level.
  5560. If the cursor is at the beginning of a headline, the same level as
  5561. that headline is used to paste the tree
  5562. If not, the new level is derived from the *visible* headings
  5563. before and after the insertion point, and taken to be the inferior headline
  5564. level of the two. So if the previous visible heading is level 3 and the
  5565. next is level 4 (or vice versa), level 4 will be used for insertion.
  5566. This makes sure that the subtree remains an independent subtree and does
  5567. not swallow low level entries.
  5568. You can also force a different level, either by using a numeric prefix
  5569. argument, or by inserting the heading marker by hand. For example, if the
  5570. cursor is after \"*****\", then the tree will be shifted to level 5.
  5571. If optional TREE is given, use this text instead of the kill ring.
  5572. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5573. move back over whitespace before inserting, and move point to the end of
  5574. the inserted text when done."
  5575. (interactive "P")
  5576. (setq tree (or tree (and kill-ring (current-kill 0))))
  5577. (unless (org-kill-is-subtree-p tree)
  5578. (error "%s"
  5579. (substitute-command-keys
  5580. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5581. (let* ((visp (not (org-invisible-p)))
  5582. (txt tree)
  5583. (^re (concat "^\\(" outline-regexp "\\)"))
  5584. (re (concat "\\(" outline-regexp "\\)"))
  5585. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5586. (old-level (if (string-match ^re txt)
  5587. (- (match-end 0) (match-beginning 0) 1)
  5588. -1))
  5589. (force-level (cond (level (prefix-numeric-value level))
  5590. ((and (looking-at "[ \t]*$")
  5591. (string-match
  5592. ^re_ (buffer-substring
  5593. (point-at-bol) (point))))
  5594. (- (match-end 1) (match-beginning 1)))
  5595. ((and (bolp)
  5596. (looking-at org-outline-regexp))
  5597. (- (match-end 0) (point) 1))
  5598. (t nil)))
  5599. (previous-level (save-excursion
  5600. (condition-case nil
  5601. (progn
  5602. (outline-previous-visible-heading 1)
  5603. (if (looking-at re)
  5604. (- (match-end 0) (match-beginning 0) 1)
  5605. 1))
  5606. (error 1))))
  5607. (next-level (save-excursion
  5608. (condition-case nil
  5609. (progn
  5610. (or (looking-at outline-regexp)
  5611. (outline-next-visible-heading 1))
  5612. (if (looking-at re)
  5613. (- (match-end 0) (match-beginning 0) 1)
  5614. 1))
  5615. (error 1))))
  5616. (new-level (or force-level (max previous-level next-level)))
  5617. (shift (if (or (= old-level -1)
  5618. (= new-level -1)
  5619. (= old-level new-level))
  5620. 0
  5621. (- new-level old-level)))
  5622. (delta (if (> shift 0) -1 1))
  5623. (func (if (> shift 0) 'org-demote 'org-promote))
  5624. (org-odd-levels-only nil)
  5625. beg end newend)
  5626. ;; Remove the forced level indicator
  5627. (if force-level
  5628. (delete-region (point-at-bol) (point)))
  5629. ;; Paste
  5630. (beginning-of-line 1)
  5631. (unless for-yank (org-back-over-empty-lines))
  5632. (setq beg (point))
  5633. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5634. (insert-before-markers txt)
  5635. (unless (string-match "\n\\'" txt) (insert "\n"))
  5636. (setq newend (point))
  5637. (org-reinstall-markers-in-region beg)
  5638. (setq end (point))
  5639. (goto-char beg)
  5640. (skip-chars-forward " \t\n\r")
  5641. (setq beg (point))
  5642. (if (and (org-invisible-p) visp)
  5643. (save-excursion (outline-show-heading)))
  5644. ;; Shift if necessary
  5645. (unless (= shift 0)
  5646. (save-restriction
  5647. (narrow-to-region beg end)
  5648. (while (not (= shift 0))
  5649. (org-map-region func (point-min) (point-max))
  5650. (setq shift (+ delta shift)))
  5651. (goto-char (point-min))
  5652. (setq newend (point-max))))
  5653. (when (or (interactive-p) for-yank)
  5654. (message "Clipboard pasted as level %d subtree" new-level))
  5655. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5656. kill-ring
  5657. (eq org-subtree-clip (current-kill 0))
  5658. org-subtree-clip-folded)
  5659. ;; The tree was folded before it was killed/copied
  5660. (hide-subtree))
  5661. (and for-yank (goto-char newend))))
  5662. (defun org-kill-is-subtree-p (&optional txt)
  5663. "Check if the current kill is an outline subtree, or a set of trees.
  5664. Returns nil if kill does not start with a headline, or if the first
  5665. headline level is not the largest headline level in the tree.
  5666. So this will actually accept several entries of equal levels as well,
  5667. which is OK for `org-paste-subtree'.
  5668. If optional TXT is given, check this string instead of the current kill."
  5669. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5670. (start-level (and kill
  5671. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5672. org-outline-regexp "\\)")
  5673. kill)
  5674. (- (match-end 2) (match-beginning 2) 1)))
  5675. (re (concat "^" org-outline-regexp))
  5676. (start (1+ (or (match-beginning 2) -1))))
  5677. (if (not start-level)
  5678. (progn
  5679. nil) ;; does not even start with a heading
  5680. (catch 'exit
  5681. (while (setq start (string-match re kill (1+ start)))
  5682. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5683. (throw 'exit nil)))
  5684. t))))
  5685. (defvar org-markers-to-move nil
  5686. "Markers that should be moved with a cut-and-paste operation.
  5687. Those markers are stored together with their positions relative to
  5688. the start of the region.")
  5689. (defun org-save-markers-in-region (beg end)
  5690. "Check markers in region.
  5691. If these markers are between BEG and END, record their position relative
  5692. to BEG, so that after moving the block of text, we can put the markers back
  5693. into place.
  5694. This function gets called just before an entry or tree gets cut from the
  5695. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5696. called immediately, to move the markers with the entries."
  5697. (setq org-markers-to-move nil)
  5698. (when (featurep 'org-clock)
  5699. (org-clock-save-markers-for-cut-and-paste beg end))
  5700. (when (featurep 'org-agenda)
  5701. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5702. (defun org-check-and-save-marker (marker beg end)
  5703. "Check if MARKER is between BEG and END.
  5704. If yes, remember the marker and the distance to BEG."
  5705. (when (and (marker-buffer marker)
  5706. (equal (marker-buffer marker) (current-buffer)))
  5707. (if (and (>= marker beg) (< marker end))
  5708. (push (cons marker (- marker beg)) org-markers-to-move))))
  5709. (defun org-reinstall-markers-in-region (beg)
  5710. "Move all remembered markers to their position relative to BEG."
  5711. (mapc (lambda (x)
  5712. (move-marker (car x) (+ beg (cdr x))))
  5713. org-markers-to-move)
  5714. (setq org-markers-to-move nil))
  5715. (defun org-narrow-to-subtree ()
  5716. "Narrow buffer to the current subtree."
  5717. (interactive)
  5718. (save-excursion
  5719. (save-match-data
  5720. (narrow-to-region
  5721. (progn (org-back-to-heading t) (point))
  5722. (progn (org-end-of-subtree t) (point))))))
  5723. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5724. "Clone the task (subtree) at point N times.
  5725. The clones will be inserted as siblings.
  5726. In interactive use, the user will be prompted for the number of clones
  5727. to be produced, and for a time SHIFT, which may be a repeater as used
  5728. in time stamps, for example `+3d'.
  5729. When a valid repeater is given and the entry contains any time stamps,
  5730. the clones will become a sequence in time, with time stamps in the
  5731. subtree shifted for each clone produced. If SHIFT is nil or the
  5732. empty string, time stamps will be left alone.
  5733. If the original subtree did contain time stamps with a repeater,
  5734. the following will happen:
  5735. - the repeater will be removed in each clone
  5736. - an additional clone will be produced, with the current, unshifted
  5737. date(s) in the entry.
  5738. - the original entry will be placed *after* all the clones, with
  5739. repeater intact.
  5740. - the start days in the repeater in the original entry will be shifted
  5741. to past the last clone.
  5742. I this way you can spell out a number of instances of a repeating task,
  5743. and still retain the repeater to cover future instances of the task."
  5744. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5745. (let (beg end template task
  5746. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5747. (if (not (and (integerp n) (> n 0)))
  5748. (error "Invalid number of replications %s" n))
  5749. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5750. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5751. shift)))
  5752. (error "Invalid shift specification %s" shift))
  5753. (when doshift
  5754. (setq shift-n (string-to-number (match-string 1 shift))
  5755. shift-what (cdr (assoc (match-string 2 shift)
  5756. '(("d" . day) ("w" . week)
  5757. ("m" . month) ("y" . year))))))
  5758. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5759. (setq nmin 1 nmax n)
  5760. (org-back-to-heading t)
  5761. (setq beg (point))
  5762. (org-end-of-subtree t t)
  5763. (setq end (point))
  5764. (setq template (buffer-substring beg end))
  5765. (when (and doshift
  5766. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5767. (delete-region beg end)
  5768. (setq end beg)
  5769. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5770. (goto-char end)
  5771. (loop for n from nmin to nmax do
  5772. (if (not doshift)
  5773. (setq task template)
  5774. (with-temp-buffer
  5775. (insert template)
  5776. (org-mode)
  5777. (goto-char (point-min))
  5778. (while (re-search-forward org-ts-regexp-both nil t)
  5779. (org-timestamp-change (* n shift-n) shift-what))
  5780. (unless (= n n-no-remove)
  5781. (goto-char (point-min))
  5782. (while (re-search-forward org-ts-regexp nil t)
  5783. (save-excursion
  5784. (goto-char (match-beginning 0))
  5785. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5786. (delete-region (match-beginning 1) (match-end 1))))))
  5787. (setq task (buffer-string))))
  5788. (insert task))
  5789. (goto-char beg)))
  5790. ;;; Outline Sorting
  5791. (defun org-sort (with-case)
  5792. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5793. Optional argument WITH-CASE means sort case-sensitively.
  5794. With a double prefix argument, also remove duplicate entries."
  5795. (interactive "P")
  5796. (if (org-at-table-p)
  5797. (org-call-with-arg 'org-table-sort-lines with-case)
  5798. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5799. (defun org-sort-remove-invisible (s)
  5800. (remove-text-properties 0 (length s) org-rm-props s)
  5801. (while (string-match org-bracket-link-regexp s)
  5802. (setq s (replace-match (if (match-end 2)
  5803. (match-string 3 s)
  5804. (match-string 1 s)) t t s)))
  5805. s)
  5806. (defvar org-priority-regexp) ; defined later in the file
  5807. (defvar org-after-sorting-entries-or-items-hook nil
  5808. "Hook that is run after a bunch of entries or items have been sorted.
  5809. When children are sorted, the cursor is in the parent line when this
  5810. hook gets called. When a region or a plain list is sorted, the cursor
  5811. will be in the first entry of the sorted region/list.")
  5812. (defun org-sort-entries-or-items
  5813. (&optional with-case sorting-type getkey-func compare-func property)
  5814. "Sort entries on a certain level of an outline tree, or plain list items.
  5815. If there is an active region, the entries in the region are sorted.
  5816. Else, if the cursor is before the first entry, sort the top-level items.
  5817. Else, the children of the entry at point are sorted.
  5818. If the cursor is at the first item in a plain list, the list items will be
  5819. sorted.
  5820. Sorting can be alphabetically, numerically, by date/time as given by
  5821. a time stamp, by a property or by priority.
  5822. The command prompts for the sorting type unless it has been given to the
  5823. function through the SORTING-TYPE argument, which needs to a character,
  5824. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5825. precise meaning of each character:
  5826. n Numerically, by converting the beginning of the entry/item to a number.
  5827. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5828. t By date/time, either the first active time stamp in the entry, or, if
  5829. none exist, by the first inactive one.
  5830. In items, only the first line will be chekced.
  5831. s By the scheduled date/time.
  5832. d By deadline date/time.
  5833. c By creation time, which is assumed to be the first inactive time stamp
  5834. at the beginning of a line.
  5835. p By priority according to the cookie.
  5836. r By the value of a property.
  5837. Capital letters will reverse the sort order.
  5838. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5839. called with point at the beginning of the record. It must return either
  5840. a string or a number that should serve as the sorting key for that record.
  5841. Comparing entries ignores case by default. However, with an optional argument
  5842. WITH-CASE, the sorting considers case as well."
  5843. (interactive "P")
  5844. (let ((case-func (if with-case 'identity 'downcase))
  5845. start beg end stars re re2
  5846. txt what tmp plain-list-p)
  5847. ;; Find beginning and end of region to sort
  5848. (cond
  5849. ((org-region-active-p)
  5850. ;; we will sort the region
  5851. (setq end (region-end)
  5852. what "region")
  5853. (goto-char (region-beginning))
  5854. (if (not (org-on-heading-p)) (outline-next-heading))
  5855. (setq start (point)))
  5856. ((org-at-item-p)
  5857. ;; we will sort this plain list
  5858. (org-beginning-of-item-list) (setq start (point))
  5859. (org-end-of-item-list) (setq end (point))
  5860. (goto-char start)
  5861. (setq plain-list-p t
  5862. what "plain list"))
  5863. ((or (org-on-heading-p)
  5864. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5865. ;; we will sort the children of the current headline
  5866. (org-back-to-heading)
  5867. (setq start (point)
  5868. end (progn (org-end-of-subtree t t)
  5869. (org-back-over-empty-lines)
  5870. (point))
  5871. what "children")
  5872. (goto-char start)
  5873. (show-subtree)
  5874. (outline-next-heading))
  5875. (t
  5876. ;; we will sort the top-level entries in this file
  5877. (goto-char (point-min))
  5878. (or (org-on-heading-p) (outline-next-heading))
  5879. (setq start (point) end (point-max) what "top-level")
  5880. (goto-char start)
  5881. (show-all)))
  5882. (setq beg (point))
  5883. (if (>= beg end) (error "Nothing to sort"))
  5884. (unless plain-list-p
  5885. (looking-at "\\(\\*+\\)")
  5886. (setq stars (match-string 1)
  5887. re (concat "^" (regexp-quote stars) " +")
  5888. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5889. txt (buffer-substring beg end))
  5890. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5891. (if (and (not (equal stars "*")) (string-match re2 txt))
  5892. (error "Region to sort contains a level above the first entry")))
  5893. (unless sorting-type
  5894. (message
  5895. (if plain-list-p
  5896. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5897. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5898. [t]ime [s]cheduled [d]eadline [c]reated
  5899. A/N/T/S/D/C/P/O/F means reversed:")
  5900. what)
  5901. (setq sorting-type (read-char-exclusive))
  5902. (and (= (downcase sorting-type) ?f)
  5903. (setq getkey-func
  5904. (org-icompleting-read "Sort using function: "
  5905. obarray 'fboundp t nil nil))
  5906. (setq getkey-func (intern getkey-func)))
  5907. (and (= (downcase sorting-type) ?r)
  5908. (setq property
  5909. (org-icompleting-read "Property: "
  5910. (mapcar 'list (org-buffer-property-keys t))
  5911. nil t))))
  5912. (message "Sorting entries...")
  5913. (save-restriction
  5914. (narrow-to-region start end)
  5915. (let ((dcst (downcase sorting-type))
  5916. (case-fold-search nil)
  5917. (now (current-time)))
  5918. (sort-subr
  5919. (/= dcst sorting-type)
  5920. ;; This function moves to the beginning character of the "record" to
  5921. ;; be sorted.
  5922. (if plain-list-p
  5923. (lambda nil
  5924. (if (org-at-item-p) t (goto-char (point-max))))
  5925. (lambda nil
  5926. (if (re-search-forward re nil t)
  5927. (goto-char (match-beginning 0))
  5928. (goto-char (point-max)))))
  5929. ;; This function moves to the last character of the "record" being
  5930. ;; sorted.
  5931. (if plain-list-p
  5932. 'org-end-of-item
  5933. (lambda nil
  5934. (save-match-data
  5935. (condition-case nil
  5936. (outline-forward-same-level 1)
  5937. (error
  5938. (goto-char (point-max)))))))
  5939. ;; This function returns the value that gets sorted against.
  5940. (if plain-list-p
  5941. (lambda nil
  5942. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5943. (cond
  5944. ((= dcst ?n)
  5945. (string-to-number (buffer-substring (match-end 0)
  5946. (point-at-eol))))
  5947. ((= dcst ?a)
  5948. (buffer-substring (match-end 0) (point-at-eol)))
  5949. ((= dcst ?t)
  5950. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5951. (re-search-forward org-ts-regexp-both
  5952. (point-at-eol) t))
  5953. (org-time-string-to-seconds (match-string 0))
  5954. (time-to-seconds now)))
  5955. ((= dcst ?f)
  5956. (if getkey-func
  5957. (progn
  5958. (setq tmp (funcall getkey-func))
  5959. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5960. tmp)
  5961. (error "Invalid key function `%s'" getkey-func)))
  5962. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5963. (lambda nil
  5964. (cond
  5965. ((= dcst ?n)
  5966. (if (looking-at org-complex-heading-regexp)
  5967. (string-to-number (match-string 4))
  5968. nil))
  5969. ((= dcst ?a)
  5970. (if (looking-at org-complex-heading-regexp)
  5971. (funcall case-func (match-string 4))
  5972. nil))
  5973. ((= dcst ?t)
  5974. (let ((end (save-excursion (outline-next-heading) (point))))
  5975. (if (or (re-search-forward org-ts-regexp end t)
  5976. (re-search-forward org-ts-regexp-both end t))
  5977. (org-time-string-to-seconds (match-string 0))
  5978. (time-to-seconds now))))
  5979. ((= dcst ?c)
  5980. (let ((end (save-excursion (outline-next-heading) (point))))
  5981. (if (re-search-forward
  5982. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5983. end t)
  5984. (org-time-string-to-seconds (match-string 0))
  5985. (time-to-seconds now))))
  5986. ((= dcst ?s)
  5987. (let ((end (save-excursion (outline-next-heading) (point))))
  5988. (if (re-search-forward org-scheduled-time-regexp end t)
  5989. (org-time-string-to-seconds (match-string 1))
  5990. (time-to-seconds now))))
  5991. ((= dcst ?d)
  5992. (let ((end (save-excursion (outline-next-heading) (point))))
  5993. (if (re-search-forward org-deadline-time-regexp end t)
  5994. (org-time-string-to-seconds (match-string 1))
  5995. (time-to-seconds now))))
  5996. ((= dcst ?p)
  5997. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5998. (string-to-char (match-string 2))
  5999. org-default-priority))
  6000. ((= dcst ?r)
  6001. (or (org-entry-get nil property) ""))
  6002. ((= dcst ?o)
  6003. (if (looking-at org-complex-heading-regexp)
  6004. (- 9999 (length (member (match-string 2)
  6005. org-todo-keywords-1)))))
  6006. ((= dcst ?f)
  6007. (if getkey-func
  6008. (progn
  6009. (setq tmp (funcall getkey-func))
  6010. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6011. tmp)
  6012. (error "Invalid key function `%s'" getkey-func)))
  6013. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6014. nil
  6015. (cond
  6016. ((= dcst ?a) 'string<)
  6017. ((= dcst ?f) compare-func)
  6018. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6019. (t nil)))))
  6020. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6021. (message "Sorting entries...done")))
  6022. (defun org-do-sort (table what &optional with-case sorting-type)
  6023. "Sort TABLE of WHAT according to SORTING-TYPE.
  6024. The user will be prompted for the SORTING-TYPE if the call to this
  6025. function does not specify it. WHAT is only for the prompt, to indicate
  6026. what is being sorted. The sorting key will be extracted from
  6027. the car of the elements of the table.
  6028. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6029. (unless sorting-type
  6030. (message
  6031. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6032. what)
  6033. (setq sorting-type (read-char-exclusive)))
  6034. (let ((dcst (downcase sorting-type))
  6035. extractfun comparefun)
  6036. ;; Define the appropriate functions
  6037. (cond
  6038. ((= dcst ?n)
  6039. (setq extractfun 'string-to-number
  6040. comparefun (if (= dcst sorting-type) '< '>)))
  6041. ((= dcst ?a)
  6042. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6043. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6044. comparefun (if (= dcst sorting-type)
  6045. 'string<
  6046. (lambda (a b) (and (not (string< a b))
  6047. (not (string= a b)))))))
  6048. ((= dcst ?t)
  6049. (setq extractfun
  6050. (lambda (x)
  6051. (if (or (string-match org-ts-regexp x)
  6052. (string-match org-ts-regexp-both x))
  6053. (time-to-seconds
  6054. (org-time-string-to-time (match-string 0 x)))
  6055. 0))
  6056. comparefun (if (= dcst sorting-type) '< '>)))
  6057. (t (error "Invalid sorting type `%c'" sorting-type)))
  6058. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6059. table)
  6060. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6061. ;;; The orgstruct minor mode
  6062. ;; Define a minor mode which can be used in other modes in order to
  6063. ;; integrate the org-mode structure editing commands.
  6064. ;; This is really a hack, because the org-mode structure commands use
  6065. ;; keys which normally belong to the major mode. Here is how it
  6066. ;; works: The minor mode defines all the keys necessary to operate the
  6067. ;; structure commands, but wraps the commands into a function which
  6068. ;; tests if the cursor is currently at a headline or a plain list
  6069. ;; item. If that is the case, the structure command is used,
  6070. ;; temporarily setting many Org-mode variables like regular
  6071. ;; expressions for filling etc. However, when any of those keys is
  6072. ;; used at a different location, function uses `key-binding' to look
  6073. ;; up if the key has an associated command in another currently active
  6074. ;; keymap (minor modes, major mode, global), and executes that
  6075. ;; command. There might be problems if any of the keys is otherwise
  6076. ;; used as a prefix key.
  6077. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6078. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6079. ;; addresses this by checking explicitly for both bindings.
  6080. (defvar orgstruct-mode-map (make-sparse-keymap)
  6081. "Keymap for the minor `orgstruct-mode'.")
  6082. (defvar org-local-vars nil
  6083. "List of local variables, for use by `orgstruct-mode'")
  6084. ;;;###autoload
  6085. (define-minor-mode orgstruct-mode
  6086. "Toggle the minor more `orgstruct-mode'.
  6087. This mode is for using Org-mode structure commands in other modes.
  6088. The following key behave as if Org-mode was active, if the cursor
  6089. is on a headline, or on a plain list item (both in the definition
  6090. of Org-mode).
  6091. M-up Move entry/item up
  6092. M-down Move entry/item down
  6093. M-left Promote
  6094. M-right Demote
  6095. M-S-up Move entry/item up
  6096. M-S-down Move entry/item down
  6097. M-S-left Promote subtree
  6098. M-S-right Demote subtree
  6099. M-q Fill paragraph and items like in Org-mode
  6100. C-c ^ Sort entries
  6101. C-c - Cycle list bullet
  6102. TAB Cycle item visibility
  6103. M-RET Insert new heading/item
  6104. S-M-RET Insert new TODO heading / Checkbox item
  6105. C-c C-c Set tags / toggle checkbox"
  6106. nil " OrgStruct" nil
  6107. (org-load-modules-maybe)
  6108. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6109. ;;;###autoload
  6110. (defun turn-on-orgstruct ()
  6111. "Unconditionally turn on `orgstruct-mode'."
  6112. (orgstruct-mode 1))
  6113. (defun orgstruct++-mode (&optional arg)
  6114. "Toggle `orgstruct-mode', the enhanced version of it.
  6115. In addition to setting orgstruct-mode, this also exports all indentation
  6116. and autofilling variables from org-mode into the buffer. It will also
  6117. recognize item context in multiline items.
  6118. Note that turning off orgstruct-mode will *not* remove the
  6119. indentation/paragraph settings. This can only be done by refreshing the
  6120. major mode, for example with \\[normal-mode]."
  6121. (interactive "P")
  6122. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6123. (if (< arg 1)
  6124. (orgstruct-mode -1)
  6125. (orgstruct-mode 1)
  6126. (let (var val)
  6127. (mapc
  6128. (lambda (x)
  6129. (when (string-match
  6130. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6131. (symbol-name (car x)))
  6132. (setq var (car x) val (nth 1 x))
  6133. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6134. org-local-vars)
  6135. (org-set-local 'orgstruct-is-++ t))))
  6136. (defvar orgstruct-is-++ nil
  6137. "Is orgstruct-mode in ++ version in the current-buffer?")
  6138. (make-variable-buffer-local 'orgstruct-is-++)
  6139. ;;;###autoload
  6140. (defun turn-on-orgstruct++ ()
  6141. "Unconditionally turn on `orgstruct++-mode'."
  6142. (orgstruct++-mode 1))
  6143. (defun orgstruct-error ()
  6144. "Error when there is no default binding for a structure key."
  6145. (interactive)
  6146. (error "This key has no function outside structure elements"))
  6147. (defun orgstruct-setup ()
  6148. "Setup orgstruct keymaps."
  6149. (let ((nfunc 0)
  6150. (bindings
  6151. (list
  6152. '([(meta up)] org-metaup)
  6153. '([(meta down)] org-metadown)
  6154. '([(meta left)] org-metaleft)
  6155. '([(meta right)] org-metaright)
  6156. '([(meta shift up)] org-shiftmetaup)
  6157. '([(meta shift down)] org-shiftmetadown)
  6158. '([(meta shift left)] org-shiftmetaleft)
  6159. '([(meta shift right)] org-shiftmetaright)
  6160. '([?\e (up)] org-metaup)
  6161. '([?\e (down)] org-metadown)
  6162. '([?\e (left)] org-metaleft)
  6163. '([?\e (right)] org-metaright)
  6164. '([?\e (shift up)] org-shiftmetaup)
  6165. '([?\e (shift down)] org-shiftmetadown)
  6166. '([?\e (shift left)] org-shiftmetaleft)
  6167. '([?\e (shift right)] org-shiftmetaright)
  6168. '([(shift up)] org-shiftup)
  6169. '([(shift down)] org-shiftdown)
  6170. '([(shift left)] org-shiftleft)
  6171. '([(shift right)] org-shiftright)
  6172. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6173. '("\M-q" fill-paragraph)
  6174. '("\C-c^" org-sort)
  6175. '("\C-c-" org-cycle-list-bullet)))
  6176. elt key fun cmd)
  6177. (while (setq elt (pop bindings))
  6178. (setq nfunc (1+ nfunc))
  6179. (setq key (org-key (car elt))
  6180. fun (nth 1 elt)
  6181. cmd (orgstruct-make-binding fun nfunc key))
  6182. (org-defkey orgstruct-mode-map key cmd))
  6183. ;; Special treatment needed for TAB and RET
  6184. (org-defkey orgstruct-mode-map [(tab)]
  6185. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6186. (org-defkey orgstruct-mode-map "\C-i"
  6187. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6188. (org-defkey orgstruct-mode-map "\M-\C-m"
  6189. (orgstruct-make-binding 'org-insert-heading 105
  6190. "\M-\C-m" [(meta return)]))
  6191. (org-defkey orgstruct-mode-map [(meta return)]
  6192. (orgstruct-make-binding 'org-insert-heading 106
  6193. [(meta return)] "\M-\C-m"))
  6194. (org-defkey orgstruct-mode-map [(shift meta return)]
  6195. (orgstruct-make-binding 'org-insert-todo-heading 107
  6196. [(meta return)] "\M-\C-m"))
  6197. (org-defkey orgstruct-mode-map "\e\C-m"
  6198. (orgstruct-make-binding 'org-insert-heading 108
  6199. "\e\C-m" [?\e (return)]))
  6200. (org-defkey orgstruct-mode-map [?\e (return)]
  6201. (orgstruct-make-binding 'org-insert-heading 109
  6202. [?\e (return)] "\e\C-m"))
  6203. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6204. (orgstruct-make-binding 'org-insert-todo-heading 110
  6205. [?\e (return)] "\e\C-m"))
  6206. (unless org-local-vars
  6207. (setq org-local-vars (org-get-local-variables)))
  6208. t))
  6209. (defun orgstruct-make-binding (fun n &rest keys)
  6210. "Create a function for binding in the structure minor mode.
  6211. FUN is the command to call inside a table. N is used to create a unique
  6212. command name. KEYS are keys that should be checked in for a command
  6213. to execute outside of tables."
  6214. (eval
  6215. (list 'defun
  6216. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6217. '(arg)
  6218. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6219. "Outside of structure, run the binding of `"
  6220. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6221. "'.")
  6222. '(interactive "p")
  6223. (list 'if
  6224. `(org-context-p 'headline 'item
  6225. (and orgstruct-is-++
  6226. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6227. 'item-body))
  6228. (list 'org-run-like-in-org-mode (list 'quote fun))
  6229. (list 'let '(orgstruct-mode)
  6230. (list 'call-interactively
  6231. (append '(or)
  6232. (mapcar (lambda (k)
  6233. (list 'key-binding k))
  6234. keys)
  6235. '('orgstruct-error))))))))
  6236. (defun org-context-p (&rest contexts)
  6237. "Check if local context is any of CONTEXTS.
  6238. Possible values in the list of contexts are `table', `headline', and `item'."
  6239. (let ((pos (point)))
  6240. (goto-char (point-at-bol))
  6241. (prog1 (or (and (memq 'table contexts)
  6242. (looking-at "[ \t]*|"))
  6243. (and (memq 'headline contexts)
  6244. ;;????????? (looking-at "\\*+"))
  6245. (looking-at outline-regexp))
  6246. (and (memq 'item contexts)
  6247. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6248. (and (memq 'item-body contexts)
  6249. (org-in-item-p)))
  6250. (goto-char pos))))
  6251. (defun org-get-local-variables ()
  6252. "Return a list of all local variables in an org-mode buffer."
  6253. (let (varlist)
  6254. (with-current-buffer (get-buffer-create "*Org tmp*")
  6255. (erase-buffer)
  6256. (org-mode)
  6257. (setq varlist (buffer-local-variables)))
  6258. (kill-buffer "*Org tmp*")
  6259. (delq nil
  6260. (mapcar
  6261. (lambda (x)
  6262. (setq x
  6263. (if (symbolp x)
  6264. (list x)
  6265. (list (car x) (list 'quote (cdr x)))))
  6266. (if (string-match
  6267. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6268. (symbol-name (car x)))
  6269. x nil))
  6270. varlist))))
  6271. ;;;###autoload
  6272. (defun org-run-like-in-org-mode (cmd)
  6273. "Run a command, pretending that the current buffer is in Org-mode.
  6274. This will temporarily bind local variables that are typically bound in
  6275. Org-mode to the values they have in Org-mode, and then interactively
  6276. call CMD."
  6277. (org-load-modules-maybe)
  6278. (unless org-local-vars
  6279. (setq org-local-vars (org-get-local-variables)))
  6280. (eval (list 'let org-local-vars
  6281. (list 'call-interactively (list 'quote cmd)))))
  6282. ;;;; Archiving
  6283. (defun org-get-category (&optional pos)
  6284. "Get the category applying to position POS."
  6285. (get-text-property (or pos (point)) 'org-category))
  6286. (defun org-refresh-category-properties ()
  6287. "Refresh category text properties in the buffer."
  6288. (let ((def-cat (cond
  6289. ((null org-category)
  6290. (if buffer-file-name
  6291. (file-name-sans-extension
  6292. (file-name-nondirectory buffer-file-name))
  6293. "???"))
  6294. ((symbolp org-category) (symbol-name org-category))
  6295. (t org-category)))
  6296. beg end cat pos optionp)
  6297. (org-unmodified
  6298. (save-excursion
  6299. (save-restriction
  6300. (widen)
  6301. (goto-char (point-min))
  6302. (put-text-property (point) (point-max) 'org-category def-cat)
  6303. (while (re-search-forward
  6304. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6305. (setq pos (match-end 0)
  6306. optionp (equal (char-after (match-beginning 0)) ?#)
  6307. cat (org-trim (match-string 2)))
  6308. (if optionp
  6309. (setq beg (point-at-bol) end (point-max))
  6310. (org-back-to-heading t)
  6311. (setq beg (point) end (org-end-of-subtree t t)))
  6312. (put-text-property beg end 'org-category cat)
  6313. (goto-char pos)))))))
  6314. ;;;; Link Stuff
  6315. ;;; Link abbreviations
  6316. (defun org-link-expand-abbrev (link)
  6317. "Apply replacements as defined in `org-link-abbrev-alist."
  6318. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6319. (let* ((key (match-string 1 link))
  6320. (as (or (assoc key org-link-abbrev-alist-local)
  6321. (assoc key org-link-abbrev-alist)))
  6322. (tag (and (match-end 2) (match-string 3 link)))
  6323. rpl)
  6324. (if (not as)
  6325. link
  6326. (setq rpl (cdr as))
  6327. (cond
  6328. ((symbolp rpl) (funcall rpl tag))
  6329. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6330. ((string-match "%h" rpl)
  6331. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6332. (t (concat rpl tag)))))
  6333. link))
  6334. ;;; Storing and inserting links
  6335. (defvar org-insert-link-history nil
  6336. "Minibuffer history for links inserted with `org-insert-link'.")
  6337. (defvar org-stored-links nil
  6338. "Contains the links stored with `org-store-link'.")
  6339. (defvar org-store-link-plist nil
  6340. "Plist with info about the most recently link created with `org-store-link'.")
  6341. (defvar org-link-protocols nil
  6342. "Link protocols added to Org-mode using `org-add-link-type'.")
  6343. (defvar org-store-link-functions nil
  6344. "List of functions that are called to create and store a link.
  6345. Each function will be called in turn until one returns a non-nil
  6346. value. Each function should check if it is responsible for creating
  6347. this link (for example by looking at the major mode).
  6348. If not, it must exit and return nil.
  6349. If yes, it should return a non-nil value after a calling
  6350. `org-store-link-props' with a list of properties and values.
  6351. Special properties are:
  6352. :type The link prefix. like \"http\". This must be given.
  6353. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6354. This is obligatory as well.
  6355. :description Optional default description for the second pair
  6356. of brackets in an Org-mode link. The user can still change
  6357. this when inserting this link into an Org-mode buffer.
  6358. In addition to these, any additional properties can be specified
  6359. and then used in remember templates.")
  6360. (defun org-add-link-type (type &optional follow export)
  6361. "Add TYPE to the list of `org-link-types'.
  6362. Re-compute all regular expressions depending on `org-link-types'
  6363. FOLLOW and EXPORT are two functions.
  6364. FOLLOW should take the link path as the single argument and do whatever
  6365. is necessary to follow the link, for example find a file or display
  6366. a mail message.
  6367. EXPORT should format the link path for export to one of the export formats.
  6368. It should be a function accepting three arguments:
  6369. path the path of the link, the text after the prefix (like \"http:\")
  6370. desc the description of the link, if any, nil if there was no description
  6371. format the export format, a symbol like `html' or `latex'.
  6372. The function may use the FORMAT information to return different values
  6373. depending on the format. The return value will be put literally into
  6374. the exported file.
  6375. Org-mode has a built-in default for exporting links. If you are happy with
  6376. this default, there is no need to define an export function for the link
  6377. type. For a simple example of an export function, see `org-bbdb.el'."
  6378. (add-to-list 'org-link-types type t)
  6379. (org-make-link-regexps)
  6380. (if (assoc type org-link-protocols)
  6381. (setcdr (assoc type org-link-protocols) (list follow export))
  6382. (push (list type follow export) org-link-protocols)))
  6383. ;;;###autoload
  6384. (defun org-store-link (arg)
  6385. "\\<org-mode-map>Store an org-link to the current location.
  6386. This link is added to `org-stored-links' and can later be inserted
  6387. into an org-buffer with \\[org-insert-link].
  6388. For some link types, a prefix arg is interpreted:
  6389. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6390. For file links, arg negates `org-context-in-file-links'."
  6391. (interactive "P")
  6392. (org-load-modules-maybe)
  6393. (setq org-store-link-plist nil) ; reset
  6394. (let ((outline-regexp (org-get-limited-outline-regexp))
  6395. link cpltxt desc description search txt custom-id)
  6396. (cond
  6397. ((run-hook-with-args-until-success 'org-store-link-functions)
  6398. (setq link (plist-get org-store-link-plist :link)
  6399. desc (or (plist-get org-store-link-plist :description) link)))
  6400. ((equal (buffer-name) "*Org Edit Src Example*")
  6401. (let (label gc)
  6402. (while (or (not label)
  6403. (save-excursion
  6404. (save-restriction
  6405. (widen)
  6406. (goto-char (point-min))
  6407. (re-search-forward
  6408. (regexp-quote (format org-coderef-label-format label))
  6409. nil t))))
  6410. (when label (message "Label exists already") (sit-for 2))
  6411. (setq label (read-string "Code line label: " label)))
  6412. (end-of-line 1)
  6413. (setq link (format org-coderef-label-format label))
  6414. (setq gc (- 79 (length link)))
  6415. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6416. (insert link)
  6417. (setq link (concat "(" label ")") desc nil)))
  6418. ((eq major-mode 'calendar-mode)
  6419. (let ((cd (calendar-cursor-to-date)))
  6420. (setq link
  6421. (format-time-string
  6422. (car org-time-stamp-formats)
  6423. (apply 'encode-time
  6424. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6425. nil nil nil))))
  6426. (org-store-link-props :type "calendar" :date cd)))
  6427. ((eq major-mode 'w3-mode)
  6428. (setq cpltxt (if (and (buffer-name)
  6429. (not (string-match "Untitled" (buffer-name))))
  6430. (buffer-name)
  6431. (url-view-url t))
  6432. link (org-make-link (url-view-url t)))
  6433. (org-store-link-props :type "w3" :url (url-view-url t)))
  6434. ((eq major-mode 'w3m-mode)
  6435. (setq cpltxt (or w3m-current-title w3m-current-url)
  6436. link (org-make-link w3m-current-url))
  6437. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6438. ((setq search (run-hook-with-args-until-success
  6439. 'org-create-file-search-functions))
  6440. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6441. "::" search))
  6442. (setq cpltxt (or description link)))
  6443. ((eq major-mode 'image-mode)
  6444. (setq cpltxt (concat "file:"
  6445. (abbreviate-file-name buffer-file-name))
  6446. link (org-make-link cpltxt))
  6447. (org-store-link-props :type "image" :file buffer-file-name))
  6448. ((eq major-mode 'dired-mode)
  6449. ;; link to the file in the current line
  6450. (setq cpltxt (concat "file:"
  6451. (abbreviate-file-name
  6452. (expand-file-name
  6453. (dired-get-filename nil t))))
  6454. link (org-make-link cpltxt)))
  6455. ((and buffer-file-name (org-mode-p))
  6456. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6457. (cond
  6458. ((org-in-regexp "<<\\(.*?\\)>>")
  6459. (setq cpltxt
  6460. (concat "file:"
  6461. (abbreviate-file-name buffer-file-name)
  6462. "::" (match-string 1))
  6463. link (org-make-link cpltxt)))
  6464. ((and (featurep 'org-id)
  6465. (or (eq org-link-to-org-use-id t)
  6466. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6467. (interactive-p))
  6468. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6469. (interactive-p)
  6470. (not custom-id))
  6471. (and org-link-to-org-use-id
  6472. (condition-case nil
  6473. (org-entry-get nil "ID")
  6474. (error nil)))))
  6475. ;; We can make a link using the ID.
  6476. (setq link (condition-case nil
  6477. (prog1 (org-id-store-link)
  6478. (setq desc (plist-get org-store-link-plist
  6479. :description)))
  6480. (error
  6481. ;; probably before first headline, link to file only
  6482. (concat "file:"
  6483. (abbreviate-file-name buffer-file-name))))))
  6484. (t
  6485. ;; Just link to current headline
  6486. (setq cpltxt (concat "file:"
  6487. (abbreviate-file-name buffer-file-name)))
  6488. ;; Add a context search string
  6489. (when (org-xor org-context-in-file-links arg)
  6490. (setq txt (cond
  6491. ((org-on-heading-p) nil)
  6492. ((org-region-active-p)
  6493. (buffer-substring (region-beginning) (region-end)))
  6494. (t nil)))
  6495. (when (or (null txt) (string-match "\\S-" txt))
  6496. (setq cpltxt
  6497. (concat cpltxt "::"
  6498. (condition-case nil
  6499. (org-make-org-heading-search-string txt)
  6500. (error "")))
  6501. desc (or (nth 4 (org-heading-components)) "NONE"))))
  6502. (if (string-match "::\\'" cpltxt)
  6503. (setq cpltxt (substring cpltxt 0 -2)))
  6504. (setq link (org-make-link cpltxt)))))
  6505. ((buffer-file-name (buffer-base-buffer))
  6506. ;; Just link to this file here.
  6507. (setq cpltxt (concat "file:"
  6508. (abbreviate-file-name
  6509. (buffer-file-name (buffer-base-buffer)))))
  6510. ;; Add a context string
  6511. (when (org-xor org-context-in-file-links arg)
  6512. (setq txt (if (org-region-active-p)
  6513. (buffer-substring (region-beginning) (region-end))
  6514. (buffer-substring (point-at-bol) (point-at-eol))))
  6515. ;; Only use search option if there is some text.
  6516. (when (string-match "\\S-" txt)
  6517. (setq cpltxt
  6518. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6519. desc "NONE")))
  6520. (setq link (org-make-link cpltxt)))
  6521. ((interactive-p)
  6522. (error "Cannot link to a buffer which is not visiting a file"))
  6523. (t (setq link nil)))
  6524. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6525. (setq link (or link cpltxt)
  6526. desc (or desc cpltxt))
  6527. (if (equal desc "NONE") (setq desc nil))
  6528. (if (and (or (interactive-p) executing-kbd-macro) link)
  6529. (progn
  6530. (setq org-stored-links
  6531. (cons (list link desc) org-stored-links))
  6532. (message "Stored: %s" (or desc link))
  6533. (when custom-id
  6534. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6535. "::#" custom-id))
  6536. (setq org-stored-links
  6537. (cons (list link desc) org-stored-links))))
  6538. (and link (org-make-link-string link desc)))))
  6539. (defun org-store-link-props (&rest plist)
  6540. "Store link properties, extract names and addresses."
  6541. (let (x adr)
  6542. (when (setq x (plist-get plist :from))
  6543. (setq adr (mail-extract-address-components x))
  6544. (setq plist (plist-put plist :fromname (car adr)))
  6545. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6546. (when (setq x (plist-get plist :to))
  6547. (setq adr (mail-extract-address-components x))
  6548. (setq plist (plist-put plist :toname (car adr)))
  6549. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6550. (let ((from (plist-get plist :from))
  6551. (to (plist-get plist :to)))
  6552. (when (and from to org-from-is-user-regexp)
  6553. (setq plist
  6554. (plist-put plist :fromto
  6555. (if (string-match org-from-is-user-regexp from)
  6556. (concat "to %t")
  6557. (concat "from %f"))))))
  6558. (setq org-store-link-plist plist))
  6559. (defun org-add-link-props (&rest plist)
  6560. "Add these properties to the link property list."
  6561. (let (key value)
  6562. (while plist
  6563. (setq key (pop plist) value (pop plist))
  6564. (setq org-store-link-plist
  6565. (plist-put org-store-link-plist key value)))))
  6566. (defun org-email-link-description (&optional fmt)
  6567. "Return the description part of an email link.
  6568. This takes information from `org-store-link-plist' and formats it
  6569. according to FMT (default from `org-email-link-description-format')."
  6570. (setq fmt (or fmt org-email-link-description-format))
  6571. (let* ((p org-store-link-plist)
  6572. (to (plist-get p :toaddress))
  6573. (from (plist-get p :fromaddress))
  6574. (table
  6575. (list
  6576. (cons "%c" (plist-get p :fromto))
  6577. (cons "%F" (plist-get p :from))
  6578. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6579. (cons "%T" (plist-get p :to))
  6580. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6581. (cons "%s" (plist-get p :subject))
  6582. (cons "%m" (plist-get p :message-id)))))
  6583. (when (string-match "%c" fmt)
  6584. ;; Check if the user wrote this message
  6585. (if (and org-from-is-user-regexp from to
  6586. (save-match-data (string-match org-from-is-user-regexp from)))
  6587. (setq fmt (replace-match "to %t" t t fmt))
  6588. (setq fmt (replace-match "from %f" t t fmt))))
  6589. (org-replace-escapes fmt table)))
  6590. (defun org-make-org-heading-search-string (&optional string heading)
  6591. "Make search string for STRING or current headline."
  6592. (interactive)
  6593. (let ((s (or string (org-get-heading))))
  6594. (unless (and string (not heading))
  6595. ;; We are using a headline, clean up garbage in there.
  6596. (if (string-match org-todo-regexp s)
  6597. (setq s (replace-match "" t t s)))
  6598. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6599. (setq s (replace-match "" t t s)))
  6600. (setq s (org-trim s))
  6601. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6602. org-comment-string "\\)") s)
  6603. (setq s (replace-match "" t t s)))
  6604. (while (string-match org-ts-regexp s)
  6605. (setq s (replace-match "" t t s))))
  6606. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6607. (setq s (replace-match " " t t s)))
  6608. (or string (setq s (concat "*" s))) ; Add * for headlines
  6609. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6610. (defun org-make-link (&rest strings)
  6611. "Concatenate STRINGS."
  6612. (apply 'concat strings))
  6613. (defun org-make-link-string (link &optional description)
  6614. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6615. (unless (string-match "\\S-" link)
  6616. (error "Empty link"))
  6617. (when (stringp description)
  6618. ;; Remove brackets from the description, they are fatal.
  6619. (while (string-match "\\[" description)
  6620. (setq description (replace-match "{" t t description)))
  6621. (while (string-match "\\]" description)
  6622. (setq description (replace-match "}" t t description))))
  6623. (when (equal (org-link-escape link) description)
  6624. ;; No description needed, it is identical
  6625. (setq description nil))
  6626. (when (and (not description)
  6627. (not (equal link (org-link-escape link))))
  6628. (setq description (org-extract-attributes link)))
  6629. (concat "[[" (org-link-escape link) "]"
  6630. (if description (concat "[" description "]") "")
  6631. "]"))
  6632. (defconst org-link-escape-chars
  6633. '((?\ . "%20")
  6634. (?\[ . "%5B")
  6635. (?\] . "%5D")
  6636. (?\340 . "%E0") ; `a
  6637. (?\342 . "%E2") ; ^a
  6638. (?\347 . "%E7") ; ,c
  6639. (?\350 . "%E8") ; `e
  6640. (?\351 . "%E9") ; 'e
  6641. (?\352 . "%EA") ; ^e
  6642. (?\356 . "%EE") ; ^i
  6643. (?\364 . "%F4") ; ^o
  6644. (?\371 . "%F9") ; `u
  6645. (?\373 . "%FB") ; ^u
  6646. (?\; . "%3B")
  6647. (?? . "%3F")
  6648. (?= . "%3D")
  6649. (?+ . "%2B")
  6650. )
  6651. "Association list of escapes for some characters problematic in links.
  6652. This is the list that is used for internal purposes.")
  6653. (defvar org-url-encoding-use-url-hexify nil)
  6654. (defconst org-link-escape-chars-browser
  6655. '((?\ . "%20")) ; 32 for the SPC char
  6656. "Association list of escapes for some characters problematic in links.
  6657. This is the list that is used before handing over to the browser.")
  6658. (defun org-link-escape (text &optional table)
  6659. "Escape characters in TEXT that are problematic for links."
  6660. (if org-url-encoding-use-url-hexify
  6661. (url-hexify-string text)
  6662. (setq table (or table org-link-escape-chars))
  6663. (when text
  6664. (let ((re (mapconcat (lambda (x) (regexp-quote
  6665. (char-to-string (car x))))
  6666. table "\\|")))
  6667. (while (string-match re text)
  6668. (setq text
  6669. (replace-match
  6670. (cdr (assoc (string-to-char (match-string 0 text))
  6671. table))
  6672. t t text)))
  6673. text))))
  6674. (defun org-link-unescape (text &optional table)
  6675. "Reverse the action of `org-link-escape'."
  6676. (if org-url-encoding-use-url-hexify
  6677. (url-unhex-string text)
  6678. (setq table (or table org-link-escape-chars))
  6679. (when text
  6680. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6681. table "\\|")))
  6682. (while (string-match re text)
  6683. (setq text
  6684. (replace-match
  6685. (char-to-string (car (rassoc (match-string 0 text) table)))
  6686. t t text)))
  6687. text))))
  6688. (defun org-xor (a b)
  6689. "Exclusive or."
  6690. (if a (not b) b))
  6691. (defun org-fixup-message-id-for-http (s)
  6692. "Replace special characters in a message id, so it can be used in an http query."
  6693. (while (string-match "<" s)
  6694. (setq s (replace-match "%3C" t t s)))
  6695. (while (string-match ">" s)
  6696. (setq s (replace-match "%3E" t t s)))
  6697. (while (string-match "@" s)
  6698. (setq s (replace-match "%40" t t s)))
  6699. s)
  6700. ;;;###autoload
  6701. (defun org-insert-link-global ()
  6702. "Insert a link like Org-mode does.
  6703. This command can be called in any mode to insert a link in Org-mode syntax."
  6704. (interactive)
  6705. (org-load-modules-maybe)
  6706. (org-run-like-in-org-mode 'org-insert-link))
  6707. (defun org-insert-link (&optional complete-file link-location)
  6708. "Insert a link. At the prompt, enter the link.
  6709. Completion can be used to insert any of the link protocol prefixes like
  6710. http or ftp in use.
  6711. The history can be used to select a link previously stored with
  6712. `org-store-link'. When the empty string is entered (i.e. if you just
  6713. press RET at the prompt), the link defaults to the most recently
  6714. stored link. As SPC triggers completion in the minibuffer, you need to
  6715. use M-SPC or C-q SPC to force the insertion of a space character.
  6716. You will also be prompted for a description, and if one is given, it will
  6717. be displayed in the buffer instead of the link.
  6718. If there is already a link at point, this command will allow you to edit link
  6719. and description parts.
  6720. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6721. be selected using completion. The path to the file will be relative to the
  6722. current directory if the file is in the current directory or a subdirectory.
  6723. Otherwise, the link will be the absolute path as completed in the minibuffer
  6724. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6725. option `org-link-file-path-type'.
  6726. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6727. the current directory or below.
  6728. With three \\[universal-argument] prefixes, negate the meaning of
  6729. `org-keep-stored-link-after-insertion'.
  6730. If `org-make-link-description-function' is non-nil, this function will be
  6731. called with the link target, and the result will be the default
  6732. link description.
  6733. If the LINK-LOCATION parameter is non-nil, this value will be
  6734. used as the link location instead of reading one interactively."
  6735. (interactive "P")
  6736. (let* ((wcf (current-window-configuration))
  6737. (region (if (org-region-active-p)
  6738. (buffer-substring (region-beginning) (region-end))))
  6739. (remove (and region (list (region-beginning) (region-end))))
  6740. (desc region)
  6741. tmphist ; byte-compile incorrectly complains about this
  6742. (link link-location)
  6743. entry file all-prefixes)
  6744. (cond
  6745. (link-location) ; specified by arg, just use it.
  6746. ((org-in-regexp org-bracket-link-regexp 1)
  6747. ;; We do have a link at point, and we are going to edit it.
  6748. (setq remove (list (match-beginning 0) (match-end 0)))
  6749. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6750. (setq link (read-string "Link: "
  6751. (org-link-unescape
  6752. (org-match-string-no-properties 1)))))
  6753. ((or (org-in-regexp org-angle-link-re)
  6754. (org-in-regexp org-plain-link-re))
  6755. ;; Convert to bracket link
  6756. (setq remove (list (match-beginning 0) (match-end 0))
  6757. link (read-string "Link: "
  6758. (org-remove-angle-brackets (match-string 0)))))
  6759. ((member complete-file '((4) (16)))
  6760. ;; Completing read for file names.
  6761. (setq link (org-file-complete-link complete-file)))
  6762. (t
  6763. ;; Read link, with completion for stored links.
  6764. (with-output-to-temp-buffer "*Org Links*"
  6765. (princ "Insert a link.
  6766. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6767. (when org-stored-links
  6768. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6769. (princ (mapconcat
  6770. (lambda (x)
  6771. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6772. (reverse org-stored-links) "\n"))))
  6773. (let ((cw (selected-window)))
  6774. (select-window (get-buffer-window "*Org Links*"))
  6775. (setq truncate-lines t)
  6776. (unless (pos-visible-in-window-p (point-max))
  6777. (org-fit-window-to-buffer))
  6778. (and (window-live-p cw) (select-window cw)))
  6779. ;; Fake a link history, containing the stored links.
  6780. (setq tmphist (append (mapcar 'car org-stored-links)
  6781. org-insert-link-history))
  6782. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6783. (mapcar 'car org-link-abbrev-alist)
  6784. org-link-types))
  6785. (unwind-protect
  6786. (progn
  6787. (setq link
  6788. (let ((org-completion-use-ido nil)
  6789. (org-completion-use-iswitchb nil))
  6790. (org-completing-read
  6791. "Link: "
  6792. (append
  6793. (mapcar (lambda (x) (list (concat x ":")))
  6794. all-prefixes)
  6795. (mapcar 'car org-stored-links))
  6796. nil nil nil
  6797. 'tmphist
  6798. (car (car org-stored-links)))))
  6799. (if (or (member link all-prefixes)
  6800. (and (equal ":" (substring link -1))
  6801. (member (substring link 0 -1) all-prefixes)
  6802. (setq link (substring link 0 -1))))
  6803. (setq link (org-link-try-special-completion link))))
  6804. (set-window-configuration wcf)
  6805. (kill-buffer "*Org Links*"))
  6806. (setq entry (assoc link org-stored-links))
  6807. (or entry (push link org-insert-link-history))
  6808. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6809. (not org-keep-stored-link-after-insertion))
  6810. (setq org-stored-links (delq (assoc link org-stored-links)
  6811. org-stored-links)))
  6812. (setq desc (or desc (nth 1 entry)))))
  6813. (if (string-match org-plain-link-re link)
  6814. ;; URL-like link, normalize the use of angular brackets.
  6815. (setq link (org-make-link (org-remove-angle-brackets link))))
  6816. ;; Check if we are linking to the current file with a search option
  6817. ;; If yes, simplify the link by using only the search option.
  6818. (when (and buffer-file-name
  6819. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6820. (let* ((path (match-string 1 link))
  6821. (case-fold-search nil)
  6822. (search (match-string 2 link)))
  6823. (save-match-data
  6824. (if (equal (file-truename buffer-file-name) (file-truename path))
  6825. ;; We are linking to this same file, with a search option
  6826. (setq link search)))))
  6827. ;; Check if we can/should use a relative path. If yes, simplify the link
  6828. (when (string-match "^file:\\(.*\\)" link)
  6829. (let* ((path (match-string 1 link))
  6830. (origpath path)
  6831. (case-fold-search nil))
  6832. (cond
  6833. ((or (eq org-link-file-path-type 'absolute)
  6834. (equal complete-file '(16)))
  6835. (setq path (abbreviate-file-name (expand-file-name path))))
  6836. ((eq org-link-file-path-type 'noabbrev)
  6837. (setq path (expand-file-name path)))
  6838. ((eq org-link-file-path-type 'relative)
  6839. (setq path (file-relative-name path)))
  6840. (t
  6841. (save-match-data
  6842. (if (string-match (concat "^" (regexp-quote
  6843. (file-name-as-directory
  6844. (expand-file-name "."))))
  6845. (expand-file-name path))
  6846. ;; We are linking a file with relative path name.
  6847. (setq path (substring (expand-file-name path)
  6848. (match-end 0)))
  6849. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6850. (setq link (concat "file:" path))
  6851. (if (equal desc origpath)
  6852. (setq desc path))))
  6853. (if org-make-link-description-function
  6854. (setq desc (funcall org-make-link-description-function link desc)))
  6855. (setq desc (read-string "Description: " desc))
  6856. (unless (string-match "\\S-" desc) (setq desc nil))
  6857. (if remove (apply 'delete-region remove))
  6858. (insert (org-make-link-string link desc))))
  6859. (defun org-link-try-special-completion (type)
  6860. "If there is completion support for link type TYPE, offer it."
  6861. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6862. (if (functionp fun)
  6863. (funcall fun)
  6864. (read-string "Link (no completion support): " (concat type ":")))))
  6865. (defun org-file-complete-link (&optional arg)
  6866. "Create a file link using completion."
  6867. (let (file link)
  6868. (setq file (read-file-name "File: "))
  6869. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6870. (pwd1 (file-name-as-directory (abbreviate-file-name
  6871. (expand-file-name ".")))))
  6872. (cond
  6873. ((equal arg '(16))
  6874. (setq link (org-make-link
  6875. "file:"
  6876. (abbreviate-file-name (expand-file-name file)))))
  6877. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6878. (setq link (org-make-link "file:" (match-string 1 file))))
  6879. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6880. (expand-file-name file))
  6881. (setq link (org-make-link
  6882. "file:" (match-string 1 (expand-file-name file)))))
  6883. (t (setq link (org-make-link "file:" file)))))
  6884. link))
  6885. (defun org-completing-read (&rest args)
  6886. "Completing-read with SPACE being a normal character."
  6887. (let ((minibuffer-local-completion-map
  6888. (copy-keymap minibuffer-local-completion-map)))
  6889. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6890. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6891. (apply 'org-icompleting-read args)))
  6892. (defun org-completing-read-no-i (&rest args)
  6893. (let (org-completion-use-ido org-completion-use-iswitchb)
  6894. (apply 'org-completing-read args)))
  6895. (defun org-iswitchb-completing-read (prompt choices &rest args)
  6896. "Use iswitch as a completing-read replacement to choose from choices.
  6897. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  6898. from."
  6899. (let ((iswitchb-make-buflist-hook
  6900. (lambda ()
  6901. (setq iswitchb-temp-buflist choices))))
  6902. (iswitchb-read-buffer prompt)))
  6903. (defun org-icompleting-read (&rest args)
  6904. "Completing-read using `ido-mode' or `iswitchb' speedups if available"
  6905. (if (and org-completion-use-ido
  6906. (fboundp 'ido-completing-read)
  6907. (boundp 'ido-mode) ido-mode
  6908. (listp (second args)))
  6909. (let ((ido-enter-matching-directory nil))
  6910. (apply 'ido-completing-read (concat (car args))
  6911. (if (consp (car (nth 1 args)))
  6912. (mapcar (lambda (x) (car x)) (nth 1 args))
  6913. (nth 1 args))
  6914. (cddr args)))
  6915. (if (and org-completion-use-iswitchb
  6916. (boundp 'iswitchb-mode) iswitchb-mode
  6917. (listp (second args)))
  6918. (apply 'org-iswitchb-completing-read (concat (car args))
  6919. (mapcar (lambda (x) (car x)) (nth 1 args))
  6920. (cddr args))
  6921. (apply 'completing-read args))))
  6922. (defun org-extract-attributes (s)
  6923. "Extract the attributes cookie from a string and set as text property."
  6924. (let (a attr (start 0) key value)
  6925. (save-match-data
  6926. (when (string-match "{{\\([^}]+\\)}}$" s)
  6927. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6928. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6929. (setq key (match-string 1 a) value (match-string 2 a)
  6930. start (match-end 0)
  6931. attr (plist-put attr (intern key) value))))
  6932. (org-add-props s nil 'org-attr attr))
  6933. s))
  6934. (defun org-extract-attributes-from-string (tag)
  6935. (let (key value attr)
  6936. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6937. (setq key (match-string 1 tag) value (match-string 2 tag)
  6938. tag (replace-match "" t t tag)
  6939. attr (plist-put attr (intern key) value)))
  6940. (cons tag attr)))
  6941. (defun org-attributes-to-string (plist)
  6942. "Format a property list into an HTML attribute list."
  6943. (let ((s "") key value)
  6944. (while plist
  6945. (setq key (pop plist) value (pop plist))
  6946. (and value
  6947. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6948. s))
  6949. ;;; Opening/following a link
  6950. (defvar org-link-search-failed nil)
  6951. (defun org-next-link ()
  6952. "Move forward to the next link.
  6953. If the link is in hidden text, expose it."
  6954. (interactive)
  6955. (when (and org-link-search-failed (eq this-command last-command))
  6956. (goto-char (point-min))
  6957. (message "Link search wrapped back to beginning of buffer"))
  6958. (setq org-link-search-failed nil)
  6959. (let* ((pos (point))
  6960. (ct (org-context))
  6961. (a (assoc :link ct)))
  6962. (if a (goto-char (nth 2 a)))
  6963. (if (re-search-forward org-any-link-re nil t)
  6964. (progn
  6965. (goto-char (match-beginning 0))
  6966. (if (org-invisible-p) (org-show-context)))
  6967. (goto-char pos)
  6968. (setq org-link-search-failed t)
  6969. (error "No further link found"))))
  6970. (defun org-previous-link ()
  6971. "Move backward to the previous link.
  6972. If the link is in hidden text, expose it."
  6973. (interactive)
  6974. (when (and org-link-search-failed (eq this-command last-command))
  6975. (goto-char (point-max))
  6976. (message "Link search wrapped back to end of buffer"))
  6977. (setq org-link-search-failed nil)
  6978. (let* ((pos (point))
  6979. (ct (org-context))
  6980. (a (assoc :link ct)))
  6981. (if a (goto-char (nth 1 a)))
  6982. (if (re-search-backward org-any-link-re nil t)
  6983. (progn
  6984. (goto-char (match-beginning 0))
  6985. (if (org-invisible-p) (org-show-context)))
  6986. (goto-char pos)
  6987. (setq org-link-search-failed t)
  6988. (error "No further link found"))))
  6989. (defun org-translate-link (s)
  6990. "Translate a link string if a translation function has been defined."
  6991. (if (and org-link-translation-function
  6992. (fboundp org-link-translation-function)
  6993. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6994. (progn
  6995. (setq s (funcall org-link-translation-function
  6996. (match-string 1) (match-string 2)))
  6997. (concat (car s) ":" (cdr s)))
  6998. s))
  6999. (defun org-translate-link-from-planner (type path)
  7000. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7001. This is still an experimental function, your mileage may vary."
  7002. (cond
  7003. ((member type '("http" "https" "news" "ftp"))
  7004. ;; standard Internet links are the same.
  7005. nil)
  7006. ((and (equal type "irc") (string-match "^//" path))
  7007. ;; Planner has two / at the beginning of an irc link, we have 1.
  7008. ;; We should have zero, actually....
  7009. (setq path (substring path 1)))
  7010. ((and (equal type "lisp") (string-match "^/" path))
  7011. ;; Planner has a slash, we do not.
  7012. (setq type "elisp" path (substring path 1)))
  7013. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7014. ;; A typical message link. Planner has the id after the fina slash,
  7015. ;; we separate it with a hash mark
  7016. (setq path (concat (match-string 1 path) "#"
  7017. (org-remove-angle-brackets (match-string 2 path)))))
  7018. )
  7019. (cons type path))
  7020. (defun org-find-file-at-mouse (ev)
  7021. "Open file link or URL at mouse."
  7022. (interactive "e")
  7023. (mouse-set-point ev)
  7024. (org-open-at-point 'in-emacs))
  7025. (defun org-open-at-mouse (ev)
  7026. "Open file link or URL at mouse."
  7027. (interactive "e")
  7028. (mouse-set-point ev)
  7029. (if (eq major-mode 'org-agenda-mode)
  7030. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7031. (org-open-at-point))
  7032. (defvar org-window-config-before-follow-link nil
  7033. "The window configuration before following a link.
  7034. This is saved in case the need arises to restore it.")
  7035. (defvar org-open-link-marker (make-marker)
  7036. "Marker pointing to the location where `org-open-at-point; was called.")
  7037. ;;;###autoload
  7038. (defun org-open-at-point-global ()
  7039. "Follow a link like Org-mode does.
  7040. This command can be called in any mode to follow a link that has
  7041. Org-mode syntax."
  7042. (interactive)
  7043. (org-run-like-in-org-mode 'org-open-at-point))
  7044. ;;;###autoload
  7045. (defun org-open-link-from-string (s &optional arg)
  7046. "Open a link in the string S, as if it was in Org-mode."
  7047. (interactive "sLink: \nP")
  7048. (let ((reference-buffer (current-buffer)))
  7049. (with-temp-buffer
  7050. (let ((org-inhibit-startup t))
  7051. (org-mode)
  7052. (insert s)
  7053. (goto-char (point-min))
  7054. (org-open-at-point arg reference-buffer)))))
  7055. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7056. "Open link at or after point.
  7057. If there is no link at point, this function will search forward up to
  7058. the end of the current line.
  7059. Normally, files will be opened by an appropriate application. If the
  7060. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7061. With a double prefix argument, try to open outside of Emacs, in the
  7062. application the system uses for this file type."
  7063. (interactive "P")
  7064. (org-load-modules-maybe)
  7065. (move-marker org-open-link-marker (point))
  7066. (setq org-window-config-before-follow-link (current-window-configuration))
  7067. (org-remove-occur-highlights nil nil t)
  7068. (cond
  7069. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7070. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7071. (org-footnote-action))
  7072. (t
  7073. (let (type path link line search (pos (point)))
  7074. (catch 'match
  7075. (save-excursion
  7076. (skip-chars-forward "^]\n\r")
  7077. (when (org-in-regexp org-bracket-link-regexp)
  7078. (setq link (org-extract-attributes
  7079. (org-link-unescape (org-match-string-no-properties 1))))
  7080. (while (string-match " *\n *" link)
  7081. (setq link (replace-match " " t t link)))
  7082. (setq link (org-link-expand-abbrev link))
  7083. (cond
  7084. ((or (file-name-absolute-p link)
  7085. (string-match "^\\.\\.?/" link))
  7086. (setq type "file" path link))
  7087. ((string-match org-link-re-with-space3 link)
  7088. (setq type (match-string 1 link) path (match-string 2 link)))
  7089. (t (setq type "thisfile" path link)))
  7090. (throw 'match t)))
  7091. (when (get-text-property (point) 'org-linked-text)
  7092. (setq type "thisfile"
  7093. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7094. (1+ (point)) (point))
  7095. path (buffer-substring
  7096. (previous-single-property-change pos 'org-linked-text)
  7097. (next-single-property-change pos 'org-linked-text)))
  7098. (throw 'match t))
  7099. (save-excursion
  7100. (when (or (org-in-regexp org-angle-link-re)
  7101. (org-in-regexp org-plain-link-re))
  7102. (setq type (match-string 1) path (match-string 2))
  7103. (throw 'match t)))
  7104. (save-excursion
  7105. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7106. (setq type "tags"
  7107. path (match-string 1))
  7108. (while (string-match ":" path)
  7109. (setq path (replace-match "+" t t path)))
  7110. (throw 'match t)))
  7111. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7112. (setq type "tree-match"
  7113. path (match-string 1))
  7114. (throw 'match t)))
  7115. (unless path
  7116. (error "No link found"))
  7117. ;; switch back to reference buffer
  7118. ;; needed when if called in a temporary buffer through
  7119. ;; org-open-link-from-string
  7120. (and reference-buffer (switch-to-buffer reference-buffer))
  7121. ;; Remove any trailing spaces in path
  7122. (if (string-match " +\\'" path)
  7123. (setq path (replace-match "" t t path)))
  7124. (if (and org-link-translation-function
  7125. (fboundp org-link-translation-function))
  7126. ;; Check if we need to translate the link
  7127. (let ((tmp (funcall org-link-translation-function type path)))
  7128. (setq type (car tmp) path (cdr tmp))))
  7129. (cond
  7130. ((assoc type org-link-protocols)
  7131. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7132. ((equal type "mailto")
  7133. (let ((cmd (car org-link-mailto-program))
  7134. (args (cdr org-link-mailto-program)) args1
  7135. (address path) (subject "") a)
  7136. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7137. (setq address (match-string 1 path)
  7138. subject (org-link-escape (match-string 2 path))))
  7139. (while args
  7140. (cond
  7141. ((not (stringp (car args))) (push (pop args) args1))
  7142. (t (setq a (pop args))
  7143. (if (string-match "%a" a)
  7144. (setq a (replace-match address t t a)))
  7145. (if (string-match "%s" a)
  7146. (setq a (replace-match subject t t a)))
  7147. (push a args1))))
  7148. (apply cmd (nreverse args1))))
  7149. ((member type '("http" "https" "ftp" "news"))
  7150. (browse-url (concat type ":" (org-link-escape
  7151. path org-link-escape-chars-browser))))
  7152. ((member type '("message"))
  7153. (browse-url (concat type ":" path)))
  7154. ((string= type "tags")
  7155. (org-tags-view in-emacs path))
  7156. ((string= type "thisfile")
  7157. (if in-emacs
  7158. (switch-to-buffer-other-window
  7159. (org-get-buffer-for-internal-link (current-buffer)))
  7160. (org-mark-ring-push))
  7161. (let ((cmd `(org-link-search
  7162. ,path
  7163. ,(cond ((equal in-emacs '(4)) 'occur)
  7164. ((equal in-emacs '(16)) 'org-occur)
  7165. (t nil))
  7166. ,pos)))
  7167. (condition-case nil (eval cmd)
  7168. (error (progn (widen) (eval cmd))))))
  7169. ((string= type "tree-match")
  7170. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7171. ((string= type "file")
  7172. (if (string-match "::\\([0-9]+\\)\\'" path)
  7173. (setq line (string-to-number (match-string 1 path))
  7174. path (substring path 0 (match-beginning 0)))
  7175. (if (string-match "::\\(.+\\)\\'" path)
  7176. (setq search (match-string 1 path)
  7177. path (substring path 0 (match-beginning 0)))))
  7178. (if (string-match "[*?{]" (file-name-nondirectory path))
  7179. (dired path)
  7180. (org-open-file path in-emacs line search)))
  7181. ((string= type "news")
  7182. (require 'org-gnus)
  7183. (org-gnus-follow-link path))
  7184. ((string= type "shell")
  7185. (let ((cmd path))
  7186. (if (or (not org-confirm-shell-link-function)
  7187. (funcall org-confirm-shell-link-function
  7188. (format "Execute \"%s\" in shell? "
  7189. (org-add-props cmd nil
  7190. 'face 'org-warning))))
  7191. (progn
  7192. (message "Executing %s" cmd)
  7193. (shell-command cmd))
  7194. (error "Abort"))))
  7195. ((string= type "elisp")
  7196. (let ((cmd path))
  7197. (if (or (not org-confirm-elisp-link-function)
  7198. (funcall org-confirm-elisp-link-function
  7199. (format "Execute \"%s\" as elisp? "
  7200. (org-add-props cmd nil
  7201. 'face 'org-warning))))
  7202. (message "%s => %s" cmd
  7203. (if (equal (string-to-char cmd) ?\()
  7204. (eval (read cmd))
  7205. (call-interactively (read cmd))))
  7206. (error "Abort"))))
  7207. (t
  7208. (browse-url-at-point))))))
  7209. (move-marker org-open-link-marker nil)
  7210. (run-hook-with-args 'org-follow-link-hook))
  7211. ;;;; Time estimates
  7212. (defun org-get-effort (&optional pom)
  7213. "Get the effort estimate for the current entry."
  7214. (org-entry-get pom org-effort-property))
  7215. ;;; File search
  7216. (defvar org-create-file-search-functions nil
  7217. "List of functions to construct the right search string for a file link.
  7218. These functions are called in turn with point at the location to
  7219. which the link should point.
  7220. A function in the hook should first test if it would like to
  7221. handle this file type, for example by checking the major-mode or
  7222. the file extension. If it decides not to handle this file, it
  7223. should just return nil to give other functions a chance. If it
  7224. does handle the file, it must return the search string to be used
  7225. when following the link. The search string will be part of the
  7226. file link, given after a double colon, and `org-open-at-point'
  7227. will automatically search for it. If special measures must be
  7228. taken to make the search successful, another function should be
  7229. added to the companion hook `org-execute-file-search-functions',
  7230. which see.
  7231. A function in this hook may also use `setq' to set the variable
  7232. `description' to provide a suggestion for the descriptive text to
  7233. be used for this link when it gets inserted into an Org-mode
  7234. buffer with \\[org-insert-link].")
  7235. (defvar org-execute-file-search-functions nil
  7236. "List of functions to execute a file search triggered by a link.
  7237. Functions added to this hook must accept a single argument, the
  7238. search string that was part of the file link, the part after the
  7239. double colon. The function must first check if it would like to
  7240. handle this search, for example by checking the major-mode or the
  7241. file extension. If it decides not to handle this search, it
  7242. should just return nil to give other functions a chance. If it
  7243. does handle the search, it must return a non-nil value to keep
  7244. other functions from trying.
  7245. Each function can access the current prefix argument through the
  7246. variable `current-prefix-argument'. Note that a single prefix is
  7247. used to force opening a link in Emacs, so it may be good to only
  7248. use a numeric or double prefix to guide the search function.
  7249. In case this is needed, a function in this hook can also restore
  7250. the window configuration before `org-open-at-point' was called using:
  7251. (set-window-configuration org-window-config-before-follow-link)")
  7252. (defun org-link-search (s &optional type avoid-pos)
  7253. "Search for a link search option.
  7254. If S is surrounded by forward slashes, it is interpreted as a
  7255. regular expression. In org-mode files, this will create an `org-occur'
  7256. sparse tree. In ordinary files, `occur' will be used to list matches.
  7257. If the current buffer is in `dired-mode', grep will be used to search
  7258. in all files. If AVOID-POS is given, ignore matches near that position."
  7259. (let ((case-fold-search t)
  7260. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7261. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7262. (append '(("") (" ") ("\t") ("\n"))
  7263. org-emphasis-alist)
  7264. "\\|") "\\)"))
  7265. (pos (point))
  7266. (pre nil) (post nil)
  7267. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7268. (cond
  7269. ;; First check if there are any special
  7270. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7271. ;; Now try the builtin stuff
  7272. ((and (equal (string-to-char s0) ?#)
  7273. (> (length s0) 1)
  7274. (save-excursion
  7275. (goto-char (point-min))
  7276. (and
  7277. (re-search-forward
  7278. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7279. (setq type 'dedicated
  7280. pos (match-beginning 0))))
  7281. ;; There is an exact target for this
  7282. (goto-char pos)
  7283. (org-back-to-heading t)))
  7284. ((save-excursion
  7285. (goto-char (point-min))
  7286. (and
  7287. (re-search-forward
  7288. (concat "<<" (regexp-quote s0) ">>") nil t)
  7289. (setq type 'dedicated
  7290. pos (match-beginning 0))))
  7291. ;; There is an exact target for this
  7292. (goto-char pos))
  7293. ((and (string-match "^(\\(.*\\))$" s0)
  7294. (save-excursion
  7295. (goto-char (point-min))
  7296. (and
  7297. (re-search-forward
  7298. (concat "[^[]" (regexp-quote
  7299. (format org-coderef-label-format
  7300. (match-string 1 s0))))
  7301. nil t)
  7302. (setq type 'dedicated
  7303. pos (1+ (match-beginning 0))))))
  7304. ;; There is a coderef target for this
  7305. (goto-char pos))
  7306. ((string-match "^/\\(.*\\)/$" s)
  7307. ;; A regular expression
  7308. (cond
  7309. ((org-mode-p)
  7310. (org-occur (match-string 1 s)))
  7311. ;;((eq major-mode 'dired-mode)
  7312. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7313. (t (org-do-occur (match-string 1 s)))))
  7314. (t
  7315. ;; A normal search strings
  7316. (when (equal (string-to-char s) ?*)
  7317. ;; Anchor on headlines, post may include tags.
  7318. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7319. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7320. s (substring s 1)))
  7321. (remove-text-properties
  7322. 0 (length s)
  7323. '(face nil mouse-face nil keymap nil fontified nil) s)
  7324. ;; Make a series of regular expressions to find a match
  7325. (setq words (org-split-string s "[ \n\r\t]+")
  7326. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7327. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7328. "\\)" markers)
  7329. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7330. re2a (concat "[ \t\r\n]" re2a_)
  7331. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7332. re4 (concat "[^a-zA-Z_]" re4_)
  7333. re1 (concat pre re2 post)
  7334. re3 (concat pre (if pre re4_ re4) post)
  7335. re5 (concat pre ".*" re4)
  7336. re2 (concat pre re2)
  7337. re2a (concat pre (if pre re2a_ re2a))
  7338. re4 (concat pre (if pre re4_ re4))
  7339. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7340. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7341. re5 "\\)"
  7342. ))
  7343. (cond
  7344. ((eq type 'org-occur) (org-occur reall))
  7345. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7346. (t (goto-char (point-min))
  7347. (setq type 'fuzzy)
  7348. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7349. (org-search-not-self 1 re1 nil t)
  7350. (org-search-not-self 1 re2 nil t)
  7351. (org-search-not-self 1 re2a nil t)
  7352. (org-search-not-self 1 re3 nil t)
  7353. (org-search-not-self 1 re4 nil t)
  7354. (org-search-not-self 1 re5 nil t)
  7355. )
  7356. (goto-char (match-beginning 1))
  7357. (goto-char pos)
  7358. (error "No match")))))
  7359. (t
  7360. ;; Normal string-search
  7361. (goto-char (point-min))
  7362. (if (search-forward s nil t)
  7363. (goto-char (match-beginning 0))
  7364. (error "No match"))))
  7365. (and (org-mode-p) (org-show-context 'link-search))
  7366. type))
  7367. (defun org-search-not-self (group &rest args)
  7368. "Execute `re-search-forward', but only accept matches that do not
  7369. enclose the position of `org-open-link-marker'."
  7370. (let ((m org-open-link-marker))
  7371. (catch 'exit
  7372. (while (apply 're-search-forward args)
  7373. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7374. (goto-char (match-end group))
  7375. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7376. (> (match-beginning 0) (marker-position m))
  7377. (< (match-end 0) (marker-position m)))
  7378. (save-match-data
  7379. (or (not (org-in-regexp
  7380. org-bracket-link-analytic-regexp 1))
  7381. (not (match-end 4)) ; no description
  7382. (and (<= (match-beginning 4) (point))
  7383. (>= (match-end 4) (point))))))
  7384. (throw 'exit (point))))))))
  7385. (defun org-get-buffer-for-internal-link (buffer)
  7386. "Return a buffer to be used for displaying the link target of internal links."
  7387. (cond
  7388. ((not org-display-internal-link-with-indirect-buffer)
  7389. buffer)
  7390. ((string-match "(Clone)$" (buffer-name buffer))
  7391. (message "Buffer is already a clone, not making another one")
  7392. ;; we also do not modify visibility in this case
  7393. buffer)
  7394. (t ; make a new indirect buffer for displaying the link
  7395. (let* ((bn (buffer-name buffer))
  7396. (ibn (concat bn "(Clone)"))
  7397. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7398. (with-current-buffer ib (org-overview))
  7399. ib))))
  7400. (defun org-do-occur (regexp &optional cleanup)
  7401. "Call the Emacs command `occur'.
  7402. If CLEANUP is non-nil, remove the printout of the regular expression
  7403. in the *Occur* buffer. This is useful if the regex is long and not useful
  7404. to read."
  7405. (occur regexp)
  7406. (when cleanup
  7407. (let ((cwin (selected-window)) win beg end)
  7408. (when (setq win (get-buffer-window "*Occur*"))
  7409. (select-window win))
  7410. (goto-char (point-min))
  7411. (when (re-search-forward "match[a-z]+" nil t)
  7412. (setq beg (match-end 0))
  7413. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7414. (setq end (1- (match-beginning 0)))))
  7415. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7416. (goto-char (point-min))
  7417. (select-window cwin))))
  7418. ;;; The mark ring for links jumps
  7419. (defvar org-mark-ring nil
  7420. "Mark ring for positions before jumps in Org-mode.")
  7421. (defvar org-mark-ring-last-goto nil
  7422. "Last position in the mark ring used to go back.")
  7423. ;; Fill and close the ring
  7424. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7425. (loop for i from 1 to org-mark-ring-length do
  7426. (push (make-marker) org-mark-ring))
  7427. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7428. org-mark-ring)
  7429. (defun org-mark-ring-push (&optional pos buffer)
  7430. "Put the current position or POS into the mark ring and rotate it."
  7431. (interactive)
  7432. (setq pos (or pos (point)))
  7433. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7434. (move-marker (car org-mark-ring)
  7435. (or pos (point))
  7436. (or buffer (current-buffer)))
  7437. (message "%s"
  7438. (substitute-command-keys
  7439. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7440. (defun org-mark-ring-goto (&optional n)
  7441. "Jump to the previous position in the mark ring.
  7442. With prefix arg N, jump back that many stored positions. When
  7443. called several times in succession, walk through the entire ring.
  7444. Org-mode commands jumping to a different position in the current file,
  7445. or to another Org-mode file, automatically push the old position
  7446. onto the ring."
  7447. (interactive "p")
  7448. (let (p m)
  7449. (if (eq last-command this-command)
  7450. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7451. (setq p org-mark-ring))
  7452. (setq org-mark-ring-last-goto p)
  7453. (setq m (car p))
  7454. (switch-to-buffer (marker-buffer m))
  7455. (goto-char m)
  7456. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7457. (defun org-remove-angle-brackets (s)
  7458. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7459. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7460. s)
  7461. (defun org-add-angle-brackets (s)
  7462. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7463. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7464. s)
  7465. (defun org-remove-double-quotes (s)
  7466. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7467. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7468. s)
  7469. ;;; Following specific links
  7470. (defun org-follow-timestamp-link ()
  7471. (cond
  7472. ((org-at-date-range-p t)
  7473. (let ((org-agenda-start-on-weekday)
  7474. (t1 (match-string 1))
  7475. (t2 (match-string 2)))
  7476. (setq t1 (time-to-days (org-time-string-to-time t1))
  7477. t2 (time-to-days (org-time-string-to-time t2)))
  7478. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7479. ((org-at-timestamp-p t)
  7480. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7481. (substring (match-string 1) 0 10)))
  7482. 1))
  7483. (t (error "This should not happen"))))
  7484. ;;; Following file links
  7485. (defvar org-wait nil)
  7486. (defun org-open-file (path &optional in-emacs line search)
  7487. "Open the file at PATH.
  7488. First, this expands any special file name abbreviations. Then the
  7489. configuration variable `org-file-apps' is checked if it contains an
  7490. entry for this file type, and if yes, the corresponding command is launched.
  7491. If no application is found, Emacs simply visits the file.
  7492. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7493. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7494. and o use an external application to visit the file.
  7495. Optional LINE specifies a line to go to, optional SEARCH a string to
  7496. search for. If LINE or SEARCH is given, the file will always be
  7497. opened in Emacs.
  7498. If the file does not exist, an error is thrown."
  7499. (setq in-emacs (or in-emacs line search))
  7500. (let* ((file (if (equal path "")
  7501. buffer-file-name
  7502. (substitute-in-file-name (expand-file-name path))))
  7503. (apps (append org-file-apps (org-default-apps)))
  7504. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7505. (dirp (if remp nil (file-directory-p file)))
  7506. (file (if (and dirp org-open-directory-means-index-dot-org)
  7507. (concat (file-name-as-directory file) "index.org")
  7508. file))
  7509. (a-m-a-p (assq 'auto-mode apps))
  7510. (dfile (downcase file))
  7511. (old-buffer (current-buffer))
  7512. (old-pos (point))
  7513. (old-mode major-mode)
  7514. ext cmd)
  7515. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7516. (setq ext (match-string 1 dfile))
  7517. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7518. (setq ext (match-string 1 dfile))))
  7519. (cond
  7520. ((equal in-emacs '(16))
  7521. (setq cmd (cdr (assoc 'system apps))))
  7522. (in-emacs (setq cmd 'emacs))
  7523. (t
  7524. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7525. (and dirp (cdr (assoc 'directory apps)))
  7526. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7527. 'string-match)
  7528. (cdr (assoc ext apps))
  7529. (cdr (assoc t apps))))))
  7530. (when (eq cmd 'system)
  7531. (setq cmd (cdr (assoc 'system apps))))
  7532. (when (eq cmd 'default)
  7533. (setq cmd (cdr (assoc t apps))))
  7534. (when (eq cmd 'mailcap)
  7535. (require 'mailcap)
  7536. (mailcap-parse-mailcaps)
  7537. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7538. (command (mailcap-mime-info mime-type)))
  7539. (if (stringp command)
  7540. (setq cmd command)
  7541. (setq cmd 'emacs))))
  7542. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7543. (not (file-exists-p file))
  7544. (not org-open-non-existing-files))
  7545. (error "No such file: %s" file))
  7546. (cond
  7547. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7548. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7549. (while (string-match "['\"]%s['\"]" cmd)
  7550. (setq cmd (replace-match "%s" t t cmd)))
  7551. (while (string-match "%s" cmd)
  7552. (setq cmd (replace-match
  7553. (save-match-data
  7554. (shell-quote-argument
  7555. (convert-standard-filename file)))
  7556. t t cmd)))
  7557. (save-window-excursion
  7558. (start-process-shell-command cmd nil cmd)
  7559. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7560. ))
  7561. ((or (stringp cmd)
  7562. (eq cmd 'emacs))
  7563. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7564. (widen)
  7565. (if line (goto-line line)
  7566. (if search (org-link-search search))))
  7567. ((consp cmd)
  7568. (let ((file (convert-standard-filename file)))
  7569. (eval cmd)))
  7570. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7571. (and (org-mode-p) (eq old-mode 'org-mode)
  7572. (or (not (equal old-buffer (current-buffer)))
  7573. (not (equal old-pos (point))))
  7574. (org-mark-ring-push old-pos old-buffer))))
  7575. (defun org-default-apps ()
  7576. "Return the default applications for this operating system."
  7577. (cond
  7578. ((eq system-type 'darwin)
  7579. org-file-apps-defaults-macosx)
  7580. ((eq system-type 'windows-nt)
  7581. org-file-apps-defaults-windowsnt)
  7582. (t org-file-apps-defaults-gnu)))
  7583. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7584. "Convert extensions to regular expressions in the cars of LIST.
  7585. Also, weed out any non-string entries, because the return value is used
  7586. only for regexp matching.
  7587. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7588. point to the symbol `emacs', indicating that the file should
  7589. be opened in Emacs."
  7590. (append
  7591. (delq nil
  7592. (mapcar (lambda (x)
  7593. (if (not (stringp (car x)))
  7594. nil
  7595. (if (string-match "\\W" (car x))
  7596. x
  7597. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7598. list))
  7599. (if add-auto-mode
  7600. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7601. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7602. (defun org-file-remote-p (file)
  7603. "Test whether FILE specifies a location on a remote system.
  7604. Return non-nil if the location is indeed remote.
  7605. For example, the filename \"/user@host:/foo\" specifies a location
  7606. on the system \"/user@host:\"."
  7607. (cond ((fboundp 'file-remote-p)
  7608. (file-remote-p file))
  7609. ((fboundp 'tramp-handle-file-remote-p)
  7610. (tramp-handle-file-remote-p file))
  7611. ((and (boundp 'ange-ftp-name-format)
  7612. (string-match (car ange-ftp-name-format) file))
  7613. t)
  7614. (t nil)))
  7615. ;;;; Refiling
  7616. (defun org-get-org-file ()
  7617. "Read a filename, with default directory `org-directory'."
  7618. (let ((default (or org-default-notes-file remember-data-file)))
  7619. (read-file-name (format "File name [%s]: " default)
  7620. (file-name-as-directory org-directory)
  7621. default)))
  7622. (defun org-notes-order-reversed-p ()
  7623. "Check if the current file should receive notes in reversed order."
  7624. (cond
  7625. ((not org-reverse-note-order) nil)
  7626. ((eq t org-reverse-note-order) t)
  7627. ((not (listp org-reverse-note-order)) nil)
  7628. (t (catch 'exit
  7629. (let ((all org-reverse-note-order)
  7630. entry)
  7631. (while (setq entry (pop all))
  7632. (if (string-match (car entry) buffer-file-name)
  7633. (throw 'exit (cdr entry))))
  7634. nil)))))
  7635. (defvar org-refile-target-table nil
  7636. "The list of refile targets, created by `org-refile'.")
  7637. (defvar org-agenda-new-buffers nil
  7638. "Buffers created to visit agenda files.")
  7639. (defun org-get-refile-targets (&optional default-buffer)
  7640. "Produce a table with refile targets."
  7641. (let ((case-fold-search nil)
  7642. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7643. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7644. targets txt re files f desc descre fast-path-p level pos0)
  7645. (message "Getting targets...")
  7646. (with-current-buffer (or default-buffer (current-buffer))
  7647. (while (setq entry (pop entries))
  7648. (setq files (car entry) desc (cdr entry))
  7649. (setq fast-path-p nil)
  7650. (cond
  7651. ((null files) (setq files (list (current-buffer))))
  7652. ((eq files 'org-agenda-files)
  7653. (setq files (org-agenda-files 'unrestricted)))
  7654. ((and (symbolp files) (fboundp files))
  7655. (setq files (funcall files)))
  7656. ((and (symbolp files) (boundp files))
  7657. (setq files (symbol-value files))))
  7658. (if (stringp files) (setq files (list files)))
  7659. (cond
  7660. ((eq (car desc) :tag)
  7661. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7662. ((eq (car desc) :todo)
  7663. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7664. ((eq (car desc) :regexp)
  7665. (setq descre (cdr desc)))
  7666. ((eq (car desc) :level)
  7667. (setq descre (concat "^\\*\\{" (number-to-string
  7668. (if org-odd-levels-only
  7669. (1- (* 2 (cdr desc)))
  7670. (cdr desc)))
  7671. "\\}[ \t]")))
  7672. ((eq (car desc) :maxlevel)
  7673. (setq fast-path-p t)
  7674. (setq descre (concat "^\\*\\{1," (number-to-string
  7675. (if org-odd-levels-only
  7676. (1- (* 2 (cdr desc)))
  7677. (cdr desc)))
  7678. "\\}[ \t]")))
  7679. (t (error "Bad refiling target description %s" desc)))
  7680. (while (setq f (pop files))
  7681. (save-excursion
  7682. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7683. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7684. (setq f (expand-file-name f))
  7685. (if (eq org-refile-use-outline-path 'file)
  7686. (push (list (file-name-nondirectory f) f nil nil) targets))
  7687. (save-excursion
  7688. (save-restriction
  7689. (widen)
  7690. (goto-char (point-min))
  7691. (while (re-search-forward descre nil t)
  7692. (goto-char (setq pos0 (point-at-bol)))
  7693. (catch 'next
  7694. (when org-refile-target-verify-function
  7695. (save-match-data
  7696. (or (funcall org-refile-target-verify-function)
  7697. (throw 'next t))))
  7698. (when (looking-at org-complex-heading-regexp)
  7699. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7700. txt (org-link-display-format (match-string 4))
  7701. re (concat "^" (regexp-quote
  7702. (buffer-substring (match-beginning 1)
  7703. (match-end 4)))))
  7704. (if (match-end 5) (setq re (concat re "[ \t]+"
  7705. (regexp-quote
  7706. (match-string 5)))))
  7707. (setq re (concat re "[ \t]*$"))
  7708. (when org-refile-use-outline-path
  7709. (setq txt (mapconcat 'org-protect-slash
  7710. (append
  7711. (if (eq org-refile-use-outline-path 'file)
  7712. (list (file-name-nondirectory
  7713. (buffer-file-name (buffer-base-buffer))))
  7714. (if (eq org-refile-use-outline-path 'full-file-path)
  7715. (list (buffer-file-name (buffer-base-buffer)))))
  7716. (org-get-outline-path fast-path-p level txt)
  7717. (list txt))
  7718. "/")))
  7719. (push (list txt f re (point)) targets)))
  7720. (when (= (point) pos0)
  7721. ;; verification function has not moved point
  7722. (goto-char (point-at-eol))))))))))
  7723. (message "Getting targets...done")
  7724. (nreverse targets)))
  7725. (defun org-protect-slash (s)
  7726. (while (string-match "/" s)
  7727. (setq s (replace-match "\\" t t s)))
  7728. s)
  7729. (defvar org-olpa (make-vector 20 nil))
  7730. (defun org-get-outline-path (&optional fastp level heading)
  7731. "Return the outline path to the current entry, as a list."
  7732. (if fastp
  7733. (progn
  7734. (if (> level 19)
  7735. (error "Outline path failure, more than 19 levels."))
  7736. (loop for i from level upto 19 do
  7737. (aset org-olpa i nil))
  7738. (prog1
  7739. (delq nil (append org-olpa nil))
  7740. (aset org-olpa level heading)))
  7741. (let (rtn)
  7742. (save-excursion
  7743. (while (org-up-heading-safe)
  7744. (when (looking-at org-complex-heading-regexp)
  7745. (push (org-match-string-no-properties 4) rtn)))
  7746. rtn))))
  7747. (defvar org-refile-history nil
  7748. "History for refiling operations.")
  7749. (defvar org-after-refile-insert-hook nil
  7750. "Hook run after `org-refile' has inserted its stuff at the new location.
  7751. Note that this is still *before* the stuff will be removed from
  7752. the *old* location.")
  7753. (defun org-refile (&optional goto default-buffer rfloc)
  7754. "Move the entry at point to another heading.
  7755. The list of target headings is compiled using the information in
  7756. `org-refile-targets', which see. This list is created before each use
  7757. and will therefore always be up-to-date.
  7758. At the target location, the entry is filed as a subitem of the target heading.
  7759. Depending on `org-reverse-note-order', the new subitem will either be the
  7760. first or the last subitem.
  7761. If there is an active region, all entries in that region will be moved.
  7762. However, the region must fulfil the requirement that the first heading
  7763. is the first one sets the top-level of the moved text - at most siblings
  7764. below it are allowed.
  7765. With prefix arg GOTO, the command will only visit the target location,
  7766. not actually move anything.
  7767. With a double prefix `C-u C-u', go to the location where the last refiling
  7768. operation has put the subtree.
  7769. RFLOC can be a refile location obtained in a different way.
  7770. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7771. (interactive "P")
  7772. (let* ((cbuf (current-buffer))
  7773. (regionp (org-region-active-p))
  7774. (region-start (and regionp (region-beginning)))
  7775. (region-end (and regionp (region-end)))
  7776. (region-length (and regionp (- region-end region-start)))
  7777. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7778. pos it nbuf file re level reversed)
  7779. (when regionp
  7780. (goto-char region-start)
  7781. (or (bolp) (goto-char (point-at-bol)))
  7782. (setq region-start (point))
  7783. (unless (org-kill-is-subtree-p
  7784. (buffer-substring region-start region-end))
  7785. (error "The region is not a (sequence of) subtree(s)")))
  7786. (if (equal goto '(16))
  7787. (org-refile-goto-last-stored)
  7788. (when (setq it (or rfloc
  7789. (save-excursion
  7790. (org-refile-get-location
  7791. (if goto "Goto: " "Refile to: ") default-buffer
  7792. org-refile-allow-creating-parent-nodes))))
  7793. (setq file (nth 1 it)
  7794. re (nth 2 it)
  7795. pos (nth 3 it))
  7796. (if (and (not goto)
  7797. pos
  7798. (equal (buffer-file-name) file)
  7799. (if regionp
  7800. (and (>= pos region-start)
  7801. (<= pos region-end))
  7802. (and (>= pos (point))
  7803. (< pos (save-excursion
  7804. (org-end-of-subtree t t))))))
  7805. (error "Cannot refile to position inside the tree or region"))
  7806. (setq nbuf (or (find-buffer-visiting file)
  7807. (find-file-noselect file)))
  7808. (if goto
  7809. (progn
  7810. (switch-to-buffer nbuf)
  7811. (goto-char pos)
  7812. (org-show-context 'org-goto))
  7813. (if regionp
  7814. (progn
  7815. (org-kill-new (buffer-substring region-start region-end))
  7816. (org-save-markers-in-region region-start region-end))
  7817. (org-copy-subtree 1 nil t))
  7818. (save-excursion
  7819. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7820. (find-file-noselect file))))
  7821. (setq reversed (org-notes-order-reversed-p))
  7822. (save-excursion
  7823. (save-restriction
  7824. (widen)
  7825. (if pos
  7826. (progn
  7827. (goto-char pos)
  7828. (looking-at outline-regexp)
  7829. (setq level (org-get-valid-level (funcall outline-level) 1))
  7830. (goto-char
  7831. (if reversed
  7832. (or (outline-next-heading) (point-max))
  7833. (or (save-excursion (outline-get-next-sibling))
  7834. (org-end-of-subtree t t)
  7835. (point-max)))))
  7836. (setq level 1)
  7837. (if (not reversed)
  7838. (goto-char (point-max))
  7839. (goto-char (point-min))
  7840. (or (outline-next-heading) (goto-char (point-max)))))
  7841. (if (not (bolp)) (newline))
  7842. (bookmark-set "org-refile-last-stored")
  7843. (org-paste-subtree level)
  7844. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7845. (run-hooks 'org-after-refile-insert-hook))))
  7846. (if regionp
  7847. (delete-region (point) (+ (point) region-length))
  7848. (org-cut-subtree))
  7849. (when (featurep 'org-inlinetask)
  7850. (org-inlinetask-remove-END-maybe))
  7851. (setq org-markers-to-move nil)
  7852. (message "Refiled to \"%s\"" (car it))))))
  7853. (org-reveal))
  7854. (defun org-refile-goto-last-stored ()
  7855. "Go to the location where the last refile was stored."
  7856. (interactive)
  7857. (bookmark-jump "org-refile-last-stored")
  7858. (message "This is the location of the last refile"))
  7859. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7860. "Prompt the user for a refile location, using PROMPT."
  7861. (let ((org-refile-targets org-refile-targets)
  7862. (org-refile-use-outline-path org-refile-use-outline-path))
  7863. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7864. (unless org-refile-target-table
  7865. (error "No refile targets"))
  7866. (let* ((cbuf (current-buffer))
  7867. (partial-completion-mode nil)
  7868. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7869. (cfunc (if (and org-refile-use-outline-path
  7870. org-outline-path-complete-in-steps)
  7871. 'org-olpath-completing-read
  7872. 'org-icompleting-read))
  7873. (extra (if org-refile-use-outline-path "/" ""))
  7874. (filename (and cfn (expand-file-name cfn)))
  7875. (tbl (mapcar
  7876. (lambda (x)
  7877. (if (and (not (member org-refile-use-outline-path
  7878. '(file full-file-path)))
  7879. (not (equal filename (nth 1 x))))
  7880. (cons (concat (car x) extra " ("
  7881. (file-name-nondirectory (nth 1 x)) ")")
  7882. (cdr x))
  7883. (cons (concat (car x) extra) (cdr x))))
  7884. org-refile-target-table))
  7885. (completion-ignore-case t)
  7886. pa answ parent-target child parent old-hist)
  7887. (setq old-hist org-refile-history)
  7888. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7889. nil 'org-refile-history))
  7890. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7891. (if pa
  7892. (progn
  7893. (when (or (not org-refile-history)
  7894. (not (eq old-hist org-refile-history))
  7895. (not (equal (car pa) (car org-refile-history))))
  7896. (setq org-refile-history
  7897. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  7898. org-refile-history
  7899. (cdr org-refile-history))))
  7900. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  7901. (pop org-refile-history)))
  7902. pa)
  7903. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7904. (setq parent (match-string 1 answ)
  7905. child (match-string 2 answ))
  7906. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7907. (when (and parent-target
  7908. (or (eq new-nodes t)
  7909. (and (eq new-nodes 'confirm)
  7910. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7911. (org-refile-new-child parent-target child))))))
  7912. (defun org-refile-new-child (parent-target child)
  7913. "Use refile target PARENT-TARGET to add new CHILD below it."
  7914. (unless parent-target
  7915. (error "Cannot find parent for new node"))
  7916. (let ((file (nth 1 parent-target))
  7917. (pos (nth 3 parent-target))
  7918. level)
  7919. (with-current-buffer (or (find-buffer-visiting file)
  7920. (find-file-noselect file))
  7921. (save-excursion
  7922. (save-restriction
  7923. (widen)
  7924. (if pos
  7925. (goto-char pos)
  7926. (goto-char (point-max))
  7927. (if (not (bolp)) (newline)))
  7928. (when (looking-at outline-regexp)
  7929. (setq level (funcall outline-level))
  7930. (org-end-of-subtree t t))
  7931. (org-back-over-empty-lines)
  7932. (insert "\n" (make-string
  7933. (if pos (org-get-valid-level level 1) 1) ?*)
  7934. " " child "\n")
  7935. (beginning-of-line 0)
  7936. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7937. (defun org-olpath-completing-read (prompt collection &rest args)
  7938. "Read an outline path like a file name."
  7939. (let ((thetable collection)
  7940. (org-completion-use-ido nil) ; does not work with ido.
  7941. (org-completion-use-iswitchb nil)) ; or iswitchb
  7942. (apply
  7943. 'org-icompleting-read prompt
  7944. (lambda (string predicate &optional flag)
  7945. (let (rtn r f (l (length string)))
  7946. (cond
  7947. ((eq flag nil)
  7948. ;; try completion
  7949. (try-completion string thetable))
  7950. ((eq flag t)
  7951. ;; all-completions
  7952. (setq rtn (all-completions string thetable predicate))
  7953. (mapcar
  7954. (lambda (x)
  7955. (setq r (substring x l))
  7956. (if (string-match " ([^)]*)$" x)
  7957. (setq f (match-string 0 x))
  7958. (setq f ""))
  7959. (if (string-match "/" r)
  7960. (concat string (substring r 0 (match-end 0)) f)
  7961. x))
  7962. rtn))
  7963. ((eq flag 'lambda)
  7964. ;; exact match?
  7965. (assoc string thetable)))
  7966. ))
  7967. args)))
  7968. ;;;; Dynamic blocks
  7969. (defun org-find-dblock (name)
  7970. "Find the first dynamic block with name NAME in the buffer.
  7971. If not found, stay at current position and return nil."
  7972. (let (pos)
  7973. (save-excursion
  7974. (goto-char (point-min))
  7975. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7976. nil t)
  7977. (match-beginning 0))))
  7978. (if pos (goto-char pos))
  7979. pos))
  7980. (defconst org-dblock-start-re
  7981. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7982. "Matches the startline of a dynamic block, with parameters.")
  7983. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7984. "Matches the end of a dynamic block.")
  7985. (defun org-create-dblock (plist)
  7986. "Create a dynamic block section, with parameters taken from PLIST.
  7987. PLIST must contain a :name entry which is used as name of the block."
  7988. (unless (bolp) (newline))
  7989. (let ((name (plist-get plist :name)))
  7990. (insert "#+BEGIN: " name)
  7991. (while plist
  7992. (if (eq (car plist) :name)
  7993. (setq plist (cddr plist))
  7994. (insert " " (prin1-to-string (pop plist)))))
  7995. (insert "\n\n#+END:\n")
  7996. (beginning-of-line -2)))
  7997. (defun org-prepare-dblock ()
  7998. "Prepare dynamic block for refresh.
  7999. This empties the block, puts the cursor at the insert position and returns
  8000. the property list including an extra property :name with the block name."
  8001. (unless (looking-at org-dblock-start-re)
  8002. (error "Not at a dynamic block"))
  8003. (let* ((begdel (1+ (match-end 0)))
  8004. (name (org-no-properties (match-string 1)))
  8005. (params (append (list :name name)
  8006. (read (concat "(" (match-string 3) ")")))))
  8007. (unless (re-search-forward org-dblock-end-re nil t)
  8008. (error "Dynamic block not terminated"))
  8009. (setq params
  8010. (append params
  8011. (list :content (buffer-substring
  8012. begdel (match-beginning 0)))))
  8013. (delete-region begdel (match-beginning 0))
  8014. (goto-char begdel)
  8015. (open-line 1)
  8016. params))
  8017. (defun org-map-dblocks (&optional command)
  8018. "Apply COMMAND to all dynamic blocks in the current buffer.
  8019. If COMMAND is not given, use `org-update-dblock'."
  8020. (let ((cmd (or command 'org-update-dblock))
  8021. pos)
  8022. (save-excursion
  8023. (goto-char (point-min))
  8024. (while (re-search-forward org-dblock-start-re nil t)
  8025. (goto-char (setq pos (match-beginning 0)))
  8026. (condition-case nil
  8027. (funcall cmd)
  8028. (error (message "Error during update of dynamic block")))
  8029. (goto-char pos)
  8030. (unless (re-search-forward org-dblock-end-re nil t)
  8031. (error "Dynamic block not terminated"))))))
  8032. (defun org-dblock-update (&optional arg)
  8033. "User command for updating dynamic blocks.
  8034. Update the dynamic block at point. With prefix ARG, update all dynamic
  8035. blocks in the buffer."
  8036. (interactive "P")
  8037. (if arg
  8038. (org-update-all-dblocks)
  8039. (or (looking-at org-dblock-start-re)
  8040. (org-beginning-of-dblock))
  8041. (org-update-dblock)))
  8042. (defun org-update-dblock ()
  8043. "Update the dynamic block at point
  8044. This means to empty the block, parse for parameters and then call
  8045. the correct writing function."
  8046. (save-window-excursion
  8047. (let* ((pos (point))
  8048. (line (org-current-line))
  8049. (params (org-prepare-dblock))
  8050. (name (plist-get params :name))
  8051. (cmd (intern (concat "org-dblock-write:" name))))
  8052. (message "Updating dynamic block `%s' at line %d..." name line)
  8053. (funcall cmd params)
  8054. (message "Updating dynamic block `%s' at line %d...done" name line)
  8055. (goto-char pos))))
  8056. (defun org-beginning-of-dblock ()
  8057. "Find the beginning of the dynamic block at point.
  8058. Error if there is no such block at point."
  8059. (let ((pos (point))
  8060. beg)
  8061. (end-of-line 1)
  8062. (if (and (re-search-backward org-dblock-start-re nil t)
  8063. (setq beg (match-beginning 0))
  8064. (re-search-forward org-dblock-end-re nil t)
  8065. (> (match-end 0) pos))
  8066. (goto-char beg)
  8067. (goto-char pos)
  8068. (error "Not in a dynamic block"))))
  8069. (defun org-update-all-dblocks ()
  8070. "Update all dynamic blocks in the buffer.
  8071. This function can be used in a hook."
  8072. (when (org-mode-p)
  8073. (org-map-dblocks 'org-update-dblock)))
  8074. ;;;; Completion
  8075. (defconst org-additional-option-like-keywords
  8076. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8077. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8078. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  8079. "BEGIN:" "END:"
  8080. "ORGTBL" "TBLFM:" "TBLNAME:"
  8081. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8082. "BEGIN_QUOTE" "END_QUOTE"
  8083. "BEGIN_VERSE" "END_VERSE"
  8084. "BEGIN_CENTER" "END_CENTER"
  8085. "BEGIN_SRC" "END_SRC"
  8086. "CATEGORY" "COLUMNS"
  8087. "CAPTION" "LABEL"
  8088. "BIND"
  8089. "MACRO"))
  8090. (defcustom org-structure-template-alist
  8091. '(
  8092. ("s" "#+begin_src ?\n\n#+end_src"
  8093. "<src lang=\"?\">\n\n</src>")
  8094. ("e" "#+begin_example\n?\n#+end_example"
  8095. "<example>\n?\n</example>")
  8096. ("q" "#+begin_quote\n?\n#+end_quote"
  8097. "<quote>\n?\n</quote>")
  8098. ("v" "#+begin_verse\n?\n#+end_verse"
  8099. "<verse>\n?\n/verse>")
  8100. ("c" "#+begin_center\n?\n#+end_center"
  8101. "<center>\n?\n/center>")
  8102. ("l" "#+begin_latex\n?\n#+end_latex"
  8103. "<literal style=\"latex\">\n?\n</literal>")
  8104. ("L" "#+latex: "
  8105. "<literal style=\"latex\">?</literal>")
  8106. ("h" "#+begin_html\n?\n#+end_html"
  8107. "<literal style=\"html\">\n?\n</literal>")
  8108. ("H" "#+html: "
  8109. "<literal style=\"html\">?</literal>")
  8110. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8111. ("A" "#+ascii: ")
  8112. ("i" "#+include %file ?"
  8113. "<include file=%file markup=\"?\">")
  8114. )
  8115. "Structure completion elements.
  8116. This is a list of abbreviation keys and values. The value gets inserted
  8117. it you type @samp{.} followed by the key and then the completion key,
  8118. usually `M-TAB'. %file will be replaced by a file name after prompting
  8119. for the file using completion.
  8120. There are two templates for each key, the first uses the original Org syntax,
  8121. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8122. the default when the /org-mtags.el/ module has been loaded. See also the
  8123. variable `org-mtags-prefer-muse-templates'.
  8124. This is an experimental feature, it is undecided if it is going to stay in."
  8125. :group 'org-completion
  8126. :type '(repeat
  8127. (string :tag "Key")
  8128. (string :tag "Template")
  8129. (string :tag "Muse Template")))
  8130. (defun org-try-structure-completion ()
  8131. "Try to complete a structure template before point.
  8132. This looks for strings like \"<e\" on an otherwise empty line and
  8133. expands them."
  8134. (let ((l (buffer-substring (point-at-bol) (point)))
  8135. a)
  8136. (when (and (looking-at "[ \t]*$")
  8137. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8138. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8139. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8140. (match-beginning 1)) a)
  8141. t)))
  8142. (defun org-complete-expand-structure-template (start cell)
  8143. "Expand a structure template."
  8144. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8145. (rpl (nth (if musep 2 1) cell))
  8146. (ind ""))
  8147. (delete-region start (point))
  8148. (when (string-match "\\`#\\+" rpl)
  8149. (cond
  8150. ((bolp))
  8151. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8152. (setq ind (buffer-substring (point-at-bol) (point))))
  8153. (t (newline))))
  8154. (setq start (point))
  8155. (if (string-match "%file" rpl)
  8156. (setq rpl (replace-match
  8157. (concat
  8158. "\""
  8159. (save-match-data
  8160. (abbreviate-file-name (read-file-name "Include file: ")))
  8161. "\"")
  8162. t t rpl)))
  8163. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8164. (concat "\n" ind)))
  8165. (insert rpl)
  8166. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8167. (defun org-complete (&optional arg)
  8168. "Perform completion on word at point.
  8169. At the beginning of a headline, this completes TODO keywords as given in
  8170. `org-todo-keywords'.
  8171. If the current word is preceded by a backslash, completes the TeX symbols
  8172. that are supported for HTML support.
  8173. If the current word is preceded by \"#+\", completes special words for
  8174. setting file options.
  8175. In the line after \"#+STARTUP:, complete valid keywords.\"
  8176. At all other locations, this simply calls the value of
  8177. `org-completion-fallback-command'."
  8178. (interactive "P")
  8179. (org-without-partial-completion
  8180. (catch 'exit
  8181. (let* ((a nil)
  8182. (end (point))
  8183. (beg1 (save-excursion
  8184. (skip-chars-backward (org-re "[:alnum:]_@"))
  8185. (point)))
  8186. (beg (save-excursion
  8187. (skip-chars-backward "a-zA-Z0-9_:$")
  8188. (point)))
  8189. (confirm (lambda (x) (stringp (car x))))
  8190. (searchhead (equal (char-before beg) ?*))
  8191. (struct
  8192. (when (and (member (char-before beg1) '(?. ?<))
  8193. (setq a (assoc (buffer-substring beg1 (point))
  8194. org-structure-template-alist)))
  8195. (org-complete-expand-structure-template (1- beg1) a)
  8196. (throw 'exit t)))
  8197. (tag (and (equal (char-before beg1) ?:)
  8198. (equal (char-after (point-at-bol)) ?*)))
  8199. (prop (and (equal (char-before beg1) ?:)
  8200. (not (equal (char-after (point-at-bol)) ?*))))
  8201. (texp (equal (char-before beg) ?\\))
  8202. (link (equal (char-before beg) ?\[))
  8203. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8204. beg)
  8205. "#+"))
  8206. (startup (string-match "^#\\+STARTUP:.*"
  8207. (buffer-substring (point-at-bol) (point))))
  8208. (completion-ignore-case opt)
  8209. (type nil)
  8210. (tbl nil)
  8211. (table (cond
  8212. (opt
  8213. (setq type :opt)
  8214. (require 'org-exp)
  8215. (append
  8216. (delq nil
  8217. (mapcar
  8218. (lambda (x)
  8219. (if (string-match
  8220. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8221. (cons (match-string 2 x)
  8222. (match-string 1 x))))
  8223. (org-split-string (org-get-current-options) "\n")))
  8224. (mapcar 'list org-additional-option-like-keywords)))
  8225. (startup
  8226. (setq type :startup)
  8227. org-startup-options)
  8228. (link (append org-link-abbrev-alist-local
  8229. org-link-abbrev-alist))
  8230. (texp
  8231. (setq type :tex)
  8232. org-html-entities)
  8233. ((string-match "\\`\\*+[ \t]+\\'"
  8234. (buffer-substring (point-at-bol) beg))
  8235. (setq type :todo)
  8236. (mapcar 'list org-todo-keywords-1))
  8237. (searchhead
  8238. (setq type :searchhead)
  8239. (save-excursion
  8240. (goto-char (point-min))
  8241. (while (re-search-forward org-todo-line-regexp nil t)
  8242. (push (list
  8243. (org-make-org-heading-search-string
  8244. (match-string 3) t))
  8245. tbl)))
  8246. tbl)
  8247. (tag (setq type :tag beg beg1)
  8248. (or org-tag-alist (org-get-buffer-tags)))
  8249. (prop (setq type :prop beg beg1)
  8250. (mapcar 'list (org-buffer-property-keys nil t t)))
  8251. (t (progn
  8252. (call-interactively org-completion-fallback-command)
  8253. (throw 'exit nil)))))
  8254. (pattern (buffer-substring-no-properties beg end))
  8255. (completion (try-completion pattern table confirm)))
  8256. (cond ((eq completion t)
  8257. (if (not (assoc (upcase pattern) table))
  8258. (message "Already complete")
  8259. (if (and (equal type :opt)
  8260. (not (member (car (assoc (upcase pattern) table))
  8261. org-additional-option-like-keywords)))
  8262. (insert (substring (cdr (assoc (upcase pattern) table))
  8263. (length pattern)))
  8264. (if (memq type '(:tag :prop)) (insert ":")))))
  8265. ((null completion)
  8266. (message "Can't find completion for \"%s\"" pattern)
  8267. (ding))
  8268. ((not (string= pattern completion))
  8269. (delete-region beg end)
  8270. (if (string-match " +$" completion)
  8271. (setq completion (replace-match "" t t completion)))
  8272. (insert completion)
  8273. (if (get-buffer-window "*Completions*")
  8274. (delete-window (get-buffer-window "*Completions*")))
  8275. (if (assoc completion table)
  8276. (if (eq type :todo) (insert " ")
  8277. (if (memq type '(:tag :prop)) (insert ":"))))
  8278. (if (and (equal type :opt) (assoc completion table))
  8279. (message "%s" (substitute-command-keys
  8280. "Press \\[org-complete] again to insert example settings"))))
  8281. (t
  8282. (message "Making completion list...")
  8283. (let ((list (sort (all-completions pattern table confirm)
  8284. 'string<)))
  8285. (with-output-to-temp-buffer "*Completions*"
  8286. (condition-case nil
  8287. ;; Protection needed for XEmacs and emacs 21
  8288. (display-completion-list list pattern)
  8289. (error (display-completion-list list)))))
  8290. (message "Making completion list...%s" "done")))))))
  8291. ;;;; TODO, DEADLINE, Comments
  8292. (defun org-toggle-comment ()
  8293. "Change the COMMENT state of an entry."
  8294. (interactive)
  8295. (save-excursion
  8296. (org-back-to-heading)
  8297. (let (case-fold-search)
  8298. (if (looking-at (concat outline-regexp
  8299. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8300. (replace-match "" t t nil 1)
  8301. (if (looking-at outline-regexp)
  8302. (progn
  8303. (goto-char (match-end 0))
  8304. (insert org-comment-string " ")))))))
  8305. (defvar org-last-todo-state-is-todo nil
  8306. "This is non-nil when the last TODO state change led to a TODO state.
  8307. If the last change removed the TODO tag or switched to DONE, then
  8308. this is nil.")
  8309. (defvar org-setting-tags nil) ; dynamically skipped
  8310. (defun org-parse-local-options (string var)
  8311. "Parse STRING for startup setting relevant for variable VAR."
  8312. (let ((rtn (symbol-value var))
  8313. e opts)
  8314. (save-match-data
  8315. (if (or (not string) (not (string-match "\\S-" string)))
  8316. rtn
  8317. (setq opts (delq nil (mapcar (lambda (x)
  8318. (setq e (assoc x org-startup-options))
  8319. (if (eq (nth 1 e) var) e nil))
  8320. (org-split-string string "[ \t]+"))))
  8321. (if (not opts)
  8322. rtn
  8323. (setq rtn nil)
  8324. (while (setq e (pop opts))
  8325. (if (not (nth 3 e))
  8326. (setq rtn (nth 2 e))
  8327. (if (not (listp rtn)) (setq rtn nil))
  8328. (push (nth 2 e) rtn)))
  8329. rtn)))))
  8330. (defvar org-todo-setup-filter-hook nil
  8331. "Hook for functions that pre-filter todo specs.
  8332. Each function takes a todo spec and returns either `nil' or the spec
  8333. transformed into canonical form." )
  8334. (defvar org-todo-get-default-hook nil
  8335. "Hook for functions that get a default item for todo.
  8336. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8337. `nil' or a string to be used for the todo mark." )
  8338. (defvar org-agenda-headline-snapshot-before-repeat)
  8339. (defun org-todo (&optional arg)
  8340. "Change the TODO state of an item.
  8341. The state of an item is given by a keyword at the start of the heading,
  8342. like
  8343. *** TODO Write paper
  8344. *** DONE Call mom
  8345. The different keywords are specified in the variable `org-todo-keywords'.
  8346. By default the available states are \"TODO\" and \"DONE\".
  8347. So for this example: when the item starts with TODO, it is changed to DONE.
  8348. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8349. DONE are present, add TODO at the beginning of the heading.
  8350. With C-u prefix arg, use completion to determine the new state.
  8351. With numeric prefix arg, switch to that state.
  8352. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8353. With a tripple C-u prefix, circumvent any state blocking.
  8354. For calling through lisp, arg is also interpreted in the following way:
  8355. 'none -> empty state
  8356. \"\"(empty string) -> switch to empty state
  8357. 'done -> switch to DONE
  8358. 'nextset -> switch to the next set of keywords
  8359. 'previousset -> switch to the previous set of keywords
  8360. \"WAITING\" -> switch to the specified keyword, but only if it
  8361. really is a member of `org-todo-keywords'."
  8362. (interactive "P")
  8363. (if (equal arg '(16)) (setq arg 'nextset))
  8364. (let ((org-blocker-hook org-blocker-hook)
  8365. (case-fold-search nil))
  8366. (when (equal arg '(64))
  8367. (setq arg nil org-blocker-hook nil))
  8368. (when (and org-blocker-hook
  8369. (or org-inhibit-blocking
  8370. (org-entry-get nil "NOBLOCKING")))
  8371. (setq org-blocker-hook nil))
  8372. (save-excursion
  8373. (catch 'exit
  8374. (org-back-to-heading)
  8375. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8376. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8377. (looking-at " *"))
  8378. (let* ((match-data (match-data))
  8379. (startpos (point-at-bol))
  8380. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8381. (org-log-done org-log-done)
  8382. (org-log-repeat org-log-repeat)
  8383. (org-todo-log-states org-todo-log-states)
  8384. (this (match-string 1))
  8385. (hl-pos (match-beginning 0))
  8386. (head (org-get-todo-sequence-head this))
  8387. (ass (assoc head org-todo-kwd-alist))
  8388. (interpret (nth 1 ass))
  8389. (done-word (nth 3 ass))
  8390. (final-done-word (nth 4 ass))
  8391. (last-state (or this ""))
  8392. (completion-ignore-case t)
  8393. (member (member this org-todo-keywords-1))
  8394. (tail (cdr member))
  8395. (state (cond
  8396. ((and org-todo-key-trigger
  8397. (or (and (equal arg '(4))
  8398. (eq org-use-fast-todo-selection 'prefix))
  8399. (and (not arg) org-use-fast-todo-selection
  8400. (not (eq org-use-fast-todo-selection
  8401. 'prefix)))))
  8402. ;; Use fast selection
  8403. (org-fast-todo-selection))
  8404. ((and (equal arg '(4))
  8405. (or (not org-use-fast-todo-selection)
  8406. (not org-todo-key-trigger)))
  8407. ;; Read a state with completion
  8408. (org-icompleting-read
  8409. "State: " (mapcar (lambda(x) (list x))
  8410. org-todo-keywords-1)
  8411. nil t))
  8412. ((eq arg 'right)
  8413. (if this
  8414. (if tail (car tail) nil)
  8415. (car org-todo-keywords-1)))
  8416. ((eq arg 'left)
  8417. (if (equal member org-todo-keywords-1)
  8418. nil
  8419. (if this
  8420. (nth (- (length org-todo-keywords-1)
  8421. (length tail) 2)
  8422. org-todo-keywords-1)
  8423. (org-last org-todo-keywords-1))))
  8424. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8425. (setq arg nil))) ; hack to fall back to cycling
  8426. (arg
  8427. ;; user or caller requests a specific state
  8428. (cond
  8429. ((equal arg "") nil)
  8430. ((eq arg 'none) nil)
  8431. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8432. ((eq arg 'nextset)
  8433. (or (car (cdr (member head org-todo-heads)))
  8434. (car org-todo-heads)))
  8435. ((eq arg 'previousset)
  8436. (let ((org-todo-heads (reverse org-todo-heads)))
  8437. (or (car (cdr (member head org-todo-heads)))
  8438. (car org-todo-heads))))
  8439. ((car (member arg org-todo-keywords-1)))
  8440. ((nth (1- (prefix-numeric-value arg))
  8441. org-todo-keywords-1))))
  8442. ((null member) (or head (car org-todo-keywords-1)))
  8443. ((equal this final-done-word) nil) ;; -> make empty
  8444. ((null tail) nil) ;; -> first entry
  8445. ((memq interpret '(type priority))
  8446. (if (eq this-command last-command)
  8447. (car tail)
  8448. (if (> (length tail) 0)
  8449. (or done-word (car org-done-keywords))
  8450. nil)))
  8451. (t
  8452. (car tail))))
  8453. (state (or
  8454. (run-hook-with-args-until-success
  8455. 'org-todo-get-default-hook state last-state)
  8456. state))
  8457. (next (if state (concat " " state " ") " "))
  8458. (change-plist (list :type 'todo-state-change :from this :to state
  8459. :position startpos))
  8460. dolog now-done-p)
  8461. (when org-blocker-hook
  8462. (setq org-last-todo-state-is-todo
  8463. (not (member this org-done-keywords)))
  8464. (unless (save-excursion
  8465. (save-match-data
  8466. (run-hook-with-args-until-failure
  8467. 'org-blocker-hook change-plist)))
  8468. (if (interactive-p)
  8469. (error "TODO state change from %s to %s blocked" this state)
  8470. ;; fail silently
  8471. (message "TODO state change from %s to %s blocked" this state)
  8472. (throw 'exit nil))))
  8473. (store-match-data match-data)
  8474. (replace-match next t t)
  8475. (unless (pos-visible-in-window-p hl-pos)
  8476. (message "TODO state changed to %s" (org-trim next)))
  8477. (unless head
  8478. (setq head (org-get-todo-sequence-head state)
  8479. ass (assoc head org-todo-kwd-alist)
  8480. interpret (nth 1 ass)
  8481. done-word (nth 3 ass)
  8482. final-done-word (nth 4 ass)))
  8483. (when (memq arg '(nextset previousset))
  8484. (message "Keyword-Set %d/%d: %s"
  8485. (- (length org-todo-sets) -1
  8486. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8487. (length org-todo-sets)
  8488. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8489. (setq org-last-todo-state-is-todo
  8490. (not (member state org-done-keywords)))
  8491. (setq now-done-p (and (member state org-done-keywords)
  8492. (not (member this org-done-keywords))))
  8493. (and logging (org-local-logging logging))
  8494. (when (and (or org-todo-log-states org-log-done)
  8495. (not (eq org-inhibit-logging t))
  8496. (not (memq arg '(nextset previousset))))
  8497. ;; we need to look at recording a time and note
  8498. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8499. (nth 2 (assoc this org-todo-log-states))))
  8500. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8501. (setq dolog 'time))
  8502. (when (and state
  8503. (member state org-not-done-keywords)
  8504. (not (member this org-not-done-keywords)))
  8505. ;; This is now a todo state and was not one before
  8506. ;; If there was a CLOSED time stamp, get rid of it.
  8507. (org-add-planning-info nil nil 'closed))
  8508. (when (and now-done-p org-log-done)
  8509. ;; It is now done, and it was not done before
  8510. (org-add-planning-info 'closed (org-current-time))
  8511. (if (and (not dolog) (eq 'note org-log-done))
  8512. (org-add-log-setup 'done state this 'findpos 'note)))
  8513. (when (and state dolog)
  8514. ;; This is a non-nil state, and we need to log it
  8515. (org-add-log-setup 'state state this 'findpos dolog)))
  8516. ;; Fixup tag positioning
  8517. (org-todo-trigger-tag-changes state)
  8518. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8519. (when org-provide-todo-statistics
  8520. (org-update-parent-todo-statistics))
  8521. (run-hooks 'org-after-todo-state-change-hook)
  8522. (if (and arg (not (member state org-done-keywords)))
  8523. (setq head (org-get-todo-sequence-head state)))
  8524. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8525. ;; Do we need to trigger a repeat?
  8526. (when now-done-p
  8527. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8528. ;; This is for the agenda, take a snapshot of the headline.
  8529. (save-match-data
  8530. (setq org-agenda-headline-snapshot-before-repeat
  8531. (org-get-heading))))
  8532. (org-auto-repeat-maybe state))
  8533. ;; Fixup cursor location if close to the keyword
  8534. (if (and (outline-on-heading-p)
  8535. (not (bolp))
  8536. (save-excursion (beginning-of-line 1)
  8537. (looking-at org-todo-line-regexp))
  8538. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8539. (progn
  8540. (goto-char (or (match-end 2) (match-end 1)))
  8541. (and (looking-at " ") (just-one-space))))
  8542. (when org-trigger-hook
  8543. (save-excursion
  8544. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8545. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8546. "Block turning an entry into a TODO, using the hierarchy.
  8547. This checks whether the current task should be blocked from state
  8548. changes. Such blocking occurs when:
  8549. 1. The task has children which are not all in a completed state.
  8550. 2. A task has a parent with the property :ORDERED:, and there
  8551. are siblings prior to the current task with incomplete
  8552. status.
  8553. 3. The parent of the task is blocked because it has siblings that should
  8554. be done first, or is child of a block grandparent TODO entry."
  8555. (catch 'dont-block
  8556. ;; If this is not a todo state change, or if this entry is already DONE,
  8557. ;; do not block
  8558. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8559. (member (plist-get change-plist :from)
  8560. (cons 'done org-done-keywords))
  8561. (member (plist-get change-plist :to)
  8562. (cons 'todo org-not-done-keywords)))
  8563. (throw 'dont-block t))
  8564. ;; If this task has children, and any are undone, it's blocked
  8565. (save-excursion
  8566. (org-back-to-heading t)
  8567. (let ((this-level (funcall outline-level)))
  8568. (outline-next-heading)
  8569. (let ((child-level (funcall outline-level)))
  8570. (while (and (not (eobp))
  8571. (> child-level this-level))
  8572. ;; this todo has children, check whether they are all
  8573. ;; completed
  8574. (if (and (not (org-entry-is-done-p))
  8575. (org-entry-is-todo-p))
  8576. (throw 'dont-block nil))
  8577. (outline-next-heading)
  8578. (setq child-level (funcall outline-level))))))
  8579. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8580. ;; any previous siblings are undone, it's blocked
  8581. (save-excursion
  8582. (org-back-to-heading t)
  8583. (let* ((pos (point))
  8584. (parent-pos (and (org-up-heading-safe) (point))))
  8585. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8586. (when (and (org-entry-get (point) "ORDERED")
  8587. (forward-line 1)
  8588. (re-search-forward org-not-done-heading-regexp pos t))
  8589. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8590. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8591. (while t
  8592. (goto-char parent-pos)
  8593. (if (not (looking-at org-not-done-heading-regexp))
  8594. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8595. (setq pos (point))
  8596. (setq parent-pos (and (org-up-heading-safe) (point)))
  8597. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8598. (when (and (org-entry-get (point) "ORDERED")
  8599. (forward-line 1)
  8600. (re-search-forward org-not-done-heading-regexp pos t))
  8601. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8602. (defcustom org-track-ordered-property-with-tag nil
  8603. "Should the ORDERED property also be shown as a tag?
  8604. The ORDERED property decides if an entry should require subtasks to be
  8605. completed in sequence. Since a property is not very visible, setting
  8606. this option means that toggling the ORDERED property with the command
  8607. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8608. not relevant for the behavior, but it makes things more visible.
  8609. Note that toggling the tag with tags commands will not change the property
  8610. and therefore not influence behavior!
  8611. This can be t, meaning the tag ORDERED should be used, It can also be a
  8612. string to select a different tag for this task."
  8613. :group 'org-todo
  8614. :type '(choice
  8615. (const :tag "No tracking" nil)
  8616. (const :tag "Track with ORDERED tag" t)
  8617. (string :tag "Use other tag")))
  8618. (defun org-toggle-ordered-property ()
  8619. "Toggle the ORDERED property of the current entry.
  8620. For better visibility, you can track the value of this property with a tag.
  8621. See variable `org-track-ordered-property-with-tag'."
  8622. (interactive)
  8623. (let* ((t1 org-track-ordered-property-with-tag)
  8624. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8625. (save-excursion
  8626. (org-back-to-heading)
  8627. (if (org-entry-get nil "ORDERED")
  8628. (progn
  8629. (org-delete-property "ORDERED")
  8630. (and tag (org-toggle-tag tag 'off))
  8631. (message "Subtasks can be completed in arbitrary order"))
  8632. (org-entry-put nil "ORDERED" "t")
  8633. (and tag (org-toggle-tag tag 'on))
  8634. (message "Subtasks must be completed in sequence")))))
  8635. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8636. (defun org-block-todo-from-checkboxes (change-plist)
  8637. "Block turning an entry into a TODO, using checkboxes.
  8638. This checks whether the current task should be blocked from state
  8639. changes because there are uncheckd boxes in this entry."
  8640. (catch 'dont-block
  8641. ;; If this is not a todo state change, or if this entry is already DONE,
  8642. ;; do not block
  8643. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8644. (member (plist-get change-plist :from)
  8645. (cons 'done org-done-keywords))
  8646. (member (plist-get change-plist :to)
  8647. (cons 'todo org-not-done-keywords)))
  8648. (throw 'dont-block t))
  8649. ;; If this task has checkboxes that are not checked, it's blocked
  8650. (save-excursion
  8651. (org-back-to-heading t)
  8652. (let ((beg (point)) end)
  8653. (outline-next-heading)
  8654. (setq end (point))
  8655. (goto-char beg)
  8656. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8657. end t)
  8658. (progn
  8659. (if (boundp 'org-blocked-by-checkboxes)
  8660. (setq org-blocked-by-checkboxes t))
  8661. (throw 'dont-block nil)))))
  8662. t)) ; do not block
  8663. (defvar org-entry-property-inherited-from) ;; defined below
  8664. (defun org-update-parent-todo-statistics ()
  8665. "Update any statistics cookie in the parent of the current headline.
  8666. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8667. the entire subtree and this will travel up the hierarchy and update
  8668. statistics everywhere."
  8669. (interactive)
  8670. (let* ((lim 0) prop
  8671. (recursive (or (not org-hierarchical-todo-statistics)
  8672. (string-match
  8673. "\\<recursive\\>"
  8674. (or (setq prop (org-entry-get
  8675. nil "COOKIE_DATA" 'inherit)) ""))))
  8676. (lim (or (and prop (marker-position
  8677. org-entry-property-inherited-from))
  8678. lim))
  8679. (first t)
  8680. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8681. level ltoggle l1
  8682. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8683. (catch 'exit
  8684. (save-excursion
  8685. (beginning-of-line 1)
  8686. (if (org-at-heading-p)
  8687. (setq ltoggle (funcall outline-level))
  8688. (error "This should not happen"))
  8689. (while (and (setq level (org-up-heading-safe))
  8690. (or recursive first)
  8691. (>= (point) lim))
  8692. (setq first nil)
  8693. (unless (and level
  8694. (not (string-match
  8695. "\\<checkbox\\>"
  8696. (downcase
  8697. (or (org-entry-get
  8698. nil "COOKIE_DATA")
  8699. "")))))
  8700. (throw 'exit nil))
  8701. (while (re-search-forward box-re (point-at-eol) t)
  8702. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8703. (setq is-percent (match-end 2))
  8704. (save-match-data
  8705. (unless (outline-next-heading) (throw 'exit nil))
  8706. (while (and (looking-at org-complex-heading-regexp)
  8707. (> (setq l1 (length (match-string 1))) level))
  8708. (setq kwd (and (or recursive (= l1 ltoggle))
  8709. (match-string 2)))
  8710. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8711. (and (listp org-provide-todo-statistics)
  8712. (or (member kwd org-provide-todo-statistics)
  8713. (member kwd org-done-keywords))))
  8714. (setq cnt-all (1+ cnt-all))
  8715. (if (eq org-provide-todo-statistics t)
  8716. (and kwd (setq cnt-all (1+ cnt-all)))))
  8717. (and (member kwd org-done-keywords)
  8718. (setq cnt-done (1+ cnt-done)))
  8719. (outline-next-heading)))
  8720. (replace-match
  8721. (if is-percent
  8722. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8723. (format "[%d/%d]" cnt-done cnt-all)))))
  8724. (when cookie-present
  8725. (run-hook-with-args 'org-after-todo-statistics-hook
  8726. cnt-done (- cnt-all cnt-done)))))
  8727. (run-hooks 'org-todo-statistics-hook)))
  8728. (defvar org-after-todo-statistics-hook nil
  8729. "Hook that is called after a TODO statistics cookie has been updated.
  8730. Each function is called with two arguments: the number of not-done entries
  8731. and the number of done entries.
  8732. For example, the following function, when added to this hook, will switch
  8733. an entry to DONE when all children are done, and back to TODO when new
  8734. entries are set to a TODO status. Note that this hook is only called
  8735. when there is a statistics cookie in the headline!
  8736. (defun org-summary-todo (n-done n-not-done)
  8737. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8738. (let (org-log-done org-log-states) ; turn off logging
  8739. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8740. ")
  8741. (defvar org-todo-statistics-hook nil
  8742. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8743. This hook runs even if there is no statisics cookie present, in which case
  8744. `org-after-todo-statistics-hook' would not run.")
  8745. (defun org-todo-trigger-tag-changes (state)
  8746. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8747. (let ((l org-todo-state-tags-triggers)
  8748. changes)
  8749. (when (or (not state) (equal state ""))
  8750. (setq changes (append changes (cdr (assoc "" l)))))
  8751. (when (and (stringp state) (> (length state) 0))
  8752. (setq changes (append changes (cdr (assoc state l)))))
  8753. (when (member state org-not-done-keywords)
  8754. (setq changes (append changes (cdr (assoc 'todo l)))))
  8755. (when (member state org-done-keywords)
  8756. (setq changes (append changes (cdr (assoc 'done l)))))
  8757. (dolist (c changes)
  8758. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8759. (defun org-local-logging (value)
  8760. "Get logging settings from a property VALUE."
  8761. (let* (words w a)
  8762. ;; directly set the variables, they are already local.
  8763. (setq org-log-done nil
  8764. org-log-repeat nil
  8765. org-todo-log-states nil)
  8766. (setq words (org-split-string value))
  8767. (while (setq w (pop words))
  8768. (cond
  8769. ((setq a (assoc w org-startup-options))
  8770. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8771. (set (nth 1 a) (nth 2 a))))
  8772. ((setq a (org-extract-log-state-settings w))
  8773. (and (member (car a) org-todo-keywords-1)
  8774. (push a org-todo-log-states)))))))
  8775. (defun org-get-todo-sequence-head (kwd)
  8776. "Return the head of the TODO sequence to which KWD belongs.
  8777. If KWD is not set, check if there is a text property remembering the
  8778. right sequence."
  8779. (let (p)
  8780. (cond
  8781. ((not kwd)
  8782. (or (get-text-property (point-at-bol) 'org-todo-head)
  8783. (progn
  8784. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8785. nil (point-at-eol)))
  8786. (get-text-property p 'org-todo-head))))
  8787. ((not (member kwd org-todo-keywords-1))
  8788. (car org-todo-keywords-1))
  8789. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8790. (defun org-fast-todo-selection ()
  8791. "Fast TODO keyword selection with single keys.
  8792. Returns the new TODO keyword, or nil if no state change should occur."
  8793. (let* ((fulltable org-todo-key-alist)
  8794. (done-keywords org-done-keywords) ;; needed for the faces.
  8795. (maxlen (apply 'max (mapcar
  8796. (lambda (x)
  8797. (if (stringp (car x)) (string-width (car x)) 0))
  8798. fulltable)))
  8799. (expert nil)
  8800. (fwidth (+ maxlen 3 1 3))
  8801. (ncol (/ (- (window-width) 4) fwidth))
  8802. tg cnt e c tbl
  8803. groups ingroup)
  8804. (save-excursion
  8805. (save-window-excursion
  8806. (if expert
  8807. (set-buffer (get-buffer-create " *Org todo*"))
  8808. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8809. (erase-buffer)
  8810. (org-set-local 'org-done-keywords done-keywords)
  8811. (setq tbl fulltable cnt 0)
  8812. (while (setq e (pop tbl))
  8813. (cond
  8814. ((equal e '(:startgroup))
  8815. (push '() groups) (setq ingroup t)
  8816. (when (not (= cnt 0))
  8817. (setq cnt 0)
  8818. (insert "\n"))
  8819. (insert "{ "))
  8820. ((equal e '(:endgroup))
  8821. (setq ingroup nil cnt 0)
  8822. (insert "}\n"))
  8823. ((equal e '(:newline))
  8824. (when (not (= cnt 0))
  8825. (setq cnt 0)
  8826. (insert "\n")
  8827. (setq e (car tbl))
  8828. (while (equal (car tbl) '(:newline))
  8829. (insert "\n")
  8830. (setq tbl (cdr tbl)))))
  8831. (t
  8832. (setq tg (car e) c (cdr e))
  8833. (if ingroup (push tg (car groups)))
  8834. (setq tg (org-add-props tg nil 'face
  8835. (org-get-todo-face tg)))
  8836. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8837. (insert "[" c "] " tg (make-string
  8838. (- fwidth 4 (length tg)) ?\ ))
  8839. (when (= (setq cnt (1+ cnt)) ncol)
  8840. (insert "\n")
  8841. (if ingroup (insert " "))
  8842. (setq cnt 0)))))
  8843. (insert "\n")
  8844. (goto-char (point-min))
  8845. (if (not expert) (org-fit-window-to-buffer))
  8846. (message "[a-z..]:Set [SPC]:clear")
  8847. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8848. (cond
  8849. ((or (= c ?\C-g)
  8850. (and (= c ?q) (not (rassoc c fulltable))))
  8851. (setq quit-flag t))
  8852. ((= c ?\ ) nil)
  8853. ((setq e (rassoc c fulltable) tg (car e))
  8854. tg)
  8855. (t (setq quit-flag t)))))))
  8856. (defun org-entry-is-todo-p ()
  8857. (member (org-get-todo-state) org-not-done-keywords))
  8858. (defun org-entry-is-done-p ()
  8859. (member (org-get-todo-state) org-done-keywords))
  8860. (defun org-get-todo-state ()
  8861. (save-excursion
  8862. (org-back-to-heading t)
  8863. (and (looking-at org-todo-line-regexp)
  8864. (match-end 2)
  8865. (match-string 2))))
  8866. (defun org-at-date-range-p (&optional inactive-ok)
  8867. "Is the cursor inside a date range?"
  8868. (interactive)
  8869. (save-excursion
  8870. (catch 'exit
  8871. (let ((pos (point)))
  8872. (skip-chars-backward "^[<\r\n")
  8873. (skip-chars-backward "<[")
  8874. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8875. (>= (match-end 0) pos)
  8876. (throw 'exit t))
  8877. (skip-chars-backward "^<[\r\n")
  8878. (skip-chars-backward "<[")
  8879. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8880. (>= (match-end 0) pos)
  8881. (throw 'exit t)))
  8882. nil)))
  8883. (defun org-get-repeat ()
  8884. "Check if there is a deadline/schedule with repeater in this entry."
  8885. (save-match-data
  8886. (save-excursion
  8887. (org-back-to-heading t)
  8888. (if (re-search-forward
  8889. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8890. (match-string 1)))))
  8891. (defvar org-last-changed-timestamp)
  8892. (defvar org-last-inserted-timestamp)
  8893. (defvar org-log-post-message)
  8894. (defvar org-log-note-purpose)
  8895. (defvar org-log-note-how)
  8896. (defvar org-log-note-extra)
  8897. (defun org-auto-repeat-maybe (done-word)
  8898. "Check if the current headline contains a repeated deadline/schedule.
  8899. If yes, set TODO state back to what it was and change the base date
  8900. of repeating deadline/scheduled time stamps to new date.
  8901. This function is run automatically after each state change to a DONE state."
  8902. ;; last-state is dynamically scoped into this function
  8903. (let* ((repeat (org-get-repeat))
  8904. (aa (assoc last-state org-todo-kwd-alist))
  8905. (interpret (nth 1 aa))
  8906. (head (nth 2 aa))
  8907. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8908. (msg "Entry repeats: ")
  8909. (org-log-done nil)
  8910. (org-todo-log-states nil)
  8911. (nshiftmax 10) (nshift 0)
  8912. re type n what ts time)
  8913. (when repeat
  8914. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8915. (org-todo (if (eq interpret 'type) last-state head))
  8916. (org-entry-put nil "LAST_REPEAT" (format-time-string
  8917. (org-time-stamp-format t t)))
  8918. (when org-log-repeat
  8919. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8920. (memq 'org-add-log-note post-command-hook))
  8921. ;; OK, we are already setup for some record
  8922. (if (eq org-log-repeat 'note)
  8923. ;; make sure we take a note, not only a time stamp
  8924. (setq org-log-note-how 'note))
  8925. ;; Set up for taking a record
  8926. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8927. last-state
  8928. 'findpos org-log-repeat)))
  8929. (org-back-to-heading t)
  8930. (org-add-planning-info nil nil 'closed)
  8931. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8932. org-deadline-time-regexp "\\)\\|\\("
  8933. org-ts-regexp "\\)"))
  8934. (while (re-search-forward
  8935. re (save-excursion (outline-next-heading) (point)) t)
  8936. (setq type (if (match-end 1) org-scheduled-string
  8937. (if (match-end 3) org-deadline-string "Plain:"))
  8938. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8939. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8940. (setq n (string-to-number (match-string 2 ts))
  8941. what (match-string 3 ts))
  8942. (if (equal what "w") (setq n (* n 7) what "d"))
  8943. ;; Preparation, see if we need to modify the start date for the change
  8944. (when (match-end 1)
  8945. (setq time (save-match-data (org-time-string-to-time ts)))
  8946. (cond
  8947. ((equal (match-string 1 ts) ".")
  8948. ;; Shift starting date to today
  8949. (org-timestamp-change
  8950. (- (time-to-days (current-time)) (time-to-days time))
  8951. 'day))
  8952. ((equal (match-string 1 ts) "+")
  8953. (while (or (= nshift 0)
  8954. (<= (time-to-days time) (time-to-days (current-time))))
  8955. (when (= (incf nshift) nshiftmax)
  8956. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8957. (error "Abort")))
  8958. (org-timestamp-change n (cdr (assoc what whata)))
  8959. (org-at-timestamp-p t)
  8960. (setq ts (match-string 1))
  8961. (setq time (save-match-data (org-time-string-to-time ts))))
  8962. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8963. ;; rematch, so that we have everything in place for the real shift
  8964. (org-at-timestamp-p t)
  8965. (setq ts (match-string 1))
  8966. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8967. (org-timestamp-change n (cdr (assoc what whata)))
  8968. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8969. (setq org-log-post-message msg)
  8970. (message "%s" msg))))
  8971. (defun org-show-todo-tree (arg)
  8972. "Make a compact tree which shows all headlines marked with TODO.
  8973. The tree will show the lines where the regexp matches, and all higher
  8974. headlines above the match.
  8975. With a \\[universal-argument] prefix, prompt for a regexp to match.
  8976. With a numeric prefix N, construct a sparse tree for the Nth element
  8977. of `org-todo-keywords-1'."
  8978. (interactive "P")
  8979. (let ((case-fold-search nil)
  8980. (kwd-re
  8981. (cond ((null arg) org-not-done-regexp)
  8982. ((equal arg '(4))
  8983. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  8984. (mapcar 'list org-todo-keywords-1))))
  8985. (concat "\\("
  8986. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8987. "\\)\\>")))
  8988. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8989. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8990. org-todo-keywords-1)))
  8991. (t (error "Invalid prefix argument: %s" arg)))))
  8992. (message "%d TODO entries found"
  8993. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8994. (defun org-deadline (&optional remove time)
  8995. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8996. With argument REMOVE, remove any deadline from the item.
  8997. When TIME is set, it should be an internal time specification, and the
  8998. scheduling will use the corresponding date."
  8999. (interactive "P")
  9000. (if remove
  9001. (progn
  9002. (org-remove-timestamp-with-keyword org-deadline-string)
  9003. (message "Item no longer has a deadline."))
  9004. (if (org-get-repeat)
  9005. (error "Cannot change deadline on task with repeater, please do that by hand")
  9006. (org-add-planning-info 'deadline time 'closed)
  9007. (message "Deadline on %s" org-last-inserted-timestamp))))
  9008. (defun org-schedule (&optional remove time)
  9009. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9010. With argument REMOVE, remove any scheduling date from the item.
  9011. When TIME is set, it should be an internal time specification, and the
  9012. scheduling will use the corresponding date."
  9013. (interactive "P")
  9014. (if remove
  9015. (progn
  9016. (org-remove-timestamp-with-keyword org-scheduled-string)
  9017. (message "Item is no longer scheduled."))
  9018. (if (org-get-repeat)
  9019. (error "Cannot reschedule task with repeater, please do that by hand")
  9020. (org-add-planning-info 'scheduled time 'closed)
  9021. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9022. (defun org-get-scheduled-time (pom &optional inherit)
  9023. "Get the scheduled time as a time tuple, of a format suitable
  9024. for calling org-schedule with, or if there is no scheduling,
  9025. returns nil."
  9026. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9027. (when time
  9028. (apply 'encode-time (org-parse-time-string time)))))
  9029. (defun org-get-deadline-time (pom &optional inherit)
  9030. "Get the deadine as a time tuple, of a format suitable for
  9031. calling org-deadlin with, or if there is no scheduling, returns
  9032. nil."
  9033. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9034. (when time
  9035. (apply 'encode-time (org-parse-time-string time)))))
  9036. (defun org-remove-timestamp-with-keyword (keyword)
  9037. "Remove all time stamps with KEYWORD in the current entry."
  9038. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9039. beg)
  9040. (save-excursion
  9041. (org-back-to-heading t)
  9042. (setq beg (point))
  9043. (org-end-of-subtree t t)
  9044. (while (re-search-backward re beg t)
  9045. (replace-match "")
  9046. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9047. (equal (char-before) ?\ ))
  9048. (backward-delete-char 1)
  9049. (if (string-match "^[ \t]*$" (buffer-substring
  9050. (point-at-bol) (point-at-eol)))
  9051. (delete-region (point-at-bol)
  9052. (min (point-max) (1+ (point-at-eol))))))))))
  9053. (defun org-add-planning-info (what &optional time &rest remove)
  9054. "Insert new timestamp with keyword in the line directly after the headline.
  9055. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9056. If non is given, the user is prompted for a date.
  9057. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9058. be removed."
  9059. (interactive)
  9060. (let (org-time-was-given org-end-time-was-given ts
  9061. end default-time default-input)
  9062. (catch 'exit
  9063. (when (and (not time) (memq what '(scheduled deadline)))
  9064. ;; Try to get a default date/time from existing timestamp
  9065. (save-excursion
  9066. (org-back-to-heading t)
  9067. (setq end (save-excursion (outline-next-heading) (point)))
  9068. (when (re-search-forward (if (eq what 'scheduled)
  9069. org-scheduled-time-regexp
  9070. org-deadline-time-regexp)
  9071. end t)
  9072. (setq ts (match-string 1)
  9073. default-time
  9074. (apply 'encode-time (org-parse-time-string ts))
  9075. default-input (and ts (org-get-compact-tod ts))))))
  9076. (when what
  9077. ;; If necessary, get the time from the user
  9078. (setq time (or time (org-read-date nil 'to-time nil nil
  9079. default-time default-input))))
  9080. (when (and org-insert-labeled-timestamps-at-point
  9081. (member what '(scheduled deadline)))
  9082. (insert
  9083. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9084. (org-insert-time-stamp time org-time-was-given
  9085. nil nil nil (list org-end-time-was-given))
  9086. (setq what nil))
  9087. (save-excursion
  9088. (save-restriction
  9089. (let (col list elt ts buffer-invisibility-spec)
  9090. (org-back-to-heading t)
  9091. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9092. (goto-char (match-end 1))
  9093. (setq col (current-column))
  9094. (goto-char (match-end 0))
  9095. (if (eobp) (insert "\n") (forward-char 1))
  9096. (when (and (not what)
  9097. (not (looking-at
  9098. (concat "[ \t]*"
  9099. org-keyword-time-not-clock-regexp))))
  9100. ;; Nothing to add, nothing to remove...... :-)
  9101. (throw 'exit nil))
  9102. (if (and (not (looking-at outline-regexp))
  9103. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9104. "[^\r\n]*"))
  9105. (not (equal (match-string 1) org-clock-string)))
  9106. (narrow-to-region (match-beginning 0) (match-end 0))
  9107. (insert-before-markers "\n")
  9108. (backward-char 1)
  9109. (narrow-to-region (point) (point))
  9110. (and org-adapt-indentation (org-indent-to-column col)))
  9111. ;; Check if we have to remove something.
  9112. (setq list (cons what remove))
  9113. (while list
  9114. (setq elt (pop list))
  9115. (goto-char (point-min))
  9116. (when (or (and (eq elt 'scheduled)
  9117. (re-search-forward org-scheduled-time-regexp nil t))
  9118. (and (eq elt 'deadline)
  9119. (re-search-forward org-deadline-time-regexp nil t))
  9120. (and (eq elt 'closed)
  9121. (re-search-forward org-closed-time-regexp nil t)))
  9122. (replace-match "")
  9123. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9124. (if (looking-at " +") (replace-match ""))))
  9125. (goto-char (point-max))
  9126. (when what
  9127. (insert
  9128. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9129. (cond ((eq what 'scheduled) org-scheduled-string)
  9130. ((eq what 'deadline) org-deadline-string)
  9131. ((eq what 'closed) org-closed-string))
  9132. " ")
  9133. (setq ts (org-insert-time-stamp
  9134. time
  9135. (or org-time-was-given
  9136. (and (eq what 'closed) org-log-done-with-time))
  9137. (eq what 'closed)
  9138. nil nil (list org-end-time-was-given)))
  9139. (end-of-line 1))
  9140. (goto-char (point-min))
  9141. (widen)
  9142. (if (and (looking-at "[ \t]+\n")
  9143. (equal (char-before) ?\n))
  9144. (delete-region (1- (point)) (point-at-eol)))
  9145. ts))))))
  9146. (defvar org-log-note-marker (make-marker))
  9147. (defvar org-log-note-purpose nil)
  9148. (defvar org-log-note-state nil)
  9149. (defvar org-log-note-previous-state nil)
  9150. (defvar org-log-note-how nil)
  9151. (defvar org-log-note-extra nil)
  9152. (defvar org-log-note-window-configuration nil)
  9153. (defvar org-log-note-return-to (make-marker))
  9154. (defvar org-log-post-message nil
  9155. "Message to be displayed after a log note has been stored.
  9156. The auto-repeater uses this.")
  9157. (defun org-add-note ()
  9158. "Add a note to the current entry.
  9159. This is done in the same way as adding a state change note."
  9160. (interactive)
  9161. (org-add-log-setup 'note nil nil 'findpos nil))
  9162. (defvar org-property-end-re)
  9163. (defun org-add-log-setup (&optional purpose state prev-state
  9164. findpos how &optional extra)
  9165. "Set up the post command hook to take a note.
  9166. If this is about to TODO state change, the new state is expected in STATE.
  9167. When FINDPOS is non-nil, find the correct position for the note in
  9168. the current entry. If not, assume that it can be inserted at point.
  9169. HOW is an indicator what kind of note should be created.
  9170. EXTRA is additional text that will be inserted into the notes buffer."
  9171. (let* ((org-log-into-drawer (org-log-into-drawer))
  9172. (drawer (cond ((stringp org-log-into-drawer)
  9173. org-log-into-drawer)
  9174. (org-log-into-drawer "LOGBOOK")
  9175. (t nil))))
  9176. (save-restriction
  9177. (save-excursion
  9178. (when findpos
  9179. (org-back-to-heading t)
  9180. (narrow-to-region (point) (save-excursion
  9181. (outline-next-heading) (point)))
  9182. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9183. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9184. "[^\r\n]*\\)?"))
  9185. (goto-char (match-end 0))
  9186. (cond
  9187. (drawer
  9188. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9189. nil t)
  9190. (progn
  9191. (goto-char (match-end 0))
  9192. (or org-log-states-order-reversed
  9193. (and (re-search-forward org-property-end-re nil t)
  9194. (goto-char (1- (match-beginning 0))))))
  9195. (insert "\n:" drawer ":\n:END:")
  9196. (beginning-of-line 0)
  9197. (org-indent-line-function)
  9198. (beginning-of-line 2)
  9199. (org-indent-line-function)
  9200. (end-of-line 0)))
  9201. ((and org-log-state-notes-insert-after-drawers
  9202. (save-excursion
  9203. (forward-line) (looking-at org-drawer-regexp)))
  9204. (forward-line)
  9205. (while (looking-at org-drawer-regexp)
  9206. (goto-char (match-end 0))
  9207. (re-search-forward org-property-end-re (point-max) t)
  9208. (forward-line))
  9209. (forward-line -1)))
  9210. (unless org-log-states-order-reversed
  9211. (and (= (char-after) ?\n) (forward-char 1))
  9212. (org-skip-over-state-notes)
  9213. (skip-chars-backward " \t\n\r")))
  9214. (move-marker org-log-note-marker (point))
  9215. (setq org-log-note-purpose purpose
  9216. org-log-note-state state
  9217. org-log-note-previous-state prev-state
  9218. org-log-note-how how
  9219. org-log-note-extra extra)
  9220. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9221. (defun org-skip-over-state-notes ()
  9222. "Skip past the list of State notes in an entry."
  9223. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9224. (while (looking-at "[ \t]*- State")
  9225. (condition-case nil
  9226. (org-next-item)
  9227. (error (org-end-of-item)))))
  9228. (defun org-add-log-note (&optional purpose)
  9229. "Pop up a window for taking a note, and add this note later at point."
  9230. (remove-hook 'post-command-hook 'org-add-log-note)
  9231. (setq org-log-note-window-configuration (current-window-configuration))
  9232. (delete-other-windows)
  9233. (move-marker org-log-note-return-to (point))
  9234. (switch-to-buffer (marker-buffer org-log-note-marker))
  9235. (goto-char org-log-note-marker)
  9236. (org-switch-to-buffer-other-window "*Org Note*")
  9237. (erase-buffer)
  9238. (if (memq org-log-note-how '(time state))
  9239. (let (current-prefix-arg) (org-store-log-note))
  9240. (let ((org-inhibit-startup t)) (org-mode))
  9241. (insert (format "# Insert note for %s.
  9242. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9243. (cond
  9244. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9245. ((eq org-log-note-purpose 'done) "closed todo item")
  9246. ((eq org-log-note-purpose 'state)
  9247. (format "state change from \"%s\" to \"%s\""
  9248. (or org-log-note-previous-state "")
  9249. (or org-log-note-state "")))
  9250. ((eq org-log-note-purpose 'note)
  9251. "this entry")
  9252. (t (error "This should not happen")))))
  9253. (if org-log-note-extra (insert org-log-note-extra))
  9254. (org-set-local 'org-finish-function 'org-store-log-note)))
  9255. (defvar org-note-abort nil) ; dynamically scoped
  9256. (defun org-store-log-note ()
  9257. "Finish taking a log note, and insert it to where it belongs."
  9258. (let ((txt (buffer-string))
  9259. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9260. lines ind)
  9261. (kill-buffer (current-buffer))
  9262. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9263. (setq txt (replace-match "" t t txt)))
  9264. (if (string-match "\\s-+\\'" txt)
  9265. (setq txt (replace-match "" t t txt)))
  9266. (setq lines (org-split-string txt "\n"))
  9267. (when (and note (string-match "\\S-" note))
  9268. (setq note
  9269. (org-replace-escapes
  9270. note
  9271. (list (cons "%u" (user-login-name))
  9272. (cons "%U" user-full-name)
  9273. (cons "%t" (format-time-string
  9274. (org-time-stamp-format 'long 'inactive)
  9275. (current-time)))
  9276. (cons "%s" (if org-log-note-state
  9277. (concat "\"" org-log-note-state "\"")
  9278. ""))
  9279. (cons "%S" (if org-log-note-previous-state
  9280. (concat "\"" org-log-note-previous-state "\"")
  9281. "\"\"")))))
  9282. (if lines (setq note (concat note " \\\\")))
  9283. (push note lines))
  9284. (when (or current-prefix-arg org-note-abort)
  9285. (when org-log-into-drawer
  9286. (org-remove-empty-drawer-at
  9287. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9288. org-log-note-marker))
  9289. (setq lines nil))
  9290. (when lines
  9291. (save-excursion
  9292. (set-buffer (marker-buffer org-log-note-marker))
  9293. (save-excursion
  9294. (goto-char org-log-note-marker)
  9295. (move-marker org-log-note-marker nil)
  9296. (end-of-line 1)
  9297. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9298. (insert "- " (pop lines))
  9299. (org-indent-line-function)
  9300. (beginning-of-line 1)
  9301. (looking-at "[ \t]*")
  9302. (setq ind (concat (match-string 0) " "))
  9303. (end-of-line 1)
  9304. (while lines (insert "\n" ind (pop lines)))
  9305. (message "Note stored")
  9306. (org-back-to-heading t)
  9307. (org-cycle-hide-drawers 'children)))))
  9308. (set-window-configuration org-log-note-window-configuration)
  9309. (with-current-buffer (marker-buffer org-log-note-return-to)
  9310. (goto-char org-log-note-return-to))
  9311. (move-marker org-log-note-return-to nil)
  9312. (and org-log-post-message (message "%s" org-log-post-message)))
  9313. (defun org-remove-empty-drawer-at (drawer pos)
  9314. "Remove an emptyr DARWER drawer at position POS.
  9315. POS may also be a marker."
  9316. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9317. (save-excursion
  9318. (save-restriction
  9319. (widen)
  9320. (goto-char pos)
  9321. (if (org-in-regexp
  9322. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9323. (replace-match ""))))))
  9324. (defun org-sparse-tree (&optional arg)
  9325. "Create a sparse tree, prompt for the details.
  9326. This command can create sparse trees. You first need to select the type
  9327. of match used to create the tree:
  9328. t Show entries with a specific TODO keyword.
  9329. m Show entries selected by a tags/property match.
  9330. p Enter a property name and its value (both with completion on existing
  9331. names/values) and show entries with that property.
  9332. r Show entries matching a regular expression.
  9333. d Show deadlines due within `org-deadline-warning-days'.
  9334. b Show deadlines and scheduled items before a date.
  9335. a Show deadlines and scheduled items after a date."
  9336. (interactive "P")
  9337. (let (ans kwd value)
  9338. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9339. (setq ans (read-char-exclusive))
  9340. (cond
  9341. ((equal ans ?d)
  9342. (call-interactively 'org-check-deadlines))
  9343. ((equal ans ?b)
  9344. (call-interactively 'org-check-before-date))
  9345. ((equal ans ?a)
  9346. (call-interactively 'org-check-after-date))
  9347. ((equal ans ?t)
  9348. (org-show-todo-tree '(4)))
  9349. ((member ans '(?T ?m))
  9350. (call-interactively 'org-match-sparse-tree))
  9351. ((member ans '(?p ?P))
  9352. (setq kwd (org-icompleting-read "Property: "
  9353. (mapcar 'list (org-buffer-property-keys))))
  9354. (setq value (org-icompleting-read "Value: "
  9355. (mapcar 'list (org-property-values kwd))))
  9356. (unless (string-match "\\`{.*}\\'" value)
  9357. (setq value (concat "\"" value "\"")))
  9358. (org-match-sparse-tree arg (concat kwd "=" value)))
  9359. ((member ans '(?r ?R ?/))
  9360. (call-interactively 'org-occur))
  9361. (t (error "No such sparse tree command \"%c\"" ans)))))
  9362. (defvar org-occur-highlights nil
  9363. "List of overlays used for occur matches.")
  9364. (make-variable-buffer-local 'org-occur-highlights)
  9365. (defvar org-occur-parameters nil
  9366. "Parameters of the active org-occur calls.
  9367. This is a list, each call to org-occur pushes as cons cell,
  9368. containing the regular expression and the callback, onto the list.
  9369. The list can contain several entries if `org-occur' has been called
  9370. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9371. will only contain one set of parameters. When the highlights are
  9372. removed (for example with `C-c C-c', or with the next edit (depending
  9373. on `org-remove-highlights-with-change'), this variable is emptied
  9374. as well.")
  9375. (make-variable-buffer-local 'org-occur-parameters)
  9376. (defun org-occur (regexp &optional keep-previous callback)
  9377. "Make a compact tree which shows all matches of REGEXP.
  9378. The tree will show the lines where the regexp matches, and all higher
  9379. headlines above the match. It will also show the heading after the match,
  9380. to make sure editing the matching entry is easy.
  9381. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9382. call to `org-occur' will be kept, to allow stacking of calls to this
  9383. command.
  9384. If CALLBACK is non-nil, it is a function which is called to confirm
  9385. that the match should indeed be shown."
  9386. (interactive "sRegexp: \nP")
  9387. (when (equal regexp "")
  9388. (error "Regexp cannot be empty"))
  9389. (unless keep-previous
  9390. (org-remove-occur-highlights nil nil t))
  9391. (push (cons regexp callback) org-occur-parameters)
  9392. (let ((cnt 0))
  9393. (save-excursion
  9394. (goto-char (point-min))
  9395. (if (or (not keep-previous) ; do not want to keep
  9396. (not org-occur-highlights)) ; no previous matches
  9397. ;; hide everything
  9398. (org-overview))
  9399. (while (re-search-forward regexp nil t)
  9400. (when (or (not callback)
  9401. (save-match-data (funcall callback)))
  9402. (setq cnt (1+ cnt))
  9403. (when org-highlight-sparse-tree-matches
  9404. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9405. (org-show-context 'occur-tree))))
  9406. (when org-remove-highlights-with-change
  9407. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9408. nil 'local))
  9409. (unless org-sparse-tree-open-archived-trees
  9410. (org-hide-archived-subtrees (point-min) (point-max)))
  9411. (run-hooks 'org-occur-hook)
  9412. (if (interactive-p)
  9413. (message "%d match(es) for regexp %s" cnt regexp))
  9414. cnt))
  9415. (defun org-show-context (&optional key)
  9416. "Make sure point and context and visible.
  9417. How much context is shown depends upon the variables
  9418. `org-show-hierarchy-above', `org-show-following-heading'. and
  9419. `org-show-siblings'."
  9420. (let ((heading-p (org-on-heading-p t))
  9421. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9422. (following-p (org-get-alist-option org-show-following-heading key))
  9423. (entry-p (org-get-alist-option org-show-entry-below key))
  9424. (siblings-p (org-get-alist-option org-show-siblings key)))
  9425. (catch 'exit
  9426. ;; Show heading or entry text
  9427. (if (and heading-p (not entry-p))
  9428. (org-flag-heading nil) ; only show the heading
  9429. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9430. (org-show-hidden-entry))) ; show entire entry
  9431. (when following-p
  9432. ;; Show next sibling, or heading below text
  9433. (save-excursion
  9434. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9435. (org-flag-heading nil))))
  9436. (when siblings-p (org-show-siblings))
  9437. (when hierarchy-p
  9438. ;; show all higher headings, possibly with siblings
  9439. (save-excursion
  9440. (while (and (condition-case nil
  9441. (progn (org-up-heading-all 1) t)
  9442. (error nil))
  9443. (not (bobp)))
  9444. (org-flag-heading nil)
  9445. (when siblings-p (org-show-siblings))))))))
  9446. (defun org-reveal (&optional siblings)
  9447. "Show current entry, hierarchy above it, and the following headline.
  9448. This can be used to show a consistent set of context around locations
  9449. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9450. not t for the search context.
  9451. With optional argument SIBLINGS, on each level of the hierarchy all
  9452. siblings are shown. This repairs the tree structure to what it would
  9453. look like when opened with hierarchical calls to `org-cycle'."
  9454. (interactive "P")
  9455. (let ((org-show-hierarchy-above t)
  9456. (org-show-following-heading t)
  9457. (org-show-siblings (if siblings t org-show-siblings)))
  9458. (org-show-context nil)))
  9459. (defun org-highlight-new-match (beg end)
  9460. "Highlight from BEG to END and mark the highlight is an occur headline."
  9461. (let ((ov (org-make-overlay beg end)))
  9462. (org-overlay-put ov 'face 'secondary-selection)
  9463. (push ov org-occur-highlights)))
  9464. (defun org-remove-occur-highlights (&optional beg end noremove)
  9465. "Remove the occur highlights from the buffer.
  9466. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9467. from the `before-change-functions' in the current buffer."
  9468. (interactive)
  9469. (unless org-inhibit-highlight-removal
  9470. (mapc 'org-delete-overlay org-occur-highlights)
  9471. (setq org-occur-highlights nil)
  9472. (setq org-occur-parameters nil)
  9473. (unless noremove
  9474. (remove-hook 'before-change-functions
  9475. 'org-remove-occur-highlights 'local))))
  9476. ;;;; Priorities
  9477. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9478. "Regular expression matching the priority indicator.")
  9479. (defvar org-remove-priority-next-time nil)
  9480. (defun org-priority-up ()
  9481. "Increase the priority of the current item."
  9482. (interactive)
  9483. (org-priority 'up))
  9484. (defun org-priority-down ()
  9485. "Decrease the priority of the current item."
  9486. (interactive)
  9487. (org-priority 'down))
  9488. (defun org-priority (&optional action)
  9489. "Change the priority of an item by ARG.
  9490. ACTION can be `set', `up', `down', or a character."
  9491. (interactive)
  9492. (unless org-enable-priority-commands
  9493. (error "Priority commands are disabled"))
  9494. (setq action (or action 'set))
  9495. (let (current new news have remove)
  9496. (save-excursion
  9497. (org-back-to-heading t)
  9498. (if (looking-at org-priority-regexp)
  9499. (setq current (string-to-char (match-string 2))
  9500. have t)
  9501. (setq current org-default-priority))
  9502. (cond
  9503. ((or (eq action 'set)
  9504. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9505. (if (not (eq action 'set))
  9506. (setq new action)
  9507. (message "Priority %c-%c, SPC to remove: "
  9508. org-highest-priority org-lowest-priority)
  9509. (setq new (read-char-exclusive)))
  9510. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9511. (= (upcase org-lowest-priority) org-lowest-priority))
  9512. (setq new (upcase new)))
  9513. (cond ((equal new ?\ ) (setq remove t))
  9514. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9515. (error "Priority must be between `%c' and `%c'"
  9516. org-highest-priority org-lowest-priority))))
  9517. ((eq action 'up)
  9518. (if (and (not have) (eq last-command this-command))
  9519. (setq new org-lowest-priority)
  9520. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9521. org-default-priority (1- current)))))
  9522. ((eq action 'down)
  9523. (if (and (not have) (eq last-command this-command))
  9524. (setq new org-highest-priority)
  9525. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9526. org-default-priority (1+ current)))))
  9527. (t (error "Invalid action")))
  9528. (if (or (< (upcase new) org-highest-priority)
  9529. (> (upcase new) org-lowest-priority))
  9530. (setq remove t))
  9531. (setq news (format "%c" new))
  9532. (if have
  9533. (if remove
  9534. (replace-match "" t t nil 1)
  9535. (replace-match news t t nil 2))
  9536. (if remove
  9537. (error "No priority cookie found in line")
  9538. (let ((case-fold-search nil))
  9539. (looking-at org-todo-line-regexp))
  9540. (if (match-end 2)
  9541. (progn
  9542. (goto-char (match-end 2))
  9543. (insert " [#" news "]"))
  9544. (goto-char (match-beginning 3))
  9545. (insert "[#" news "] "))))
  9546. (org-preserve-lc (org-set-tags nil 'align)))
  9547. (if remove
  9548. (message "Priority removed")
  9549. (message "Priority of current item set to %s" news))))
  9550. (defun org-get-priority (s)
  9551. "Find priority cookie and return priority."
  9552. (save-match-data
  9553. (if (not (string-match org-priority-regexp s))
  9554. (* 1000 (- org-lowest-priority org-default-priority))
  9555. (* 1000 (- org-lowest-priority
  9556. (string-to-char (match-string 2 s)))))))
  9557. ;;;; Tags
  9558. (defvar org-agenda-archives-mode)
  9559. (defvar org-map-continue-from nil
  9560. "Position from where mapping should continue.
  9561. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9562. (defvar org-scanner-tags nil
  9563. "The current tag list while the tags scanner is running.")
  9564. (defvar org-trust-scanner-tags nil
  9565. "Should `org-get-tags-at' use the tags fro the scanner.
  9566. This is for internal dynamical scoping only.
  9567. When this is non-nil, the function `org-get-tags-at' will return the value
  9568. of `org-scanner-tags' instead of building the list by itself. This
  9569. can lead to large speed-ups when the tags scanner is used in a file with
  9570. many entries, and when the list of tags is retrieved, for example to
  9571. obtain a list of properties. Building the tags list for each entry in such
  9572. a file becomes an N^2 operation - but with this variable set, it scales
  9573. as N.")
  9574. (defun org-scan-tags (action matcher &optional todo-only)
  9575. "Scan headline tags with inheritance and produce output ACTION.
  9576. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9577. or `agenda' to produce an entry list for an agenda view. It can also be
  9578. a Lisp form or a function that should be called at each matched headline, in
  9579. this case the return value is a list of all return values from these calls.
  9580. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9581. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9582. only lines with a TODO keyword are included in the output."
  9583. (require 'org-agenda)
  9584. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9585. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9586. (org-re
  9587. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9588. (props (list 'face 'default
  9589. 'done-face 'org-agenda-done
  9590. 'undone-face 'default
  9591. 'mouse-face 'highlight
  9592. 'org-not-done-regexp org-not-done-regexp
  9593. 'org-todo-regexp org-todo-regexp
  9594. 'keymap org-agenda-keymap
  9595. 'help-echo
  9596. (format "mouse-2 or RET jump to org file %s"
  9597. (abbreviate-file-name
  9598. (or (buffer-file-name (buffer-base-buffer))
  9599. (buffer-name (buffer-base-buffer)))))))
  9600. (case-fold-search nil)
  9601. (org-map-continue-from nil)
  9602. lspos tags tags-list
  9603. (tags-alist (list (cons 0 org-file-tags)))
  9604. (llast 0) rtn rtn1 level category i txt
  9605. todo marker entry priority)
  9606. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9607. (setq action (list 'lambda nil action)))
  9608. (save-excursion
  9609. (goto-char (point-min))
  9610. (when (eq action 'sparse-tree)
  9611. (org-overview)
  9612. (org-remove-occur-highlights))
  9613. (while (re-search-forward re nil t)
  9614. (catch :skip
  9615. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9616. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9617. (goto-char (setq lspos (match-beginning 0)))
  9618. (setq level (org-reduced-level (funcall outline-level))
  9619. category (org-get-category))
  9620. (setq i llast llast level)
  9621. ;; remove tag lists from same and sublevels
  9622. (while (>= i level)
  9623. (when (setq entry (assoc i tags-alist))
  9624. (setq tags-alist (delete entry tags-alist)))
  9625. (setq i (1- i)))
  9626. ;; add the next tags
  9627. (when tags
  9628. (setq tags (org-split-string tags ":")
  9629. tags-alist
  9630. (cons (cons level tags) tags-alist)))
  9631. ;; compile tags for current headline
  9632. (setq tags-list
  9633. (if org-use-tag-inheritance
  9634. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9635. tags)
  9636. org-scanner-tags tags-list)
  9637. (when org-use-tag-inheritance
  9638. (setcdr (car tags-alist)
  9639. (mapcar (lambda (x)
  9640. (setq x (copy-sequence x))
  9641. (org-add-prop-inherited x))
  9642. (cdar tags-alist))))
  9643. (when (and tags org-use-tag-inheritance
  9644. (or (not (eq t org-use-tag-inheritance))
  9645. org-tags-exclude-from-inheritance))
  9646. ;; selective inheritance, remove uninherited ones
  9647. (setcdr (car tags-alist)
  9648. (org-remove-uniherited-tags (cdar tags-alist))))
  9649. (when (and (or (not todo-only)
  9650. (and (member todo org-not-done-keywords)
  9651. (or (not org-agenda-tags-todo-honor-ignore-options)
  9652. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9653. (let ((case-fold-search t)) (eval matcher))
  9654. (or
  9655. (not (member org-archive-tag tags-list))
  9656. ;; we have an archive tag, should we use this anyway?
  9657. (or (not org-agenda-skip-archived-trees)
  9658. (and (eq action 'agenda) org-agenda-archives-mode))))
  9659. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9660. ;; select this headline
  9661. (cond
  9662. ((eq action 'sparse-tree)
  9663. (and org-highlight-sparse-tree-matches
  9664. (org-get-heading) (match-end 0)
  9665. (org-highlight-new-match
  9666. (match-beginning 0) (match-beginning 1)))
  9667. (org-show-context 'tags-tree))
  9668. ((eq action 'agenda)
  9669. (setq txt (org-format-agenda-item
  9670. ""
  9671. (concat
  9672. (if (eq org-tags-match-list-sublevels 'indented)
  9673. (make-string (1- level) ?.) "")
  9674. (org-get-heading))
  9675. category
  9676. tags-list
  9677. )
  9678. priority (org-get-priority txt))
  9679. (goto-char lspos)
  9680. (setq marker (org-agenda-new-marker))
  9681. (org-add-props txt props
  9682. 'org-marker marker 'org-hd-marker marker 'org-category category
  9683. 'todo-state todo
  9684. 'priority priority 'type "tagsmatch")
  9685. (push txt rtn))
  9686. ((functionp action)
  9687. (setq org-map-continue-from nil)
  9688. (save-excursion
  9689. (setq rtn1 (funcall action))
  9690. (push rtn1 rtn)))
  9691. (t (error "Invalid action")))
  9692. ;; if we are to skip sublevels, jump to end of subtree
  9693. (unless org-tags-match-list-sublevels
  9694. (org-end-of-subtree t)
  9695. (backward-char 1))))
  9696. ;; Get the correct position from where to continue
  9697. (if org-map-continue-from
  9698. (goto-char org-map-continue-from)
  9699. (and (= (point) lspos) (end-of-line 1)))))
  9700. (when (and (eq action 'sparse-tree)
  9701. (not org-sparse-tree-open-archived-trees))
  9702. (org-hide-archived-subtrees (point-min) (point-max)))
  9703. (nreverse rtn)))
  9704. (defun org-remove-uniherited-tags (tags)
  9705. "Remove all tags that are not inherited from the list TAGS."
  9706. (cond
  9707. ((eq org-use-tag-inheritance t)
  9708. (if org-tags-exclude-from-inheritance
  9709. (org-delete-all org-tags-exclude-from-inheritance tags)
  9710. tags))
  9711. ((not org-use-tag-inheritance) nil)
  9712. ((stringp org-use-tag-inheritance)
  9713. (delq nil (mapcar
  9714. (lambda (x)
  9715. (if (and (string-match org-use-tag-inheritance x)
  9716. (not (member x org-tags-exclude-from-inheritance)))
  9717. x nil))
  9718. tags)))
  9719. ((listp org-use-tag-inheritance)
  9720. (delq nil (mapcar
  9721. (lambda (x)
  9722. (if (member x org-use-tag-inheritance) x nil))
  9723. tags)))))
  9724. (defvar todo-only) ;; dynamically scoped
  9725. (defun org-match-sparse-tree (&optional todo-only match)
  9726. "Create a sparse tree according to tags string MATCH.
  9727. MATCH can contain positive and negative selection of tags, like
  9728. \"+WORK+URGENT-WITHBOSS\".
  9729. If optional argument TODO-ONLY is non-nil, only select lines that are
  9730. also TODO lines."
  9731. (interactive "P")
  9732. (org-prepare-agenda-buffers (list (current-buffer)))
  9733. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9734. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9735. (defvar org-cached-props nil)
  9736. (defun org-cached-entry-get (pom property)
  9737. (if (or (eq t org-use-property-inheritance)
  9738. (and (stringp org-use-property-inheritance)
  9739. (string-match org-use-property-inheritance property))
  9740. (and (listp org-use-property-inheritance)
  9741. (member property org-use-property-inheritance)))
  9742. ;; Caching is not possible, check it directly
  9743. (org-entry-get pom property 'inherit)
  9744. ;; Get all properties, so that we can do complicated checks easily
  9745. (cdr (assoc property (or org-cached-props
  9746. (setq org-cached-props
  9747. (org-entry-properties pom)))))))
  9748. (defun org-global-tags-completion-table (&optional files)
  9749. "Return the list of all tags in all agenda buffer/files."
  9750. (save-excursion
  9751. (org-uniquify
  9752. (delq nil
  9753. (apply 'append
  9754. (mapcar
  9755. (lambda (file)
  9756. (set-buffer (find-file-noselect file))
  9757. (append (org-get-buffer-tags)
  9758. (mapcar (lambda (x) (if (stringp (car-safe x))
  9759. (list (car-safe x)) nil))
  9760. org-tag-alist)))
  9761. (if (and files (car files))
  9762. files
  9763. (org-agenda-files))))))))
  9764. (defun org-make-tags-matcher (match)
  9765. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9766. ;; todo-only is scoped dynamically into this function, and the function
  9767. ;; may change it if the matcher asks for it.
  9768. (unless match
  9769. ;; Get a new match request, with completion
  9770. (let ((org-last-tags-completion-table
  9771. (org-global-tags-completion-table)))
  9772. (setq match (org-completing-read-no-i
  9773. "Match: " 'org-tags-completion-function nil nil nil
  9774. 'org-tags-history))))
  9775. ;; Parse the string and create a lisp form
  9776. (let ((match0 match)
  9777. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9778. minus tag mm
  9779. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9780. orterms term orlist re-p str-p level-p level-op time-p
  9781. prop-p pn pv po cat-p gv rest)
  9782. (if (string-match "/+" match)
  9783. ;; match contains also a todo-matching request
  9784. (progn
  9785. (setq tagsmatch (substring match 0 (match-beginning 0))
  9786. todomatch (substring match (match-end 0)))
  9787. (if (string-match "^!" todomatch)
  9788. (setq todo-only t todomatch (substring todomatch 1)))
  9789. (if (string-match "^\\s-*$" todomatch)
  9790. (setq todomatch nil)))
  9791. ;; only matching tags
  9792. (setq tagsmatch match todomatch nil))
  9793. ;; Make the tags matcher
  9794. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9795. (setq tagsmatcher t)
  9796. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9797. (while (setq term (pop orterms))
  9798. (while (and (equal (substring term -1) "\\") orterms)
  9799. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9800. (while (string-match re term)
  9801. (setq rest (substring term (match-end 0))
  9802. minus (and (match-end 1)
  9803. (equal (match-string 1 term) "-"))
  9804. tag (match-string 2 term)
  9805. re-p (equal (string-to-char tag) ?{)
  9806. level-p (match-end 4)
  9807. prop-p (match-end 5)
  9808. mm (cond
  9809. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9810. (level-p
  9811. (setq level-op (org-op-to-function (match-string 3 term)))
  9812. `(,level-op level ,(string-to-number
  9813. (match-string 4 term))))
  9814. (prop-p
  9815. (setq pn (match-string 5 term)
  9816. po (match-string 6 term)
  9817. pv (match-string 7 term)
  9818. cat-p (equal pn "CATEGORY")
  9819. re-p (equal (string-to-char pv) ?{)
  9820. str-p (equal (string-to-char pv) ?\")
  9821. time-p (save-match-data
  9822. (string-match "^\"[[<].*[]>]\"$" pv))
  9823. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9824. (if time-p (setq pv (org-matcher-time pv)))
  9825. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9826. (cond
  9827. ((equal pn "CATEGORY")
  9828. (setq gv '(get-text-property (point) 'org-category)))
  9829. ((equal pn "TODO")
  9830. (setq gv 'todo))
  9831. (t
  9832. (setq gv `(org-cached-entry-get nil ,pn))))
  9833. (if re-p
  9834. (if (eq po 'org<>)
  9835. `(not (string-match ,pv (or ,gv "")))
  9836. `(string-match ,pv (or ,gv "")))
  9837. (if str-p
  9838. `(,po (or ,gv "") ,pv)
  9839. `(,po (string-to-number (or ,gv ""))
  9840. ,(string-to-number pv) ))))
  9841. (t `(member ,tag tags-list)))
  9842. mm (if minus (list 'not mm) mm)
  9843. term rest)
  9844. (push mm tagsmatcher))
  9845. (push (if (> (length tagsmatcher) 1)
  9846. (cons 'and tagsmatcher)
  9847. (car tagsmatcher))
  9848. orlist)
  9849. (setq tagsmatcher nil))
  9850. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9851. (setq tagsmatcher
  9852. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9853. ;; Make the todo matcher
  9854. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9855. (setq todomatcher t)
  9856. (setq orterms (org-split-string todomatch "|") orlist nil)
  9857. (while (setq term (pop orterms))
  9858. (while (string-match re term)
  9859. (setq minus (and (match-end 1)
  9860. (equal (match-string 1 term) "-"))
  9861. kwd (match-string 2 term)
  9862. re-p (equal (string-to-char kwd) ?{)
  9863. term (substring term (match-end 0))
  9864. mm (if re-p
  9865. `(string-match ,(substring kwd 1 -1) todo)
  9866. (list 'equal 'todo kwd))
  9867. mm (if minus (list 'not mm) mm))
  9868. (push mm todomatcher))
  9869. (push (if (> (length todomatcher) 1)
  9870. (cons 'and todomatcher)
  9871. (car todomatcher))
  9872. orlist)
  9873. (setq todomatcher nil))
  9874. (setq todomatcher (if (> (length orlist) 1)
  9875. (cons 'or orlist) (car orlist))))
  9876. ;; Return the string and lisp forms of the matcher
  9877. (setq matcher (if todomatcher
  9878. (list 'and tagsmatcher todomatcher)
  9879. tagsmatcher))
  9880. (cons match0 matcher)))
  9881. (defun org-op-to-function (op &optional stringp)
  9882. "Turn an operator into the appropriate function."
  9883. (setq op
  9884. (cond
  9885. ((equal op "<" ) '(< string< org-time<))
  9886. ((equal op ">" ) '(> org-string> org-time>))
  9887. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9888. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9889. ((member op '("=" "==")) '(= string= org-time=))
  9890. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9891. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9892. (defun org<> (a b) (not (= a b)))
  9893. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9894. (defun org-string>= (a b) (not (string< a b)))
  9895. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9896. (defun org-string<> (a b) (not (string= a b)))
  9897. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9898. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9899. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9900. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9901. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9902. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9903. (defun org-2ft (s)
  9904. "Convert S to a floating point time.
  9905. If S is already a number, just return it. If it is a string, parse
  9906. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9907. (cond
  9908. ((numberp s) s)
  9909. ((stringp s)
  9910. (condition-case nil
  9911. (float-time (apply 'encode-time (org-parse-time-string s)))
  9912. (error 0.)))
  9913. (t 0.)))
  9914. (defun org-time-today ()
  9915. "Time in seconds today at 0:00.
  9916. Returns the float number of seconds since the beginning of the
  9917. epoch to the beginning of today (00:00)."
  9918. (float-time (apply 'encode-time
  9919. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9920. (defun org-matcher-time (s)
  9921. "Interpret a time comparison value."
  9922. (save-match-data
  9923. (cond
  9924. ((string= s "<now>") (float-time))
  9925. ((string= s "<today>") (org-time-today))
  9926. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9927. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9928. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9929. (+ (org-time-today)
  9930. (* (string-to-number (match-string 1 s))
  9931. (cdr (assoc (match-string 2 s)
  9932. '(("d" . 86400.0) ("w" . 604800.0)
  9933. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9934. (t (org-2ft s)))))
  9935. (defun org-match-any-p (re list)
  9936. "Does re match any element of list?"
  9937. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9938. (delq nil list))
  9939. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9940. (defvar org-tags-overlay (org-make-overlay 1 1))
  9941. (org-detach-overlay org-tags-overlay)
  9942. (defun org-get-local-tags-at (&optional pos)
  9943. "Get a list of tags defined in the current headline."
  9944. (org-get-tags-at pos 'local))
  9945. (defun org-get-local-tags ()
  9946. "Get a list of tags defined in the current headline."
  9947. (org-get-tags-at nil 'local))
  9948. (defun org-get-tags-at (&optional pos local)
  9949. "Get a list of all headline tags applicable at POS.
  9950. POS defaults to point. If tags are inherited, the list contains
  9951. the targets in the same sequence as the headlines appear, i.e.
  9952. the tags of the current headline come last.
  9953. When LOCAL is non-nil, only return tags from the current headline,
  9954. ignore inherited ones."
  9955. (interactive)
  9956. (if (and org-trust-scanner-tags
  9957. (or (not pos) (equal pos (point)))
  9958. (not local))
  9959. org-scanner-tags
  9960. (let (tags ltags lastpos parent)
  9961. (save-excursion
  9962. (save-restriction
  9963. (widen)
  9964. (goto-char (or pos (point)))
  9965. (save-match-data
  9966. (catch 'done
  9967. (condition-case nil
  9968. (progn
  9969. (org-back-to-heading t)
  9970. (while (not (equal lastpos (point)))
  9971. (setq lastpos (point))
  9972. (when (looking-at
  9973. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9974. (setq ltags (org-split-string
  9975. (org-match-string-no-properties 1) ":"))
  9976. (when parent
  9977. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9978. (setq tags (append
  9979. (if parent
  9980. (org-remove-uniherited-tags ltags)
  9981. ltags)
  9982. tags)))
  9983. (or org-use-tag-inheritance (throw 'done t))
  9984. (if local (throw 'done t))
  9985. (or (org-up-heading-safe) (error nil))
  9986. (setq parent t)))
  9987. (error nil)))))
  9988. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9989. (defun org-add-prop-inherited (s)
  9990. (add-text-properties 0 (length s) '(inherited t) s)
  9991. s)
  9992. (defun org-toggle-tag (tag &optional onoff)
  9993. "Toggle the tag TAG for the current line.
  9994. If ONOFF is `on' or `off', don't toggle but set to this state."
  9995. (let (res current)
  9996. (save-excursion
  9997. (org-back-to-heading t)
  9998. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9999. (point-at-eol) t)
  10000. (progn
  10001. (setq current (match-string 1))
  10002. (replace-match ""))
  10003. (setq current ""))
  10004. (setq current (nreverse (org-split-string current ":")))
  10005. (cond
  10006. ((eq onoff 'on)
  10007. (setq res t)
  10008. (or (member tag current) (push tag current)))
  10009. ((eq onoff 'off)
  10010. (or (not (member tag current)) (setq current (delete tag current))))
  10011. (t (if (member tag current)
  10012. (setq current (delete tag current))
  10013. (setq res t)
  10014. (push tag current))))
  10015. (end-of-line 1)
  10016. (if current
  10017. (progn
  10018. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10019. (org-set-tags nil t))
  10020. (delete-horizontal-space))
  10021. (run-hooks 'org-after-tags-change-hook))
  10022. res))
  10023. (defun org-align-tags-here (to-col)
  10024. ;; Assumes that this is a headline
  10025. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10026. (beginning-of-line 1)
  10027. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10028. (< pos (match-beginning 2)))
  10029. (progn
  10030. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10031. (goto-char (match-beginning 1))
  10032. (insert " ")
  10033. (delete-region (point) (1+ (match-beginning 2)))
  10034. (setq ncol (max (1+ (current-column))
  10035. (1+ col)
  10036. (if (> to-col 0)
  10037. to-col
  10038. (- (abs to-col) tags-l))))
  10039. (setq p (point))
  10040. (insert (make-string (- ncol (current-column)) ?\ ))
  10041. (setq ncol (current-column))
  10042. (when indent-tabs-mode (tabify p (point-at-eol)))
  10043. (org-move-to-column (min ncol col) t))
  10044. (goto-char pos))))
  10045. (defun org-set-tags-command (&optional arg just-align)
  10046. "Call the set-tags command for the current entry."
  10047. (interactive "P")
  10048. (if (org-on-heading-p)
  10049. (org-set-tags arg just-align)
  10050. (save-excursion
  10051. (org-back-to-heading t)
  10052. (org-set-tags arg just-align))))
  10053. (defun org-set-tags (&optional arg just-align)
  10054. "Set the tags for the current headline.
  10055. With prefix ARG, realign all tags in headings in the current buffer."
  10056. (interactive "P")
  10057. (let* ((re (concat "^" outline-regexp))
  10058. (current (org-get-tags-string))
  10059. (col (current-column))
  10060. (org-setting-tags t)
  10061. table current-tags inherited-tags ; computed below when needed
  10062. tags p0 c0 c1 rpl)
  10063. (if arg
  10064. (save-excursion
  10065. (goto-char (point-min))
  10066. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10067. (while (re-search-forward re nil t)
  10068. (org-set-tags nil t)
  10069. (end-of-line 1)))
  10070. (message "All tags realigned to column %d" org-tags-column))
  10071. (if just-align
  10072. (setq tags current)
  10073. ;; Get a new set of tags from the user
  10074. (save-excursion
  10075. (setq table (append org-tag-persistent-alist
  10076. (or org-tag-alist (org-get-buffer-tags)))
  10077. org-last-tags-completion-table table
  10078. current-tags (org-split-string current ":")
  10079. inherited-tags (nreverse
  10080. (nthcdr (length current-tags)
  10081. (nreverse (org-get-tags-at))))
  10082. tags
  10083. (if (or (eq t org-use-fast-tag-selection)
  10084. (and org-use-fast-tag-selection
  10085. (delq nil (mapcar 'cdr table))))
  10086. (org-fast-tag-selection
  10087. current-tags inherited-tags table
  10088. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10089. (let ((org-add-colon-after-tag-completion t))
  10090. (org-trim
  10091. (org-without-partial-completion
  10092. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10093. nil nil current 'org-tags-history)))))))
  10094. (while (string-match "[-+&]+" tags)
  10095. ;; No boolean logic, just a list
  10096. (setq tags (replace-match ":" t t tags))))
  10097. (if org-tags-sort-function
  10098. (setq tags (mapconcat 'identity
  10099. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10100. org-tags-sort-function) ":")))
  10101. (if (string-match "\\`[\t ]*\\'" tags)
  10102. (setq tags "")
  10103. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10104. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10105. ;; Insert new tags at the correct column
  10106. (beginning-of-line 1)
  10107. (cond
  10108. ((and (equal current "") (equal tags "")))
  10109. ((re-search-forward
  10110. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10111. (point-at-eol) t)
  10112. (if (equal tags "")
  10113. (setq rpl "")
  10114. (goto-char (match-beginning 0))
  10115. (setq c0 (current-column) p0 (point)
  10116. c1 (max (1+ c0) (if (> org-tags-column 0)
  10117. org-tags-column
  10118. (- (- org-tags-column) (length tags))))
  10119. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10120. (replace-match rpl t t)
  10121. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10122. tags)
  10123. (t (error "Tags alignment failed")))
  10124. (org-move-to-column col)
  10125. (unless just-align
  10126. (run-hooks 'org-after-tags-change-hook)))))
  10127. (defun org-change-tag-in-region (beg end tag off)
  10128. "Add or remove TAG for each entry in the region.
  10129. This works in the agenda, and also in an org-mode buffer."
  10130. (interactive
  10131. (list (region-beginning) (region-end)
  10132. (let ((org-last-tags-completion-table
  10133. (if (org-mode-p)
  10134. (org-get-buffer-tags)
  10135. (org-global-tags-completion-table))))
  10136. (org-icompleting-read
  10137. "Tag: " 'org-tags-completion-function nil nil nil
  10138. 'org-tags-history))
  10139. (progn
  10140. (message "[s]et or [r]emove? ")
  10141. (equal (read-char-exclusive) ?r))))
  10142. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10143. (let ((agendap (equal major-mode 'org-agenda-mode))
  10144. l1 l2 m buf pos newhead (cnt 0))
  10145. (goto-char end)
  10146. (setq l2 (1- (org-current-line)))
  10147. (goto-char beg)
  10148. (setq l1 (org-current-line))
  10149. (loop for l from l1 to l2 do
  10150. (goto-line l)
  10151. (setq m (get-text-property (point) 'org-hd-marker))
  10152. (when (or (and (org-mode-p) (org-on-heading-p))
  10153. (and agendap m))
  10154. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10155. pos (if agendap m (point)))
  10156. (with-current-buffer buf
  10157. (save-excursion
  10158. (save-restriction
  10159. (goto-char pos)
  10160. (setq cnt (1+ cnt))
  10161. (org-toggle-tag tag (if off 'off 'on))
  10162. (setq newhead (org-get-heading)))))
  10163. (and agendap (org-agenda-change-all-lines newhead m))))
  10164. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10165. (defun org-tags-completion-function (string predicate &optional flag)
  10166. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10167. (confirm (lambda (x) (stringp (car x)))))
  10168. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10169. (setq s1 (match-string 1 string)
  10170. s2 (match-string 2 string))
  10171. (setq s1 "" s2 string))
  10172. (cond
  10173. ((eq flag nil)
  10174. ;; try completion
  10175. (setq rtn (try-completion s2 ctable confirm))
  10176. (if (stringp rtn)
  10177. (setq rtn
  10178. (concat s1 s2 (substring rtn (length s2))
  10179. (if (and org-add-colon-after-tag-completion
  10180. (assoc rtn ctable))
  10181. ":" ""))))
  10182. rtn)
  10183. ((eq flag t)
  10184. ;; all-completions
  10185. (all-completions s2 ctable confirm)
  10186. )
  10187. ((eq flag 'lambda)
  10188. ;; exact match?
  10189. (assoc s2 ctable)))
  10190. ))
  10191. (defun org-fast-tag-insert (kwd tags face &optional end)
  10192. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10193. (insert (format "%-12s" (concat kwd ":"))
  10194. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10195. (or end "")))
  10196. (defun org-fast-tag-show-exit (flag)
  10197. (save-excursion
  10198. (goto-line 3)
  10199. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10200. (replace-match ""))
  10201. (when flag
  10202. (end-of-line 1)
  10203. (org-move-to-column (- (window-width) 19) t)
  10204. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10205. (defun org-set-current-tags-overlay (current prefix)
  10206. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10207. (if (featurep 'xemacs)
  10208. (org-overlay-display org-tags-overlay (concat prefix s)
  10209. 'secondary-selection)
  10210. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10211. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10212. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10213. "Fast tag selection with single keys.
  10214. CURRENT is the current list of tags in the headline, INHERITED is the
  10215. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10216. possibly with grouping information. TODO-TABLE is a similar table with
  10217. TODO keywords, should these have keys assigned to them.
  10218. If the keys are nil, a-z are automatically assigned.
  10219. Returns the new tags string, or nil to not change the current settings."
  10220. (let* ((fulltable (append table todo-table))
  10221. (maxlen (apply 'max (mapcar
  10222. (lambda (x)
  10223. (if (stringp (car x)) (string-width (car x)) 0))
  10224. fulltable)))
  10225. (buf (current-buffer))
  10226. (expert (eq org-fast-tag-selection-single-key 'expert))
  10227. (buffer-tags nil)
  10228. (fwidth (+ maxlen 3 1 3))
  10229. (ncol (/ (- (window-width) 4) fwidth))
  10230. (i-face 'org-done)
  10231. (c-face 'org-todo)
  10232. tg cnt e c char c1 c2 ntable tbl rtn
  10233. ov-start ov-end ov-prefix
  10234. (exit-after-next org-fast-tag-selection-single-key)
  10235. (done-keywords org-done-keywords)
  10236. groups ingroup)
  10237. (save-excursion
  10238. (beginning-of-line 1)
  10239. (if (looking-at
  10240. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10241. (setq ov-start (match-beginning 1)
  10242. ov-end (match-end 1)
  10243. ov-prefix "")
  10244. (setq ov-start (1- (point-at-eol))
  10245. ov-end (1+ ov-start))
  10246. (skip-chars-forward "^\n\r")
  10247. (setq ov-prefix
  10248. (concat
  10249. (buffer-substring (1- (point)) (point))
  10250. (if (> (current-column) org-tags-column)
  10251. " "
  10252. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10253. (org-move-overlay org-tags-overlay ov-start ov-end)
  10254. (save-window-excursion
  10255. (if expert
  10256. (set-buffer (get-buffer-create " *Org tags*"))
  10257. (delete-other-windows)
  10258. (split-window-vertically)
  10259. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10260. (erase-buffer)
  10261. (org-set-local 'org-done-keywords done-keywords)
  10262. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10263. (org-fast-tag-insert "Current" current c-face "\n\n")
  10264. (org-fast-tag-show-exit exit-after-next)
  10265. (org-set-current-tags-overlay current ov-prefix)
  10266. (setq tbl fulltable char ?a cnt 0)
  10267. (while (setq e (pop tbl))
  10268. (cond
  10269. ((equal e '(:startgroup))
  10270. (push '() groups) (setq ingroup t)
  10271. (when (not (= cnt 0))
  10272. (setq cnt 0)
  10273. (insert "\n"))
  10274. (insert "{ "))
  10275. ((equal e '(:endgroup))
  10276. (setq ingroup nil cnt 0)
  10277. (insert "}\n"))
  10278. ((equal e '(:newline))
  10279. (when (not (= cnt 0))
  10280. (setq cnt 0)
  10281. (insert "\n")
  10282. (setq e (car tbl))
  10283. (while (equal (car tbl) '(:newline))
  10284. (insert "\n")
  10285. (setq tbl (cdr tbl)))))
  10286. (t
  10287. (setq tg (copy-sequence (car e)) c2 nil)
  10288. (if (cdr e)
  10289. (setq c (cdr e))
  10290. ;; automatically assign a character.
  10291. (setq c1 (string-to-char
  10292. (downcase (substring
  10293. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10294. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10295. (while (or (rassoc char ntable) (rassoc char table))
  10296. (setq char (1+ char)))
  10297. (setq c2 c1))
  10298. (setq c (or c2 char)))
  10299. (if ingroup (push tg (car groups)))
  10300. (setq tg (org-add-props tg nil 'face
  10301. (cond
  10302. ((not (assoc tg table))
  10303. (org-get-todo-face tg))
  10304. ((member tg current) c-face)
  10305. ((member tg inherited) i-face)
  10306. (t nil))))
  10307. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10308. (insert "[" c "] " tg (make-string
  10309. (- fwidth 4 (length tg)) ?\ ))
  10310. (push (cons tg c) ntable)
  10311. (when (= (setq cnt (1+ cnt)) ncol)
  10312. (insert "\n")
  10313. (if ingroup (insert " "))
  10314. (setq cnt 0)))))
  10315. (setq ntable (nreverse ntable))
  10316. (insert "\n")
  10317. (goto-char (point-min))
  10318. (if (not expert) (org-fit-window-to-buffer))
  10319. (setq rtn
  10320. (catch 'exit
  10321. (while t
  10322. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10323. (if groups " [!] no groups" " [!]groups")
  10324. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10325. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10326. (cond
  10327. ((= c ?\r) (throw 'exit t))
  10328. ((= c ?!)
  10329. (setq groups (not groups))
  10330. (goto-char (point-min))
  10331. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10332. ((= c ?\C-c)
  10333. (if (not expert)
  10334. (org-fast-tag-show-exit
  10335. (setq exit-after-next (not exit-after-next)))
  10336. (setq expert nil)
  10337. (delete-other-windows)
  10338. (split-window-vertically)
  10339. (org-switch-to-buffer-other-window " *Org tags*")
  10340. (org-fit-window-to-buffer)))
  10341. ((or (= c ?\C-g)
  10342. (and (= c ?q) (not (rassoc c ntable))))
  10343. (org-detach-overlay org-tags-overlay)
  10344. (setq quit-flag t))
  10345. ((= c ?\ )
  10346. (setq current nil)
  10347. (if exit-after-next (setq exit-after-next 'now)))
  10348. ((= c ?\t)
  10349. (condition-case nil
  10350. (setq tg (org-icompleting-read
  10351. "Tag: "
  10352. (or buffer-tags
  10353. (with-current-buffer buf
  10354. (org-get-buffer-tags)))))
  10355. (quit (setq tg "")))
  10356. (when (string-match "\\S-" tg)
  10357. (add-to-list 'buffer-tags (list tg))
  10358. (if (member tg current)
  10359. (setq current (delete tg current))
  10360. (push tg current)))
  10361. (if exit-after-next (setq exit-after-next 'now)))
  10362. ((setq e (rassoc c todo-table) tg (car e))
  10363. (with-current-buffer buf
  10364. (save-excursion (org-todo tg)))
  10365. (if exit-after-next (setq exit-after-next 'now)))
  10366. ((setq e (rassoc c ntable) tg (car e))
  10367. (if (member tg current)
  10368. (setq current (delete tg current))
  10369. (loop for g in groups do
  10370. (if (member tg g)
  10371. (mapc (lambda (x)
  10372. (setq current (delete x current)))
  10373. g)))
  10374. (push tg current))
  10375. (if exit-after-next (setq exit-after-next 'now))))
  10376. ;; Create a sorted list
  10377. (setq current
  10378. (sort current
  10379. (lambda (a b)
  10380. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10381. (if (eq exit-after-next 'now) (throw 'exit t))
  10382. (goto-char (point-min))
  10383. (beginning-of-line 2)
  10384. (delete-region (point) (point-at-eol))
  10385. (org-fast-tag-insert "Current" current c-face)
  10386. (org-set-current-tags-overlay current ov-prefix)
  10387. (while (re-search-forward
  10388. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10389. (setq tg (match-string 1))
  10390. (add-text-properties
  10391. (match-beginning 1) (match-end 1)
  10392. (list 'face
  10393. (cond
  10394. ((member tg current) c-face)
  10395. ((member tg inherited) i-face)
  10396. (t (get-text-property (match-beginning 1) 'face))))))
  10397. (goto-char (point-min)))))
  10398. (org-detach-overlay org-tags-overlay)
  10399. (if rtn
  10400. (mapconcat 'identity current ":")
  10401. nil))))
  10402. (defun org-get-tags-string ()
  10403. "Get the TAGS string in the current headline."
  10404. (unless (org-on-heading-p t)
  10405. (error "Not on a heading"))
  10406. (save-excursion
  10407. (beginning-of-line 1)
  10408. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10409. (org-match-string-no-properties 1)
  10410. "")))
  10411. (defun org-get-tags ()
  10412. "Get the list of tags specified in the current headline."
  10413. (org-split-string (org-get-tags-string) ":"))
  10414. (defun org-get-buffer-tags ()
  10415. "Get a table of all tags used in the buffer, for completion."
  10416. (let (tags)
  10417. (save-excursion
  10418. (goto-char (point-min))
  10419. (while (re-search-forward
  10420. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10421. (when (equal (char-after (point-at-bol 0)) ?*)
  10422. (mapc (lambda (x) (add-to-list 'tags x))
  10423. (org-split-string (org-match-string-no-properties 1) ":")))))
  10424. (mapcar 'list tags)))
  10425. ;;;; The mapping API
  10426. ;;;###autoload
  10427. (defun org-map-entries (func &optional match scope &rest skip)
  10428. "Call FUNC at each headline selected by MATCH in SCOPE.
  10429. FUNC is a function or a lisp form. The function will be called without
  10430. arguments, with the cursor positioned at the beginning of the headline.
  10431. The return values of all calls to the function will be collected and
  10432. returned as a list.
  10433. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10434. does not need to preserve point. After evaluation, the cursor will be
  10435. moved to the end of the line (presumably of the headline of the
  10436. processed entry) and search continues from there. Under some
  10437. circumstances, this may not produce the wanted results. For example,
  10438. if you have removed (e.g. archived) the current (sub)tree it could
  10439. mean that the next entry will be skipped entirely. In such cases, you
  10440. can specify the position from where search should continue by making
  10441. FUNC set the variable `org-map-continue-from' to the desired buffer
  10442. position.
  10443. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10444. Only headlines that are matched by this query will be considered during
  10445. the iteration. When MATCH is nil or t, all headlines will be
  10446. visited by the iteration.
  10447. SCOPE determines the scope of this command. It can be any of:
  10448. nil The current buffer, respecting the restriction if any
  10449. tree The subtree started with the entry at point
  10450. file The current buffer, without restriction
  10451. file-with-archives
  10452. The current buffer, and any archives associated with it
  10453. agenda All agenda files
  10454. agenda-with-archives
  10455. All agenda files with any archive files associated with them
  10456. \(file1 file2 ...)
  10457. If this is a list, all files in the list will be scanned
  10458. The remaining args are treated as settings for the skipping facilities of
  10459. the scanner. The following items can be given here:
  10460. archive skip trees with the archive tag.
  10461. comment skip trees with the COMMENT keyword
  10462. function or Emacs Lisp form:
  10463. will be used as value for `org-agenda-skip-function', so whenever
  10464. the the function returns t, FUNC will not be called for that
  10465. entry and search will continue from the point where the
  10466. function leaves it.
  10467. If your function needs to retrieve the tags including inherited tags
  10468. at the *current* entry, you can use the value of the variable
  10469. `org-scanner-tags' which will be much faster than getting the value
  10470. with `org-get-tags-at'. If your function gets properties with
  10471. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10472. to t around the call to `org-entry-properties' to get the same speedup.
  10473. Note that if your function moves around to retrieve tags and properties at
  10474. a *different* entry, you cannot use these techniques."
  10475. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10476. (org-agenda-skip-archived-trees (memq 'archive skip))
  10477. (org-agenda-skip-comment-trees (memq 'comment skip))
  10478. (org-agenda-skip-function
  10479. (car (org-delete-all '(comment archive) skip)))
  10480. (org-tags-match-list-sublevels t)
  10481. matcher file res
  10482. org-todo-keywords-for-agenda
  10483. org-done-keywords-for-agenda
  10484. org-todo-keyword-alist-for-agenda
  10485. org-tag-alist-for-agenda)
  10486. (cond
  10487. ((eq match t) (setq matcher t))
  10488. ((eq match nil) (setq matcher t))
  10489. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10490. (save-excursion
  10491. (save-restriction
  10492. (when (eq scope 'tree)
  10493. (org-back-to-heading t)
  10494. (org-narrow-to-subtree)
  10495. (setq scope nil))
  10496. (if (not scope)
  10497. (progn
  10498. (org-prepare-agenda-buffers
  10499. (list (buffer-file-name (current-buffer))))
  10500. (setq res (org-scan-tags func matcher)))
  10501. ;; Get the right scope
  10502. (cond
  10503. ((and scope (listp scope) (symbolp (car scope)))
  10504. (setq scope (eval scope)))
  10505. ((eq scope 'agenda)
  10506. (setq scope (org-agenda-files t)))
  10507. ((eq scope 'agenda-with-archives)
  10508. (setq scope (org-agenda-files t))
  10509. (setq scope (org-add-archive-files scope)))
  10510. ((eq scope 'file)
  10511. (setq scope (list (buffer-file-name))))
  10512. ((eq scope 'file-with-archives)
  10513. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10514. (org-prepare-agenda-buffers scope)
  10515. (while (setq file (pop scope))
  10516. (with-current-buffer (org-find-base-buffer-visiting file)
  10517. (save-excursion
  10518. (save-restriction
  10519. (widen)
  10520. (goto-char (point-min))
  10521. (setq res (append res (org-scan-tags func matcher))))))))))
  10522. res))
  10523. ;;;; Properties
  10524. ;;; Setting and retrieving properties
  10525. (defconst org-special-properties
  10526. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10527. "TIMESTAMP" "TIMESTAMP_IA")
  10528. "The special properties valid in Org-mode.
  10529. These are properties that are not defined in the property drawer,
  10530. but in some other way.")
  10531. (defconst org-default-properties
  10532. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10533. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10534. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10535. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10536. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10537. "CLOCK_MODELINE_TOTAL")
  10538. "Some properties that are used by Org-mode for various purposes.
  10539. Being in this list makes sure that they are offered for completion.")
  10540. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10541. "Regular expression matching the first line of a property drawer.")
  10542. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10543. "Regular expression matching the first line of a property drawer.")
  10544. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10545. "Regular expression matching the first line of a property drawer.")
  10546. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10547. "Regular expression matching the first line of a property drawer.")
  10548. (defconst org-property-drawer-re
  10549. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10550. org-property-end-re "\\)\n?")
  10551. "Matches an entire property drawer.")
  10552. (defconst org-clock-drawer-re
  10553. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10554. org-property-end-re "\\)\n?")
  10555. "Matches an entire clock drawer.")
  10556. (defun org-property-action ()
  10557. "Do an action on properties."
  10558. (interactive)
  10559. (let (c)
  10560. (org-at-property-p)
  10561. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10562. (setq c (read-char-exclusive))
  10563. (cond
  10564. ((equal c ?s)
  10565. (call-interactively 'org-set-property))
  10566. ((equal c ?d)
  10567. (call-interactively 'org-delete-property))
  10568. ((equal c ?D)
  10569. (call-interactively 'org-delete-property-globally))
  10570. ((equal c ?c)
  10571. (call-interactively 'org-compute-property-at-point))
  10572. (t (error "No such property action %c" c)))))
  10573. (defun org-at-property-p ()
  10574. "Is the cursor in a property line?"
  10575. ;; FIXME: Does not check if we are actually in the drawer.
  10576. ;; FIXME: also returns true on any drawers.....
  10577. ;; This is used by C-c C-c for property action.
  10578. (save-excursion
  10579. (beginning-of-line 1)
  10580. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10581. (defun org-get-property-block (&optional beg end force)
  10582. "Return the (beg . end) range of the body of the property drawer.
  10583. BEG and END can be beginning and end of subtree, if not given
  10584. they will be found.
  10585. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10586. (catch 'exit
  10587. (save-excursion
  10588. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10589. (end (or end (progn (outline-next-heading) (point)))))
  10590. (goto-char beg)
  10591. (if (re-search-forward org-property-start-re end t)
  10592. (setq beg (1+ (match-end 0)))
  10593. (if force
  10594. (save-excursion
  10595. (org-insert-property-drawer)
  10596. (setq end (progn (outline-next-heading) (point))))
  10597. (throw 'exit nil))
  10598. (goto-char beg)
  10599. (if (re-search-forward org-property-start-re end t)
  10600. (setq beg (1+ (match-end 0)))))
  10601. (if (re-search-forward org-property-end-re end t)
  10602. (setq end (match-beginning 0))
  10603. (or force (throw 'exit nil))
  10604. (goto-char beg)
  10605. (setq end beg)
  10606. (org-indent-line-function)
  10607. (insert ":END:\n"))
  10608. (cons beg end)))))
  10609. (defun org-entry-properties (&optional pom which)
  10610. "Get all properties of the entry at point-or-marker POM.
  10611. This includes the TODO keyword, the tags, time strings for deadline,
  10612. scheduled, and clocking, and any additional properties defined in the
  10613. entry. The return value is an alist, keys may occur multiple times
  10614. if the property key was used several times.
  10615. POM may also be nil, in which case the current entry is used.
  10616. If WHICH is nil or `all', get all properties. If WHICH is
  10617. `special' or `standard', only get that subclass."
  10618. (setq which (or which 'all))
  10619. (org-with-point-at pom
  10620. (let ((clockstr (substring org-clock-string 0 -1))
  10621. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10622. beg end range props sum-props key value string clocksum)
  10623. (save-excursion
  10624. (when (condition-case nil
  10625. (and (org-mode-p) (org-back-to-heading t))
  10626. (error nil))
  10627. (setq beg (point))
  10628. (setq sum-props (get-text-property (point) 'org-summaries))
  10629. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10630. (outline-next-heading)
  10631. (setq end (point))
  10632. (when (memq which '(all special))
  10633. ;; Get the special properties, like TODO and tags
  10634. (goto-char beg)
  10635. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10636. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10637. (when (looking-at org-priority-regexp)
  10638. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10639. (when (and (setq value (org-get-tags-string))
  10640. (string-match "\\S-" value))
  10641. (push (cons "TAGS" value) props))
  10642. (when (setq value (org-get-tags-at))
  10643. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10644. props))
  10645. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10646. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10647. string (if (equal key clockstr)
  10648. (org-no-properties
  10649. (org-trim
  10650. (buffer-substring
  10651. (match-beginning 3) (goto-char (point-at-eol)))))
  10652. (substring (org-match-string-no-properties 3) 1 -1)))
  10653. (unless key
  10654. (if (= (char-after (match-beginning 3)) ?\[)
  10655. (setq key "TIMESTAMP_IA")
  10656. (setq key "TIMESTAMP")))
  10657. (when (or (equal key clockstr) (not (assoc key props)))
  10658. (push (cons key string) props)))
  10659. )
  10660. (when (memq which '(all standard))
  10661. ;; Get the standard properties, like :PROP: ...
  10662. (setq range (org-get-property-block beg end))
  10663. (when range
  10664. (goto-char (car range))
  10665. (while (re-search-forward
  10666. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10667. (cdr range) t)
  10668. (setq key (org-match-string-no-properties 1)
  10669. value (org-trim (or (org-match-string-no-properties 2) "")))
  10670. (unless (member key excluded)
  10671. (push (cons key (or value "")) props)))))
  10672. (if clocksum
  10673. (push (cons "CLOCKSUM"
  10674. (org-columns-number-to-string (/ (float clocksum) 60.)
  10675. 'add_times))
  10676. props))
  10677. (unless (assoc "CATEGORY" props)
  10678. (setq value (or (org-get-category)
  10679. (progn (org-refresh-category-properties)
  10680. (org-get-category))))
  10681. (push (cons "CATEGORY" value) props))
  10682. (append sum-props (nreverse props)))))))
  10683. (defun org-entry-get (pom property &optional inherit)
  10684. "Get value of PROPERTY for entry at point-or-marker POM.
  10685. If INHERIT is non-nil and the entry does not have the property,
  10686. then also check higher levels of the hierarchy.
  10687. If INHERIT is the symbol `selective', use inheritance only if the setting
  10688. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10689. If the property is present but empty, the return value is the empty string.
  10690. If the property is not present at all, nil is returned."
  10691. (org-with-point-at pom
  10692. (if (and inherit (if (eq inherit 'selective)
  10693. (org-property-inherit-p property)
  10694. t))
  10695. (org-entry-get-with-inheritance property)
  10696. (if (member property org-special-properties)
  10697. ;; We need a special property. Use brute force, get all properties.
  10698. (cdr (assoc property (org-entry-properties nil 'special)))
  10699. (let ((range (org-get-property-block)))
  10700. (if (and range
  10701. (goto-char (car range))
  10702. (re-search-forward
  10703. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10704. (cdr range) t))
  10705. ;; Found the property, return it.
  10706. (if (match-end 1)
  10707. (org-match-string-no-properties 1)
  10708. "")))))))
  10709. (defun org-property-or-variable-value (var &optional inherit)
  10710. "Check if there is a property fixing the value of VAR.
  10711. If yes, return this value. If not, return the current value of the variable."
  10712. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10713. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10714. (read prop)
  10715. (symbol-value var))))
  10716. (defun org-entry-delete (pom property)
  10717. "Delete the property PROPERTY from entry at point-or-marker POM."
  10718. (org-with-point-at pom
  10719. (if (member property org-special-properties)
  10720. nil ; cannot delete these properties.
  10721. (let ((range (org-get-property-block)))
  10722. (if (and range
  10723. (goto-char (car range))
  10724. (re-search-forward
  10725. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10726. (cdr range) t))
  10727. (progn
  10728. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10729. t)
  10730. nil)))))
  10731. ;; Multi-values properties are properties that contain multiple values
  10732. ;; These values are assumed to be single words, separated by whitespace.
  10733. (defun org-entry-add-to-multivalued-property (pom property value)
  10734. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10735. (let* ((old (org-entry-get pom property))
  10736. (values (and old (org-split-string old "[ \t]"))))
  10737. (setq value (org-entry-protect-space value))
  10738. (unless (member value values)
  10739. (setq values (cons value values))
  10740. (org-entry-put pom property
  10741. (mapconcat 'identity values " ")))))
  10742. (defun org-entry-remove-from-multivalued-property (pom property value)
  10743. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10744. (let* ((old (org-entry-get pom property))
  10745. (values (and old (org-split-string old "[ \t]"))))
  10746. (setq value (org-entry-protect-space value))
  10747. (when (member value values)
  10748. (setq values (delete value values))
  10749. (org-entry-put pom property
  10750. (mapconcat 'identity values " ")))))
  10751. (defun org-entry-member-in-multivalued-property (pom property value)
  10752. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10753. (let* ((old (org-entry-get pom property))
  10754. (values (and old (org-split-string old "[ \t]"))))
  10755. (setq value (org-entry-protect-space value))
  10756. (member value values)))
  10757. (defun org-entry-get-multivalued-property (pom property)
  10758. "Return a list of values in a multivalued property."
  10759. (let* ((value (org-entry-get pom property))
  10760. (values (and value (org-split-string value "[ \t]"))))
  10761. (mapcar 'org-entry-restore-space values)))
  10762. (defun org-entry-put-multivalued-property (pom property &rest values)
  10763. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10764. VALUES should be a list of strings. Spaces will be protected."
  10765. (org-entry-put pom property
  10766. (mapconcat 'org-entry-protect-space values " "))
  10767. (let* ((value (org-entry-get pom property))
  10768. (values (and value (org-split-string value "[ \t]"))))
  10769. (mapcar 'org-entry-restore-space values)))
  10770. (defun org-entry-protect-space (s)
  10771. "Protect spaces and newline in string S."
  10772. (while (string-match " " s)
  10773. (setq s (replace-match "%20" t t s)))
  10774. (while (string-match "\n" s)
  10775. (setq s (replace-match "%0A" t t s)))
  10776. s)
  10777. (defun org-entry-restore-space (s)
  10778. "Restore spaces and newline in string S."
  10779. (while (string-match "%20" s)
  10780. (setq s (replace-match " " t t s)))
  10781. (while (string-match "%0A" s)
  10782. (setq s (replace-match "\n" t t s)))
  10783. s)
  10784. (defvar org-entry-property-inherited-from (make-marker)
  10785. "Marker pointing to the entry from where a property was inherited.
  10786. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10787. location of the entry where the inheritance search matched. If there was
  10788. no match, the marker will point nowhere.
  10789. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10790. is set.")
  10791. (defun org-entry-get-with-inheritance (property)
  10792. "Get entry property, and search higher levels if not present."
  10793. (move-marker org-entry-property-inherited-from nil)
  10794. (let (tmp)
  10795. (save-excursion
  10796. (save-restriction
  10797. (widen)
  10798. (catch 'ex
  10799. (while t
  10800. (when (setq tmp (org-entry-get nil property))
  10801. (org-back-to-heading t)
  10802. (move-marker org-entry-property-inherited-from (point))
  10803. (throw 'ex tmp))
  10804. (or (org-up-heading-safe) (throw 'ex nil)))))
  10805. (or tmp
  10806. (cdr (assoc property org-file-properties))
  10807. (cdr (assoc property org-global-properties))
  10808. (cdr (assoc property org-global-properties-fixed))))))
  10809. (defun org-entry-put (pom property value)
  10810. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10811. (org-with-point-at pom
  10812. (org-back-to-heading t)
  10813. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10814. range)
  10815. (cond
  10816. ((equal property "TODO")
  10817. (when (and (stringp value) (string-match "\\S-" value)
  10818. (not (member value org-todo-keywords-1)))
  10819. (error "\"%s\" is not a valid TODO state" value))
  10820. (if (or (not value)
  10821. (not (string-match "\\S-" value)))
  10822. (setq value 'none))
  10823. (org-todo value)
  10824. (org-set-tags nil 'align))
  10825. ((equal property "PRIORITY")
  10826. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10827. (string-to-char value) ?\ ))
  10828. (org-set-tags nil 'align))
  10829. ((equal property "SCHEDULED")
  10830. (if (re-search-forward org-scheduled-time-regexp end t)
  10831. (cond
  10832. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10833. ((eq value 'later) (org-timestamp-change 1 'day))
  10834. (t (call-interactively 'org-schedule)))
  10835. (call-interactively 'org-schedule)))
  10836. ((equal property "DEADLINE")
  10837. (if (re-search-forward org-deadline-time-regexp end t)
  10838. (cond
  10839. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10840. ((eq value 'later) (org-timestamp-change 1 'day))
  10841. (t (call-interactively 'org-deadline)))
  10842. (call-interactively 'org-deadline)))
  10843. ((member property org-special-properties)
  10844. (error "The %s property can not yet be set with `org-entry-put'"
  10845. property))
  10846. (t ; a non-special property
  10847. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10848. (setq range (org-get-property-block beg end 'force))
  10849. (goto-char (car range))
  10850. (if (re-search-forward
  10851. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10852. (progn
  10853. (delete-region (match-beginning 1) (match-end 1))
  10854. (goto-char (match-beginning 1)))
  10855. (goto-char (cdr range))
  10856. (insert "\n")
  10857. (backward-char 1)
  10858. (org-indent-line-function)
  10859. (insert ":" property ":"))
  10860. (and value (insert " " value))
  10861. (org-indent-line-function)))))))
  10862. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10863. "Get all property keys in the current buffer.
  10864. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10865. like tags and TODO state.
  10866. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10867. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10868. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10869. formats in the current buffer."
  10870. (let (rtn range cfmt s p)
  10871. (save-excursion
  10872. (save-restriction
  10873. (widen)
  10874. (goto-char (point-min))
  10875. (while (re-search-forward org-property-start-re nil t)
  10876. (setq range (org-get-property-block))
  10877. (goto-char (car range))
  10878. (while (re-search-forward
  10879. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10880. (cdr range) t)
  10881. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10882. (outline-next-heading))))
  10883. (when include-specials
  10884. (setq rtn (append org-special-properties rtn)))
  10885. (when include-defaults
  10886. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10887. (add-to-list 'rtn org-effort-property))
  10888. (when include-columns
  10889. (save-excursion
  10890. (save-restriction
  10891. (widen)
  10892. (goto-char (point-min))
  10893. (while (re-search-forward
  10894. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10895. nil t)
  10896. (setq cfmt (match-string 2) s 0)
  10897. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10898. cfmt s)
  10899. (setq s (match-end 0)
  10900. p (match-string 1 cfmt))
  10901. (unless (or (equal p "ITEM")
  10902. (member p org-special-properties))
  10903. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10904. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10905. (defun org-property-values (key)
  10906. "Return a list of all values of property KEY."
  10907. (save-excursion
  10908. (save-restriction
  10909. (widen)
  10910. (goto-char (point-min))
  10911. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10912. values)
  10913. (while (re-search-forward re nil t)
  10914. (add-to-list 'values (org-trim (match-string 1))))
  10915. (delete "" values)))))
  10916. (defun org-insert-property-drawer ()
  10917. "Insert a property drawer into the current entry."
  10918. (interactive)
  10919. (org-back-to-heading t)
  10920. (looking-at outline-regexp)
  10921. (let ((indent (if org-adapt-indentation
  10922. (- (match-end 0)(match-beginning 0))
  10923. 0))
  10924. (beg (point))
  10925. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10926. end hiddenp)
  10927. (outline-next-heading)
  10928. (setq end (point))
  10929. (goto-char beg)
  10930. (while (re-search-forward re end t))
  10931. (setq hiddenp (org-invisible-p))
  10932. (end-of-line 1)
  10933. (and (equal (char-after) ?\n) (forward-char 1))
  10934. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10935. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10936. ;; just skip this line
  10937. (beginning-of-line 2)
  10938. ;; Drawer start, find the end
  10939. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10940. (beginning-of-line 1)))
  10941. (org-skip-over-state-notes)
  10942. (skip-chars-backward " \t\n\r")
  10943. (if (eq (char-before) ?*) (forward-char 1))
  10944. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10945. (beginning-of-line 0)
  10946. (org-indent-to-column indent)
  10947. (beginning-of-line 2)
  10948. (org-indent-to-column indent)
  10949. (beginning-of-line 0)
  10950. (if hiddenp
  10951. (save-excursion
  10952. (org-back-to-heading t)
  10953. (hide-entry))
  10954. (org-flag-drawer t))))
  10955. (defun org-set-property (property value)
  10956. "In the current entry, set PROPERTY to VALUE.
  10957. When called interactively, this will prompt for a property name, offering
  10958. completion on existing and default properties. And then it will prompt
  10959. for a value, offering completion either on allowed values (via an inherited
  10960. xxx_ALL property) or on existing values in other instances of this property
  10961. in the current file."
  10962. (interactive
  10963. (let* ((completion-ignore-case t)
  10964. (keys (org-buffer-property-keys nil t t))
  10965. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  10966. (prop (if (member prop0 keys)
  10967. prop0
  10968. (or (cdr (assoc (downcase prop0)
  10969. (mapcar (lambda (x) (cons (downcase x) x))
  10970. keys)))
  10971. prop0)))
  10972. (cur (org-entry-get nil prop))
  10973. (allowed (org-property-get-allowed-values nil prop 'table))
  10974. (existing (mapcar 'list (org-property-values prop)))
  10975. (val (if allowed
  10976. (org-completing-read "Value: " allowed nil 'req-match)
  10977. (let (org-completion-use-ido org-completion-use-iswitchb)
  10978. (org-completing-read
  10979. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10980. (concat "[" cur "]") "")
  10981. ": ")
  10982. existing nil nil "" nil cur)))))
  10983. (list prop (if (equal val "") cur val))))
  10984. (unless (equal (org-entry-get nil property) value)
  10985. (org-entry-put nil property value)))
  10986. (defun org-delete-property (property)
  10987. "In the current entry, delete PROPERTY."
  10988. (interactive
  10989. (let* ((completion-ignore-case t)
  10990. (prop (org-icompleting-read
  10991. "Property: " (org-entry-properties nil 'standard))))
  10992. (list prop)))
  10993. (message "Property %s %s" property
  10994. (if (org-entry-delete nil property)
  10995. "deleted"
  10996. "was not present in the entry")))
  10997. (defun org-delete-property-globally (property)
  10998. "Remove PROPERTY globally, from all entries."
  10999. (interactive
  11000. (let* ((completion-ignore-case t)
  11001. (prop (org-icompleting-read
  11002. "Globally remove property: "
  11003. (mapcar 'list (org-buffer-property-keys)))))
  11004. (list prop)))
  11005. (save-excursion
  11006. (save-restriction
  11007. (widen)
  11008. (goto-char (point-min))
  11009. (let ((cnt 0))
  11010. (while (re-search-forward
  11011. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11012. nil t)
  11013. (setq cnt (1+ cnt))
  11014. (replace-match ""))
  11015. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11016. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11017. (defun org-compute-property-at-point ()
  11018. "Compute the property at point.
  11019. This looks for an enclosing column format, extracts the operator and
  11020. then applies it to the property in the column format's scope."
  11021. (interactive)
  11022. (unless (org-at-property-p)
  11023. (error "Not at a property"))
  11024. (let ((prop (org-match-string-no-properties 2)))
  11025. (org-columns-get-format-and-top-level)
  11026. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11027. (error "No operator defined for property %s" prop))
  11028. (org-columns-compute prop)))
  11029. (defun org-property-get-allowed-values (pom property &optional table)
  11030. "Get allowed values for the property PROPERTY.
  11031. When TABLE is non-nil, return an alist that can directly be used for
  11032. completion."
  11033. (let (vals)
  11034. (cond
  11035. ((equal property "TODO")
  11036. (setq vals (org-with-point-at pom
  11037. (append org-todo-keywords-1 '("")))))
  11038. ((equal property "PRIORITY")
  11039. (let ((n org-lowest-priority))
  11040. (while (>= n org-highest-priority)
  11041. (push (char-to-string n) vals)
  11042. (setq n (1- n)))))
  11043. ((member property org-special-properties))
  11044. (t
  11045. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11046. (when (and vals (string-match "\\S-" vals))
  11047. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11048. (setq vals (mapcar (lambda (x)
  11049. (cond ((stringp x) x)
  11050. ((numberp x) (number-to-string x))
  11051. ((symbolp x) (symbol-name x))
  11052. (t "???")))
  11053. vals)))))
  11054. (if table (mapcar 'list vals) vals)))
  11055. (defun org-property-previous-allowed-value (&optional previous)
  11056. "Switch to the next allowed value for this property."
  11057. (interactive)
  11058. (org-property-next-allowed-value t))
  11059. (defun org-property-next-allowed-value (&optional previous)
  11060. "Switch to the next allowed value for this property."
  11061. (interactive)
  11062. (unless (org-at-property-p)
  11063. (error "Not at a property"))
  11064. (let* ((key (match-string 2))
  11065. (value (match-string 3))
  11066. (allowed (or (org-property-get-allowed-values (point) key)
  11067. (and (member value '("[ ]" "[-]" "[X]"))
  11068. '("[ ]" "[X]"))))
  11069. nval)
  11070. (unless allowed
  11071. (error "Allowed values for this property have not been defined"))
  11072. (if previous (setq allowed (reverse allowed)))
  11073. (if (member value allowed)
  11074. (setq nval (car (cdr (member value allowed)))))
  11075. (setq nval (or nval (car allowed)))
  11076. (if (equal nval value)
  11077. (error "Only one allowed value for this property"))
  11078. (org-at-property-p)
  11079. (replace-match (concat " :" key ": " nval) t t)
  11080. (org-indent-line-function)
  11081. (beginning-of-line 1)
  11082. (skip-chars-forward " \t")))
  11083. (defun org-find-entry-with-id (ident)
  11084. "Locate the entry that contains the ID property with exact value IDENT.
  11085. IDENT can be a string, a symbol or a number, this function will search for
  11086. the string representation of it.
  11087. Return the position where this entry starts, or nil if there is no such entry."
  11088. (interactive "sID: ")
  11089. (let ((id (cond
  11090. ((stringp ident) ident)
  11091. ((symbol-name ident) (symbol-name ident))
  11092. ((numberp ident) (number-to-string ident))
  11093. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11094. (case-fold-search nil))
  11095. (save-excursion
  11096. (save-restriction
  11097. (widen)
  11098. (goto-char (point-min))
  11099. (when (re-search-forward
  11100. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11101. nil t)
  11102. (org-back-to-heading t)
  11103. (point))))))
  11104. ;;;; Timestamps
  11105. (defvar org-last-changed-timestamp nil)
  11106. (defvar org-last-inserted-timestamp nil
  11107. "The last time stamp inserted with `org-insert-time-stamp'.")
  11108. (defvar org-time-was-given) ; dynamically scoped parameter
  11109. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11110. (defvar org-ts-what) ; dynamically scoped parameter
  11111. (defun org-time-stamp (arg &optional inactive)
  11112. "Prompt for a date/time and insert a time stamp.
  11113. If the user specifies a time like HH:MM, or if this command is called
  11114. with a prefix argument, the time stamp will contain date and time.
  11115. Otherwise, only the date will be included. All parts of a date not
  11116. specified by the user will be filled in from the current date/time.
  11117. So if you press just return without typing anything, the time stamp
  11118. will represent the current date/time. If there is already a timestamp
  11119. at the cursor, it will be modified."
  11120. (interactive "P")
  11121. (let* ((ts nil)
  11122. (default-time
  11123. ;; Default time is either today, or, when entering a range,
  11124. ;; the range start.
  11125. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11126. (save-excursion
  11127. (re-search-backward
  11128. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11129. (- (point) 20) t)))
  11130. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11131. (current-time)))
  11132. (default-input (and ts (org-get-compact-tod ts)))
  11133. org-time-was-given org-end-time-was-given time)
  11134. (cond
  11135. ((and (org-at-timestamp-p t)
  11136. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11137. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11138. (insert "--")
  11139. (setq time (let ((this-command this-command))
  11140. (org-read-date arg 'totime nil nil
  11141. default-time default-input)))
  11142. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11143. ((org-at-timestamp-p t)
  11144. (setq time (let ((this-command this-command))
  11145. (org-read-date arg 'totime nil nil default-time default-input)))
  11146. (when (org-at-timestamp-p t) ; just to get the match data
  11147. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11148. (replace-match "")
  11149. (setq org-last-changed-timestamp
  11150. (org-insert-time-stamp
  11151. time (or org-time-was-given arg)
  11152. inactive nil nil (list org-end-time-was-given))))
  11153. (message "Timestamp updated"))
  11154. (t
  11155. (setq time (let ((this-command this-command))
  11156. (org-read-date arg 'totime nil nil default-time default-input)))
  11157. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11158. nil nil (list org-end-time-was-given))))))
  11159. ;; FIXME: can we use this for something else, like computing time differences?
  11160. (defun org-get-compact-tod (s)
  11161. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11162. (let* ((t1 (match-string 1 s))
  11163. (h1 (string-to-number (match-string 2 s)))
  11164. (m1 (string-to-number (match-string 3 s)))
  11165. (t2 (and (match-end 4) (match-string 5 s)))
  11166. (h2 (and t2 (string-to-number (match-string 6 s))))
  11167. (m2 (and t2 (string-to-number (match-string 7 s))))
  11168. dh dm)
  11169. (if (not t2)
  11170. t1
  11171. (setq dh (- h2 h1) dm (- m2 m1))
  11172. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11173. (concat t1 "+" (number-to-string dh)
  11174. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11175. (defun org-time-stamp-inactive (&optional arg)
  11176. "Insert an inactive time stamp.
  11177. An inactive time stamp is enclosed in square brackets instead of angle
  11178. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11179. does not link to the calendar and cannot be changed with the S-cursor keys.
  11180. So these are more for recording a certain time/date."
  11181. (interactive "P")
  11182. (org-time-stamp arg 'inactive))
  11183. (defvar org-date-ovl (org-make-overlay 1 1))
  11184. (org-overlay-put org-date-ovl 'face 'org-warning)
  11185. (org-detach-overlay org-date-ovl)
  11186. (defvar org-ans1) ; dynamically scoped parameter
  11187. (defvar org-ans2) ; dynamically scoped parameter
  11188. (defvar org-plain-time-of-day-regexp) ; defined below
  11189. (defvar org-overriding-default-time nil) ; dynamically scoped
  11190. (defvar org-read-date-overlay nil)
  11191. (defvar org-dcst nil) ; dynamically scoped
  11192. (defvar org-read-date-history nil)
  11193. (defvar org-read-date-final-answer nil)
  11194. (defun org-read-date (&optional with-time to-time from-string prompt
  11195. default-time default-input)
  11196. "Read a date, possibly a time, and make things smooth for the user.
  11197. The prompt will suggest to enter an ISO date, but you can also enter anything
  11198. which will at least partially be understood by `parse-time-string'.
  11199. Unrecognized parts of the date will default to the current day, month, year,
  11200. hour and minute. If this command is called to replace a timestamp at point,
  11201. of to enter the second timestamp of a range, the default time is taken from the
  11202. existing stamp. For example,
  11203. 3-2-5 --> 2003-02-05
  11204. feb 15 --> currentyear-02-15
  11205. sep 12 9 --> 2009-09-12
  11206. 12:45 --> today 12:45
  11207. 22 sept 0:34 --> currentyear-09-22 0:34
  11208. 12 --> currentyear-currentmonth-12
  11209. Fri --> nearest Friday (today or later)
  11210. etc.
  11211. Furthermore you can specify a relative date by giving, as the *first* thing
  11212. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11213. change in days weeks, months, years.
  11214. With a single plus or minus, the date is relative to today. With a double
  11215. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11216. +4d --> four days from today
  11217. +4 --> same as above
  11218. +2w --> two weeks from today
  11219. ++5 --> five days from default date
  11220. The function understands only English month and weekday abbreviations,
  11221. but this can be configured with the variables `parse-time-months' and
  11222. `parse-time-weekdays'.
  11223. While prompting, a calendar is popped up - you can also select the
  11224. date with the mouse (button 1). The calendar shows a period of three
  11225. months. To scroll it to other months, use the keys `>' and `<'.
  11226. If you don't like the calendar, turn it off with
  11227. \(setq org-read-date-popup-calendar nil)
  11228. With optional argument TO-TIME, the date will immediately be converted
  11229. to an internal time.
  11230. With an optional argument WITH-TIME, the prompt will suggest to also
  11231. insert a time. Note that when WITH-TIME is not set, you can still
  11232. enter a time, and this function will inform the calling routine about
  11233. this change. The calling routine may then choose to change the format
  11234. used to insert the time stamp into the buffer to include the time.
  11235. With optional argument FROM-STRING, read from this string instead from
  11236. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11237. the time/date that is used for everything that is not specified by the
  11238. user."
  11239. (require 'parse-time)
  11240. (let* ((org-time-stamp-rounding-minutes
  11241. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11242. (org-dcst org-display-custom-times)
  11243. (ct (org-current-time))
  11244. (def (or org-overriding-default-time default-time ct))
  11245. (defdecode (decode-time def))
  11246. (dummy (progn
  11247. (when (< (nth 2 defdecode) org-extend-today-until)
  11248. (setcar (nthcdr 2 defdecode) -1)
  11249. (setcar (nthcdr 1 defdecode) 59)
  11250. (setq def (apply 'encode-time defdecode)
  11251. defdecode (decode-time def)))))
  11252. (calendar-frame-setup nil)
  11253. (calendar-move-hook nil)
  11254. (calendar-view-diary-initially-flag nil)
  11255. (view-diary-entries-initially nil)
  11256. (calendar-view-holidays-initially-flag nil)
  11257. (view-calendar-holidays-initially nil)
  11258. (timestr (format-time-string
  11259. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11260. (prompt (concat (if prompt (concat prompt " ") "")
  11261. (format "Date+time [%s]: " timestr)))
  11262. ans (org-ans0 "") org-ans1 org-ans2 final)
  11263. (cond
  11264. (from-string (setq ans from-string))
  11265. (org-read-date-popup-calendar
  11266. (save-excursion
  11267. (save-window-excursion
  11268. (calendar)
  11269. (calendar-forward-day (- (time-to-days def)
  11270. (calendar-absolute-from-gregorian
  11271. (calendar-current-date))))
  11272. (org-eval-in-calendar nil t)
  11273. (let* ((old-map (current-local-map))
  11274. (map (copy-keymap calendar-mode-map))
  11275. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11276. (org-defkey map (kbd "RET") 'org-calendar-select)
  11277. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11278. 'org-calendar-select-mouse)
  11279. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11280. 'org-calendar-select-mouse)
  11281. (org-defkey minibuffer-local-map [(meta shift left)]
  11282. (lambda () (interactive)
  11283. (org-eval-in-calendar '(calendar-backward-month 1))))
  11284. (org-defkey minibuffer-local-map [(meta shift right)]
  11285. (lambda () (interactive)
  11286. (org-eval-in-calendar '(calendar-forward-month 1))))
  11287. (org-defkey minibuffer-local-map [(meta shift up)]
  11288. (lambda () (interactive)
  11289. (org-eval-in-calendar '(calendar-backward-year 1))))
  11290. (org-defkey minibuffer-local-map [(meta shift down)]
  11291. (lambda () (interactive)
  11292. (org-eval-in-calendar '(calendar-forward-year 1))))
  11293. (org-defkey minibuffer-local-map [?\e (shift left)]
  11294. (lambda () (interactive)
  11295. (org-eval-in-calendar '(calendar-backward-month 1))))
  11296. (org-defkey minibuffer-local-map [?\e (shift right)]
  11297. (lambda () (interactive)
  11298. (org-eval-in-calendar '(calendar-forward-month 1))))
  11299. (org-defkey minibuffer-local-map [?\e (shift up)]
  11300. (lambda () (interactive)
  11301. (org-eval-in-calendar '(calendar-backward-year 1))))
  11302. (org-defkey minibuffer-local-map [?\e (shift down)]
  11303. (lambda () (interactive)
  11304. (org-eval-in-calendar '(calendar-forward-year 1))))
  11305. (org-defkey minibuffer-local-map [(shift up)]
  11306. (lambda () (interactive)
  11307. (org-eval-in-calendar '(calendar-backward-week 1))))
  11308. (org-defkey minibuffer-local-map [(shift down)]
  11309. (lambda () (interactive)
  11310. (org-eval-in-calendar '(calendar-forward-week 1))))
  11311. (org-defkey minibuffer-local-map [(shift left)]
  11312. (lambda () (interactive)
  11313. (org-eval-in-calendar '(calendar-backward-day 1))))
  11314. (org-defkey minibuffer-local-map [(shift right)]
  11315. (lambda () (interactive)
  11316. (org-eval-in-calendar '(calendar-forward-day 1))))
  11317. (org-defkey minibuffer-local-map ">"
  11318. (lambda () (interactive)
  11319. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11320. (org-defkey minibuffer-local-map "<"
  11321. (lambda () (interactive)
  11322. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11323. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11324. (unwind-protect
  11325. (progn
  11326. (use-local-map map)
  11327. (add-hook 'post-command-hook 'org-read-date-display)
  11328. (setq org-ans0 (read-string prompt default-input
  11329. 'org-read-date-history nil))
  11330. ;; org-ans0: from prompt
  11331. ;; org-ans1: from mouse click
  11332. ;; org-ans2: from calendar motion
  11333. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11334. (remove-hook 'post-command-hook 'org-read-date-display)
  11335. (use-local-map old-map)
  11336. (when org-read-date-overlay
  11337. (org-delete-overlay org-read-date-overlay)
  11338. (setq org-read-date-overlay nil)))))))
  11339. (t ; Naked prompt only
  11340. (unwind-protect
  11341. (setq ans (read-string prompt default-input
  11342. 'org-read-date-history timestr))
  11343. (when org-read-date-overlay
  11344. (org-delete-overlay org-read-date-overlay)
  11345. (setq org-read-date-overlay nil)))))
  11346. (setq final (org-read-date-analyze ans def defdecode))
  11347. (setq org-read-date-final-answer ans)
  11348. (if to-time
  11349. (apply 'encode-time final)
  11350. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11351. (format "%04d-%02d-%02d %02d:%02d"
  11352. (nth 5 final) (nth 4 final) (nth 3 final)
  11353. (nth 2 final) (nth 1 final))
  11354. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11355. (defvar def)
  11356. (defvar defdecode)
  11357. (defvar with-time)
  11358. (defun org-read-date-display ()
  11359. "Display the current date prompt interpretation in the minibuffer."
  11360. (when org-read-date-display-live
  11361. (when org-read-date-overlay
  11362. (org-delete-overlay org-read-date-overlay))
  11363. (let ((p (point)))
  11364. (end-of-line 1)
  11365. (while (not (equal (buffer-substring
  11366. (max (point-min) (- (point) 4)) (point))
  11367. " "))
  11368. (insert " "))
  11369. (goto-char p))
  11370. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11371. " " (or org-ans1 org-ans2)))
  11372. (org-end-time-was-given nil)
  11373. (f (org-read-date-analyze ans def defdecode))
  11374. (fmts (if org-dcst
  11375. org-time-stamp-custom-formats
  11376. org-time-stamp-formats))
  11377. (fmt (if (or with-time
  11378. (and (boundp 'org-time-was-given) org-time-was-given))
  11379. (cdr fmts)
  11380. (car fmts)))
  11381. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11382. (when (and org-end-time-was-given
  11383. (string-match org-plain-time-of-day-regexp txt))
  11384. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11385. org-end-time-was-given
  11386. (substring txt (match-end 0)))))
  11387. (setq org-read-date-overlay
  11388. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11389. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11390. (defun org-read-date-analyze (ans def defdecode)
  11391. "Analyse the combined answer of the date prompt."
  11392. ;; FIXME: cleanup and comment
  11393. (let (delta deltan deltaw deltadef year month day
  11394. hour minute second wday pm h2 m2 tl wday1
  11395. iso-year iso-weekday iso-week iso-year iso-date)
  11396. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11397. (setq ans "+0"))
  11398. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11399. (setq ans (replace-match "" t t ans)
  11400. deltan (car delta)
  11401. deltaw (nth 1 delta)
  11402. deltadef (nth 2 delta)))
  11403. ;; Check if there is an iso week date in there
  11404. ;; If yes, sore the info and postpone interpreting it until the rest
  11405. ;; of the parsing is done
  11406. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11407. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11408. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11409. iso-week (string-to-number (match-string 2 ans)))
  11410. (setq ans (replace-match "" t t ans)))
  11411. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11412. (when (string-match
  11413. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11414. (setq year (if (match-end 2)
  11415. (string-to-number (match-string 2 ans))
  11416. (string-to-number (format-time-string "%Y")))
  11417. month (string-to-number (match-string 3 ans))
  11418. day (string-to-number (match-string 4 ans)))
  11419. (if (< year 100) (setq year (+ 2000 year)))
  11420. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11421. t nil ans)))
  11422. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11423. ;; If there is a time with am/pm, and *no* time without it, we convert
  11424. ;; so that matching will be successful.
  11425. (loop for i from 1 to 2 do ; twice, for end time as well
  11426. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11427. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11428. (setq hour (string-to-number (match-string 1 ans))
  11429. minute (if (match-end 3)
  11430. (string-to-number (match-string 3 ans))
  11431. 0)
  11432. pm (equal ?p
  11433. (string-to-char (downcase (match-string 4 ans)))))
  11434. (if (and (= hour 12) (not pm))
  11435. (setq hour 0)
  11436. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11437. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11438. t t ans))))
  11439. ;; Check if a time range is given as a duration
  11440. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11441. (setq hour (string-to-number (match-string 1 ans))
  11442. h2 (+ hour (string-to-number (match-string 3 ans)))
  11443. minute (string-to-number (match-string 2 ans))
  11444. m2 (+ minute (if (match-end 5) (string-to-number
  11445. (match-string 5 ans))0)))
  11446. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11447. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11448. t t ans)))
  11449. ;; Check if there is a time range
  11450. (when (boundp 'org-end-time-was-given)
  11451. (setq org-time-was-given nil)
  11452. (when (and (string-match org-plain-time-of-day-regexp ans)
  11453. (match-end 8))
  11454. (setq org-end-time-was-given (match-string 8 ans))
  11455. (setq ans (concat (substring ans 0 (match-beginning 7))
  11456. (substring ans (match-end 7))))))
  11457. (setq tl (parse-time-string ans)
  11458. day (or (nth 3 tl) (nth 3 defdecode))
  11459. month (or (nth 4 tl)
  11460. (if (and org-read-date-prefer-future
  11461. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11462. (1+ (nth 4 defdecode))
  11463. (nth 4 defdecode)))
  11464. year (or (nth 5 tl)
  11465. (if (and org-read-date-prefer-future
  11466. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11467. (1+ (nth 5 defdecode))
  11468. (nth 5 defdecode)))
  11469. hour (or (nth 2 tl) (nth 2 defdecode))
  11470. minute (or (nth 1 tl) (nth 1 defdecode))
  11471. second (or (nth 0 tl) 0)
  11472. wday (nth 6 tl))
  11473. ;; Special date definitions below
  11474. (cond
  11475. (iso-week
  11476. ;; There was an iso week
  11477. (setq year (or iso-year year)
  11478. day (or iso-weekday wday 1)
  11479. wday nil ; to make sure that the trigger below does not match
  11480. iso-date (calendar-gregorian-from-absolute
  11481. (calendar-absolute-from-iso
  11482. (list iso-week day year))))
  11483. ; FIXME: Should we also push ISO weeks into the future?
  11484. ; (when (and org-read-date-prefer-future
  11485. ; (not iso-year)
  11486. ; (< (calendar-absolute-from-gregorian iso-date)
  11487. ; (time-to-days (current-time))))
  11488. ; (setq year (1+ year)
  11489. ; iso-date (calendar-gregorian-from-absolute
  11490. ; (calendar-absolute-from-iso
  11491. ; (list iso-week day year)))))
  11492. (setq month (car iso-date)
  11493. year (nth 2 iso-date)
  11494. day (nth 1 iso-date)))
  11495. (deltan
  11496. (unless deltadef
  11497. (let ((now (decode-time (current-time))))
  11498. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11499. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11500. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11501. ((equal deltaw "m") (setq month (+ month deltan)))
  11502. ((equal deltaw "y") (setq year (+ year deltan)))))
  11503. ((and wday (not (nth 3 tl)))
  11504. ;; Weekday was given, but no day, so pick that day in the week
  11505. ;; on or after the derived date.
  11506. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11507. (unless (equal wday wday1)
  11508. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11509. (if (and (boundp 'org-time-was-given)
  11510. (nth 2 tl))
  11511. (setq org-time-was-given t))
  11512. (if (< year 100) (setq year (+ 2000 year)))
  11513. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11514. (list second minute hour day month year)))
  11515. (defvar parse-time-weekdays)
  11516. (defun org-read-date-get-relative (s today default)
  11517. "Check string S for special relative date string.
  11518. TODAY and DEFAULT are internal times, for today and for a default.
  11519. Return shift list (N what def-flag)
  11520. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11521. N is the number of WHATs to shift.
  11522. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11523. the DEFAULT date rather than TODAY."
  11524. (when (and
  11525. (string-match
  11526. (concat
  11527. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11528. "\\([0-9]+\\)?"
  11529. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11530. "\\([ \t]\\|$\\)") s)
  11531. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11532. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11533. (string-to-char (substring (match-string 1 s) -1))
  11534. ?+))
  11535. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11536. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11537. (what (if (match-end 3) (match-string 3 s) "d"))
  11538. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11539. (date (if rel default today))
  11540. (wday (nth 6 (decode-time date)))
  11541. delta)
  11542. (if wday1
  11543. (progn
  11544. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11545. (if (= dir ?-) (setq delta (- delta 7)))
  11546. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11547. (list delta "d" rel))
  11548. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11549. (defun org-eval-in-calendar (form &optional keepdate)
  11550. "Eval FORM in the calendar window and return to current window.
  11551. Also, store the cursor date in variable org-ans2."
  11552. (let ((sf (selected-frame))
  11553. (sw (selected-window)))
  11554. (select-window (get-buffer-window "*Calendar*" t))
  11555. (eval form)
  11556. (when (and (not keepdate) (calendar-cursor-to-date))
  11557. (let* ((date (calendar-cursor-to-date))
  11558. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11559. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11560. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11561. (select-window sw)
  11562. (select-frame-set-input-focus sf)))
  11563. (defun org-calendar-select ()
  11564. "Return to `org-read-date' with the date currently selected.
  11565. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11566. (interactive)
  11567. (when (calendar-cursor-to-date)
  11568. (let* ((date (calendar-cursor-to-date))
  11569. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11570. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11571. (if (active-minibuffer-window) (exit-minibuffer))))
  11572. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11573. "Insert a date stamp for the date given by the internal TIME.
  11574. WITH-HM means, use the stamp format that includes the time of the day.
  11575. INACTIVE means use square brackets instead of angular ones, so that the
  11576. stamp will not contribute to the agenda.
  11577. PRE and POST are optional strings to be inserted before and after the
  11578. stamp.
  11579. The command returns the inserted time stamp."
  11580. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11581. stamp)
  11582. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11583. (insert-before-markers (or pre ""))
  11584. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11585. (when (listp extra)
  11586. (setq extra (car extra))
  11587. (if (and (stringp extra)
  11588. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11589. (setq extra (format "-%02d:%02d"
  11590. (string-to-number (match-string 1 extra))
  11591. (string-to-number (match-string 2 extra))))
  11592. (setq extra nil)))
  11593. (when extra
  11594. (backward-char 1)
  11595. (insert-before-markers extra)
  11596. (forward-char 1))
  11597. (insert-before-markers (or post ""))
  11598. (setq org-last-inserted-timestamp stamp)))
  11599. (defun org-toggle-time-stamp-overlays ()
  11600. "Toggle the use of custom time stamp formats."
  11601. (interactive)
  11602. (setq org-display-custom-times (not org-display-custom-times))
  11603. (unless org-display-custom-times
  11604. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11605. (while (setq p (next-single-property-change p 'display))
  11606. (if (and (get-text-property p 'display)
  11607. (eq (get-text-property p 'face) 'org-date))
  11608. (remove-text-properties
  11609. p (setq p (next-single-property-change p 'display))
  11610. '(display t))))
  11611. (set-buffer-modified-p bmp)))
  11612. (if (featurep 'xemacs)
  11613. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11614. (org-restart-font-lock)
  11615. (setq org-table-may-need-update t)
  11616. (if org-display-custom-times
  11617. (message "Time stamps are overlayed with custom format")
  11618. (message "Time stamp overlays removed")))
  11619. (defun org-display-custom-time (beg end)
  11620. "Overlay modified time stamp format over timestamp between BEG and END."
  11621. (let* ((ts (buffer-substring beg end))
  11622. t1 w1 with-hm tf time str w2 (off 0))
  11623. (save-match-data
  11624. (setq t1 (org-parse-time-string ts t))
  11625. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11626. (setq off (- (match-end 0) (match-beginning 0)))))
  11627. (setq end (- end off))
  11628. (setq w1 (- end beg)
  11629. with-hm (and (nth 1 t1) (nth 2 t1))
  11630. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11631. time (org-fix-decoded-time t1)
  11632. str (org-add-props
  11633. (format-time-string
  11634. (substring tf 1 -1) (apply 'encode-time time))
  11635. nil 'mouse-face 'highlight)
  11636. w2 (length str))
  11637. (if (not (= w2 w1))
  11638. (add-text-properties (1+ beg) (+ 2 beg)
  11639. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11640. (if (featurep 'xemacs)
  11641. (progn
  11642. (put-text-property beg end 'invisible t)
  11643. (put-text-property beg end 'end-glyph (make-glyph str)))
  11644. (put-text-property beg end 'display str))))
  11645. (defun org-translate-time (string)
  11646. "Translate all timestamps in STRING to custom format.
  11647. But do this only if the variable `org-display-custom-times' is set."
  11648. (when org-display-custom-times
  11649. (save-match-data
  11650. (let* ((start 0)
  11651. (re org-ts-regexp-both)
  11652. t1 with-hm inactive tf time str beg end)
  11653. (while (setq start (string-match re string start))
  11654. (setq beg (match-beginning 0)
  11655. end (match-end 0)
  11656. t1 (save-match-data
  11657. (org-parse-time-string (substring string beg end) t))
  11658. with-hm (and (nth 1 t1) (nth 2 t1))
  11659. inactive (equal (substring string beg (1+ beg)) "[")
  11660. tf (funcall (if with-hm 'cdr 'car)
  11661. org-time-stamp-custom-formats)
  11662. time (org-fix-decoded-time t1)
  11663. str (format-time-string
  11664. (concat
  11665. (if inactive "[" "<") (substring tf 1 -1)
  11666. (if inactive "]" ">"))
  11667. (apply 'encode-time time))
  11668. string (replace-match str t t string)
  11669. start (+ start (length str)))))))
  11670. string)
  11671. (defun org-fix-decoded-time (time)
  11672. "Set 0 instead of nil for the first 6 elements of time.
  11673. Don't touch the rest."
  11674. (let ((n 0))
  11675. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11676. (defun org-days-to-time (timestamp-string)
  11677. "Difference between TIMESTAMP-STRING and now in days."
  11678. (- (time-to-days (org-time-string-to-time timestamp-string))
  11679. (time-to-days (current-time))))
  11680. (defun org-deadline-close (timestamp-string &optional ndays)
  11681. "Is the time in TIMESTAMP-STRING close to the current date?"
  11682. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11683. (and (< (org-days-to-time timestamp-string) ndays)
  11684. (not (org-entry-is-done-p))))
  11685. (defun org-get-wdays (ts)
  11686. "Get the deadline lead time appropriate for timestring TS."
  11687. (cond
  11688. ((<= org-deadline-warning-days 0)
  11689. ;; 0 or negative, enforce this value no matter what
  11690. (- org-deadline-warning-days))
  11691. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11692. ;; lead time is specified.
  11693. (floor (* (string-to-number (match-string 1 ts))
  11694. (cdr (assoc (match-string 2 ts)
  11695. '(("d" . 1) ("w" . 7)
  11696. ("m" . 30.4) ("y" . 365.25)))))))
  11697. ;; go for the default.
  11698. (t org-deadline-warning-days)))
  11699. (defun org-calendar-select-mouse (ev)
  11700. "Return to `org-read-date' with the date currently selected.
  11701. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11702. (interactive "e")
  11703. (mouse-set-point ev)
  11704. (when (calendar-cursor-to-date)
  11705. (let* ((date (calendar-cursor-to-date))
  11706. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11707. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11708. (if (active-minibuffer-window) (exit-minibuffer))))
  11709. (defun org-check-deadlines (ndays)
  11710. "Check if there are any deadlines due or past due.
  11711. A deadline is considered due if it happens within `org-deadline-warning-days'
  11712. days from today's date. If the deadline appears in an entry marked DONE,
  11713. it is not shown. The prefix arg NDAYS can be used to test that many
  11714. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11715. (interactive "P")
  11716. (let* ((org-warn-days
  11717. (cond
  11718. ((equal ndays '(4)) 100000)
  11719. (ndays (prefix-numeric-value ndays))
  11720. (t (abs org-deadline-warning-days))))
  11721. (case-fold-search nil)
  11722. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11723. (callback
  11724. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11725. (message "%d deadlines past-due or due within %d days"
  11726. (org-occur regexp nil callback)
  11727. org-warn-days)))
  11728. (defun org-check-before-date (date)
  11729. "Check if there are deadlines or scheduled entries before DATE."
  11730. (interactive (list (org-read-date)))
  11731. (let ((case-fold-search nil)
  11732. (regexp (concat "\\<\\(" org-deadline-string
  11733. "\\|" org-scheduled-string
  11734. "\\) *<\\([^>]+\\)>"))
  11735. (callback
  11736. (lambda () (time-less-p
  11737. (org-time-string-to-time (match-string 2))
  11738. (org-time-string-to-time date)))))
  11739. (message "%d entries before %s"
  11740. (org-occur regexp nil callback) date)))
  11741. (defun org-check-after-date (date)
  11742. "Check if there are deadlines or scheduled entries after DATE."
  11743. (interactive (list (org-read-date)))
  11744. (let ((case-fold-search nil)
  11745. (regexp (concat "\\<\\(" org-deadline-string
  11746. "\\|" org-scheduled-string
  11747. "\\) *<\\([^>]+\\)>"))
  11748. (callback
  11749. (lambda () (not
  11750. (time-less-p
  11751. (org-time-string-to-time (match-string 2))
  11752. (org-time-string-to-time date))))))
  11753. (message "%d entries after %s"
  11754. (org-occur regexp nil callback) date)))
  11755. (defun org-evaluate-time-range (&optional to-buffer)
  11756. "Evaluate a time range by computing the difference between start and end.
  11757. Normally the result is just printed in the echo area, but with prefix arg
  11758. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11759. If the time range is actually in a table, the result is inserted into the
  11760. next column.
  11761. For time difference computation, a year is assumed to be exactly 365
  11762. days in order to avoid rounding problems."
  11763. (interactive "P")
  11764. (or
  11765. (org-clock-update-time-maybe)
  11766. (save-excursion
  11767. (unless (org-at-date-range-p t)
  11768. (goto-char (point-at-bol))
  11769. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11770. (if (not (org-at-date-range-p t))
  11771. (error "Not at a time-stamp range, and none found in current line")))
  11772. (let* ((ts1 (match-string 1))
  11773. (ts2 (match-string 2))
  11774. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11775. (match-end (match-end 0))
  11776. (time1 (org-time-string-to-time ts1))
  11777. (time2 (org-time-string-to-time ts2))
  11778. (t1 (time-to-seconds time1))
  11779. (t2 (time-to-seconds time2))
  11780. (diff (abs (- t2 t1)))
  11781. (negative (< (- t2 t1) 0))
  11782. ;; (ys (floor (* 365 24 60 60)))
  11783. (ds (* 24 60 60))
  11784. (hs (* 60 60))
  11785. (fy "%dy %dd %02d:%02d")
  11786. (fy1 "%dy %dd")
  11787. (fd "%dd %02d:%02d")
  11788. (fd1 "%dd")
  11789. (fh "%02d:%02d")
  11790. y d h m align)
  11791. (if havetime
  11792. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11793. y 0
  11794. d (floor (/ diff ds)) diff (mod diff ds)
  11795. h (floor (/ diff hs)) diff (mod diff hs)
  11796. m (floor (/ diff 60)))
  11797. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11798. y 0
  11799. d (floor (+ (/ diff ds) 0.5))
  11800. h 0 m 0))
  11801. (if (not to-buffer)
  11802. (message "%s" (org-make-tdiff-string y d h m))
  11803. (if (org-at-table-p)
  11804. (progn
  11805. (goto-char match-end)
  11806. (setq align t)
  11807. (and (looking-at " *|") (goto-char (match-end 0))))
  11808. (goto-char match-end))
  11809. (if (looking-at
  11810. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11811. (replace-match ""))
  11812. (if negative (insert " -"))
  11813. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11814. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11815. (insert " " (format fh h m))))
  11816. (if align (org-table-align))
  11817. (message "Time difference inserted")))))
  11818. (defun org-make-tdiff-string (y d h m)
  11819. (let ((fmt "")
  11820. (l nil))
  11821. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11822. l (push y l)))
  11823. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11824. l (push d l)))
  11825. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11826. l (push h l)))
  11827. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11828. l (push m l)))
  11829. (apply 'format fmt (nreverse l))))
  11830. (defun org-time-string-to-time (s)
  11831. (apply 'encode-time (org-parse-time-string s)))
  11832. (defun org-time-string-to-seconds (s)
  11833. (time-to-seconds (org-time-string-to-time s)))
  11834. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11835. "Convert a time stamp to an absolute day number.
  11836. If there is a specifyer for a cyclic time stamp, get the closest date to
  11837. DAYNR.
  11838. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  11839. the variable date is bound by the calendar when this is called."
  11840. (cond
  11841. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11842. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11843. daynr
  11844. (+ daynr 1000)))
  11845. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11846. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11847. (time-to-days (current-time))) (match-string 0 s)
  11848. prefer show-all))
  11849. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11850. (defun org-days-to-iso-week (days)
  11851. "Return the iso week number."
  11852. (require 'cal-iso)
  11853. (car (calendar-iso-from-absolute days)))
  11854. (defun org-small-year-to-year (year)
  11855. "Convert 2-digit years into 4-digit years.
  11856. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11857. The year 2000 cannot be abbreviated. Any year larger than 99
  11858. is returned unchanged."
  11859. (if (< year 38)
  11860. (setq year (+ 2000 year))
  11861. (if (< year 100)
  11862. (setq year (+ 1900 year))))
  11863. year)
  11864. (defun org-time-from-absolute (d)
  11865. "Return the time corresponding to date D.
  11866. D may be an absolute day number, or a calendar-type list (month day year)."
  11867. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11868. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11869. (defun org-calendar-holiday ()
  11870. "List of holidays, for Diary display in Org-mode."
  11871. (require 'holidays)
  11872. (let ((hl (funcall
  11873. (if (fboundp 'calendar-check-holidays)
  11874. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11875. (if hl (mapconcat 'identity hl "; "))))
  11876. (defun org-diary-sexp-entry (sexp entry date)
  11877. "Process a SEXP diary ENTRY for DATE."
  11878. (require 'diary-lib)
  11879. (let ((result (if calendar-debug-sexp
  11880. (let ((stack-trace-on-error t))
  11881. (eval (car (read-from-string sexp))))
  11882. (condition-case nil
  11883. (eval (car (read-from-string sexp)))
  11884. (error
  11885. (beep)
  11886. (message "Bad sexp at line %d in %s: %s"
  11887. (org-current-line)
  11888. (buffer-file-name) sexp)
  11889. (sleep-for 2))))))
  11890. (cond ((stringp result) result)
  11891. ((and (consp result)
  11892. (stringp (cdr result))) (cdr result))
  11893. (result entry)
  11894. (t nil))))
  11895. (defun org-diary-to-ical-string (frombuf)
  11896. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11897. This uses the icalendar.el library."
  11898. (let* ((tmpdir (if (featurep 'xemacs)
  11899. (temp-directory)
  11900. temporary-file-directory))
  11901. (tmpfile (make-temp-name
  11902. (expand-file-name "orgics" tmpdir)))
  11903. buf rtn b e)
  11904. (save-excursion
  11905. (set-buffer frombuf)
  11906. (icalendar-export-region (point-min) (point-max) tmpfile)
  11907. (setq buf (find-buffer-visiting tmpfile))
  11908. (set-buffer buf)
  11909. (goto-char (point-min))
  11910. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11911. (setq b (match-beginning 0)))
  11912. (goto-char (point-max))
  11913. (if (re-search-backward "^END:VEVENT" nil t)
  11914. (setq e (match-end 0)))
  11915. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11916. (kill-buffer buf)
  11917. (delete-file tmpfile)
  11918. rtn))
  11919. (defun org-closest-date (start current change prefer show-all)
  11920. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11921. When PREFER is `past' return a date that is either CURRENT or past.
  11922. When PREFER is `future', return a date that is either CURRENT or future.
  11923. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11924. ;; Make the proper lists from the dates
  11925. (catch 'exit
  11926. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11927. dn dw sday cday n1 n2 n0
  11928. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11929. (setq start (org-date-to-gregorian start)
  11930. current (org-date-to-gregorian
  11931. (if show-all
  11932. current
  11933. (time-to-days (current-time))))
  11934. sday (calendar-absolute-from-gregorian start)
  11935. cday (calendar-absolute-from-gregorian current))
  11936. (if (<= cday sday) (throw 'exit sday))
  11937. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11938. (setq dn (string-to-number (match-string 1 change))
  11939. dw (cdr (assoc (match-string 2 change) a1)))
  11940. (error "Invalid change specifyer: %s" change))
  11941. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11942. (cond
  11943. ((eq dw 'day)
  11944. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11945. n2 (+ n1 dn)))
  11946. ((eq dw 'year)
  11947. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11948. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11949. (setq date1 (list m d y1)
  11950. n1 (calendar-absolute-from-gregorian date1)
  11951. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11952. n2 (calendar-absolute-from-gregorian date2)))
  11953. ((eq dw 'month)
  11954. ;; approx number of month between the two dates
  11955. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11956. ;; How often does dn fit in there?
  11957. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11958. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11959. m (+ m nm)
  11960. ny (floor (/ m 12))
  11961. y (+ y ny)
  11962. m (- m (* ny 12)))
  11963. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11964. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11965. (setq m2 (+ m dn) y2 y)
  11966. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11967. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11968. (while (<= n2 cday)
  11969. (setq n1 n2 m m2 y y2)
  11970. (setq m2 (+ m dn) y2 y)
  11971. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11972. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11973. ;; Make sure n1 is the earlier date
  11974. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11975. (if show-all
  11976. (cond
  11977. ((eq prefer 'past) n1)
  11978. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11979. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11980. (cond
  11981. ((eq prefer 'past) n1)
  11982. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11983. (t (if (= cday n1) n1 n2)))))))
  11984. (defun org-date-to-gregorian (date)
  11985. "Turn any specification of DATE into a gregorian date for the calendar."
  11986. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11987. ((and (listp date) (= (length date) 3)) date)
  11988. ((stringp date)
  11989. (setq date (org-parse-time-string date))
  11990. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11991. ((listp date)
  11992. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11993. (defun org-parse-time-string (s &optional nodefault)
  11994. "Parse the standard Org-mode time string.
  11995. This should be a lot faster than the normal `parse-time-string'.
  11996. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11997. hour and minute fields will be nil if not given."
  11998. (if (string-match org-ts-regexp0 s)
  11999. (list 0
  12000. (if (or (match-beginning 8) (not nodefault))
  12001. (string-to-number (or (match-string 8 s) "0")))
  12002. (if (or (match-beginning 7) (not nodefault))
  12003. (string-to-number (or (match-string 7 s) "0")))
  12004. (string-to-number (match-string 4 s))
  12005. (string-to-number (match-string 3 s))
  12006. (string-to-number (match-string 2 s))
  12007. nil nil nil)
  12008. (make-list 9 0)))
  12009. (defun org-timestamp-up (&optional arg)
  12010. "Increase the date item at the cursor by one.
  12011. If the cursor is on the year, change the year. If it is on the month or
  12012. the day, change that.
  12013. With prefix ARG, change by that many units."
  12014. (interactive "p")
  12015. (org-timestamp-change (prefix-numeric-value arg)))
  12016. (defun org-timestamp-down (&optional arg)
  12017. "Decrease the date item at the cursor by one.
  12018. If the cursor is on the year, change the year. If it is on the month or
  12019. the day, change that.
  12020. With prefix ARG, change by that many units."
  12021. (interactive "p")
  12022. (org-timestamp-change (- (prefix-numeric-value arg))))
  12023. (defun org-timestamp-up-day (&optional arg)
  12024. "Increase the date in the time stamp by one day.
  12025. With prefix ARG, change that many days."
  12026. (interactive "p")
  12027. (if (and (not (org-at-timestamp-p t))
  12028. (org-on-heading-p))
  12029. (org-todo 'up)
  12030. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12031. (defun org-timestamp-down-day (&optional arg)
  12032. "Decrease the date in the time stamp by one day.
  12033. With prefix ARG, change that many days."
  12034. (interactive "p")
  12035. (if (and (not (org-at-timestamp-p t))
  12036. (org-on-heading-p))
  12037. (org-todo 'down)
  12038. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12039. (defun org-at-timestamp-p (&optional inactive-ok)
  12040. "Determine if the cursor is in or at a timestamp."
  12041. (interactive)
  12042. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12043. (pos (point))
  12044. (ans (or (looking-at tsr)
  12045. (save-excursion
  12046. (skip-chars-backward "^[<\n\r\t")
  12047. (if (> (point) (point-min)) (backward-char 1))
  12048. (and (looking-at tsr)
  12049. (> (- (match-end 0) pos) -1))))))
  12050. (and ans
  12051. (boundp 'org-ts-what)
  12052. (setq org-ts-what
  12053. (cond
  12054. ((= pos (match-beginning 0)) 'bracket)
  12055. ((= pos (1- (match-end 0))) 'bracket)
  12056. ((org-pos-in-match-range pos 2) 'year)
  12057. ((org-pos-in-match-range pos 3) 'month)
  12058. ((org-pos-in-match-range pos 7) 'hour)
  12059. ((org-pos-in-match-range pos 8) 'minute)
  12060. ((or (org-pos-in-match-range pos 4)
  12061. (org-pos-in-match-range pos 5)) 'day)
  12062. ((and (> pos (or (match-end 8) (match-end 5)))
  12063. (< pos (match-end 0)))
  12064. (- pos (or (match-end 8) (match-end 5))))
  12065. (t 'day))))
  12066. ans))
  12067. (defun org-toggle-timestamp-type ()
  12068. "Toggle the type (<active> or [inactive]) of a time stamp."
  12069. (interactive)
  12070. (when (org-at-timestamp-p t)
  12071. (let ((beg (match-beginning 0)) (end (match-end 0))
  12072. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12073. (save-excursion
  12074. (goto-char beg)
  12075. (while (re-search-forward "[][<>]" end t)
  12076. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12077. t t)))
  12078. (message "Timestamp is now %sactive"
  12079. (if (equal (char-after beg) ?<) "" "in")))))
  12080. (defun org-timestamp-change (n &optional what)
  12081. "Change the date in the time stamp at point.
  12082. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12083. `year', `minute', `second'. If WHAT is not given, the cursor position
  12084. in the timestamp determines what will be changed."
  12085. (let ((pos (point))
  12086. with-hm inactive
  12087. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12088. org-ts-what
  12089. extra rem
  12090. ts time time0)
  12091. (if (not (org-at-timestamp-p t))
  12092. (error "Not at a timestamp"))
  12093. (if (and (not what) (eq org-ts-what 'bracket))
  12094. (org-toggle-timestamp-type)
  12095. (if (and (not what) (not (eq org-ts-what 'day))
  12096. org-display-custom-times
  12097. (get-text-property (point) 'display)
  12098. (not (get-text-property (1- (point)) 'display)))
  12099. (setq org-ts-what 'day))
  12100. (setq org-ts-what (or what org-ts-what)
  12101. inactive (= (char-after (match-beginning 0)) ?\[)
  12102. ts (match-string 0))
  12103. (replace-match "")
  12104. (if (string-match
  12105. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12106. ts)
  12107. (setq extra (match-string 1 ts)))
  12108. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12109. (setq with-hm t))
  12110. (setq time0 (org-parse-time-string ts))
  12111. (when (and (eq org-ts-what 'minute)
  12112. (eq current-prefix-arg nil))
  12113. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12114. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12115. (setcar (cdr time0) (+ (nth 1 time0)
  12116. (if (> n 0) (- rem) (- dm rem))))))
  12117. (setq time
  12118. (encode-time (or (car time0) 0)
  12119. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12120. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12121. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12122. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12123. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12124. (nthcdr 6 time0)))
  12125. (when (and (member org-ts-what '(hour minute))
  12126. extra
  12127. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12128. (setq extra (org-modify-ts-extra
  12129. extra
  12130. (if (eq org-ts-what 'hour) 2 5)
  12131. n dm)))
  12132. (when (integerp org-ts-what)
  12133. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12134. (if (eq what 'calendar)
  12135. (let ((cal-date (org-get-date-from-calendar)))
  12136. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12137. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12138. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12139. (setcar time0 (or (car time0) 0))
  12140. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12141. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12142. (setq time (apply 'encode-time time0))))
  12143. (setq org-last-changed-timestamp
  12144. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12145. (org-clock-update-time-maybe)
  12146. (goto-char pos)
  12147. ;; Try to recenter the calendar window, if any
  12148. (if (and org-calendar-follow-timestamp-change
  12149. (get-buffer-window "*Calendar*" t)
  12150. (memq org-ts-what '(day month year)))
  12151. (org-recenter-calendar (time-to-days time))))))
  12152. (defun org-modify-ts-extra (s pos n dm)
  12153. "Change the different parts of the lead-time and repeat fields in timestamp."
  12154. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12155. ng h m new rem)
  12156. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12157. (cond
  12158. ((or (org-pos-in-match-range pos 2)
  12159. (org-pos-in-match-range pos 3))
  12160. (setq m (string-to-number (match-string 3 s))
  12161. h (string-to-number (match-string 2 s)))
  12162. (if (org-pos-in-match-range pos 2)
  12163. (setq h (+ h n))
  12164. (setq n (* dm (org-no-warnings (signum n))))
  12165. (when (not (= 0 (setq rem (% m dm))))
  12166. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12167. (setq m (+ m n)))
  12168. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12169. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12170. (setq h (min 24 (max 0 h)))
  12171. (setq ng 1 new (format "-%02d:%02d" h m)))
  12172. ((org-pos-in-match-range pos 6)
  12173. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12174. ((org-pos-in-match-range pos 5)
  12175. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12176. ((org-pos-in-match-range pos 9)
  12177. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12178. ((org-pos-in-match-range pos 8)
  12179. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12180. (when ng
  12181. (setq s (concat
  12182. (substring s 0 (match-beginning ng))
  12183. new
  12184. (substring s (match-end ng))))))
  12185. s))
  12186. (defun org-recenter-calendar (date)
  12187. "If the calendar is visible, recenter it to DATE."
  12188. (let* ((win (selected-window))
  12189. (cwin (get-buffer-window "*Calendar*" t))
  12190. (calendar-move-hook nil))
  12191. (when cwin
  12192. (select-window cwin)
  12193. (calendar-goto-date (if (listp date) date
  12194. (calendar-gregorian-from-absolute date)))
  12195. (select-window win))))
  12196. (defun org-goto-calendar (&optional arg)
  12197. "Go to the Emacs calendar at the current date.
  12198. If there is a time stamp in the current line, go to that date.
  12199. A prefix ARG can be used to force the current date."
  12200. (interactive "P")
  12201. (let ((tsr org-ts-regexp) diff
  12202. (calendar-move-hook nil)
  12203. (calendar-view-holidays-initially-flag nil)
  12204. (view-calendar-holidays-initially nil)
  12205. (calendar-view-diary-initially-flag nil)
  12206. (view-diary-entries-initially nil))
  12207. (if (or (org-at-timestamp-p)
  12208. (save-excursion
  12209. (beginning-of-line 1)
  12210. (looking-at (concat ".*" tsr))))
  12211. (let ((d1 (time-to-days (current-time)))
  12212. (d2 (time-to-days
  12213. (org-time-string-to-time (match-string 1)))))
  12214. (setq diff (- d2 d1))))
  12215. (calendar)
  12216. (calendar-goto-today)
  12217. (if (and diff (not arg)) (calendar-forward-day diff))))
  12218. (defun org-get-date-from-calendar ()
  12219. "Return a list (month day year) of date at point in calendar."
  12220. (with-current-buffer "*Calendar*"
  12221. (save-match-data
  12222. (calendar-cursor-to-date))))
  12223. (defun org-date-from-calendar ()
  12224. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12225. If there is already a time stamp at the cursor position, update it."
  12226. (interactive)
  12227. (if (org-at-timestamp-p t)
  12228. (org-timestamp-change 0 'calendar)
  12229. (let ((cal-date (org-get-date-from-calendar)))
  12230. (org-insert-time-stamp
  12231. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12232. (defun org-minutes-to-hh:mm-string (m)
  12233. "Compute H:MM from a number of minutes."
  12234. (let ((h (/ m 60)))
  12235. (setq m (- m (* 60 h)))
  12236. (format org-time-clocksum-format h m)))
  12237. (defun org-hh:mm-string-to-minutes (s)
  12238. "Convert a string H:MM to a number of minutes.
  12239. If the string is just a number, interprete it as minutes.
  12240. In fact, the first hh:mm or number in the string will be taken,
  12241. there can be extra stuff in the string.
  12242. If no number is found, the return value is 0."
  12243. (cond
  12244. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12245. (+ (* (string-to-number (match-string 1 s)) 60)
  12246. (string-to-number (match-string 2 s))))
  12247. ((string-match "\\([0-9]+\\)" s)
  12248. (string-to-number (match-string 1 s)))
  12249. (t 0)))
  12250. ;;;; Files
  12251. (defun org-save-all-org-buffers ()
  12252. "Save all Org-mode buffers without user confirmation."
  12253. (interactive)
  12254. (message "Saving all Org-mode buffers...")
  12255. (save-some-buffers t 'org-mode-p)
  12256. (when (featurep 'org-id) (org-id-locations-save))
  12257. (message "Saving all Org-mode buffers... done"))
  12258. (defun org-revert-all-org-buffers ()
  12259. "Revert all Org-mode buffers.
  12260. Prompt for confirmation when there are unsaved changes.
  12261. Be sure you know what you are doing before letting this function
  12262. overwrite your changes.
  12263. This function is useful in a setup where one tracks org files
  12264. with a version control system, to revert on one machine after pulling
  12265. changes from another. I believe the procedure must be like this:
  12266. 1. M-x org-save-all-org-buffers
  12267. 2. Pull changes from the other machine, resolve conflicts
  12268. 3. M-x org-revert-all-org-buffers"
  12269. (interactive)
  12270. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12271. (error "Abort"))
  12272. (save-excursion
  12273. (save-window-excursion
  12274. (mapc
  12275. (lambda (b)
  12276. (when (and (with-current-buffer b (org-mode-p))
  12277. (with-current-buffer b buffer-file-name))
  12278. (switch-to-buffer b)
  12279. (revert-buffer t 'no-confirm)))
  12280. (buffer-list))
  12281. (when (and (featurep 'org-id) org-id-track-globally)
  12282. (org-id-locations-load)))))
  12283. ;;;; Agenda files
  12284. ;;;###autoload
  12285. (defun org-iswitchb (&optional arg)
  12286. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12287. With a prefix argument, restrict available to files.
  12288. With two prefix arguments, restrict available buffers to agenda files.
  12289. Due to some yet unresolved reason, the global function
  12290. `iswitchb-mode' needs to be active for this function to work."
  12291. (interactive "P")
  12292. (require 'iswitchb)
  12293. (let ((enabled iswitchb-mode) blist)
  12294. (or enabled (iswitchb-mode 1))
  12295. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12296. ((equal arg '(16)) (org-buffer-list 'agenda))
  12297. (t (org-buffer-list))))
  12298. (unwind-protect
  12299. (let ((iswitchb-make-buflist-hook
  12300. (lambda ()
  12301. (setq iswitchb-temp-buflist
  12302. (mapcar 'buffer-name blist)))))
  12303. (switch-to-buffer
  12304. (iswitchb-read-buffer
  12305. "Switch-to: " nil t)))
  12306. (or enabled (iswitchb-mode -1)))))
  12307. ;;;###autoload
  12308. (defun org-iswitchb (&optional arg)
  12309. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12310. With a prefix argument, restrict available to files.
  12311. With two prefix arguments, restrict available buffers to agenda files."
  12312. (interactive "P")
  12313. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12314. ((equal arg '(16)) (org-buffer-list 'agenda))
  12315. (t (org-buffer-list)))))
  12316. (switch-to-buffer
  12317. (org-icompleting-read "Org buffer: "
  12318. (mapcar 'list (mapcar 'buffer-name blist))
  12319. nil t))))
  12320. ;;;###autoload
  12321. (defalias 'org-ido-switchb 'org-iswitchb)
  12322. (defun org-buffer-list (&optional predicate exclude-tmp)
  12323. "Return a list of Org buffers.
  12324. PREDICATE can be `export', `files' or `agenda'.
  12325. export restrict the list to Export buffers.
  12326. files restrict the list to buffers visiting Org files.
  12327. agenda restrict the list to buffers visiting agenda files.
  12328. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12329. (let* ((bfn nil)
  12330. (agenda-files (and (eq predicate 'agenda)
  12331. (mapcar 'file-truename (org-agenda-files t))))
  12332. (filter
  12333. (cond
  12334. ((eq predicate 'files)
  12335. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12336. ((eq predicate 'export)
  12337. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12338. ((eq predicate 'agenda)
  12339. (lambda (b)
  12340. (with-current-buffer b
  12341. (and (eq major-mode 'org-mode)
  12342. (setq bfn (buffer-file-name b))
  12343. (member (file-truename bfn) agenda-files)))))
  12344. (t (lambda (b) (with-current-buffer b
  12345. (or (eq major-mode 'org-mode)
  12346. (string-match "\*Org .*Export"
  12347. (buffer-name b)))))))))
  12348. (delq nil
  12349. (mapcar
  12350. (lambda(b)
  12351. (if (and (funcall filter b)
  12352. (or (not exclude-tmp)
  12353. (not (string-match "tmp" (buffer-name b)))))
  12354. b
  12355. nil))
  12356. (buffer-list)))))
  12357. (defun org-agenda-files (&optional unrestricted archives)
  12358. "Get the list of agenda files.
  12359. Optional UNRESTRICTED means return the full list even if a restriction
  12360. is currently in place.
  12361. When ARCHIVES is t, include all archive files hat are really being
  12362. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12363. `org-agenda-archives-mode' is t."
  12364. (let ((files
  12365. (cond
  12366. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12367. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12368. ((listp org-agenda-files) org-agenda-files)
  12369. (t (error "Invalid value of `org-agenda-files'")))))
  12370. (setq files (apply 'append
  12371. (mapcar (lambda (f)
  12372. (if (file-directory-p f)
  12373. (directory-files
  12374. f t org-agenda-file-regexp)
  12375. (list f)))
  12376. files)))
  12377. (when org-agenda-skip-unavailable-files
  12378. (setq files (delq nil
  12379. (mapcar (function
  12380. (lambda (file)
  12381. (and (file-readable-p file) file)))
  12382. files))))
  12383. (when (or (eq archives t)
  12384. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12385. (setq files (org-add-archive-files files)))
  12386. files))
  12387. (defun org-edit-agenda-file-list ()
  12388. "Edit the list of agenda files.
  12389. Depending on setup, this either uses customize to edit the variable
  12390. `org-agenda-files', or it visits the file that is holding the list. In the
  12391. latter case, the buffer is set up in a way that saving it automatically kills
  12392. the buffer and restores the previous window configuration."
  12393. (interactive)
  12394. (if (stringp org-agenda-files)
  12395. (let ((cw (current-window-configuration)))
  12396. (find-file org-agenda-files)
  12397. (org-set-local 'org-window-configuration cw)
  12398. (org-add-hook 'after-save-hook
  12399. (lambda ()
  12400. (set-window-configuration
  12401. (prog1 org-window-configuration
  12402. (kill-buffer (current-buffer))))
  12403. (org-install-agenda-files-menu)
  12404. (message "New agenda file list installed"))
  12405. nil 'local)
  12406. (message "%s" (substitute-command-keys
  12407. "Edit list and finish with \\[save-buffer]")))
  12408. (customize-variable 'org-agenda-files)))
  12409. (defun org-store-new-agenda-file-list (list)
  12410. "Set new value for the agenda file list and save it correctly."
  12411. (if (stringp org-agenda-files)
  12412. (let ((f org-agenda-files) b)
  12413. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12414. (with-temp-file f
  12415. (insert (mapconcat 'identity list "\n") "\n")))
  12416. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12417. (setq org-agenda-files list)
  12418. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12419. (defun org-read-agenda-file-list ()
  12420. "Read the list of agenda files from a file."
  12421. (when (file-directory-p org-agenda-files)
  12422. (error "`org-agenda-files' cannot be a single directory"))
  12423. (when (stringp org-agenda-files)
  12424. (with-temp-buffer
  12425. (insert-file-contents org-agenda-files)
  12426. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12427. ;;;###autoload
  12428. (defun org-cycle-agenda-files ()
  12429. "Cycle through the files in `org-agenda-files'.
  12430. If the current buffer visits an agenda file, find the next one in the list.
  12431. If the current buffer does not, find the first agenda file."
  12432. (interactive)
  12433. (let* ((fs (org-agenda-files t))
  12434. (files (append fs (list (car fs))))
  12435. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12436. file)
  12437. (unless files (error "No agenda files"))
  12438. (catch 'exit
  12439. (while (setq file (pop files))
  12440. (if (equal (file-truename file) tcf)
  12441. (when (car files)
  12442. (find-file (car files))
  12443. (throw 'exit t))))
  12444. (find-file (car fs)))
  12445. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12446. (defun org-agenda-file-to-front (&optional to-end)
  12447. "Move/add the current file to the top of the agenda file list.
  12448. If the file is not present in the list, it is added to the front. If it is
  12449. present, it is moved there. With optional argument TO-END, add/move to the
  12450. end of the list."
  12451. (interactive "P")
  12452. (let ((org-agenda-skip-unavailable-files nil)
  12453. (file-alist (mapcar (lambda (x)
  12454. (cons (file-truename x) x))
  12455. (org-agenda-files t)))
  12456. (ctf (file-truename buffer-file-name))
  12457. x had)
  12458. (setq x (assoc ctf file-alist) had x)
  12459. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12460. (if to-end
  12461. (setq file-alist (append (delq x file-alist) (list x)))
  12462. (setq file-alist (cons x (delq x file-alist))))
  12463. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12464. (org-install-agenda-files-menu)
  12465. (message "File %s to %s of agenda file list"
  12466. (if had "moved" "added") (if to-end "end" "front"))))
  12467. (defun org-remove-file (&optional file)
  12468. "Remove current file from the list of files in variable `org-agenda-files'.
  12469. These are the files which are being checked for agenda entries.
  12470. Optional argument FILE means, use this file instead of the current."
  12471. (interactive)
  12472. (let* ((org-agenda-skip-unavailable-files nil)
  12473. (file (or file buffer-file-name))
  12474. (true-file (file-truename file))
  12475. (afile (abbreviate-file-name file))
  12476. (files (delq nil (mapcar
  12477. (lambda (x)
  12478. (if (equal true-file
  12479. (file-truename x))
  12480. nil x))
  12481. (org-agenda-files t)))))
  12482. (if (not (= (length files) (length (org-agenda-files t))))
  12483. (progn
  12484. (org-store-new-agenda-file-list files)
  12485. (org-install-agenda-files-menu)
  12486. (message "Removed file: %s" afile))
  12487. (message "File was not in list: %s (not removed)" afile))))
  12488. (defun org-file-menu-entry (file)
  12489. (vector file (list 'find-file file) t))
  12490. (defun org-check-agenda-file (file)
  12491. "Make sure FILE exists. If not, ask user what to do."
  12492. (when (not (file-exists-p file))
  12493. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12494. (abbreviate-file-name file))
  12495. (let ((r (downcase (read-char-exclusive))))
  12496. (cond
  12497. ((equal r ?r)
  12498. (org-remove-file file)
  12499. (throw 'nextfile t))
  12500. (t (error "Abort"))))))
  12501. (defun org-get-agenda-file-buffer (file)
  12502. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12503. it to the list of buffers which might be released later."
  12504. (let ((buf (org-find-base-buffer-visiting file)))
  12505. (if buf
  12506. buf ; just return it
  12507. ;; Make a new buffer and remember it
  12508. (setq buf (find-file-noselect file))
  12509. (if buf (push buf org-agenda-new-buffers))
  12510. buf)))
  12511. (defun org-release-buffers (blist)
  12512. "Release all buffers in list, asking the user for confirmation when needed.
  12513. When a buffer is unmodified, it is just killed. When modified, it is saved
  12514. \(if the user agrees) and then killed."
  12515. (let (buf file)
  12516. (while (setq buf (pop blist))
  12517. (setq file (buffer-file-name buf))
  12518. (when (and (buffer-modified-p buf)
  12519. file
  12520. (y-or-n-p (format "Save file %s? " file)))
  12521. (with-current-buffer buf (save-buffer)))
  12522. (kill-buffer buf))))
  12523. (defun org-prepare-agenda-buffers (files)
  12524. "Create buffers for all agenda files, protect archived trees and comments."
  12525. (interactive)
  12526. (let ((pa '(:org-archived t))
  12527. (pc '(:org-comment t))
  12528. (pall '(:org-archived t :org-comment t))
  12529. (inhibit-read-only t)
  12530. (rea (concat ":" org-archive-tag ":"))
  12531. bmp file re)
  12532. (save-excursion
  12533. (save-restriction
  12534. (while (setq file (pop files))
  12535. (catch 'nextfile
  12536. (if (bufferp file)
  12537. (set-buffer file)
  12538. (org-check-agenda-file file)
  12539. (set-buffer (org-get-agenda-file-buffer file)))
  12540. (widen)
  12541. (setq bmp (buffer-modified-p))
  12542. (org-refresh-category-properties)
  12543. (setq org-todo-keywords-for-agenda
  12544. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12545. (setq org-done-keywords-for-agenda
  12546. (append org-done-keywords-for-agenda org-done-keywords))
  12547. (setq org-todo-keyword-alist-for-agenda
  12548. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12549. (setq org-tag-alist-for-agenda
  12550. (append org-tag-alist-for-agenda org-tag-alist))
  12551. (save-excursion
  12552. (remove-text-properties (point-min) (point-max) pall)
  12553. (when org-agenda-skip-archived-trees
  12554. (goto-char (point-min))
  12555. (while (re-search-forward rea nil t)
  12556. (if (org-on-heading-p t)
  12557. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12558. (goto-char (point-min))
  12559. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12560. (while (re-search-forward re nil t)
  12561. (add-text-properties
  12562. (match-beginning 0) (org-end-of-subtree t) pc)))
  12563. (set-buffer-modified-p bmp)))))
  12564. (setq org-todo-keyword-alist-for-agenda
  12565. (org-uniquify org-todo-keyword-alist-for-agenda)
  12566. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12567. ;;;; Embedded LaTeX
  12568. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12569. "Keymap for the minor `org-cdlatex-mode'.")
  12570. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12571. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12572. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12573. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12574. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12575. (defvar org-cdlatex-texmathp-advice-is-done nil
  12576. "Flag remembering if we have applied the advice to texmathp already.")
  12577. (define-minor-mode org-cdlatex-mode
  12578. "Toggle the minor `org-cdlatex-mode'.
  12579. This mode supports entering LaTeX environment and math in LaTeX fragments
  12580. in Org-mode.
  12581. \\{org-cdlatex-mode-map}"
  12582. nil " OCDL" nil
  12583. (when org-cdlatex-mode (require 'cdlatex))
  12584. (unless org-cdlatex-texmathp-advice-is-done
  12585. (setq org-cdlatex-texmathp-advice-is-done t)
  12586. (defadvice texmathp (around org-math-always-on activate)
  12587. "Always return t in org-mode buffers.
  12588. This is because we want to insert math symbols without dollars even outside
  12589. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12590. an embedded LaTeX fragment, let texmathp do its job.
  12591. \\[org-cdlatex-mode-map]"
  12592. (interactive)
  12593. (let (p)
  12594. (cond
  12595. ((not (org-mode-p)) ad-do-it)
  12596. ((eq this-command 'cdlatex-math-symbol)
  12597. (setq ad-return-value t
  12598. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12599. (t
  12600. (let ((p (org-inside-LaTeX-fragment-p)))
  12601. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12602. (setq ad-return-value t
  12603. texmathp-why '("Org-mode embedded math" . 0))
  12604. (if p ad-do-it)))))))))
  12605. (defun turn-on-org-cdlatex ()
  12606. "Unconditionally turn on `org-cdlatex-mode'."
  12607. (org-cdlatex-mode 1))
  12608. (defun org-inside-LaTeX-fragment-p ()
  12609. "Test if point is inside a LaTeX fragment.
  12610. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12611. sequence appearing also before point.
  12612. Even though the matchers for math are configurable, this function assumes
  12613. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12614. delimiters are skipped when they have been removed by customization.
  12615. The return value is nil, or a cons cell with the delimiter and
  12616. and the position of this delimiter.
  12617. This function does a reasonably good job, but can locally be fooled by
  12618. for example currency specifications. For example it will assume being in
  12619. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12620. fragments that are properly closed, but during editing, we have to live
  12621. with the uncertainty caused by missing closing delimiters. This function
  12622. looks only before point, not after."
  12623. (catch 'exit
  12624. (let ((pos (point))
  12625. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12626. (lim (progn
  12627. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12628. (point)))
  12629. dd-on str (start 0) m re)
  12630. (goto-char pos)
  12631. (when dodollar
  12632. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12633. re (nth 1 (assoc "$" org-latex-regexps)))
  12634. (while (string-match re str start)
  12635. (cond
  12636. ((= (match-end 0) (length str))
  12637. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12638. ((= (match-end 0) (- (length str) 5))
  12639. (throw 'exit nil))
  12640. (t (setq start (match-end 0))))))
  12641. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12642. (goto-char pos)
  12643. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12644. (and (match-beginning 2) (throw 'exit nil))
  12645. ;; count $$
  12646. (while (re-search-backward "\\$\\$" lim t)
  12647. (setq dd-on (not dd-on)))
  12648. (goto-char pos)
  12649. (if dd-on (cons "$$" m))))))
  12650. (defun org-try-cdlatex-tab ()
  12651. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12652. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12653. - inside a LaTeX fragment, or
  12654. - after the first word in a line, where an abbreviation expansion could
  12655. insert a LaTeX environment."
  12656. (when org-cdlatex-mode
  12657. (cond
  12658. ((save-excursion
  12659. (skip-chars-backward "a-zA-Z0-9*")
  12660. (skip-chars-backward " \t")
  12661. (bolp))
  12662. (cdlatex-tab) t)
  12663. ((org-inside-LaTeX-fragment-p)
  12664. (cdlatex-tab) t)
  12665. (t nil))))
  12666. (defun org-cdlatex-underscore-caret (&optional arg)
  12667. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12668. Revert to the normal definition outside of these fragments."
  12669. (interactive "P")
  12670. (if (org-inside-LaTeX-fragment-p)
  12671. (call-interactively 'cdlatex-sub-superscript)
  12672. (let (org-cdlatex-mode)
  12673. (call-interactively (key-binding (vector last-input-event))))))
  12674. (defun org-cdlatex-math-modify (&optional arg)
  12675. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12676. Revert to the normal definition outside of these fragments."
  12677. (interactive "P")
  12678. (if (org-inside-LaTeX-fragment-p)
  12679. (call-interactively 'cdlatex-math-modify)
  12680. (let (org-cdlatex-mode)
  12681. (call-interactively (key-binding (vector last-input-event))))))
  12682. (defvar org-latex-fragment-image-overlays nil
  12683. "List of overlays carrying the images of latex fragments.")
  12684. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12685. (defun org-remove-latex-fragment-image-overlays ()
  12686. "Remove all overlays with LaTeX fragment images in current buffer."
  12687. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12688. (setq org-latex-fragment-image-overlays nil))
  12689. (defun org-preview-latex-fragment (&optional subtree)
  12690. "Preview the LaTeX fragment at point, or all locally or globally.
  12691. If the cursor is in a LaTeX fragment, create the image and overlay
  12692. it over the source code. If there is no fragment at point, display
  12693. all fragments in the current text, from one headline to the next. With
  12694. prefix SUBTREE, display all fragments in the current subtree. With a
  12695. double prefix `C-u C-u', or when the cursor is before the first headline,
  12696. display all fragments in the buffer.
  12697. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12698. (interactive "P")
  12699. (org-remove-latex-fragment-image-overlays)
  12700. (save-excursion
  12701. (save-restriction
  12702. (let (beg end at msg)
  12703. (cond
  12704. ((or (equal subtree '(16))
  12705. (not (save-excursion
  12706. (re-search-backward (concat "^" outline-regexp) nil t))))
  12707. (setq beg (point-min) end (point-max)
  12708. msg "Creating images for buffer...%s"))
  12709. ((equal subtree '(4))
  12710. (org-back-to-heading)
  12711. (setq beg (point) end (org-end-of-subtree t)
  12712. msg "Creating images for subtree...%s"))
  12713. (t
  12714. (if (setq at (org-inside-LaTeX-fragment-p))
  12715. (goto-char (max (point-min) (- (cdr at) 2)))
  12716. (org-back-to-heading))
  12717. (setq beg (point) end (progn (outline-next-heading) (point))
  12718. msg (if at "Creating image...%s"
  12719. "Creating images for entry...%s"))))
  12720. (message msg "")
  12721. (narrow-to-region beg end)
  12722. (goto-char beg)
  12723. (org-format-latex
  12724. (concat "ltxpng/" (file-name-sans-extension
  12725. (file-name-nondirectory
  12726. buffer-file-name)))
  12727. default-directory 'overlays msg at 'forbuffer)
  12728. (message msg "done. Use `C-c C-c' to remove images.")))))
  12729. (defvar org-latex-regexps
  12730. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12731. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12732. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12733. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12734. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12735. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12736. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12737. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12738. "Regular expressions for matching embedded LaTeX.")
  12739. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12740. "Replace LaTeX fragments with links to an image, and produce images."
  12741. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12742. (let* ((prefixnodir (file-name-nondirectory prefix))
  12743. (absprefix (expand-file-name prefix dir))
  12744. (todir (file-name-directory absprefix))
  12745. (opt org-format-latex-options)
  12746. (matchers (plist-get opt :matchers))
  12747. (re-list org-latex-regexps)
  12748. (cnt 0) txt link beg end re e checkdir
  12749. executables-checked
  12750. m n block linkfile movefile ov)
  12751. ;; Check if there are old images files with this prefix, and remove them
  12752. (when (file-directory-p todir)
  12753. (mapc 'delete-file
  12754. (directory-files
  12755. todir 'full
  12756. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12757. ;; Check the different regular expressions
  12758. (while (setq e (pop re-list))
  12759. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12760. block (if (nth 3 e) "\n\n" ""))
  12761. (when (member m matchers)
  12762. (goto-char (point-min))
  12763. (while (re-search-forward re nil t)
  12764. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12765. (not (get-text-property (match-beginning n)
  12766. 'org-protected))
  12767. (or (not overlays)
  12768. (not (eq (get-char-property (match-beginning n)
  12769. 'org-overlay-type)
  12770. 'org-latex-overlay))))
  12771. (setq txt (match-string n)
  12772. beg (match-beginning n) end (match-end n)
  12773. cnt (1+ cnt)
  12774. linkfile (format "%s_%04d.png" prefix cnt)
  12775. movefile (format "%s_%04d.png" absprefix cnt)
  12776. link (concat block "[[file:" linkfile "]]" block))
  12777. (if msg (message msg cnt))
  12778. (goto-char beg)
  12779. (unless checkdir ; make sure the directory exists
  12780. (setq checkdir t)
  12781. (or (file-directory-p todir) (make-directory todir)))
  12782. (unless executables-checked
  12783. (org-check-external-command
  12784. "latex" "needed to convert LaTeX fragments to images")
  12785. (org-check-external-command
  12786. "dvipng" "needed to convert LaTeX fragments to images")
  12787. (setq executables-checked t))
  12788. (org-create-formula-image
  12789. txt movefile opt forbuffer)
  12790. (if overlays
  12791. (progn
  12792. (mapc (lambda (o)
  12793. (if (eq (org-overlay-get o 'org-overlay-type)
  12794. 'org-latex-overlay)
  12795. (org-delete-overlay o)))
  12796. (org-overlays-in beg end))
  12797. (setq ov (org-make-overlay beg end))
  12798. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  12799. (if (featurep 'xemacs)
  12800. (progn
  12801. (org-overlay-put ov 'invisible t)
  12802. (org-overlay-put
  12803. ov 'end-glyph
  12804. (make-glyph (vector 'png :file movefile))))
  12805. (org-overlay-put
  12806. ov 'display
  12807. (list 'image :type 'png :file movefile :ascent 'center)))
  12808. (push ov org-latex-fragment-image-overlays)
  12809. (goto-char end))
  12810. (delete-region beg end)
  12811. (insert link))))))))
  12812. ;; This function borrows from Ganesh Swami's latex2png.el
  12813. (defun org-create-formula-image (string tofile options buffer)
  12814. (require 'org-latex)
  12815. (let* ((tmpdir (if (featurep 'xemacs)
  12816. (temp-directory)
  12817. temporary-file-directory))
  12818. (texfilebase (make-temp-name
  12819. (expand-file-name "orgtex" tmpdir)))
  12820. (texfile (concat texfilebase ".tex"))
  12821. (dvifile (concat texfilebase ".dvi"))
  12822. (pngfile (concat texfilebase ".png"))
  12823. (fnh (if (featurep 'xemacs)
  12824. (font-height (get-face-font 'default))
  12825. (face-attribute 'default :height nil)))
  12826. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12827. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12828. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12829. "Black"))
  12830. (bg (or (plist-get options (if buffer :background :html-background))
  12831. "Transparent")))
  12832. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12833. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12834. (with-temp-file texfile
  12835. (insert org-format-latex-header
  12836. (if org-export-latex-packages-alist
  12837. (concat "\n"
  12838. (mapconcat (lambda(p)
  12839. (if (equal "" (car p))
  12840. (format "\\usepackage{%s}" (cadr p))
  12841. (format "\\usepackage[%s]{%s}"
  12842. (car p) (cadr p))))
  12843. org-export-latex-packages-alist "\n"))
  12844. "")
  12845. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12846. (debug)
  12847. (let ((dir default-directory))
  12848. (condition-case nil
  12849. (progn
  12850. (cd tmpdir)
  12851. (call-process "latex" nil nil nil texfile))
  12852. (error nil))
  12853. (cd dir))
  12854. (if (not (file-exists-p dvifile))
  12855. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12856. (condition-case nil
  12857. (call-process "dvipng" nil nil nil
  12858. "-fg" fg "-bg" bg
  12859. "-D" dpi
  12860. ;;"-x" scale "-y" scale
  12861. "-T" "tight"
  12862. "-o" pngfile
  12863. dvifile)
  12864. (error nil))
  12865. (if (not (file-exists-p pngfile))
  12866. (progn (message "Failed to create png file from %s" texfile) nil)
  12867. ;; Use the requested file name and clean up
  12868. (copy-file pngfile tofile 'replace)
  12869. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12870. (delete-file (concat texfilebase e)))
  12871. pngfile))))
  12872. (defun org-dvipng-color (attr)
  12873. "Return an rgb color specification for dvipng."
  12874. (apply 'format "rgb %s %s %s"
  12875. (mapcar 'org-normalize-color
  12876. (color-values (face-attribute 'default attr nil)))))
  12877. (defun org-normalize-color (value)
  12878. "Return string to be used as color value for an RGB component."
  12879. (format "%g" (/ value 65535.0)))
  12880. ;;;; Key bindings
  12881. ;; Make `C-c C-x' a prefix key
  12882. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12883. ;; TAB key with modifiers
  12884. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12885. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12886. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12887. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12888. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12889. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12890. ;; The following line is necessary under Suse GNU/Linux
  12891. (unless (featurep 'xemacs)
  12892. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12893. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12894. (define-key org-mode-map [backtab] 'org-shifttab)
  12895. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12896. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12897. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12898. ;; Cursor keys with modifiers
  12899. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12900. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12901. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12902. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12903. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12904. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12905. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12906. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12907. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12908. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12909. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12910. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12911. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12912. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12913. ;;; Extra keys for tty access.
  12914. ;; We only set them when really needed because otherwise the
  12915. ;; menus don't show the simple keys
  12916. (when (or org-use-extra-keys
  12917. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12918. (not window-system))
  12919. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12920. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12921. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12922. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12923. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12924. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12925. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12926. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12927. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12928. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12929. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12930. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12931. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12932. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12933. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12934. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12935. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12936. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12937. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12938. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12939. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12940. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12941. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12942. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12943. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12944. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12945. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12946. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12947. ;; All the other keys
  12948. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12949. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12950. (if (boundp 'narrow-map)
  12951. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12952. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12953. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  12954. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  12955. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12956. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12957. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12958. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12959. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12960. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12961. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12962. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12963. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12964. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12965. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12966. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12967. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12968. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12969. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12970. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12971. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12972. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12973. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12974. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12975. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12976. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12977. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12978. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12979. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12980. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12981. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12982. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12983. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12984. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12985. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12986. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12987. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12988. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12989. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12990. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12991. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12992. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12993. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12994. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12995. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12996. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12997. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12998. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12999. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13000. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  13001. (org-defkey org-mode-map "\C-m" 'org-return)
  13002. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13003. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13004. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13005. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13006. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13007. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13008. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13009. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13010. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13011. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13012. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13013. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13014. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13015. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13016. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13017. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13018. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13019. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13020. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13021. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13022. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13023. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13024. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13025. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13026. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13027. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13028. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13029. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13030. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13031. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13032. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13033. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13034. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13035. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13036. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13037. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13038. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13039. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13040. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13041. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13042. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13043. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13044. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13045. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13046. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13047. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13048. (when (featurep 'xemacs)
  13049. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13050. (defvar org-self-insert-command-undo-counter 0)
  13051. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13052. (defun org-self-insert-command (N)
  13053. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13054. If the cursor is in a table looking at whitespace, the whitespace is
  13055. overwritten, and the table is not marked as requiring realignment."
  13056. (interactive "p")
  13057. (if (and
  13058. (org-table-p)
  13059. (progn
  13060. ;; check if we blank the field, and if that triggers align
  13061. (and (featurep 'org-table) org-table-auto-blank-field
  13062. (member last-command
  13063. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13064. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13065. ;; got extra space, this field does not determine column width
  13066. (let (org-table-may-need-update) (org-table-blank-field))
  13067. ;; no extra space, this field may determine column width
  13068. (org-table-blank-field)))
  13069. t)
  13070. (eq N 1)
  13071. (looking-at "[^|\n]* |"))
  13072. (let (org-table-may-need-update)
  13073. (goto-char (1- (match-end 0)))
  13074. (delete-backward-char 1)
  13075. (goto-char (match-beginning 0))
  13076. (self-insert-command N))
  13077. (setq org-table-may-need-update t)
  13078. (self-insert-command N)
  13079. (org-fix-tags-on-the-fly)
  13080. (if org-self-insert-cluster-for-undo
  13081. (if (not (eq last-command 'org-self-insert-command))
  13082. (setq org-self-insert-command-undo-counter 1)
  13083. (if (>= org-self-insert-command-undo-counter 20)
  13084. (setq org-self-insert-command-undo-counter 1)
  13085. (and (> org-self-insert-command-undo-counter 0)
  13086. buffer-undo-list
  13087. (not (cadr buffer-undo-list)) ; remove nil entry
  13088. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13089. (setq org-self-insert-command-undo-counter
  13090. (1+ org-self-insert-command-undo-counter)))))))
  13091. (defun org-fix-tags-on-the-fly ()
  13092. (when (and (equal (char-after (point-at-bol)) ?*)
  13093. (org-on-heading-p))
  13094. (org-align-tags-here org-tags-column)))
  13095. (defun org-delete-backward-char (N)
  13096. "Like `delete-backward-char', insert whitespace at field end in tables.
  13097. When deleting backwards, in tables this function will insert whitespace in
  13098. front of the next \"|\" separator, to keep the table aligned. The table will
  13099. still be marked for re-alignment if the field did fill the entire column,
  13100. because, in this case the deletion might narrow the column."
  13101. (interactive "p")
  13102. (if (and (org-table-p)
  13103. (eq N 1)
  13104. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13105. (looking-at ".*?|"))
  13106. (let ((pos (point))
  13107. (noalign (looking-at "[^|\n\r]* |"))
  13108. (c org-table-may-need-update))
  13109. (backward-delete-char N)
  13110. (skip-chars-forward "^|")
  13111. (insert " ")
  13112. (goto-char (1- pos))
  13113. ;; noalign: if there were two spaces at the end, this field
  13114. ;; does not determine the width of the column.
  13115. (if noalign (setq org-table-may-need-update c)))
  13116. (backward-delete-char N)
  13117. (org-fix-tags-on-the-fly)))
  13118. (defun org-delete-char (N)
  13119. "Like `delete-char', but insert whitespace at field end in tables.
  13120. When deleting characters, in tables this function will insert whitespace in
  13121. front of the next \"|\" separator, to keep the table aligned. The table will
  13122. still be marked for re-alignment if the field did fill the entire column,
  13123. because, in this case the deletion might narrow the column."
  13124. (interactive "p")
  13125. (if (and (org-table-p)
  13126. (not (bolp))
  13127. (not (= (char-after) ?|))
  13128. (eq N 1))
  13129. (if (looking-at ".*?|")
  13130. (let ((pos (point))
  13131. (noalign (looking-at "[^|\n\r]* |"))
  13132. (c org-table-may-need-update))
  13133. (replace-match (concat
  13134. (substring (match-string 0) 1 -1)
  13135. " |"))
  13136. (goto-char pos)
  13137. ;; noalign: if there were two spaces at the end, this field
  13138. ;; does not determine the width of the column.
  13139. (if noalign (setq org-table-may-need-update c)))
  13140. (delete-char N))
  13141. (delete-char N)
  13142. (org-fix-tags-on-the-fly)))
  13143. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13144. (put 'org-self-insert-command 'delete-selection t)
  13145. (put 'orgtbl-self-insert-command 'delete-selection t)
  13146. (put 'org-delete-char 'delete-selection 'supersede)
  13147. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13148. (put 'org-yank 'delete-selection 'yank)
  13149. ;; Make `flyspell-mode' delay after some commands
  13150. (put 'org-self-insert-command 'flyspell-delayed t)
  13151. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13152. (put 'org-delete-char 'flyspell-delayed t)
  13153. (put 'org-delete-backward-char 'flyspell-delayed t)
  13154. ;; Make pabbrev-mode expand after org-mode commands
  13155. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13156. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13157. ;; How to do this: Measure non-white length of current string
  13158. ;; If equal to column width, we should realign.
  13159. (defun org-remap (map &rest commands)
  13160. "In MAP, remap the functions given in COMMANDS.
  13161. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13162. (let (new old)
  13163. (while commands
  13164. (setq old (pop commands) new (pop commands))
  13165. (if (fboundp 'command-remapping)
  13166. (org-defkey map (vector 'remap old) new)
  13167. (substitute-key-definition old new map global-map)))))
  13168. (when (eq org-enable-table-editor 'optimized)
  13169. ;; If the user wants maximum table support, we need to hijack
  13170. ;; some standard editing functions
  13171. (org-remap org-mode-map
  13172. 'self-insert-command 'org-self-insert-command
  13173. 'delete-char 'org-delete-char
  13174. 'delete-backward-char 'org-delete-backward-char)
  13175. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13176. (defvar org-ctrl-c-ctrl-c-hook nil
  13177. "Hook for functions attaching themselves to `C-c C-c'.
  13178. This can be used to add additional functionality to the C-c C-c key which
  13179. executes context-dependent commands.
  13180. Each function will be called with no arguments. The function must check
  13181. if the context is appropriate for it to act. If yes, it should do its
  13182. thing and then return a non-nil value. If the context is wrong,
  13183. just do nothing and return nil.")
  13184. (defvar org-tab-first-hook nil
  13185. "Hook for functions to attach themselves to TAB.
  13186. See `org-ctrl-c-ctrl-c-hook' for more information.
  13187. This hook runs as the first action when TAB is pressed, even before
  13188. `org-cycle' messes around with the `outline-regexp' to cater for
  13189. inline tasks and plain list item folding.
  13190. If any function in this hook returns t, not other actions like table
  13191. field motion visibility cycling will be done.")
  13192. (defvar org-tab-after-check-for-table-hook nil
  13193. "Hook for functions to attach themselves to TAB.
  13194. See `org-ctrl-c-ctrl-c-hook' for more information.
  13195. This hook runs after it has been established that the cursor is not in a
  13196. table, but before checking if the cursor is in a headline or if global cycling
  13197. should be done.
  13198. If any function in this hook returns t, not other actions like visibility
  13199. cycling will be done.")
  13200. (defvar org-tab-after-check-for-cycling-hook nil
  13201. "Hook for functions to attach themselves to TAB.
  13202. See `org-ctrl-c-ctrl-c-hook' for more information.
  13203. This hook runs after it has been established that not table field motion and
  13204. not visibility should be done because of current context. This is probably
  13205. the place where a package like yasnippets can hook in.")
  13206. (defvar org-metaleft-hook nil
  13207. "Hook for functions attaching themselves to `M-left'.
  13208. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13209. (defvar org-metaright-hook nil
  13210. "Hook for functions attaching themselves to `M-right'.
  13211. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13212. (defvar org-metaup-hook nil
  13213. "Hook for functions attaching themselves to `M-up'.
  13214. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13215. (defvar org-metadown-hook nil
  13216. "Hook for functions attaching themselves to `M-down'.
  13217. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13218. (defvar org-shiftmetaleft-hook nil
  13219. "Hook for functions attaching themselves to `M-S-left'.
  13220. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13221. (defvar org-shiftmetaright-hook nil
  13222. "Hook for functions attaching themselves to `M-S-right'.
  13223. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13224. (defvar org-shiftmetaup-hook nil
  13225. "Hook for functions attaching themselves to `M-S-up'.
  13226. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13227. (defvar org-shiftmetadown-hook nil
  13228. "Hook for functions attaching themselves to `M-S-down'.
  13229. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13230. (defvar org-metareturn-hook nil
  13231. "Hook for functions attaching themselves to `M-RET'.
  13232. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13233. (defun org-modifier-cursor-error ()
  13234. "Throw an error, a modified cursor command was applied in wrong context."
  13235. (error "This command is active in special context like tables, headlines or items"))
  13236. (defun org-shiftselect-error ()
  13237. "Throw an error because Shift-Cursor command was applied in wrong context."
  13238. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13239. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13240. (error "This command works only in special context like headlines or timestamps.")))
  13241. (defun org-call-for-shift-select (cmd)
  13242. (let ((this-command-keys-shift-translated t))
  13243. (call-interactively cmd)))
  13244. (defun org-shifttab (&optional arg)
  13245. "Global visibility cycling or move to previous table field.
  13246. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13247. on context.
  13248. See the individual commands for more information."
  13249. (interactive "P")
  13250. (cond
  13251. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13252. ((integerp arg)
  13253. (message "Content view to level: %d" arg)
  13254. (org-content (prefix-numeric-value arg))
  13255. (setq org-cycle-global-status 'overview))
  13256. (t (call-interactively 'org-global-cycle))))
  13257. (defun org-shiftmetaleft ()
  13258. "Promote subtree or delete table column.
  13259. Calls `org-promote-subtree', `org-outdent-item',
  13260. or `org-table-delete-column', depending on context.
  13261. See the individual commands for more information."
  13262. (interactive)
  13263. (cond
  13264. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13265. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13266. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13267. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13268. (t (org-modifier-cursor-error))))
  13269. (defun org-shiftmetaright ()
  13270. "Demote subtree or insert table column.
  13271. Calls `org-demote-subtree', `org-indent-item',
  13272. or `org-table-insert-column', depending on context.
  13273. See the individual commands for more information."
  13274. (interactive)
  13275. (cond
  13276. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13277. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13278. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13279. ((org-at-item-p) (call-interactively 'org-indent-item))
  13280. (t (org-modifier-cursor-error))))
  13281. (defun org-shiftmetaup (&optional arg)
  13282. "Move subtree up or kill table row.
  13283. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13284. `org-move-item-up' depending on context. See the individual commands
  13285. for more information."
  13286. (interactive "P")
  13287. (cond
  13288. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13289. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13290. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13291. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13292. (t (org-modifier-cursor-error))))
  13293. (defun org-shiftmetadown (&optional arg)
  13294. "Move subtree down or insert table row.
  13295. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13296. `org-move-item-down', depending on context. See the individual
  13297. commands for more information."
  13298. (interactive "P")
  13299. (cond
  13300. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13301. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13302. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13303. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13304. (t (org-modifier-cursor-error))))
  13305. (defun org-metaleft (&optional arg)
  13306. "Promote heading or move table column to left.
  13307. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13308. With no specific context, calls the Emacs default `backward-word'.
  13309. See the individual commands for more information."
  13310. (interactive "P")
  13311. (cond
  13312. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13313. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13314. ((or (org-on-heading-p)
  13315. (and (org-region-active-p)
  13316. (save-excursion
  13317. (goto-char (region-beginning))
  13318. (org-on-heading-p))))
  13319. (call-interactively 'org-do-promote))
  13320. ((or (org-at-item-p)
  13321. (and (org-region-active-p)
  13322. (save-excursion
  13323. (goto-char (region-beginning))
  13324. (org-at-item-p))))
  13325. (call-interactively 'org-outdent-item))
  13326. (t (call-interactively 'backward-word))))
  13327. (defun org-metaright (&optional arg)
  13328. "Demote subtree or move table column to right.
  13329. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13330. With no specific context, calls the Emacs default `forward-word'.
  13331. See the individual commands for more information."
  13332. (interactive "P")
  13333. (cond
  13334. ((run-hook-with-args-until-success 'org-metaright-hook))
  13335. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13336. ((or (org-on-heading-p)
  13337. (and (org-region-active-p)
  13338. (save-excursion
  13339. (goto-char (region-beginning))
  13340. (org-on-heading-p))))
  13341. (call-interactively 'org-do-demote))
  13342. ((or (org-at-item-p)
  13343. (and (org-region-active-p)
  13344. (save-excursion
  13345. (goto-char (region-beginning))
  13346. (org-at-item-p))))
  13347. (call-interactively 'org-indent-item))
  13348. (t (call-interactively 'forward-word))))
  13349. (defun org-metaup (&optional arg)
  13350. "Move subtree up or move table row up.
  13351. Calls `org-move-subtree-up' or `org-table-move-row' or
  13352. `org-move-item-up', depending on context. See the individual commands
  13353. for more information."
  13354. (interactive "P")
  13355. (cond
  13356. ((run-hook-with-args-until-success 'org-metaup-hook))
  13357. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13358. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13359. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13360. (t (transpose-lines 1) (beginning-of-line -1))))
  13361. (defun org-metadown (&optional arg)
  13362. "Move subtree down or move table row down.
  13363. Calls `org-move-subtree-down' or `org-table-move-row' or
  13364. `org-move-item-down', depending on context. See the individual
  13365. commands for more information."
  13366. (interactive "P")
  13367. (cond
  13368. ((run-hook-with-args-until-success 'org-metadown-hook))
  13369. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13370. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13371. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13372. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13373. (defun org-shiftup (&optional arg)
  13374. "Increase item in timestamp or increase priority of current headline.
  13375. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13376. depending on context. See the individual commands for more information."
  13377. (interactive "P")
  13378. (cond
  13379. ((and org-support-shift-select (org-region-active-p))
  13380. (org-call-for-shift-select 'previous-line))
  13381. ((org-at-timestamp-p t)
  13382. (call-interactively (if org-edit-timestamp-down-means-later
  13383. 'org-timestamp-down 'org-timestamp-up)))
  13384. ((and (not (eq org-support-shift-select 'always))
  13385. org-enable-priority-commands
  13386. (org-on-heading-p))
  13387. (call-interactively 'org-priority-up))
  13388. ((and (not org-support-shift-select) (org-at-item-p))
  13389. (call-interactively 'org-previous-item))
  13390. ((org-clocktable-try-shift 'up arg))
  13391. (org-support-shift-select
  13392. (org-call-for-shift-select 'previous-line))
  13393. (t (org-shiftselect-error))))
  13394. (defun org-shiftdown (&optional arg)
  13395. "Decrease item in timestamp or decrease priority of current headline.
  13396. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13397. depending on context. See the individual commands for more information."
  13398. (interactive "P")
  13399. (cond
  13400. ((and org-support-shift-select (org-region-active-p))
  13401. (org-call-for-shift-select 'next-line))
  13402. ((org-at-timestamp-p t)
  13403. (call-interactively (if org-edit-timestamp-down-means-later
  13404. 'org-timestamp-up 'org-timestamp-down)))
  13405. ((and (not (eq org-support-shift-select 'always))
  13406. org-enable-priority-commands
  13407. (org-on-heading-p))
  13408. (call-interactively 'org-priority-down))
  13409. ((and (not org-support-shift-select) (org-at-item-p))
  13410. (call-interactively 'org-next-item))
  13411. ((org-clocktable-try-shift 'down arg))
  13412. (org-support-shift-select
  13413. (org-call-for-shift-select 'next-line))
  13414. (t (org-shiftselect-error))))
  13415. (defun org-shiftright (&optional arg)
  13416. "Cycle the thing at point or in the current line, depending on context.
  13417. Depending on context, this does one of the following:
  13418. - switch a timestamp at point one day into the future
  13419. - on a headline, switch to the next TODO keyword.
  13420. - on an item, switch entire list to the next bullet type
  13421. - on a property line, switch to the next allowed value
  13422. - on a clocktable definition line, move time block into the future"
  13423. (interactive "P")
  13424. (cond
  13425. ((and org-support-shift-select (org-region-active-p))
  13426. (org-call-for-shift-select 'forward-char))
  13427. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13428. ((and (not (eq org-support-shift-select 'always))
  13429. (org-on-heading-p))
  13430. (let ((org-inhibit-logging
  13431. (not org-treat-S-cursor-todo-selection-as-state-change))
  13432. (org-inhibit-blocking
  13433. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13434. (org-call-with-arg 'org-todo 'right)))
  13435. ((or (and org-support-shift-select
  13436. (not (eq org-support-shift-select 'always))
  13437. (org-at-item-bullet-p))
  13438. (and (not org-support-shift-select) (org-at-item-p)))
  13439. (org-call-with-arg 'org-cycle-list-bullet nil))
  13440. ((and (not (eq org-support-shift-select 'always))
  13441. (org-at-property-p))
  13442. (call-interactively 'org-property-next-allowed-value))
  13443. ((org-clocktable-try-shift 'right arg))
  13444. (org-support-shift-select
  13445. (org-call-for-shift-select 'forward-char))
  13446. (t (org-shiftselect-error))))
  13447. (defun org-shiftleft (&optional arg)
  13448. "Cycle the thing at point or in the current line, depending on context.
  13449. Depending on context, this does one of the following:
  13450. - switch a timestamp at point one day into the past
  13451. - on a headline, switch to the previous TODO keyword.
  13452. - on an item, switch entire list to the previous bullet type
  13453. - on a property line, switch to the previous allowed value
  13454. - on a clocktable definition line, move time block into the past"
  13455. (interactive "P")
  13456. (cond
  13457. ((and org-support-shift-select (org-region-active-p))
  13458. (org-call-for-shift-select 'backward-char))
  13459. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13460. ((and (not (eq org-support-shift-select 'always))
  13461. (org-on-heading-p))
  13462. (let ((org-inhibit-logging
  13463. (not org-treat-S-cursor-todo-selection-as-state-change))
  13464. (org-inhibit-blocking
  13465. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13466. (org-call-with-arg 'org-todo 'left)))
  13467. ((or (and org-support-shift-select
  13468. (not (eq org-support-shift-select 'always))
  13469. (org-at-item-bullet-p))
  13470. (and (not org-support-shift-select) (org-at-item-p)))
  13471. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13472. ((and (not (eq org-support-shift-select 'always))
  13473. (org-at-property-p))
  13474. (call-interactively 'org-property-previous-allowed-value))
  13475. ((org-clocktable-try-shift 'left arg))
  13476. (org-support-shift-select
  13477. (org-call-for-shift-select 'backward-char))
  13478. (t (org-shiftselect-error))))
  13479. (defun org-shiftcontrolright ()
  13480. "Switch to next TODO set."
  13481. (interactive)
  13482. (cond
  13483. ((and org-support-shift-select (org-region-active-p))
  13484. (org-call-for-shift-select 'forward-word))
  13485. ((and (not (eq org-support-shift-select 'always))
  13486. (org-on-heading-p))
  13487. (org-call-with-arg 'org-todo 'nextset))
  13488. (org-support-shift-select
  13489. (org-call-for-shift-select 'forward-word))
  13490. (t (org-shiftselect-error))))
  13491. (defun org-shiftcontrolleft ()
  13492. "Switch to previous TODO set."
  13493. (interactive)
  13494. (cond
  13495. ((and org-support-shift-select (org-region-active-p))
  13496. (org-call-for-shift-select 'backward-word))
  13497. ((and (not (eq org-support-shift-select 'always))
  13498. (org-on-heading-p))
  13499. (org-call-with-arg 'org-todo 'previousset))
  13500. (org-support-shift-select
  13501. (org-call-for-shift-select 'backward-word))
  13502. (t (org-shiftselect-error))))
  13503. (defun org-ctrl-c-ret ()
  13504. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13505. (interactive)
  13506. (cond
  13507. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13508. (t (call-interactively 'org-insert-heading))))
  13509. (defun org-copy-special ()
  13510. "Copy region in table or copy current subtree.
  13511. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13512. See the individual commands for more information."
  13513. (interactive)
  13514. (call-interactively
  13515. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13516. (defun org-cut-special ()
  13517. "Cut region in table or cut current subtree.
  13518. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13519. See the individual commands for more information."
  13520. (interactive)
  13521. (call-interactively
  13522. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13523. (defun org-paste-special (arg)
  13524. "Paste rectangular region into table, or past subtree relative to level.
  13525. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13526. See the individual commands for more information."
  13527. (interactive "P")
  13528. (if (org-at-table-p)
  13529. (org-table-paste-rectangle)
  13530. (org-paste-subtree arg)))
  13531. (defun org-edit-special ()
  13532. "Call a special editor for the stuff at point.
  13533. When at a table, call the formula editor with `org-table-edit-formulas'.
  13534. When at the first line of an src example, call `org-edit-src-code'.
  13535. When in an #+include line, visit the include file. Otherwise call
  13536. `ffap' to visit the file at point."
  13537. (interactive)
  13538. (cond
  13539. ((org-at-table-p)
  13540. (call-interactively 'org-table-edit-formulas))
  13541. ((save-excursion
  13542. (beginning-of-line 1)
  13543. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13544. (find-file (org-trim (match-string 1))))
  13545. ((org-edit-src-code))
  13546. ((org-edit-fixed-width-region))
  13547. (t (call-interactively 'ffap))))
  13548. (defun org-ctrl-c-ctrl-c (&optional arg)
  13549. "Set tags in headline, or update according to changed information at point.
  13550. This command does many different things, depending on context:
  13551. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13552. this is what we do.
  13553. - If the cursor is in a headline, prompt for tags and insert them
  13554. into the current line, aligned to `org-tags-column'. When called
  13555. with prefix arg, realign all tags in the current buffer.
  13556. - If the cursor is in one of the special #+KEYWORD lines, this
  13557. triggers scanning the buffer for these lines and updating the
  13558. information.
  13559. - If the cursor is inside a table, realign the table. This command
  13560. works even if the automatic table editor has been turned off.
  13561. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13562. the entire table.
  13563. - If the cursor is at a footnote reference or definition, jump to
  13564. the corresponding definition or references, respectively.
  13565. - If the cursor is a the beginning of a dynamic block, update it.
  13566. - If the cursor is inside a table created by the table.el package,
  13567. activate that table.
  13568. - If the current buffer is a remember buffer, close note and file
  13569. it. A prefix argument of 1 files to the default location
  13570. without further interaction. A prefix argument of 2 files to
  13571. the currently clocking task.
  13572. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13573. links in this buffer.
  13574. - If the cursor is on a numbered item in a plain list, renumber the
  13575. ordered list.
  13576. - If the cursor is on a checkbox, toggle it."
  13577. (interactive "P")
  13578. (let ((org-enable-table-editor t))
  13579. (cond
  13580. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13581. org-occur-highlights
  13582. org-latex-fragment-image-overlays)
  13583. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13584. (org-remove-occur-highlights)
  13585. (org-remove-latex-fragment-image-overlays)
  13586. (message "Temporary highlights/overlays removed from current buffer"))
  13587. ((and (local-variable-p 'org-finish-function (current-buffer))
  13588. (fboundp org-finish-function))
  13589. (funcall org-finish-function))
  13590. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13591. ((org-at-property-p)
  13592. (call-interactively 'org-property-action))
  13593. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13594. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13595. ((org-at-table.el-p)
  13596. (require 'table)
  13597. (beginning-of-line 1)
  13598. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13599. (call-interactively 'table-recognize-table))
  13600. ((org-at-table-p)
  13601. (org-table-maybe-eval-formula)
  13602. (if arg
  13603. (call-interactively 'org-table-recalculate)
  13604. (org-table-maybe-recalculate-line))
  13605. (call-interactively 'org-table-align))
  13606. ((or (org-footnote-at-reference-p)
  13607. (org-footnote-at-definition-p))
  13608. (call-interactively 'org-footnote-action))
  13609. ((org-at-item-checkbox-p)
  13610. (call-interactively 'org-toggle-checkbox))
  13611. ((org-at-item-p)
  13612. (if arg
  13613. (call-interactively 'org-toggle-checkbox)
  13614. (call-interactively 'org-maybe-renumber-ordered-list)))
  13615. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13616. ;; Dynamic block
  13617. (beginning-of-line 1)
  13618. (save-excursion (org-update-dblock)))
  13619. ((save-excursion
  13620. (beginning-of-line 1)
  13621. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13622. (cond
  13623. ((equal (match-string 1) "TBLFM")
  13624. ;; Recalculate the table before this line
  13625. (save-excursion
  13626. (beginning-of-line 1)
  13627. (skip-chars-backward " \r\n\t")
  13628. (if (org-at-table-p)
  13629. (org-call-with-arg 'org-table-recalculate t))))
  13630. (t
  13631. ; (org-set-regexps-and-options)
  13632. ; (org-restart-font-lock)
  13633. (let ((org-inhibit-startup t)) (org-mode-restart))
  13634. (message "Local setup has been refreshed"))))
  13635. ((org-clock-update-time-maybe))
  13636. (t (error "C-c C-c can do nothing useful at this location.")))))
  13637. (defun org-mode-restart ()
  13638. "Restart Org-mode, to scan again for special lines.
  13639. Also updates the keyword regular expressions."
  13640. (interactive)
  13641. (org-mode)
  13642. (message "Org-mode restarted"))
  13643. (defun org-kill-note-or-show-branches ()
  13644. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13645. (interactive)
  13646. (if (not org-finish-function)
  13647. (call-interactively 'show-branches)
  13648. (let ((org-note-abort t))
  13649. (funcall org-finish-function))))
  13650. (defun org-return (&optional indent)
  13651. "Goto next table row or insert a newline.
  13652. Calls `org-table-next-row' or `newline', depending on context.
  13653. See the individual commands for more information."
  13654. (interactive)
  13655. (cond
  13656. ((bobp) (if indent (newline-and-indent) (newline)))
  13657. ((org-at-table-p)
  13658. (org-table-justify-field-maybe)
  13659. (call-interactively 'org-table-next-row))
  13660. ((and org-return-follows-link
  13661. (eq (get-text-property (point) 'face) 'org-link))
  13662. (call-interactively 'org-open-at-point))
  13663. ((and (org-at-heading-p)
  13664. (looking-at
  13665. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13666. (org-show-entry)
  13667. (end-of-line 1)
  13668. (newline))
  13669. (t (if indent (newline-and-indent) (newline)))))
  13670. (defun org-return-indent ()
  13671. "Goto next table row or insert a newline and indent.
  13672. Calls `org-table-next-row' or `newline-and-indent', depending on
  13673. context. See the individual commands for more information."
  13674. (interactive)
  13675. (org-return t))
  13676. (defun org-ctrl-c-star ()
  13677. "Compute table, or change heading status of lines.
  13678. Calls `org-table-recalculate' or `org-toggle-heading',
  13679. depending on context."
  13680. (interactive)
  13681. (cond
  13682. ((org-at-table-p)
  13683. (call-interactively 'org-table-recalculate))
  13684. (t
  13685. ;; Convert all lines in region to list items
  13686. (call-interactively 'org-toggle-heading))))
  13687. (defun org-ctrl-c-minus ()
  13688. "Insert separator line in table or modify bullet status of line.
  13689. Also turns a plain line or a region of lines into list items.
  13690. Calls `org-table-insert-hline', `org-toggle-item', or
  13691. `org-cycle-list-bullet', depending on context."
  13692. (interactive)
  13693. (cond
  13694. ((org-at-table-p)
  13695. (call-interactively 'org-table-insert-hline))
  13696. ((org-region-active-p)
  13697. (call-interactively 'org-toggle-item))
  13698. ((org-in-item-p)
  13699. (call-interactively 'org-cycle-list-bullet))
  13700. (t
  13701. (call-interactively 'org-toggle-item))))
  13702. (defun org-toggle-item ()
  13703. "Convert headings or normal lines to items, items to normal lines.
  13704. If there is no active region, only the current line is considered.
  13705. If the first line in the region is a headline, convert all headlines to items.
  13706. If the first line in the region is an item, convert all items to normal lines.
  13707. If the first line is normal text, add an item bullet to each line."
  13708. (interactive)
  13709. (let (l2 l beg end)
  13710. (if (org-region-active-p)
  13711. (setq beg (region-beginning) end (region-end))
  13712. (setq beg (point-at-bol)
  13713. end (min (1+ (point-at-eol)) (point-max))))
  13714. (save-excursion
  13715. (goto-char end)
  13716. (setq l2 (org-current-line))
  13717. (goto-char beg)
  13718. (beginning-of-line 1)
  13719. (setq l (1- (org-current-line)))
  13720. (if (org-at-item-p)
  13721. ;; We already have items, de-itemize
  13722. (while (< (setq l (1+ l)) l2)
  13723. (when (org-at-item-p)
  13724. (goto-char (match-beginning 2))
  13725. (delete-region (match-beginning 2) (match-end 2))
  13726. (and (looking-at "[ \t]+") (replace-match "")))
  13727. (beginning-of-line 2))
  13728. (if (org-on-heading-p)
  13729. ;; Headings, convert to items
  13730. (while (< (setq l (1+ l)) l2)
  13731. (if (looking-at org-outline-regexp)
  13732. (replace-match "- " t t))
  13733. (beginning-of-line 2))
  13734. ;; normal lines, turn them into items
  13735. (while (< (setq l (1+ l)) l2)
  13736. (unless (org-at-item-p)
  13737. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13738. (replace-match "\\1- \\2")))
  13739. (beginning-of-line 2)))))))
  13740. (defun org-toggle-heading (&optional nstars)
  13741. "Convert headings to normal text, or items or text to headings.
  13742. If there is no active region, only the current line is considered.
  13743. If the first line is a heading, remove the stars from all headlines
  13744. in the region.
  13745. If the first line is a plain list item, turn all plain list items
  13746. into headings.
  13747. If the first line is a normal line, turn each and every line in the
  13748. region into a heading.
  13749. When converting a line into a heading, the number of stars is chosen
  13750. such that the lines become children of the current entry. However,
  13751. when a prefix argument is given, its value determines the number of
  13752. stars to add."
  13753. (interactive "P")
  13754. (let (l2 l itemp beg end)
  13755. (if (org-region-active-p)
  13756. (setq beg (region-beginning) end (region-end))
  13757. (setq beg (point-at-bol)
  13758. end (min (1+ (point-at-eol)) (point-max))))
  13759. (save-excursion
  13760. (goto-char end)
  13761. (setq l2 (org-current-line))
  13762. (goto-char beg)
  13763. (beginning-of-line 1)
  13764. (setq l (1- (org-current-line)))
  13765. (if (org-on-heading-p)
  13766. ;; We already have headlines, de-star them
  13767. (while (< (setq l (1+ l)) l2)
  13768. (when (org-on-heading-p t)
  13769. (and (looking-at outline-regexp) (replace-match "")))
  13770. (beginning-of-line 2))
  13771. (setq itemp (org-at-item-p))
  13772. (let* ((stars
  13773. (if nstars
  13774. (make-string (prefix-numeric-value current-prefix-arg)
  13775. ?*)
  13776. (save-excursion
  13777. (if (re-search-backward org-complex-heading-regexp nil t)
  13778. (match-string 1) ""))))
  13779. (add-stars (cond (nstars "")
  13780. ((equal stars "") "*")
  13781. (org-odd-levels-only "**")
  13782. (t "*")))
  13783. (rpl (concat stars add-stars " ")))
  13784. (while (< (setq l (1+ l)) l2)
  13785. (if itemp
  13786. (and (org-at-item-p) (replace-match rpl t t))
  13787. (unless (org-on-heading-p)
  13788. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13789. (replace-match (concat rpl (match-string 2))))))
  13790. (beginning-of-line 2)))))))
  13791. (defun org-meta-return (&optional arg)
  13792. "Insert a new heading or wrap a region in a table.
  13793. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13794. See the individual commands for more information."
  13795. (interactive "P")
  13796. (cond
  13797. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13798. ((org-at-table-p)
  13799. (call-interactively 'org-table-wrap-region))
  13800. (t (call-interactively 'org-insert-heading))))
  13801. ;;; Menu entries
  13802. ;; Define the Org-mode menus
  13803. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13804. '("Tbl"
  13805. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13806. ["Next Field" org-cycle (org-at-table-p)]
  13807. ["Previous Field" org-shifttab (org-at-table-p)]
  13808. ["Next Row" org-return (org-at-table-p)]
  13809. "--"
  13810. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13811. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13812. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13813. "--"
  13814. ("Column"
  13815. ["Move Column Left" org-metaleft (org-at-table-p)]
  13816. ["Move Column Right" org-metaright (org-at-table-p)]
  13817. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13818. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13819. ("Row"
  13820. ["Move Row Up" org-metaup (org-at-table-p)]
  13821. ["Move Row Down" org-metadown (org-at-table-p)]
  13822. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13823. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13824. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13825. "--"
  13826. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13827. ("Rectangle"
  13828. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13829. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13830. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13831. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13832. "--"
  13833. ("Calculate"
  13834. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13835. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13836. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13837. "--"
  13838. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13839. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13840. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13841. "--"
  13842. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13843. "--"
  13844. ["Sum Column/Rectangle" org-table-sum
  13845. (or (org-at-table-p) (org-region-active-p))]
  13846. ["Which Column?" org-table-current-column (org-at-table-p)])
  13847. ["Debug Formulas"
  13848. org-table-toggle-formula-debugger
  13849. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13850. ["Show Col/Row Numbers"
  13851. org-table-toggle-coordinate-overlays
  13852. :style toggle
  13853. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13854. "--"
  13855. ["Create" org-table-create (and (not (org-at-table-p))
  13856. org-enable-table-editor)]
  13857. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13858. ["Import from File" org-table-import (not (org-at-table-p))]
  13859. ["Export to File" org-table-export (org-at-table-p)]
  13860. "--"
  13861. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13862. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13863. '("Org"
  13864. ("Show/Hide"
  13865. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13866. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13867. ["Sparse Tree..." org-sparse-tree t]
  13868. ["Reveal Context" org-reveal t]
  13869. ["Show All" show-all t]
  13870. "--"
  13871. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13872. "--"
  13873. ["New Heading" org-insert-heading t]
  13874. ("Navigate Headings"
  13875. ["Up" outline-up-heading t]
  13876. ["Next" outline-next-visible-heading t]
  13877. ["Previous" outline-previous-visible-heading t]
  13878. ["Next Same Level" outline-forward-same-level t]
  13879. ["Previous Same Level" outline-backward-same-level t]
  13880. "--"
  13881. ["Jump" org-goto t])
  13882. ("Edit Structure"
  13883. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13884. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13885. "--"
  13886. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13887. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13888. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13889. "--"
  13890. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13891. "--"
  13892. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13893. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13894. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13895. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13896. "--"
  13897. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13898. "--"
  13899. ["Convert to odd levels" org-convert-to-odd-levels t]
  13900. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13901. ("Editing"
  13902. ["Emphasis..." org-emphasize t]
  13903. ["Edit Source Example" org-edit-special t]
  13904. "--"
  13905. ["Footnote new/jump" org-footnote-action t]
  13906. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13907. ("Archive"
  13908. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13909. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13910. ; :active t :keys "C-u C-c C-x C-a"]
  13911. ["Sparse trees open ARCHIVE trees"
  13912. (setq org-sparse-tree-open-archived-trees
  13913. (not org-sparse-tree-open-archived-trees))
  13914. :style toggle :selected org-sparse-tree-open-archived-trees]
  13915. ["Cycling opens ARCHIVE trees"
  13916. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13917. :style toggle :selected org-cycle-open-archived-trees]
  13918. "--"
  13919. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13920. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13921. ; ["Check and Move Children" (org-archive-subtree '(4))
  13922. ; :active t :keys "C-u C-c C-x C-s"]
  13923. )
  13924. "--"
  13925. ("Hyperlinks"
  13926. ["Store Link (Global)" org-store-link t]
  13927. ["Find existing link to here" org-occur-link-in-agenda-files t]
  13928. ["Insert Link" org-insert-link t]
  13929. ["Follow Link" org-open-at-point t]
  13930. "--"
  13931. ["Next link" org-next-link t]
  13932. ["Previous link" org-previous-link t]
  13933. "--"
  13934. ["Descriptive Links"
  13935. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13936. :style radio
  13937. :selected (member '(org-link) buffer-invisibility-spec)]
  13938. ["Literal Links"
  13939. (progn
  13940. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13941. :style radio
  13942. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13943. "--"
  13944. ("TODO Lists"
  13945. ["TODO/DONE/-" org-todo t]
  13946. ("Select keyword"
  13947. ["Next keyword" org-shiftright (org-on-heading-p)]
  13948. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13949. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13950. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13951. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13952. ["Show TODO Tree" org-show-todo-tree t]
  13953. ["Global TODO list" org-todo-list t]
  13954. "--"
  13955. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13956. :selected org-enforce-todo-dependencies :style toggle :active t]
  13957. "Settings for tree at point"
  13958. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13959. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13960. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13961. ["Do Children parallel" org-toggle-ordered-property :style radio
  13962. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13963. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13964. "--"
  13965. ["Set Priority" org-priority t]
  13966. ["Priority Up" org-shiftup t]
  13967. ["Priority Down" org-shiftdown t]
  13968. "--"
  13969. ["Get news from all feeds" org-feed-update-all t]
  13970. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13971. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13972. ("TAGS and Properties"
  13973. ["Set Tags" org-set-tags-command t]
  13974. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13975. "--"
  13976. ["Set property" org-set-property t]
  13977. ["Column view of properties" org-columns t]
  13978. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13979. ("Dates and Scheduling"
  13980. ["Timestamp" org-time-stamp t]
  13981. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13982. ("Change Date"
  13983. ["1 Day Later" org-shiftright t]
  13984. ["1 Day Earlier" org-shiftleft t]
  13985. ["1 ... Later" org-shiftup t]
  13986. ["1 ... Earlier" org-shiftdown t])
  13987. ["Compute Time Range" org-evaluate-time-range t]
  13988. ["Schedule Item" org-schedule t]
  13989. ["Deadline" org-deadline t]
  13990. "--"
  13991. ["Custom time format" org-toggle-time-stamp-overlays
  13992. :style radio :selected org-display-custom-times]
  13993. "--"
  13994. ["Goto Calendar" org-goto-calendar t]
  13995. ["Date from Calendar" org-date-from-calendar t]
  13996. "--"
  13997. ["Start/Restart Timer" org-timer-start t]
  13998. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13999. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14000. ["Insert Timer String" org-timer t]
  14001. ["Insert Timer Item" org-timer-item t])
  14002. ("Logging work"
  14003. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14004. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14005. ["Clock out" org-clock-out t]
  14006. ["Clock cancel" org-clock-cancel t]
  14007. "--"
  14008. ["Mark as default task" org-clock-mark-default-task t]
  14009. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14010. ["Goto running clock" org-clock-goto t]
  14011. "--"
  14012. ["Display times" org-clock-display t]
  14013. ["Create clock table" org-clock-report t]
  14014. "--"
  14015. ["Record DONE time"
  14016. (progn (setq org-log-done (not org-log-done))
  14017. (message "Switching to %s will %s record a timestamp"
  14018. (car org-done-keywords)
  14019. (if org-log-done "automatically" "not")))
  14020. :style toggle :selected org-log-done])
  14021. "--"
  14022. ["Agenda Command..." org-agenda t]
  14023. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14024. ("File List for Agenda")
  14025. ("Special views current file"
  14026. ["TODO Tree" org-show-todo-tree t]
  14027. ["Check Deadlines" org-check-deadlines t]
  14028. ["Timeline" org-timeline t]
  14029. ["Tags/Property tree" org-match-sparse-tree t])
  14030. "--"
  14031. ["Export/Publish..." org-export t]
  14032. ("LaTeX"
  14033. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14034. :selected org-cdlatex-mode]
  14035. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14036. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14037. ["Modify math symbol" org-cdlatex-math-modify
  14038. (org-inside-LaTeX-fragment-p)]
  14039. ["Insert citation" org-reftex-citation t]
  14040. "--"
  14041. ["Export LaTeX fragments as images"
  14042. (if (featurep 'org-exp)
  14043. (setq org-export-with-LaTeX-fragments
  14044. (not org-export-with-LaTeX-fragments))
  14045. (require 'org-exp))
  14046. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14047. org-export-with-LaTeX-fragments)])
  14048. "--"
  14049. ("Documentation"
  14050. ["Show Version" org-version t]
  14051. ["Info Documentation" org-info t])
  14052. ("Customize"
  14053. ["Browse Org Group" org-customize t]
  14054. "--"
  14055. ["Expand This Menu" org-create-customize-menu
  14056. (fboundp 'customize-menu-create)])
  14057. ["Send bug report" org-submit-bug-report t]
  14058. "--"
  14059. ("Refresh/Reload"
  14060. ["Refresh setup current buffer" org-mode-restart t]
  14061. ["Reload Org (after update)" org-reload t]
  14062. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14063. ))
  14064. (defun org-info (&optional node)
  14065. "Read documentation for Org-mode in the info system.
  14066. With optional NODE, go directly to that node."
  14067. (interactive)
  14068. (info (format "(org)%s" (or node ""))))
  14069. ;;;###autoload
  14070. (defun org-submit-bug-report ()
  14071. "Submit a bug report on Org-mode via mail.
  14072. Don't hesitate to report any problems or inaccurate documentation.
  14073. If you don't have setup sending mail from (X)Emacs, please copy the
  14074. output buffer into your mail program, as it gives us important
  14075. information about your Org-mode version and configuration."
  14076. (interactive)
  14077. (require 'reporter)
  14078. (org-load-modules-maybe)
  14079. (org-require-autoloaded-modules)
  14080. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14081. (reporter-submit-bug-report
  14082. "emacs-orgmode@gnu.org"
  14083. (org-version)
  14084. (let (list)
  14085. (save-window-excursion
  14086. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14087. (delete-other-windows)
  14088. (erase-buffer)
  14089. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14090. We would like to add your full Org-mode and Outline configuration to the
  14091. bug report. This greatly simplifies the work of the maintainer and
  14092. other experts on the mailing list.
  14093. HOWEVER, some variables you have customized may contain private
  14094. information. The names of customers, colleagues, or friends, might
  14095. appear in the form of file names, tags, todo states, or search strings.
  14096. If you answer yes to the prompt, you might want to check and remove
  14097. such private information before sending the email.")
  14098. (add-text-properties (point-min) (point-max) '(face org-warning))
  14099. (when (yes-or-no-p "Include your Org-mode configuration ")
  14100. (mapatoms
  14101. (lambda (v)
  14102. (and (boundp v)
  14103. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14104. (or (and (symbol-value v)
  14105. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14106. (and
  14107. (get v 'custom-type) (get v 'standard-value)
  14108. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14109. (push v list)))))
  14110. (kill-buffer (get-buffer "*Warn about privacy*"))
  14111. list))
  14112. nil nil
  14113. "Remember to cover the basics, that is, what you expected to happen and
  14114. what in fact did happen. You don't know hoe to make a good report? See
  14115. http://orgmode.org/manual/Feedback.html#Feedback
  14116. Your bug report will be posted to the Org-mode mailing list.
  14117. ------------------------------------------------------------------------")))
  14118. (defun org-install-agenda-files-menu ()
  14119. (let ((bl (buffer-list)))
  14120. (save-excursion
  14121. (while bl
  14122. (set-buffer (pop bl))
  14123. (if (org-mode-p) (setq bl nil)))
  14124. (when (org-mode-p)
  14125. (easy-menu-change
  14126. '("Org") "File List for Agenda"
  14127. (append
  14128. (list
  14129. ["Edit File List" (org-edit-agenda-file-list) t]
  14130. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14131. ["Remove Current File from List" org-remove-file t]
  14132. ["Cycle through agenda files" org-cycle-agenda-files t]
  14133. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14134. "--")
  14135. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14136. ;;;; Documentation
  14137. ;;;###autoload
  14138. (defun org-require-autoloaded-modules ()
  14139. (interactive)
  14140. (mapc 'require
  14141. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14142. org-docbook org-exp org-html org-icalendar
  14143. org-id org-latex
  14144. org-publish org-remember org-table
  14145. org-timer org-xoxo)))
  14146. ;;;###autoload
  14147. (defun org-reload (&optional uncompiled)
  14148. "Reload all org lisp files.
  14149. With prefix arg UNCOMPILED, load the uncompiled versions."
  14150. (interactive "P")
  14151. (require 'find-func)
  14152. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14153. (dir-org (file-name-directory (org-find-library-name "org")))
  14154. (dir-org-contrib (ignore-errors
  14155. (file-name-directory
  14156. (org-find-library-name "org-contribdir"))))
  14157. (files
  14158. (append (directory-files dir-org t file-re)
  14159. (and dir-org-contrib
  14160. (directory-files dir-org-contrib t file-re))))
  14161. (remove-re (concat (if (featurep 'xemacs)
  14162. "org-colview" "org-colview-xemacs")
  14163. "\\'")))
  14164. (setq files (mapcar 'file-name-sans-extension files))
  14165. (setq files (mapcar
  14166. (lambda (x) (if (string-match remove-re x) nil x))
  14167. files))
  14168. (setq files (delq nil files))
  14169. (mapc
  14170. (lambda (f)
  14171. (when (featurep (intern (file-name-nondirectory f)))
  14172. (if (and (not uncompiled)
  14173. (file-exists-p (concat f ".elc")))
  14174. (load (concat f ".elc") nil nil t)
  14175. (load (concat f ".el") nil nil t))))
  14176. files))
  14177. (org-version))
  14178. ;;;###autoload
  14179. (defun org-customize ()
  14180. "Call the customize function with org as argument."
  14181. (interactive)
  14182. (org-load-modules-maybe)
  14183. (org-require-autoloaded-modules)
  14184. (customize-browse 'org))
  14185. (defun org-create-customize-menu ()
  14186. "Create a full customization menu for Org-mode, insert it into the menu."
  14187. (interactive)
  14188. (org-load-modules-maybe)
  14189. (org-require-autoloaded-modules)
  14190. (if (fboundp 'customize-menu-create)
  14191. (progn
  14192. (easy-menu-change
  14193. '("Org") "Customize"
  14194. `(["Browse Org group" org-customize t]
  14195. "--"
  14196. ,(customize-menu-create 'org)
  14197. ["Set" Custom-set t]
  14198. ["Save" Custom-save t]
  14199. ["Reset to Current" Custom-reset-current t]
  14200. ["Reset to Saved" Custom-reset-saved t]
  14201. ["Reset to Standard Settings" Custom-reset-standard t]))
  14202. (message "\"Org\"-menu now contains full customization menu"))
  14203. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14204. ;;;; Miscellaneous stuff
  14205. ;;; Generally useful functions
  14206. (defun org-find-text-property-in-string (prop s)
  14207. "Return the first non-nil value of property PROP in string S."
  14208. (or (get-text-property 0 prop s)
  14209. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14210. prop s)))
  14211. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14212. "Display the given MESSAGE as a warning."
  14213. (if (fboundp 'display-warning)
  14214. (display-warning 'org message
  14215. (if (featurep 'xemacs)
  14216. 'warning
  14217. :warning))
  14218. (let ((buf (get-buffer-create "*Org warnings*")))
  14219. (with-current-buffer buf
  14220. (goto-char (point-max))
  14221. (insert "Warning (Org): " message)
  14222. (unless (bolp)
  14223. (newline)))
  14224. (display-buffer buf)
  14225. (sit-for 0))))
  14226. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14227. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14228. (if (and marker (marker-buffer marker)
  14229. (buffer-live-p (marker-buffer marker)))
  14230. (progn
  14231. (switch-to-buffer (marker-buffer marker))
  14232. (if (or (> marker (point-max)) (< marker (point-min)))
  14233. (widen))
  14234. (goto-char marker)
  14235. (org-show-context 'org-goto))
  14236. (if bookmark
  14237. (bookmark-jump bookmark)
  14238. (error "Cannot find location"))))
  14239. (defun org-quote-csv-field (s)
  14240. "Quote field for inclusion in CSV material."
  14241. (if (string-match "[\",]" s)
  14242. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14243. s))
  14244. (defun org-plist-delete (plist property)
  14245. "Delete PROPERTY from PLIST.
  14246. This is in contrast to merely setting it to 0."
  14247. (let (p)
  14248. (while plist
  14249. (if (not (eq property (car plist)))
  14250. (setq p (plist-put p (car plist) (nth 1 plist))))
  14251. (setq plist (cddr plist)))
  14252. p))
  14253. (defun org-force-self-insert (N)
  14254. "Needed to enforce self-insert under remapping."
  14255. (interactive "p")
  14256. (self-insert-command N))
  14257. (defun org-string-width (s)
  14258. "Compute width of string, ignoring invisible characters.
  14259. This ignores character with invisibility property `org-link', and also
  14260. characters with property `org-cwidth', because these will become invisible
  14261. upon the next fontification round."
  14262. (let (b l)
  14263. (when (or (eq t buffer-invisibility-spec)
  14264. (assq 'org-link buffer-invisibility-spec))
  14265. (while (setq b (text-property-any 0 (length s)
  14266. 'invisible 'org-link s))
  14267. (setq s (concat (substring s 0 b)
  14268. (substring s (or (next-single-property-change
  14269. b 'invisible s) (length s)))))))
  14270. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14271. (setq s (concat (substring s 0 b)
  14272. (substring s (or (next-single-property-change
  14273. b 'org-cwidth s) (length s))))))
  14274. (setq l (string-width s) b -1)
  14275. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14276. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14277. l))
  14278. (defun org-get-indentation (&optional line)
  14279. "Get the indentation of the current line, interpreting tabs.
  14280. When LINE is given, assume it represents a line and compute its indentation."
  14281. (if line
  14282. (if (string-match "^ *" (org-remove-tabs line))
  14283. (match-end 0))
  14284. (save-excursion
  14285. (beginning-of-line 1)
  14286. (skip-chars-forward " \t")
  14287. (current-column))))
  14288. (defun org-remove-tabs (s &optional width)
  14289. "Replace tabulators in S with spaces.
  14290. Assumes that s is a single line, starting in column 0."
  14291. (setq width (or width tab-width))
  14292. (while (string-match "\t" s)
  14293. (setq s (replace-match
  14294. (make-string
  14295. (- (* width (/ (+ (match-beginning 0) width) width))
  14296. (match-beginning 0)) ?\ )
  14297. t t s)))
  14298. s)
  14299. (defun org-fix-indentation (line ind)
  14300. "Fix indentation in LINE.
  14301. IND is a cons cell with target and minimum indentation.
  14302. If the current indentation in LINE is smaller than the minimum,
  14303. leave it alone. If it is larger than ind, set it to the target."
  14304. (let* ((l (org-remove-tabs line))
  14305. (i (org-get-indentation l))
  14306. (i1 (car ind)) (i2 (cdr ind)))
  14307. (if (>= i i2) (setq l (substring line i2)))
  14308. (if (> i1 0)
  14309. (concat (make-string i1 ?\ ) l)
  14310. l)))
  14311. (defun org-remove-indentation (code &optional n)
  14312. "Remove the maximum common indentation from the lines in CODE.
  14313. N may optionally be the number of spaces to remove."
  14314. (with-temp-buffer
  14315. (insert code)
  14316. (org-do-remove-indentation n)
  14317. (buffer-string)))
  14318. (defun org-do-remove-indentation (&optional n)
  14319. "Remove the maximum common indentation from the buffer."
  14320. (untabify (point-min) (point-max))
  14321. (let ((min 10000) re)
  14322. (if n
  14323. (setq min n)
  14324. (goto-char (point-min))
  14325. (while (re-search-forward "^ *[^ \n]" nil t)
  14326. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14327. (unless (or (= min 0) (= min 10000))
  14328. (setq re (format "^ \\{%d\\}" min))
  14329. (goto-char (point-min))
  14330. (while (re-search-forward re nil t)
  14331. (replace-match "")
  14332. (end-of-line 1))
  14333. min)))
  14334. (defun org-base-buffer (buffer)
  14335. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14336. (if (not buffer)
  14337. buffer
  14338. (or (buffer-base-buffer buffer)
  14339. buffer)))
  14340. (defun org-trim (s)
  14341. "Remove whitespace at beginning and end of string."
  14342. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14343. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14344. s)
  14345. (defun org-wrap (string &optional width lines)
  14346. "Wrap string to either a number of lines, or a width in characters.
  14347. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14348. that costs. If there is a word longer than WIDTH, the text is actually
  14349. wrapped to the length of that word.
  14350. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14351. many lines, whatever width that takes.
  14352. The return value is a list of lines, without newlines at the end."
  14353. (let* ((words (org-split-string string "[ \t\n]+"))
  14354. (maxword (apply 'max (mapcar 'org-string-width words)))
  14355. w ll)
  14356. (cond (width
  14357. (org-do-wrap words (max maxword width)))
  14358. (lines
  14359. (setq w maxword)
  14360. (setq ll (org-do-wrap words maxword))
  14361. (if (<= (length ll) lines)
  14362. ll
  14363. (setq ll words)
  14364. (while (> (length ll) lines)
  14365. (setq w (1+ w))
  14366. (setq ll (org-do-wrap words w)))
  14367. ll))
  14368. (t (error "Cannot wrap this")))))
  14369. (defun org-do-wrap (words width)
  14370. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14371. (let (lines line)
  14372. (while words
  14373. (setq line (pop words))
  14374. (while (and words (< (+ (length line) (length (car words))) width))
  14375. (setq line (concat line " " (pop words))))
  14376. (setq lines (push line lines)))
  14377. (nreverse lines)))
  14378. (defun org-split-string (string &optional separators)
  14379. "Splits STRING into substrings at SEPARATORS.
  14380. No empty strings are returned if there are matches at the beginning
  14381. and end of string."
  14382. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14383. (start 0)
  14384. notfirst
  14385. (list nil))
  14386. (while (and (string-match rexp string
  14387. (if (and notfirst
  14388. (= start (match-beginning 0))
  14389. (< start (length string)))
  14390. (1+ start) start))
  14391. (< (match-beginning 0) (length string)))
  14392. (setq notfirst t)
  14393. (or (eq (match-beginning 0) 0)
  14394. (and (eq (match-beginning 0) (match-end 0))
  14395. (eq (match-beginning 0) start))
  14396. (setq list
  14397. (cons (substring string start (match-beginning 0))
  14398. list)))
  14399. (setq start (match-end 0)))
  14400. (or (eq start (length string))
  14401. (setq list
  14402. (cons (substring string start)
  14403. list)))
  14404. (nreverse list)))
  14405. (defun org-quote-vert (s)
  14406. "Replace \"|\" with \"\\vert\"."
  14407. (while (string-match "|" s)
  14408. (setq s (replace-match "\\vert" t t s)))
  14409. s)
  14410. (defun org-uuidgen-p (s)
  14411. "Is S an ID created by UUIDGEN?"
  14412. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14413. (defun org-context ()
  14414. "Return a list of contexts of the current cursor position.
  14415. If several contexts apply, all are returned.
  14416. Each context entry is a list with a symbol naming the context, and
  14417. two positions indicating start and end of the context. Possible
  14418. contexts are:
  14419. :headline anywhere in a headline
  14420. :headline-stars on the leading stars in a headline
  14421. :todo-keyword on a TODO keyword (including DONE) in a headline
  14422. :tags on the TAGS in a headline
  14423. :priority on the priority cookie in a headline
  14424. :item on the first line of a plain list item
  14425. :item-bullet on the bullet/number of a plain list item
  14426. :checkbox on the checkbox in a plain list item
  14427. :table in an org-mode table
  14428. :table-special on a special filed in a table
  14429. :table-table in a table.el table
  14430. :link on a hyperlink
  14431. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14432. :target on a <<target>>
  14433. :radio-target on a <<<radio-target>>>
  14434. :latex-fragment on a LaTeX fragment
  14435. :latex-preview on a LaTeX fragment with overlayed preview image
  14436. This function expects the position to be visible because it uses font-lock
  14437. faces as a help to recognize the following contexts: :table-special, :link,
  14438. and :keyword."
  14439. (let* ((f (get-text-property (point) 'face))
  14440. (faces (if (listp f) f (list f)))
  14441. (p (point)) clist o)
  14442. ;; First the large context
  14443. (cond
  14444. ((org-on-heading-p t)
  14445. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14446. (when (progn
  14447. (beginning-of-line 1)
  14448. (looking-at org-todo-line-tags-regexp))
  14449. (push (org-point-in-group p 1 :headline-stars) clist)
  14450. (push (org-point-in-group p 2 :todo-keyword) clist)
  14451. (push (org-point-in-group p 4 :tags) clist))
  14452. (goto-char p)
  14453. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14454. (if (looking-at "\\[#[A-Z0-9]\\]")
  14455. (push (org-point-in-group p 0 :priority) clist)))
  14456. ((org-at-item-p)
  14457. (push (org-point-in-group p 2 :item-bullet) clist)
  14458. (push (list :item (point-at-bol)
  14459. (save-excursion (org-end-of-item) (point)))
  14460. clist)
  14461. (and (org-at-item-checkbox-p)
  14462. (push (org-point-in-group p 0 :checkbox) clist)))
  14463. ((org-at-table-p)
  14464. (push (list :table (org-table-begin) (org-table-end)) clist)
  14465. (if (memq 'org-formula faces)
  14466. (push (list :table-special
  14467. (previous-single-property-change p 'face)
  14468. (next-single-property-change p 'face)) clist)))
  14469. ((org-at-table-p 'any)
  14470. (push (list :table-table) clist)))
  14471. (goto-char p)
  14472. ;; Now the small context
  14473. (cond
  14474. ((org-at-timestamp-p)
  14475. (push (org-point-in-group p 0 :timestamp) clist))
  14476. ((memq 'org-link faces)
  14477. (push (list :link
  14478. (previous-single-property-change p 'face)
  14479. (next-single-property-change p 'face)) clist))
  14480. ((memq 'org-special-keyword faces)
  14481. (push (list :keyword
  14482. (previous-single-property-change p 'face)
  14483. (next-single-property-change p 'face)) clist))
  14484. ((org-on-target-p)
  14485. (push (org-point-in-group p 0 :target) clist)
  14486. (goto-char (1- (match-beginning 0)))
  14487. (if (looking-at org-radio-target-regexp)
  14488. (push (org-point-in-group p 0 :radio-target) clist))
  14489. (goto-char p))
  14490. ((setq o (car (delq nil
  14491. (mapcar
  14492. (lambda (x)
  14493. (if (memq x org-latex-fragment-image-overlays) x))
  14494. (org-overlays-at (point))))))
  14495. (push (list :latex-fragment
  14496. (org-overlay-start o) (org-overlay-end o)) clist)
  14497. (push (list :latex-preview
  14498. (org-overlay-start o) (org-overlay-end o)) clist))
  14499. ((org-inside-LaTeX-fragment-p)
  14500. ;; FIXME: positions wrong.
  14501. (push (list :latex-fragment (point) (point)) clist)))
  14502. (setq clist (nreverse (delq nil clist)))
  14503. clist))
  14504. ;; FIXME: Compare with at-regexp-p Do we need both?
  14505. (defun org-in-regexp (re &optional nlines visually)
  14506. "Check if point is inside a match of regexp.
  14507. Normally only the current line is checked, but you can include NLINES extra
  14508. lines both before and after point into the search.
  14509. If VISUALLY is set, require that the cursor is not after the match but
  14510. really on, so that the block visually is on the match."
  14511. (catch 'exit
  14512. (let ((pos (point))
  14513. (eol (point-at-eol (+ 1 (or nlines 0))))
  14514. (inc (if visually 1 0)))
  14515. (save-excursion
  14516. (beginning-of-line (- 1 (or nlines 0)))
  14517. (while (re-search-forward re eol t)
  14518. (if (and (<= (match-beginning 0) pos)
  14519. (>= (+ inc (match-end 0)) pos))
  14520. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14521. (defun org-at-regexp-p (regexp)
  14522. "Is point inside a match of REGEXP in the current line?"
  14523. (catch 'exit
  14524. (save-excursion
  14525. (let ((pos (point)) (end (point-at-eol)))
  14526. (beginning-of-line 1)
  14527. (while (re-search-forward regexp end t)
  14528. (if (and (<= (match-beginning 0) pos)
  14529. (>= (match-end 0) pos))
  14530. (throw 'exit t)))
  14531. nil))))
  14532. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14533. "Call `multi-occur' with buffers for all agenda files."
  14534. (interactive "sOrg-files matching: \np")
  14535. (let* ((files (org-agenda-files))
  14536. (tnames (mapcar 'file-truename files))
  14537. (extra org-agenda-text-search-extra-files)
  14538. f)
  14539. (when (eq (car extra) 'agenda-archives)
  14540. (setq extra (cdr extra))
  14541. (setq files (org-add-archive-files files)))
  14542. (while (setq f (pop extra))
  14543. (unless (member (file-truename f) tnames)
  14544. (add-to-list 'files f 'append)
  14545. (add-to-list 'tnames (file-truename f) 'append)))
  14546. (multi-occur
  14547. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14548. regexp)))
  14549. (if (boundp 'occur-mode-find-occurrence-hook)
  14550. ;; Emacs 23
  14551. (add-hook 'occur-mode-find-occurrence-hook
  14552. (lambda ()
  14553. (when (org-mode-p)
  14554. (org-reveal))))
  14555. ;; Emacs 22
  14556. (defadvice occur-mode-goto-occurrence
  14557. (after org-occur-reveal activate)
  14558. (and (org-mode-p) (org-reveal)))
  14559. (defadvice occur-mode-goto-occurrence-other-window
  14560. (after org-occur-reveal activate)
  14561. (and (org-mode-p) (org-reveal)))
  14562. (defadvice occur-mode-display-occurrence
  14563. (after org-occur-reveal activate)
  14564. (when (org-mode-p)
  14565. (let ((pos (occur-mode-find-occurrence)))
  14566. (with-current-buffer (marker-buffer pos)
  14567. (save-excursion
  14568. (goto-char pos)
  14569. (org-reveal)))))))
  14570. (defun org-occur-link-in-agenda-files ()
  14571. "Create a link and search for it in the agendas.
  14572. The link is not stored in `org-stored-links', it is just created
  14573. for the search purpose."
  14574. (interactive)
  14575. (let ((link (condition-case nil
  14576. (org-store-link nil)
  14577. (error "Unable to create a link to here"))))
  14578. (org-occur-in-agenda-files (regexp-quote link))))
  14579. (defun org-uniquify (list)
  14580. "Remove duplicate elements from LIST."
  14581. (let (res)
  14582. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14583. res))
  14584. (defun org-delete-all (elts list)
  14585. "Remove all elements in ELTS from LIST."
  14586. (while elts
  14587. (setq list (delete (pop elts) list)))
  14588. list)
  14589. (defun org-back-over-empty-lines ()
  14590. "Move backwards over whitespace, to the beginning of the first empty line.
  14591. Returns the number of empty lines passed."
  14592. (let ((pos (point)))
  14593. (skip-chars-backward " \t\n\r")
  14594. (beginning-of-line 2)
  14595. (goto-char (min (point) pos))
  14596. (count-lines (point) pos)))
  14597. (defun org-skip-whitespace ()
  14598. (skip-chars-forward " \t\n\r"))
  14599. (defun org-point-in-group (point group &optional context)
  14600. "Check if POINT is in match-group GROUP.
  14601. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14602. match. If the match group does ot exist or point is not inside it,
  14603. return nil."
  14604. (and (match-beginning group)
  14605. (>= point (match-beginning group))
  14606. (<= point (match-end group))
  14607. (if context
  14608. (list context (match-beginning group) (match-end group))
  14609. t)))
  14610. (defun org-switch-to-buffer-other-window (&rest args)
  14611. "Switch to buffer in a second window on the current frame.
  14612. In particular, do not allow pop-up frames."
  14613. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14614. special-display-function)
  14615. (apply 'switch-to-buffer-other-window args)))
  14616. (defun org-combine-plists (&rest plists)
  14617. "Create a single property list from all plists in PLISTS.
  14618. The process starts by copying the first list, and then setting properties
  14619. from the other lists. Settings in the last list are the most significant
  14620. ones and overrule settings in the other lists."
  14621. (let ((rtn (copy-sequence (pop plists)))
  14622. p v ls)
  14623. (while plists
  14624. (setq ls (pop plists))
  14625. (while ls
  14626. (setq p (pop ls) v (pop ls))
  14627. (setq rtn (plist-put rtn p v))))
  14628. rtn))
  14629. (defun org-move-line-down (arg)
  14630. "Move the current line down. With prefix argument, move it past ARG lines."
  14631. (interactive "p")
  14632. (let ((col (current-column))
  14633. beg end pos)
  14634. (beginning-of-line 1) (setq beg (point))
  14635. (beginning-of-line 2) (setq end (point))
  14636. (beginning-of-line (+ 1 arg))
  14637. (setq pos (move-marker (make-marker) (point)))
  14638. (insert (delete-and-extract-region beg end))
  14639. (goto-char pos)
  14640. (org-move-to-column col)))
  14641. (defun org-move-line-up (arg)
  14642. "Move the current line up. With prefix argument, move it past ARG lines."
  14643. (interactive "p")
  14644. (let ((col (current-column))
  14645. beg end pos)
  14646. (beginning-of-line 1) (setq beg (point))
  14647. (beginning-of-line 2) (setq end (point))
  14648. (beginning-of-line (- arg))
  14649. (setq pos (move-marker (make-marker) (point)))
  14650. (insert (delete-and-extract-region beg end))
  14651. (goto-char pos)
  14652. (org-move-to-column col)))
  14653. (defun org-replace-escapes (string table)
  14654. "Replace %-escapes in STRING with values in TABLE.
  14655. TABLE is an association list with keys like \"%a\" and string values.
  14656. The sequences in STRING may contain normal field width and padding information,
  14657. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14658. so values can contain further %-escapes if they are define later in TABLE."
  14659. (let ((case-fold-search nil)
  14660. e re rpl)
  14661. (while (setq e (pop table))
  14662. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14663. (while (string-match re string)
  14664. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14665. (cdr e)))
  14666. (setq string (replace-match rpl t t string))))
  14667. string))
  14668. (defun org-sublist (list start end)
  14669. "Return a section of LIST, from START to END.
  14670. Counting starts at 1."
  14671. (let (rtn (c start))
  14672. (setq list (nthcdr (1- start) list))
  14673. (while (and list (<= c end))
  14674. (push (pop list) rtn)
  14675. (setq c (1+ c)))
  14676. (nreverse rtn)))
  14677. (defun org-find-base-buffer-visiting (file)
  14678. "Like `find-buffer-visiting' but always return the base buffer and
  14679. not an indirect buffer."
  14680. (let ((buf (or (get-file-buffer file)
  14681. (find-buffer-visiting file))))
  14682. (if buf
  14683. (or (buffer-base-buffer buf) buf)
  14684. nil)))
  14685. (defun org-image-file-name-regexp (&optional extensions)
  14686. "Return regexp matching the file names of images.
  14687. If EXTENSIONS is given, only match these."
  14688. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14689. (image-file-name-regexp)
  14690. (let ((image-file-name-extensions
  14691. (or extensions
  14692. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14693. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14694. (concat "\\."
  14695. (regexp-opt (nconc (mapcar 'upcase
  14696. image-file-name-extensions)
  14697. image-file-name-extensions)
  14698. t)
  14699. "\\'"))))
  14700. (defun org-file-image-p (file &optional extensions)
  14701. "Return non-nil if FILE is an image."
  14702. (save-match-data
  14703. (string-match (org-image-file-name-regexp extensions) file)))
  14704. (defun org-get-cursor-date ()
  14705. "Return the date at cursor in as a time.
  14706. This works in the calendar and in the agenda, anywhere else it just
  14707. returns the current time."
  14708. (let (date day defd)
  14709. (cond
  14710. ((eq major-mode 'calendar-mode)
  14711. (setq date (calendar-cursor-to-date)
  14712. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14713. ((eq major-mode 'org-agenda-mode)
  14714. (setq day (get-text-property (point) 'day))
  14715. (if day
  14716. (setq date (calendar-gregorian-from-absolute day)
  14717. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14718. (nth 2 date))))))
  14719. (or defd (current-time))))
  14720. (defvar org-agenda-action-marker (make-marker)
  14721. "Marker pointing to the entry for the next agenda action.")
  14722. (defun org-mark-entry-for-agenda-action ()
  14723. "Mark the current entry as target of an agenda action.
  14724. Agenda actions are actions executed from the agenda with the key `k',
  14725. which make use of the date at the cursor."
  14726. (interactive)
  14727. (move-marker org-agenda-action-marker
  14728. (save-excursion (org-back-to-heading t) (point))
  14729. (current-buffer))
  14730. (message
  14731. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14732. ;;; Paragraph filling stuff.
  14733. ;; We want this to be just right, so use the full arsenal.
  14734. (defun org-indent-line-function ()
  14735. "Indent line like previous, but further if previous was headline or item."
  14736. (interactive)
  14737. (let* ((pos (point))
  14738. (itemp (org-at-item-p))
  14739. (case-fold-search t)
  14740. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14741. column bpos bcol tpos tcol bullet btype bullet-type)
  14742. ;; Find the previous relevant line
  14743. (beginning-of-line 1)
  14744. (cond
  14745. ((looking-at "#") (setq column 0))
  14746. ((looking-at "\\*+ ") (setq column 0))
  14747. ((and (looking-at "[ \t]*:END:")
  14748. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14749. (save-excursion
  14750. (goto-char (1- (match-beginning 1)))
  14751. (setq column (current-column))))
  14752. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14753. (save-excursion
  14754. (re-search-backward
  14755. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14756. (setq column (org-get-indentation (match-string 0))))
  14757. (t
  14758. (beginning-of-line 0)
  14759. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14760. (not (looking-at "[ \t]*:END:"))
  14761. (not (looking-at org-drawer-regexp)))
  14762. (beginning-of-line 0))
  14763. (cond
  14764. ((looking-at "\\*+[ \t]+")
  14765. (if (not org-adapt-indentation)
  14766. (setq column 0)
  14767. (goto-char (match-end 0))
  14768. (setq column (current-column))))
  14769. ((looking-at org-drawer-regexp)
  14770. (goto-char (1- (match-beginning 1)))
  14771. (setq column (current-column)))
  14772. ((looking-at "\\([ \t]*\\):END:")
  14773. (goto-char (match-end 1))
  14774. (setq column (current-column)))
  14775. ((org-in-item-p)
  14776. (org-beginning-of-item)
  14777. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14778. (setq bpos (match-beginning 1) tpos (match-end 0)
  14779. bcol (progn (goto-char bpos) (current-column))
  14780. tcol (progn (goto-char tpos) (current-column))
  14781. bullet (match-string 1)
  14782. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14783. (if (> tcol (+ bcol org-description-max-indent))
  14784. (setq tcol (+ bcol 5)))
  14785. (if (not itemp)
  14786. (setq column tcol)
  14787. (goto-char pos)
  14788. (beginning-of-line 1)
  14789. (if (looking-at "\\S-")
  14790. (progn
  14791. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14792. (setq bullet (match-string 1)
  14793. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14794. (setq column (if (equal btype bullet-type) bcol tcol)))
  14795. (setq column (org-get-indentation)))))
  14796. (t (setq column (org-get-indentation))))))
  14797. (goto-char pos)
  14798. (if (<= (current-column) (current-indentation))
  14799. (org-indent-line-to column)
  14800. (save-excursion (org-indent-line-to column)))
  14801. (setq column (current-column))
  14802. (beginning-of-line 1)
  14803. (if (looking-at
  14804. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14805. (replace-match (concat "\\1" (format org-property-format
  14806. (match-string 2) (match-string 3)))
  14807. t nil))
  14808. (org-move-to-column column)))
  14809. (defun org-set-autofill-regexps ()
  14810. (interactive)
  14811. ;; In the paragraph separator we include headlines, because filling
  14812. ;; text in a line directly attached to a headline would otherwise
  14813. ;; fill the headline as well.
  14814. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14815. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14816. ;; The paragraph starter includes hand-formatted lists.
  14817. (org-set-local
  14818. 'paragraph-start
  14819. (concat
  14820. "\f" "\\|"
  14821. "[ ]*$" "\\|"
  14822. "\\*+ " "\\|"
  14823. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  14824. "[ \t]*[:|]" "\\|"
  14825. "\\$\\$" "\\|"
  14826. "\\\\\\(begin\\|end\\|[][]\\)"))
  14827. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14828. ;; But only if the user has not turned off tables or fixed-width regions
  14829. (org-set-local
  14830. 'auto-fill-inhibit-regexp
  14831. (concat "\\*+ \\|#\\+"
  14832. "\\|[ \t]*" org-keyword-time-regexp
  14833. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14834. (concat
  14835. "\\|[ \t]*["
  14836. (if org-enable-table-editor "|" "")
  14837. (if org-enable-fixed-width-editor ":" "")
  14838. "]"))))
  14839. ;; We use our own fill-paragraph function, to make sure that tables
  14840. ;; and fixed-width regions are not wrapped. That function will pass
  14841. ;; through to `fill-paragraph' when appropriate.
  14842. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14843. ; Adaptive filling: To get full control, first make sure that
  14844. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14845. (org-set-local 'adaptive-fill-regexp "\000")
  14846. (org-set-local 'adaptive-fill-function
  14847. 'org-adaptive-fill-function)
  14848. (org-set-local
  14849. 'align-mode-rules-list
  14850. '((org-in-buffer-settings
  14851. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14852. (modes . '(org-mode))))))
  14853. (defun org-fill-paragraph (&optional justify)
  14854. "Re-align a table, pass through to fill-paragraph if no table."
  14855. (let ((table-p (org-at-table-p))
  14856. (table.el-p (org-at-table.el-p)))
  14857. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14858. (save-excursion (goto-char (point-at-bol))
  14859. (looking-at outline-regexp)))
  14860. t) ; skip headlines
  14861. (table.el-p t) ; skip table.el tables
  14862. (table-p (org-table-align) t) ; align org-mode tables
  14863. (t nil)))) ; call paragraph-fill
  14864. ;; For reference, this is the default value of adaptive-fill-regexp
  14865. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14866. (defun org-adaptive-fill-function ()
  14867. "Return a fill prefix for org-mode files.
  14868. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14869. work correctly."
  14870. (cond ((looking-at "#[ \t]+")
  14871. (match-string 0))
  14872. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14873. (save-excursion
  14874. (if (> (match-end 1) (+ (match-beginning 1)
  14875. org-description-max-indent))
  14876. (goto-char (+ (match-beginning 1) 5))
  14877. (goto-char (match-end 0)))
  14878. (make-string (current-column) ?\ )))
  14879. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14880. (save-excursion
  14881. (goto-char (match-end 0))
  14882. (make-string (current-column) ?\ )))
  14883. (t nil)))
  14884. ;;; Other stuff.
  14885. (defun org-toggle-fixed-width-section (arg)
  14886. "Toggle the fixed-width export.
  14887. If there is no active region, the QUOTE keyword at the current headline is
  14888. inserted or removed. When present, it causes the text between this headline
  14889. and the next to be exported as fixed-width text, and unmodified.
  14890. If there is an active region, this command adds or removes a colon as the
  14891. first character of this line. If the first character of a line is a colon,
  14892. this line is also exported in fixed-width font."
  14893. (interactive "P")
  14894. (let* ((cc 0)
  14895. (regionp (org-region-active-p))
  14896. (beg (if regionp (region-beginning) (point)))
  14897. (end (if regionp (region-end)))
  14898. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14899. (case-fold-search nil)
  14900. (re "[ \t]*\\(: \\)")
  14901. off)
  14902. (if regionp
  14903. (save-excursion
  14904. (goto-char beg)
  14905. (setq cc (current-column))
  14906. (beginning-of-line 1)
  14907. (setq off (looking-at re))
  14908. (while (> nlines 0)
  14909. (setq nlines (1- nlines))
  14910. (beginning-of-line 1)
  14911. (cond
  14912. (arg
  14913. (org-move-to-column cc t)
  14914. (insert ": \n")
  14915. (forward-line -1))
  14916. ((and off (looking-at re))
  14917. (replace-match "" t t nil 1))
  14918. ((not off) (org-move-to-column cc t) (insert ": ")))
  14919. (forward-line 1)))
  14920. (save-excursion
  14921. (org-back-to-heading)
  14922. (if (looking-at (concat outline-regexp
  14923. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14924. (replace-match "" t t nil 1)
  14925. (if (looking-at outline-regexp)
  14926. (progn
  14927. (goto-char (match-end 0))
  14928. (insert org-quote-string " "))))))))
  14929. (defun org-reftex-citation ()
  14930. "Use reftex-citation to insert a citation into the buffer.
  14931. This looks for a line like
  14932. #+BIBLIOGRAPHY: foo plain option:-d
  14933. and derives from it that foo.bib is the bbliography file relevant
  14934. for this document. It then installs the necessary environment for RefTeX
  14935. to work in this buffer and calls `reftex-citation' to insert a citation
  14936. into the buffer.
  14937. Export of such citations to both LaTeX and HTML is handled by the contributed
  14938. package org-exp-bibtex by Taru Karttunen."
  14939. (interactive)
  14940. (let ((reftex-docstruct-symbol 'rds)
  14941. (reftex-cite-format "\\cite{%l}")
  14942. rds bib)
  14943. (save-excursion
  14944. (save-restriction
  14945. (widen)
  14946. (let ((case-fold-search t)
  14947. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14948. (if (not (save-excursion
  14949. (or (re-search-forward re nil t)
  14950. (re-search-backward re nil t))))
  14951. (error "No bibliography defined in file")
  14952. (setq bib (concat (match-string 1) ".bib")
  14953. rds (list (list 'bib bib)))))))
  14954. (call-interactively 'reftex-citation)))
  14955. ;;;; Functions extending outline functionality
  14956. (defun org-beginning-of-line (&optional arg)
  14957. "Go to the beginning of the current line. If that is invisible, continue
  14958. to a visible line beginning. This makes the function of C-a more intuitive.
  14959. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14960. first attempt, and only move to after the tags when the cursor is already
  14961. beyond the end of the headline."
  14962. (interactive "P")
  14963. (let ((pos (point))
  14964. (special (if (consp org-special-ctrl-a/e)
  14965. (car org-special-ctrl-a/e)
  14966. org-special-ctrl-a/e))
  14967. refpos)
  14968. (if (org-bound-and-true-p line-move-visual)
  14969. (beginning-of-visual-line 1)
  14970. (beginning-of-line 1))
  14971. (if (and arg (fboundp 'move-beginning-of-line))
  14972. (call-interactively 'move-beginning-of-line)
  14973. (if (bobp)
  14974. nil
  14975. (backward-char 1)
  14976. (if (org-invisible-p)
  14977. (while (and (not (bobp)) (org-invisible-p))
  14978. (backward-char 1)
  14979. (beginning-of-line 1))
  14980. (forward-char 1))))
  14981. (when special
  14982. (cond
  14983. ((and (looking-at org-complex-heading-regexp)
  14984. (= (char-after (match-end 1)) ?\ ))
  14985. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14986. (point-at-eol)))
  14987. (goto-char
  14988. (if (eq special t)
  14989. (cond ((> pos refpos) refpos)
  14990. ((= pos (point)) refpos)
  14991. (t (point)))
  14992. (cond ((> pos (point)) (point))
  14993. ((not (eq last-command this-command)) (point))
  14994. (t refpos)))))
  14995. ((org-at-item-p)
  14996. (goto-char
  14997. (if (eq special t)
  14998. (cond ((> pos (match-end 4)) (match-end 4))
  14999. ((= pos (point)) (match-end 4))
  15000. (t (point)))
  15001. (cond ((> pos (point)) (point))
  15002. ((not (eq last-command this-command)) (point))
  15003. (t (match-end 4))))))))
  15004. (org-no-warnings
  15005. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15006. (defun org-end-of-line (&optional arg)
  15007. "Go to the end of the line.
  15008. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15009. first attempt, and only move to after the tags when the cursor is already
  15010. beyond the end of the headline."
  15011. (interactive "P")
  15012. (let ((special (if (consp org-special-ctrl-a/e)
  15013. (cdr org-special-ctrl-a/e)
  15014. org-special-ctrl-a/e)))
  15015. (if (or (not special)
  15016. (not (org-on-heading-p))
  15017. arg)
  15018. (call-interactively
  15019. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15020. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15021. (t 'end-of-line)))
  15022. (let ((pos (point)))
  15023. (beginning-of-line 1)
  15024. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  15025. (if (eq special t)
  15026. (if (or (< pos (match-beginning 1))
  15027. (= pos (match-end 0)))
  15028. (goto-char (match-beginning 1))
  15029. (goto-char (match-end 0)))
  15030. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15031. (goto-char (match-end 0))
  15032. (goto-char (match-beginning 1))))
  15033. (call-interactively (if (fboundp 'move-end-of-line)
  15034. 'move-end-of-line
  15035. 'end-of-line)))))
  15036. (org-no-warnings
  15037. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15038. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15039. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15040. (defun org-backward-sentence (&optional arg)
  15041. "Go to beginning of sentence, or beginning of table field.
  15042. This will call `backward-sentence' or `org-table-beginning-of-field',
  15043. depending on context."
  15044. (interactive "P")
  15045. (cond
  15046. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15047. (t (call-interactively 'backward-sentence))))
  15048. (defun org-forward-sentence (&optional arg)
  15049. "Go to end of sentence, or end of table field.
  15050. This will call `forward-sentence' or `org-table-end-of-field',
  15051. depending on context."
  15052. (interactive "P")
  15053. (cond
  15054. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15055. (t (call-interactively 'forward-sentence))))
  15056. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15057. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15058. (defun org-kill-line (&optional arg)
  15059. "Kill line, to tags or end of line."
  15060. (interactive "P")
  15061. (cond
  15062. ((or (not org-special-ctrl-k)
  15063. (bolp)
  15064. (not (org-on-heading-p)))
  15065. (call-interactively 'kill-line))
  15066. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15067. (kill-region (point) (match-beginning 1))
  15068. (org-set-tags nil t))
  15069. (t (kill-region (point) (point-at-eol)))))
  15070. (define-key org-mode-map "\C-k" 'org-kill-line)
  15071. (defun org-yank (&optional arg)
  15072. "Yank. If the kill is a subtree, treat it specially.
  15073. This command will look at the current kill and check if is a single
  15074. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15075. cursor is at the beginning of a line or after the stars of a currently
  15076. empty headline, then the yank is handled specially. How exactly depends
  15077. on the value of the following variables, both set by default.
  15078. org-yank-folded-subtrees
  15079. When set, the subtree(s) will be folded after insertion, but only
  15080. if doing so would now swallow text after the yanked text.
  15081. org-yank-adjusted-subtrees
  15082. When set, the subtree will be promoted or demoted in order to
  15083. fit into the local outline tree structure, which means that the level
  15084. will be adjusted so that it becomes the smaller one of the two
  15085. *visible* surrounding headings.
  15086. Any prefix to this command will cause `yank' to be called directly with
  15087. no special treatment. In particular, a simple `C-u' prefix will just
  15088. plainly yank the text as it is.
  15089. \[1] The test checks if the first non-white line is a heading
  15090. and if there are no other headings with fewer stars."
  15091. (interactive "P")
  15092. (org-yank-generic 'yank arg))
  15093. (defun org-yank-generic (command arg)
  15094. "Perform some yank-like command.
  15095. This function implements the behavior described in the `org-yank'
  15096. documentation. However, it has been generalized to work for any
  15097. interactive command with similar behavior."
  15098. ;; pretend to be command COMMAND
  15099. (setq this-command command)
  15100. (if arg
  15101. (call-interactively command)
  15102. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15103. (and (org-kill-is-subtree-p)
  15104. (or (bolp)
  15105. (and (looking-at "[ \t]*$")
  15106. (string-match
  15107. "\\`\\*+\\'"
  15108. (buffer-substring (point-at-bol) (point)))))))
  15109. swallowp)
  15110. (cond
  15111. ((and subtreep org-yank-folded-subtrees)
  15112. (let ((beg (point))
  15113. end)
  15114. (if (and subtreep org-yank-adjusted-subtrees)
  15115. (org-paste-subtree nil nil 'for-yank)
  15116. (call-interactively command))
  15117. (setq end (point))
  15118. (goto-char beg)
  15119. (when (and (bolp) subtreep
  15120. (not (setq swallowp
  15121. (org-yank-folding-would-swallow-text beg end))))
  15122. (or (looking-at outline-regexp)
  15123. (re-search-forward (concat "^" outline-regexp) end t))
  15124. (while (and (< (point) end) (looking-at outline-regexp))
  15125. (hide-subtree)
  15126. (org-cycle-show-empty-lines 'folded)
  15127. (condition-case nil
  15128. (outline-forward-same-level 1)
  15129. (error (goto-char end)))))
  15130. (when swallowp
  15131. (message
  15132. "Inserted text not folded because that would swallow text"))
  15133. (goto-char end)
  15134. (skip-chars-forward " \t\n\r")
  15135. (beginning-of-line 1)
  15136. (push-mark beg 'nomsg)))
  15137. ((and subtreep org-yank-adjusted-subtrees)
  15138. (let ((beg (point-at-bol)))
  15139. (org-paste-subtree nil nil 'for-yank)
  15140. (push-mark beg 'nomsg)))
  15141. (t
  15142. (call-interactively command))))))
  15143. (defun org-yank-folding-would-swallow-text (beg end)
  15144. "Would hide-subtree at BEG swallow any text after END?"
  15145. (let (level)
  15146. (save-excursion
  15147. (goto-char beg)
  15148. (when (or (looking-at outline-regexp)
  15149. (re-search-forward (concat "^" outline-regexp) end t))
  15150. (setq level (org-outline-level)))
  15151. (goto-char end)
  15152. (skip-chars-forward " \t\r\n\v\f")
  15153. (if (or (eobp)
  15154. (and (bolp) (looking-at org-outline-regexp)
  15155. (<= (org-outline-level) level)))
  15156. nil ; Nothing would be swallowed
  15157. t)))) ; something would swallow
  15158. (define-key org-mode-map "\C-y" 'org-yank)
  15159. (defun org-invisible-p ()
  15160. "Check if point is at a character currently not visible."
  15161. ;; Early versions of noutline don't have `outline-invisible-p'.
  15162. (if (fboundp 'outline-invisible-p)
  15163. (outline-invisible-p)
  15164. (get-char-property (point) 'invisible)))
  15165. (defun org-invisible-p2 ()
  15166. "Check if point is at a character currently not visible."
  15167. (save-excursion
  15168. (if (and (eolp) (not (bobp))) (backward-char 1))
  15169. ;; Early versions of noutline don't have `outline-invisible-p'.
  15170. (if (fboundp 'outline-invisible-p)
  15171. (outline-invisible-p)
  15172. (get-char-property (point) 'invisible))))
  15173. (defun org-back-to-heading (&optional invisible-ok)
  15174. "Call `outline-back-to-heading', but provide a better error message."
  15175. (condition-case nil
  15176. (outline-back-to-heading invisible-ok)
  15177. (error (error "Before first headline at position %d in buffer %s"
  15178. (point) (current-buffer)))))
  15179. (defun org-before-first-heading-p ()
  15180. "Before first heading?"
  15181. (save-excursion
  15182. (null (re-search-backward "^\\*+ " nil t))))
  15183. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15184. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15185. (defun org-at-heading-or-item-p ()
  15186. (or (org-on-heading-p) (org-at-item-p)))
  15187. (defun org-on-target-p ()
  15188. (or (org-in-regexp org-radio-target-regexp)
  15189. (org-in-regexp org-target-regexp)))
  15190. (defun org-up-heading-all (arg)
  15191. "Move to the heading line of which the present line is a subheading.
  15192. This function considers both visible and invisible heading lines.
  15193. With argument, move up ARG levels."
  15194. (if (fboundp 'outline-up-heading-all)
  15195. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15196. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15197. (defun org-up-heading-safe ()
  15198. "Move to the heading line of which the present line is a subheading.
  15199. This version will not throw an error. It will return the level of the
  15200. headline found, or nil if no higher level is found.
  15201. Also, this function will be a lot faster than `outline-up-heading',
  15202. because it relies on stars being the outline starters. This can really
  15203. make a significant difference in outlines with very many siblings."
  15204. (let (start-level re)
  15205. (org-back-to-heading t)
  15206. (setq start-level (funcall outline-level))
  15207. (if (equal start-level 1)
  15208. nil
  15209. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15210. (if (re-search-backward re nil t)
  15211. (funcall outline-level)))))
  15212. (defun org-first-sibling-p ()
  15213. "Is this heading the first child of its parents?"
  15214. (interactive)
  15215. (let ((re (concat "^" outline-regexp))
  15216. level l)
  15217. (unless (org-at-heading-p t)
  15218. (error "Not at a heading"))
  15219. (setq level (funcall outline-level))
  15220. (save-excursion
  15221. (if (not (re-search-backward re nil t))
  15222. t
  15223. (setq l (funcall outline-level))
  15224. (< l level)))))
  15225. (defun org-goto-sibling (&optional previous)
  15226. "Goto the next sibling, even if it is invisible.
  15227. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15228. when a sibling was found. When none is found, return nil and don't
  15229. move point."
  15230. (let ((fun (if previous 're-search-backward 're-search-forward))
  15231. (pos (point))
  15232. (re (concat "^" outline-regexp))
  15233. level l)
  15234. (when (condition-case nil (org-back-to-heading t) (error nil))
  15235. (setq level (funcall outline-level))
  15236. (catch 'exit
  15237. (or previous (forward-char 1))
  15238. (while (funcall fun re nil t)
  15239. (setq l (funcall outline-level))
  15240. (when (< l level) (goto-char pos) (throw 'exit nil))
  15241. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15242. (goto-char pos)
  15243. nil))))
  15244. (defun org-show-siblings ()
  15245. "Show all siblings of the current headline."
  15246. (save-excursion
  15247. (while (org-goto-sibling) (org-flag-heading nil)))
  15248. (save-excursion
  15249. (while (org-goto-sibling 'previous)
  15250. (org-flag-heading nil))))
  15251. (defun org-show-hidden-entry ()
  15252. "Show an entry where even the heading is hidden."
  15253. (save-excursion
  15254. (org-show-entry)))
  15255. (defun org-flag-heading (flag &optional entry)
  15256. "Flag the current heading. FLAG non-nil means make invisible.
  15257. When ENTRY is non-nil, show the entire entry."
  15258. (save-excursion
  15259. (org-back-to-heading t)
  15260. ;; Check if we should show the entire entry
  15261. (if entry
  15262. (progn
  15263. (org-show-entry)
  15264. (save-excursion
  15265. (and (outline-next-heading)
  15266. (org-flag-heading nil))))
  15267. (outline-flag-region (max (point-min) (1- (point)))
  15268. (save-excursion (outline-end-of-heading) (point))
  15269. flag))))
  15270. (defun org-get-next-sibling ()
  15271. "Move to next heading of the same level, and return point.
  15272. If there is no such heading, return nil.
  15273. This is like outline-next-sibling, but invisible headings are ok."
  15274. (let ((level (funcall outline-level)))
  15275. (outline-next-heading)
  15276. (while (and (not (eobp)) (> (funcall outline-level) level))
  15277. (outline-next-heading))
  15278. (if (or (eobp) (< (funcall outline-level) level))
  15279. nil
  15280. (point))))
  15281. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15282. ;; This contains an exact copy of the original function, but it uses
  15283. ;; `org-back-to-heading', to make it work also in invisible
  15284. ;; trees. And is uses an invisible-OK argument.
  15285. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15286. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15287. ;; with many children and grandchildren etc, this can be much faster
  15288. ;; than the outline version.
  15289. (org-back-to-heading invisible-OK)
  15290. (let ((first t)
  15291. (level (funcall outline-level)))
  15292. (if (and (org-mode-p) (< level 1000))
  15293. ;; A true heading (not a plain list item), in Org-mode
  15294. ;; This means we can easily find the end by looking
  15295. ;; only for the right number of stars. Using a regexp to do
  15296. ;; this is so much faster than using a Lisp loop.
  15297. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15298. (forward-char 1)
  15299. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15300. ;; something else, do it the slow way
  15301. (while (and (not (eobp))
  15302. (or first (> (funcall outline-level) level)))
  15303. (setq first nil)
  15304. (outline-next-heading)))
  15305. (unless to-heading
  15306. (if (memq (preceding-char) '(?\n ?\^M))
  15307. (progn
  15308. ;; Go to end of line before heading
  15309. (forward-char -1)
  15310. (if (memq (preceding-char) '(?\n ?\^M))
  15311. ;; leave blank line before heading
  15312. (forward-char -1))))))
  15313. (point))
  15314. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15315. "Use Org version in org-mode, for dramatic speed-up."
  15316. (if (eq major-mode 'org-mode)
  15317. (progn
  15318. (org-end-of-subtree nil t)
  15319. (backward-char 1))
  15320. ad-do-it))
  15321. (defun org-forward-same-level (arg &optional invisible-ok)
  15322. "Move forward to the arg'th subheading at same level as this one.
  15323. Stop at the first and last subheadings of a superior heading."
  15324. (interactive "p")
  15325. (org-back-to-heading invisible-ok)
  15326. (org-on-heading-p)
  15327. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15328. (re (format "^\\*\\{1,%d\\} " level))
  15329. l)
  15330. (forward-char 1)
  15331. (while (> arg 0)
  15332. (while (and (re-search-forward re nil 'move)
  15333. (setq l (- (match-end 0) (match-beginning 0) 1))
  15334. (= l level)
  15335. (not invisible-ok)
  15336. (org-invisible-p))
  15337. (if (< l level) (setq arg 1)))
  15338. (setq arg (1- arg)))
  15339. (beginning-of-line 1)))
  15340. (defun org-backward-same-level (arg &optional invisible-ok)
  15341. "Move backward to the arg'th subheading at same level as this one.
  15342. Stop at the first and last subheadings of a superior heading."
  15343. (interactive "p")
  15344. (org-back-to-heading)
  15345. (org-on-heading-p)
  15346. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15347. (re (format "^\\*\\{1,%d\\} " level))
  15348. l)
  15349. (while (> arg 0)
  15350. (while (and (re-search-backward re nil 'move)
  15351. (setq l (- (match-end 0) (match-beginning 0) 1))
  15352. (= l level)
  15353. (not invisible-ok)
  15354. (org-invisible-p))
  15355. (if (< l level) (setq arg 1)))
  15356. (setq arg (1- arg)))))
  15357. (defun org-show-subtree ()
  15358. "Show everything after this heading at deeper levels."
  15359. (outline-flag-region
  15360. (point)
  15361. (save-excursion
  15362. (outline-end-of-subtree) (outline-next-heading) (point))
  15363. nil))
  15364. (defun org-show-entry ()
  15365. "Show the body directly following this heading.
  15366. Show the heading too, if it is currently invisible."
  15367. (interactive)
  15368. (save-excursion
  15369. (condition-case nil
  15370. (progn
  15371. (org-back-to-heading t)
  15372. (outline-flag-region
  15373. (max (point-min) (1- (point)))
  15374. (save-excursion
  15375. (if (re-search-forward
  15376. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15377. (match-beginning 1)
  15378. (point-max)))
  15379. nil)
  15380. (org-cycle-hide-drawers 'children))
  15381. (error nil))))
  15382. (defun org-make-options-regexp (kwds &optional extra)
  15383. "Make a regular expression for keyword lines."
  15384. (concat
  15385. "^"
  15386. "#?[ \t]*\\+\\("
  15387. (mapconcat 'regexp-quote kwds "\\|")
  15388. (if extra (concat "\\|" extra))
  15389. "\\):[ \t]*"
  15390. "\\(.*\\)"))
  15391. ;; Make isearch reveal the necessary context
  15392. (defun org-isearch-end ()
  15393. "Reveal context after isearch exits."
  15394. (when isearch-success ; only if search was successful
  15395. (if (featurep 'xemacs)
  15396. ;; Under XEmacs, the hook is run in the correct place,
  15397. ;; we directly show the context.
  15398. (org-show-context 'isearch)
  15399. ;; In Emacs the hook runs *before* restoring the overlays.
  15400. ;; So we have to use a one-time post-command-hook to do this.
  15401. ;; (Emacs 22 has a special variable, see function `org-mode')
  15402. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15403. isearch-mode-end-hook-quit)
  15404. ;; Only when the isearch was not quitted.
  15405. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15406. 'append 'local)))))
  15407. (defun org-isearch-post-command ()
  15408. "Remove self from hook, and show context."
  15409. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15410. (org-show-context 'isearch))
  15411. ;;;; Integration with and fixes for other packages
  15412. ;;; Imenu support
  15413. (defvar org-imenu-markers nil
  15414. "All markers currently used by Imenu.")
  15415. (make-variable-buffer-local 'org-imenu-markers)
  15416. (defun org-imenu-new-marker (&optional pos)
  15417. "Return a new marker for use by Imenu, and remember the marker."
  15418. (let ((m (make-marker)))
  15419. (move-marker m (or pos (point)))
  15420. (push m org-imenu-markers)
  15421. m))
  15422. (defun org-imenu-get-tree ()
  15423. "Produce the index for Imenu."
  15424. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15425. (setq org-imenu-markers nil)
  15426. (let* ((n org-imenu-depth)
  15427. (re (concat "^" outline-regexp))
  15428. (subs (make-vector (1+ n) nil))
  15429. (last-level 0)
  15430. m level head)
  15431. (save-excursion
  15432. (save-restriction
  15433. (widen)
  15434. (goto-char (point-max))
  15435. (while (re-search-backward re nil t)
  15436. (setq level (org-reduced-level (funcall outline-level)))
  15437. (when (<= level n)
  15438. (looking-at org-complex-heading-regexp)
  15439. (setq head (org-link-display-format
  15440. (org-match-string-no-properties 4))
  15441. m (org-imenu-new-marker))
  15442. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15443. (if (>= level last-level)
  15444. (push (cons head m) (aref subs level))
  15445. (push (cons head (aref subs (1+ level))) (aref subs level))
  15446. (loop for i from (1+ level) to n do (aset subs i nil)))
  15447. (setq last-level level)))))
  15448. (aref subs 1)))
  15449. (eval-after-load "imenu"
  15450. '(progn
  15451. (add-hook 'imenu-after-jump-hook
  15452. (lambda ()
  15453. (if (eq major-mode 'org-mode)
  15454. (org-show-context 'org-goto))))))
  15455. (defun org-link-display-format (link)
  15456. "Replace a link with either the description, or the link target
  15457. if no description is present"
  15458. (save-match-data
  15459. (if (string-match org-bracket-link-analytic-regexp link)
  15460. (replace-match (or (match-string 5 link)
  15461. (concat (match-string 1 link)
  15462. (match-string 3 link)))
  15463. nil nil link)
  15464. link)))
  15465. ;; Speedbar support
  15466. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15467. "Overlay marking the agenda restriction line in speedbar.")
  15468. (org-overlay-put org-speedbar-restriction-lock-overlay
  15469. 'face 'org-agenda-restriction-lock)
  15470. (org-overlay-put org-speedbar-restriction-lock-overlay
  15471. 'help-echo "Agendas are currently limited to this item.")
  15472. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15473. (defun org-speedbar-set-agenda-restriction ()
  15474. "Restrict future agenda commands to the location at point in speedbar.
  15475. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15476. (interactive)
  15477. (require 'org-agenda)
  15478. (let (p m tp np dir txt)
  15479. (cond
  15480. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15481. 'org-imenu t))
  15482. (setq m (get-text-property p 'org-imenu-marker))
  15483. (save-excursion
  15484. (save-restriction
  15485. (set-buffer (marker-buffer m))
  15486. (goto-char m)
  15487. (org-agenda-set-restriction-lock 'subtree))))
  15488. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15489. 'speedbar-function 'speedbar-find-file))
  15490. (setq tp (previous-single-property-change
  15491. (1+ p) 'speedbar-function)
  15492. np (next-single-property-change
  15493. tp 'speedbar-function)
  15494. dir (speedbar-line-directory)
  15495. txt (buffer-substring-no-properties (or tp (point-min))
  15496. (or np (point-max))))
  15497. (save-excursion
  15498. (save-restriction
  15499. (set-buffer (find-file-noselect
  15500. (let ((default-directory dir))
  15501. (expand-file-name txt))))
  15502. (unless (org-mode-p)
  15503. (error "Cannot restrict to non-Org-mode file"))
  15504. (org-agenda-set-restriction-lock 'file))))
  15505. (t (error "Don't know how to restrict Org-mode's agenda")))
  15506. (org-move-overlay org-speedbar-restriction-lock-overlay
  15507. (point-at-bol) (point-at-eol))
  15508. (setq current-prefix-arg nil)
  15509. (org-agenda-maybe-redo)))
  15510. (eval-after-load "speedbar"
  15511. '(progn
  15512. (speedbar-add-supported-extension ".org")
  15513. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15514. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15515. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15516. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15517. (add-hook 'speedbar-visiting-tag-hook
  15518. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15519. ;;; Fixes and Hacks for problems with other packages
  15520. ;; Make flyspell not check words in links, to not mess up our keymap
  15521. (defun org-mode-flyspell-verify ()
  15522. "Don't let flyspell put overlays at active buttons."
  15523. (and (not (get-text-property (point) 'keymap))
  15524. (not (get-text-property (point) 'org-no-flyspell))))
  15525. (defun org-remove-flyspell-overlays-in (beg end)
  15526. "Remove flyspell overlays in region."
  15527. (and (org-bound-and-true-p flyspell-mode)
  15528. (fboundp 'flyspell-delete-region-overlays)
  15529. (flyspell-delete-region-overlays beg end))
  15530. (add-text-properties beg end '(org-no-flyspell t)))
  15531. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15532. (eval-after-load "bookmark"
  15533. '(if (boundp 'bookmark-after-jump-hook)
  15534. ;; We can use the hook
  15535. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15536. ;; Hook not available, use advice
  15537. (defadvice bookmark-jump (after org-make-visible activate)
  15538. "Make the position visible."
  15539. (org-bookmark-jump-unhide))))
  15540. ;; Make sure saveplace show the location if it was hidden
  15541. (eval-after-load "saveplace"
  15542. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15543. "Make the position visible."
  15544. (org-bookmark-jump-unhide)))
  15545. (defun org-bookmark-jump-unhide ()
  15546. "Unhide the current position, to show the bookmark location."
  15547. (and (org-mode-p)
  15548. (or (org-invisible-p)
  15549. (save-excursion (goto-char (max (point-min) (1- (point))))
  15550. (org-invisible-p)))
  15551. (org-show-context 'bookmark-jump)))
  15552. ;; Make session.el ignore our circular variable
  15553. (eval-after-load "session"
  15554. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15555. ;;;; Experimental code
  15556. (defun org-closed-in-range ()
  15557. "Sparse tree of items closed in a certain time range.
  15558. Still experimental, may disappear in the future."
  15559. (interactive)
  15560. ;; Get the time interval from the user.
  15561. (let* ((time1 (time-to-seconds
  15562. (org-read-date nil 'to-time nil "Starting date: ")))
  15563. (time2 (time-to-seconds
  15564. (org-read-date nil 'to-time nil "End date:")))
  15565. ;; callback function
  15566. (callback (lambda ()
  15567. (let ((time
  15568. (time-to-seconds
  15569. (apply 'encode-time
  15570. (org-parse-time-string
  15571. (match-string 1))))))
  15572. ;; check if time in interval
  15573. (and (>= time time1) (<= time time2))))))
  15574. ;; make tree, check each match with the callback
  15575. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15576. ;;;; Finish up
  15577. (provide 'org)
  15578. (run-hooks 'org-load-hook)
  15579. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15580. ;;; org.el ends here