org.el 676 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128
  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.34trans
  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.34trans"
  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* ((origin default-directory)
  96. (version org-version)
  97. (git-version)
  98. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  99. (when (and (file-exists-p (expand-file-name ".git" dir))
  100. (executable-find "git"))
  101. (unwind-protect
  102. (progn
  103. (cd dir)
  104. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  105. (with-current-buffer "*Shell Command Output*"
  106. (goto-char (point-min))
  107. (setq git-version (buffer-substring (point) (point-at-eol))))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (when (string-match "\\S-"
  110. (shell-command-to-string
  111. "git diff-index --name-only HEAD --"))
  112. (setq git-version (concat git-version ".dirty")))
  113. (setq version (concat version " (" git-version ")"))))
  114. (cd origin)))
  115. (setq version (format "Org-mode version %s" version))
  116. (if here (insert version))
  117. (message version)))
  118. ;;; Compatibility constants
  119. ;;; The custom variables
  120. (defgroup org nil
  121. "Outline-based notes management and organizer."
  122. :tag "Org"
  123. :group 'outlines
  124. :group 'hypermedia
  125. :group 'calendar)
  126. (defcustom org-mode-hook nil
  127. "Mode hook for Org-mode, run after the mode was turned on."
  128. :group 'org
  129. :type 'hook)
  130. (defcustom org-load-hook nil
  131. "Hook that is run after org.el has been loaded."
  132. :group 'org
  133. :type 'hook)
  134. (defvar org-modules) ; defined below
  135. (defvar org-modules-loaded nil
  136. "Have the modules been loaded already?")
  137. (defun org-load-modules-maybe (&optional force)
  138. "Load all extensions listed in `org-modules'."
  139. (when (or force (not org-modules-loaded))
  140. (mapc (lambda (ext)
  141. (condition-case nil (require ext)
  142. (error (message "Problems while trying to load feature `%s'" ext))))
  143. org-modules)
  144. (setq org-modules-loaded t)))
  145. (defun org-set-modules (var value)
  146. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  147. (set var value)
  148. (when (featurep 'org)
  149. (org-load-modules-maybe 'force)))
  150. (when (org-bound-and-true-p org-modules)
  151. (let ((a (member 'org-infojs org-modules)))
  152. (and a (setcar a 'org-jsinfo))))
  153. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  154. "Modules that should always be loaded together with org.el.
  155. If a description starts with <C>, the file is not part of Emacs
  156. and loading it will require that you have downloaded and properly installed
  157. the org-mode distribution.
  158. You can also use this system to load external packages (i.e. neither Org
  159. core modules, nor modules from the CONTRIB directory). Just add symbols
  160. to the end of the list. If the package is called org-xyz.el, then you need
  161. to add the symbol `xyz', and the package must have a call to
  162. (provide 'org-xyz)"
  163. :group 'org
  164. :set 'org-set-modules
  165. :type
  166. '(set :greedy t
  167. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  168. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  169. (const :tag " crypt: Encryption of subtrees" org-crypt)
  170. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  171. (const :tag " docview: Links to doc-view buffers" org-docview)
  172. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  173. (const :tag " id: Global IDs for identifying entries" org-id)
  174. (const :tag " info: Links to Info nodes" org-info)
  175. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  176. (const :tag " habit: Track your consistency with habits" org-habit)
  177. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  178. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  179. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  180. (const :tag " mew Links to Mew folders/messages" org-mew)
  181. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  182. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  183. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  184. (const :tag " vm: Links to VM folders/messages" org-vm)
  185. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  186. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  187. (const :tag " mouse: Additional mouse support" org-mouse)
  188. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  189. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  190. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  191. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  192. (const :tag "C collector: Collect properties into tables" org-collector)
  193. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  194. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  195. (const :tag "C eval: Include command output as text" org-eval)
  196. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  197. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  198. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  199. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  200. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  201. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  202. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  203. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  204. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  205. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  206. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  207. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  208. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  209. (const :tag "C R: Computation using the R language" org-R)
  210. (const :tag "C registry: A registry for Org-mode links" org-registry)
  211. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  212. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  213. (const :tag "C secretary: Team management with org-mode" org-secretary)
  214. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  215. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  216. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  217. (const :tag "C track: Keep up with Org-mode development" org-track)
  218. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  219. (defcustom org-support-shift-select nil
  220. "Non-nil means, make shift-cursor commands select text when possible.
  221. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  222. selecting a region, or enlarge thusly regions started in this way.
  223. In Org-mode, in special contexts, these same keys are used for other
  224. purposes, important enough to compete with shift selection. Org tries
  225. to balance these needs by supporting `shift-select-mode' outside these
  226. special contexts, under control of this variable.
  227. The default of this variable is nil, to avoid confusing behavior. Shifted
  228. cursor keys will then execute Org commands in the following contexts:
  229. - on a headline, changing TODO state (left/right) and priority (up/down)
  230. - on a time stamp, changing the time
  231. - in a plain list item, changing the bullet type
  232. - in a property definition line, switching between allowed values
  233. - in the BEGIN line of a clock table (changing the time block).
  234. Outside these contexts, the commands will throw an error.
  235. When this variable is t and the cursor is not in a special context,
  236. Org-mode will support shift-selection for making and enlarging regions.
  237. To make this more effective, the bullet cycling will no longer happen
  238. anywhere in an item line, but only if the cursor is exactly on the bullet.
  239. If you set this variable to the symbol `always', then the keys
  240. will not be special in headlines, property lines, and item lines, to make
  241. shift selection work there as well. If this is what you want, you can
  242. use the following alternative commands: `C-c C-t' and `C-c ,' to
  243. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  244. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  245. edited by hand or in column view.
  246. However, when the cursor is on a timestamp, shift-cursor commands
  247. will still edit the time stamp - this is just too good to give up.
  248. XEmacs user should have this variable set to nil, because shift-select-mode
  249. is Emacs 23 only."
  250. :group 'org
  251. :type '(choice
  252. (const :tag "Never" nil)
  253. (const :tag "When outside special context" t)
  254. (const :tag "Everywhere except timestamps" always)))
  255. (defgroup org-startup nil
  256. "Options concerning startup of Org-mode."
  257. :tag "Org Startup"
  258. :group 'org)
  259. (defcustom org-startup-folded t
  260. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  261. This can also be configured on a per-file basis by adding one of
  262. the following lines anywhere in the buffer:
  263. #+STARTUP: fold (or `overview', this is equivalent)
  264. #+STARTUP: nofold (or `showall', this is equivalent)
  265. #+STARTUP: content
  266. #+STARTUP: showeverything"
  267. :group 'org-startup
  268. :type '(choice
  269. (const :tag "nofold: show all" nil)
  270. (const :tag "fold: overview" t)
  271. (const :tag "content: all headlines" content)
  272. (const :tag "show everything, even drawers" showeverything)))
  273. (defcustom org-startup-truncated t
  274. "Non-nil means, entering Org-mode will set `truncate-lines'.
  275. This is useful since some lines containing links can be very long and
  276. uninteresting. Also tables look terrible when wrapped."
  277. :group 'org-startup
  278. :type 'boolean)
  279. (defcustom org-startup-indented nil
  280. "Non-nil means, turn on `org-indent-mode' on startup.
  281. This can also be configured on a per-file basis by adding one of
  282. the following lines anywhere in the buffer:
  283. #+STARTUP: indent
  284. #+STARTUP: noindent"
  285. :group 'org-structure
  286. :type '(choice
  287. (const :tag "Not" nil)
  288. (const :tag "Globally (slow on startup in large files)" t)))
  289. (defcustom org-startup-with-beamer-mode nil
  290. "Non-nil means, turn on `org-beamer-mode' on startup.
  291. This can also be configured on a per-file basis by adding one of
  292. the following lines anywhere in the buffer:
  293. #+STARTUP: beamer"
  294. :group 'org-startup
  295. :type 'boolean)
  296. (defcustom org-startup-align-all-tables nil
  297. "Non-nil means, align all tables when visiting a file.
  298. This is useful when the column width in tables is forced with <N> cookies
  299. in table fields. Such tables will look correct only after the first re-align.
  300. This can also be configured on a per-file basis by adding one of
  301. the following lines anywhere in the buffer:
  302. #+STARTUP: align
  303. #+STARTUP: noalign"
  304. :group 'org-startup
  305. :type 'boolean)
  306. (defcustom org-insert-mode-line-in-empty-file nil
  307. "Non-nil means insert the first line setting Org-mode in empty files.
  308. When the function `org-mode' is called interactively in an empty file, this
  309. normally means that the file name does not automatically trigger Org-mode.
  310. To ensure that the file will always be in Org-mode in the future, a
  311. line enforcing Org-mode will be inserted into the buffer, if this option
  312. has been set."
  313. :group 'org-startup
  314. :type 'boolean)
  315. (defcustom org-replace-disputed-keys nil
  316. "Non-nil means use alternative key bindings for some keys.
  317. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  318. These keys are also used by other packages like shift-selection-mode'
  319. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  320. If you want to use Org-mode together with one of these other modes,
  321. or more generally if you would like to move some Org-mode commands to
  322. other keys, set this variable and configure the keys with the variable
  323. `org-disputed-keys'.
  324. This option is only relevant at load-time of Org-mode, and must be set
  325. *before* org.el is loaded. Changing it requires a restart of Emacs to
  326. become effective."
  327. :group 'org-startup
  328. :type 'boolean)
  329. (defcustom org-use-extra-keys nil
  330. "Non-nil means use extra key sequence definitions for certain
  331. commands. This happens automatically if you run XEmacs or if
  332. window-system is nil. This variable lets you do the same
  333. manually. You must set it before loading org.
  334. Example: on Carbon Emacs 22 running graphically, with an external
  335. keyboard on a Powerbook, the default way of setting M-left might
  336. not work for either Alt or ESC. Setting this variable will make
  337. it work for ESC."
  338. :group 'org-startup
  339. :type 'boolean)
  340. (if (fboundp 'defvaralias)
  341. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  342. (defcustom org-disputed-keys
  343. '(([(shift up)] . [(meta p)])
  344. ([(shift down)] . [(meta n)])
  345. ([(shift left)] . [(meta -)])
  346. ([(shift right)] . [(meta +)])
  347. ([(control shift right)] . [(meta shift +)])
  348. ([(control shift left)] . [(meta shift -)]))
  349. "Keys for which Org-mode and other modes compete.
  350. This is an alist, cars are the default keys, second element specifies
  351. the alternative to use when `org-replace-disputed-keys' is t.
  352. Keys can be specified in any syntax supported by `define-key'.
  353. The value of this option takes effect only at Org-mode's startup,
  354. therefore you'll have to restart Emacs to apply it after changing."
  355. :group 'org-startup
  356. :type 'alist)
  357. (defun org-key (key)
  358. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  359. Or return the original if not disputed."
  360. (if org-replace-disputed-keys
  361. (let* ((nkey (key-description key))
  362. (x (org-find-if (lambda (x)
  363. (equal (key-description (car x)) nkey))
  364. org-disputed-keys)))
  365. (if x (cdr x) key))
  366. key))
  367. (defun org-find-if (predicate seq)
  368. (catch 'exit
  369. (while seq
  370. (if (funcall predicate (car seq))
  371. (throw 'exit (car seq))
  372. (pop seq)))))
  373. (defun org-defkey (keymap key def)
  374. "Define a key, possibly translated, as returned by `org-key'."
  375. (define-key keymap (org-key key) def))
  376. (defcustom org-ellipsis nil
  377. "The ellipsis to use in the Org-mode outline.
  378. When nil, just use the standard three dots. When a string, use that instead,
  379. When a face, use the standard 3 dots, but with the specified face.
  380. The change affects only Org-mode (which will then use its own display table).
  381. Changing this requires executing `M-x org-mode' in a buffer to become
  382. effective."
  383. :group 'org-startup
  384. :type '(choice (const :tag "Default" nil)
  385. (face :tag "Face" :value org-warning)
  386. (string :tag "String" :value "...#")))
  387. (defvar org-display-table nil
  388. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  389. (defgroup org-keywords nil
  390. "Keywords in Org-mode."
  391. :tag "Org Keywords"
  392. :group 'org)
  393. (defcustom org-deadline-string "DEADLINE:"
  394. "String to mark deadline entries.
  395. A deadline is this string, followed by a time stamp. Should be a word,
  396. terminated by a colon. You can insert a schedule keyword and
  397. a timestamp with \\[org-deadline].
  398. Changes become only effective after restarting Emacs."
  399. :group 'org-keywords
  400. :type 'string)
  401. (defcustom org-scheduled-string "SCHEDULED:"
  402. "String to mark scheduled TODO entries.
  403. A schedule is this string, followed by a time stamp. Should be a word,
  404. terminated by a colon. You can insert a schedule keyword and
  405. a timestamp with \\[org-schedule].
  406. Changes become only effective after restarting Emacs."
  407. :group 'org-keywords
  408. :type 'string)
  409. (defcustom org-closed-string "CLOSED:"
  410. "String used as the prefix for timestamps logging closing a TODO entry."
  411. :group 'org-keywords
  412. :type 'string)
  413. (defcustom org-clock-string "CLOCK:"
  414. "String used as prefix for timestamps clocking work hours on an item."
  415. :group 'org-keywords
  416. :type 'string)
  417. (defcustom org-comment-string "COMMENT"
  418. "Entries starting with this keyword will never be exported.
  419. An entry can be toggled between COMMENT and normal with
  420. \\[org-toggle-comment].
  421. Changes become only effective after restarting Emacs."
  422. :group 'org-keywords
  423. :type 'string)
  424. (defcustom org-quote-string "QUOTE"
  425. "Entries starting with this keyword will be exported in fixed-width font.
  426. Quoting applies only to the text in the entry following the headline, and does
  427. not extend beyond the next headline, even if that is lower level.
  428. An entry can be toggled between QUOTE and normal with
  429. \\[org-toggle-fixed-width-section]."
  430. :group 'org-keywords
  431. :type 'string)
  432. (defconst org-repeat-re
  433. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  434. "Regular expression for specifying repeated events.
  435. After a match, group 1 contains the repeat expression.")
  436. (defgroup org-structure nil
  437. "Options concerning the general structure of Org-mode files."
  438. :tag "Org Structure"
  439. :group 'org)
  440. (defgroup org-reveal-location nil
  441. "Options about how to make context of a location visible."
  442. :tag "Org Reveal Location"
  443. :group 'org-structure)
  444. (defconst org-context-choice
  445. '(choice
  446. (const :tag "Always" t)
  447. (const :tag "Never" nil)
  448. (repeat :greedy t :tag "Individual contexts"
  449. (cons
  450. (choice :tag "Context"
  451. (const agenda)
  452. (const org-goto)
  453. (const occur-tree)
  454. (const tags-tree)
  455. (const link-search)
  456. (const mark-goto)
  457. (const bookmark-jump)
  458. (const isearch)
  459. (const default))
  460. (boolean))))
  461. "Contexts for the reveal options.")
  462. (defcustom org-show-hierarchy-above '((default . t))
  463. "Non-nil means, show full hierarchy when revealing a location.
  464. Org-mode often shows locations in an org-mode file which might have
  465. been invisible before. When this is set, the hierarchy of headings
  466. above the exposed location is shown.
  467. Turning this off for example for sparse trees makes them very compact.
  468. Instead of t, this can also be an alist specifying this option for different
  469. contexts. Valid contexts are
  470. agenda when exposing an entry from the agenda
  471. org-goto when using the command `org-goto' on key C-c C-j
  472. occur-tree when using the command `org-occur' on key C-c /
  473. tags-tree when constructing a sparse tree based on tags matches
  474. link-search when exposing search matches associated with a link
  475. mark-goto when exposing the jump goal of a mark
  476. bookmark-jump when exposing a bookmark location
  477. isearch when exiting from an incremental search
  478. default default for all contexts not set explicitly"
  479. :group 'org-reveal-location
  480. :type org-context-choice)
  481. (defcustom org-show-following-heading '((default . nil))
  482. "Non-nil means, show following heading when revealing a location.
  483. Org-mode often shows locations in an org-mode file which might have
  484. been invisible before. When this is set, the heading following the
  485. match is shown.
  486. Turning this off for example for sparse trees makes them very compact,
  487. but makes it harder to edit the location of the match. In such a case,
  488. use the command \\[org-reveal] to show more context.
  489. Instead of t, this can also be an alist specifying this option for different
  490. contexts. See `org-show-hierarchy-above' for valid contexts."
  491. :group 'org-reveal-location
  492. :type org-context-choice)
  493. (defcustom org-show-siblings '((default . nil) (isearch t))
  494. "Non-nil means, show all sibling heading when revealing a location.
  495. Org-mode often shows locations in an org-mode file which might have
  496. been invisible before. When this is set, the sibling of the current entry
  497. heading are all made visible. If `org-show-hierarchy-above' is t,
  498. the same happens on each level of the hierarchy above the current entry.
  499. By default this is on for the isearch context, off for all other contexts.
  500. Turning this off for example for sparse trees makes them very compact,
  501. but makes it harder to edit the location of the match. In such a case,
  502. use the command \\[org-reveal] to show more context.
  503. Instead of t, this can also be an alist specifying this option for different
  504. contexts. See `org-show-hierarchy-above' for valid contexts."
  505. :group 'org-reveal-location
  506. :type org-context-choice)
  507. (defcustom org-show-entry-below '((default . nil))
  508. "Non-nil means, show the entry below a headline when revealing a location.
  509. Org-mode often shows locations in an org-mode file which might have
  510. been invisible before. When this is set, the text below the headline that is
  511. exposed is also shown.
  512. By default this is off for all contexts.
  513. Instead of t, this can also be an alist specifying this option for different
  514. contexts. See `org-show-hierarchy-above' for valid contexts."
  515. :group 'org-reveal-location
  516. :type org-context-choice)
  517. (defcustom org-indirect-buffer-display 'other-window
  518. "How should indirect tree buffers be displayed?
  519. This applies to indirect buffers created with the commands
  520. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  521. Valid values are:
  522. current-window Display in the current window
  523. other-window Just display in another window.
  524. dedicated-frame Create one new frame, and re-use it each time.
  525. new-frame Make a new frame each time. Note that in this case
  526. previously-made indirect buffers are kept, and you need to
  527. kill these buffers yourself."
  528. :group 'org-structure
  529. :group 'org-agenda-windows
  530. :type '(choice
  531. (const :tag "In current window" current-window)
  532. (const :tag "In current frame, other window" other-window)
  533. (const :tag "Each time a new frame" new-frame)
  534. (const :tag "One dedicated frame" dedicated-frame)))
  535. (defcustom org-use-speed-commands nil
  536. "Non-nil means, activate single letter commands at beginning of a headline.
  537. This may also be a function to test for appropriate locations where speed
  538. commands should be active."
  539. :group 'org-structure
  540. :type '(choice
  541. (const :tag "Never" nil)
  542. (const :tag "At beginning of headline stars" t)
  543. (function)))
  544. (defcustom org-speed-commands-user nil
  545. "Alist of additional speed commands.
  546. This list will be checked before `org-speed-commands-default'
  547. when the variable `org-use-speed-commands' is non-nil
  548. and when the cursor is at the beginning of a headline.
  549. The car if each entry is a string with a single letter, which must
  550. be assigned to `self-insert-command' in the global map.
  551. The cdr is either a command to be called interactively, a function
  552. to be called, or a form to be evaluated.
  553. An entry that is just a list with a single string will be interpreted
  554. as a descriptive headline that will be added when listing the speed
  555. copmmands in the Help buffer using the `?' speed command."
  556. :group 'org-structure
  557. :type '(repeat :value ("k" . ignore)
  558. (choice :value ("k" . ignore)
  559. (list :tag "Descriptive Headline" (string :tag "Headline"))
  560. (cons :tag "Letter and Command"
  561. (string :tag "Command letter")
  562. (choice
  563. (function)
  564. (sexp))))))
  565. (defgroup org-cycle nil
  566. "Options concerning visibility cycling in Org-mode."
  567. :tag "Org Cycle"
  568. :group 'org-structure)
  569. (defcustom org-cycle-skip-children-state-if-no-children t
  570. "Non-nil means, skip CHILDREN state in entries that don't have any."
  571. :group 'org-cycle
  572. :type 'boolean)
  573. (defcustom org-cycle-max-level nil
  574. "Maximum level which should still be subject to visibility cycling.
  575. Levels higher than this will, for cycling, be treated as text, not a headline.
  576. When `org-odd-levels-only' is set, a value of N in this variable actually
  577. means 2N-1 stars as the limiting headline.
  578. When nil, cycle all levels.
  579. Note that the limiting level of cycling is also influenced by
  580. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  581. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  582. than its value."
  583. :group 'org-cycle
  584. :type '(choice
  585. (const :tag "No limit" nil)
  586. (integer :tag "Maximum level")))
  587. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  588. "Names of drawers. Drawers are not opened by cycling on the headline above.
  589. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  590. this:
  591. :DRAWERNAME:
  592. .....
  593. :END:
  594. The drawer \"PROPERTIES\" is special for capturing properties through
  595. the property API.
  596. Drawers can be defined on the per-file basis with a line like:
  597. #+DRAWERS: HIDDEN STATE PROPERTIES"
  598. :group 'org-structure
  599. :group 'org-cycle
  600. :type '(repeat (string :tag "Drawer Name")))
  601. (defcustom org-hide-block-startup nil
  602. "Non-nil means, , entering Org-mode will fold all blocks.
  603. This can also be set in on a per-file basis with
  604. #+STARTUP: hideblocks
  605. #+STARTUP: showblocks"
  606. :group 'org-startup
  607. :group 'org-cycle
  608. :type 'boolean)
  609. (defcustom org-cycle-global-at-bob nil
  610. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  611. This makes it possible to do global cycling without having to use S-TAB or
  612. C-u TAB. For this special case to work, the first line of the buffer
  613. must not be a headline - it may be empty or some other text. When used in
  614. this way, `org-cycle-hook' is disables temporarily, to make sure the
  615. cursor stays at the beginning of the buffer.
  616. When this option is nil, don't do anything special at the beginning
  617. of the buffer."
  618. :group 'org-cycle
  619. :type 'boolean)
  620. (defcustom org-cycle-level-after-item/entry-creation t
  621. "Non-nil means, cycle entry level or item indentation in new empty entries.
  622. When the cursor is at the end of an empty headline, i.e with only stars
  623. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  624. and then all possible anchestor states, before returning to the original state.
  625. This makes data entry extremely fast: M-RET to create a new headline,
  626. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  627. When the cursor is at the end of an empty plain list item, one TAB will
  628. make it a subitem, two or more tabs will back up to make this an item
  629. higher up in the item hierarchy."
  630. :group 'org-cycle
  631. :type 'boolean)
  632. (defcustom org-cycle-emulate-tab t
  633. "Where should `org-cycle' emulate TAB.
  634. nil Never
  635. white Only in completely white lines
  636. whitestart Only at the beginning of lines, before the first non-white char
  637. t Everywhere except in headlines
  638. exc-hl-bol Everywhere except at the start of a headline
  639. If TAB is used in a place where it does not emulate TAB, the current subtree
  640. visibility is cycled."
  641. :group 'org-cycle
  642. :type '(choice (const :tag "Never" nil)
  643. (const :tag "Only in completely white lines" white)
  644. (const :tag "Before first char in a line" whitestart)
  645. (const :tag "Everywhere except in headlines" t)
  646. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  647. ))
  648. (defcustom org-cycle-separator-lines 2
  649. "Number of empty lines needed to keep an empty line between collapsed trees.
  650. If you leave an empty line between the end of a subtree and the following
  651. headline, this empty line is hidden when the subtree is folded.
  652. Org-mode will leave (exactly) one empty line visible if the number of
  653. empty lines is equal or larger to the number given in this variable.
  654. So the default 2 means, at least 2 empty lines after the end of a subtree
  655. are needed to produce free space between a collapsed subtree and the
  656. following headline.
  657. If the number is negative, and the number of empty lines is at least -N,
  658. all empty lines are shown.
  659. Special case: when 0, never leave empty lines in collapsed view."
  660. :group 'org-cycle
  661. :type 'integer)
  662. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  663. (defcustom org-pre-cycle-hook nil
  664. "Hook that is run before visibility cycling is happening.
  665. The function(s) in this hook must accept a single argument which indicates
  666. the new state that will be set right after running this hook. The
  667. argument is a symbol. Before a global state change, it can have the values
  668. `overview', `content', or `all'. Before a local state change, it can have
  669. the values `folded', `children', or `subtree'."
  670. :group 'org-cycle
  671. :type 'hook)
  672. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  673. org-cycle-hide-drawers
  674. org-cycle-show-empty-lines
  675. org-optimize-window-after-visibility-change)
  676. "Hook that is run after `org-cycle' has changed the buffer visibility.
  677. The function(s) in this hook must accept a single argument which indicates
  678. the new state that was set by the most recent `org-cycle' command. The
  679. argument is a symbol. After a global state change, it can have the values
  680. `overview', `content', or `all'. After a local state change, it can have
  681. the values `folded', `children', or `subtree'."
  682. :group 'org-cycle
  683. :type 'hook)
  684. (defgroup org-edit-structure nil
  685. "Options concerning structure editing in Org-mode."
  686. :tag "Org Edit Structure"
  687. :group 'org-structure)
  688. (defcustom org-odd-levels-only nil
  689. "Non-nil means, skip even levels and only use odd levels for the outline.
  690. This has the effect that two stars are being added/taken away in
  691. promotion/demotion commands. It also influences how levels are
  692. handled by the exporters.
  693. Changing it requires restart of `font-lock-mode' to become effective
  694. for fontification also in regions already fontified.
  695. You may also set this on a per-file basis by adding one of the following
  696. lines to the buffer:
  697. #+STARTUP: odd
  698. #+STARTUP: oddeven"
  699. :group 'org-edit-structure
  700. :group 'org-font-lock
  701. :type 'boolean)
  702. (defcustom org-adapt-indentation t
  703. "Non-nil means, adapt indentation to outline node level.
  704. When this variable is set, Org assumes that you write outlines by
  705. indenting text in each node to align with the headline (after the stars).
  706. The following issues are influenced by this variable:
  707. - When this is set and the *entire* text in an entry is indented, the
  708. indentation is increased by one space in a demotion command, and
  709. decreased by one in a promotion command. If any line in the entry
  710. body starts with text at column 0, indentation is not changed at all.
  711. - Property drawers and planning information is inserted indented when
  712. this variable s set. When nil, they will not be indented.
  713. - TAB indents a line relative to context. The lines below a headline
  714. will be indented when this variable is set.
  715. Note that this is all about true indentation, by adding and removing
  716. space characters. See also `org-indent.el' which does level-dependent
  717. indentation in a virtual way, i.e. at display time in Emacs."
  718. :group 'org-edit-structure
  719. :type 'boolean)
  720. (defcustom org-special-ctrl-a/e nil
  721. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  722. When t, `C-a' will bring back the cursor to the beginning of the
  723. headline text, i.e. after the stars and after a possible TODO keyword.
  724. In an item, this will be the position after the bullet.
  725. When the cursor is already at that position, another `C-a' will bring
  726. it to the beginning of the line.
  727. `C-e' will jump to the end of the headline, ignoring the presence of tags
  728. in the headline. A second `C-e' will then jump to the true end of the
  729. line, after any tags. This also means that, when this variable is
  730. non-nil, `C-e' also will never jump beyond the end of the heading of a
  731. folded section, i.e. not after the ellipses.
  732. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  733. going to the true line boundary first. Only a directly following, identical
  734. keypress will bring the cursor to the special positions.
  735. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  736. set separately."
  737. :group 'org-edit-structure
  738. :type '(choice
  739. (const :tag "off" nil)
  740. (const :tag "on: after stars/bullet and before tags first" t)
  741. (const :tag "reversed: true line boundary first" reversed)
  742. (cons :tag "Set C-a and C-e separately"
  743. (choice :tag "Special C-a"
  744. (const :tag "off" nil)
  745. (const :tag "on: after stars/bullet first" t)
  746. (const :tag "reversed: before stars/bullet first" reversed))
  747. (choice :tag "Special C-e"
  748. (const :tag "off" nil)
  749. (const :tag "on: before tags first" t)
  750. (const :tag "reversed: after tags first" reversed)))))
  751. (if (fboundp 'defvaralias)
  752. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  753. (defcustom org-special-ctrl-k nil
  754. "Non-nil means `C-k' will behave specially in headlines.
  755. When nil, `C-k' will call the default `kill-line' command.
  756. When t, the following will happen while the cursor is in the headline:
  757. - When the cursor is at the beginning of a headline, kill the entire
  758. line and possible the folded subtree below the line.
  759. - When in the middle of the headline text, kill the headline up to the tags.
  760. - When after the headline text, kill the tags."
  761. :group 'org-edit-structure
  762. :type 'boolean)
  763. (defcustom org-yank-folded-subtrees t
  764. "Non-nil means, when yanking subtrees, fold them.
  765. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  766. it starts with a heading and all other headings in it are either children
  767. or siblings, then fold all the subtrees. However, do this only if no
  768. text after the yank would be swallowed into a folded tree by this action."
  769. :group 'org-edit-structure
  770. :type 'boolean)
  771. (defcustom org-yank-adjusted-subtrees nil
  772. "Non-nil means, when yanking subtrees, adjust the level.
  773. With this setting, `org-paste-subtree' is used to insert the subtree, see
  774. this function for details."
  775. :group 'org-edit-structure
  776. :type 'boolean)
  777. (defcustom org-M-RET-may-split-line '((default . t))
  778. "Non-nil means, M-RET will split the line at the cursor position.
  779. When nil, it will go to the end of the line before making a
  780. new line.
  781. You may also set this option in a different way for different
  782. contexts. Valid contexts are:
  783. headline when creating a new headline
  784. item when creating a new item
  785. table in a table field
  786. default the value to be used for all contexts not explicitly
  787. customized"
  788. :group 'org-structure
  789. :group 'org-table
  790. :type '(choice
  791. (const :tag "Always" t)
  792. (const :tag "Never" nil)
  793. (repeat :greedy t :tag "Individual contexts"
  794. (cons
  795. (choice :tag "Context"
  796. (const headline)
  797. (const item)
  798. (const table)
  799. (const default))
  800. (boolean)))))
  801. (defcustom org-insert-heading-respect-content nil
  802. "Non-nil means, insert new headings after the current subtree.
  803. When nil, the new heading is created directly after the current line.
  804. The commands \\[org-insert-heading-respect-content] and
  805. \\[org-insert-todo-heading-respect-content] turn this variable on
  806. for the duration of the command."
  807. :group 'org-structure
  808. :type 'boolean)
  809. (defcustom org-blank-before-new-entry '((heading . auto)
  810. (plain-list-item . auto))
  811. "Should `org-insert-heading' leave a blank line before new heading/item?
  812. The value is an alist, with `heading' and `plain-list-item' as car,
  813. and a boolean flag as cdr. For plain lists, if the variable
  814. `org-empty-line-terminates-plain-lists' is set, the setting here
  815. is ignored and no empty line is inserted, to keep the list in tact."
  816. :group 'org-edit-structure
  817. :type '(list
  818. (cons (const heading)
  819. (choice (const :tag "Never" nil)
  820. (const :tag "Always" t)
  821. (const :tag "Auto" auto)))
  822. (cons (const plain-list-item)
  823. (choice (const :tag "Never" nil)
  824. (const :tag "Always" t)
  825. (const :tag "Auto" auto)))))
  826. (defcustom org-insert-heading-hook nil
  827. "Hook being run after inserting a new heading."
  828. :group 'org-edit-structure
  829. :type 'hook)
  830. (defcustom org-enable-fixed-width-editor t
  831. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  832. This currently only means, they are never auto-wrapped.
  833. When nil, such lines will be treated like ordinary lines.
  834. See also the QUOTE keyword."
  835. :group 'org-edit-structure
  836. :type 'boolean)
  837. (defcustom org-goto-auto-isearch t
  838. "Non-nil means, typing characters in org-goto starts incremental search."
  839. :group 'org-edit-structure
  840. :type 'boolean)
  841. (defgroup org-sparse-trees nil
  842. "Options concerning sparse trees in Org-mode."
  843. :tag "Org Sparse Trees"
  844. :group 'org-structure)
  845. (defcustom org-highlight-sparse-tree-matches t
  846. "Non-nil means, highlight all matches that define a sparse tree.
  847. The highlights will automatically disappear the next time the buffer is
  848. changed by an edit command."
  849. :group 'org-sparse-trees
  850. :type 'boolean)
  851. (defcustom org-remove-highlights-with-change t
  852. "Non-nil means, any change to the buffer will remove temporary highlights.
  853. Such highlights are created by `org-occur' and `org-clock-display'.
  854. When nil, `C-c C-c needs to be used to get rid of the highlights.
  855. The highlights created by `org-preview-latex-fragment' always need
  856. `C-c C-c' to be removed."
  857. :group 'org-sparse-trees
  858. :group 'org-time
  859. :type 'boolean)
  860. (defcustom org-occur-hook '(org-first-headline-recenter)
  861. "Hook that is run after `org-occur' has constructed a sparse tree.
  862. This can be used to recenter the window to show as much of the structure
  863. as possible."
  864. :group 'org-sparse-trees
  865. :type 'hook)
  866. (defgroup org-imenu-and-speedbar nil
  867. "Options concerning imenu and speedbar in Org-mode."
  868. :tag "Org Imenu and Speedbar"
  869. :group 'org-structure)
  870. (defcustom org-imenu-depth 2
  871. "The maximum level for Imenu access to Org-mode headlines.
  872. This also applied for speedbar access."
  873. :group 'org-imenu-and-speedbar
  874. :type 'integer)
  875. (defgroup org-table nil
  876. "Options concerning tables in Org-mode."
  877. :tag "Org Table"
  878. :group 'org)
  879. (defcustom org-enable-table-editor 'optimized
  880. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  881. When nil, such lines will be treated like ordinary lines.
  882. When equal to the symbol `optimized', the table editor will be optimized to
  883. do the following:
  884. - Automatic overwrite mode in front of whitespace in table fields.
  885. This makes the structure of the table stay in tact as long as the edited
  886. field does not exceed the column width.
  887. - Minimize the number of realigns. Normally, the table is aligned each time
  888. TAB or RET are pressed to move to another field. With optimization this
  889. happens only if changes to a field might have changed the column width.
  890. Optimization requires replacing the functions `self-insert-command',
  891. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  892. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  893. very good at guessing when a re-align will be necessary, but you can always
  894. force one with \\[org-ctrl-c-ctrl-c].
  895. If you would like to use the optimized version in Org-mode, but the
  896. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  897. This variable can be used to turn on and off the table editor during a session,
  898. but in order to toggle optimization, a restart is required.
  899. See also the variable `org-table-auto-blank-field'."
  900. :group 'org-table
  901. :type '(choice
  902. (const :tag "off" nil)
  903. (const :tag "on" t)
  904. (const :tag "on, optimized" optimized)))
  905. (defcustom org-self-insert-cluster-for-undo t
  906. "Non-nil means cluster self-insert commands for undo when possible.
  907. If this is set, then, like in the Emacs command loop, 20 consecutive
  908. characters will be undone together.
  909. This is configurable, because there is some impact on typing performance."
  910. :group 'org-table
  911. :type 'boolean)
  912. (defcustom org-table-tab-recognizes-table.el t
  913. "Non-nil means, TAB will automatically notice a table.el table.
  914. When it sees such a table, it moves point into it and - if necessary -
  915. calls `table-recognize-table'."
  916. :group 'org-table-editing
  917. :type 'boolean)
  918. (defgroup org-link nil
  919. "Options concerning links in Org-mode."
  920. :tag "Org Link"
  921. :group 'org)
  922. (defvar org-link-abbrev-alist-local nil
  923. "Buffer-local version of `org-link-abbrev-alist', which see.
  924. The value of this is taken from the #+LINK lines.")
  925. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  926. (defcustom org-link-abbrev-alist nil
  927. "Alist of link abbreviations.
  928. The car of each element is a string, to be replaced at the start of a link.
  929. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  930. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  931. [[linkkey:tag][description]]
  932. The 'linkkey' must be a word word, starting with a letter, followed
  933. by letters, numbers, '-' or '_'.
  934. If REPLACE is a string, the tag will simply be appended to create the link.
  935. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  936. the placeholder \"%h\" will cause a url-encoded version of the tag to
  937. be inserted at that point (see the function `url-hexify-string').
  938. REPLACE may also be a function that will be called with the tag as the
  939. only argument to create the link, which should be returned as a string.
  940. See the manual for examples."
  941. :group 'org-link
  942. :type '(repeat
  943. (cons
  944. (string :tag "Protocol")
  945. (choice
  946. (string :tag "Format")
  947. (function)))))
  948. (defcustom org-descriptive-links t
  949. "Non-nil means, hide link part and only show description of bracket links.
  950. Bracket links are like [[link][description]]. This variable sets the initial
  951. state in new org-mode buffers. The setting can then be toggled on a
  952. per-buffer basis from the Org->Hyperlinks menu."
  953. :group 'org-link
  954. :type 'boolean)
  955. (defcustom org-link-file-path-type 'adaptive
  956. "How the path name in file links should be stored.
  957. Valid values are:
  958. relative Relative to the current directory, i.e. the directory of the file
  959. into which the link is being inserted.
  960. absolute Absolute path, if possible with ~ for home directory.
  961. noabbrev Absolute path, no abbreviation of home directory.
  962. adaptive Use relative path for files in the current directory and sub-
  963. directories of it. For other files, use an absolute path."
  964. :group 'org-link
  965. :type '(choice
  966. (const relative)
  967. (const absolute)
  968. (const noabbrev)
  969. (const adaptive)))
  970. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  971. "Types of links that should be activated in Org-mode files.
  972. This is a list of symbols, each leading to the activation of a certain link
  973. type. In principle, it does not hurt to turn on most link types - there may
  974. be a small gain when turning off unused link types. The types are:
  975. bracket The recommended [[link][description]] or [[link]] links with hiding.
  976. angular Links in angular brackets that may contain whitespace like
  977. <bbdb:Carsten Dominik>.
  978. plain Plain links in normal text, no whitespace, like http://google.com.
  979. radio Text that is matched by a radio target, see manual for details.
  980. tag Tag settings in a headline (link to tag search).
  981. date Time stamps (link to calendar).
  982. footnote Footnote labels.
  983. Changing this variable requires a restart of Emacs to become effective."
  984. :group 'org-link
  985. :type '(set :greedy t
  986. (const :tag "Double bracket links (new style)" bracket)
  987. (const :tag "Angular bracket links (old style)" angular)
  988. (const :tag "Plain text links" plain)
  989. (const :tag "Radio target matches" radio)
  990. (const :tag "Tags" tag)
  991. (const :tag "Timestamps" date)
  992. (const :tag "Footnotes" footnote)))
  993. (defcustom org-make-link-description-function nil
  994. "Function to use to generate link descriptions from links. If
  995. nil the link location will be used. This function must take two
  996. parameters; the first is the link and the second the description
  997. org-insert-link has generated, and should return the description
  998. to use."
  999. :group 'org-link
  1000. :type 'function)
  1001. (defgroup org-link-store nil
  1002. "Options concerning storing links in Org-mode."
  1003. :tag "Org Store Link"
  1004. :group 'org-link)
  1005. (defcustom org-email-link-description-format "Email %c: %.30s"
  1006. "Format of the description part of a link to an email or usenet message.
  1007. The following %-escapes will be replaced by corresponding information:
  1008. %F full \"From\" field
  1009. %f name, taken from \"From\" field, address if no name
  1010. %T full \"To\" field
  1011. %t first name in \"To\" field, address if no name
  1012. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1013. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1014. %s subject
  1015. %m message-id.
  1016. You may use normal field width specification between the % and the letter.
  1017. This is for example useful to limit the length of the subject.
  1018. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1019. :group 'org-link-store
  1020. :type 'string)
  1021. (defcustom org-from-is-user-regexp
  1022. (let (r1 r2)
  1023. (when (and user-mail-address (not (string= user-mail-address "")))
  1024. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1025. (when (and user-full-name (not (string= user-full-name "")))
  1026. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1027. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1028. "Regexp matched against the \"From:\" header of an email or usenet message.
  1029. It should match if the message is from the user him/herself."
  1030. :group 'org-link-store
  1031. :type 'regexp)
  1032. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1033. "Non-nil means, storing a link to an Org file will use entry IDs.
  1034. Note that before this variable is even considered, org-id must be loaded,
  1035. so please customize `org-modules' and turn it on.
  1036. The variable can have the following values:
  1037. t Create an ID if needed to make a link to the current entry.
  1038. create-if-interactive
  1039. If `org-store-link' is called directly (interactively, as a user
  1040. command), do create an ID to support the link. But when doing the
  1041. job for remember, only use the ID if it already exists. The
  1042. purpose of this setting is to avoid proliferation of unwanted
  1043. IDs, just because you happen to be in an Org file when you
  1044. call `org-remember' that automatically and preemptively
  1045. creates a link. If you do want to get an ID link in a remember
  1046. template to an entry not having an ID, create it first by
  1047. explicitly creating a link to it, using `C-c C-l' first.
  1048. create-if-interactive-and-no-custom-id
  1049. Like create-if-interactive, but do not create an ID if there is
  1050. a CUSTOM_ID property defined in the entry. This is the default.
  1051. use-existing
  1052. Use existing ID, do not create one.
  1053. nil Never use an ID to make a link, instead link using a text search for
  1054. the headline text."
  1055. :group 'org-link-store
  1056. :type '(choice
  1057. (const :tag "Create ID to make link" t)
  1058. (const :tag "Create if storing link interactively"
  1059. create-if-interactive)
  1060. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1061. create-if-interactive-and-no-custom-id)
  1062. (const :tag "Only use existing" use-existing)
  1063. (const :tag "Do not use ID to create link" nil)))
  1064. (defcustom org-context-in-file-links t
  1065. "Non-nil means, file links from `org-store-link' contain context.
  1066. A search string will be added to the file name with :: as separator and
  1067. used to find the context when the link is activated by the command
  1068. `org-open-at-point'.
  1069. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1070. negates this setting for the duration of the command."
  1071. :group 'org-link-store
  1072. :type 'boolean)
  1073. (defcustom org-keep-stored-link-after-insertion nil
  1074. "Non-nil means, keep link in list for entire session.
  1075. The command `org-store-link' adds a link pointing to the current
  1076. location to an internal list. These links accumulate during a session.
  1077. The command `org-insert-link' can be used to insert links into any
  1078. Org-mode file (offering completion for all stored links). When this
  1079. option is nil, every link which has been inserted once using \\[org-insert-link]
  1080. will be removed from the list, to make completing the unused links
  1081. more efficient."
  1082. :group 'org-link-store
  1083. :type 'boolean)
  1084. (defgroup org-link-follow nil
  1085. "Options concerning following links in Org-mode."
  1086. :tag "Org Follow Link"
  1087. :group 'org-link)
  1088. (defcustom org-link-translation-function nil
  1089. "Function to translate links with different syntax to Org syntax.
  1090. This can be used to translate links created for example by the Planner
  1091. or emacs-wiki packages to Org syntax.
  1092. The function must accept two parameters, a TYPE containing the link
  1093. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1094. which is everything after the link protocol. It should return a cons
  1095. with possibly modified values of type and path.
  1096. Org contains a function for this, so if you set this variable to
  1097. `org-translate-link-from-planner', you should be able follow many
  1098. links created by planner."
  1099. :group 'org-link-follow
  1100. :type 'function)
  1101. (defcustom org-follow-link-hook nil
  1102. "Hook that is run after a link has been followed."
  1103. :group 'org-link-follow
  1104. :type 'hook)
  1105. (defcustom org-tab-follows-link nil
  1106. "Non-nil means, on links TAB will follow the link.
  1107. Needs to be set before org.el is loaded.
  1108. This really should not be used, it does not make sense, and the
  1109. implementation is bad."
  1110. :group 'org-link-follow
  1111. :type 'boolean)
  1112. (defcustom org-return-follows-link nil
  1113. "Non-nil means, on links RET will follow the link.
  1114. Needs to be set before org.el is loaded."
  1115. :group 'org-link-follow
  1116. :type 'boolean)
  1117. (defcustom org-mouse-1-follows-link
  1118. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1119. "Non-nil means, mouse-1 on a link will follow the link.
  1120. A longer mouse click will still set point. Does not work on XEmacs.
  1121. Needs to be set before org.el is loaded."
  1122. :group 'org-link-follow
  1123. :type 'boolean)
  1124. (defcustom org-mark-ring-length 4
  1125. "Number of different positions to be recorded in the ring
  1126. Changing this requires a restart of Emacs to work correctly."
  1127. :group 'org-link-follow
  1128. :type 'integer)
  1129. (defcustom org-link-frame-setup
  1130. '((vm . vm-visit-folder-other-frame)
  1131. (gnus . gnus-other-frame)
  1132. (file . find-file-other-window))
  1133. "Setup the frame configuration for following links.
  1134. When following a link with Emacs, it may often be useful to display
  1135. this link in another window or frame. This variable can be used to
  1136. set this up for the different types of links.
  1137. For VM, use any of
  1138. `vm-visit-folder'
  1139. `vm-visit-folder-other-frame'
  1140. For Gnus, use any of
  1141. `gnus'
  1142. `gnus-other-frame'
  1143. `org-gnus-no-new-news'
  1144. For FILE, use any of
  1145. `find-file'
  1146. `find-file-other-window'
  1147. `find-file-other-frame'
  1148. For the calendar, use the variable `calendar-setup'.
  1149. For BBDB, it is currently only possible to display the matches in
  1150. another window."
  1151. :group 'org-link-follow
  1152. :type '(list
  1153. (cons (const vm)
  1154. (choice
  1155. (const vm-visit-folder)
  1156. (const vm-visit-folder-other-window)
  1157. (const vm-visit-folder-other-frame)))
  1158. (cons (const gnus)
  1159. (choice
  1160. (const gnus)
  1161. (const gnus-other-frame)
  1162. (const org-gnus-no-new-news)))
  1163. (cons (const file)
  1164. (choice
  1165. (const find-file)
  1166. (const find-file-other-window)
  1167. (const find-file-other-frame)))))
  1168. (defcustom org-display-internal-link-with-indirect-buffer nil
  1169. "Non-nil means, use indirect buffer to display infile links.
  1170. Activating internal links (from one location in a file to another location
  1171. in the same file) normally just jumps to the location. When the link is
  1172. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1173. another window. When this option is set, the other window actually displays
  1174. an indirect buffer clone of the current buffer, to avoid any visibility
  1175. changes to the current buffer."
  1176. :group 'org-link-follow
  1177. :type 'boolean)
  1178. (defcustom org-open-non-existing-files nil
  1179. "Non-nil means, `org-open-file' will open non-existing files.
  1180. When nil, an error will be generated.
  1181. This variable applies only to external applications because they
  1182. might choke on non-existing files. If the link is to a file that
  1183. will be opened in Emacs, the variable is ignored."
  1184. :group 'org-link-follow
  1185. :type 'boolean)
  1186. (defcustom org-open-directory-means-index-dot-org nil
  1187. "Non-nil means, a link to a directory really means to index.org.
  1188. When nil, following a directory link will run dired or open a finder/explorer
  1189. window on that directory."
  1190. :group 'org-link-follow
  1191. :type 'boolean)
  1192. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1193. "Function and arguments to call for following mailto links.
  1194. This is a list with the first element being a lisp function, and the
  1195. remaining elements being arguments to the function. In string arguments,
  1196. %a will be replaced by the address, and %s will be replaced by the subject
  1197. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1198. :group 'org-link-follow
  1199. :type '(choice
  1200. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1201. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1202. (const :tag "message-mail" (message-mail "%a" "%s"))
  1203. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1204. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1205. "Non-nil means, ask for confirmation before executing shell links.
  1206. Shell links can be dangerous: just think about a link
  1207. [[shell:rm -rf ~/*][Google Search]]
  1208. This link would show up in your Org-mode document as \"Google Search\",
  1209. but really it would remove your entire home directory.
  1210. Therefore we advise against setting this variable to nil.
  1211. Just change it to `y-or-n-p' if you want to confirm with a
  1212. single keystroke rather than having to type \"yes\"."
  1213. :group 'org-link-follow
  1214. :type '(choice
  1215. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1216. (const :tag "with y-or-n (faster)" y-or-n-p)
  1217. (const :tag "no confirmation (dangerous)" nil)))
  1218. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1219. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1220. Elisp links can be dangerous: just think about a link
  1221. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1222. This link would show up in your Org-mode document as \"Google Search\",
  1223. but really it would remove your entire home directory.
  1224. Therefore we advise against setting this variable to nil.
  1225. Just change it to `y-or-n-p' if you want to confirm with a
  1226. single keystroke rather than having to type \"yes\"."
  1227. :group 'org-link-follow
  1228. :type '(choice
  1229. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1230. (const :tag "with y-or-n (faster)" y-or-n-p)
  1231. (const :tag "no confirmation (dangerous)" nil)))
  1232. (defconst org-file-apps-defaults-gnu
  1233. '((remote . emacs)
  1234. (system . mailcap)
  1235. (t . mailcap))
  1236. "Default file applications on a UNIX or GNU/Linux system.
  1237. See `org-file-apps'.")
  1238. (defconst org-file-apps-defaults-macosx
  1239. '((remote . emacs)
  1240. (t . "open %s")
  1241. (system . "open %s")
  1242. ("ps.gz" . "gv %s")
  1243. ("eps.gz" . "gv %s")
  1244. ("dvi" . "xdvi %s")
  1245. ("fig" . "xfig %s"))
  1246. "Default file applications on a MacOS X system.
  1247. The system \"open\" is known as a default, but we use X11 applications
  1248. for some files for which the OS does not have a good default.
  1249. See `org-file-apps'.")
  1250. (defconst org-file-apps-defaults-windowsnt
  1251. (list
  1252. '(remote . emacs)
  1253. (cons t
  1254. (list (if (featurep 'xemacs)
  1255. 'mswindows-shell-execute
  1256. 'w32-shell-execute)
  1257. "open" 'file))
  1258. (cons 'system
  1259. (list (if (featurep 'xemacs)
  1260. 'mswindows-shell-execute
  1261. 'w32-shell-execute)
  1262. "open" 'file)))
  1263. "Default file applications on a Windows NT system.
  1264. The system \"open\" is used for most files.
  1265. See `org-file-apps'.")
  1266. (defcustom org-file-apps
  1267. '(
  1268. (auto-mode . emacs)
  1269. ("\\.mm\\'" . default)
  1270. ("\\.x?html?\\'" . default)
  1271. ("\\.pdf\\'" . default)
  1272. )
  1273. "External applications for opening `file:path' items in a document.
  1274. Org-mode uses system defaults for different file types, but
  1275. you can use this variable to set the application for a given file
  1276. extension. The entries in this list are cons cells where the car identifies
  1277. files and the cdr the corresponding command. Possible values for the
  1278. file identifier are
  1279. \"regex\" Regular expression matched against the file name. For backward
  1280. compatibility, this can also be a string with only alphanumeric
  1281. characters, which is then interpreted as an extension.
  1282. `directory' Matches a directory
  1283. `remote' Matches a remote file, accessible through tramp or efs.
  1284. Remote files most likely should be visited through Emacs
  1285. because external applications cannot handle such paths.
  1286. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1287. so all files Emacs knows how to handle. Using this with
  1288. command `emacs' will open most files in Emacs. Beware that this
  1289. will also open html files inside Emacs, unless you add
  1290. (\"html\" . default) to the list as well.
  1291. t Default for files not matched by any of the other options.
  1292. `system' The system command to open files, like `open' on Windows
  1293. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1294. that will be selected if you call `C-c C-o' with a double
  1295. `C-u C-u' prefix.
  1296. Possible values for the command are:
  1297. `emacs' The file will be visited by the current Emacs process.
  1298. `default' Use the default application for this file type, which is the
  1299. association for t in the list, most likely in the system-specific
  1300. part.
  1301. This can be used to overrule an unwanted setting in the
  1302. system-specific variable.
  1303. `system' Use the system command for opening files, like \"open\".
  1304. This command is specified by the entry whose car is `system'.
  1305. Most likely, the system-specific version of this variable
  1306. does define this command, but you can overrule/replace it
  1307. here.
  1308. string A command to be executed by a shell; %s will be replaced
  1309. by the path to the file.
  1310. sexp A Lisp form which will be evaluated. The file path will
  1311. be available in the Lisp variable `file'.
  1312. For more examples, see the system specific constants
  1313. `org-file-apps-defaults-macosx'
  1314. `org-file-apps-defaults-windowsnt'
  1315. `org-file-apps-defaults-gnu'."
  1316. :group 'org-link-follow
  1317. :type '(repeat
  1318. (cons (choice :value ""
  1319. (string :tag "Extension")
  1320. (const :tag "System command to open files" system)
  1321. (const :tag "Default for unrecognized files" t)
  1322. (const :tag "Remote file" remote)
  1323. (const :tag "Links to a directory" directory)
  1324. (const :tag "Any files that have Emacs modes"
  1325. auto-mode))
  1326. (choice :value ""
  1327. (const :tag "Visit with Emacs" emacs)
  1328. (const :tag "Use default" default)
  1329. (const :tag "Use the system command" system)
  1330. (string :tag "Command")
  1331. (sexp :tag "Lisp form")))))
  1332. (defgroup org-refile nil
  1333. "Options concerning refiling entries in Org-mode."
  1334. :tag "Org Refile"
  1335. :group 'org)
  1336. (defcustom org-directory "~/org"
  1337. "Directory with org files.
  1338. This is just a default location to look for Org files. There is no need
  1339. at all to put your files into this directory. It is only used in the
  1340. following situations:
  1341. 1. When a remember template specifies a target file that is not an
  1342. absolute path. The path will then be interpreted relative to
  1343. `org-directory'
  1344. 2. When a remember note is filed away in an interactive way (when exiting the
  1345. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1346. with `org-directory' as the default path."
  1347. :group 'org-refile
  1348. :group 'org-remember
  1349. :type 'directory)
  1350. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1351. "Default target for storing notes.
  1352. Used by the hooks for remember.el. This can be a string, or nil to mean
  1353. the value of `remember-data-file'.
  1354. You can set this on a per-template basis with the variable
  1355. `org-remember-templates'."
  1356. :group 'org-refile
  1357. :group 'org-remember
  1358. :type '(choice
  1359. (const :tag "Default from remember-data-file" nil)
  1360. file))
  1361. (defcustom org-goto-interface 'outline
  1362. "The default interface to be used for `org-goto'.
  1363. Allowed values are:
  1364. outline The interface shows an outline of the relevant file
  1365. and the correct heading is found by moving through
  1366. the outline or by searching with incremental search.
  1367. outline-path-completion Headlines in the current buffer are offered via
  1368. completion. This is the interface also used by
  1369. the refile command."
  1370. :group 'org-refile
  1371. :type '(choice
  1372. (const :tag "Outline" outline)
  1373. (const :tag "Outline-path-completion" outline-path-completion)))
  1374. (defcustom org-goto-max-level 5
  1375. "Maximum level to be considered when running org-goto with refile interface."
  1376. :group 'org-refile
  1377. :type 'integer)
  1378. (defcustom org-reverse-note-order nil
  1379. "Non-nil means, store new notes at the beginning of a file or entry.
  1380. When nil, new notes will be filed to the end of a file or entry.
  1381. This can also be a list with cons cells of regular expressions that
  1382. are matched against file names, and values."
  1383. :group 'org-remember
  1384. :group 'org-refile
  1385. :type '(choice
  1386. (const :tag "Reverse always" t)
  1387. (const :tag "Reverse never" nil)
  1388. (repeat :tag "By file name regexp"
  1389. (cons regexp boolean))))
  1390. (defcustom org-refile-targets nil
  1391. "Targets for refiling entries with \\[org-refile].
  1392. This is list of cons cells. Each cell contains:
  1393. - a specification of the files to be considered, either a list of files,
  1394. or a symbol whose function or variable value will be used to retrieve
  1395. a file name or a list of file names. If you use `org-agenda-files' for
  1396. that, all agenda files will be scanned for targets. Nil means, consider
  1397. headings in the current buffer.
  1398. - A specification of how to find candidate refile targets. This may be
  1399. any of:
  1400. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1401. This tag has to be present in all target headlines, inheritance will
  1402. not be considered.
  1403. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1404. todo keyword.
  1405. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1406. headlines that are refiling targets.
  1407. - a cons cell (:level . N). Any headline of level N is considered a target.
  1408. Note that, when `org-odd-levels-only' is set, level corresponds to
  1409. order in hierarchy, not to the number of stars.
  1410. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1411. Note that, when `org-odd-levels-only' is set, level corresponds to
  1412. order in hierarchy, not to the number of stars.
  1413. You can set the variable `org-refile-target-verify-function' to a function
  1414. to verify each headline found by the simple critery above.
  1415. When this variable is nil, all top-level headlines in the current buffer
  1416. are used, equivalent to the value `((nil . (:level . 1))'."
  1417. :group 'org-refile
  1418. :type '(repeat
  1419. (cons
  1420. (choice :value org-agenda-files
  1421. (const :tag "All agenda files" org-agenda-files)
  1422. (const :tag "Current buffer" nil)
  1423. (function) (variable) (file))
  1424. (choice :tag "Identify target headline by"
  1425. (cons :tag "Specific tag" (const :value :tag) (string))
  1426. (cons :tag "TODO keyword" (const :value :todo) (string))
  1427. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1428. (cons :tag "Level number" (const :value :level) (integer))
  1429. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1430. (defcustom org-refile-target-verify-function nil
  1431. "Function to verify if the headline at point should be a refile target.
  1432. The function will be called without arguments, with point at the
  1433. beginning of the headline. It should return t and leave point
  1434. where it is if the headline is a valid target for refiling.
  1435. If the target should not be selected, the function must return nil.
  1436. In addition to this, it may move point to a place from where the search
  1437. should be continued. For example, the function may decide that the entire
  1438. subtree of the current entry should be excluded and move point to the end
  1439. of the subtree."
  1440. :group 'org-refile
  1441. :type 'function)
  1442. (defcustom org-refile-use-outline-path nil
  1443. "Non-nil means, provide refile targets as paths.
  1444. So a level 3 headline will be available as level1/level2/level3.
  1445. When the value is `file', also include the file name (without directory)
  1446. into the path. In this case, you can also stop the completion after
  1447. the file name, to get entries inserted as top level in the file.
  1448. When `full-file-path', include the full file path."
  1449. :group 'org-refile
  1450. :type '(choice
  1451. (const :tag "Not" nil)
  1452. (const :tag "Yes" t)
  1453. (const :tag "Start with file name" file)
  1454. (const :tag "Start with full file path" full-file-path)))
  1455. (defcustom org-outline-path-complete-in-steps t
  1456. "Non-nil means, complete the outline path in hierarchical steps.
  1457. When Org-mode uses the refile interface to select an outline path
  1458. \(see variable `org-refile-use-outline-path'), the completion of
  1459. the path can be done is a single go, or if can be done in steps down
  1460. the headline hierarchy. Going in steps is probably the best if you
  1461. do not use a special completion package like `ido' or `icicles'.
  1462. However, when using these packages, going in one step can be very
  1463. fast, while still showing the whole path to the entry."
  1464. :group 'org-refile
  1465. :type 'boolean)
  1466. (defcustom org-refile-allow-creating-parent-nodes nil
  1467. "Non-nil means, allow to create new nodes as refile targets.
  1468. New nodes are then created by adding \"/new node name\" to the completion
  1469. of an existing node. When the value of this variable is `confirm',
  1470. new node creation must be confirmed by the user (recommended)
  1471. When nil, the completion must match an existing entry.
  1472. Note that, if the new heading is not seen by the criteria
  1473. listed in `org-refile-targets', multiple instances of the same
  1474. heading would be created by trying again to file under the new
  1475. heading."
  1476. :group 'org-refile
  1477. :type '(choice
  1478. (const :tag "Never" nil)
  1479. (const :tag "Always" t)
  1480. (const :tag "Prompt for confirmation" confirm)))
  1481. (defgroup org-todo nil
  1482. "Options concerning TODO items in Org-mode."
  1483. :tag "Org TODO"
  1484. :group 'org)
  1485. (defgroup org-progress nil
  1486. "Options concerning Progress logging in Org-mode."
  1487. :tag "Org Progress"
  1488. :group 'org-time)
  1489. (defvar org-todo-interpretation-widgets
  1490. '(
  1491. (:tag "Sequence (cycling hits every state)" sequence)
  1492. (:tag "Type (cycling directly to DONE)" type))
  1493. "The available interpretation symbols for customizing
  1494. `org-todo-keywords'.
  1495. Interested libraries should add to this list.")
  1496. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1497. "List of TODO entry keyword sequences and their interpretation.
  1498. \\<org-mode-map>This is a list of sequences.
  1499. Each sequence starts with a symbol, either `sequence' or `type',
  1500. indicating if the keywords should be interpreted as a sequence of
  1501. action steps, or as different types of TODO items. The first
  1502. keywords are states requiring action - these states will select a headline
  1503. for inclusion into the global TODO list Org-mode produces. If one of
  1504. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1505. signify that no further action is necessary. If \"|\" is not found,
  1506. the last keyword is treated as the only DONE state of the sequence.
  1507. The command \\[org-todo] cycles an entry through these states, and one
  1508. additional state where no keyword is present. For details about this
  1509. cycling, see the manual.
  1510. TODO keywords and interpretation can also be set on a per-file basis with
  1511. the special #+SEQ_TODO and #+TYP_TODO lines.
  1512. Each keyword can optionally specify a character for fast state selection
  1513. \(in combination with the variable `org-use-fast-todo-selection')
  1514. and specifiers for state change logging, using the same syntax
  1515. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1516. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1517. indicates to record a time stamp each time this state is selected.
  1518. Each keyword may also specify if a timestamp or a note should be
  1519. recorded when entering or leaving the state, by adding additional
  1520. characters in the parenthesis after the keyword. This looks like this:
  1521. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1522. record only the time of the state change. With X and Y being either
  1523. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1524. Y when leaving the state if and only if the *target* state does not
  1525. define X. You may omit any of the fast-selection key or X or /Y,
  1526. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1527. For backward compatibility, this variable may also be just a list
  1528. of keywords - in this case the interpretation (sequence or type) will be
  1529. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1530. :group 'org-todo
  1531. :group 'org-keywords
  1532. :type '(choice
  1533. (repeat :tag "Old syntax, just keywords"
  1534. (string :tag "Keyword"))
  1535. (repeat :tag "New syntax"
  1536. (cons
  1537. (choice
  1538. :tag "Interpretation"
  1539. ;;Quick and dirty way to see
  1540. ;;`org-todo-interpretations'. This takes the
  1541. ;;place of item arguments
  1542. :convert-widget
  1543. (lambda (widget)
  1544. (widget-put widget
  1545. :args (mapcar
  1546. #'(lambda (x)
  1547. (widget-convert
  1548. (cons 'const x)))
  1549. org-todo-interpretation-widgets))
  1550. widget))
  1551. (repeat
  1552. (string :tag "Keyword"))))))
  1553. (defvar org-todo-keywords-1 nil
  1554. "All TODO and DONE keywords active in a buffer.")
  1555. (make-variable-buffer-local 'org-todo-keywords-1)
  1556. (defvar org-todo-keywords-for-agenda nil)
  1557. (defvar org-done-keywords-for-agenda nil)
  1558. (defvar org-drawers-for-agenda nil)
  1559. (defvar org-todo-keyword-alist-for-agenda nil)
  1560. (defvar org-tag-alist-for-agenda nil)
  1561. (defvar org-agenda-contributing-files nil)
  1562. (defvar org-not-done-keywords nil)
  1563. (make-variable-buffer-local 'org-not-done-keywords)
  1564. (defvar org-done-keywords nil)
  1565. (make-variable-buffer-local 'org-done-keywords)
  1566. (defvar org-todo-heads nil)
  1567. (make-variable-buffer-local 'org-todo-heads)
  1568. (defvar org-todo-sets nil)
  1569. (make-variable-buffer-local 'org-todo-sets)
  1570. (defvar org-todo-log-states nil)
  1571. (make-variable-buffer-local 'org-todo-log-states)
  1572. (defvar org-todo-kwd-alist nil)
  1573. (make-variable-buffer-local 'org-todo-kwd-alist)
  1574. (defvar org-todo-key-alist nil)
  1575. (make-variable-buffer-local 'org-todo-key-alist)
  1576. (defvar org-todo-key-trigger nil)
  1577. (make-variable-buffer-local 'org-todo-key-trigger)
  1578. (defcustom org-todo-interpretation 'sequence
  1579. "Controls how TODO keywords are interpreted.
  1580. This variable is in principle obsolete and is only used for
  1581. backward compatibility, if the interpretation of todo keywords is
  1582. not given already in `org-todo-keywords'. See that variable for
  1583. more information."
  1584. :group 'org-todo
  1585. :group 'org-keywords
  1586. :type '(choice (const sequence)
  1587. (const type)))
  1588. (defcustom org-use-fast-todo-selection t
  1589. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1590. This variable describes if and under what circumstances the cycling
  1591. mechanism for TODO keywords will be replaced by a single-key, direct
  1592. selection scheme.
  1593. When nil, fast selection is never used.
  1594. When the symbol `prefix', it will be used when `org-todo' is called with
  1595. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1596. in an agenda buffer.
  1597. When t, fast selection is used by default. In this case, the prefix
  1598. argument forces cycling instead.
  1599. In all cases, the special interface is only used if access keys have actually
  1600. been assigned by the user, i.e. if keywords in the configuration are followed
  1601. by a letter in parenthesis, like TODO(t)."
  1602. :group 'org-todo
  1603. :type '(choice
  1604. (const :tag "Never" nil)
  1605. (const :tag "By default" t)
  1606. (const :tag "Only with C-u C-c C-t" prefix)))
  1607. (defcustom org-provide-todo-statistics t
  1608. "Non-nil means, update todo statistics after insert and toggle.
  1609. ALL-HEADLINES means update todo statistics by including headlines
  1610. with no TODO keyword as well, counting them as not done.
  1611. A list of TODO keywords means the same, but skip keywords that are
  1612. not in this list.
  1613. When this is set, todo statistics is updated in the parent of the
  1614. current entry each time a todo state is changed."
  1615. :group 'org-todo
  1616. :type '(choice
  1617. (const :tag "Yes, only for TODO entries" t)
  1618. (const :tag "Yes, including all entries" 'all-headlines)
  1619. (repeat :tag "Yes, for TODOs in this list"
  1620. (string :tag "TODO keyword"))
  1621. (other :tag "No TODO statistics" nil)))
  1622. (defcustom org-hierarchical-todo-statistics t
  1623. "Non-nil means, TODO statistics covers just direct children.
  1624. When nil, all entries in the subtree are considered.
  1625. This has only an effect if `org-provide-todo-statistics' is set.
  1626. To set this to nil for only a single subtree, use a COOKIE_DATA
  1627. property and include the word \"recursive\" into the value."
  1628. :group 'org-todo
  1629. :type 'boolean)
  1630. (defcustom org-after-todo-state-change-hook nil
  1631. "Hook which is run after the state of a TODO item was changed.
  1632. The new state (a string with a TODO keyword, or nil) is available in the
  1633. Lisp variable `state'."
  1634. :group 'org-todo
  1635. :type 'hook)
  1636. (defvar org-blocker-hook nil
  1637. "Hook for functions that are allowed to block a state change.
  1638. Each function gets as its single argument a property list, see
  1639. `org-trigger-hook' for more information about this list.
  1640. If any of the functions in this hook returns nil, the state change
  1641. is blocked.")
  1642. (defvar org-trigger-hook nil
  1643. "Hook for functions that are triggered by a state change.
  1644. Each function gets as its single argument a property list with at least
  1645. the following elements:
  1646. (:type type-of-change :position pos-at-entry-start
  1647. :from old-state :to new-state)
  1648. Depending on the type, more properties may be present.
  1649. This mechanism is currently implemented for:
  1650. TODO state changes
  1651. ------------------
  1652. :type todo-state-change
  1653. :from previous state (keyword as a string), or nil, or a symbol
  1654. 'todo' or 'done', to indicate the general type of state.
  1655. :to new state, like in :from")
  1656. (defcustom org-enforce-todo-dependencies nil
  1657. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1658. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1659. be blocked if any prior sibling is not yet done.
  1660. Finally, if the parent is blocked because of ordered siblings of its own,
  1661. the child will also be blocked.
  1662. This variable needs to be set before org.el is loaded, and you need to
  1663. restart Emacs after a change to make the change effective. The only way
  1664. to change is while Emacs is running is through the customize interface."
  1665. :set (lambda (var val)
  1666. (set var val)
  1667. (if val
  1668. (add-hook 'org-blocker-hook
  1669. 'org-block-todo-from-children-or-siblings-or-parent)
  1670. (remove-hook 'org-blocker-hook
  1671. 'org-block-todo-from-children-or-siblings-or-parent)))
  1672. :group 'org-todo
  1673. :type 'boolean)
  1674. (defcustom org-enforce-todo-checkbox-dependencies nil
  1675. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1676. When this is nil, checkboxes have no influence on switching TODO states.
  1677. When non-nil, you first need to check off all check boxes before the TODO
  1678. entry can be switched to DONE.
  1679. This variable needs to be set before org.el is loaded, and you need to
  1680. restart Emacs after a change to make the change effective. The only way
  1681. to change is while Emacs is running is through the customize interface."
  1682. :set (lambda (var val)
  1683. (set var val)
  1684. (if val
  1685. (add-hook 'org-blocker-hook
  1686. 'org-block-todo-from-checkboxes)
  1687. (remove-hook 'org-blocker-hook
  1688. 'org-block-todo-from-checkboxes)))
  1689. :group 'org-todo
  1690. :type 'boolean)
  1691. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1692. "Non-nil means, inserting a TODO heading is treated as state change.
  1693. So when the command \\[org-insert-todo-heading] is used, state change
  1694. logging will apply if appropriate. When nil, the new TODO item will
  1695. be inserted directly, and no logging will take place."
  1696. :group 'org-todo
  1697. :type 'boolean)
  1698. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1699. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1700. This is the default behavior. However, setting this to nil allows a
  1701. convenient way to select a TODO state and bypass any logging associated
  1702. with that."
  1703. :group 'org-todo
  1704. :type 'boolean)
  1705. (defcustom org-todo-state-tags-triggers nil
  1706. "Tag changes that should be triggered by TODO state changes.
  1707. This is a list. Each entry is
  1708. (state-change (tag . flag) .......)
  1709. State-change can be a string with a state, and empty string to indicate the
  1710. state that has no TODO keyword, or it can be one of the symbols `todo'
  1711. or `done', meaning any not-done or done state, respectively."
  1712. :group 'org-todo
  1713. :group 'org-tags
  1714. :type '(repeat
  1715. (cons (choice :tag "When changing to"
  1716. (const :tag "Not-done state" todo)
  1717. (const :tag "Done state" done)
  1718. (string :tag "State"))
  1719. (repeat
  1720. (cons :tag "Tag action"
  1721. (string :tag "Tag")
  1722. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1723. (defcustom org-log-done nil
  1724. "Information to record when a task moves to the DONE state.
  1725. Possible values are:
  1726. nil Don't add anything, just change the keyword
  1727. time Add a time stamp to the task
  1728. note Prompt for a note and add it with template `org-log-note-headings'
  1729. This option can also be set with on a per-file-basis with
  1730. #+STARTUP: nologdone
  1731. #+STARTUP: logdone
  1732. #+STARTUP: lognotedone
  1733. You can have local logging settings for a subtree by setting the LOGGING
  1734. property to one or more of these keywords."
  1735. :group 'org-todo
  1736. :group 'org-progress
  1737. :type '(choice
  1738. (const :tag "No logging" nil)
  1739. (const :tag "Record CLOSED timestamp" time)
  1740. (const :tag "Record CLOSED timestamp with note." note)))
  1741. ;; Normalize old uses of org-log-done.
  1742. (cond
  1743. ((eq org-log-done t) (setq org-log-done 'time))
  1744. ((and (listp org-log-done) (memq 'done org-log-done))
  1745. (setq org-log-done 'note)))
  1746. (defcustom org-log-reschedule nil
  1747. "Information to record when the scheduling date of a tasks is modified.
  1748. Possible values are:
  1749. nil Don't add anything, just change the date
  1750. time Add a time stamp to the task
  1751. note Prompt for a note and add it with template `org-log-note-headings'
  1752. This option can also be set with on a per-file-basis with
  1753. #+STARTUP: nologreschedule
  1754. #+STARTUP: logreschedule
  1755. #+STARTUP: lognotereschedule"
  1756. :group 'org-todo
  1757. :group 'org-progress
  1758. :type '(choice
  1759. (const :tag "No logging" nil)
  1760. (const :tag "Record timestamp" time)
  1761. (const :tag "Record timestamp with note." note)))
  1762. (defcustom org-log-redeadline nil
  1763. "Information to record when the deadline date of a tasks is modified.
  1764. Possible values are:
  1765. nil Don't add anything, just change the date
  1766. time Add a time stamp to the task
  1767. note Prompt for a note and add it with template `org-log-note-headings'
  1768. This option can also be set with on a per-file-basis with
  1769. #+STARTUP: nologredeadline
  1770. #+STARTUP: logredeadline
  1771. #+STARTUP: lognoteredeadline
  1772. You can have local logging settings for a subtree by setting the LOGGING
  1773. property to one or more of these keywords."
  1774. :group 'org-todo
  1775. :group 'org-progress
  1776. :type '(choice
  1777. (const :tag "No logging" nil)
  1778. (const :tag "Record timestamp" time)
  1779. (const :tag "Record timestamp with note." note)))
  1780. (defcustom org-log-note-clock-out nil
  1781. "Non-nil means, record a note when clocking out of an item.
  1782. This can also be configured on a per-file basis by adding one of
  1783. the following lines anywhere in the buffer:
  1784. #+STARTUP: lognoteclock-out
  1785. #+STARTUP: nolognoteclock-out"
  1786. :group 'org-todo
  1787. :group 'org-progress
  1788. :type 'boolean)
  1789. (defcustom org-log-done-with-time t
  1790. "Non-nil means, the CLOSED time stamp will contain date and time.
  1791. When nil, only the date will be recorded."
  1792. :group 'org-progress
  1793. :type 'boolean)
  1794. (defcustom org-log-note-headings
  1795. '((done . "CLOSING NOTE %t")
  1796. (state . "State %-12s from %-12S %t")
  1797. (note . "Note taken on %t")
  1798. (reschedule . "Rescheduled from %S on %t")
  1799. (delschedule . "Not scheduled, was %S on %t")
  1800. (redeadline . "New deadline from %S on %t")
  1801. (deldeadline . "Removed deadline, was %S on %t")
  1802. (clock-out . ""))
  1803. "Headings for notes added to entries.
  1804. The value is an alist, with the car being a symbol indicating the note
  1805. context, and the cdr is the heading to be used. The heading may also be the
  1806. empty string.
  1807. %t in the heading will be replaced by a time stamp.
  1808. %s will be replaced by the new TODO state, in double quotes.
  1809. %S will be replaced by the old TODO state, in double quotes.
  1810. %u will be replaced by the user name.
  1811. %U will be replaced by the full user name.
  1812. In fact, it is not a good idea to change the `state' entry, because
  1813. agenda log mode depends on the format of these entries."
  1814. :group 'org-todo
  1815. :group 'org-progress
  1816. :type '(list :greedy t
  1817. (cons (const :tag "Heading when closing an item" done) string)
  1818. (cons (const :tag
  1819. "Heading when changing todo state (todo sequence only)"
  1820. state) string)
  1821. (cons (const :tag "Heading when just taking a note" note) string)
  1822. (cons (const :tag "Heading when clocking out" clock-out) string)
  1823. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1824. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1825. (cons (const :tag "Heading when changing deadline" redeadline) string
  1826. (cons (const :tag "Heading when deleting a deadline" deldeadline) string))))
  1827. (unless (assq 'note org-log-note-headings)
  1828. (push '(note . "%t") org-log-note-headings))
  1829. (defcustom org-log-into-drawer nil
  1830. "Non-nil means, insert state change notes and time stamps into a drawer.
  1831. When nil, state changes notes will be inserted after the headline and
  1832. any scheduling and clock lines, but not inside a drawer.
  1833. The value of this variable should be the name of the drawer to use.
  1834. LOGBOOK is proposed at the default drawer for this purpose, you can
  1835. also set this to a string to define the drawer of your choice.
  1836. A value of t is also allowed, representing \"LOGBOOK\".
  1837. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1838. will be ignored.
  1839. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1840. a subtree."
  1841. :group 'org-todo
  1842. :group 'org-progress
  1843. :type '(choice
  1844. (const :tag "Not into a drawer" nil)
  1845. (const :tag "LOGBOOK" t)
  1846. (string :tag "Other")))
  1847. (if (fboundp 'defvaralias)
  1848. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1849. (defun org-log-into-drawer ()
  1850. "Return the value of `org-log-into-drawer', but let properties overrule.
  1851. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1852. used instead of the default value."
  1853. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1854. (cond
  1855. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1856. ((equal p "t") "LOGBOOK")
  1857. (t p))))
  1858. (defcustom org-log-state-notes-insert-after-drawers nil
  1859. "Non-nil means, insert state change notes after any drawers in entry.
  1860. Only the drawers that *immediately* follow the headline and the
  1861. deadline/scheduled line are skipped.
  1862. When nil, insert notes right after the heading and perhaps the line
  1863. with deadline/scheduling if present.
  1864. This variable will have no effect if `org-log-into-drawer' is
  1865. set."
  1866. :group 'org-todo
  1867. :group 'org-progress
  1868. :type 'boolean)
  1869. (defcustom org-log-states-order-reversed t
  1870. "Non-nil means, the latest state change note will be directly after heading.
  1871. When nil, the notes will be orderer according to time."
  1872. :group 'org-todo
  1873. :group 'org-progress
  1874. :type 'boolean)
  1875. (defcustom org-log-repeat 'time
  1876. "Non-nil means, record moving through the DONE state when triggering repeat.
  1877. An auto-repeating task is immediately switched back to TODO when
  1878. marked DONE. If you are not logging state changes (by adding \"@\"
  1879. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1880. record a closing note, there will be no record of the task moving
  1881. through DONE. This variable forces taking a note anyway.
  1882. nil Don't force a record
  1883. time Record a time stamp
  1884. note Record a note
  1885. This option can also be set with on a per-file-basis with
  1886. #+STARTUP: logrepeat
  1887. #+STARTUP: lognoterepeat
  1888. #+STARTUP: nologrepeat
  1889. You can have local logging settings for a subtree by setting the LOGGING
  1890. property to one or more of these keywords."
  1891. :group 'org-todo
  1892. :group 'org-progress
  1893. :type '(choice
  1894. (const :tag "Don't force a record" nil)
  1895. (const :tag "Force recording the DONE state" time)
  1896. (const :tag "Force recording a note with the DONE state" note)))
  1897. (defgroup org-priorities nil
  1898. "Priorities in Org-mode."
  1899. :tag "Org Priorities"
  1900. :group 'org-todo)
  1901. (defcustom org-enable-priority-commands t
  1902. "Non-nil means, priority commands are active.
  1903. When nil, these commands will be disabled, so that you never accidentally
  1904. set a priority."
  1905. :group 'org-priorities
  1906. :type 'boolean)
  1907. (defcustom org-highest-priority ?A
  1908. "The highest priority of TODO items. A character like ?A, ?B etc.
  1909. Must have a smaller ASCII number than `org-lowest-priority'."
  1910. :group 'org-priorities
  1911. :type 'character)
  1912. (defcustom org-lowest-priority ?C
  1913. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1914. Must have a larger ASCII number than `org-highest-priority'."
  1915. :group 'org-priorities
  1916. :type 'character)
  1917. (defcustom org-default-priority ?B
  1918. "The default priority of TODO items.
  1919. This is the priority an item get if no explicit priority is given."
  1920. :group 'org-priorities
  1921. :type 'character)
  1922. (defcustom org-priority-start-cycle-with-default t
  1923. "Non-nil means, start with default priority when starting to cycle.
  1924. When this is nil, the first step in the cycle will be (depending on the
  1925. command used) one higher or lower that the default priority."
  1926. :group 'org-priorities
  1927. :type 'boolean)
  1928. (defgroup org-time nil
  1929. "Options concerning time stamps and deadlines in Org-mode."
  1930. :tag "Org Time"
  1931. :group 'org)
  1932. (defcustom org-insert-labeled-timestamps-at-point nil
  1933. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1934. When nil, these labeled time stamps are forces into the second line of an
  1935. entry, just after the headline. When scheduling from the global TODO list,
  1936. the time stamp will always be forced into the second line."
  1937. :group 'org-time
  1938. :type 'boolean)
  1939. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1940. "Formats for `format-time-string' which are used for time stamps.
  1941. It is not recommended to change this constant.")
  1942. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1943. "Number of minutes to round time stamps to.
  1944. These are two values, the first applies when first creating a time stamp.
  1945. The second applies when changing it with the commands `S-up' and `S-down'.
  1946. When changing the time stamp, this means that it will change in steps
  1947. of N minutes, as given by the second value.
  1948. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1949. numbers should be factors of 60, so for example 5, 10, 15.
  1950. When this is larger than 1, you can still force an exact time-stamp by using
  1951. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1952. and by using a prefix arg to `S-up/down' to specify the exact number
  1953. of minutes to shift."
  1954. :group 'org-time
  1955. :get '(lambda (var) ; Make sure all entries have 5 elements
  1956. (if (integerp (default-value var))
  1957. (list (default-value var) 5)
  1958. (default-value var)))
  1959. :type '(list
  1960. (integer :tag "when inserting times")
  1961. (integer :tag "when modifying times")))
  1962. ;; Normalize old customizations of this variable.
  1963. (when (integerp org-time-stamp-rounding-minutes)
  1964. (setq org-time-stamp-rounding-minutes
  1965. (list org-time-stamp-rounding-minutes
  1966. org-time-stamp-rounding-minutes)))
  1967. (defcustom org-display-custom-times nil
  1968. "Non-nil means, overlay custom formats over all time stamps.
  1969. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1970. To turn this on on a per-file basis, insert anywhere in the file:
  1971. #+STARTUP: customtime"
  1972. :group 'org-time
  1973. :set 'set-default
  1974. :type 'sexp)
  1975. (make-variable-buffer-local 'org-display-custom-times)
  1976. (defcustom org-time-stamp-custom-formats
  1977. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1978. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1979. These are overlayed over the default ISO format if the variable
  1980. `org-display-custom-times' is set. Time like %H:%M should be at the
  1981. end of the second format. The custom formats are also honored by export
  1982. commands, if custom time display is turned on at the time of export."
  1983. :group 'org-time
  1984. :type 'sexp)
  1985. (defun org-time-stamp-format (&optional long inactive)
  1986. "Get the right format for a time string."
  1987. (let ((f (if long (cdr org-time-stamp-formats)
  1988. (car org-time-stamp-formats))))
  1989. (if inactive
  1990. (concat "[" (substring f 1 -1) "]")
  1991. f)))
  1992. (defcustom org-time-clocksum-format "%d:%02d"
  1993. "The format string used when creating CLOCKSUM lines, or when
  1994. org-mode generates a time duration."
  1995. :group 'org-time
  1996. :type 'string)
  1997. (defcustom org-time-clocksum-use-fractional nil
  1998. "If non-nil, \\[org-clock-display] uses fractional times.
  1999. org-mode generates a time duration."
  2000. :group 'org-time
  2001. :type 'boolean)
  2002. (defcustom org-time-clocksum-fractional-format "%.2f"
  2003. "The format string used when creating CLOCKSUM lines, or when
  2004. org-mode generates a time duration."
  2005. :group 'org-time
  2006. :type 'string)
  2007. (defcustom org-deadline-warning-days 14
  2008. "No. of days before expiration during which a deadline becomes active.
  2009. This variable governs the display in sparse trees and in the agenda.
  2010. When 0 or negative, it means use this number (the absolute value of it)
  2011. even if a deadline has a different individual lead time specified.
  2012. Custom commands can set this variable in the options section."
  2013. :group 'org-time
  2014. :group 'org-agenda-daily/weekly
  2015. :type 'integer)
  2016. (defcustom org-read-date-prefer-future t
  2017. "Non-nil means, assume future for incomplete date input from user.
  2018. This affects the following situations:
  2019. 1. The user gives a month but not a year.
  2020. For example, if it is april and you enter \"feb 2\", this will be read
  2021. as feb 2, *next* year. \"May 5\", however, will be this year.
  2022. 2. The user gives a day, but no month.
  2023. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2024. read this as the third of *next* month. However, if you enter \"17\",
  2025. it will be considered as *this* month.
  2026. If you set this variable to the symbol `time', then also the following
  2027. will work:
  2028. 3. If the user gives a time, but no day. If the time is before now,
  2029. to will be interpreted as tomorrow.
  2030. Currently none of this works for ISO week specifications.
  2031. When this option is nil, the current day, month and year will always be
  2032. used as defaults."
  2033. :group 'org-time
  2034. :type '(choice
  2035. (const :tag "Never" nil)
  2036. (const :tag "Check month and day" t)
  2037. (const :tag "Check month, day, and time" time)))
  2038. (defcustom org-read-date-display-live t
  2039. "Non-nil means, display current interpretation of date prompt live.
  2040. This display will be in an overlay, in the minibuffer."
  2041. :group 'org-time
  2042. :type 'boolean)
  2043. (defcustom org-read-date-popup-calendar t
  2044. "Non-nil means, pop up a calendar when prompting for a date.
  2045. In the calendar, the date can be selected with mouse-1. However, the
  2046. minibuffer will also be active, and you can simply enter the date as well.
  2047. When nil, only the minibuffer will be available."
  2048. :group 'org-time
  2049. :type 'boolean)
  2050. (if (fboundp 'defvaralias)
  2051. (defvaralias 'org-popup-calendar-for-date-prompt
  2052. 'org-read-date-popup-calendar))
  2053. (defcustom org-read-date-minibuffer-setup-hook nil
  2054. "Hook to be used to set up keys for the date/time interface.
  2055. Add key definitions to `minibuffer-local-map', which will be a temporary
  2056. copy."
  2057. :group 'org-time
  2058. :type 'hook)
  2059. (defcustom org-extend-today-until 0
  2060. "The hour when your day really ends. Must be an integer.
  2061. This has influence for the following applications:
  2062. - When switching the agenda to \"today\". It it is still earlier than
  2063. the time given here, the day recognized as TODAY is actually yesterday.
  2064. - When a date is read from the user and it is still before the time given
  2065. here, the current date and time will be assumed to be yesterday, 23:59.
  2066. Also, timestamps inserted in remember templates follow this rule.
  2067. IMPORTANT: This is a feature whose implementation is and likely will
  2068. remain incomplete. Really, it is only here because past midnight seems to
  2069. be the favorite working time of John Wiegley :-)"
  2070. :group 'org-time
  2071. :type 'integer)
  2072. (defcustom org-edit-timestamp-down-means-later nil
  2073. "Non-nil means, S-down will increase the time in a time stamp.
  2074. When nil, S-up will increase."
  2075. :group 'org-time
  2076. :type 'boolean)
  2077. (defcustom org-calendar-follow-timestamp-change t
  2078. "Non-nil means, make the calendar window follow timestamp changes.
  2079. When a timestamp is modified and the calendar window is visible, it will be
  2080. moved to the new date."
  2081. :group 'org-time
  2082. :type 'boolean)
  2083. (defgroup org-tags nil
  2084. "Options concerning tags in Org-mode."
  2085. :tag "Org Tags"
  2086. :group 'org)
  2087. (defcustom org-tag-alist nil
  2088. "List of tags allowed in Org-mode files.
  2089. When this list is nil, Org-mode will base TAG input on what is already in the
  2090. buffer.
  2091. The value of this variable is an alist, the car of each entry must be a
  2092. keyword as a string, the cdr may be a character that is used to select
  2093. that tag through the fast-tag-selection interface.
  2094. See the manual for details."
  2095. :group 'org-tags
  2096. :type '(repeat
  2097. (choice
  2098. (cons (string :tag "Tag name")
  2099. (character :tag "Access char"))
  2100. (list :tag "Start radio group"
  2101. (const :startgroup)
  2102. (option (string :tag "Group description")))
  2103. (list :tag "End radio group"
  2104. (const :endgroup)
  2105. (option (string :tag "Group description")))
  2106. (const :tag "New line" (:newline)))))
  2107. (defcustom org-tag-persistent-alist nil
  2108. "List of tags that will always appear in all Org-mode files.
  2109. This is in addition to any in buffer settings or customizations
  2110. of `org-tag-alist'.
  2111. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2112. The value of this variable is an alist, the car of each entry must be a
  2113. keyword as a string, the cdr may be a character that is used to select
  2114. that tag through the fast-tag-selection interface.
  2115. See the manual for details.
  2116. To disable these tags on a per-file basis, insert anywhere in the file:
  2117. #+STARTUP: noptag"
  2118. :group 'org-tags
  2119. :type '(repeat
  2120. (choice
  2121. (cons (string :tag "Tag name")
  2122. (character :tag "Access char"))
  2123. (const :tag "Start radio group" (:startgroup))
  2124. (const :tag "End radio group" (:endgroup))
  2125. (const :tag "New line" (:newline)))))
  2126. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2127. "If non-nil, always offer completion for all tags of all agenda files.
  2128. Instead of customizing this variable directly, you might want to
  2129. set it locally for remember buffers, because there no list of
  2130. tags in that file can be created dynamically (there are none).
  2131. (add-hook 'org-remember-mode-hook
  2132. (lambda ()
  2133. (set (make-local-variable
  2134. 'org-complete-tags-always-offer-all-agenda-tags)
  2135. t)))"
  2136. :group 'org-tags
  2137. :type 'boolean)
  2138. (defvar org-file-tags nil
  2139. "List of tags that can be inherited by all entries in the file.
  2140. The tags will be inherited if the variable `org-use-tag-inheritance'
  2141. says they should be.
  2142. This variable is populated from #+FILETAGS lines.")
  2143. (defcustom org-use-fast-tag-selection 'auto
  2144. "Non-nil means, use fast tag selection scheme.
  2145. This is a special interface to select and deselect tags with single keys.
  2146. When nil, fast selection is never used.
  2147. When the symbol `auto', fast selection is used if and only if selection
  2148. characters for tags have been configured, either through the variable
  2149. `org-tag-alist' or through a #+TAGS line in the buffer.
  2150. When t, fast selection is always used and selection keys are assigned
  2151. automatically if necessary."
  2152. :group 'org-tags
  2153. :type '(choice
  2154. (const :tag "Always" t)
  2155. (const :tag "Never" nil)
  2156. (const :tag "When selection characters are configured" 'auto)))
  2157. (defcustom org-fast-tag-selection-single-key nil
  2158. "Non-nil means, fast tag selection exits after first change.
  2159. When nil, you have to press RET to exit it.
  2160. During fast tag selection, you can toggle this flag with `C-c'.
  2161. This variable can also have the value `expert'. In this case, the window
  2162. displaying the tags menu is not even shown, until you press C-c again."
  2163. :group 'org-tags
  2164. :type '(choice
  2165. (const :tag "No" nil)
  2166. (const :tag "Yes" t)
  2167. (const :tag "Expert" expert)))
  2168. (defvar org-fast-tag-selection-include-todo nil
  2169. "Non-nil means, fast tags selection interface will also offer TODO states.
  2170. This is an undocumented feature, you should not rely on it.")
  2171. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2172. "The column to which tags should be indented in a headline.
  2173. If this number is positive, it specifies the column. If it is negative,
  2174. it means that the tags should be flushright to that column. For example,
  2175. -80 works well for a normal 80 character screen."
  2176. :group 'org-tags
  2177. :type 'integer)
  2178. (defcustom org-auto-align-tags t
  2179. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2180. These operations change the length of a headline and therefore shift
  2181. the tags around. With this options turned on, after each such operation
  2182. the tags are again aligned to `org-tags-column'."
  2183. :group 'org-tags
  2184. :type 'boolean)
  2185. (defcustom org-use-tag-inheritance t
  2186. "Non-nil means, tags in levels apply also for sublevels.
  2187. When nil, only the tags directly given in a specific line apply there.
  2188. This may also be a list of tags that should be inherited, or a regexp that
  2189. matches tags that should be inherited. Additional control is possible
  2190. with the variable `org-tags-exclude-from-inheritance' which gives an
  2191. explicit list of tags to be excluded from inheritance., even if the value of
  2192. `org-use-tag-inheritance' would select it for inheritance.
  2193. If this option is t, a match early-on in a tree can lead to a large
  2194. number of matches in the subtree when constructing the agenda or creating
  2195. a sparse tree. If you only want to see the first match in a tree during
  2196. a search, check out the variable `org-tags-match-list-sublevels'."
  2197. :group 'org-tags
  2198. :type '(choice
  2199. (const :tag "Not" nil)
  2200. (const :tag "Always" t)
  2201. (repeat :tag "Specific tags" (string :tag "Tag"))
  2202. (regexp :tag "Tags matched by regexp")))
  2203. (defcustom org-tags-exclude-from-inheritance nil
  2204. "List of tags that should never be inherited.
  2205. This is a way to exclude a few tags from inheritance. For way to do
  2206. the opposite, to actively allow inheritance for selected tags,
  2207. see the variable `org-use-tag-inheritance'."
  2208. :group 'org-tags
  2209. :type '(repeat (string :tag "Tag")))
  2210. (defun org-tag-inherit-p (tag)
  2211. "Check if TAG is one that should be inherited."
  2212. (cond
  2213. ((member tag org-tags-exclude-from-inheritance) nil)
  2214. ((eq org-use-tag-inheritance t) t)
  2215. ((not org-use-tag-inheritance) nil)
  2216. ((stringp org-use-tag-inheritance)
  2217. (string-match org-use-tag-inheritance tag))
  2218. ((listp org-use-tag-inheritance)
  2219. (member tag org-use-tag-inheritance))
  2220. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2221. (defcustom org-tags-match-list-sublevels t
  2222. "Non-nil means list also sublevels of headlines matching a search.
  2223. This variable applies to tags/property searches, and also to stuck
  2224. projects because this search is based on a tags match as well.
  2225. When set to the symbol `indented', sublevels are indented with
  2226. leading dots.
  2227. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2228. the sublevels of a headline matching a tag search often also match
  2229. the same search. Listing all of them can create very long lists.
  2230. Setting this variable to nil causes subtrees of a match to be skipped.
  2231. This variable is semi-obsolete and probably should always be true. It
  2232. is better to limit inheritance to certain tags using the variables
  2233. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2234. :group 'org-tags
  2235. :type '(choice
  2236. (const :tag "No, don't list them" nil)
  2237. (const :tag "Yes, do list them" t)
  2238. (const :tag "List them, indented with leading dots" indented)))
  2239. (defcustom org-tags-sort-function nil
  2240. "When set, tags are sorted using this function as a comparator"
  2241. :group 'org-tags
  2242. :type '(choice
  2243. (const :tag "No sorting" nil)
  2244. (const :tag "Alphabetical" string<)
  2245. (const :tag "Reverse alphabetical" string>)
  2246. (function :tag "Custom function" nil)))
  2247. (defvar org-tags-history nil
  2248. "History of minibuffer reads for tags.")
  2249. (defvar org-last-tags-completion-table nil
  2250. "The last used completion table for tags.")
  2251. (defvar org-after-tags-change-hook nil
  2252. "Hook that is run after the tags in a line have changed.")
  2253. (defgroup org-properties nil
  2254. "Options concerning properties in Org-mode."
  2255. :tag "Org Properties"
  2256. :group 'org)
  2257. (defcustom org-property-format "%-10s %s"
  2258. "How property key/value pairs should be formatted by `indent-line'.
  2259. When `indent-line' hits a property definition, it will format the line
  2260. according to this format, mainly to make sure that the values are
  2261. lined-up with respect to each other."
  2262. :group 'org-properties
  2263. :type 'string)
  2264. (defcustom org-use-property-inheritance nil
  2265. "Non-nil means, properties apply also for sublevels.
  2266. This setting is chiefly used during property searches. Turning it on can
  2267. cause significant overhead when doing a search, which is why it is not
  2268. on by default.
  2269. When nil, only the properties directly given in the current entry count.
  2270. When t, every property is inherited. The value may also be a list of
  2271. properties that should have inheritance, or a regular expression matching
  2272. properties that should be inherited.
  2273. However, note that some special properties use inheritance under special
  2274. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2275. and the properties ending in \"_ALL\" when they are used as descriptor
  2276. for valid values of a property.
  2277. Note for programmers:
  2278. When querying an entry with `org-entry-get', you can control if inheritance
  2279. should be used. By default, `org-entry-get' looks only at the local
  2280. properties. You can request inheritance by setting the inherit argument
  2281. to t (to force inheritance) or to `selective' (to respect the setting
  2282. in this variable)."
  2283. :group 'org-properties
  2284. :type '(choice
  2285. (const :tag "Not" nil)
  2286. (const :tag "Always" t)
  2287. (repeat :tag "Specific properties" (string :tag "Property"))
  2288. (regexp :tag "Properties matched by regexp")))
  2289. (defun org-property-inherit-p (property)
  2290. "Check if PROPERTY is one that should be inherited."
  2291. (cond
  2292. ((eq org-use-property-inheritance t) t)
  2293. ((not org-use-property-inheritance) nil)
  2294. ((stringp org-use-property-inheritance)
  2295. (string-match org-use-property-inheritance property))
  2296. ((listp org-use-property-inheritance)
  2297. (member property org-use-property-inheritance))
  2298. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2299. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2300. "The default column format, if no other format has been defined.
  2301. This variable can be set on the per-file basis by inserting a line
  2302. #+COLUMNS: %25ITEM ....."
  2303. :group 'org-properties
  2304. :type 'string)
  2305. (defcustom org-columns-ellipses ".."
  2306. "The ellipses to be used when a field in column view is truncated.
  2307. When this is the empty string, as many characters as possible are shown,
  2308. but then there will be no visual indication that the field has been truncated.
  2309. When this is a string of length N, the last N characters of a truncated
  2310. field are replaced by this string. If the column is narrower than the
  2311. ellipses string, only part of the ellipses string will be shown."
  2312. :group 'org-properties
  2313. :type 'string)
  2314. (defcustom org-columns-modify-value-for-display-function nil
  2315. "Function that modifies values for display in column view.
  2316. For example, it can be used to cut out a certain part from a time stamp.
  2317. The function must take 2 arguments:
  2318. column-title The title of the column (*not* the property name)
  2319. value The value that should be modified.
  2320. The function should return the value that should be displayed,
  2321. or nil if the normal value should be used."
  2322. :group 'org-properties
  2323. :type 'function)
  2324. (defcustom org-effort-property "Effort"
  2325. "The property that is being used to keep track of effort estimates.
  2326. Effort estimates given in this property need to have the format H:MM."
  2327. :group 'org-properties
  2328. :group 'org-progress
  2329. :type '(string :tag "Property"))
  2330. (defconst org-global-properties-fixed
  2331. '(("VISIBILITY_ALL" . "folded children content all")
  2332. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2333. "List of property/value pairs that can be inherited by any entry.
  2334. These are fixed values, for the preset properties. The user variable
  2335. that can be used to add to this list is `org-global-properties'.
  2336. The entries in this list are cons cells where the car is a property
  2337. name and cdr is a string with the value. If the value represents
  2338. multiple items like an \"_ALL\" property, separate the items by
  2339. spaces.")
  2340. (defcustom org-global-properties nil
  2341. "List of property/value pairs that can be inherited by any entry.
  2342. This list will be combined with the constant `org-global-properties-fixed'.
  2343. The entries in this list are cons cells where the car is a property
  2344. name and cdr is a string with the value.
  2345. You can set buffer-local values for the same purpose in the variable
  2346. `org-file-properties' this by adding lines like
  2347. #+PROPERTY: NAME VALUE"
  2348. :group 'org-properties
  2349. :type '(repeat
  2350. (cons (string :tag "Property")
  2351. (string :tag "Value"))))
  2352. (defvar org-file-properties nil
  2353. "List of property/value pairs that can be inherited by any entry.
  2354. Valid for the current buffer.
  2355. This variable is populated from #+PROPERTY lines.")
  2356. (make-variable-buffer-local 'org-file-properties)
  2357. (defgroup org-agenda nil
  2358. "Options concerning agenda views in Org-mode."
  2359. :tag "Org Agenda"
  2360. :group 'org)
  2361. (defvar org-category nil
  2362. "Variable used by org files to set a category for agenda display.
  2363. Such files should use a file variable to set it, for example
  2364. # -*- mode: org; org-category: \"ELisp\"
  2365. or contain a special line
  2366. #+CATEGORY: ELisp
  2367. If the file does not specify a category, then file's base name
  2368. is used instead.")
  2369. (make-variable-buffer-local 'org-category)
  2370. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2371. (defcustom org-agenda-files nil
  2372. "The files to be used for agenda display.
  2373. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2374. \\[org-remove-file]. You can also use customize to edit the list.
  2375. If an entry is a directory, all files in that directory that are matched by
  2376. `org-agenda-file-regexp' will be part of the file list.
  2377. If the value of the variable is not a list but a single file name, then
  2378. the list of agenda files is actually stored and maintained in that file, one
  2379. agenda file per line."
  2380. :group 'org-agenda
  2381. :type '(choice
  2382. (repeat :tag "List of files and directories" file)
  2383. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2384. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2385. "Regular expression to match files for `org-agenda-files'.
  2386. If any element in the list in that variable contains a directory instead
  2387. of a normal file, all files in that directory that are matched by this
  2388. regular expression will be included."
  2389. :group 'org-agenda
  2390. :type 'regexp)
  2391. (defcustom org-agenda-text-search-extra-files nil
  2392. "List of extra files to be searched by text search commands.
  2393. These files will be search in addition to the agenda files by the
  2394. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2395. Note that these files will only be searched for text search commands,
  2396. not for the other agenda views like todo lists, tag searches or the weekly
  2397. agenda. This variable is intended to list notes and possibly archive files
  2398. that should also be searched by these two commands.
  2399. In fact, if the first element in the list is the symbol `agenda-archives',
  2400. than all archive files of all agenda files will be added to the search
  2401. scope."
  2402. :group 'org-agenda
  2403. :type '(set :greedy t
  2404. (const :tag "Agenda Archives" agenda-archives)
  2405. (repeat :inline t (file))))
  2406. (if (fboundp 'defvaralias)
  2407. (defvaralias 'org-agenda-multi-occur-extra-files
  2408. 'org-agenda-text-search-extra-files))
  2409. (defcustom org-agenda-skip-unavailable-files nil
  2410. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2411. A nil value means to remove them, after a query, from the list."
  2412. :group 'org-agenda
  2413. :type 'boolean)
  2414. (defcustom org-calendar-to-agenda-key [?c]
  2415. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2416. The command `org-calendar-goto-agenda' will be bound to this key. The
  2417. default is the character `c' because then `c' can be used to switch back and
  2418. forth between agenda and calendar."
  2419. :group 'org-agenda
  2420. :type 'sexp)
  2421. (defcustom org-calendar-agenda-action-key [?k]
  2422. "The key to be installed in `calendar-mode-map' for agenda-action.
  2423. The command `org-agenda-action' will be bound to this key. The
  2424. default is the character `k' because we use the same key in the agenda."
  2425. :group 'org-agenda
  2426. :type 'sexp)
  2427. (defcustom org-calendar-insert-diary-entry-key [?i]
  2428. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2429. This option is irrelevant until `org-agenda-diary-file' has been configured
  2430. to point to an Org-mode file. When that is the case, the command
  2431. `org-agenda-diary-entry' will be bound to the key given here, by default
  2432. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2433. if you want to continue doing this, you need to change this to a different
  2434. key."
  2435. :group 'org-agenda
  2436. :type 'sexp)
  2437. (defcustom org-agenda-diary-file 'diary-file
  2438. "File to which to add new entries with the `i' key in agenda and calendar.
  2439. When this is the symbol `diary-file', the functionality in the Emacs
  2440. calendar will be used to add entries to the `diary-file'. But when this
  2441. points to a file, `org-agenda-diary-entry' will be used instead."
  2442. :group 'org-agenda
  2443. :type '(choice
  2444. (const :tag "The standard Emacs diary file" diary-file)
  2445. (file :tag "Special Org file diary entries")))
  2446. (eval-after-load "calendar"
  2447. '(progn
  2448. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2449. 'org-calendar-goto-agenda)
  2450. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2451. 'org-agenda-action)
  2452. (add-hook 'calendar-mode-hook
  2453. (lambda ()
  2454. (unless (eq org-agenda-diary-file 'diary-file)
  2455. (define-key calendar-mode-map
  2456. org-calendar-insert-diary-entry-key
  2457. 'org-agenda-diary-entry))))))
  2458. (defgroup org-latex nil
  2459. "Options for embedding LaTeX code into Org-mode."
  2460. :tag "Org LaTeX"
  2461. :group 'org)
  2462. (defcustom org-format-latex-options
  2463. '(:foreground default :background default :scale 1.0
  2464. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2465. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2466. "Options for creating images from LaTeX fragments.
  2467. This is a property list with the following properties:
  2468. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2469. `default' means use the foreground of the default face.
  2470. :background the background color, or \"Transparent\".
  2471. `default' means use the background of the default face.
  2472. :scale a scaling factor for the size of the images.
  2473. :html-foreground, :html-background, :html-scale
  2474. the same numbers for HTML export.
  2475. :matchers a list indicating which matchers should be used to
  2476. find LaTeX fragments. Valid members of this list are:
  2477. \"begin\" find environments
  2478. \"$1\" find single characters surrounded by $.$
  2479. \"$\" find math expressions surrounded by $...$
  2480. \"$$\" find math expressions surrounded by $$....$$
  2481. \"\\(\" find math expressions surrounded by \\(...\\)
  2482. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2483. :group 'org-latex
  2484. :type 'plist)
  2485. (defcustom org-format-latex-header "\\documentclass{article}
  2486. \\usepackage{amssymb}
  2487. \\usepackage[usenames]{color}
  2488. \\usepackage{amsmath}
  2489. \\usepackage{latexsym}
  2490. \\usepackage[mathscr]{eucal}
  2491. \\pagestyle{empty} % do not remove
  2492. % The settings below are copied from fullpage.sty
  2493. \\setlength{\\textwidth}{\\paperwidth}
  2494. \\addtolength{\\textwidth}{-3cm}
  2495. \\setlength{\\oddsidemargin}{1.5cm}
  2496. \\addtolength{\\oddsidemargin}{-2.54cm}
  2497. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2498. \\setlength{\\textheight}{\\paperheight}
  2499. \\addtolength{\\textheight}{-\\headheight}
  2500. \\addtolength{\\textheight}{-\\headsep}
  2501. \\addtolength{\\textheight}{-\\footskip}
  2502. \\addtolength{\\textheight}{-3cm}
  2503. \\setlength{\\topmargin}{1.5cm}
  2504. \\addtolength{\\topmargin}{-2.54cm}"
  2505. "The document header used for processing LaTeX fragments.
  2506. It is imperative that this header make sure that no page number
  2507. appears on the page."
  2508. :group 'org-latex
  2509. :type 'string)
  2510. (defvar org-format-latex-header-extra nil)
  2511. ;; The following variable is defined here because is it also used
  2512. ;; when formatting latex fragments. Originally it was part of the
  2513. ;; LaTeX exporter, which is why the name includes "export".
  2514. (defcustom org-export-latex-packages-alist nil
  2515. "Alist of packages to be inserted in the header.
  2516. Each cell is of the format \( \"option\" . \"package\" \)."
  2517. :group 'org-export-latex
  2518. :type '(repeat
  2519. (list
  2520. (string :tag "option")
  2521. (string :tag "package"))))
  2522. (defgroup org-font-lock nil
  2523. "Font-lock settings for highlighting in Org-mode."
  2524. :tag "Org Font Lock"
  2525. :group 'org)
  2526. (defcustom org-level-color-stars-only nil
  2527. "Non-nil means fontify only the stars in each headline.
  2528. When nil, the entire headline is fontified.
  2529. Changing it requires restart of `font-lock-mode' to become effective
  2530. also in regions already fontified."
  2531. :group 'org-font-lock
  2532. :type 'boolean)
  2533. (defcustom org-hide-leading-stars nil
  2534. "Non-nil means, hide the first N-1 stars in a headline.
  2535. This works by using the face `org-hide' for these stars. This
  2536. face is white for a light background, and black for a dark
  2537. background. You may have to customize the face `org-hide' to
  2538. make this work.
  2539. Changing it requires restart of `font-lock-mode' to become effective
  2540. also in regions already fontified.
  2541. You may also set this on a per-file basis by adding one of the following
  2542. lines to the buffer:
  2543. #+STARTUP: hidestars
  2544. #+STARTUP: showstars"
  2545. :group 'org-font-lock
  2546. :type 'boolean)
  2547. (defcustom org-fontify-done-headline nil
  2548. "Non-nil means, change the face of a headline if it is marked DONE.
  2549. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2550. When this is non-nil, the headline after the keyword is set to the
  2551. `org-headline-done' as an additional indication."
  2552. :group 'org-font-lock
  2553. :type 'boolean)
  2554. (defcustom org-fontify-emphasized-text t
  2555. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2556. Changing this variable requires a restart of Emacs to take effect."
  2557. :group 'org-font-lock
  2558. :type 'boolean)
  2559. (defcustom org-fontify-whole-heading-line nil
  2560. "Non-nil means fontify the whole line for headings.
  2561. This is useful when setting a background color for the
  2562. org-level-* faces."
  2563. :group 'org-font-lock
  2564. :type 'boolean)
  2565. (defcustom org-highlight-latex-fragments-and-specials nil
  2566. "Non-nil means, fontify what is treated specially by the exporters."
  2567. :group 'org-font-lock
  2568. :type 'boolean)
  2569. (defcustom org-hide-emphasis-markers nil
  2570. "Non-nil mean font-lock should hide the emphasis marker characters."
  2571. :group 'org-font-lock
  2572. :type 'boolean)
  2573. (defvar org-emph-re nil
  2574. "Regular expression for matching emphasis.")
  2575. (defvar org-verbatim-re nil
  2576. "Regular expression for matching verbatim text.")
  2577. (defvar org-emphasis-regexp-components) ; defined just below
  2578. (defvar org-emphasis-alist) ; defined just below
  2579. (defun org-set-emph-re (var val)
  2580. "Set variable and compute the emphasis regular expression."
  2581. (set var val)
  2582. (when (and (boundp 'org-emphasis-alist)
  2583. (boundp 'org-emphasis-regexp-components)
  2584. org-emphasis-alist org-emphasis-regexp-components)
  2585. (let* ((e org-emphasis-regexp-components)
  2586. (pre (car e))
  2587. (post (nth 1 e))
  2588. (border (nth 2 e))
  2589. (body (nth 3 e))
  2590. (nl (nth 4 e))
  2591. (body1 (concat body "*?"))
  2592. (markers (mapconcat 'car org-emphasis-alist ""))
  2593. (vmarkers (mapconcat
  2594. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2595. org-emphasis-alist "")))
  2596. ;; make sure special characters appear at the right position in the class
  2597. (if (string-match "\\^" markers)
  2598. (setq markers (concat (replace-match "" t t markers) "^")))
  2599. (if (string-match "-" markers)
  2600. (setq markers (concat (replace-match "" t t markers) "-")))
  2601. (if (string-match "\\^" vmarkers)
  2602. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2603. (if (string-match "-" vmarkers)
  2604. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2605. (if (> nl 0)
  2606. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2607. (int-to-string nl) "\\}")))
  2608. ;; Make the regexp
  2609. (setq org-emph-re
  2610. (concat "\\([" pre "]\\|^\\)"
  2611. "\\("
  2612. "\\([" markers "]\\)"
  2613. "\\("
  2614. "[^" border "]\\|"
  2615. "[^" border "]"
  2616. body1
  2617. "[^" border "]"
  2618. "\\)"
  2619. "\\3\\)"
  2620. "\\([" post "]\\|$\\)"))
  2621. (setq org-verbatim-re
  2622. (concat "\\([" pre "]\\|^\\)"
  2623. "\\("
  2624. "\\([" vmarkers "]\\)"
  2625. "\\("
  2626. "[^" border "]\\|"
  2627. "[^" border "]"
  2628. body1
  2629. "[^" border "]"
  2630. "\\)"
  2631. "\\3\\)"
  2632. "\\([" post "]\\|$\\)")))))
  2633. (defcustom org-emphasis-regexp-components
  2634. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2635. "Components used to build the regular expression for emphasis.
  2636. This is a list with 6 entries. Terminology: In an emphasis string
  2637. like \" *strong word* \", we call the initial space PREMATCH, the final
  2638. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2639. and \"trong wor\" is the body. The different components in this variable
  2640. specify what is allowed/forbidden in each part:
  2641. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2642. post Chars allowed as postmatch. End of line will be allowed too.
  2643. border The chars *forbidden* as border characters.
  2644. body-regexp A regexp like \".\" to match a body character. Don't use
  2645. non-shy groups here, and don't allow newline here.
  2646. newline The maximum number of newlines allowed in an emphasis exp.
  2647. Use customize to modify this, or restart Emacs after changing it."
  2648. :group 'org-font-lock
  2649. :set 'org-set-emph-re
  2650. :type '(list
  2651. (sexp :tag "Allowed chars in pre ")
  2652. (sexp :tag "Allowed chars in post ")
  2653. (sexp :tag "Forbidden chars in border ")
  2654. (sexp :tag "Regexp for body ")
  2655. (integer :tag "number of newlines allowed")
  2656. (option (boolean :tag "Please ignore this button"))))
  2657. (defcustom org-emphasis-alist
  2658. `(("*" bold "<b>" "</b>")
  2659. ("/" italic "<i>" "</i>")
  2660. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2661. ("=" org-code "<code>" "</code>" verbatim)
  2662. ("~" org-verbatim "<code>" "</code>" verbatim)
  2663. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2664. "<del>" "</del>")
  2665. )
  2666. "Special syntax for emphasized text.
  2667. Text starting and ending with a special character will be emphasized, for
  2668. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2669. characters, the face to be used by font-lock for highlighting in Org-mode
  2670. Emacs buffers, and the HTML tags to be used for this.
  2671. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2672. Use customize to modify this, or restart Emacs after changing it."
  2673. :group 'org-font-lock
  2674. :set 'org-set-emph-re
  2675. :type '(repeat
  2676. (list
  2677. (string :tag "Marker character")
  2678. (choice
  2679. (face :tag "Font-lock-face")
  2680. (plist :tag "Face property list"))
  2681. (string :tag "HTML start tag")
  2682. (string :tag "HTML end tag")
  2683. (option (const verbatim)))))
  2684. (defvar org-protecting-blocks
  2685. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2686. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2687. This is needed for font-lock setup.")
  2688. ;;; Miscellaneous options
  2689. (defgroup org-completion nil
  2690. "Completion in Org-mode."
  2691. :tag "Org Completion"
  2692. :group 'org)
  2693. (defcustom org-completion-use-ido nil
  2694. "Non-nil means, use ido completion wherever possible.
  2695. Note that `ido-mode' must be active for this variable to be relevant.
  2696. If you decide to turn this variable on, you might well want to turn off
  2697. `org-outline-path-complete-in-steps'.
  2698. See also `org-completion-use-iswitchb'."
  2699. :group 'org-completion
  2700. :type 'boolean)
  2701. (defcustom org-completion-use-iswitchb nil
  2702. "Non-nil means, use iswitchb completion wherever possible.
  2703. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2704. If you decide to turn this variable on, you might well want to turn off
  2705. `org-outline-path-complete-in-steps'.
  2706. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2707. :group 'org-completion
  2708. :type 'boolean)
  2709. (defcustom org-completion-fallback-command 'hippie-expand
  2710. "The expansion command called by \\[org-complete] in normal context.
  2711. Normal means, no org-mode-specific context."
  2712. :group 'org-completion
  2713. :type 'function)
  2714. ;;; Functions and variables from their packages
  2715. ;; Declared here to avoid compiler warnings
  2716. ;; XEmacs only
  2717. (defvar outline-mode-menu-heading)
  2718. (defvar outline-mode-menu-show)
  2719. (defvar outline-mode-menu-hide)
  2720. (defvar zmacs-regions) ; XEmacs regions
  2721. ;; Emacs only
  2722. (defvar mark-active)
  2723. ;; Various packages
  2724. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2725. (declare-function calendar-forward-day "cal-move" (arg))
  2726. (declare-function calendar-goto-date "cal-move" (date))
  2727. (declare-function calendar-goto-today "cal-move" ())
  2728. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2729. (defvar calc-embedded-close-formula)
  2730. (defvar calc-embedded-open-formula)
  2731. (declare-function cdlatex-tab "ext:cdlatex" ())
  2732. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2733. (defvar font-lock-unfontify-region-function)
  2734. (declare-function iswitchb-read-buffer "iswitchb"
  2735. (prompt &optional default require-match start matches-set))
  2736. (defvar iswitchb-temp-buflist)
  2737. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2738. (defvar org-agenda-tags-todo-honor-ignore-options)
  2739. (declare-function org-agenda-skip "org-agenda" ())
  2740. (declare-function
  2741. org-format-agenda-item "org-agenda"
  2742. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2743. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2744. (declare-function org-agenda-change-all-lines "org-agenda"
  2745. (newhead hdmarker &optional fixface just-this))
  2746. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2747. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2748. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2749. (beg end))
  2750. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2751. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2752. "org-agenda" (&optional end))
  2753. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2754. (declare-function org-indent-mode "org-indent" (&optional arg))
  2755. (declare-function parse-time-string "parse-time" (string))
  2756. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2757. (defvar remember-data-file)
  2758. (defvar texmathp-why)
  2759. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2760. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2761. (defvar w3m-current-url)
  2762. (defvar w3m-current-title)
  2763. (defvar org-latex-regexps)
  2764. ;;; Autoload and prepare some org modules
  2765. ;; Some table stuff that needs to be defined here, because it is used
  2766. ;; by the functions setting up org-mode or checking for table context.
  2767. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2768. "Detects an org-type or table-type table.")
  2769. (defconst org-table-line-regexp "^[ \t]*|"
  2770. "Detects an org-type table line.")
  2771. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2772. "Detects an org-type table line.")
  2773. (defconst org-table-hline-regexp "^[ \t]*|-"
  2774. "Detects an org-type table hline.")
  2775. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2776. "Detects a table-type table hline.")
  2777. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2778. "Searching from within a table (any type) this finds the first line
  2779. outside the table.")
  2780. ;; Autoload the functions in org-table.el that are needed by functions here.
  2781. (eval-and-compile
  2782. (org-autoload "org-table"
  2783. '(org-table-align org-table-begin org-table-blank-field
  2784. org-table-convert org-table-convert-region org-table-copy-down
  2785. org-table-copy-region org-table-create
  2786. org-table-create-or-convert-from-region
  2787. org-table-create-with-table.el org-table-current-dline
  2788. org-table-cut-region org-table-delete-column org-table-edit-field
  2789. org-table-edit-formulas org-table-end org-table-eval-formula
  2790. org-table-export org-table-field-info
  2791. org-table-get-stored-formulas org-table-goto-column
  2792. org-table-hline-and-move org-table-import org-table-insert-column
  2793. org-table-insert-hline org-table-insert-row org-table-iterate
  2794. org-table-justify-field-maybe org-table-kill-row
  2795. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2796. org-table-move-column org-table-move-column-left
  2797. org-table-move-column-right org-table-move-row
  2798. org-table-move-row-down org-table-move-row-up
  2799. org-table-next-field org-table-next-row org-table-paste-rectangle
  2800. org-table-previous-field org-table-recalculate
  2801. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2802. org-table-toggle-coordinate-overlays
  2803. org-table-toggle-formula-debugger org-table-wrap-region
  2804. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2805. (defun org-at-table-p (&optional table-type)
  2806. "Return t if the cursor is inside an org-type table.
  2807. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2808. (if org-enable-table-editor
  2809. (save-excursion
  2810. (beginning-of-line 1)
  2811. (looking-at (if table-type org-table-any-line-regexp
  2812. org-table-line-regexp)))
  2813. nil))
  2814. (defsubst org-table-p () (org-at-table-p))
  2815. (defun org-at-table.el-p ()
  2816. "Return t if and only if we are at a table.el table."
  2817. (and (org-at-table-p 'any)
  2818. (save-excursion
  2819. (goto-char (org-table-begin 'any))
  2820. (looking-at org-table1-hline-regexp))))
  2821. (defun org-table-recognize-table.el ()
  2822. "If there is a table.el table nearby, recognize it and move into it."
  2823. (if org-table-tab-recognizes-table.el
  2824. (if (org-at-table.el-p)
  2825. (progn
  2826. (beginning-of-line 1)
  2827. (if (looking-at org-table-dataline-regexp)
  2828. nil
  2829. (if (looking-at org-table1-hline-regexp)
  2830. (progn
  2831. (beginning-of-line 2)
  2832. (if (looking-at org-table-any-border-regexp)
  2833. (beginning-of-line -1)))))
  2834. (if (re-search-forward "|" (org-table-end t) t)
  2835. (progn
  2836. (require 'table)
  2837. (if (table--at-cell-p (point))
  2838. t
  2839. (message "recognizing table.el table...")
  2840. (table-recognize-table)
  2841. (message "recognizing table.el table...done")))
  2842. (error "This should not happen..."))
  2843. t)
  2844. nil)
  2845. nil))
  2846. (defun org-at-table-hline-p ()
  2847. "Return t if the cursor is inside a hline in a table."
  2848. (if org-enable-table-editor
  2849. (save-excursion
  2850. (beginning-of-line 1)
  2851. (looking-at org-table-hline-regexp))
  2852. nil))
  2853. (defvar org-table-clean-did-remove-column nil)
  2854. (defun org-table-map-tables (function)
  2855. "Apply FUNCTION to the start of all tables in the buffer."
  2856. (save-excursion
  2857. (save-restriction
  2858. (widen)
  2859. (goto-char (point-min))
  2860. (while (re-search-forward org-table-any-line-regexp nil t)
  2861. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2862. (beginning-of-line 1)
  2863. (when (looking-at org-table-line-regexp)
  2864. (save-excursion (funcall function))
  2865. (or (looking-at org-table-line-regexp)
  2866. (forward-char 1)))
  2867. (re-search-forward org-table-any-border-regexp nil 1))))
  2868. (message "Mapping tables: done"))
  2869. ;; Declare and autoload functions from org-exp.el & Co
  2870. (declare-function org-default-export-plist "org-exp")
  2871. (declare-function org-infile-export-plist "org-exp")
  2872. (declare-function org-get-current-options "org-exp")
  2873. (eval-and-compile
  2874. (org-autoload "org-exp"
  2875. '(org-export org-export-visible
  2876. org-insert-export-options-template
  2877. org-table-clean-before-export))
  2878. (org-autoload "org-ascii"
  2879. '(org-export-as-ascii org-export-ascii-preprocess
  2880. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2881. org-export-region-as-ascii))
  2882. (org-autoload "org-latex"
  2883. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  2884. org-replace-region-by-latex org-export-region-as-latex
  2885. org-export-as-latex org-export-as-pdf
  2886. org-export-as-pdf-and-open))
  2887. (org-autoload "org-html"
  2888. '(org-export-as-html-and-open
  2889. org-export-as-html-batch org-export-as-html-to-buffer
  2890. org-replace-region-by-html org-export-region-as-html
  2891. org-export-as-html))
  2892. (org-autoload "org-docbook"
  2893. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  2894. org-replace-region-by-docbook org-export-region-as-docbook
  2895. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  2896. org-export-as-docbook))
  2897. (org-autoload "org-icalendar"
  2898. '(org-export-icalendar-this-file
  2899. org-export-icalendar-all-agenda-files
  2900. org-export-icalendar-combine-agenda-files))
  2901. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  2902. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  2903. ;; Declare and autoload functions from org-agenda.el
  2904. (eval-and-compile
  2905. (org-autoload "org-agenda"
  2906. '(org-agenda org-agenda-list org-search-view
  2907. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2908. org-diary org-agenda-to-appt
  2909. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2910. ;; Autoload org-remember
  2911. (eval-and-compile
  2912. (org-autoload "org-remember"
  2913. '(org-remember-insinuate org-remember-annotation
  2914. org-remember-apply-template org-remember org-remember-handler)))
  2915. ;; Autoload org-clock.el
  2916. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2917. (beg end))
  2918. (declare-function org-clock-update-mode-line "org-clock" ())
  2919. (declare-function org-resolve-clocks "org-clock"
  2920. (&optional also-non-dangling-p prompt last-valid))
  2921. (defvar org-clock-start-time)
  2922. (defvar org-clock-marker (make-marker)
  2923. "Marker recording the last clock-in.")
  2924. (defvar org-clock-hd-marker (make-marker)
  2925. "Marker recording the last clock-in, but the headline position.")
  2926. (defvar org-clock-heading ""
  2927. "The heading of the current clock entry.")
  2928. (defun org-clock-is-active ()
  2929. "Return non-nil if clock is currently running.
  2930. The return value is actually the clock marker."
  2931. (marker-buffer org-clock-marker))
  2932. (eval-and-compile
  2933. (org-autoload
  2934. "org-clock"
  2935. '(org-clock-in org-clock-out org-clock-cancel
  2936. org-clock-goto org-clock-sum org-clock-display
  2937. org-clock-remove-overlays org-clock-report
  2938. org-clocktable-shift org-dblock-write:clocktable
  2939. org-get-clocktable org-resolve-clocks)))
  2940. (defun org-clock-update-time-maybe ()
  2941. "If this is a CLOCK line, update it and return t.
  2942. Otherwise, return nil."
  2943. (interactive)
  2944. (save-excursion
  2945. (beginning-of-line 1)
  2946. (skip-chars-forward " \t")
  2947. (when (looking-at org-clock-string)
  2948. (let ((re (concat "[ \t]*" org-clock-string
  2949. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2950. "\\([ \t]*=>.*\\)?\\)?"))
  2951. ts te h m s neg)
  2952. (cond
  2953. ((not (looking-at re))
  2954. nil)
  2955. ((not (match-end 2))
  2956. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2957. (> org-clock-marker (point))
  2958. (<= org-clock-marker (point-at-eol)))
  2959. ;; The clock is running here
  2960. (setq org-clock-start-time
  2961. (apply 'encode-time
  2962. (org-parse-time-string (match-string 1))))
  2963. (org-clock-update-mode-line)))
  2964. (t
  2965. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2966. (end-of-line 1)
  2967. (setq ts (match-string 1)
  2968. te (match-string 3))
  2969. (setq s (- (org-float-time
  2970. (apply 'encode-time (org-parse-time-string te)))
  2971. (org-float-time
  2972. (apply 'encode-time (org-parse-time-string ts))))
  2973. neg (< s 0)
  2974. s (abs s)
  2975. h (floor (/ s 3600))
  2976. s (- s (* 3600 h))
  2977. m (floor (/ s 60))
  2978. s (- s (* 60 s)))
  2979. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2980. t))))))
  2981. (defun org-check-running-clock ()
  2982. "Check if the current buffer contains the running clock.
  2983. If yes, offer to stop it and to save the buffer with the changes."
  2984. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2985. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2986. (buffer-name))))
  2987. (org-clock-out)
  2988. (when (y-or-n-p "Save changed buffer?")
  2989. (save-buffer))))
  2990. (defun org-clocktable-try-shift (dir n)
  2991. "Check if this line starts a clock table, if yes, shift the time block."
  2992. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2993. (org-clocktable-shift dir n)))
  2994. ;; Autoload org-timer.el
  2995. (eval-and-compile
  2996. (org-autoload
  2997. "org-timer"
  2998. '(org-timer-start org-timer org-timer-item
  2999. org-timer-change-times-in-region
  3000. org-timer-set-timer
  3001. org-timer-reset-timers
  3002. org-timer-show-remaining-time)))
  3003. ;; Autoload org-feed.el
  3004. (eval-and-compile
  3005. (org-autoload
  3006. "org-feed"
  3007. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3008. ;; Autoload org-indent.el
  3009. ;; Define the variable already here, to make sure we have it.
  3010. (defvar org-indent-mode nil
  3011. "Non-nil if Org-Indent mode is enabled.
  3012. Use the command `org-indent-mode' to change this variable.")
  3013. (eval-and-compile
  3014. (org-autoload
  3015. "org-indent"
  3016. '(org-indent-mode)))
  3017. ;; Autoload org-mobile.el
  3018. (eval-and-compile
  3019. (org-autoload
  3020. "org-mobile"
  3021. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3022. ;; Autoload archiving code
  3023. ;; The stuff that is needed for cycling and tags has to be defined here.
  3024. (defgroup org-archive nil
  3025. "Options concerning archiving in Org-mode."
  3026. :tag "Org Archive"
  3027. :group 'org-structure)
  3028. (defcustom org-archive-location "%s_archive::"
  3029. "The location where subtrees should be archived.
  3030. The value of this variable is a string, consisting of two parts,
  3031. separated by a double-colon. The first part is a filename and
  3032. the second part is a headline.
  3033. When the filename is omitted, archiving happens in the same file.
  3034. %s in the filename will be replaced by the current file
  3035. name (without the directory part). Archiving to a different file
  3036. is useful to keep archived entries from contributing to the
  3037. Org-mode Agenda.
  3038. The archived entries will be filed as subtrees of the specified
  3039. headline. When the headline is omitted, the subtrees are simply
  3040. filed away at the end of the file, as top-level entries. Also in
  3041. the heading you can use %s to represent the file name, this can be
  3042. useful when using the same archive for a number of different files.
  3043. Here are a few examples:
  3044. \"%s_archive::\"
  3045. If the current file is Projects.org, archive in file
  3046. Projects.org_archive, as top-level trees. This is the default.
  3047. \"::* Archived Tasks\"
  3048. Archive in the current file, under the top-level headline
  3049. \"* Archived Tasks\".
  3050. \"~/org/archive.org::\"
  3051. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3052. \"~/org/archive.org::From %s\"
  3053. Archive in file ~/org/archive.org (absolute path), under headlines
  3054. \"From FILENAME\" where file name is the current file name.
  3055. \"basement::** Finished Tasks\"
  3056. Archive in file ./basement (relative path), as level 3 trees
  3057. below the level 2 heading \"** Finished Tasks\".
  3058. You may set this option on a per-file basis by adding to the buffer a
  3059. line like
  3060. #+ARCHIVE: basement::** Finished Tasks
  3061. You may also define it locally for a subtree by setting an ARCHIVE property
  3062. in the entry. If such a property is found in an entry, or anywhere up
  3063. the hierarchy, it will be used."
  3064. :group 'org-archive
  3065. :type 'string)
  3066. (defcustom org-archive-tag "ARCHIVE"
  3067. "The tag that marks a subtree as archived.
  3068. An archived subtree does not open during visibility cycling, and does
  3069. not contribute to the agenda listings.
  3070. After changing this, font-lock must be restarted in the relevant buffers to
  3071. get the proper fontification."
  3072. :group 'org-archive
  3073. :group 'org-keywords
  3074. :type 'string)
  3075. (defcustom org-agenda-skip-archived-trees t
  3076. "Non-nil means, the agenda will skip any items located in archived trees.
  3077. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3078. variable is no longer recommended, you should leave it at the value t.
  3079. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3080. :group 'org-archive
  3081. :group 'org-agenda-skip
  3082. :type 'boolean)
  3083. (defcustom org-columns-skip-archived-trees t
  3084. "Non-nil means, ignore archived trees when creating column view."
  3085. :group 'org-archive
  3086. :group 'org-properties
  3087. :type 'boolean)
  3088. (defcustom org-cycle-open-archived-trees nil
  3089. "Non-nil means, `org-cycle' will open archived trees.
  3090. An archived tree is a tree marked with the tag ARCHIVE.
  3091. When nil, archived trees will stay folded. You can still open them with
  3092. normal outline commands like `show-all', but not with the cycling commands."
  3093. :group 'org-archive
  3094. :group 'org-cycle
  3095. :type 'boolean)
  3096. (defcustom org-sparse-tree-open-archived-trees nil
  3097. "Non-nil means sparse tree construction shows matches in archived trees.
  3098. When nil, matches in these trees are highlighted, but the trees are kept in
  3099. collapsed state."
  3100. :group 'org-archive
  3101. :group 'org-sparse-trees
  3102. :type 'boolean)
  3103. (defun org-cycle-hide-archived-subtrees (state)
  3104. "Re-hide all archived subtrees after a visibility state change."
  3105. (when (and (not org-cycle-open-archived-trees)
  3106. (not (memq state '(overview folded))))
  3107. (save-excursion
  3108. (let* ((globalp (memq state '(contents all)))
  3109. (beg (if globalp (point-min) (point)))
  3110. (end (if globalp (point-max) (org-end-of-subtree t))))
  3111. (org-hide-archived-subtrees beg end)
  3112. (goto-char beg)
  3113. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3114. (message "%s" (substitute-command-keys
  3115. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3116. (defun org-force-cycle-archived ()
  3117. "Cycle subtree even if it is archived."
  3118. (interactive)
  3119. (setq this-command 'org-cycle)
  3120. (let ((org-cycle-open-archived-trees t))
  3121. (call-interactively 'org-cycle)))
  3122. (defun org-hide-archived-subtrees (beg end)
  3123. "Re-hide all archived subtrees after a visibility state change."
  3124. (save-excursion
  3125. (let* ((re (concat ":" org-archive-tag ":")))
  3126. (goto-char beg)
  3127. (while (re-search-forward re end t)
  3128. (and (org-on-heading-p) (org-flag-subtree t))
  3129. (org-end-of-subtree t)))))
  3130. (defun org-flag-subtree (flag)
  3131. (save-excursion
  3132. (org-back-to-heading t)
  3133. (outline-end-of-heading)
  3134. (outline-flag-region (point)
  3135. (progn (org-end-of-subtree t) (point))
  3136. flag)))
  3137. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3138. (eval-and-compile
  3139. (org-autoload "org-archive"
  3140. '(org-add-archive-files org-archive-subtree
  3141. org-archive-to-archive-sibling org-toggle-archive-tag
  3142. org-archive-subtree-default
  3143. org-archive-subtree-default-with-confirmation)))
  3144. ;; Autoload Column View Code
  3145. (declare-function org-columns-number-to-string "org-colview")
  3146. (declare-function org-columns-get-format-and-top-level "org-colview")
  3147. (declare-function org-columns-compute "org-colview")
  3148. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3149. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3150. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3151. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3152. ;; Autoload ID code
  3153. (declare-function org-id-store-link "org-id")
  3154. (declare-function org-id-locations-load "org-id")
  3155. (declare-function org-id-locations-save "org-id")
  3156. (defvar org-id-track-globally)
  3157. (org-autoload "org-id"
  3158. '(org-id-get-create org-id-new org-id-copy org-id-get
  3159. org-id-get-with-outline-path-completion
  3160. org-id-get-with-outline-drilling
  3161. org-id-goto org-id-find org-id-store-link))
  3162. ;; Autoload Plotting Code
  3163. (org-autoload "org-plot"
  3164. '(org-plot/gnuplot))
  3165. ;;; Variables for pre-computed regular expressions, all buffer local
  3166. (defvar org-drawer-regexp nil
  3167. "Matches first line of a hidden block.")
  3168. (make-variable-buffer-local 'org-drawer-regexp)
  3169. (defvar org-todo-regexp nil
  3170. "Matches any of the TODO state keywords.")
  3171. (make-variable-buffer-local 'org-todo-regexp)
  3172. (defvar org-not-done-regexp nil
  3173. "Matches any of the TODO state keywords except the last one.")
  3174. (make-variable-buffer-local 'org-not-done-regexp)
  3175. (defvar org-not-done-heading-regexp nil
  3176. "Matches a TODO headline that is not done.")
  3177. (make-variable-buffer-local 'org-not-done-regexp)
  3178. (defvar org-todo-line-regexp nil
  3179. "Matches a headline and puts TODO state into group 2 if present.")
  3180. (make-variable-buffer-local 'org-todo-line-regexp)
  3181. (defvar org-complex-heading-regexp nil
  3182. "Matches a headline and puts everything into groups:
  3183. group 1: the stars
  3184. group 2: The todo keyword, maybe
  3185. group 3: Priority cookie
  3186. group 4: True headline
  3187. group 5: Tags")
  3188. (make-variable-buffer-local 'org-complex-heading-regexp)
  3189. (defvar org-complex-heading-regexp-format nil)
  3190. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3191. (defvar org-todo-line-tags-regexp nil
  3192. "Matches a headline and puts TODO state into group 2 if present.
  3193. Also put tags into group 4 if tags are present.")
  3194. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3195. (defvar org-nl-done-regexp nil
  3196. "Matches newline followed by a headline with the DONE keyword.")
  3197. (make-variable-buffer-local 'org-nl-done-regexp)
  3198. (defvar org-looking-at-done-regexp nil
  3199. "Matches the DONE keyword a point.")
  3200. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3201. (defvar org-ds-keyword-length 12
  3202. "Maximum length of the Deadline and SCHEDULED keywords.")
  3203. (make-variable-buffer-local 'org-ds-keyword-length)
  3204. (defvar org-deadline-regexp nil
  3205. "Matches the DEADLINE keyword.")
  3206. (make-variable-buffer-local 'org-deadline-regexp)
  3207. (defvar org-deadline-time-regexp nil
  3208. "Matches the DEADLINE keyword together with a time stamp.")
  3209. (make-variable-buffer-local 'org-deadline-time-regexp)
  3210. (defvar org-deadline-line-regexp nil
  3211. "Matches the DEADLINE keyword and the rest of the line.")
  3212. (make-variable-buffer-local 'org-deadline-line-regexp)
  3213. (defvar org-scheduled-regexp nil
  3214. "Matches the SCHEDULED keyword.")
  3215. (make-variable-buffer-local 'org-scheduled-regexp)
  3216. (defvar org-scheduled-time-regexp nil
  3217. "Matches the SCHEDULED keyword together with a time stamp.")
  3218. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3219. (defvar org-closed-time-regexp nil
  3220. "Matches the CLOSED keyword together with a time stamp.")
  3221. (make-variable-buffer-local 'org-closed-time-regexp)
  3222. (defvar org-keyword-time-regexp nil
  3223. "Matches any of the 4 keywords, together with the time stamp.")
  3224. (make-variable-buffer-local 'org-keyword-time-regexp)
  3225. (defvar org-keyword-time-not-clock-regexp nil
  3226. "Matches any of the 3 keywords, together with the time stamp.")
  3227. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3228. (defvar org-maybe-keyword-time-regexp nil
  3229. "Matches a timestamp, possibly preceeded by a keyword.")
  3230. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3231. (defvar org-planning-or-clock-line-re nil
  3232. "Matches a line with planning or clock info.")
  3233. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3234. (defvar org-all-time-keywords nil
  3235. "List of time keywords.")
  3236. (make-variable-buffer-local 'org-all-time-keywords)
  3237. (defconst org-plain-time-of-day-regexp
  3238. (concat
  3239. "\\(\\<[012]?[0-9]"
  3240. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3241. "\\(--?"
  3242. "\\(\\<[012]?[0-9]"
  3243. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3244. "\\)?")
  3245. "Regular expression to match a plain time or time range.
  3246. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3247. groups carry important information:
  3248. 0 the full match
  3249. 1 the first time, range or not
  3250. 8 the second time, if it is a range.")
  3251. (defconst org-plain-time-extension-regexp
  3252. (concat
  3253. "\\(\\<[012]?[0-9]"
  3254. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3255. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3256. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3257. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3258. groups carry important information:
  3259. 0 the full match
  3260. 7 hours of duration
  3261. 9 minutes of duration")
  3262. (defconst org-stamp-time-of-day-regexp
  3263. (concat
  3264. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3265. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3266. "\\(--?"
  3267. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3268. "Regular expression to match a timestamp time or time range.
  3269. After a match, the following groups carry important information:
  3270. 0 the full match
  3271. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3272. 2 the first time, range or not
  3273. 4 the second time, if it is a range.")
  3274. (defconst org-startup-options
  3275. '(("fold" org-startup-folded t)
  3276. ("overview" org-startup-folded t)
  3277. ("nofold" org-startup-folded nil)
  3278. ("showall" org-startup-folded nil)
  3279. ("showeverything" org-startup-folded showeverything)
  3280. ("content" org-startup-folded content)
  3281. ("indent" org-startup-indented t)
  3282. ("noindent" org-startup-indented nil)
  3283. ("hidestars" org-hide-leading-stars t)
  3284. ("showstars" org-hide-leading-stars nil)
  3285. ("odd" org-odd-levels-only t)
  3286. ("oddeven" org-odd-levels-only nil)
  3287. ("align" org-startup-align-all-tables t)
  3288. ("noalign" org-startup-align-all-tables nil)
  3289. ("customtime" org-display-custom-times t)
  3290. ("logdone" org-log-done time)
  3291. ("lognotedone" org-log-done note)
  3292. ("nologdone" org-log-done nil)
  3293. ("lognoteclock-out" org-log-note-clock-out t)
  3294. ("nolognoteclock-out" org-log-note-clock-out nil)
  3295. ("logrepeat" org-log-repeat state)
  3296. ("lognoterepeat" org-log-repeat note)
  3297. ("nologrepeat" org-log-repeat nil)
  3298. ("logreschedule" org-log-reschedule time)
  3299. ("lognotereschedule" org-log-reschedule note)
  3300. ("nologreschedule" org-log-reschedule nil)
  3301. ("logredeadline" org-log-redeadline time)
  3302. ("lognoteredeadline" org-log-redeadline note)
  3303. ("nologredeadline" org-log-redeadline nil)
  3304. ("fninline" org-footnote-define-inline t)
  3305. ("nofninline" org-footnote-define-inline nil)
  3306. ("fnlocal" org-footnote-section nil)
  3307. ("fnauto" org-footnote-auto-label t)
  3308. ("fnprompt" org-footnote-auto-label nil)
  3309. ("fnconfirm" org-footnote-auto-label confirm)
  3310. ("fnplain" org-footnote-auto-label plain)
  3311. ("fnadjust" org-footnote-auto-adjust t)
  3312. ("nofnadjust" org-footnote-auto-adjust nil)
  3313. ("constcgs" constants-unit-system cgs)
  3314. ("constSI" constants-unit-system SI)
  3315. ("noptag" org-tag-persistent-alist nil)
  3316. ("hideblocks" org-hide-block-startup t)
  3317. ("nohideblocks" org-hide-block-startup nil)
  3318. ("beamer" org-startup-with-beamer-mode t))
  3319. "Variable associated with STARTUP options for org-mode.
  3320. Each element is a list of three items: The startup options as written
  3321. in the #+STARTUP line, the corresponding variable, and the value to
  3322. set this variable to if the option is found. An optional forth element PUSH
  3323. means to push this value onto the list in the variable.")
  3324. (defun org-set-regexps-and-options ()
  3325. "Precompute regular expressions for current buffer."
  3326. (when (org-mode-p)
  3327. (org-set-local 'org-todo-kwd-alist nil)
  3328. (org-set-local 'org-todo-key-alist nil)
  3329. (org-set-local 'org-todo-key-trigger nil)
  3330. (org-set-local 'org-todo-keywords-1 nil)
  3331. (org-set-local 'org-done-keywords nil)
  3332. (org-set-local 'org-todo-heads nil)
  3333. (org-set-local 'org-todo-sets nil)
  3334. (org-set-local 'org-todo-log-states nil)
  3335. (org-set-local 'org-file-properties nil)
  3336. (org-set-local 'org-file-tags nil)
  3337. (let ((re (org-make-options-regexp
  3338. '("CATEGORY" "TODO" "COLUMNS"
  3339. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3340. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3341. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3342. (splitre "[ \t]+")
  3343. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3344. tail sep kws1 prio props ftags drawers beamer-p
  3345. ext-setup-or-nil setup-contents (start 0))
  3346. (save-excursion
  3347. (save-restriction
  3348. (widen)
  3349. (goto-char (point-min))
  3350. (while (or (and ext-setup-or-nil
  3351. (string-match re ext-setup-or-nil start)
  3352. (setq start (match-end 0)))
  3353. (and (setq ext-setup-or-nil nil start 0)
  3354. (re-search-forward re nil t)))
  3355. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3356. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3357. (cond
  3358. ((equal key "CATEGORY")
  3359. (if (string-match "[ \t]+$" value)
  3360. (setq value (replace-match "" t t value)))
  3361. (setq cat value))
  3362. ((member key '("SEQ_TODO" "TODO"))
  3363. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3364. ((equal key "TYP_TODO")
  3365. (push (cons 'type (org-split-string value splitre)) kwds))
  3366. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3367. ;; general TODO-like setup
  3368. (push (cons (intern (downcase (match-string 1 key)))
  3369. (org-split-string value splitre)) kwds))
  3370. ((equal key "TAGS")
  3371. (setq tags (append tags (if tags '("\\n") nil)
  3372. (org-split-string value splitre))))
  3373. ((equal key "COLUMNS")
  3374. (org-set-local 'org-columns-default-format value))
  3375. ((equal key "LINK")
  3376. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3377. (push (cons (match-string 1 value)
  3378. (org-trim (match-string 2 value)))
  3379. links)))
  3380. ((equal key "PRIORITIES")
  3381. (setq prio (org-split-string value " +")))
  3382. ((equal key "PROPERTY")
  3383. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3384. (push (cons (match-string 1 value) (match-string 2 value))
  3385. props)))
  3386. ((equal key "FILETAGS")
  3387. (when (string-match "\\S-" value)
  3388. (setq ftags
  3389. (append
  3390. ftags
  3391. (apply 'append
  3392. (mapcar (lambda (x) (org-split-string x ":"))
  3393. (org-split-string value)))))))
  3394. ((equal key "DRAWERS")
  3395. (setq drawers (org-split-string value splitre)))
  3396. ((equal key "CONSTANTS")
  3397. (setq const (append const (org-split-string value splitre))))
  3398. ((equal key "STARTUP")
  3399. (let ((opts (org-split-string value splitre))
  3400. l var val)
  3401. (while (setq l (pop opts))
  3402. (when (setq l (assoc l org-startup-options))
  3403. (setq var (nth 1 l) val (nth 2 l))
  3404. (if (not (nth 3 l))
  3405. (set (make-local-variable var) val)
  3406. (if (not (listp (symbol-value var)))
  3407. (set (make-local-variable var) nil))
  3408. (set (make-local-variable var) (symbol-value var))
  3409. (add-to-list var val))))))
  3410. ((equal key "ARCHIVE")
  3411. (string-match " *$" value)
  3412. (setq arch (replace-match "" t t value))
  3413. (remove-text-properties 0 (length arch)
  3414. '(face t fontified t) arch))
  3415. ((equal key "LATEX_CLASS")
  3416. (setq beamer-p (equal value "beamer")))
  3417. ((equal key "SETUPFILE")
  3418. (setq setup-contents (org-file-contents
  3419. (expand-file-name
  3420. (org-remove-double-quotes value))
  3421. 'noerror))
  3422. (if (not ext-setup-or-nil)
  3423. (setq ext-setup-or-nil setup-contents start 0)
  3424. (setq ext-setup-or-nil
  3425. (concat (substring ext-setup-or-nil 0 start)
  3426. "\n" setup-contents "\n"
  3427. (substring ext-setup-or-nil start)))))
  3428. ))))
  3429. (when cat
  3430. (org-set-local 'org-category (intern cat))
  3431. (push (cons "CATEGORY" cat) props))
  3432. (when prio
  3433. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3434. (setq prio (mapcar 'string-to-char prio))
  3435. (org-set-local 'org-highest-priority (nth 0 prio))
  3436. (org-set-local 'org-lowest-priority (nth 1 prio))
  3437. (org-set-local 'org-default-priority (nth 2 prio)))
  3438. (and props (org-set-local 'org-file-properties (nreverse props)))
  3439. (and ftags (org-set-local 'org-file-tags
  3440. (mapcar 'org-add-prop-inherited ftags)))
  3441. (and drawers (org-set-local 'org-drawers drawers))
  3442. (and arch (org-set-local 'org-archive-location arch))
  3443. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3444. ;; Process the TODO keywords
  3445. (unless kwds
  3446. ;; Use the global values as if they had been given locally.
  3447. (setq kwds (default-value 'org-todo-keywords))
  3448. (if (stringp (car kwds))
  3449. (setq kwds (list (cons org-todo-interpretation
  3450. (default-value 'org-todo-keywords)))))
  3451. (setq kwds (reverse kwds)))
  3452. (setq kwds (nreverse kwds))
  3453. (let (inter kws kw)
  3454. (while (setq kws (pop kwds))
  3455. (let ((kws (or
  3456. (run-hook-with-args-until-success
  3457. 'org-todo-setup-filter-hook kws)
  3458. kws)))
  3459. (setq inter (pop kws) sep (member "|" kws)
  3460. kws0 (delete "|" (copy-sequence kws))
  3461. kwsa nil
  3462. kws1 (mapcar
  3463. (lambda (x)
  3464. ;; 1 2
  3465. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3466. (progn
  3467. (setq kw (match-string 1 x)
  3468. key (and (match-end 2) (match-string 2 x))
  3469. log (org-extract-log-state-settings x))
  3470. (push (cons kw (and key (string-to-char key))) kwsa)
  3471. (and log (push log org-todo-log-states))
  3472. kw)
  3473. (error "Invalid TODO keyword %s" x)))
  3474. kws0)
  3475. kwsa (if kwsa (append '((:startgroup))
  3476. (nreverse kwsa)
  3477. '((:endgroup))))
  3478. hw (car kws1)
  3479. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3480. tail (list inter hw (car dws) (org-last dws))))
  3481. (add-to-list 'org-todo-heads hw 'append)
  3482. (push kws1 org-todo-sets)
  3483. (setq org-done-keywords (append org-done-keywords dws nil))
  3484. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3485. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3486. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3487. (setq org-todo-sets (nreverse org-todo-sets)
  3488. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3489. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3490. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3491. ;; Process the constants
  3492. (when const
  3493. (let (e cst)
  3494. (while (setq e (pop const))
  3495. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3496. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3497. (setq org-table-formula-constants-local cst)))
  3498. ;; Process the tags.
  3499. (when tags
  3500. (let (e tgs)
  3501. (while (setq e (pop tags))
  3502. (cond
  3503. ((equal e "{") (push '(:startgroup) tgs))
  3504. ((equal e "}") (push '(:endgroup) tgs))
  3505. ((equal e "\\n") (push '(:newline) tgs))
  3506. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3507. (push (cons (match-string 1 e)
  3508. (string-to-char (match-string 2 e)))
  3509. tgs))
  3510. (t (push (list e) tgs))))
  3511. (org-set-local 'org-tag-alist nil)
  3512. (while (setq e (pop tgs))
  3513. (or (and (stringp (car e))
  3514. (assoc (car e) org-tag-alist))
  3515. (push e org-tag-alist)))))
  3516. ;; Compute the regular expressions and other local variables
  3517. (if (not org-done-keywords)
  3518. (setq org-done-keywords (and org-todo-keywords-1
  3519. (list (org-last org-todo-keywords-1)))))
  3520. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3521. (length org-scheduled-string)
  3522. (length org-clock-string)
  3523. (length org-closed-string)))
  3524. org-drawer-regexp
  3525. (concat "^[ \t]*:\\("
  3526. (mapconcat 'regexp-quote org-drawers "\\|")
  3527. "\\):[ \t]*$")
  3528. org-not-done-keywords
  3529. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3530. org-todo-regexp
  3531. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3532. "\\|") "\\)\\>")
  3533. org-not-done-regexp
  3534. (concat "\\<\\("
  3535. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3536. "\\)\\>")
  3537. org-not-done-heading-regexp
  3538. (concat "^\\(\\*+\\)[ \t]+\\("
  3539. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3540. "\\)\\>")
  3541. org-todo-line-regexp
  3542. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3543. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3544. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3545. org-complex-heading-regexp
  3546. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3547. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3548. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3549. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3550. org-complex-heading-regexp-format
  3551. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3552. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3553. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3554. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3555. org-nl-done-regexp
  3556. (concat "\n\\*+[ \t]+"
  3557. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3558. "\\)" "\\>")
  3559. org-todo-line-tags-regexp
  3560. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3561. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3562. (org-re
  3563. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3564. org-looking-at-done-regexp
  3565. (concat "^" "\\(?:"
  3566. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3567. "\\>")
  3568. org-deadline-regexp (concat "\\<" org-deadline-string)
  3569. org-deadline-time-regexp
  3570. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3571. org-deadline-line-regexp
  3572. (concat "\\<\\(" org-deadline-string "\\).*")
  3573. org-scheduled-regexp
  3574. (concat "\\<" org-scheduled-string)
  3575. org-scheduled-time-regexp
  3576. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3577. org-closed-time-regexp
  3578. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3579. org-keyword-time-regexp
  3580. (concat "\\<\\(" org-scheduled-string
  3581. "\\|" org-deadline-string
  3582. "\\|" org-closed-string
  3583. "\\|" org-clock-string "\\)"
  3584. " *[[<]\\([^]>]+\\)[]>]")
  3585. org-keyword-time-not-clock-regexp
  3586. (concat "\\<\\(" org-scheduled-string
  3587. "\\|" org-deadline-string
  3588. "\\|" org-closed-string
  3589. "\\)"
  3590. " *[[<]\\([^]>]+\\)[]>]")
  3591. org-maybe-keyword-time-regexp
  3592. (concat "\\(\\<\\(" org-scheduled-string
  3593. "\\|" org-deadline-string
  3594. "\\|" org-closed-string
  3595. "\\|" org-clock-string "\\)\\)?"
  3596. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3597. org-planning-or-clock-line-re
  3598. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3599. "\\|" org-deadline-string
  3600. "\\|" org-closed-string "\\|" org-clock-string
  3601. "\\)\\>\\)")
  3602. org-all-time-keywords
  3603. (mapcar (lambda (w) (substring w 0 -1))
  3604. (list org-scheduled-string org-deadline-string
  3605. org-clock-string org-closed-string))
  3606. )
  3607. (org-compute-latex-and-specials-regexp)
  3608. (org-set-font-lock-defaults))))
  3609. (defun org-file-contents (file &optional noerror)
  3610. "Return the contents of FILE, as a string."
  3611. (if (or (not file)
  3612. (not (file-readable-p file)))
  3613. (if noerror
  3614. (progn
  3615. (message "Cannot read file %s" file)
  3616. (ding) (sit-for 2)
  3617. "")
  3618. (error "Cannot read file %s" file))
  3619. (with-temp-buffer
  3620. (insert-file-contents file)
  3621. (buffer-string))))
  3622. (defun org-extract-log-state-settings (x)
  3623. "Extract the log state setting from a TODO keyword string.
  3624. This will extract info from a string like \"WAIT(w@/!)\"."
  3625. (let (kw key log1 log2)
  3626. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3627. (setq kw (match-string 1 x)
  3628. key (and (match-end 2) (match-string 2 x))
  3629. log1 (and (match-end 3) (match-string 3 x))
  3630. log2 (and (match-end 4) (match-string 4 x)))
  3631. (and (or log1 log2)
  3632. (list kw
  3633. (and log1 (if (equal log1 "!") 'time 'note))
  3634. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3635. (defun org-remove-keyword-keys (list)
  3636. "Remove a pair of parenthesis at the end of each string in LIST."
  3637. (mapcar (lambda (x)
  3638. (if (string-match "(.*)$" x)
  3639. (substring x 0 (match-beginning 0))
  3640. x))
  3641. list))
  3642. ;; FIXME: this could be done much better, using second characters etc.
  3643. (defun org-assign-fast-keys (alist)
  3644. "Assign fast keys to a keyword-key alist.
  3645. Respect keys that are already there."
  3646. (let (new e k c c1 c2 (char ?a))
  3647. (while (setq e (pop alist))
  3648. (cond
  3649. ((equal e '(:startgroup)) (push e new))
  3650. ((equal e '(:endgroup)) (push e new))
  3651. ((equal e '(:newline)) (push e new))
  3652. (t
  3653. (setq k (car e) c2 nil)
  3654. (if (cdr e)
  3655. (setq c (cdr e))
  3656. ;; automatically assign a character.
  3657. (setq c1 (string-to-char
  3658. (downcase (substring
  3659. k (if (= (string-to-char k) ?@) 1 0)))))
  3660. (if (or (rassoc c1 new) (rassoc c1 alist))
  3661. (while (or (rassoc char new) (rassoc char alist))
  3662. (setq char (1+ char)))
  3663. (setq c2 c1))
  3664. (setq c (or c2 char)))
  3665. (push (cons k c) new))))
  3666. (nreverse new)))
  3667. ;;; Some variables used in various places
  3668. (defvar org-window-configuration nil
  3669. "Used in various places to store a window configuration.")
  3670. (defvar org-selected-window nil
  3671. "Used in various places to store a window configuration.")
  3672. (defvar org-finish-function nil
  3673. "Function to be called when `C-c C-c' is used.
  3674. This is for getting out of special buffers like remember.")
  3675. ;; FIXME: Occasionally check by commenting these, to make sure
  3676. ;; no other functions uses these, forgetting to let-bind them.
  3677. (defvar entry)
  3678. (defvar last-state)
  3679. (defvar date)
  3680. ;; Defined somewhere in this file, but used before definition.
  3681. (defvar org-html-entities)
  3682. (defvar org-struct-menu)
  3683. (defvar org-org-menu)
  3684. (defvar org-tbl-menu)
  3685. ;;;; Define the Org-mode
  3686. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3687. (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."))
  3688. ;; We use a before-change function to check if a table might need
  3689. ;; an update.
  3690. (defvar org-table-may-need-update t
  3691. "Indicates that a table might need an update.
  3692. This variable is set by `org-before-change-function'.
  3693. `org-table-align' sets it back to nil.")
  3694. (defun org-before-change-function (beg end)
  3695. "Every change indicates that a table might need an update."
  3696. (setq org-table-may-need-update t))
  3697. (defvar org-mode-map)
  3698. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3699. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3700. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3701. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3702. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3703. (defvar org-table-buffer-is-an nil)
  3704. (defconst org-outline-regexp "\\*+ ")
  3705. ;;;###autoload
  3706. (define-derived-mode org-mode outline-mode "Org"
  3707. "Outline-based notes management and organizer, alias
  3708. \"Carsten's outline-mode for keeping track of everything.\"
  3709. Org-mode develops organizational tasks around a NOTES file which
  3710. contains information about projects as plain text. Org-mode is
  3711. implemented on top of outline-mode, which is ideal to keep the content
  3712. of large files well structured. It supports ToDo items, deadlines and
  3713. time stamps, which magically appear in the diary listing of the Emacs
  3714. calendar. Tables are easily created with a built-in table editor.
  3715. Plain text URL-like links connect to websites, emails (VM), Usenet
  3716. messages (Gnus), BBDB entries, and any files related to the project.
  3717. For printing and sharing of notes, an Org-mode file (or a part of it)
  3718. can be exported as a structured ASCII or HTML file.
  3719. The following commands are available:
  3720. \\{org-mode-map}"
  3721. ;; Get rid of Outline menus, they are not needed
  3722. ;; Need to do this here because define-derived-mode sets up
  3723. ;; the keymap so late. Still, it is a waste to call this each time
  3724. ;; we switch another buffer into org-mode.
  3725. (if (featurep 'xemacs)
  3726. (when (boundp 'outline-mode-menu-heading)
  3727. ;; Assume this is Greg's port, it used easymenu
  3728. (easy-menu-remove outline-mode-menu-heading)
  3729. (easy-menu-remove outline-mode-menu-show)
  3730. (easy-menu-remove outline-mode-menu-hide))
  3731. (define-key org-mode-map [menu-bar headings] 'undefined)
  3732. (define-key org-mode-map [menu-bar hide] 'undefined)
  3733. (define-key org-mode-map [menu-bar show] 'undefined))
  3734. (org-load-modules-maybe)
  3735. (easy-menu-add org-org-menu)
  3736. (easy-menu-add org-tbl-menu)
  3737. (org-install-agenda-files-menu)
  3738. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3739. (org-add-to-invisibility-spec '(org-cwidth))
  3740. (org-add-to-invisibility-spec '(org-hide-block . t))
  3741. (when (featurep 'xemacs)
  3742. (org-set-local 'line-move-ignore-invisible t))
  3743. (org-set-local 'outline-regexp org-outline-regexp)
  3744. (org-set-local 'outline-level 'org-outline-level)
  3745. (when (and org-ellipsis
  3746. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3747. (fboundp 'make-glyph-code))
  3748. (unless org-display-table
  3749. (setq org-display-table (make-display-table)))
  3750. (set-display-table-slot
  3751. org-display-table 4
  3752. (vconcat (mapcar
  3753. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3754. org-ellipsis)))
  3755. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3756. (setq buffer-display-table org-display-table))
  3757. (org-set-regexps-and-options)
  3758. (when (and org-tag-faces (not org-tags-special-faces-re))
  3759. ;; tag faces set outside customize.... force initialization.
  3760. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3761. ;; Calc embedded
  3762. (org-set-local 'calc-embedded-open-mode "# ")
  3763. (modify-syntax-entry ?# "<")
  3764. (modify-syntax-entry ?@ "w")
  3765. (if org-startup-truncated (setq truncate-lines t))
  3766. (org-set-local 'font-lock-unfontify-region-function
  3767. 'org-unfontify-region)
  3768. ;; Activate before-change-function
  3769. (org-set-local 'org-table-may-need-update t)
  3770. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3771. 'local)
  3772. ;; Check for running clock before killing a buffer
  3773. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3774. ;; Paragraphs and auto-filling
  3775. (org-set-autofill-regexps)
  3776. (setq indent-line-function 'org-indent-line-function)
  3777. (org-update-radio-target-regexp)
  3778. ;; Make sure dependence stuff works reliably, even for users who set it
  3779. ;; too late :-(
  3780. (if org-enforce-todo-dependencies
  3781. (add-hook 'org-blocker-hook
  3782. 'org-block-todo-from-children-or-siblings-or-parent)
  3783. (remove-hook 'org-blocker-hook
  3784. 'org-block-todo-from-children-or-siblings-or-parent))
  3785. (if org-enforce-todo-checkbox-dependencies
  3786. (add-hook 'org-blocker-hook
  3787. 'org-block-todo-from-checkboxes)
  3788. (remove-hook 'org-blocker-hook
  3789. 'org-block-todo-from-checkboxes))
  3790. ;; Comment characters
  3791. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3792. (org-set-local 'comment-padding " ")
  3793. ;; Align options lines
  3794. (org-set-local
  3795. 'align-mode-rules-list
  3796. '((org-in-buffer-settings
  3797. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3798. (modes . '(org-mode)))))
  3799. ;; Imenu
  3800. (org-set-local 'imenu-create-index-function
  3801. 'org-imenu-get-tree)
  3802. ;; Make isearch reveal context
  3803. (if (or (featurep 'xemacs)
  3804. (not (boundp 'outline-isearch-open-invisible-function)))
  3805. ;; Emacs 21 and XEmacs make use of the hook
  3806. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3807. ;; Emacs 22 deals with this through a special variable
  3808. (org-set-local 'outline-isearch-open-invisible-function
  3809. (lambda (&rest ignore) (org-show-context 'isearch))))
  3810. ;; Turn on org-beamer-mode?
  3811. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3812. ;; If empty file that did not turn on org-mode automatically, make it to.
  3813. (if (and org-insert-mode-line-in-empty-file
  3814. (interactive-p)
  3815. (= (point-min) (point-max)))
  3816. (insert "# -*- mode: org -*-\n\n"))
  3817. (unless org-inhibit-startup
  3818. (when org-startup-align-all-tables
  3819. (let ((bmp (buffer-modified-p)))
  3820. (org-table-map-tables 'org-table-align)
  3821. (set-buffer-modified-p bmp)))
  3822. (when org-startup-indented
  3823. (require 'org-indent)
  3824. (org-indent-mode 1))
  3825. (unless org-inhibit-startup-visibility-stuff
  3826. (org-set-startup-visibility))))
  3827. (when (fboundp 'abbrev-table-put)
  3828. (abbrev-table-put org-mode-abbrev-table
  3829. :parents (list text-mode-abbrev-table)))
  3830. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3831. (defun org-current-time ()
  3832. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3833. (if (> (car org-time-stamp-rounding-minutes) 1)
  3834. (let ((r (car org-time-stamp-rounding-minutes))
  3835. (time (decode-time)))
  3836. (apply 'encode-time
  3837. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3838. (nthcdr 2 time))))
  3839. (current-time)))
  3840. ;;;; Font-Lock stuff, including the activators
  3841. (defvar org-mouse-map (make-sparse-keymap))
  3842. (org-defkey org-mouse-map
  3843. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3844. (org-defkey org-mouse-map
  3845. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3846. (when org-mouse-1-follows-link
  3847. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3848. (when org-tab-follows-link
  3849. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3850. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3851. (require 'font-lock)
  3852. (defconst org-non-link-chars "]\t\n\r<>")
  3853. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3854. "shell" "elisp"))
  3855. (defvar org-link-types-re nil
  3856. "Matches a link that has a url-like prefix like \"http:\"")
  3857. (defvar org-link-re-with-space nil
  3858. "Matches a link with spaces, optional angular brackets around it.")
  3859. (defvar org-link-re-with-space2 nil
  3860. "Matches a link with spaces, optional angular brackets around it.")
  3861. (defvar org-link-re-with-space3 nil
  3862. "Matches a link with spaces, only for internal part in bracket links.")
  3863. (defvar org-angle-link-re nil
  3864. "Matches link with angular brackets, spaces are allowed.")
  3865. (defvar org-plain-link-re nil
  3866. "Matches plain link, without spaces.")
  3867. (defvar org-bracket-link-regexp nil
  3868. "Matches a link in double brackets.")
  3869. (defvar org-bracket-link-analytic-regexp nil
  3870. "Regular expression used to analyze links.
  3871. Here is what the match groups contain after a match:
  3872. 1: http:
  3873. 2: http
  3874. 3: path
  3875. 4: [desc]
  3876. 5: desc")
  3877. (defvar org-bracket-link-analytic-regexp++ nil
  3878. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3879. (defvar org-any-link-re nil
  3880. "Regular expression matching any link.")
  3881. (defun org-make-link-regexps ()
  3882. "Update the link regular expressions.
  3883. This should be called after the variable `org-link-types' has changed."
  3884. (setq org-link-types-re
  3885. (concat
  3886. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  3887. org-link-re-with-space
  3888. (concat
  3889. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3890. "\\([^" org-non-link-chars " ]"
  3891. "[^" org-non-link-chars "]*"
  3892. "[^" org-non-link-chars " ]\\)>?")
  3893. org-link-re-with-space2
  3894. (concat
  3895. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3896. "\\([^" org-non-link-chars " ]"
  3897. "[^\t\n\r]*"
  3898. "[^" org-non-link-chars " ]\\)>?")
  3899. org-link-re-with-space3
  3900. (concat
  3901. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3902. "\\([^" org-non-link-chars " ]"
  3903. "[^\t\n\r]*\\)")
  3904. org-angle-link-re
  3905. (concat
  3906. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3907. "\\([^" org-non-link-chars " ]"
  3908. "[^" org-non-link-chars "]*"
  3909. "\\)>")
  3910. org-plain-link-re
  3911. (concat
  3912. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3913. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  3914. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3915. org-bracket-link-regexp
  3916. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3917. org-bracket-link-analytic-regexp
  3918. (concat
  3919. "\\[\\["
  3920. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  3921. "\\([^]]+\\)"
  3922. "\\]"
  3923. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3924. "\\]")
  3925. org-bracket-link-analytic-regexp++
  3926. (concat
  3927. "\\[\\["
  3928. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3929. "\\([^]]+\\)"
  3930. "\\]"
  3931. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3932. "\\]")
  3933. org-any-link-re
  3934. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3935. org-angle-link-re "\\)\\|\\("
  3936. org-plain-link-re "\\)")))
  3937. (org-make-link-regexps)
  3938. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3939. "Regular expression for fast time stamp matching.")
  3940. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3941. "Regular expression for fast time stamp matching.")
  3942. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3943. "Regular expression matching time strings for analysis.
  3944. This one does not require the space after the date, so it can be used
  3945. on a string that terminates immediately after the date.")
  3946. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3947. "Regular expression matching time strings for analysis.")
  3948. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3949. "Regular expression matching time stamps, with groups.")
  3950. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3951. "Regular expression matching time stamps (also [..]), with groups.")
  3952. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3953. "Regular expression matching a time stamp range.")
  3954. (defconst org-tr-regexp-both
  3955. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3956. "Regular expression matching a time stamp range.")
  3957. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3958. org-ts-regexp "\\)?")
  3959. "Regular expression matching a time stamp or time stamp range.")
  3960. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3961. org-ts-regexp-both "\\)?")
  3962. "Regular expression matching a time stamp or time stamp range.
  3963. The time stamps may be either active or inactive.")
  3964. (defvar org-emph-face nil)
  3965. (defun org-do-emphasis-faces (limit)
  3966. "Run through the buffer and add overlays to links."
  3967. (let (rtn a)
  3968. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3969. (if (not (= (char-after (match-beginning 3))
  3970. (char-after (match-beginning 4))))
  3971. (progn
  3972. (setq rtn t)
  3973. (setq a (assoc (match-string 3) org-emphasis-alist))
  3974. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3975. 'face
  3976. (nth 1 a))
  3977. (and (nth 4 a)
  3978. (org-remove-flyspell-overlays-in
  3979. (match-beginning 0) (match-end 0)))
  3980. (add-text-properties (match-beginning 2) (match-end 2)
  3981. '(font-lock-multiline t))
  3982. (when org-hide-emphasis-markers
  3983. (add-text-properties (match-end 4) (match-beginning 5)
  3984. '(invisible org-link))
  3985. (add-text-properties (match-beginning 3) (match-end 3)
  3986. '(invisible org-link)))))
  3987. (backward-char 1))
  3988. rtn))
  3989. (defun org-emphasize (&optional char)
  3990. "Insert or change an emphasis, i.e. a font like bold or italic.
  3991. If there is an active region, change that region to a new emphasis.
  3992. If there is no region, just insert the marker characters and position
  3993. the cursor between them.
  3994. CHAR should be either the marker character, or the first character of the
  3995. HTML tag associated with that emphasis. If CHAR is a space, the means
  3996. to remove the emphasis of the selected region.
  3997. If char is not given (for example in an interactive call) it
  3998. will be prompted for."
  3999. (interactive)
  4000. (let ((eal org-emphasis-alist) e det
  4001. (erc org-emphasis-regexp-components)
  4002. (prompt "")
  4003. (string "") beg end move tag c s)
  4004. (if (org-region-active-p)
  4005. (setq beg (region-beginning) end (region-end)
  4006. string (buffer-substring beg end))
  4007. (setq move t))
  4008. (while (setq e (pop eal))
  4009. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4010. c (aref tag 0))
  4011. (push (cons c (string-to-char (car e))) det)
  4012. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4013. (substring tag 1)))))
  4014. (setq det (nreverse det))
  4015. (unless char
  4016. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4017. (setq char (read-char-exclusive)))
  4018. (setq char (or (cdr (assoc char det)) char))
  4019. (if (equal char ?\ )
  4020. (setq s "" move nil)
  4021. (unless (assoc (char-to-string char) org-emphasis-alist)
  4022. (error "No such emphasis marker: \"%c\"" char))
  4023. (setq s (char-to-string char)))
  4024. (while (and (> (length string) 1)
  4025. (equal (substring string 0 1) (substring string -1))
  4026. (assoc (substring string 0 1) org-emphasis-alist))
  4027. (setq string (substring string 1 -1)))
  4028. (setq string (concat s string s))
  4029. (if beg (delete-region beg end))
  4030. (unless (or (bolp)
  4031. (string-match (concat "[" (nth 0 erc) "\n]")
  4032. (char-to-string (char-before (point)))))
  4033. (insert " "))
  4034. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4035. (char-to-string (char-after (point))))
  4036. (insert " ") (backward-char 1))
  4037. (insert string)
  4038. (and move (backward-char 1))))
  4039. (defconst org-nonsticky-props
  4040. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4041. (defsubst org-rear-nonsticky-at (pos)
  4042. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4043. (defun org-activate-plain-links (limit)
  4044. "Run through the buffer and add overlays to links."
  4045. (catch 'exit
  4046. (let (f)
  4047. (if (re-search-forward org-plain-link-re limit t)
  4048. (progn
  4049. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4050. (setq f (get-text-property (match-beginning 0) 'face))
  4051. (if (or (eq f 'org-tag)
  4052. (and (listp f) (memq 'org-tag f)))
  4053. nil
  4054. (add-text-properties (match-beginning 0) (match-end 0)
  4055. (list 'mouse-face 'highlight
  4056. 'face 'org-link
  4057. 'keymap org-mouse-map))
  4058. (org-rear-nonsticky-at (match-end 0)))
  4059. t)))))
  4060. (defun org-activate-code (limit)
  4061. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4062. (progn
  4063. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4064. (remove-text-properties (match-beginning 0) (match-end 0)
  4065. '(display t invisible t intangible t))
  4066. t)))
  4067. (defun org-fontify-meta-lines-and-blocks (limit)
  4068. "Fontify #+ lines and blocks, in the correct ways."
  4069. (let ((case-fold-search t))
  4070. (if (re-search-forward
  4071. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4072. limit t)
  4073. (let ((beg (match-beginning 0))
  4074. (beg1 (line-beginning-position 2))
  4075. (dc1 (downcase (match-string 2)))
  4076. (dc3 (downcase (match-string 3)))
  4077. end end1 quoting block-type)
  4078. (cond
  4079. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4080. ;; a single line of backend-specific content
  4081. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4082. (remove-text-properties (match-beginning 0) (match-end 0)
  4083. '(display t invisible t intangible t))
  4084. (add-text-properties (match-beginning 1) (match-end 3)
  4085. '(font-lock-fontified t face org-meta-line))
  4086. (add-text-properties (match-beginning 6) (match-end 6)
  4087. '(font-lock-fontified t face org-block))
  4088. t)
  4089. ((and (match-end 4) (equal dc3 "begin"))
  4090. ;; Truely a block
  4091. (setq block-type (downcase (match-string 5))
  4092. quoting (member block-type org-protecting-blocks))
  4093. (when (re-search-forward
  4094. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4095. nil t) ;; on purpose, we look further than LIMIT
  4096. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4097. (when quoting
  4098. (remove-text-properties beg end
  4099. '(display t invisible t intangible t)))
  4100. (add-text-properties
  4101. beg end
  4102. '(font-lock-fontified t font-lock-multiline t))
  4103. (add-text-properties beg beg1 '(face org-meta-line))
  4104. (add-text-properties end1 end '(face org-meta-line))
  4105. (cond
  4106. (quoting
  4107. (add-text-properties beg1 end1 '(face org-block)))
  4108. ((string= block-type "quote")
  4109. (add-text-properties beg1 end1 '(face org-quote)))
  4110. ((string= block-type "verse")
  4111. (add-text-properties beg1 end1 '(face org-verse))))
  4112. t))
  4113. ((not (member (char-after beg) '(?\ ?\t)))
  4114. ;; just any other in-buffer setting, but not indented
  4115. (add-text-properties
  4116. beg (match-end 0)
  4117. '(font-lock-fontified t face org-meta-line))
  4118. t)
  4119. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4120. "orgtbl:" "tblfm:" "tblname:"))
  4121. (and (match-end 4) (equal dc3 "attr")))
  4122. (add-text-properties
  4123. beg (match-end 0)
  4124. '(font-lock-fontified t face org-meta-line))
  4125. t)
  4126. ((member dc3 '(" " ""))
  4127. (add-text-properties
  4128. beg (match-end 0)
  4129. '(font-lock-fontified t face font-lock-comment-face)))
  4130. (t nil))))))
  4131. (defun org-activate-angle-links (limit)
  4132. "Run through the buffer and add overlays to links."
  4133. (if (re-search-forward org-angle-link-re limit t)
  4134. (progn
  4135. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4136. (add-text-properties (match-beginning 0) (match-end 0)
  4137. (list 'mouse-face 'highlight
  4138. 'keymap org-mouse-map))
  4139. (org-rear-nonsticky-at (match-end 0))
  4140. t)))
  4141. (defun org-activate-footnote-links (limit)
  4142. "Run through the buffer and add overlays to links."
  4143. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4144. limit t)
  4145. (progn
  4146. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4147. (add-text-properties (match-beginning 2) (match-end 2)
  4148. (list 'mouse-face 'highlight
  4149. 'keymap org-mouse-map
  4150. 'help-echo
  4151. (if (= (point-at-bol) (match-beginning 2))
  4152. "Footnote definition"
  4153. "Footnote reference")
  4154. ))
  4155. (org-rear-nonsticky-at (match-end 2))
  4156. t)))
  4157. (defun org-activate-bracket-links (limit)
  4158. "Run through the buffer and add overlays to bracketed links."
  4159. (if (re-search-forward org-bracket-link-regexp limit t)
  4160. (let* ((help (concat "LINK: "
  4161. (org-match-string-no-properties 1)))
  4162. ;; FIXME: above we should remove the escapes.
  4163. ;; but that requires another match, protecting match data,
  4164. ;; a lot of overhead for font-lock.
  4165. (ip (org-maybe-intangible
  4166. (list 'invisible 'org-link
  4167. 'keymap org-mouse-map 'mouse-face 'highlight
  4168. 'font-lock-multiline t 'help-echo help)))
  4169. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4170. 'font-lock-multiline t 'help-echo help)))
  4171. ;; We need to remove the invisible property here. Table narrowing
  4172. ;; may have made some of this invisible.
  4173. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4174. (remove-text-properties (match-beginning 0) (match-end 0)
  4175. '(invisible nil))
  4176. (if (match-end 3)
  4177. (progn
  4178. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4179. (org-rear-nonsticky-at (match-beginning 3))
  4180. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4181. (org-rear-nonsticky-at (match-end 3))
  4182. (add-text-properties (match-end 3) (match-end 0) ip)
  4183. (org-rear-nonsticky-at (match-end 0)))
  4184. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4185. (org-rear-nonsticky-at (match-beginning 1))
  4186. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4187. (org-rear-nonsticky-at (match-end 1))
  4188. (add-text-properties (match-end 1) (match-end 0) ip)
  4189. (org-rear-nonsticky-at (match-end 0)))
  4190. t)))
  4191. (defun org-activate-dates (limit)
  4192. "Run through the buffer and add overlays to dates."
  4193. (if (re-search-forward org-tsr-regexp-both limit t)
  4194. (progn
  4195. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4196. (add-text-properties (match-beginning 0) (match-end 0)
  4197. (list 'mouse-face 'highlight
  4198. 'keymap org-mouse-map))
  4199. (org-rear-nonsticky-at (match-end 0))
  4200. (when org-display-custom-times
  4201. (if (match-end 3)
  4202. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4203. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4204. t)))
  4205. (defvar org-target-link-regexp nil
  4206. "Regular expression matching radio targets in plain text.")
  4207. (make-variable-buffer-local 'org-target-link-regexp)
  4208. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4209. "Regular expression matching a link target.")
  4210. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4211. "Regular expression matching a radio target.")
  4212. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4213. "Regular expression matching any target.")
  4214. (defun org-activate-target-links (limit)
  4215. "Run through the buffer and add overlays to target matches."
  4216. (when org-target-link-regexp
  4217. (let ((case-fold-search t))
  4218. (if (re-search-forward org-target-link-regexp limit t)
  4219. (progn
  4220. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4221. (add-text-properties (match-beginning 0) (match-end 0)
  4222. (list 'mouse-face 'highlight
  4223. 'keymap org-mouse-map
  4224. 'help-echo "Radio target link"
  4225. 'org-linked-text t))
  4226. (org-rear-nonsticky-at (match-end 0))
  4227. t)))))
  4228. (defun org-update-radio-target-regexp ()
  4229. "Find all radio targets in this file and update the regular expression."
  4230. (interactive)
  4231. (when (memq 'radio org-activate-links)
  4232. (setq org-target-link-regexp
  4233. (org-make-target-link-regexp (org-all-targets 'radio)))
  4234. (org-restart-font-lock)))
  4235. (defun org-hide-wide-columns (limit)
  4236. (let (s e)
  4237. (setq s (text-property-any (point) (or limit (point-max))
  4238. 'org-cwidth t))
  4239. (when s
  4240. (setq e (next-single-property-change s 'org-cwidth))
  4241. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4242. (goto-char e)
  4243. t)))
  4244. (defvar org-latex-and-specials-regexp nil
  4245. "Regular expression for highlighting export special stuff.")
  4246. (defvar org-match-substring-regexp)
  4247. (defvar org-match-substring-with-braces-regexp)
  4248. ;; This should be with the exporter code, but we also use if for font-locking
  4249. (defconst org-export-html-special-string-regexps
  4250. '(("\\\\-" . "&shy;")
  4251. ("---\\([^-]\\)" . "&mdash;\\1")
  4252. ("--\\([^-]\\)" . "&ndash;\\1")
  4253. ("\\.\\.\\." . "&hellip;"))
  4254. "Regular expressions for special string conversion.")
  4255. (defun org-compute-latex-and-specials-regexp ()
  4256. "Compute regular expression for stuff treated specially by exporters."
  4257. (if (not org-highlight-latex-fragments-and-specials)
  4258. (org-set-local 'org-latex-and-specials-regexp nil)
  4259. (require 'org-exp)
  4260. (let*
  4261. ((matchers (plist-get org-format-latex-options :matchers))
  4262. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4263. org-latex-regexps)))
  4264. (org-export-allow-BIND nil)
  4265. (options (org-combine-plists (org-default-export-plist)
  4266. (org-infile-export-plist)))
  4267. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4268. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4269. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4270. (org-export-html-expand (plist-get options :expand-quoted-html))
  4271. (org-export-with-special-strings (plist-get options :special-strings))
  4272. (re-sub
  4273. (cond
  4274. ((equal org-export-with-sub-superscripts '{})
  4275. (list org-match-substring-with-braces-regexp))
  4276. (org-export-with-sub-superscripts
  4277. (list org-match-substring-regexp))
  4278. (t nil)))
  4279. (re-latex
  4280. (if org-export-with-LaTeX-fragments
  4281. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4282. (re-macros
  4283. (if org-export-with-TeX-macros
  4284. (list (concat "\\\\"
  4285. (regexp-opt
  4286. (append (mapcar 'car org-html-entities)
  4287. (if (boundp 'org-latex-entities)
  4288. (mapcar (lambda (x)
  4289. (or (car-safe x) x))
  4290. org-latex-entities)
  4291. nil))
  4292. 'words))) ; FIXME
  4293. ))
  4294. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4295. (re-special (if org-export-with-special-strings
  4296. (mapcar (lambda (x) (car x))
  4297. org-export-html-special-string-regexps)))
  4298. (re-rest
  4299. (delq nil
  4300. (list
  4301. (if org-export-html-expand "@<[^>\n]+>")
  4302. ))))
  4303. (org-set-local
  4304. 'org-latex-and-specials-regexp
  4305. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4306. re-rest) "\\|")))))
  4307. (defun org-do-latex-and-special-faces (limit)
  4308. "Run through the buffer and add overlays to links."
  4309. (when org-latex-and-specials-regexp
  4310. (let (rtn d)
  4311. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4312. limit t))
  4313. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4314. 'face))
  4315. '(org-code org-verbatim underline)))
  4316. (progn
  4317. (setq rtn t
  4318. d (cond ((member (char-after (1+ (match-beginning 0)))
  4319. '(?_ ?^)) 1)
  4320. (t 0)))
  4321. (font-lock-prepend-text-property
  4322. (+ d (match-beginning 0)) (match-end 0)
  4323. 'face 'org-latex-and-export-specials)
  4324. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4325. '(font-lock-multiline t)))))
  4326. rtn)))
  4327. (defun org-restart-font-lock ()
  4328. "Restart font-lock-mode, to force refontification."
  4329. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4330. (font-lock-mode -1)
  4331. (font-lock-mode 1)))
  4332. (defun org-all-targets (&optional radio)
  4333. "Return a list of all targets in this file.
  4334. With optional argument RADIO, only find radio targets."
  4335. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4336. rtn)
  4337. (save-excursion
  4338. (goto-char (point-min))
  4339. (while (re-search-forward re nil t)
  4340. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4341. rtn)))
  4342. (defun org-make-target-link-regexp (targets)
  4343. "Make regular expression matching all strings in TARGETS.
  4344. The regular expression finds the targets also if there is a line break
  4345. between words."
  4346. (and targets
  4347. (concat
  4348. "\\<\\("
  4349. (mapconcat
  4350. (lambda (x)
  4351. (while (string-match " +" x)
  4352. (setq x (replace-match "\\s-+" t t x)))
  4353. x)
  4354. targets
  4355. "\\|")
  4356. "\\)\\>")))
  4357. (defun org-activate-tags (limit)
  4358. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4359. (progn
  4360. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4361. (add-text-properties (match-beginning 1) (match-end 1)
  4362. (list 'mouse-face 'highlight
  4363. 'keymap org-mouse-map))
  4364. (org-rear-nonsticky-at (match-end 1))
  4365. t)))
  4366. (defun org-outline-level ()
  4367. "Compute the outline level of the heading at point.
  4368. This function assumes that the cursor is at the beginning of a line matched
  4369. by outline-regexp. Otherwise it returns garbage.
  4370. If this is called at a normal headline, the level is the number of stars.
  4371. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4372. For plain list items, if they are matched by `outline-regexp', this returns
  4373. 1000 plus the line indentation."
  4374. (save-excursion
  4375. (looking-at outline-regexp)
  4376. (if (match-beginning 1)
  4377. (+ (org-get-string-indentation (match-string 1)) 1000)
  4378. (1- (- (match-end 0) (match-beginning 0))))))
  4379. (defvar org-font-lock-keywords nil)
  4380. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4381. "Regular expression matching a property line.")
  4382. (defvar org-font-lock-hook nil
  4383. "Functions to be called for special font lock stuff.")
  4384. (defun org-font-lock-hook (limit)
  4385. (run-hook-with-args 'org-font-lock-hook limit))
  4386. (defun org-set-font-lock-defaults ()
  4387. (let* ((em org-fontify-emphasized-text)
  4388. (lk org-activate-links)
  4389. (org-font-lock-extra-keywords
  4390. (list
  4391. ;; Call the hook
  4392. '(org-font-lock-hook)
  4393. ;; Headlines
  4394. `(,(if org-fontify-whole-heading-line
  4395. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4396. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4397. (1 (org-get-level-face 1))
  4398. (2 (org-get-level-face 2))
  4399. (3 (org-get-level-face 3)))
  4400. ;; Table lines
  4401. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4402. (1 'org-table t))
  4403. ;; Table internals
  4404. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4405. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4406. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4407. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4408. ;; Drawers
  4409. (list org-drawer-regexp '(0 'org-special-keyword t))
  4410. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4411. ;; Properties
  4412. (list org-property-re
  4413. '(1 'org-special-keyword t)
  4414. '(3 'org-property-value t))
  4415. ;; Links
  4416. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4417. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4418. (if (memq 'plain lk) '(org-activate-plain-links))
  4419. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4420. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4421. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4422. (if (memq 'footnote lk) '(org-activate-footnote-links
  4423. (2 'org-footnote t)))
  4424. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4425. '(org-hide-wide-columns (0 nil append))
  4426. ;; TODO lines
  4427. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4428. '(1 (org-get-todo-face 1) t))
  4429. ;; DONE
  4430. (if org-fontify-done-headline
  4431. (list (concat "^[*]+ +\\<\\("
  4432. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4433. "\\)\\(.*\\)")
  4434. '(2 'org-headline-done t))
  4435. nil)
  4436. ;; Priorities
  4437. '(org-font-lock-add-priority-faces)
  4438. ;; Tags
  4439. '(org-font-lock-add-tag-faces)
  4440. ;; Special keywords
  4441. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4442. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4443. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4444. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4445. ;; Emphasis
  4446. (if em
  4447. (if (featurep 'xemacs)
  4448. '(org-do-emphasis-faces (0 nil append))
  4449. '(org-do-emphasis-faces)))
  4450. ;; Checkboxes
  4451. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4452. 2 'org-checkbox prepend)
  4453. (if org-provide-checkbox-statistics
  4454. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4455. (0 (org-get-checkbox-statistics-face) t)))
  4456. ;; Description list items
  4457. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4458. 2 'bold prepend)
  4459. ;; ARCHIVEd headings
  4460. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4461. '(1 'org-archived prepend))
  4462. ;; Specials
  4463. '(org-do-latex-and-special-faces)
  4464. ;; Code
  4465. '(org-activate-code (1 'org-code t))
  4466. ;; COMMENT
  4467. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4468. "\\|" org-quote-string "\\)\\>")
  4469. '(1 'org-special-keyword t))
  4470. '("^#.*" (0 'font-lock-comment-face t))
  4471. ;; Blocks and meta lines
  4472. '(org-fontify-meta-lines-and-blocks)
  4473. )))
  4474. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4475. ;; Now set the full font-lock-keywords
  4476. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4477. (org-set-local 'font-lock-defaults
  4478. '(org-font-lock-keywords t nil nil backward-paragraph))
  4479. (kill-local-variable 'font-lock-keywords) nil))
  4480. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4481. "Fontify string S like in Org-mode"
  4482. (with-temp-buffer
  4483. (insert s)
  4484. (let ((org-odd-levels-only odd-levels))
  4485. (org-mode)
  4486. (font-lock-fontify-buffer)
  4487. (buffer-string))))
  4488. (defvar org-m nil)
  4489. (defvar org-l nil)
  4490. (defvar org-f nil)
  4491. (defun org-get-level-face (n)
  4492. "Get the right face for match N in font-lock matching of headlines."
  4493. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4494. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4495. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4496. (cond
  4497. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4498. ((eq n 2) org-f)
  4499. (t (if org-level-color-stars-only nil org-f))))
  4500. (defun org-get-todo-face (kwd)
  4501. "Get the right face for a TODO keyword KWD.
  4502. If KWD is a number, get the corresponding match group."
  4503. (if (numberp kwd) (setq kwd (match-string kwd)))
  4504. (or (cdr (assoc kwd org-todo-keyword-faces))
  4505. (and (member kwd org-done-keywords) 'org-done)
  4506. 'org-todo))
  4507. (defun org-font-lock-add-tag-faces (limit)
  4508. "Add the special tag faces."
  4509. (when (and org-tag-faces org-tags-special-faces-re)
  4510. (while (re-search-forward org-tags-special-faces-re limit t)
  4511. (add-text-properties (match-beginning 1) (match-end 1)
  4512. (list 'face (org-get-tag-face 1)
  4513. 'font-lock-fontified t))
  4514. (backward-char 1))))
  4515. (defun org-font-lock-add-priority-faces (limit)
  4516. "Add the special priority faces."
  4517. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4518. (add-text-properties
  4519. (match-beginning 0) (match-end 0)
  4520. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4521. org-priority-faces))
  4522. 'org-special-keyword)
  4523. 'font-lock-fontified t))))
  4524. (defun org-get-tag-face (kwd)
  4525. "Get the right face for a TODO keyword KWD.
  4526. If KWD is a number, get the corresponding match group."
  4527. (if (numberp kwd) (setq kwd (match-string kwd)))
  4528. (or (cdr (assoc kwd org-tag-faces))
  4529. 'org-tag))
  4530. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4531. "Remove fontification and activation overlays from links."
  4532. (font-lock-default-unfontify-region beg end)
  4533. (let* ((buffer-undo-list t)
  4534. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4535. (inhibit-modification-hooks t)
  4536. deactivate-mark buffer-file-name buffer-file-truename)
  4537. (remove-text-properties
  4538. beg end
  4539. (if org-indent-mode
  4540. ;; also remove line-prefix and wrap-prefix properties
  4541. '(mouse-face t keymap t org-linked-text t
  4542. invisible t intangible t
  4543. line-prefix t wrap-prefix t
  4544. org-no-flyspell t)
  4545. '(mouse-face t keymap t org-linked-text t
  4546. invisible t intangible t
  4547. org-no-flyspell t)))))
  4548. ;;;; Visibility cycling, including org-goto and indirect buffer
  4549. ;;; Cycling
  4550. (defvar org-cycle-global-status nil)
  4551. (make-variable-buffer-local 'org-cycle-global-status)
  4552. (defvar org-cycle-subtree-status nil)
  4553. (make-variable-buffer-local 'org-cycle-subtree-status)
  4554. ;;;###autoload
  4555. (defvar org-inlinetask-min-level)
  4556. (defun org-cycle (&optional arg)
  4557. "TAB-action and visibility cycling for Org-mode.
  4558. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4559. is outline visibility cycling, but it also invokes other actions
  4560. in special contexts.
  4561. - When this function is called with a prefix argument, rotate the entire
  4562. buffer through 3 states (global cycling)
  4563. 1. OVERVIEW: Show only top-level headlines.
  4564. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4565. 3. SHOW ALL: Show everything.
  4566. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4567. determined by the variable `org-startup-folded', and by any VISIBILITY
  4568. properties in the buffer.
  4569. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4570. including any drawers.
  4571. - When inside a table, re-align the table and move to the next field.
  4572. - When point is at the beginning of a headline, rotate the subtree started
  4573. by this line through 3 different states (local cycling)
  4574. 1. FOLDED: Only the main headline is shown.
  4575. 2. CHILDREN: The main headline and the direct children are shown.
  4576. From this state, you can move to one of the children
  4577. and zoom in further.
  4578. 3. SUBTREE: Show the entire subtree, including body text.
  4579. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4580. - When there is a numeric prefix, go up to a heading with level ARG, do
  4581. a `show-subtree' and return to the previous cursor position. If ARG
  4582. is negative, go up that many levels.
  4583. - When point is not at the beginning of a headline, execute the global
  4584. binding for TAB, which is re-indenting the line. See the option
  4585. `org-cycle-emulate-tab' for details.
  4586. - Special case: if point is at the beginning of the buffer and there is
  4587. no headline in line 1, this function will act as if called with prefix arg.
  4588. But only if also the variable `org-cycle-global-at-bob' is t."
  4589. (interactive "P")
  4590. (org-load-modules-maybe)
  4591. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4592. (and org-cycle-level-after-item/entry-creation
  4593. (or (org-cycle-level)
  4594. (org-cycle-item-indentation))))
  4595. (let* ((limit-level
  4596. (or org-cycle-max-level
  4597. (and (boundp 'org-inlinetask-min-level)
  4598. org-inlinetask-min-level
  4599. (1- org-inlinetask-min-level))))
  4600. (nstars (and limit-level
  4601. (if org-odd-levels-only
  4602. (and limit-level (1- (* limit-level 2)))
  4603. limit-level)))
  4604. (outline-regexp
  4605. (cond
  4606. ((not (org-mode-p)) outline-regexp)
  4607. ((or (eq org-cycle-include-plain-lists 'integrate)
  4608. (and org-cycle-include-plain-lists (org-at-item-p)))
  4609. (concat "\\(?:\\*"
  4610. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4611. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4612. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4613. (bob-special (and org-cycle-global-at-bob (bobp)
  4614. (not (looking-at outline-regexp))))
  4615. (org-cycle-hook
  4616. (if bob-special
  4617. (delq 'org-optimize-window-after-visibility-change
  4618. (copy-sequence org-cycle-hook))
  4619. org-cycle-hook))
  4620. (pos (point)))
  4621. (if (or bob-special (equal arg '(4)))
  4622. ;; special case: use global cycling
  4623. (setq arg t))
  4624. (cond
  4625. ((equal arg '(16))
  4626. (org-set-startup-visibility)
  4627. (message "Startup visibility, plus VISIBILITY properties"))
  4628. ((equal arg '(64))
  4629. (show-all)
  4630. (message "Entire buffer visible, including drawers"))
  4631. ((org-at-table-p 'any)
  4632. ;; Enter the table or move to the next field in the table
  4633. (or (org-table-recognize-table.el)
  4634. (progn
  4635. (if arg (org-table-edit-field t)
  4636. (org-table-justify-field-maybe)
  4637. (call-interactively 'org-table-next-field)))))
  4638. ((run-hook-with-args-until-success
  4639. 'org-tab-after-check-for-table-hook))
  4640. ((eq arg t) ;; Global cycling
  4641. (org-cycle-internal-global))
  4642. ((and org-drawers org-drawer-regexp
  4643. (save-excursion
  4644. (beginning-of-line 1)
  4645. (looking-at org-drawer-regexp)))
  4646. ;; Toggle block visibility
  4647. (org-flag-drawer
  4648. (not (get-char-property (match-end 0) 'invisible))))
  4649. ((integerp arg)
  4650. ;; Show-subtree, ARG levels up from here.
  4651. (save-excursion
  4652. (org-back-to-heading)
  4653. (outline-up-heading (if (< arg 0) (- arg)
  4654. (- (funcall outline-level) arg)))
  4655. (org-show-subtree)))
  4656. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4657. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4658. (org-cycle-internal-local))
  4659. ;; TAB emulation and template completion
  4660. (buffer-read-only (org-back-to-heading))
  4661. ((run-hook-with-args-until-success
  4662. 'org-tab-after-check-for-cycling-hook))
  4663. ((org-try-structure-completion))
  4664. ((org-try-cdlatex-tab))
  4665. ((run-hook-with-args-until-success
  4666. 'org-tab-before-tab-emulation-hook))
  4667. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4668. (or (not (bolp))
  4669. (not (looking-at outline-regexp))))
  4670. (call-interactively (global-key-binding "\t")))
  4671. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4672. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4673. (or (and (eq org-cycle-emulate-tab 'white)
  4674. (= (match-end 0) (point-at-eol)))
  4675. (and (eq org-cycle-emulate-tab 'whitestart)
  4676. (>= (match-end 0) pos))))
  4677. t
  4678. (eq org-cycle-emulate-tab t))
  4679. (call-interactively (global-key-binding "\t")))
  4680. (t (save-excursion
  4681. (org-back-to-heading)
  4682. (org-cycle)))))))
  4683. (defun org-cycle-internal-global ()
  4684. "Do the global cycling action."
  4685. (cond
  4686. ((and (eq last-command this-command)
  4687. (eq org-cycle-global-status 'overview))
  4688. ;; We just created the overview - now do table of contents
  4689. ;; This can be slow in very large buffers, so indicate action
  4690. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4691. (message "CONTENTS...")
  4692. (org-content)
  4693. (message "CONTENTS...done")
  4694. (setq org-cycle-global-status 'contents)
  4695. (run-hook-with-args 'org-cycle-hook 'contents))
  4696. ((and (eq last-command this-command)
  4697. (eq org-cycle-global-status 'contents))
  4698. ;; We just showed the table of contents - now show everything
  4699. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4700. (show-all)
  4701. (message "SHOW ALL")
  4702. (setq org-cycle-global-status 'all)
  4703. (run-hook-with-args 'org-cycle-hook 'all))
  4704. (t
  4705. ;; Default action: go to overview
  4706. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4707. (org-overview)
  4708. (message "OVERVIEW")
  4709. (setq org-cycle-global-status 'overview)
  4710. (run-hook-with-args 'org-cycle-hook 'overview))))
  4711. (defun org-cycle-internal-local ()
  4712. "Do the local cycling action."
  4713. (org-back-to-heading)
  4714. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4715. ;; First, some boundaries
  4716. (save-excursion
  4717. (org-back-to-heading)
  4718. (setq level (funcall outline-level))
  4719. (save-excursion
  4720. (beginning-of-line 2)
  4721. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4722. ; XEmacs does not have `next-single-char-property-change'
  4723. ; I'm not sure about Emacs 21.
  4724. (while (and (not (eobp)) ;; this is like `next-line'
  4725. (get-char-property (1- (point)) 'invisible))
  4726. (beginning-of-line 2))
  4727. (while (and (not (eobp)) ;; this is like `next-line'
  4728. (get-char-property (1- (point)) 'invisible))
  4729. (goto-char (next-single-char-property-change (point) 'invisible))
  4730. ;;;??? (or (bolp) (beginning-of-line 2))))
  4731. (and (eolp) (beginning-of-line 2))))
  4732. (setq eol (point)))
  4733. (outline-end-of-heading) (setq eoh (point))
  4734. (save-excursion
  4735. (outline-next-heading)
  4736. (setq has-children (and (org-at-heading-p t)
  4737. (> (funcall outline-level) level))))
  4738. (org-end-of-subtree t)
  4739. (unless (eobp)
  4740. (skip-chars-forward " \t\n")
  4741. (beginning-of-line 1) ; in case this is an item
  4742. )
  4743. (setq eos (if (eobp) (point) (1- (point)))))
  4744. ;; Find out what to do next and set `this-command'
  4745. (cond
  4746. ((= eos eoh)
  4747. ;; Nothing is hidden behind this heading
  4748. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4749. (message "EMPTY ENTRY")
  4750. (setq org-cycle-subtree-status nil)
  4751. (save-excursion
  4752. (goto-char eos)
  4753. (outline-next-heading)
  4754. (if (org-invisible-p) (org-flag-heading nil))))
  4755. ((and (or (>= eol eos)
  4756. (not (string-match "\\S-" (buffer-substring eol eos))))
  4757. (or has-children
  4758. (not (setq children-skipped
  4759. org-cycle-skip-children-state-if-no-children))))
  4760. ;; Entire subtree is hidden in one line: children view
  4761. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4762. (org-show-entry)
  4763. (show-children)
  4764. (message "CHILDREN")
  4765. (save-excursion
  4766. (goto-char eos)
  4767. (outline-next-heading)
  4768. (if (org-invisible-p) (org-flag-heading nil)))
  4769. (setq org-cycle-subtree-status 'children)
  4770. (run-hook-with-args 'org-cycle-hook 'children))
  4771. ((or children-skipped
  4772. (and (eq last-command this-command)
  4773. (eq org-cycle-subtree-status 'children)))
  4774. ;; We just showed the children, or no children are there,
  4775. ;; now show everything.
  4776. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4777. (org-show-subtree)
  4778. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4779. (setq org-cycle-subtree-status 'subtree)
  4780. (run-hook-with-args 'org-cycle-hook 'subtree))
  4781. (t
  4782. ;; Default action: hide the subtree.
  4783. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4784. (hide-subtree)
  4785. (message "FOLDED")
  4786. (setq org-cycle-subtree-status 'folded)
  4787. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4788. ;;;###autoload
  4789. (defun org-global-cycle (&optional arg)
  4790. "Cycle the global visibility. For details see `org-cycle'.
  4791. With C-u prefix arg, switch to startup visibility.
  4792. With a numeric prefix, show all headlines up to that level."
  4793. (interactive "P")
  4794. (let ((org-cycle-include-plain-lists
  4795. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4796. (cond
  4797. ((integerp arg)
  4798. (show-all)
  4799. (hide-sublevels arg)
  4800. (setq org-cycle-global-status 'contents))
  4801. ((equal arg '(4))
  4802. (org-set-startup-visibility)
  4803. (message "Startup visibility, plus VISIBILITY properties."))
  4804. (t
  4805. (org-cycle '(4))))))
  4806. (defun org-set-startup-visibility ()
  4807. "Set the visibility required by startup options and properties."
  4808. (cond
  4809. ((eq org-startup-folded t)
  4810. (org-cycle '(4)))
  4811. ((eq org-startup-folded 'content)
  4812. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4813. (org-cycle '(4)) (org-cycle '(4)))))
  4814. (unless (eq org-startup-folded 'showeverything)
  4815. (if org-hide-block-startup (org-hide-block-all))
  4816. (org-set-visibility-according-to-property 'no-cleanup)
  4817. (org-cycle-hide-archived-subtrees 'all)
  4818. (org-cycle-hide-drawers 'all)
  4819. (org-cycle-show-empty-lines 'all)))
  4820. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4821. "Switch subtree visibilities according to :VISIBILITY: property."
  4822. (interactive)
  4823. (let (org-show-entry-below state)
  4824. (save-excursion
  4825. (goto-char (point-min))
  4826. (while (re-search-forward
  4827. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4828. nil t)
  4829. (setq state (match-string 1))
  4830. (save-excursion
  4831. (org-back-to-heading t)
  4832. (hide-subtree)
  4833. (org-reveal)
  4834. (cond
  4835. ((equal state '("fold" "folded"))
  4836. (hide-subtree))
  4837. ((equal state "children")
  4838. (org-show-hidden-entry)
  4839. (show-children))
  4840. ((equal state "content")
  4841. (save-excursion
  4842. (save-restriction
  4843. (org-narrow-to-subtree)
  4844. (org-content))))
  4845. ((member state '("all" "showall"))
  4846. (show-subtree)))))
  4847. (unless no-cleanup
  4848. (org-cycle-hide-archived-subtrees 'all)
  4849. (org-cycle-hide-drawers 'all)
  4850. (org-cycle-show-empty-lines 'all)))))
  4851. (defun org-overview ()
  4852. "Switch to overview mode, showing only top-level headlines.
  4853. Really, this shows all headlines with level equal or greater than the level
  4854. of the first headline in the buffer. This is important, because if the
  4855. first headline is not level one, then (hide-sublevels 1) gives confusing
  4856. results."
  4857. (interactive)
  4858. (let ((level (save-excursion
  4859. (goto-char (point-min))
  4860. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4861. (progn
  4862. (goto-char (match-beginning 0))
  4863. (funcall outline-level))))))
  4864. (and level (hide-sublevels level))))
  4865. (defun org-content (&optional arg)
  4866. "Show all headlines in the buffer, like a table of contents.
  4867. With numerical argument N, show content up to level N."
  4868. (interactive "P")
  4869. (save-excursion
  4870. ;; Visit all headings and show their offspring
  4871. (and (integerp arg) (org-overview))
  4872. (goto-char (point-max))
  4873. (catch 'exit
  4874. (while (and (progn (condition-case nil
  4875. (outline-previous-visible-heading 1)
  4876. (error (goto-char (point-min))))
  4877. t)
  4878. (looking-at outline-regexp))
  4879. (if (integerp arg)
  4880. (show-children (1- arg))
  4881. (show-branches))
  4882. (if (bobp) (throw 'exit nil))))))
  4883. (defun org-optimize-window-after-visibility-change (state)
  4884. "Adjust the window after a change in outline visibility.
  4885. This function is the default value of the hook `org-cycle-hook'."
  4886. (when (get-buffer-window (current-buffer))
  4887. (cond
  4888. ((eq state 'content) nil)
  4889. ((eq state 'all) nil)
  4890. ((eq state 'folded) nil)
  4891. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4892. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4893. (defun org-remove-empty-overlays-at (pos)
  4894. "Remove outline overlays that do not contain non-white stuff."
  4895. (mapc
  4896. (lambda (o)
  4897. (and (eq 'outline (org-overlay-get o 'invisible))
  4898. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4899. (org-overlay-end o))))
  4900. (org-delete-overlay o)))
  4901. (org-overlays-at pos)))
  4902. (defun org-clean-visibility-after-subtree-move ()
  4903. "Fix visibility issues after moving a subtree."
  4904. ;; First, find a reasonable region to look at:
  4905. ;; Start two siblings above, end three below
  4906. (let* ((beg (save-excursion
  4907. (and (org-get-last-sibling)
  4908. (org-get-last-sibling))
  4909. (point)))
  4910. (end (save-excursion
  4911. (and (org-get-next-sibling)
  4912. (org-get-next-sibling)
  4913. (org-get-next-sibling))
  4914. (if (org-at-heading-p)
  4915. (point-at-eol)
  4916. (point))))
  4917. (level (looking-at "\\*+"))
  4918. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4919. (save-excursion
  4920. (save-restriction
  4921. (narrow-to-region beg end)
  4922. (when re
  4923. ;; Properly fold already folded siblings
  4924. (goto-char (point-min))
  4925. (while (re-search-forward re nil t)
  4926. (if (and (not (org-invisible-p))
  4927. (save-excursion
  4928. (goto-char (point-at-eol)) (org-invisible-p)))
  4929. (hide-entry))))
  4930. (org-cycle-show-empty-lines 'overview)
  4931. (org-cycle-hide-drawers 'overview)))))
  4932. (defun org-cycle-show-empty-lines (state)
  4933. "Show empty lines above all visible headlines.
  4934. The region to be covered depends on STATE when called through
  4935. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4936. entire buffer covered. Note that an empty line is only shown if there
  4937. are at least `org-cycle-separator-lines' empty lines before the headline."
  4938. (when (not (= org-cycle-separator-lines 0))
  4939. (save-excursion
  4940. (let* ((n (abs org-cycle-separator-lines))
  4941. (re (cond
  4942. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4943. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4944. (t (let ((ns (number-to-string (- n 2))))
  4945. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4946. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4947. beg end b e)
  4948. (cond
  4949. ((memq state '(overview contents t))
  4950. (setq beg (point-min) end (point-max)))
  4951. ((memq state '(children folded))
  4952. (setq beg (point) end (progn (org-end-of-subtree t t)
  4953. (beginning-of-line 2)
  4954. (point)))))
  4955. (when beg
  4956. (goto-char beg)
  4957. (while (re-search-forward re end t)
  4958. (unless (get-char-property (match-end 1) 'invisible)
  4959. (setq e (match-end 1))
  4960. (if (< org-cycle-separator-lines 0)
  4961. (setq b (save-excursion
  4962. (goto-char (match-beginning 0))
  4963. (org-back-over-empty-lines)
  4964. (if (save-excursion
  4965. (goto-char (max (point-min) (1- (point))))
  4966. (org-on-heading-p))
  4967. (1- (point))
  4968. (point))))
  4969. (setq b (match-beginning 1)))
  4970. (outline-flag-region b e nil)))))))
  4971. ;; Never hide empty lines at the end of the file.
  4972. (save-excursion
  4973. (goto-char (point-max))
  4974. (outline-previous-heading)
  4975. (outline-end-of-heading)
  4976. (if (and (looking-at "[ \t\n]+")
  4977. (= (match-end 0) (point-max)))
  4978. (outline-flag-region (point) (match-end 0) nil))))
  4979. (defun org-show-empty-lines-in-parent ()
  4980. "Move to the parent and re-show empty lines before visible headlines."
  4981. (save-excursion
  4982. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4983. (org-cycle-show-empty-lines context))))
  4984. (defun org-files-list ()
  4985. "Return `org-agenda-files' list, plus all open org-mode files.
  4986. This is useful for operations that need to scan all of a user's
  4987. open and agenda-wise Org files."
  4988. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  4989. (dolist (buf (buffer-list))
  4990. (with-current-buffer buf
  4991. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  4992. (let ((file (expand-file-name (buffer-file-name))))
  4993. (unless (member file files)
  4994. (push file files))))))
  4995. files))
  4996. (defsubst org-entry-beginning-position ()
  4997. "Return the beginning position of the current entry."
  4998. (save-excursion (outline-back-to-heading t) (point)))
  4999. (defsubst org-entry-end-position ()
  5000. "Return the end position of the current entry."
  5001. (save-excursion (outline-next-heading) (point)))
  5002. (defun org-cycle-hide-drawers (state)
  5003. "Re-hide all drawers after a visibility state change."
  5004. (when (and (org-mode-p)
  5005. (not (memq state '(overview folded contents))))
  5006. (save-excursion
  5007. (let* ((globalp (memq state '(contents all)))
  5008. (beg (if globalp (point-min) (point)))
  5009. (end (if globalp (point-max)
  5010. (if (eq state 'children)
  5011. (save-excursion (outline-next-heading) (point))
  5012. (org-end-of-subtree t)))))
  5013. (goto-char beg)
  5014. (while (re-search-forward org-drawer-regexp end t)
  5015. (org-flag-drawer t))))))
  5016. (defun org-flag-drawer (flag)
  5017. (save-excursion
  5018. (beginning-of-line 1)
  5019. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5020. (let ((b (match-end 0))
  5021. (outline-regexp org-outline-regexp))
  5022. (if (re-search-forward
  5023. "^[ \t]*:END:"
  5024. (save-excursion (outline-next-heading) (point)) t)
  5025. (outline-flag-region b (point-at-eol) flag)
  5026. (error ":END: line missing at position %s" b))))))
  5027. (defun org-subtree-end-visible-p ()
  5028. "Is the end of the current subtree visible?"
  5029. (pos-visible-in-window-p
  5030. (save-excursion (org-end-of-subtree t) (point))))
  5031. (defun org-first-headline-recenter (&optional N)
  5032. "Move cursor to the first headline and recenter the headline.
  5033. Optional argument N means, put the headline into the Nth line of the window."
  5034. (goto-char (point-min))
  5035. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5036. (beginning-of-line)
  5037. (recenter (prefix-numeric-value N))))
  5038. ;;; Saving and restoring visibility
  5039. (defun org-outline-overlay-data (&optional use-markers)
  5040. "Return a list of the locations of all outline overlays.
  5041. The are overlays with the `invisible' property value `outline'.
  5042. The return valus is a list of cons cells, with start and stop
  5043. positions for each overlay.
  5044. If USE-MARKERS is set, return the positions as markers."
  5045. (let (beg end)
  5046. (save-excursion
  5047. (save-restriction
  5048. (widen)
  5049. (delq nil
  5050. (mapcar (lambda (o)
  5051. (when (eq (org-overlay-get o 'invisible) 'outline)
  5052. (setq beg (org-overlay-start o)
  5053. end (org-overlay-end o))
  5054. (and beg end (> end beg)
  5055. (if use-markers
  5056. (cons (move-marker (make-marker) beg)
  5057. (move-marker (make-marker) end))
  5058. (cons beg end)))))
  5059. (org-overlays-in (point-min) (point-max))))))))
  5060. (defun org-set-outline-overlay-data (data)
  5061. "Create visibility overlays for all positions in DATA.
  5062. DATA should have been made by `org-outline-overlay-data'."
  5063. (let (o)
  5064. (save-excursion
  5065. (save-restriction
  5066. (widen)
  5067. (show-all)
  5068. (mapc (lambda (c)
  5069. (setq o (org-make-overlay (car c) (cdr c)))
  5070. (org-overlay-put o 'invisible 'outline))
  5071. data)))))
  5072. (defmacro org-save-outline-visibility (use-markers &rest body)
  5073. "Save and restore outline visibility around BODY.
  5074. If USE-MARKERS is non-nil, use markers for the positions.
  5075. This means that the buffer may change while running BODY,
  5076. but it also means that the buffer should stay alive
  5077. during the operation, because otherwise all these markers will
  5078. point nowhere."
  5079. `(let ((data (org-outline-overlay-data ,use-markers)))
  5080. (unwind-protect
  5081. (progn
  5082. ,@body
  5083. (org-set-outline-overlay-data data))
  5084. (when ,use-markers
  5085. (mapc (lambda (c)
  5086. (and (markerp (car c)) (move-marker (car c) nil))
  5087. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5088. data)))))
  5089. ;;; Folding of blocks
  5090. (defconst org-block-regexp
  5091. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5092. "Regular expression for hiding blocks.")
  5093. (defvar org-hide-block-overlays nil
  5094. "Overlays hiding blocks.")
  5095. (make-variable-buffer-local 'org-hide-block-overlays)
  5096. (defun org-block-map (function &optional start end)
  5097. "Call func at the head of all source blocks in the current
  5098. buffer. Optional arguments START and END can be used to limit
  5099. the range."
  5100. (let ((start (or start (point-min)))
  5101. (end (or end (point-max))))
  5102. (save-excursion
  5103. (goto-char start)
  5104. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5105. (save-excursion
  5106. (save-match-data
  5107. (goto-char (match-beginning 0))
  5108. (funcall function)))))))
  5109. (defun org-hide-block-toggle-all ()
  5110. "Toggle the visibility of all blocks in the current buffer."
  5111. (org-block-map #'org-hide-block-toggle))
  5112. (defun org-hide-block-all ()
  5113. "Fold all blocks in the current buffer."
  5114. (interactive)
  5115. (org-show-block-all)
  5116. (org-block-map #'org-hide-block-toggle-maybe))
  5117. (defun org-show-block-all ()
  5118. "Unfold all blocks in the current buffer."
  5119. (mapc 'org-delete-overlay org-hide-block-overlays)
  5120. (setq org-hide-block-overlays nil))
  5121. (defun org-hide-block-toggle-maybe ()
  5122. "Toggle visibility of block at point."
  5123. (interactive)
  5124. (let ((case-fold-search t))
  5125. (if (save-excursion
  5126. (beginning-of-line 1)
  5127. (looking-at org-block-regexp))
  5128. (progn (org-hide-block-toggle)
  5129. t) ;; to signal that we took action
  5130. nil))) ;; to signal that we did not
  5131. (defun org-hide-block-toggle (&optional force)
  5132. "Toggle the visibility of the current block."
  5133. (interactive)
  5134. (save-excursion
  5135. (beginning-of-line)
  5136. (if (re-search-forward org-block-regexp nil t)
  5137. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5138. (end (match-end 0)) ;; end of entire body
  5139. ov)
  5140. (if (memq t (mapcar (lambda (overlay)
  5141. (eq (org-overlay-get overlay 'invisible)
  5142. 'org-hide-block))
  5143. (org-overlays-at start)))
  5144. (if (or (not force) (eq force 'off))
  5145. (mapc (lambda (ov)
  5146. (when (member ov org-hide-block-overlays)
  5147. (setq org-hide-block-overlays
  5148. (delq ov org-hide-block-overlays)))
  5149. (when (eq (org-overlay-get ov 'invisible)
  5150. 'org-hide-block)
  5151. (org-delete-overlay ov)))
  5152. (org-overlays-at start)))
  5153. (setq ov (org-make-overlay start end))
  5154. (org-overlay-put ov 'invisible 'org-hide-block)
  5155. ;; make the block accessible to isearch
  5156. (org-overlay-put
  5157. ov 'isearch-open-invisible
  5158. (lambda (ov)
  5159. (when (member ov org-hide-block-overlays)
  5160. (setq org-hide-block-overlays
  5161. (delq ov org-hide-block-overlays)))
  5162. (when (eq (org-overlay-get ov 'invisible)
  5163. 'org-hide-block)
  5164. (org-delete-overlay ov))))
  5165. (push ov org-hide-block-overlays)))
  5166. (error "Not looking at a source block"))))
  5167. ;; org-tab-after-check-for-cycling-hook
  5168. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5169. ;; Remove overlays when changing major mode
  5170. (add-hook 'org-mode-hook
  5171. (lambda () (org-add-hook 'change-major-mode-hook
  5172. 'org-show-block-all 'append 'local)))
  5173. ;;; Org-goto
  5174. (defvar org-goto-window-configuration nil)
  5175. (defvar org-goto-marker nil)
  5176. (defvar org-goto-map
  5177. (let ((map (make-sparse-keymap)))
  5178. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5179. (while (setq cmd (pop cmds))
  5180. (substitute-key-definition cmd cmd map global-map)))
  5181. (suppress-keymap map)
  5182. (org-defkey map "\C-m" 'org-goto-ret)
  5183. (org-defkey map [(return)] 'org-goto-ret)
  5184. (org-defkey map [(left)] 'org-goto-left)
  5185. (org-defkey map [(right)] 'org-goto-right)
  5186. (org-defkey map [(control ?g)] 'org-goto-quit)
  5187. (org-defkey map "\C-i" 'org-cycle)
  5188. (org-defkey map [(tab)] 'org-cycle)
  5189. (org-defkey map [(down)] 'outline-next-visible-heading)
  5190. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5191. (if org-goto-auto-isearch
  5192. (if (fboundp 'define-key-after)
  5193. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5194. nil)
  5195. (org-defkey map "q" 'org-goto-quit)
  5196. (org-defkey map "n" 'outline-next-visible-heading)
  5197. (org-defkey map "p" 'outline-previous-visible-heading)
  5198. (org-defkey map "f" 'outline-forward-same-level)
  5199. (org-defkey map "b" 'outline-backward-same-level)
  5200. (org-defkey map "u" 'outline-up-heading))
  5201. (org-defkey map "/" 'org-occur)
  5202. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5203. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5204. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5205. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5206. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5207. map))
  5208. (defconst org-goto-help
  5209. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5210. RET=jump to location [Q]uit and return to previous location
  5211. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5212. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5213. ;; FIXME: Docstring does not mention both interfaces
  5214. (defun org-goto (&optional alternative-interface)
  5215. "Look up a different location in the current file, keeping current visibility.
  5216. When you want look-up or go to a different location in a document, the
  5217. fastest way is often to fold the entire buffer and then dive into the tree.
  5218. This method has the disadvantage, that the previous location will be folded,
  5219. which may not be what you want.
  5220. This command works around this by showing a copy of the current buffer
  5221. in an indirect buffer, in overview mode. You can dive into the tree in
  5222. that copy, use org-occur and incremental search to find a location.
  5223. When pressing RET or `Q', the command returns to the original buffer in
  5224. which the visibility is still unchanged. After RET is will also jump to
  5225. the location selected in the indirect buffer and expose the
  5226. the headline hierarchy above."
  5227. (interactive "P")
  5228. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5229. (org-refile-use-outline-path t)
  5230. (org-refile-target-verify-function nil)
  5231. (interface
  5232. (if (not alternative-interface)
  5233. org-goto-interface
  5234. (if (eq org-goto-interface 'outline)
  5235. 'outline-path-completion
  5236. 'outline)))
  5237. (org-goto-start-pos (point))
  5238. (selected-point
  5239. (if (eq interface 'outline)
  5240. (car (org-get-location (current-buffer) org-goto-help))
  5241. (nth 3 (org-refile-get-location "Goto: ")))))
  5242. (if selected-point
  5243. (progn
  5244. (org-mark-ring-push org-goto-start-pos)
  5245. (goto-char selected-point)
  5246. (if (or (org-invisible-p) (org-invisible-p2))
  5247. (org-show-context 'org-goto)))
  5248. (message "Quit"))))
  5249. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5250. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5251. (defvar org-goto-local-auto-isearch-map) ; defined below
  5252. (defun org-get-location (buf help)
  5253. "Let the user select a location in the Org-mode buffer BUF.
  5254. This function uses a recursive edit. It returns the selected position
  5255. or nil."
  5256. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5257. (isearch-hide-immediately nil)
  5258. (isearch-search-fun-function
  5259. (lambda () 'org-goto-local-search-headings))
  5260. (org-goto-selected-point org-goto-exit-command))
  5261. (save-excursion
  5262. (save-window-excursion
  5263. (delete-other-windows)
  5264. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5265. (switch-to-buffer
  5266. (condition-case nil
  5267. (make-indirect-buffer (current-buffer) "*org-goto*")
  5268. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5269. (with-output-to-temp-buffer "*Help*"
  5270. (princ help))
  5271. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5272. (setq buffer-read-only nil)
  5273. (let ((org-startup-truncated t)
  5274. (org-startup-folded nil)
  5275. (org-startup-align-all-tables nil))
  5276. (org-mode)
  5277. (org-overview))
  5278. (setq buffer-read-only t)
  5279. (if (and (boundp 'org-goto-start-pos)
  5280. (integer-or-marker-p org-goto-start-pos))
  5281. (let ((org-show-hierarchy-above t)
  5282. (org-show-siblings t)
  5283. (org-show-following-heading t))
  5284. (goto-char org-goto-start-pos)
  5285. (and (org-invisible-p) (org-show-context)))
  5286. (goto-char (point-min)))
  5287. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5288. (message "Select location and press RET")
  5289. (use-local-map org-goto-map)
  5290. (recursive-edit)
  5291. ))
  5292. (kill-buffer "*org-goto*")
  5293. (cons org-goto-selected-point org-goto-exit-command)))
  5294. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5295. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5296. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5297. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5298. (defun org-goto-local-search-headings (string bound noerror)
  5299. "Search and make sure that any matches are in headlines."
  5300. (catch 'return
  5301. (while (if isearch-forward
  5302. (search-forward string bound noerror)
  5303. (search-backward string bound noerror))
  5304. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5305. (and (member :headline context)
  5306. (not (member :tags context))))
  5307. (throw 'return (point))))))
  5308. (defun org-goto-local-auto-isearch ()
  5309. "Start isearch."
  5310. (interactive)
  5311. (goto-char (point-min))
  5312. (let ((keys (this-command-keys)))
  5313. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5314. (isearch-mode t)
  5315. (isearch-process-search-char (string-to-char keys)))))
  5316. (defun org-goto-ret (&optional arg)
  5317. "Finish `org-goto' by going to the new location."
  5318. (interactive "P")
  5319. (setq org-goto-selected-point (point)
  5320. org-goto-exit-command 'return)
  5321. (throw 'exit nil))
  5322. (defun org-goto-left ()
  5323. "Finish `org-goto' by going to the new location."
  5324. (interactive)
  5325. (if (org-on-heading-p)
  5326. (progn
  5327. (beginning-of-line 1)
  5328. (setq org-goto-selected-point (point)
  5329. org-goto-exit-command 'left)
  5330. (throw 'exit nil))
  5331. (error "Not on a heading")))
  5332. (defun org-goto-right ()
  5333. "Finish `org-goto' by going to the new location."
  5334. (interactive)
  5335. (if (org-on-heading-p)
  5336. (progn
  5337. (setq org-goto-selected-point (point)
  5338. org-goto-exit-command 'right)
  5339. (throw 'exit nil))
  5340. (error "Not on a heading")))
  5341. (defun org-goto-quit ()
  5342. "Finish `org-goto' without cursor motion."
  5343. (interactive)
  5344. (setq org-goto-selected-point nil)
  5345. (setq org-goto-exit-command 'quit)
  5346. (throw 'exit nil))
  5347. ;;; Indirect buffer display of subtrees
  5348. (defvar org-indirect-dedicated-frame nil
  5349. "This is the frame being used for indirect tree display.")
  5350. (defvar org-last-indirect-buffer nil)
  5351. (defun org-tree-to-indirect-buffer (&optional arg)
  5352. "Create indirect buffer and narrow it to current subtree.
  5353. With numerical prefix ARG, go up to this level and then take that tree.
  5354. If ARG is negative, go up that many levels.
  5355. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5356. indirect buffer previously made with this command, to avoid proliferation of
  5357. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5358. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5359. is kept so that you can work with several indirect buffers at the same time.
  5360. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5361. requests that a new frame be made for the new buffer, so that the dedicated
  5362. frame is not changed."
  5363. (interactive "P")
  5364. (let ((cbuf (current-buffer))
  5365. (cwin (selected-window))
  5366. (pos (point))
  5367. beg end level heading ibuf)
  5368. (save-excursion
  5369. (org-back-to-heading t)
  5370. (when (numberp arg)
  5371. (setq level (org-outline-level))
  5372. (if (< arg 0) (setq arg (+ level arg)))
  5373. (while (> (setq level (org-outline-level)) arg)
  5374. (outline-up-heading 1 t)))
  5375. (setq beg (point)
  5376. heading (org-get-heading))
  5377. (org-end-of-subtree t t) (setq end (point)))
  5378. (if (and (buffer-live-p org-last-indirect-buffer)
  5379. (not (eq org-indirect-buffer-display 'new-frame))
  5380. (not arg))
  5381. (kill-buffer org-last-indirect-buffer))
  5382. (setq ibuf (org-get-indirect-buffer cbuf)
  5383. org-last-indirect-buffer ibuf)
  5384. (cond
  5385. ((or (eq org-indirect-buffer-display 'new-frame)
  5386. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5387. (select-frame (make-frame))
  5388. (delete-other-windows)
  5389. (switch-to-buffer ibuf)
  5390. (org-set-frame-title heading))
  5391. ((eq org-indirect-buffer-display 'dedicated-frame)
  5392. (raise-frame
  5393. (select-frame (or (and org-indirect-dedicated-frame
  5394. (frame-live-p org-indirect-dedicated-frame)
  5395. org-indirect-dedicated-frame)
  5396. (setq org-indirect-dedicated-frame (make-frame)))))
  5397. (delete-other-windows)
  5398. (switch-to-buffer ibuf)
  5399. (org-set-frame-title (concat "Indirect: " heading)))
  5400. ((eq org-indirect-buffer-display 'current-window)
  5401. (switch-to-buffer ibuf))
  5402. ((eq org-indirect-buffer-display 'other-window)
  5403. (pop-to-buffer ibuf))
  5404. (t (error "Invalid value")))
  5405. (if (featurep 'xemacs)
  5406. (save-excursion (org-mode) (turn-on-font-lock)))
  5407. (narrow-to-region beg end)
  5408. (show-all)
  5409. (goto-char pos)
  5410. (and (window-live-p cwin) (select-window cwin))))
  5411. (defun org-get-indirect-buffer (&optional buffer)
  5412. (setq buffer (or buffer (current-buffer)))
  5413. (let ((n 1) (base (buffer-name buffer)) bname)
  5414. (while (buffer-live-p
  5415. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5416. (setq n (1+ n)))
  5417. (condition-case nil
  5418. (make-indirect-buffer buffer bname 'clone)
  5419. (error (make-indirect-buffer buffer bname)))))
  5420. (defun org-set-frame-title (title)
  5421. "Set the title of the current frame to the string TITLE."
  5422. ;; FIXME: how to name a single frame in XEmacs???
  5423. (unless (featurep 'xemacs)
  5424. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5425. ;;;; Structure editing
  5426. ;;; Inserting headlines
  5427. (defun org-previous-line-empty-p ()
  5428. (save-excursion
  5429. (and (not (bobp))
  5430. (or (beginning-of-line 0) t)
  5431. (save-match-data
  5432. (looking-at "[ \t]*$")))))
  5433. (defun org-insert-heading (&optional force-heading)
  5434. "Insert a new heading or item with same depth at point.
  5435. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5436. If point is at the beginning of a headline, insert a sibling before the
  5437. current headline. If point is not at the beginning, do not split the line,
  5438. but create the new headline after the current line."
  5439. (interactive "P")
  5440. (if (or (= (buffer-size) 0)
  5441. (and (not (save-excursion (and (ignore-errors (org-back-to-heading))
  5442. (org-on-heading-p))))
  5443. (not (org-in-item-p))))
  5444. (insert "\n* ")
  5445. (when (or force-heading (not (org-insert-item)))
  5446. (let* ((empty-line-p nil)
  5447. (head (save-excursion
  5448. (condition-case nil
  5449. (progn
  5450. (org-back-to-heading)
  5451. (setq empty-line-p (org-previous-line-empty-p))
  5452. (match-string 0))
  5453. (error "*"))))
  5454. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5455. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5456. pos hide-previous previous-pos)
  5457. (cond
  5458. ((and (org-on-heading-p) (bolp)
  5459. (or (bobp)
  5460. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5461. ;; insert before the current line
  5462. (open-line (if blank 2 1)))
  5463. ((and (bolp)
  5464. (not org-insert-heading-respect-content)
  5465. (or (bobp)
  5466. (save-excursion
  5467. (backward-char 1) (not (org-invisible-p)))))
  5468. ;; insert right here
  5469. nil)
  5470. (t
  5471. ;; somewhere in the line
  5472. (save-excursion
  5473. (setq previous-pos (point-at-bol))
  5474. (end-of-line)
  5475. (setq hide-previous (org-invisible-p)))
  5476. (and org-insert-heading-respect-content (org-show-subtree))
  5477. (let ((split
  5478. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5479. (save-excursion
  5480. (let ((p (point)))
  5481. (goto-char (point-at-bol))
  5482. (and (looking-at org-complex-heading-regexp)
  5483. (> p (match-beginning 4)))))))
  5484. tags pos)
  5485. (cond
  5486. (org-insert-heading-respect-content
  5487. (org-end-of-subtree nil t)
  5488. (or (bolp) (newline))
  5489. (or (org-previous-line-empty-p)
  5490. (and blank (newline)))
  5491. (open-line 1))
  5492. ((org-on-heading-p)
  5493. (when hide-previous
  5494. (show-children)
  5495. (org-show-entry))
  5496. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5497. (setq tags (and (match-end 2) (match-string 2)))
  5498. (and (match-end 1)
  5499. (delete-region (match-beginning 1) (match-end 1)))
  5500. (setq pos (point-at-bol))
  5501. (or split (end-of-line 1))
  5502. (delete-horizontal-space)
  5503. (newline (if blank 2 1))
  5504. (when tags
  5505. (save-excursion
  5506. (goto-char pos)
  5507. (end-of-line 1)
  5508. (insert " " tags)
  5509. (org-set-tags nil 'align))))
  5510. (t
  5511. (or split (end-of-line 1))
  5512. (newline (if blank 2 1)))))))
  5513. (insert head) (just-one-space)
  5514. (setq pos (point))
  5515. (end-of-line 1)
  5516. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5517. (when (and org-insert-heading-respect-content hide-previous)
  5518. (save-excursion
  5519. (goto-char previous-pos)
  5520. (hide-subtree)))
  5521. (run-hooks 'org-insert-heading-hook)))))
  5522. (defun org-get-heading (&optional no-tags)
  5523. "Return the heading of the current entry, without the stars."
  5524. (save-excursion
  5525. (org-back-to-heading t)
  5526. (if (looking-at
  5527. (if no-tags
  5528. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5529. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5530. (match-string 1) "")))
  5531. (defun org-heading-components ()
  5532. "Return the components of the current heading.
  5533. This is a list with the following elements:
  5534. - the level as an integer
  5535. - the reduced level, different if `org-odd-levels-only' is set.
  5536. - the TODO keyword, or nil
  5537. - the priority character, like ?A, or nil if no priority is given
  5538. - the headline text itself, or the tags string if no headline text
  5539. - the tags string, or nil."
  5540. (save-excursion
  5541. (org-back-to-heading t)
  5542. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5543. (list (length (match-string 1))
  5544. (org-reduced-level (length (match-string 1)))
  5545. (org-match-string-no-properties 2)
  5546. (and (match-end 3) (aref (match-string 3) 2))
  5547. (org-match-string-no-properties 4)
  5548. (org-match-string-no-properties 5)))))
  5549. (defun org-get-entry ()
  5550. "Get the entry text, after heading, entire subtree."
  5551. (save-excursion
  5552. (org-back-to-heading t)
  5553. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5554. (defun org-insert-heading-after-current ()
  5555. "Insert a new heading with same level as current, after current subtree."
  5556. (interactive)
  5557. (org-back-to-heading)
  5558. (org-insert-heading)
  5559. (org-move-subtree-down)
  5560. (end-of-line 1))
  5561. (defun org-insert-heading-respect-content ()
  5562. (interactive)
  5563. (let ((org-insert-heading-respect-content t))
  5564. (org-insert-heading t)))
  5565. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5566. (interactive "P")
  5567. (let ((org-insert-heading-respect-content t))
  5568. (org-insert-todo-heading force-state t)))
  5569. (defun org-insert-todo-heading (arg &optional force-heading)
  5570. "Insert a new heading with the same level and TODO state as current heading.
  5571. If the heading has no TODO state, or if the state is DONE, use the first
  5572. state (TODO by default). Also with prefix arg, force first state."
  5573. (interactive "P")
  5574. (when (or force-heading (not (org-insert-item 'checkbox)))
  5575. (org-insert-heading force-heading)
  5576. (save-excursion
  5577. (org-back-to-heading)
  5578. (outline-previous-heading)
  5579. (looking-at org-todo-line-regexp))
  5580. (let*
  5581. ((new-mark-x
  5582. (if (or arg
  5583. (not (match-beginning 2))
  5584. (member (match-string 2) org-done-keywords))
  5585. (car org-todo-keywords-1)
  5586. (match-string 2)))
  5587. (new-mark
  5588. (or
  5589. (run-hook-with-args-until-success
  5590. 'org-todo-get-default-hook new-mark-x nil)
  5591. new-mark-x)))
  5592. (beginning-of-line 1)
  5593. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5594. (if org-treat-insert-todo-heading-as-state-change
  5595. (org-todo new-mark)
  5596. (insert new-mark " "))))
  5597. (when org-provide-todo-statistics
  5598. (org-update-parent-todo-statistics))))
  5599. (defun org-insert-subheading (arg)
  5600. "Insert a new subheading and demote it.
  5601. Works for outline headings and for plain lists alike."
  5602. (interactive "P")
  5603. (org-insert-heading arg)
  5604. (cond
  5605. ((org-on-heading-p) (org-do-demote))
  5606. ((org-at-item-p) (org-indent-item 1))))
  5607. (defun org-insert-todo-subheading (arg)
  5608. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5609. Works for outline headings and for plain lists alike."
  5610. (interactive "P")
  5611. (org-insert-todo-heading arg)
  5612. (cond
  5613. ((org-on-heading-p) (org-do-demote))
  5614. ((org-at-item-p) (org-indent-item 1))))
  5615. ;;; Promotion and Demotion
  5616. (defvar org-after-demote-entry-hook nil
  5617. "Hook run after an entry has been demoted.
  5618. The cursor will be at the beginning of the entry.
  5619. When a subtree is being demoted, the hook will be called for each node.")
  5620. (defvar org-after-promote-entry-hook nil
  5621. "Hook run after an entry has been promoted.
  5622. The cursor will be at the beginning of the entry.
  5623. When a subtree is being promoted, the hook will be called for each node.")
  5624. (defun org-promote-subtree ()
  5625. "Promote the entire subtree.
  5626. See also `org-promote'."
  5627. (interactive)
  5628. (save-excursion
  5629. (org-map-tree 'org-promote))
  5630. (org-fix-position-after-promote))
  5631. (defun org-demote-subtree ()
  5632. "Demote the entire subtree. See `org-demote'.
  5633. See also `org-promote'."
  5634. (interactive)
  5635. (save-excursion
  5636. (org-map-tree 'org-demote))
  5637. (org-fix-position-after-promote))
  5638. (defun org-do-promote ()
  5639. "Promote the current heading higher up the tree.
  5640. If the region is active in `transient-mark-mode', promote all headings
  5641. in the region."
  5642. (interactive)
  5643. (save-excursion
  5644. (if (org-region-active-p)
  5645. (org-map-region 'org-promote (region-beginning) (region-end))
  5646. (org-promote)))
  5647. (org-fix-position-after-promote))
  5648. (defun org-do-demote ()
  5649. "Demote the current heading lower down the tree.
  5650. If the region is active in `transient-mark-mode', demote all headings
  5651. in the region."
  5652. (interactive)
  5653. (save-excursion
  5654. (if (org-region-active-p)
  5655. (org-map-region 'org-demote (region-beginning) (region-end))
  5656. (org-demote)))
  5657. (org-fix-position-after-promote))
  5658. (defun org-fix-position-after-promote ()
  5659. "Make sure that after pro/demotion cursor position is right."
  5660. (let ((pos (point)))
  5661. (when (save-excursion
  5662. (beginning-of-line 1)
  5663. (looking-at org-todo-line-regexp)
  5664. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5665. (cond ((eobp) (insert " "))
  5666. ((eolp) (insert " "))
  5667. ((equal (char-after) ?\ ) (forward-char 1))))))
  5668. (defun org-current-level ()
  5669. "Return the level of the current entry, or nil if before the first headline.
  5670. The level is the number of stars at the beginning of the headline."
  5671. (save-excursion
  5672. (condition-case nil
  5673. (progn
  5674. (org-back-to-heading t)
  5675. (funcall outline-level))
  5676. (error nil))))
  5677. (defun org-reduced-level (l)
  5678. "Compute the effective level of a heading.
  5679. This takes into account the setting of `org-odd-levels-only'."
  5680. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5681. (defun org-get-valid-level (level &optional change)
  5682. "Rectify a level change under the influence of `org-odd-levels-only'
  5683. LEVEL is a current level, CHANGE is by how much the level should be
  5684. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5685. even level numbers will become the next higher odd number."
  5686. (if org-odd-levels-only
  5687. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5688. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5689. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5690. (max 1 (+ level (or change 0)))))
  5691. (if (boundp 'define-obsolete-function-alias)
  5692. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5693. (define-obsolete-function-alias 'org-get-legal-level
  5694. 'org-get-valid-level)
  5695. (define-obsolete-function-alias 'org-get-legal-level
  5696. 'org-get-valid-level "23.1")))
  5697. (defun org-promote ()
  5698. "Promote the current heading higher up the tree.
  5699. If the region is active in `transient-mark-mode', promote all headings
  5700. in the region."
  5701. (org-back-to-heading t)
  5702. (let* ((level (save-match-data (funcall outline-level)))
  5703. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5704. (diff (abs (- level (length up-head) -1))))
  5705. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5706. (replace-match up-head nil t)
  5707. ;; Fixup tag positioning
  5708. (and org-auto-align-tags (org-set-tags nil t))
  5709. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5710. (run-hooks 'org-after-promote-entry-hook)))
  5711. (defun org-demote ()
  5712. "Demote the current heading lower down the tree.
  5713. If the region is active in `transient-mark-mode', demote all headings
  5714. in the region."
  5715. (org-back-to-heading t)
  5716. (let* ((level (save-match-data (funcall outline-level)))
  5717. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5718. (diff (abs (- level (length down-head) -1))))
  5719. (replace-match down-head nil t)
  5720. ;; Fixup tag positioning
  5721. (and org-auto-align-tags (org-set-tags nil t))
  5722. (if org-adapt-indentation (org-fixup-indentation diff))
  5723. (run-hooks 'org-after-demote-entry-hook)))
  5724. (defvar org-tab-ind-state nil)
  5725. (defun org-cycle-level ()
  5726. (let ((org-adapt-indentation nil))
  5727. (when (and (looking-at "[ \t]*$")
  5728. (org-looking-back
  5729. (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp "\\)?[ \t]*")))
  5730. (setq this-command 'org-cycle-level)
  5731. (if (eq last-command 'org-cycle-level)
  5732. (condition-case nil
  5733. (progn (org-do-promote)
  5734. (if (equal org-tab-ind-state (org-current-level))
  5735. (org-do-promote)))
  5736. (error
  5737. (progn
  5738. (save-excursion
  5739. (beginning-of-line 1)
  5740. (and (looking-at "\\*+")
  5741. (replace-match
  5742. (make-string org-tab-ind-state ?*))))
  5743. (setq this-command 'org-cycle))))
  5744. (setq org-tab-ind-state (- (match-end 1) (match-beginning 1)))
  5745. (org-do-demote))
  5746. t)))
  5747. (defun org-map-tree (fun)
  5748. "Call FUN for every heading underneath the current one."
  5749. (org-back-to-heading)
  5750. (let ((level (funcall outline-level)))
  5751. (save-excursion
  5752. (funcall fun)
  5753. (while (and (progn
  5754. (outline-next-heading)
  5755. (> (funcall outline-level) level))
  5756. (not (eobp)))
  5757. (funcall fun)))))
  5758. (defun org-map-region (fun beg end)
  5759. "Call FUN for every heading between BEG and END."
  5760. (let ((org-ignore-region t))
  5761. (save-excursion
  5762. (setq end (copy-marker end))
  5763. (goto-char beg)
  5764. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5765. (< (point) end))
  5766. (funcall fun))
  5767. (while (and (progn
  5768. (outline-next-heading)
  5769. (< (point) end))
  5770. (not (eobp)))
  5771. (funcall fun)))))
  5772. (defun org-fixup-indentation (diff)
  5773. "Change the indentation in the current entry by DIFF
  5774. However, if any line in the current entry has no indentation, or if it
  5775. would end up with no indentation after the change, nothing at all is done."
  5776. (save-excursion
  5777. (let ((end (save-excursion (outline-next-heading)
  5778. (point-marker)))
  5779. (prohibit (if (> diff 0)
  5780. "^\\S-"
  5781. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5782. col)
  5783. (unless (save-excursion (end-of-line 1)
  5784. (re-search-forward prohibit end t))
  5785. (while (and (< (point) end)
  5786. (re-search-forward "^[ \t]+" end t))
  5787. (goto-char (match-end 0))
  5788. (setq col (current-column))
  5789. (if (< diff 0) (replace-match ""))
  5790. (org-indent-to-column (+ diff col))))
  5791. (move-marker end nil))))
  5792. (defun org-convert-to-odd-levels ()
  5793. "Convert an org-mode file with all levels allowed to one with odd levels.
  5794. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5795. level 5 etc."
  5796. (interactive)
  5797. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5798. (let ((outline-regexp org-outline-regexp)
  5799. (outline-level 'org-outline-level)
  5800. (org-odd-levels-only nil) n)
  5801. (save-excursion
  5802. (goto-char (point-min))
  5803. (while (re-search-forward "^\\*\\*+ " nil t)
  5804. (setq n (- (length (match-string 0)) 2))
  5805. (while (>= (setq n (1- n)) 0)
  5806. (org-demote))
  5807. (end-of-line 1))))))
  5808. (defun org-convert-to-oddeven-levels ()
  5809. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5810. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5811. section with an even level, conversion would destroy the structure of the file. An error
  5812. is signaled in this case."
  5813. (interactive)
  5814. (goto-char (point-min))
  5815. ;; First check if there are no even levels
  5816. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5817. (org-show-context t)
  5818. (error "Not all levels are odd in this file. Conversion not possible"))
  5819. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5820. (let ((outline-regexp org-outline-regexp)
  5821. (outline-level 'org-outline-level)
  5822. (org-odd-levels-only nil) n)
  5823. (save-excursion
  5824. (goto-char (point-min))
  5825. (while (re-search-forward "^\\*\\*+ " nil t)
  5826. (setq n (/ (1- (length (match-string 0))) 2))
  5827. (while (>= (setq n (1- n)) 0)
  5828. (org-promote))
  5829. (end-of-line 1))))))
  5830. (defun org-tr-level (n)
  5831. "Make N odd if required."
  5832. (if org-odd-levels-only (1+ (/ n 2)) n))
  5833. ;;; Vertical tree motion, cutting and pasting of subtrees
  5834. (defun org-move-subtree-up (&optional arg)
  5835. "Move the current subtree up past ARG headlines of the same level."
  5836. (interactive "p")
  5837. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5838. (defun org-move-subtree-down (&optional arg)
  5839. "Move the current subtree down past ARG headlines of the same level."
  5840. (interactive "p")
  5841. (setq arg (prefix-numeric-value arg))
  5842. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5843. 'org-get-last-sibling))
  5844. (ins-point (make-marker))
  5845. (cnt (abs arg))
  5846. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5847. ;; Select the tree
  5848. (org-back-to-heading)
  5849. (setq beg0 (point))
  5850. (save-excursion
  5851. (setq ne-beg (org-back-over-empty-lines))
  5852. (setq beg (point)))
  5853. (save-match-data
  5854. (save-excursion (outline-end-of-heading)
  5855. (setq folded (org-invisible-p)))
  5856. (outline-end-of-subtree))
  5857. (outline-next-heading)
  5858. (setq ne-end (org-back-over-empty-lines))
  5859. (setq end (point))
  5860. (goto-char beg0)
  5861. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5862. ;; include less whitespace
  5863. (save-excursion
  5864. (goto-char beg)
  5865. (forward-line (- ne-beg ne-end))
  5866. (setq beg (point))))
  5867. ;; Find insertion point, with error handling
  5868. (while (> cnt 0)
  5869. (or (and (funcall movfunc) (looking-at outline-regexp))
  5870. (progn (goto-char beg0)
  5871. (error "Cannot move past superior level or buffer limit")))
  5872. (setq cnt (1- cnt)))
  5873. (if (> arg 0)
  5874. ;; Moving forward - still need to move over subtree
  5875. (progn (org-end-of-subtree t t)
  5876. (save-excursion
  5877. (org-back-over-empty-lines)
  5878. (or (bolp) (newline)))))
  5879. (setq ne-ins (org-back-over-empty-lines))
  5880. (move-marker ins-point (point))
  5881. (setq txt (buffer-substring beg end))
  5882. (org-save-markers-in-region beg end)
  5883. (delete-region beg end)
  5884. (org-remove-empty-overlays-at beg)
  5885. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5886. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5887. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5888. (let ((bbb (point)))
  5889. (insert-before-markers txt)
  5890. (org-reinstall-markers-in-region bbb)
  5891. (move-marker ins-point bbb))
  5892. (or (bolp) (insert "\n"))
  5893. (setq ins-end (point))
  5894. (goto-char ins-point)
  5895. (org-skip-whitespace)
  5896. (when (and (< arg 0)
  5897. (org-first-sibling-p)
  5898. (> ne-ins ne-beg))
  5899. ;; Move whitespace back to beginning
  5900. (save-excursion
  5901. (goto-char ins-end)
  5902. (let ((kill-whole-line t))
  5903. (kill-line (- ne-ins ne-beg)) (point)))
  5904. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5905. (move-marker ins-point nil)
  5906. (if folded
  5907. (hide-subtree)
  5908. (org-show-entry)
  5909. (show-children)
  5910. (org-cycle-hide-drawers 'children))
  5911. (org-clean-visibility-after-subtree-move)))
  5912. (defvar org-subtree-clip ""
  5913. "Clipboard for cut and paste of subtrees.
  5914. This is actually only a copy of the kill, because we use the normal kill
  5915. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5916. (defvar org-subtree-clip-folded nil
  5917. "Was the last copied subtree folded?
  5918. This is used to fold the tree back after pasting.")
  5919. (defun org-cut-subtree (&optional n)
  5920. "Cut the current subtree into the clipboard.
  5921. With prefix arg N, cut this many sequential subtrees.
  5922. This is a short-hand for marking the subtree and then cutting it."
  5923. (interactive "p")
  5924. (org-copy-subtree n 'cut))
  5925. (defun org-copy-subtree (&optional n cut force-store-markers)
  5926. "Cut the current subtree into the clipboard.
  5927. With prefix arg N, cut this many sequential subtrees.
  5928. This is a short-hand for marking the subtree and then copying it.
  5929. If CUT is non-nil, actually cut the subtree.
  5930. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5931. of some markers in the region, even if CUT is non-nil. This is
  5932. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5933. (interactive "p")
  5934. (let (beg end folded (beg0 (point)))
  5935. (if (interactive-p)
  5936. (org-back-to-heading nil) ; take what looks like a subtree
  5937. (org-back-to-heading t)) ; take what is really there
  5938. (org-back-over-empty-lines)
  5939. (setq beg (point))
  5940. (skip-chars-forward " \t\r\n")
  5941. (save-match-data
  5942. (save-excursion (outline-end-of-heading)
  5943. (setq folded (org-invisible-p)))
  5944. (condition-case nil
  5945. (org-forward-same-level (1- n) t)
  5946. (error nil))
  5947. (org-end-of-subtree t t))
  5948. (org-back-over-empty-lines)
  5949. (setq end (point))
  5950. (goto-char beg0)
  5951. (when (> end beg)
  5952. (setq org-subtree-clip-folded folded)
  5953. (when (or cut force-store-markers)
  5954. (org-save-markers-in-region beg end))
  5955. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5956. (setq org-subtree-clip (current-kill 0))
  5957. (message "%s: Subtree(s) with %d characters"
  5958. (if cut "Cut" "Copied")
  5959. (length org-subtree-clip)))))
  5960. (defun org-paste-subtree (&optional level tree for-yank)
  5961. "Paste the clipboard as a subtree, with modification of headline level.
  5962. The entire subtree is promoted or demoted in order to match a new headline
  5963. level.
  5964. If the cursor is at the beginning of a headline, the same level as
  5965. that headline is used to paste the tree
  5966. If not, the new level is derived from the *visible* headings
  5967. before and after the insertion point, and taken to be the inferior headline
  5968. level of the two. So if the previous visible heading is level 3 and the
  5969. next is level 4 (or vice versa), level 4 will be used for insertion.
  5970. This makes sure that the subtree remains an independent subtree and does
  5971. not swallow low level entries.
  5972. You can also force a different level, either by using a numeric prefix
  5973. argument, or by inserting the heading marker by hand. For example, if the
  5974. cursor is after \"*****\", then the tree will be shifted to level 5.
  5975. If optional TREE is given, use this text instead of the kill ring.
  5976. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5977. move back over whitespace before inserting, and move point to the end of
  5978. the inserted text when done."
  5979. (interactive "P")
  5980. (setq tree (or tree (and kill-ring (current-kill 0))))
  5981. (unless (org-kill-is-subtree-p tree)
  5982. (error "%s"
  5983. (substitute-command-keys
  5984. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5985. (let* ((visp (not (org-invisible-p)))
  5986. (txt tree)
  5987. (^re (concat "^\\(" outline-regexp "\\)"))
  5988. (re (concat "\\(" outline-regexp "\\)"))
  5989. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5990. (old-level (if (string-match ^re txt)
  5991. (- (match-end 0) (match-beginning 0) 1)
  5992. -1))
  5993. (force-level (cond (level (prefix-numeric-value level))
  5994. ((and (looking-at "[ \t]*$")
  5995. (string-match
  5996. ^re_ (buffer-substring
  5997. (point-at-bol) (point))))
  5998. (- (match-end 1) (match-beginning 1)))
  5999. ((and (bolp)
  6000. (looking-at org-outline-regexp))
  6001. (- (match-end 0) (point) 1))
  6002. (t nil)))
  6003. (previous-level (save-excursion
  6004. (condition-case nil
  6005. (progn
  6006. (outline-previous-visible-heading 1)
  6007. (if (looking-at re)
  6008. (- (match-end 0) (match-beginning 0) 1)
  6009. 1))
  6010. (error 1))))
  6011. (next-level (save-excursion
  6012. (condition-case nil
  6013. (progn
  6014. (or (looking-at outline-regexp)
  6015. (outline-next-visible-heading 1))
  6016. (if (looking-at re)
  6017. (- (match-end 0) (match-beginning 0) 1)
  6018. 1))
  6019. (error 1))))
  6020. (new-level (or force-level (max previous-level next-level)))
  6021. (shift (if (or (= old-level -1)
  6022. (= new-level -1)
  6023. (= old-level new-level))
  6024. 0
  6025. (- new-level old-level)))
  6026. (delta (if (> shift 0) -1 1))
  6027. (func (if (> shift 0) 'org-demote 'org-promote))
  6028. (org-odd-levels-only nil)
  6029. beg end newend)
  6030. ;; Remove the forced level indicator
  6031. (if force-level
  6032. (delete-region (point-at-bol) (point)))
  6033. ;; Paste
  6034. (beginning-of-line 1)
  6035. (unless for-yank (org-back-over-empty-lines))
  6036. (setq beg (point))
  6037. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6038. (insert-before-markers txt)
  6039. (unless (string-match "\n\\'" txt) (insert "\n"))
  6040. (setq newend (point))
  6041. (org-reinstall-markers-in-region beg)
  6042. (setq end (point))
  6043. (goto-char beg)
  6044. (skip-chars-forward " \t\n\r")
  6045. (setq beg (point))
  6046. (if (and (org-invisible-p) visp)
  6047. (save-excursion (outline-show-heading)))
  6048. ;; Shift if necessary
  6049. (unless (= shift 0)
  6050. (save-restriction
  6051. (narrow-to-region beg end)
  6052. (while (not (= shift 0))
  6053. (org-map-region func (point-min) (point-max))
  6054. (setq shift (+ delta shift)))
  6055. (goto-char (point-min))
  6056. (setq newend (point-max))))
  6057. (when (or (interactive-p) for-yank)
  6058. (message "Clipboard pasted as level %d subtree" new-level))
  6059. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6060. kill-ring
  6061. (eq org-subtree-clip (current-kill 0))
  6062. org-subtree-clip-folded)
  6063. ;; The tree was folded before it was killed/copied
  6064. (hide-subtree))
  6065. (and for-yank (goto-char newend))))
  6066. (defun org-kill-is-subtree-p (&optional txt)
  6067. "Check if the current kill is an outline subtree, or a set of trees.
  6068. Returns nil if kill does not start with a headline, or if the first
  6069. headline level is not the largest headline level in the tree.
  6070. So this will actually accept several entries of equal levels as well,
  6071. which is OK for `org-paste-subtree'.
  6072. If optional TXT is given, check this string instead of the current kill."
  6073. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6074. (start-level (and kill
  6075. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6076. org-outline-regexp "\\)")
  6077. kill)
  6078. (- (match-end 2) (match-beginning 2) 1)))
  6079. (re (concat "^" org-outline-regexp))
  6080. (start (1+ (or (match-beginning 2) -1))))
  6081. (if (not start-level)
  6082. (progn
  6083. nil) ;; does not even start with a heading
  6084. (catch 'exit
  6085. (while (setq start (string-match re kill (1+ start)))
  6086. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6087. (throw 'exit nil)))
  6088. t))))
  6089. (defvar org-markers-to-move nil
  6090. "Markers that should be moved with a cut-and-paste operation.
  6091. Those markers are stored together with their positions relative to
  6092. the start of the region.")
  6093. (defun org-save-markers-in-region (beg end)
  6094. "Check markers in region.
  6095. If these markers are between BEG and END, record their position relative
  6096. to BEG, so that after moving the block of text, we can put the markers back
  6097. into place.
  6098. This function gets called just before an entry or tree gets cut from the
  6099. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6100. called immediately, to move the markers with the entries."
  6101. (setq org-markers-to-move nil)
  6102. (when (featurep 'org-clock)
  6103. (org-clock-save-markers-for-cut-and-paste beg end))
  6104. (when (featurep 'org-agenda)
  6105. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6106. (defun org-check-and-save-marker (marker beg end)
  6107. "Check if MARKER is between BEG and END.
  6108. If yes, remember the marker and the distance to BEG."
  6109. (when (and (marker-buffer marker)
  6110. (equal (marker-buffer marker) (current-buffer)))
  6111. (if (and (>= marker beg) (< marker end))
  6112. (push (cons marker (- marker beg)) org-markers-to-move))))
  6113. (defun org-reinstall-markers-in-region (beg)
  6114. "Move all remembered markers to their position relative to BEG."
  6115. (mapc (lambda (x)
  6116. (move-marker (car x) (+ beg (cdr x))))
  6117. org-markers-to-move)
  6118. (setq org-markers-to-move nil))
  6119. (defun org-narrow-to-subtree ()
  6120. "Narrow buffer to the current subtree."
  6121. (interactive)
  6122. (save-excursion
  6123. (save-match-data
  6124. (narrow-to-region
  6125. (progn (org-back-to-heading t) (point))
  6126. (progn (org-end-of-subtree t t) (point))))))
  6127. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6128. "Clone the task (subtree) at point N times.
  6129. The clones will be inserted as siblings.
  6130. In interactive use, the user will be prompted for the number of clones
  6131. to be produced, and for a time SHIFT, which may be a repeater as used
  6132. in time stamps, for example `+3d'.
  6133. When a valid repeater is given and the entry contains any time stamps,
  6134. the clones will become a sequence in time, with time stamps in the
  6135. subtree shifted for each clone produced. If SHIFT is nil or the
  6136. empty string, time stamps will be left alone.
  6137. If the original subtree did contain time stamps with a repeater,
  6138. the following will happen:
  6139. - the repeater will be removed in each clone
  6140. - an additional clone will be produced, with the current, unshifted
  6141. date(s) in the entry.
  6142. - the original entry will be placed *after* all the clones, with
  6143. repeater intact.
  6144. - the start days in the repeater in the original entry will be shifted
  6145. to past the last clone.
  6146. I this way you can spell out a number of instances of a repeating task,
  6147. and still retain the repeater to cover future instances of the task."
  6148. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6149. (let (beg end template task
  6150. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6151. (if (not (and (integerp n) (> n 0)))
  6152. (error "Invalid number of replications %s" n))
  6153. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6154. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6155. shift)))
  6156. (error "Invalid shift specification %s" shift))
  6157. (when doshift
  6158. (setq shift-n (string-to-number (match-string 1 shift))
  6159. shift-what (cdr (assoc (match-string 2 shift)
  6160. '(("d" . day) ("w" . week)
  6161. ("m" . month) ("y" . year))))))
  6162. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6163. (setq nmin 1 nmax n)
  6164. (org-back-to-heading t)
  6165. (setq beg (point))
  6166. (org-end-of-subtree t t)
  6167. (or (bolp) (insert "\n"))
  6168. (setq end (point))
  6169. (setq template (buffer-substring beg end))
  6170. (when (and doshift
  6171. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6172. (delete-region beg end)
  6173. (setq end beg)
  6174. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6175. (goto-char end)
  6176. (loop for n from nmin to nmax do
  6177. (if (not doshift)
  6178. (setq task template)
  6179. (with-temp-buffer
  6180. (insert template)
  6181. (org-mode)
  6182. (goto-char (point-min))
  6183. (while (re-search-forward org-ts-regexp-both nil t)
  6184. (org-timestamp-change (* n shift-n) shift-what))
  6185. (unless (= n n-no-remove)
  6186. (goto-char (point-min))
  6187. (while (re-search-forward org-ts-regexp nil t)
  6188. (save-excursion
  6189. (goto-char (match-beginning 0))
  6190. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6191. (delete-region (match-beginning 1) (match-end 1))))))
  6192. (setq task (buffer-string))))
  6193. (insert task))
  6194. (goto-char beg)))
  6195. ;;; Outline Sorting
  6196. (defun org-sort (with-case)
  6197. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6198. Optional argument WITH-CASE means sort case-sensitively.
  6199. With a double prefix argument, also remove duplicate entries."
  6200. (interactive "P")
  6201. (if (org-at-table-p)
  6202. (org-call-with-arg 'org-table-sort-lines with-case)
  6203. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6204. (defun org-sort-remove-invisible (s)
  6205. (remove-text-properties 0 (length s) org-rm-props s)
  6206. (while (string-match org-bracket-link-regexp s)
  6207. (setq s (replace-match (if (match-end 2)
  6208. (match-string 3 s)
  6209. (match-string 1 s)) t t s)))
  6210. s)
  6211. (defvar org-priority-regexp) ; defined later in the file
  6212. (defvar org-after-sorting-entries-or-items-hook nil
  6213. "Hook that is run after a bunch of entries or items have been sorted.
  6214. When children are sorted, the cursor is in the parent line when this
  6215. hook gets called. When a region or a plain list is sorted, the cursor
  6216. will be in the first entry of the sorted region/list.")
  6217. (defun org-sort-entries-or-items
  6218. (&optional with-case sorting-type getkey-func compare-func property)
  6219. "Sort entries on a certain level of an outline tree, or plain list items.
  6220. If there is an active region, the entries in the region are sorted.
  6221. Else, if the cursor is before the first entry, sort the top-level items.
  6222. Else, the children of the entry at point are sorted.
  6223. If the cursor is at the first item in a plain list, the list items will be
  6224. sorted.
  6225. Sorting can be alphabetically, numerically, by date/time as given by
  6226. a time stamp, by a property or by priority.
  6227. The command prompts for the sorting type unless it has been given to the
  6228. function through the SORTING-TYPE argument, which needs to a character,
  6229. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6230. precise meaning of each character:
  6231. n Numerically, by converting the beginning of the entry/item to a number.
  6232. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6233. t By date/time, either the first active time stamp in the entry, or, if
  6234. none exist, by the first inactive one.
  6235. In items, only the first line will be checked.
  6236. s By the scheduled date/time.
  6237. d By deadline date/time.
  6238. c By creation time, which is assumed to be the first inactive time stamp
  6239. at the beginning of a line.
  6240. p By priority according to the cookie.
  6241. r By the value of a property.
  6242. Capital letters will reverse the sort order.
  6243. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6244. called with point at the beginning of the record. It must return either
  6245. a string or a number that should serve as the sorting key for that record.
  6246. Comparing entries ignores case by default. However, with an optional argument
  6247. WITH-CASE, the sorting considers case as well."
  6248. (interactive "P")
  6249. (let ((case-func (if with-case 'identity 'downcase))
  6250. start beg end stars re re2
  6251. txt what tmp plain-list-p)
  6252. ;; Find beginning and end of region to sort
  6253. (cond
  6254. ((org-region-active-p)
  6255. ;; we will sort the region
  6256. (setq end (region-end)
  6257. what "region")
  6258. (goto-char (region-beginning))
  6259. (if (not (org-on-heading-p)) (outline-next-heading))
  6260. (setq start (point)))
  6261. ((org-at-item-p)
  6262. ;; we will sort this plain list
  6263. (org-beginning-of-item-list) (setq start (point))
  6264. (org-end-of-item-list)
  6265. (or (bolp) (insert "\n"))
  6266. (setq end (point))
  6267. (goto-char start)
  6268. (setq plain-list-p t
  6269. what "plain list"))
  6270. ((or (org-on-heading-p)
  6271. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6272. ;; we will sort the children of the current headline
  6273. (org-back-to-heading)
  6274. (setq start (point)
  6275. end (progn (org-end-of-subtree t t)
  6276. (or (bolp) (insert "\n"))
  6277. (org-back-over-empty-lines)
  6278. (point))
  6279. what "children")
  6280. (goto-char start)
  6281. (show-subtree)
  6282. (outline-next-heading))
  6283. (t
  6284. ;; we will sort the top-level entries in this file
  6285. (goto-char (point-min))
  6286. (or (org-on-heading-p) (outline-next-heading))
  6287. (setq start (point))
  6288. (goto-char (point-max))
  6289. (beginning-of-line 1)
  6290. (when (looking-at ".*?\\S-")
  6291. ;; File ends in a non-white line
  6292. (end-of-line 1)
  6293. (insert "\n"))
  6294. (setq end (point-max))
  6295. (setq what "top-level")
  6296. (goto-char start)
  6297. (show-all)))
  6298. (setq beg (point))
  6299. (if (>= beg end) (error "Nothing to sort"))
  6300. (unless plain-list-p
  6301. (looking-at "\\(\\*+\\)")
  6302. (setq stars (match-string 1)
  6303. re (concat "^" (regexp-quote stars) " +")
  6304. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6305. txt (buffer-substring beg end))
  6306. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6307. (if (and (not (equal stars "*")) (string-match re2 txt))
  6308. (error "Region to sort contains a level above the first entry")))
  6309. (unless sorting-type
  6310. (message
  6311. (if plain-list-p
  6312. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6313. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6314. [t]ime [s]cheduled [d]eadline [c]reated
  6315. A/N/T/S/D/C/P/O/F means reversed:")
  6316. what)
  6317. (setq sorting-type (read-char-exclusive))
  6318. (and (= (downcase sorting-type) ?f)
  6319. (setq getkey-func
  6320. (org-icompleting-read "Sort using function: "
  6321. obarray 'fboundp t nil nil))
  6322. (setq getkey-func (intern getkey-func)))
  6323. (and (= (downcase sorting-type) ?r)
  6324. (setq property
  6325. (org-icompleting-read "Property: "
  6326. (mapcar 'list (org-buffer-property-keys t))
  6327. nil t))))
  6328. (message "Sorting entries...")
  6329. (save-restriction
  6330. (narrow-to-region start end)
  6331. (let ((dcst (downcase sorting-type))
  6332. (case-fold-search nil)
  6333. (now (current-time)))
  6334. (sort-subr
  6335. (/= dcst sorting-type)
  6336. ;; This function moves to the beginning character of the "record" to
  6337. ;; be sorted.
  6338. (if plain-list-p
  6339. (lambda nil
  6340. (if (org-at-item-p) t (goto-char (point-max))))
  6341. (lambda nil
  6342. (if (re-search-forward re nil t)
  6343. (goto-char (match-beginning 0))
  6344. (goto-char (point-max)))))
  6345. ;; This function moves to the last character of the "record" being
  6346. ;; sorted.
  6347. (if plain-list-p
  6348. 'org-end-of-item
  6349. (lambda nil
  6350. (save-match-data
  6351. (condition-case nil
  6352. (outline-forward-same-level 1)
  6353. (error
  6354. (goto-char (point-max)))))))
  6355. ;; This function returns the value that gets sorted against.
  6356. (if plain-list-p
  6357. (lambda nil
  6358. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6359. (cond
  6360. ((= dcst ?n)
  6361. (string-to-number (buffer-substring (match-end 0)
  6362. (point-at-eol))))
  6363. ((= dcst ?a)
  6364. (buffer-substring (match-end 0) (point-at-eol)))
  6365. ((= dcst ?t)
  6366. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6367. (re-search-forward org-ts-regexp-both
  6368. (point-at-eol) t))
  6369. (org-time-string-to-seconds (match-string 0))
  6370. (org-float-time now)))
  6371. ((= dcst ?f)
  6372. (if getkey-func
  6373. (progn
  6374. (setq tmp (funcall getkey-func))
  6375. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6376. tmp)
  6377. (error "Invalid key function `%s'" getkey-func)))
  6378. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6379. (lambda nil
  6380. (cond
  6381. ((= dcst ?n)
  6382. (if (looking-at org-complex-heading-regexp)
  6383. (string-to-number (match-string 4))
  6384. nil))
  6385. ((= dcst ?a)
  6386. (if (looking-at org-complex-heading-regexp)
  6387. (funcall case-func (match-string 4))
  6388. nil))
  6389. ((= dcst ?t)
  6390. (let ((end (save-excursion (outline-next-heading) (point))))
  6391. (if (or (re-search-forward org-ts-regexp end t)
  6392. (re-search-forward org-ts-regexp-both end t))
  6393. (org-time-string-to-seconds (match-string 0))
  6394. (org-float-time now))))
  6395. ((= dcst ?c)
  6396. (let ((end (save-excursion (outline-next-heading) (point))))
  6397. (if (re-search-forward
  6398. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6399. end t)
  6400. (org-time-string-to-seconds (match-string 0))
  6401. (org-float-time now))))
  6402. ((= dcst ?s)
  6403. (let ((end (save-excursion (outline-next-heading) (point))))
  6404. (if (re-search-forward org-scheduled-time-regexp end t)
  6405. (org-time-string-to-seconds (match-string 1))
  6406. (org-float-time now))))
  6407. ((= dcst ?d)
  6408. (let ((end (save-excursion (outline-next-heading) (point))))
  6409. (if (re-search-forward org-deadline-time-regexp end t)
  6410. (org-time-string-to-seconds (match-string 1))
  6411. (org-float-time now))))
  6412. ((= dcst ?p)
  6413. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6414. (string-to-char (match-string 2))
  6415. org-default-priority))
  6416. ((= dcst ?r)
  6417. (or (org-entry-get nil property) ""))
  6418. ((= dcst ?o)
  6419. (if (looking-at org-complex-heading-regexp)
  6420. (- 9999 (length (member (match-string 2)
  6421. org-todo-keywords-1)))))
  6422. ((= dcst ?f)
  6423. (if getkey-func
  6424. (progn
  6425. (setq tmp (funcall getkey-func))
  6426. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6427. tmp)
  6428. (error "Invalid key function `%s'" getkey-func)))
  6429. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6430. nil
  6431. (cond
  6432. ((= dcst ?a) 'string<)
  6433. ((= dcst ?f) compare-func)
  6434. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6435. (t nil)))))
  6436. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6437. (message "Sorting entries...done")))
  6438. (defun org-do-sort (table what &optional with-case sorting-type)
  6439. "Sort TABLE of WHAT according to SORTING-TYPE.
  6440. The user will be prompted for the SORTING-TYPE if the call to this
  6441. function does not specify it. WHAT is only for the prompt, to indicate
  6442. what is being sorted. The sorting key will be extracted from
  6443. the car of the elements of the table.
  6444. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6445. (unless sorting-type
  6446. (message
  6447. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6448. what)
  6449. (setq sorting-type (read-char-exclusive)))
  6450. (let ((dcst (downcase sorting-type))
  6451. extractfun comparefun)
  6452. ;; Define the appropriate functions
  6453. (cond
  6454. ((= dcst ?n)
  6455. (setq extractfun 'string-to-number
  6456. comparefun (if (= dcst sorting-type) '< '>)))
  6457. ((= dcst ?a)
  6458. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6459. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6460. comparefun (if (= dcst sorting-type)
  6461. 'string<
  6462. (lambda (a b) (and (not (string< a b))
  6463. (not (string= a b)))))))
  6464. ((= dcst ?t)
  6465. (setq extractfun
  6466. (lambda (x)
  6467. (if (or (string-match org-ts-regexp x)
  6468. (string-match org-ts-regexp-both x))
  6469. (org-float-time
  6470. (org-time-string-to-time (match-string 0 x)))
  6471. 0))
  6472. comparefun (if (= dcst sorting-type) '< '>)))
  6473. (t (error "Invalid sorting type `%c'" sorting-type)))
  6474. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6475. table)
  6476. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6477. ;;; The orgstruct minor mode
  6478. ;; Define a minor mode which can be used in other modes in order to
  6479. ;; integrate the org-mode structure editing commands.
  6480. ;; This is really a hack, because the org-mode structure commands use
  6481. ;; keys which normally belong to the major mode. Here is how it
  6482. ;; works: The minor mode defines all the keys necessary to operate the
  6483. ;; structure commands, but wraps the commands into a function which
  6484. ;; tests if the cursor is currently at a headline or a plain list
  6485. ;; item. If that is the case, the structure command is used,
  6486. ;; temporarily setting many Org-mode variables like regular
  6487. ;; expressions for filling etc. However, when any of those keys is
  6488. ;; used at a different location, function uses `key-binding' to look
  6489. ;; up if the key has an associated command in another currently active
  6490. ;; keymap (minor modes, major mode, global), and executes that
  6491. ;; command. There might be problems if any of the keys is otherwise
  6492. ;; used as a prefix key.
  6493. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6494. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6495. ;; addresses this by checking explicitly for both bindings.
  6496. (defvar orgstruct-mode-map (make-sparse-keymap)
  6497. "Keymap for the minor `orgstruct-mode'.")
  6498. (defvar org-local-vars nil
  6499. "List of local variables, for use by `orgstruct-mode'")
  6500. ;;;###autoload
  6501. (define-minor-mode orgstruct-mode
  6502. "Toggle the minor more `orgstruct-mode'.
  6503. This mode is for using Org-mode structure commands in other modes.
  6504. The following key behave as if Org-mode was active, if the cursor
  6505. is on a headline, or on a plain list item (both in the definition
  6506. of Org-mode).
  6507. M-up Move entry/item up
  6508. M-down Move entry/item down
  6509. M-left Promote
  6510. M-right Demote
  6511. M-S-up Move entry/item up
  6512. M-S-down Move entry/item down
  6513. M-S-left Promote subtree
  6514. M-S-right Demote subtree
  6515. M-q Fill paragraph and items like in Org-mode
  6516. C-c ^ Sort entries
  6517. C-c - Cycle list bullet
  6518. TAB Cycle item visibility
  6519. M-RET Insert new heading/item
  6520. S-M-RET Insert new TODO heading / Checkbox item
  6521. C-c C-c Set tags / toggle checkbox"
  6522. nil " OrgStruct" nil
  6523. (org-load-modules-maybe)
  6524. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6525. ;;;###autoload
  6526. (defun turn-on-orgstruct ()
  6527. "Unconditionally turn on `orgstruct-mode'."
  6528. (orgstruct-mode 1))
  6529. (defun orgstruct++-mode (&optional arg)
  6530. "Toggle `orgstruct-mode', the enhanced version of it.
  6531. In addition to setting orgstruct-mode, this also exports all indentation
  6532. and autofilling variables from org-mode into the buffer. It will also
  6533. recognize item context in multiline items.
  6534. Note that turning off orgstruct-mode will *not* remove the
  6535. indentation/paragraph settings. This can only be done by refreshing the
  6536. major mode, for example with \\[normal-mode]."
  6537. (interactive "P")
  6538. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6539. (if (< arg 1)
  6540. (orgstruct-mode -1)
  6541. (orgstruct-mode 1)
  6542. (let (var val)
  6543. (mapc
  6544. (lambda (x)
  6545. (when (string-match
  6546. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6547. (symbol-name (car x)))
  6548. (setq var (car x) val (nth 1 x))
  6549. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6550. org-local-vars)
  6551. (org-set-local 'orgstruct-is-++ t))))
  6552. (defvar orgstruct-is-++ nil
  6553. "Is orgstruct-mode in ++ version in the current-buffer?")
  6554. (make-variable-buffer-local 'orgstruct-is-++)
  6555. ;;;###autoload
  6556. (defun turn-on-orgstruct++ ()
  6557. "Unconditionally turn on `orgstruct++-mode'."
  6558. (orgstruct++-mode 1))
  6559. (defun orgstruct-error ()
  6560. "Error when there is no default binding for a structure key."
  6561. (interactive)
  6562. (error "This key has no function outside structure elements"))
  6563. (defun orgstruct-setup ()
  6564. "Setup orgstruct keymaps."
  6565. (let ((nfunc 0)
  6566. (bindings
  6567. (list
  6568. '([(meta up)] org-metaup)
  6569. '([(meta down)] org-metadown)
  6570. '([(meta left)] org-metaleft)
  6571. '([(meta right)] org-metaright)
  6572. '([(meta shift up)] org-shiftmetaup)
  6573. '([(meta shift down)] org-shiftmetadown)
  6574. '([(meta shift left)] org-shiftmetaleft)
  6575. '([(meta shift right)] org-shiftmetaright)
  6576. '([?\e (up)] org-metaup)
  6577. '([?\e (down)] org-metadown)
  6578. '([?\e (left)] org-metaleft)
  6579. '([?\e (right)] org-metaright)
  6580. '([?\e (shift up)] org-shiftmetaup)
  6581. '([?\e (shift down)] org-shiftmetadown)
  6582. '([?\e (shift left)] org-shiftmetaleft)
  6583. '([?\e (shift right)] org-shiftmetaright)
  6584. '([(shift up)] org-shiftup)
  6585. '([(shift down)] org-shiftdown)
  6586. '([(shift left)] org-shiftleft)
  6587. '([(shift right)] org-shiftright)
  6588. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6589. '("\M-q" fill-paragraph)
  6590. '("\C-c^" org-sort)
  6591. '("\C-c-" org-cycle-list-bullet)))
  6592. elt key fun cmd)
  6593. (while (setq elt (pop bindings))
  6594. (setq nfunc (1+ nfunc))
  6595. (setq key (org-key (car elt))
  6596. fun (nth 1 elt)
  6597. cmd (orgstruct-make-binding fun nfunc key))
  6598. (org-defkey orgstruct-mode-map key cmd))
  6599. ;; Special treatment needed for TAB and RET
  6600. (org-defkey orgstruct-mode-map [(tab)]
  6601. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6602. (org-defkey orgstruct-mode-map "\C-i"
  6603. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6604. (org-defkey orgstruct-mode-map "\M-\C-m"
  6605. (orgstruct-make-binding 'org-insert-heading 105
  6606. "\M-\C-m" [(meta return)]))
  6607. (org-defkey orgstruct-mode-map [(meta return)]
  6608. (orgstruct-make-binding 'org-insert-heading 106
  6609. [(meta return)] "\M-\C-m"))
  6610. (org-defkey orgstruct-mode-map [(shift meta return)]
  6611. (orgstruct-make-binding 'org-insert-todo-heading 107
  6612. [(meta return)] "\M-\C-m"))
  6613. (org-defkey orgstruct-mode-map "\e\C-m"
  6614. (orgstruct-make-binding 'org-insert-heading 108
  6615. "\e\C-m" [?\e (return)]))
  6616. (org-defkey orgstruct-mode-map [?\e (return)]
  6617. (orgstruct-make-binding 'org-insert-heading 109
  6618. [?\e (return)] "\e\C-m"))
  6619. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6620. (orgstruct-make-binding 'org-insert-todo-heading 110
  6621. [?\e (return)] "\e\C-m"))
  6622. (unless org-local-vars
  6623. (setq org-local-vars (org-get-local-variables)))
  6624. t))
  6625. (defun orgstruct-make-binding (fun n &rest keys)
  6626. "Create a function for binding in the structure minor mode.
  6627. FUN is the command to call inside a table. N is used to create a unique
  6628. command name. KEYS are keys that should be checked in for a command
  6629. to execute outside of tables."
  6630. (eval
  6631. (list 'defun
  6632. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6633. '(arg)
  6634. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6635. "Outside of structure, run the binding of `"
  6636. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6637. "'.")
  6638. '(interactive "p")
  6639. (list 'if
  6640. `(org-context-p 'headline 'item
  6641. (and orgstruct-is-++
  6642. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6643. 'item-body))
  6644. (list 'org-run-like-in-org-mode (list 'quote fun))
  6645. (list 'let '(orgstruct-mode)
  6646. (list 'call-interactively
  6647. (append '(or)
  6648. (mapcar (lambda (k)
  6649. (list 'key-binding k))
  6650. keys)
  6651. '('orgstruct-error))))))))
  6652. (defun org-context-p (&rest contexts)
  6653. "Check if local context is any of CONTEXTS.
  6654. Possible values in the list of contexts are `table', `headline', and `item'."
  6655. (let ((pos (point)))
  6656. (goto-char (point-at-bol))
  6657. (prog1 (or (and (memq 'table contexts)
  6658. (looking-at "[ \t]*|"))
  6659. (and (memq 'headline contexts)
  6660. ;;????????? (looking-at "\\*+"))
  6661. (looking-at outline-regexp))
  6662. (and (memq 'item contexts)
  6663. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6664. (and (memq 'item-body contexts)
  6665. (org-in-item-p)))
  6666. (goto-char pos))))
  6667. (defun org-get-local-variables ()
  6668. "Return a list of all local variables in an org-mode buffer."
  6669. (let (varlist)
  6670. (with-current-buffer (get-buffer-create "*Org tmp*")
  6671. (erase-buffer)
  6672. (org-mode)
  6673. (setq varlist (buffer-local-variables)))
  6674. (kill-buffer "*Org tmp*")
  6675. (delq nil
  6676. (mapcar
  6677. (lambda (x)
  6678. (setq x
  6679. (if (symbolp x)
  6680. (list x)
  6681. (list (car x) (list 'quote (cdr x)))))
  6682. (if (string-match
  6683. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6684. (symbol-name (car x)))
  6685. x nil))
  6686. varlist))))
  6687. ;;;###autoload
  6688. (defun org-run-like-in-org-mode (cmd)
  6689. "Run a command, pretending that the current buffer is in Org-mode.
  6690. This will temporarily bind local variables that are typically bound in
  6691. Org-mode to the values they have in Org-mode, and then interactively
  6692. call CMD."
  6693. (org-load-modules-maybe)
  6694. (unless org-local-vars
  6695. (setq org-local-vars (org-get-local-variables)))
  6696. (eval (list 'let org-local-vars
  6697. (list 'call-interactively (list 'quote cmd)))))
  6698. ;;;; Archiving
  6699. (defun org-get-category (&optional pos)
  6700. "Get the category applying to position POS."
  6701. (get-text-property (or pos (point)) 'org-category))
  6702. (defun org-refresh-category-properties ()
  6703. "Refresh category text properties in the buffer."
  6704. (let ((def-cat (cond
  6705. ((null org-category)
  6706. (if buffer-file-name
  6707. (file-name-sans-extension
  6708. (file-name-nondirectory buffer-file-name))
  6709. "???"))
  6710. ((symbolp org-category) (symbol-name org-category))
  6711. (t org-category)))
  6712. beg end cat pos optionp)
  6713. (org-unmodified
  6714. (save-excursion
  6715. (save-restriction
  6716. (widen)
  6717. (goto-char (point-min))
  6718. (put-text-property (point) (point-max) 'org-category def-cat)
  6719. (while (re-search-forward
  6720. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6721. (setq pos (match-end 0)
  6722. optionp (equal (char-after (match-beginning 0)) ?#)
  6723. cat (org-trim (match-string 2)))
  6724. (if optionp
  6725. (setq beg (point-at-bol) end (point-max))
  6726. (org-back-to-heading t)
  6727. (setq beg (point) end (org-end-of-subtree t t)))
  6728. (put-text-property beg end 'org-category cat)
  6729. (goto-char pos)))))))
  6730. ;;;; Link Stuff
  6731. ;;; Link abbreviations
  6732. (defun org-link-expand-abbrev (link)
  6733. "Apply replacements as defined in `org-link-abbrev-alist."
  6734. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6735. (let* ((key (match-string 1 link))
  6736. (as (or (assoc key org-link-abbrev-alist-local)
  6737. (assoc key org-link-abbrev-alist)))
  6738. (tag (and (match-end 2) (match-string 3 link)))
  6739. rpl)
  6740. (if (not as)
  6741. link
  6742. (setq rpl (cdr as))
  6743. (cond
  6744. ((symbolp rpl) (funcall rpl tag))
  6745. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6746. ((string-match "%h" rpl)
  6747. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6748. (t (concat rpl tag)))))
  6749. link))
  6750. ;;; Storing and inserting links
  6751. (defvar org-insert-link-history nil
  6752. "Minibuffer history for links inserted with `org-insert-link'.")
  6753. (defvar org-stored-links nil
  6754. "Contains the links stored with `org-store-link'.")
  6755. (defvar org-store-link-plist nil
  6756. "Plist with info about the most recently link created with `org-store-link'.")
  6757. (defvar org-link-protocols nil
  6758. "Link protocols added to Org-mode using `org-add-link-type'.")
  6759. (defvar org-store-link-functions nil
  6760. "List of functions that are called to create and store a link.
  6761. Each function will be called in turn until one returns a non-nil
  6762. value. Each function should check if it is responsible for creating
  6763. this link (for example by looking at the major mode).
  6764. If not, it must exit and return nil.
  6765. If yes, it should return a non-nil value after a calling
  6766. `org-store-link-props' with a list of properties and values.
  6767. Special properties are:
  6768. :type The link prefix. like \"http\". This must be given.
  6769. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6770. This is obligatory as well.
  6771. :description Optional default description for the second pair
  6772. of brackets in an Org-mode link. The user can still change
  6773. this when inserting this link into an Org-mode buffer.
  6774. In addition to these, any additional properties can be specified
  6775. and then used in remember templates.")
  6776. (defun org-add-link-type (type &optional follow export)
  6777. "Add TYPE to the list of `org-link-types'.
  6778. Re-compute all regular expressions depending on `org-link-types'
  6779. FOLLOW and EXPORT are two functions.
  6780. FOLLOW should take the link path as the single argument and do whatever
  6781. is necessary to follow the link, for example find a file or display
  6782. a mail message.
  6783. EXPORT should format the link path for export to one of the export formats.
  6784. It should be a function accepting three arguments:
  6785. path the path of the link, the text after the prefix (like \"http:\")
  6786. desc the description of the link, if any, nil if there was no description
  6787. format the export format, a symbol like `html' or `latex'.
  6788. The function may use the FORMAT information to return different values
  6789. depending on the format. The return value will be put literally into
  6790. the exported file.
  6791. Org-mode has a built-in default for exporting links. If you are happy with
  6792. this default, there is no need to define an export function for the link
  6793. type. For a simple example of an export function, see `org-bbdb.el'."
  6794. (add-to-list 'org-link-types type t)
  6795. (org-make-link-regexps)
  6796. (if (assoc type org-link-protocols)
  6797. (setcdr (assoc type org-link-protocols) (list follow export))
  6798. (push (list type follow export) org-link-protocols)))
  6799. (defvar org-agenda-buffer-name)
  6800. ;;;###autoload
  6801. (defun org-store-link (arg)
  6802. "\\<org-mode-map>Store an org-link to the current location.
  6803. This link is added to `org-stored-links' and can later be inserted
  6804. into an org-buffer with \\[org-insert-link].
  6805. For some link types, a prefix arg is interpreted:
  6806. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6807. For file links, arg negates `org-context-in-file-links'."
  6808. (interactive "P")
  6809. (org-load-modules-maybe)
  6810. (setq org-store-link-plist nil) ; reset
  6811. (let ((outline-regexp (org-get-limited-outline-regexp))
  6812. link cpltxt desc description search txt custom-id)
  6813. (cond
  6814. ((run-hook-with-args-until-success 'org-store-link-functions)
  6815. (setq link (plist-get org-store-link-plist :link)
  6816. desc (or (plist-get org-store-link-plist :description) link)))
  6817. ((equal (buffer-name) "*Org Edit Src Example*")
  6818. (let (label gc)
  6819. (while (or (not label)
  6820. (save-excursion
  6821. (save-restriction
  6822. (widen)
  6823. (goto-char (point-min))
  6824. (re-search-forward
  6825. (regexp-quote (format org-coderef-label-format label))
  6826. nil t))))
  6827. (when label (message "Label exists already") (sit-for 2))
  6828. (setq label (read-string "Code line label: " label)))
  6829. (end-of-line 1)
  6830. (setq link (format org-coderef-label-format label))
  6831. (setq gc (- 79 (length link)))
  6832. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6833. (insert link)
  6834. (setq link (concat "(" label ")") desc nil)))
  6835. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6836. ;; We are in the agenda, link to referenced location
  6837. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6838. (get-text-property (point) 'org-marker))))
  6839. (when m
  6840. (org-with-point-at m
  6841. (call-interactively 'org-store-link)))))
  6842. ((eq major-mode 'calendar-mode)
  6843. (let ((cd (calendar-cursor-to-date)))
  6844. (setq link
  6845. (format-time-string
  6846. (car org-time-stamp-formats)
  6847. (apply 'encode-time
  6848. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6849. nil nil nil))))
  6850. (org-store-link-props :type "calendar" :date cd)))
  6851. ((eq major-mode 'w3-mode)
  6852. (setq cpltxt (if (and (buffer-name)
  6853. (not (string-match "Untitled" (buffer-name))))
  6854. (buffer-name)
  6855. (url-view-url t))
  6856. link (org-make-link (url-view-url t)))
  6857. (org-store-link-props :type "w3" :url (url-view-url t)))
  6858. ((eq major-mode 'w3m-mode)
  6859. (setq cpltxt (or w3m-current-title w3m-current-url)
  6860. link (org-make-link w3m-current-url))
  6861. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6862. ((setq search (run-hook-with-args-until-success
  6863. 'org-create-file-search-functions))
  6864. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6865. "::" search))
  6866. (setq cpltxt (or description link)))
  6867. ((eq major-mode 'image-mode)
  6868. (setq cpltxt (concat "file:"
  6869. (abbreviate-file-name buffer-file-name))
  6870. link (org-make-link cpltxt))
  6871. (org-store-link-props :type "image" :file buffer-file-name))
  6872. ((eq major-mode 'dired-mode)
  6873. ;; link to the file in the current line
  6874. (setq cpltxt (concat "file:"
  6875. (abbreviate-file-name
  6876. (expand-file-name
  6877. (dired-get-filename nil t))))
  6878. link (org-make-link cpltxt)))
  6879. ((and buffer-file-name (org-mode-p))
  6880. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6881. (cond
  6882. ((org-in-regexp "<<\\(.*?\\)>>")
  6883. (setq cpltxt
  6884. (concat "file:"
  6885. (abbreviate-file-name buffer-file-name)
  6886. "::" (match-string 1))
  6887. link (org-make-link cpltxt)))
  6888. ((and (featurep 'org-id)
  6889. (or (eq org-link-to-org-use-id t)
  6890. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6891. (interactive-p))
  6892. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6893. (interactive-p)
  6894. (not custom-id))
  6895. (and org-link-to-org-use-id
  6896. (condition-case nil
  6897. (org-entry-get nil "ID")
  6898. (error nil)))))
  6899. ;; We can make a link using the ID.
  6900. (setq link (condition-case nil
  6901. (prog1 (org-id-store-link)
  6902. (setq desc (plist-get org-store-link-plist
  6903. :description)))
  6904. (error
  6905. ;; probably before first headline, link to file only
  6906. (concat "file:"
  6907. (abbreviate-file-name buffer-file-name))))))
  6908. (t
  6909. ;; Just link to current headline
  6910. (setq cpltxt (concat "file:"
  6911. (abbreviate-file-name buffer-file-name)))
  6912. ;; Add a context search string
  6913. (when (org-xor org-context-in-file-links arg)
  6914. (setq txt (cond
  6915. ((org-on-heading-p) nil)
  6916. ((org-region-active-p)
  6917. (buffer-substring (region-beginning) (region-end)))
  6918. (t nil)))
  6919. (when (or (null txt) (string-match "\\S-" txt))
  6920. (setq cpltxt
  6921. (concat cpltxt "::"
  6922. (condition-case nil
  6923. (org-make-org-heading-search-string txt)
  6924. (error "")))
  6925. desc (or (nth 4 (ignore-errors
  6926. (org-heading-components))) "NONE"))))
  6927. (if (string-match "::\\'" cpltxt)
  6928. (setq cpltxt (substring cpltxt 0 -2)))
  6929. (setq link (org-make-link cpltxt)))))
  6930. ((buffer-file-name (buffer-base-buffer))
  6931. ;; Just link to this file here.
  6932. (setq cpltxt (concat "file:"
  6933. (abbreviate-file-name
  6934. (buffer-file-name (buffer-base-buffer)))))
  6935. ;; Add a context string
  6936. (when (org-xor org-context-in-file-links arg)
  6937. (setq txt (if (org-region-active-p)
  6938. (buffer-substring (region-beginning) (region-end))
  6939. (buffer-substring (point-at-bol) (point-at-eol))))
  6940. ;; Only use search option if there is some text.
  6941. (when (string-match "\\S-" txt)
  6942. (setq cpltxt
  6943. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6944. desc "NONE")))
  6945. (setq link (org-make-link cpltxt)))
  6946. ((interactive-p)
  6947. (error "Cannot link to a buffer which is not visiting a file"))
  6948. (t (setq link nil)))
  6949. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6950. (setq link (or link cpltxt)
  6951. desc (or desc cpltxt))
  6952. (if (equal desc "NONE") (setq desc nil))
  6953. (if (and (or (interactive-p) executing-kbd-macro) link)
  6954. (progn
  6955. (setq org-stored-links
  6956. (cons (list link desc) org-stored-links))
  6957. (message "Stored: %s" (or desc link))
  6958. (when custom-id
  6959. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6960. "::#" custom-id))
  6961. (setq org-stored-links
  6962. (cons (list link desc) org-stored-links))))
  6963. (and link (org-make-link-string link desc)))))
  6964. (defun org-store-link-props (&rest plist)
  6965. "Store link properties, extract names and addresses."
  6966. (let (x adr)
  6967. (when (setq x (plist-get plist :from))
  6968. (setq adr (mail-extract-address-components x))
  6969. (setq plist (plist-put plist :fromname (car adr)))
  6970. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6971. (when (setq x (plist-get plist :to))
  6972. (setq adr (mail-extract-address-components x))
  6973. (setq plist (plist-put plist :toname (car adr)))
  6974. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6975. (let ((from (plist-get plist :from))
  6976. (to (plist-get plist :to)))
  6977. (when (and from to org-from-is-user-regexp)
  6978. (setq plist
  6979. (plist-put plist :fromto
  6980. (if (string-match org-from-is-user-regexp from)
  6981. (concat "to %t")
  6982. (concat "from %f"))))))
  6983. (setq org-store-link-plist plist))
  6984. (defun org-add-link-props (&rest plist)
  6985. "Add these properties to the link property list."
  6986. (let (key value)
  6987. (while plist
  6988. (setq key (pop plist) value (pop plist))
  6989. (setq org-store-link-plist
  6990. (plist-put org-store-link-plist key value)))))
  6991. (defun org-email-link-description (&optional fmt)
  6992. "Return the description part of an email link.
  6993. This takes information from `org-store-link-plist' and formats it
  6994. according to FMT (default from `org-email-link-description-format')."
  6995. (setq fmt (or fmt org-email-link-description-format))
  6996. (let* ((p org-store-link-plist)
  6997. (to (plist-get p :toaddress))
  6998. (from (plist-get p :fromaddress))
  6999. (table
  7000. (list
  7001. (cons "%c" (plist-get p :fromto))
  7002. (cons "%F" (plist-get p :from))
  7003. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7004. (cons "%T" (plist-get p :to))
  7005. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7006. (cons "%s" (plist-get p :subject))
  7007. (cons "%m" (plist-get p :message-id)))))
  7008. (when (string-match "%c" fmt)
  7009. ;; Check if the user wrote this message
  7010. (if (and org-from-is-user-regexp from to
  7011. (save-match-data (string-match org-from-is-user-regexp from)))
  7012. (setq fmt (replace-match "to %t" t t fmt))
  7013. (setq fmt (replace-match "from %f" t t fmt))))
  7014. (org-replace-escapes fmt table)))
  7015. (defun org-make-org-heading-search-string (&optional string heading)
  7016. "Make search string for STRING or current headline."
  7017. (interactive)
  7018. (let ((s (or string (org-get-heading))))
  7019. (unless (and string (not heading))
  7020. ;; We are using a headline, clean up garbage in there.
  7021. (if (string-match org-todo-regexp s)
  7022. (setq s (replace-match "" t t s)))
  7023. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7024. (setq s (replace-match "" t t s)))
  7025. (setq s (org-trim s))
  7026. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7027. org-comment-string "\\)") s)
  7028. (setq s (replace-match "" t t s)))
  7029. (while (string-match org-ts-regexp s)
  7030. (setq s (replace-match "" t t s))))
  7031. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7032. (setq s (replace-match " " t t s)))
  7033. (or string (setq s (concat "*" s))) ; Add * for headlines
  7034. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7035. (defun org-make-link (&rest strings)
  7036. "Concatenate STRINGS."
  7037. (apply 'concat strings))
  7038. (defun org-make-link-string (link &optional description)
  7039. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7040. (unless (string-match "\\S-" link)
  7041. (error "Empty link"))
  7042. (when (and description
  7043. (stringp description)
  7044. (not (string-match "\\S-" description)))
  7045. (setq description nil))
  7046. (when (stringp description)
  7047. ;; Remove brackets from the description, they are fatal.
  7048. (while (string-match "\\[" description)
  7049. (setq description (replace-match "{" t t description)))
  7050. (while (string-match "\\]" description)
  7051. (setq description (replace-match "}" t t description))))
  7052. (when (equal (org-link-escape link) description)
  7053. ;; No description needed, it is identical
  7054. (setq description nil))
  7055. (when (and (not description)
  7056. (not (equal link (org-link-escape link))))
  7057. (setq description (org-extract-attributes link)))
  7058. (concat "[[" (org-link-escape link) "]"
  7059. (if description (concat "[" description "]") "")
  7060. "]"))
  7061. (defconst org-link-escape-chars
  7062. '((?\ . "%20")
  7063. (?\[ . "%5B")
  7064. (?\] . "%5D")
  7065. (?\340 . "%E0") ; `a
  7066. (?\342 . "%E2") ; ^a
  7067. (?\347 . "%E7") ; ,c
  7068. (?\350 . "%E8") ; `e
  7069. (?\351 . "%E9") ; 'e
  7070. (?\352 . "%EA") ; ^e
  7071. (?\356 . "%EE") ; ^i
  7072. (?\364 . "%F4") ; ^o
  7073. (?\371 . "%F9") ; `u
  7074. (?\373 . "%FB") ; ^u
  7075. (?\; . "%3B")
  7076. (?? . "%3F")
  7077. (?= . "%3D")
  7078. (?+ . "%2B")
  7079. )
  7080. "Association list of escapes for some characters problematic in links.
  7081. This is the list that is used for internal purposes.")
  7082. (defvar org-url-encoding-use-url-hexify nil)
  7083. (defconst org-link-escape-chars-browser
  7084. '((?\ . "%20")) ; 32 for the SPC char
  7085. "Association list of escapes for some characters problematic in links.
  7086. This is the list that is used before handing over to the browser.")
  7087. (defun org-link-escape (text &optional table)
  7088. "Escape characters in TEXT that are problematic for links."
  7089. (if (and org-url-encoding-use-url-hexify (not table))
  7090. (url-hexify-string text)
  7091. (setq table (or table org-link-escape-chars))
  7092. (when text
  7093. (let ((re (mapconcat (lambda (x) (regexp-quote
  7094. (char-to-string (car x))))
  7095. table "\\|")))
  7096. (while (string-match re text)
  7097. (setq text
  7098. (replace-match
  7099. (cdr (assoc (string-to-char (match-string 0 text))
  7100. table))
  7101. t t text)))
  7102. text))))
  7103. (defun org-link-unescape (text &optional table)
  7104. "Reverse the action of `org-link-escape'."
  7105. (if (and org-url-encoding-use-url-hexify (not table))
  7106. (url-unhex-string text)
  7107. (setq table (or table org-link-escape-chars))
  7108. (when text
  7109. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  7110. table "\\|")))
  7111. (while (string-match re text)
  7112. (setq text
  7113. (replace-match
  7114. (char-to-string (car (rassoc (match-string 0 text) table)))
  7115. t t text)))
  7116. text))))
  7117. (defun org-xor (a b)
  7118. "Exclusive or."
  7119. (if a (not b) b))
  7120. (defun org-fixup-message-id-for-http (s)
  7121. "Replace special characters in a message id, so it can be used in an http query."
  7122. (while (string-match "<" s)
  7123. (setq s (replace-match "%3C" t t s)))
  7124. (while (string-match ">" s)
  7125. (setq s (replace-match "%3E" t t s)))
  7126. (while (string-match "@" s)
  7127. (setq s (replace-match "%40" t t s)))
  7128. s)
  7129. ;;;###autoload
  7130. (defun org-insert-link-global ()
  7131. "Insert a link like Org-mode does.
  7132. This command can be called in any mode to insert a link in Org-mode syntax."
  7133. (interactive)
  7134. (org-load-modules-maybe)
  7135. (org-run-like-in-org-mode 'org-insert-link))
  7136. (defun org-insert-link (&optional complete-file link-location)
  7137. "Insert a link. At the prompt, enter the link.
  7138. Completion can be used to insert any of the link protocol prefixes like
  7139. http or ftp in use.
  7140. The history can be used to select a link previously stored with
  7141. `org-store-link'. When the empty string is entered (i.e. if you just
  7142. press RET at the prompt), the link defaults to the most recently
  7143. stored link. As SPC triggers completion in the minibuffer, you need to
  7144. use M-SPC or C-q SPC to force the insertion of a space character.
  7145. You will also be prompted for a description, and if one is given, it will
  7146. be displayed in the buffer instead of the link.
  7147. If there is already a link at point, this command will allow you to edit link
  7148. and description parts.
  7149. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7150. be selected using completion. The path to the file will be relative to the
  7151. current directory if the file is in the current directory or a subdirectory.
  7152. Otherwise, the link will be the absolute path as completed in the minibuffer
  7153. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7154. option `org-link-file-path-type'.
  7155. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7156. the current directory or below.
  7157. With three \\[universal-argument] prefixes, negate the meaning of
  7158. `org-keep-stored-link-after-insertion'.
  7159. If `org-make-link-description-function' is non-nil, this function will be
  7160. called with the link target, and the result will be the default
  7161. link description.
  7162. If the LINK-LOCATION parameter is non-nil, this value will be
  7163. used as the link location instead of reading one interactively."
  7164. (interactive "P")
  7165. (let* ((wcf (current-window-configuration))
  7166. (region (if (org-region-active-p)
  7167. (buffer-substring (region-beginning) (region-end))))
  7168. (remove (and region (list (region-beginning) (region-end))))
  7169. (desc region)
  7170. tmphist ; byte-compile incorrectly complains about this
  7171. (link link-location)
  7172. entry file all-prefixes)
  7173. (cond
  7174. (link-location) ; specified by arg, just use it.
  7175. ((org-in-regexp org-bracket-link-regexp 1)
  7176. ;; We do have a link at point, and we are going to edit it.
  7177. (setq remove (list (match-beginning 0) (match-end 0)))
  7178. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7179. (setq link (read-string "Link: "
  7180. (org-link-unescape
  7181. (org-match-string-no-properties 1)))))
  7182. ((or (org-in-regexp org-angle-link-re)
  7183. (org-in-regexp org-plain-link-re))
  7184. ;; Convert to bracket link
  7185. (setq remove (list (match-beginning 0) (match-end 0))
  7186. link (read-string "Link: "
  7187. (org-remove-angle-brackets (match-string 0)))))
  7188. ((member complete-file '((4) (16)))
  7189. ;; Completing read for file names.
  7190. (setq link (org-file-complete-link complete-file)))
  7191. (t
  7192. ;; Read link, with completion for stored links.
  7193. (with-output-to-temp-buffer "*Org Links*"
  7194. (princ "Insert a link.
  7195. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7196. (when org-stored-links
  7197. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7198. (princ (mapconcat
  7199. (lambda (x)
  7200. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7201. (reverse org-stored-links) "\n"))))
  7202. (let ((cw (selected-window)))
  7203. (select-window (get-buffer-window "*Org Links*"))
  7204. (setq truncate-lines t)
  7205. (unless (pos-visible-in-window-p (point-max))
  7206. (org-fit-window-to-buffer))
  7207. (and (window-live-p cw) (select-window cw)))
  7208. ;; Fake a link history, containing the stored links.
  7209. (setq tmphist (append (mapcar 'car org-stored-links)
  7210. org-insert-link-history))
  7211. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7212. (mapcar 'car org-link-abbrev-alist)
  7213. org-link-types))
  7214. (unwind-protect
  7215. (progn
  7216. (setq link
  7217. (let ((org-completion-use-ido nil)
  7218. (org-completion-use-iswitchb nil))
  7219. (org-completing-read
  7220. "Link: "
  7221. (append
  7222. (mapcar (lambda (x) (list (concat x ":")))
  7223. all-prefixes)
  7224. (mapcar 'car org-stored-links))
  7225. nil nil nil
  7226. 'tmphist
  7227. (car (car org-stored-links)))))
  7228. (if (not (string-match "\\S-" link))
  7229. (error "No link selected"))
  7230. (if (or (member link all-prefixes)
  7231. (and (equal ":" (substring link -1))
  7232. (member (substring link 0 -1) all-prefixes)
  7233. (setq link (substring link 0 -1))))
  7234. (setq link (org-link-try-special-completion link))))
  7235. (set-window-configuration wcf)
  7236. (kill-buffer "*Org Links*"))
  7237. (setq entry (assoc link org-stored-links))
  7238. (or entry (push link org-insert-link-history))
  7239. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7240. (not org-keep-stored-link-after-insertion))
  7241. (setq org-stored-links (delq (assoc link org-stored-links)
  7242. org-stored-links)))
  7243. (setq desc (or desc (nth 1 entry)))))
  7244. (if (string-match org-plain-link-re link)
  7245. ;; URL-like link, normalize the use of angular brackets.
  7246. (setq link (org-make-link (org-remove-angle-brackets link))))
  7247. ;; Check if we are linking to the current file with a search option
  7248. ;; If yes, simplify the link by using only the search option.
  7249. (when (and buffer-file-name
  7250. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7251. (let* ((path (match-string 1 link))
  7252. (case-fold-search nil)
  7253. (search (match-string 2 link)))
  7254. (save-match-data
  7255. (if (equal (file-truename buffer-file-name) (file-truename path))
  7256. ;; We are linking to this same file, with a search option
  7257. (setq link search)))))
  7258. ;; Check if we can/should use a relative path. If yes, simplify the link
  7259. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7260. (let* ((type (match-string 1 link))
  7261. (path (match-string 2 link))
  7262. (origpath path)
  7263. (case-fold-search nil))
  7264. (cond
  7265. ((or (eq org-link-file-path-type 'absolute)
  7266. (equal complete-file '(16)))
  7267. (setq path (abbreviate-file-name (expand-file-name path))))
  7268. ((eq org-link-file-path-type 'noabbrev)
  7269. (setq path (expand-file-name path)))
  7270. ((eq org-link-file-path-type 'relative)
  7271. (setq path (file-relative-name path)))
  7272. (t
  7273. (save-match-data
  7274. (if (string-match (concat "^" (regexp-quote
  7275. (file-name-as-directory
  7276. (expand-file-name "."))))
  7277. (expand-file-name path))
  7278. ;; We are linking a file with relative path name.
  7279. (setq path (substring (expand-file-name path)
  7280. (match-end 0)))
  7281. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7282. (setq link (concat type path))
  7283. (if (equal desc origpath)
  7284. (setq desc path))))
  7285. (if org-make-link-description-function
  7286. (setq desc (funcall org-make-link-description-function link desc)))
  7287. (setq desc (read-string "Description: " desc))
  7288. (unless (string-match "\\S-" desc) (setq desc nil))
  7289. (if remove (apply 'delete-region remove))
  7290. (insert (org-make-link-string link desc))))
  7291. (defun org-link-try-special-completion (type)
  7292. "If there is completion support for link type TYPE, offer it."
  7293. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7294. (if (functionp fun)
  7295. (funcall fun)
  7296. (read-string "Link (no completion support): " (concat type ":")))))
  7297. (defun org-file-complete-link (&optional arg)
  7298. "Create a file link using completion."
  7299. (let (file link)
  7300. (setq file (read-file-name "File: "))
  7301. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7302. (pwd1 (file-name-as-directory (abbreviate-file-name
  7303. (expand-file-name ".")))))
  7304. (cond
  7305. ((equal arg '(16))
  7306. (setq link (org-make-link
  7307. "file:"
  7308. (abbreviate-file-name (expand-file-name file)))))
  7309. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7310. (setq link (org-make-link "file:" (match-string 1 file))))
  7311. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7312. (expand-file-name file))
  7313. (setq link (org-make-link
  7314. "file:" (match-string 1 (expand-file-name file)))))
  7315. (t (setq link (org-make-link "file:" file)))))
  7316. link))
  7317. (defun org-completing-read (&rest args)
  7318. "Completing-read with SPACE being a normal character."
  7319. (let ((minibuffer-local-completion-map
  7320. (copy-keymap minibuffer-local-completion-map)))
  7321. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7322. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7323. (apply 'org-icompleting-read args)))
  7324. (defun org-completing-read-no-i (&rest args)
  7325. (let (org-completion-use-ido org-completion-use-iswitchb)
  7326. (apply 'org-completing-read args)))
  7327. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7328. "Use iswitch as a completing-read replacement to choose from choices.
  7329. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7330. from."
  7331. (let* ((iswitchb-use-virtual-buffers nil)
  7332. (iswitchb-make-buflist-hook
  7333. (lambda ()
  7334. (setq iswitchb-temp-buflist choices))))
  7335. (iswitchb-read-buffer prompt)))
  7336. (defun org-icompleting-read (&rest args)
  7337. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7338. (org-without-partial-completion
  7339. (if (and org-completion-use-ido
  7340. (fboundp 'ido-completing-read)
  7341. (boundp 'ido-mode) ido-mode
  7342. (listp (second args)))
  7343. (let ((ido-enter-matching-directory nil))
  7344. (apply 'ido-completing-read (concat (car args))
  7345. (if (consp (car (nth 1 args)))
  7346. (mapcar (lambda (x) (car x)) (nth 1 args))
  7347. (nth 1 args))
  7348. (cddr args)))
  7349. (if (and org-completion-use-iswitchb
  7350. (boundp 'iswitchb-mode) iswitchb-mode
  7351. (listp (second args)))
  7352. (apply 'org-iswitchb-completing-read (concat (car args))
  7353. (if (consp (car (nth 1 args)))
  7354. (mapcar (lambda (x) (car x)) (nth 1 args))
  7355. (nth 1 args))
  7356. (cddr args))
  7357. (apply 'completing-read args)))))
  7358. (defun org-extract-attributes (s)
  7359. "Extract the attributes cookie from a string and set as text property."
  7360. (let (a attr (start 0) key value)
  7361. (save-match-data
  7362. (when (string-match "{{\\([^}]+\\)}}$" s)
  7363. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7364. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7365. (setq key (match-string 1 a) value (match-string 2 a)
  7366. start (match-end 0)
  7367. attr (plist-put attr (intern key) value))))
  7368. (org-add-props s nil 'org-attr attr))
  7369. s))
  7370. (defun org-extract-attributes-from-string (tag)
  7371. (let (key value attr)
  7372. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7373. (setq key (match-string 1 tag) value (match-string 2 tag)
  7374. tag (replace-match "" t t tag)
  7375. attr (plist-put attr (intern key) value)))
  7376. (cons tag attr)))
  7377. (defun org-attributes-to-string (plist)
  7378. "Format a property list into an HTML attribute list."
  7379. (let ((s "") key value)
  7380. (while plist
  7381. (setq key (pop plist) value (pop plist))
  7382. (and value
  7383. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7384. s))
  7385. ;;; Opening/following a link
  7386. (defvar org-link-search-failed nil)
  7387. (defvar org-open-link-functions nil
  7388. "Hook for functions finding a plain text link.
  7389. These functions must take a single argument, the link content.
  7390. They will be called for links that look like [[link text][description]]
  7391. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7392. like a filename (e.g. \"./blue.png\").
  7393. These functions will be called *before* Org attempts to resolve the
  7394. link by doing text searches in the current buffer - so if you want a
  7395. link \"[[target]]\" to still find \"<<target>>\", your function should
  7396. handle this as a special case.
  7397. When the function does handle the link, it must return a non-nil value.
  7398. If it decides that it is not responsible for this link, it must return
  7399. nil to indicate that that Org-mode can continue with other options
  7400. like exact and fuzzy text search.")
  7401. (defun org-next-link ()
  7402. "Move forward to the next link.
  7403. If the link is in hidden text, expose it."
  7404. (interactive)
  7405. (when (and org-link-search-failed (eq this-command last-command))
  7406. (goto-char (point-min))
  7407. (message "Link search wrapped back to beginning of buffer"))
  7408. (setq org-link-search-failed nil)
  7409. (let* ((pos (point))
  7410. (ct (org-context))
  7411. (a (assoc :link ct)))
  7412. (if a (goto-char (nth 2 a)))
  7413. (if (re-search-forward org-any-link-re nil t)
  7414. (progn
  7415. (goto-char (match-beginning 0))
  7416. (if (org-invisible-p) (org-show-context)))
  7417. (goto-char pos)
  7418. (setq org-link-search-failed t)
  7419. (error "No further link found"))))
  7420. (defun org-previous-link ()
  7421. "Move backward to the previous link.
  7422. If the link is in hidden text, expose it."
  7423. (interactive)
  7424. (when (and org-link-search-failed (eq this-command last-command))
  7425. (goto-char (point-max))
  7426. (message "Link search wrapped back to end of buffer"))
  7427. (setq org-link-search-failed nil)
  7428. (let* ((pos (point))
  7429. (ct (org-context))
  7430. (a (assoc :link ct)))
  7431. (if a (goto-char (nth 1 a)))
  7432. (if (re-search-backward org-any-link-re nil t)
  7433. (progn
  7434. (goto-char (match-beginning 0))
  7435. (if (org-invisible-p) (org-show-context)))
  7436. (goto-char pos)
  7437. (setq org-link-search-failed t)
  7438. (error "No further link found"))))
  7439. (defun org-translate-link (s)
  7440. "Translate a link string if a translation function has been defined."
  7441. (if (and org-link-translation-function
  7442. (fboundp org-link-translation-function)
  7443. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7444. (progn
  7445. (setq s (funcall org-link-translation-function
  7446. (match-string 1) (match-string 2)))
  7447. (concat (car s) ":" (cdr s)))
  7448. s))
  7449. (defun org-translate-link-from-planner (type path)
  7450. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7451. This is still an experimental function, your mileage may vary."
  7452. (cond
  7453. ((member type '("http" "https" "news" "ftp"))
  7454. ;; standard Internet links are the same.
  7455. nil)
  7456. ((and (equal type "irc") (string-match "^//" path))
  7457. ;; Planner has two / at the beginning of an irc link, we have 1.
  7458. ;; We should have zero, actually....
  7459. (setq path (substring path 1)))
  7460. ((and (equal type "lisp") (string-match "^/" path))
  7461. ;; Planner has a slash, we do not.
  7462. (setq type "elisp" path (substring path 1)))
  7463. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7464. ;; A typical message link. Planner has the id after the final slash,
  7465. ;; we separate it with a hash mark
  7466. (setq path (concat (match-string 1 path) "#"
  7467. (org-remove-angle-brackets (match-string 2 path)))))
  7468. )
  7469. (cons type path))
  7470. (defun org-find-file-at-mouse (ev)
  7471. "Open file link or URL at mouse."
  7472. (interactive "e")
  7473. (mouse-set-point ev)
  7474. (org-open-at-point 'in-emacs))
  7475. (defun org-open-at-mouse (ev)
  7476. "Open file link or URL at mouse."
  7477. (interactive "e")
  7478. (mouse-set-point ev)
  7479. (if (eq major-mode 'org-agenda-mode)
  7480. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7481. (org-open-at-point))
  7482. (defvar org-window-config-before-follow-link nil
  7483. "The window configuration before following a link.
  7484. This is saved in case the need arises to restore it.")
  7485. (defvar org-open-link-marker (make-marker)
  7486. "Marker pointing to the location where `org-open-at-point; was called.")
  7487. ;;;###autoload
  7488. (defun org-open-at-point-global ()
  7489. "Follow a link like Org-mode does.
  7490. This command can be called in any mode to follow a link that has
  7491. Org-mode syntax."
  7492. (interactive)
  7493. (org-run-like-in-org-mode 'org-open-at-point))
  7494. ;;;###autoload
  7495. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7496. "Open a link in the string S, as if it was in Org-mode."
  7497. (interactive "sLink: \nP")
  7498. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7499. (with-temp-buffer
  7500. (let ((org-inhibit-startup t))
  7501. (org-mode)
  7502. (insert s)
  7503. (goto-char (point-min))
  7504. (org-open-at-point arg reference-buffer)))))
  7505. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7506. "Open link at or after point.
  7507. If there is no link at point, this function will search forward up to
  7508. the end of the current line.
  7509. Normally, files will be opened by an appropriate application. If the
  7510. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7511. With a double prefix argument, try to open outside of Emacs, in the
  7512. application the system uses for this file type."
  7513. (interactive "P")
  7514. (org-load-modules-maybe)
  7515. (move-marker org-open-link-marker (point))
  7516. (setq org-window-config-before-follow-link (current-window-configuration))
  7517. (org-remove-occur-highlights nil nil t)
  7518. (cond
  7519. ((and (org-on-heading-p)
  7520. (not (org-in-regexp
  7521. (concat org-plain-link-re "\\|"
  7522. org-bracket-link-regexp "\\|"
  7523. org-angle-link-re "\\|"
  7524. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7525. (or (org-offer-links-in-entry in-emacs)
  7526. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7527. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7528. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7529. (org-footnote-action))
  7530. (t
  7531. (let (type path link line search (pos (point)))
  7532. (catch 'match
  7533. (save-excursion
  7534. (skip-chars-forward "^]\n\r")
  7535. (when (org-in-regexp org-bracket-link-regexp 1)
  7536. (setq link (org-extract-attributes
  7537. (org-link-unescape (org-match-string-no-properties 1))))
  7538. (while (string-match " *\n *" link)
  7539. (setq link (replace-match " " t t link)))
  7540. (setq link (org-link-expand-abbrev link))
  7541. (cond
  7542. ((or (file-name-absolute-p link)
  7543. (string-match "^\\.\\.?/" link))
  7544. (setq type "file" path link))
  7545. ((string-match org-link-re-with-space3 link)
  7546. (setq type (match-string 1 link) path (match-string 2 link)))
  7547. (t (setq type "thisfile" path link)))
  7548. (throw 'match t)))
  7549. (when (get-text-property (point) 'org-linked-text)
  7550. (setq type "thisfile"
  7551. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7552. (1+ (point)) (point))
  7553. path (buffer-substring
  7554. (previous-single-property-change pos 'org-linked-text)
  7555. (next-single-property-change pos 'org-linked-text)))
  7556. (throw 'match t))
  7557. (save-excursion
  7558. (when (or (org-in-regexp org-angle-link-re)
  7559. (org-in-regexp org-plain-link-re))
  7560. (setq type (match-string 1) path (match-string 2))
  7561. (throw 'match t)))
  7562. (save-excursion
  7563. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7564. (setq type "tags"
  7565. path (match-string 1))
  7566. (while (string-match ":" path)
  7567. (setq path (replace-match "+" t t path)))
  7568. (throw 'match t)))
  7569. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7570. (setq type "tree-match"
  7571. path (match-string 1))
  7572. (throw 'match t)))
  7573. (unless path
  7574. (error "No link found"))
  7575. ;; switch back to reference buffer
  7576. ;; needed when if called in a temporary buffer through
  7577. ;; org-open-link-from-string
  7578. (with-current-buffer (or reference-buffer (current-buffer))
  7579. ;; Remove any trailing spaces in path
  7580. (if (string-match " +\\'" path)
  7581. (setq path (replace-match "" t t path)))
  7582. (if (and org-link-translation-function
  7583. (fboundp org-link-translation-function))
  7584. ;; Check if we need to translate the link
  7585. (let ((tmp (funcall org-link-translation-function type path)))
  7586. (setq type (car tmp) path (cdr tmp))))
  7587. (cond
  7588. ((assoc type org-link-protocols)
  7589. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7590. ((equal type "mailto")
  7591. (let ((cmd (car org-link-mailto-program))
  7592. (args (cdr org-link-mailto-program)) args1
  7593. (address path) (subject "") a)
  7594. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7595. (setq address (match-string 1 path)
  7596. subject (org-link-escape (match-string 2 path))))
  7597. (while args
  7598. (cond
  7599. ((not (stringp (car args))) (push (pop args) args1))
  7600. (t (setq a (pop args))
  7601. (if (string-match "%a" a)
  7602. (setq a (replace-match address t t a)))
  7603. (if (string-match "%s" a)
  7604. (setq a (replace-match subject t t a)))
  7605. (push a args1))))
  7606. (apply cmd (nreverse args1))))
  7607. ((member type '("http" "https" "ftp" "news"))
  7608. (browse-url (concat type ":" (org-link-escape
  7609. path org-link-escape-chars-browser))))
  7610. ((member type '("message"))
  7611. (browse-url (concat type ":" path)))
  7612. ((string= type "tags")
  7613. (org-tags-view in-emacs path))
  7614. ((string= type "tree-match")
  7615. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7616. ((string= type "file")
  7617. (if (string-match "::\\([0-9]+\\)\\'" path)
  7618. (setq line (string-to-number (match-string 1 path))
  7619. path (substring path 0 (match-beginning 0)))
  7620. (if (string-match "::\\(.+\\)\\'" path)
  7621. (setq search (match-string 1 path)
  7622. path (substring path 0 (match-beginning 0)))))
  7623. (if (string-match "[*?{]" (file-name-nondirectory path))
  7624. (dired path)
  7625. (org-open-file path in-emacs line search)))
  7626. ((string= type "news")
  7627. (require 'org-gnus)
  7628. (org-gnus-follow-link path))
  7629. ((string= type "shell")
  7630. (let ((cmd path))
  7631. (if (or (not org-confirm-shell-link-function)
  7632. (funcall org-confirm-shell-link-function
  7633. (format "Execute \"%s\" in shell? "
  7634. (org-add-props cmd nil
  7635. 'face 'org-warning))))
  7636. (progn
  7637. (message "Executing %s" cmd)
  7638. (shell-command cmd))
  7639. (error "Abort"))))
  7640. ((string= type "elisp")
  7641. (let ((cmd path))
  7642. (if (or (not org-confirm-elisp-link-function)
  7643. (funcall org-confirm-elisp-link-function
  7644. (format "Execute \"%s\" as elisp? "
  7645. (org-add-props cmd nil
  7646. 'face 'org-warning))))
  7647. (message "%s => %s" cmd
  7648. (if (equal (string-to-char cmd) ?\()
  7649. (eval (read cmd))
  7650. (call-interactively (read cmd))))
  7651. (error "Abort"))))
  7652. ((and (string= type "thisfile")
  7653. (run-hook-with-args-until-success
  7654. 'org-open-link-functions path)))
  7655. ((string= type "thisfile")
  7656. (if in-emacs
  7657. (switch-to-buffer-other-window
  7658. (org-get-buffer-for-internal-link (current-buffer)))
  7659. (org-mark-ring-push))
  7660. (let ((cmd `(org-link-search
  7661. ,path
  7662. ,(cond ((equal in-emacs '(4)) 'occur)
  7663. ((equal in-emacs '(16)) 'org-occur)
  7664. (t nil))
  7665. ,pos)))
  7666. (condition-case nil (eval cmd)
  7667. (error (progn (widen) (eval cmd))))))
  7668. (t
  7669. (browse-url-at-point)))))))
  7670. (move-marker org-open-link-marker nil)
  7671. (run-hook-with-args 'org-follow-link-hook))
  7672. (defun org-offer-links-in-entry (&optional nth zero)
  7673. "Offer links in the current entry and follow the selected link.
  7674. If there is only one link, follow it immediately as well.
  7675. If NTH is an integer, immediately pick the NTH link found.
  7676. If ZERO is a string, check also this string for a link, and if
  7677. there is one, offer it as link number zero."
  7678. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7679. "\\(" org-angle-link-re "\\)\\|"
  7680. "\\(" org-plain-link-re "\\)"))
  7681. (cnt ?0)
  7682. (in-emacs (if (integerp nth) nil nth))
  7683. have-zero end links link c)
  7684. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7685. (push (match-string 0 zero) links)
  7686. (setq cnt (1- cnt) have-zero t))
  7687. (save-excursion
  7688. (org-back-to-heading t)
  7689. (setq end (save-excursion (outline-next-heading) (point)))
  7690. (while (re-search-forward re end t)
  7691. (push (match-string 0) links))
  7692. (setq links (org-uniquify (reverse links))))
  7693. (cond
  7694. ((null links)
  7695. (message "No links"))
  7696. ((equal (length links) 1)
  7697. (setq link (list (car links))))
  7698. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7699. (setq link (nth (if have-zero nth (1- nth)) links)))
  7700. (t ; we have to select a link
  7701. (save-excursion
  7702. (save-window-excursion
  7703. (delete-other-windows)
  7704. (with-output-to-temp-buffer "*Select Link*"
  7705. (mapc (lambda (l)
  7706. (if (not (string-match org-bracket-link-regexp l))
  7707. (princ (format "[%c] %s\n" (incf cnt)
  7708. (org-remove-angle-brackets l)))
  7709. (if (match-end 3)
  7710. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7711. (match-string 3 l) (match-string 1 l)))
  7712. (princ (format "[%c] %s\n" (incf cnt)
  7713. (match-string 1 l))))))
  7714. links))
  7715. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7716. (message "Select link to open, RET to open all:")
  7717. (setq c (read-char-exclusive))
  7718. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7719. (when (equal c ?q) (error "Abort"))
  7720. (if (equal c ?\C-m)
  7721. (setq link links)
  7722. (setq nth (- c ?0))
  7723. (if have-zero (setq nth (1+ nth)))
  7724. (unless (and (integerp nth) (>= (length links) nth))
  7725. (error "Invalid link selection"))
  7726. (setq link (list (nth (1- nth) links))))))
  7727. (if link
  7728. (let ((buf (current-buffer)))
  7729. (dolist (l link)
  7730. (org-open-link-from-string l in-emacs buf))
  7731. t)
  7732. nil)))
  7733. ;; Add special file links that specify the way of opening
  7734. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7735. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7736. (defun org-open-file-with-system (path)
  7737. "Open file at PATH using the system way of opeing it."
  7738. (org-open-file path 'system))
  7739. (defun org-open-file-with-emacs (path)
  7740. "Open file at PATH in emacs."
  7741. (org-open-file path 'emacs))
  7742. (defun org-remove-file-link-modifiers ()
  7743. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7744. (goto-char (point-min))
  7745. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7746. (org-if-unprotected
  7747. (replace-match "file:" t t))))
  7748. (eval-after-load "org-exp"
  7749. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7750. 'org-remove-file-link-modifiers))
  7751. ;;;; Time estimates
  7752. (defun org-get-effort (&optional pom)
  7753. "Get the effort estimate for the current entry."
  7754. (org-entry-get pom org-effort-property))
  7755. ;;; File search
  7756. (defvar org-create-file-search-functions nil
  7757. "List of functions to construct the right search string for a file link.
  7758. These functions are called in turn with point at the location to
  7759. which the link should point.
  7760. A function in the hook should first test if it would like to
  7761. handle this file type, for example by checking the major-mode or
  7762. the file extension. If it decides not to handle this file, it
  7763. should just return nil to give other functions a chance. If it
  7764. does handle the file, it must return the search string to be used
  7765. when following the link. The search string will be part of the
  7766. file link, given after a double colon, and `org-open-at-point'
  7767. will automatically search for it. If special measures must be
  7768. taken to make the search successful, another function should be
  7769. added to the companion hook `org-execute-file-search-functions',
  7770. which see.
  7771. A function in this hook may also use `setq' to set the variable
  7772. `description' to provide a suggestion for the descriptive text to
  7773. be used for this link when it gets inserted into an Org-mode
  7774. buffer with \\[org-insert-link].")
  7775. (defvar org-execute-file-search-functions nil
  7776. "List of functions to execute a file search triggered by a link.
  7777. Functions added to this hook must accept a single argument, the
  7778. search string that was part of the file link, the part after the
  7779. double colon. The function must first check if it would like to
  7780. handle this search, for example by checking the major-mode or the
  7781. file extension. If it decides not to handle this search, it
  7782. should just return nil to give other functions a chance. If it
  7783. does handle the search, it must return a non-nil value to keep
  7784. other functions from trying.
  7785. Each function can access the current prefix argument through the
  7786. variable `current-prefix-argument'. Note that a single prefix is
  7787. used to force opening a link in Emacs, so it may be good to only
  7788. use a numeric or double prefix to guide the search function.
  7789. In case this is needed, a function in this hook can also restore
  7790. the window configuration before `org-open-at-point' was called using:
  7791. (set-window-configuration org-window-config-before-follow-link)")
  7792. (defun org-link-search (s &optional type avoid-pos)
  7793. "Search for a link search option.
  7794. If S is surrounded by forward slashes, it is interpreted as a
  7795. regular expression. In org-mode files, this will create an `org-occur'
  7796. sparse tree. In ordinary files, `occur' will be used to list matches.
  7797. If the current buffer is in `dired-mode', grep will be used to search
  7798. in all files. If AVOID-POS is given, ignore matches near that position."
  7799. (let ((case-fold-search t)
  7800. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7801. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7802. (append '(("") (" ") ("\t") ("\n"))
  7803. org-emphasis-alist)
  7804. "\\|") "\\)"))
  7805. (pos (point))
  7806. (pre nil) (post nil)
  7807. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7808. (cond
  7809. ;; First check if there are any special
  7810. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7811. ;; Now try the builtin stuff
  7812. ((and (equal (string-to-char s0) ?#)
  7813. (> (length s0) 1)
  7814. (save-excursion
  7815. (goto-char (point-min))
  7816. (and
  7817. (re-search-forward
  7818. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7819. (setq type 'dedicated
  7820. pos (match-beginning 0))))
  7821. ;; There is an exact target for this
  7822. (goto-char pos)
  7823. (org-back-to-heading t)))
  7824. ((save-excursion
  7825. (goto-char (point-min))
  7826. (and
  7827. (re-search-forward
  7828. (concat "<<" (regexp-quote s0) ">>") nil t)
  7829. (setq type 'dedicated
  7830. pos (match-beginning 0))))
  7831. ;; There is an exact target for this
  7832. (goto-char pos))
  7833. ((and (string-match "^(\\(.*\\))$" s0)
  7834. (save-excursion
  7835. (goto-char (point-min))
  7836. (and
  7837. (re-search-forward
  7838. (concat "[^[]" (regexp-quote
  7839. (format org-coderef-label-format
  7840. (match-string 1 s0))))
  7841. nil t)
  7842. (setq type 'dedicated
  7843. pos (1+ (match-beginning 0))))))
  7844. ;; There is a coderef target for this
  7845. (goto-char pos))
  7846. ((string-match "^/\\(.*\\)/$" s)
  7847. ;; A regular expression
  7848. (cond
  7849. ((org-mode-p)
  7850. (org-occur (match-string 1 s)))
  7851. ;;((eq major-mode 'dired-mode)
  7852. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7853. (t (org-do-occur (match-string 1 s)))))
  7854. (t
  7855. ;; A normal search strings
  7856. (when (equal (string-to-char s) ?*)
  7857. ;; Anchor on headlines, post may include tags.
  7858. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7859. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7860. s (substring s 1)))
  7861. (remove-text-properties
  7862. 0 (length s)
  7863. '(face nil mouse-face nil keymap nil fontified nil) s)
  7864. ;; Make a series of regular expressions to find a match
  7865. (setq words (org-split-string s "[ \n\r\t]+")
  7866. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7867. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7868. "\\)" markers)
  7869. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7870. re2a (concat "[ \t\r\n]" re2a_)
  7871. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7872. re4 (concat "[^a-zA-Z_]" re4_)
  7873. re1 (concat pre re2 post)
  7874. re3 (concat pre (if pre re4_ re4) post)
  7875. re5 (concat pre ".*" re4)
  7876. re2 (concat pre re2)
  7877. re2a (concat pre (if pre re2a_ re2a))
  7878. re4 (concat pre (if pre re4_ re4))
  7879. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7880. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7881. re5 "\\)"
  7882. ))
  7883. (cond
  7884. ((eq type 'org-occur) (org-occur reall))
  7885. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7886. (t (goto-char (point-min))
  7887. (setq type 'fuzzy)
  7888. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7889. (org-search-not-self 1 re1 nil t)
  7890. (org-search-not-self 1 re2 nil t)
  7891. (org-search-not-self 1 re2a nil t)
  7892. (org-search-not-self 1 re3 nil t)
  7893. (org-search-not-self 1 re4 nil t)
  7894. (org-search-not-self 1 re5 nil t)
  7895. )
  7896. (goto-char (match-beginning 1))
  7897. (goto-char pos)
  7898. (error "No match")))))
  7899. (t
  7900. ;; Normal string-search
  7901. (goto-char (point-min))
  7902. (if (search-forward s nil t)
  7903. (goto-char (match-beginning 0))
  7904. (error "No match"))))
  7905. (and (org-mode-p) (org-show-context 'link-search))
  7906. type))
  7907. (defun org-search-not-self (group &rest args)
  7908. "Execute `re-search-forward', but only accept matches that do not
  7909. enclose the position of `org-open-link-marker'."
  7910. (let ((m org-open-link-marker))
  7911. (catch 'exit
  7912. (while (apply 're-search-forward args)
  7913. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7914. (goto-char (match-end group))
  7915. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7916. (> (match-beginning 0) (marker-position m))
  7917. (< (match-end 0) (marker-position m)))
  7918. (save-match-data
  7919. (or (not (org-in-regexp
  7920. org-bracket-link-analytic-regexp 1))
  7921. (not (match-end 4)) ; no description
  7922. (and (<= (match-beginning 4) (point))
  7923. (>= (match-end 4) (point))))))
  7924. (throw 'exit (point))))))))
  7925. (defun org-get-buffer-for-internal-link (buffer)
  7926. "Return a buffer to be used for displaying the link target of internal links."
  7927. (cond
  7928. ((not org-display-internal-link-with-indirect-buffer)
  7929. buffer)
  7930. ((string-match "(Clone)$" (buffer-name buffer))
  7931. (message "Buffer is already a clone, not making another one")
  7932. ;; we also do not modify visibility in this case
  7933. buffer)
  7934. (t ; make a new indirect buffer for displaying the link
  7935. (let* ((bn (buffer-name buffer))
  7936. (ibn (concat bn "(Clone)"))
  7937. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7938. (with-current-buffer ib (org-overview))
  7939. ib))))
  7940. (defun org-do-occur (regexp &optional cleanup)
  7941. "Call the Emacs command `occur'.
  7942. If CLEANUP is non-nil, remove the printout of the regular expression
  7943. in the *Occur* buffer. This is useful if the regex is long and not useful
  7944. to read."
  7945. (occur regexp)
  7946. (when cleanup
  7947. (let ((cwin (selected-window)) win beg end)
  7948. (when (setq win (get-buffer-window "*Occur*"))
  7949. (select-window win))
  7950. (goto-char (point-min))
  7951. (when (re-search-forward "match[a-z]+" nil t)
  7952. (setq beg (match-end 0))
  7953. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7954. (setq end (1- (match-beginning 0)))))
  7955. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7956. (goto-char (point-min))
  7957. (select-window cwin))))
  7958. ;;; The mark ring for links jumps
  7959. (defvar org-mark-ring nil
  7960. "Mark ring for positions before jumps in Org-mode.")
  7961. (defvar org-mark-ring-last-goto nil
  7962. "Last position in the mark ring used to go back.")
  7963. ;; Fill and close the ring
  7964. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7965. (loop for i from 1 to org-mark-ring-length do
  7966. (push (make-marker) org-mark-ring))
  7967. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7968. org-mark-ring)
  7969. (defun org-mark-ring-push (&optional pos buffer)
  7970. "Put the current position or POS into the mark ring and rotate it."
  7971. (interactive)
  7972. (setq pos (or pos (point)))
  7973. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7974. (move-marker (car org-mark-ring)
  7975. (or pos (point))
  7976. (or buffer (current-buffer)))
  7977. (message "%s"
  7978. (substitute-command-keys
  7979. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7980. (defun org-mark-ring-goto (&optional n)
  7981. "Jump to the previous position in the mark ring.
  7982. With prefix arg N, jump back that many stored positions. When
  7983. called several times in succession, walk through the entire ring.
  7984. Org-mode commands jumping to a different position in the current file,
  7985. or to another Org-mode file, automatically push the old position
  7986. onto the ring."
  7987. (interactive "p")
  7988. (let (p m)
  7989. (if (eq last-command this-command)
  7990. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7991. (setq p org-mark-ring))
  7992. (setq org-mark-ring-last-goto p)
  7993. (setq m (car p))
  7994. (switch-to-buffer (marker-buffer m))
  7995. (goto-char m)
  7996. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7997. (defun org-remove-angle-brackets (s)
  7998. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7999. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8000. s)
  8001. (defun org-add-angle-brackets (s)
  8002. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8003. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8004. s)
  8005. (defun org-remove-double-quotes (s)
  8006. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8007. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8008. s)
  8009. ;;; Following specific links
  8010. (defun org-follow-timestamp-link ()
  8011. (cond
  8012. ((org-at-date-range-p t)
  8013. (let ((org-agenda-start-on-weekday)
  8014. (t1 (match-string 1))
  8015. (t2 (match-string 2)))
  8016. (setq t1 (time-to-days (org-time-string-to-time t1))
  8017. t2 (time-to-days (org-time-string-to-time t2)))
  8018. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8019. ((org-at-timestamp-p t)
  8020. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8021. (substring (match-string 1) 0 10)))
  8022. 1))
  8023. (t (error "This should not happen"))))
  8024. ;;; Following file links
  8025. (defvar org-wait nil)
  8026. (defun org-open-file (path &optional in-emacs line search)
  8027. "Open the file at PATH.
  8028. First, this expands any special file name abbreviations. Then the
  8029. configuration variable `org-file-apps' is checked if it contains an
  8030. entry for this file type, and if yes, the corresponding command is launched.
  8031. If no application is found, Emacs simply visits the file.
  8032. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8033. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8034. and to use an external application to visit the file.
  8035. Optional LINE specifies a line to go to, optional SEARCH a string to
  8036. search for. If LINE or SEARCH is given, the file will always be
  8037. opened in Emacs.
  8038. If the file does not exist, an error is thrown."
  8039. (setq in-emacs (or in-emacs line search))
  8040. (let* ((file (if (equal path "")
  8041. buffer-file-name
  8042. (substitute-in-file-name (expand-file-name path))))
  8043. (apps (append org-file-apps (org-default-apps)))
  8044. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8045. (dirp (if remp nil (file-directory-p file)))
  8046. (file (if (and dirp org-open-directory-means-index-dot-org)
  8047. (concat (file-name-as-directory file) "index.org")
  8048. file))
  8049. (a-m-a-p (assq 'auto-mode apps))
  8050. (dfile (downcase file))
  8051. (old-buffer (current-buffer))
  8052. (old-pos (point))
  8053. (old-mode major-mode)
  8054. ext cmd)
  8055. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8056. (setq ext (match-string 1 dfile))
  8057. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8058. (setq ext (match-string 1 dfile))))
  8059. (cond
  8060. ((member in-emacs '((16) system))
  8061. (setq cmd (cdr (assoc 'system apps))))
  8062. (in-emacs (setq cmd 'emacs))
  8063. (t
  8064. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8065. (and dirp (cdr (assoc 'directory apps)))
  8066. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8067. 'string-match)
  8068. (cdr (assoc ext apps))
  8069. (cdr (assoc t apps))))))
  8070. (when (eq cmd 'system)
  8071. (setq cmd (cdr (assoc 'system apps))))
  8072. (when (eq cmd 'default)
  8073. (setq cmd (cdr (assoc t apps))))
  8074. (when (eq cmd 'mailcap)
  8075. (require 'mailcap)
  8076. (mailcap-parse-mailcaps)
  8077. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8078. (command (mailcap-mime-info mime-type)))
  8079. (if (stringp command)
  8080. (setq cmd command)
  8081. (setq cmd 'emacs))))
  8082. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8083. (not (file-exists-p file))
  8084. (not org-open-non-existing-files))
  8085. (error "No such file: %s" file))
  8086. (cond
  8087. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8088. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8089. (while (string-match "['\"]%s['\"]" cmd)
  8090. (setq cmd (replace-match "%s" t t cmd)))
  8091. (while (string-match "%s" cmd)
  8092. (setq cmd (replace-match
  8093. (save-match-data
  8094. (shell-quote-argument
  8095. (convert-standard-filename file)))
  8096. t t cmd)))
  8097. (save-window-excursion
  8098. (start-process-shell-command cmd nil cmd)
  8099. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8100. ))
  8101. ((or (stringp cmd)
  8102. (eq cmd 'emacs))
  8103. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8104. (widen)
  8105. (if line (org-goto-line line)
  8106. (if search (org-link-search search))))
  8107. ((consp cmd)
  8108. (let ((file (convert-standard-filename file)))
  8109. (eval cmd)))
  8110. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8111. (and (org-mode-p) (eq old-mode 'org-mode)
  8112. (or (not (equal old-buffer (current-buffer)))
  8113. (not (equal old-pos (point))))
  8114. (org-mark-ring-push old-pos old-buffer))))
  8115. (defun org-default-apps ()
  8116. "Return the default applications for this operating system."
  8117. (cond
  8118. ((eq system-type 'darwin)
  8119. org-file-apps-defaults-macosx)
  8120. ((eq system-type 'windows-nt)
  8121. org-file-apps-defaults-windowsnt)
  8122. (t org-file-apps-defaults-gnu)))
  8123. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8124. "Convert extensions to regular expressions in the cars of LIST.
  8125. Also, weed out any non-string entries, because the return value is used
  8126. only for regexp matching.
  8127. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8128. point to the symbol `emacs', indicating that the file should
  8129. be opened in Emacs."
  8130. (append
  8131. (delq nil
  8132. (mapcar (lambda (x)
  8133. (if (not (stringp (car x)))
  8134. nil
  8135. (if (string-match "\\W" (car x))
  8136. x
  8137. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8138. list))
  8139. (if add-auto-mode
  8140. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8141. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8142. (defun org-file-remote-p (file)
  8143. "Test whether FILE specifies a location on a remote system.
  8144. Return non-nil if the location is indeed remote.
  8145. For example, the filename \"/user@host:/foo\" specifies a location
  8146. on the system \"/user@host:\"."
  8147. (cond ((fboundp 'file-remote-p)
  8148. (file-remote-p file))
  8149. ((fboundp 'tramp-handle-file-remote-p)
  8150. (tramp-handle-file-remote-p file))
  8151. ((and (boundp 'ange-ftp-name-format)
  8152. (string-match (car ange-ftp-name-format) file))
  8153. t)
  8154. (t nil)))
  8155. ;;;; Refiling
  8156. (defun org-get-org-file ()
  8157. "Read a filename, with default directory `org-directory'."
  8158. (let ((default (or org-default-notes-file remember-data-file)))
  8159. (read-file-name (format "File name [%s]: " default)
  8160. (file-name-as-directory org-directory)
  8161. default)))
  8162. (defun org-notes-order-reversed-p ()
  8163. "Check if the current file should receive notes in reversed order."
  8164. (cond
  8165. ((not org-reverse-note-order) nil)
  8166. ((eq t org-reverse-note-order) t)
  8167. ((not (listp org-reverse-note-order)) nil)
  8168. (t (catch 'exit
  8169. (let ((all org-reverse-note-order)
  8170. entry)
  8171. (while (setq entry (pop all))
  8172. (if (string-match (car entry) buffer-file-name)
  8173. (throw 'exit (cdr entry))))
  8174. nil)))))
  8175. (defvar org-refile-target-table nil
  8176. "The list of refile targets, created by `org-refile'.")
  8177. (defvar org-agenda-new-buffers nil
  8178. "Buffers created to visit agenda files.")
  8179. (defun org-get-refile-targets (&optional default-buffer)
  8180. "Produce a table with refile targets."
  8181. (let ((case-fold-search nil)
  8182. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8183. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8184. targets txt re files f desc descre fast-path-p level pos0)
  8185. (message "Getting targets...")
  8186. (with-current-buffer (or default-buffer (current-buffer))
  8187. (while (setq entry (pop entries))
  8188. (setq files (car entry) desc (cdr entry))
  8189. (setq fast-path-p nil)
  8190. (cond
  8191. ((null files) (setq files (list (current-buffer))))
  8192. ((eq files 'org-agenda-files)
  8193. (setq files (org-agenda-files 'unrestricted)))
  8194. ((and (symbolp files) (fboundp files))
  8195. (setq files (funcall files)))
  8196. ((and (symbolp files) (boundp files))
  8197. (setq files (symbol-value files))))
  8198. (if (stringp files) (setq files (list files)))
  8199. (cond
  8200. ((eq (car desc) :tag)
  8201. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8202. ((eq (car desc) :todo)
  8203. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8204. ((eq (car desc) :regexp)
  8205. (setq descre (cdr desc)))
  8206. ((eq (car desc) :level)
  8207. (setq descre (concat "^\\*\\{" (number-to-string
  8208. (if org-odd-levels-only
  8209. (1- (* 2 (cdr desc)))
  8210. (cdr desc)))
  8211. "\\}[ \t]")))
  8212. ((eq (car desc) :maxlevel)
  8213. (setq fast-path-p t)
  8214. (setq descre (concat "^\\*\\{1," (number-to-string
  8215. (if org-odd-levels-only
  8216. (1- (* 2 (cdr desc)))
  8217. (cdr desc)))
  8218. "\\}[ \t]")))
  8219. (t (error "Bad refiling target description %s" desc)))
  8220. (while (setq f (pop files))
  8221. (with-current-buffer
  8222. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8223. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8224. (setq f (and f (expand-file-name f)))
  8225. (if (eq org-refile-use-outline-path 'file)
  8226. (push (list (file-name-nondirectory f) f nil nil) targets))
  8227. (save-excursion
  8228. (save-restriction
  8229. (widen)
  8230. (goto-char (point-min))
  8231. (while (re-search-forward descre nil t)
  8232. (goto-char (setq pos0 (point-at-bol)))
  8233. (catch 'next
  8234. (when org-refile-target-verify-function
  8235. (save-match-data
  8236. (or (funcall org-refile-target-verify-function)
  8237. (throw 'next t))))
  8238. (when (looking-at org-complex-heading-regexp)
  8239. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8240. txt (org-link-display-format (match-string 4))
  8241. re (concat "^" (regexp-quote
  8242. (buffer-substring (match-beginning 1)
  8243. (match-end 4)))))
  8244. (if (match-end 5) (setq re (concat re "[ \t]+"
  8245. (regexp-quote
  8246. (match-string 5)))))
  8247. (setq re (concat re "[ \t]*$"))
  8248. (when org-refile-use-outline-path
  8249. (setq txt (mapconcat 'org-protect-slash
  8250. (append
  8251. (if (eq org-refile-use-outline-path 'file)
  8252. (list (file-name-nondirectory
  8253. (buffer-file-name (buffer-base-buffer))))
  8254. (if (eq org-refile-use-outline-path 'full-file-path)
  8255. (list (buffer-file-name (buffer-base-buffer)))))
  8256. (org-get-outline-path fast-path-p level txt)
  8257. (list txt))
  8258. "/")))
  8259. (push (list txt f re (point)) targets)))
  8260. (when (= (point) pos0)
  8261. ;; verification function has not moved point
  8262. (goto-char (point-at-eol))))))))))
  8263. (message "Getting targets...done")
  8264. (nreverse targets)))
  8265. (defun org-protect-slash (s)
  8266. (while (string-match "/" s)
  8267. (setq s (replace-match "\\" t t s)))
  8268. s)
  8269. (defvar org-olpa (make-vector 20 nil))
  8270. (defun org-get-outline-path (&optional fastp level heading)
  8271. "Return the outline path to the current entry, as a list.
  8272. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8273. routine which makes outline path derivations for an entire file,
  8274. avoiding backtracing."
  8275. (if fastp
  8276. (progn
  8277. (if (> level 19)
  8278. (error "Outline path failure, more than 19 levels."))
  8279. (loop for i from level upto 19 do
  8280. (aset org-olpa i nil))
  8281. (prog1
  8282. (delq nil (append org-olpa nil))
  8283. (aset org-olpa level heading)))
  8284. (let (rtn case-fold-search)
  8285. (save-excursion
  8286. (save-restriction
  8287. (widen)
  8288. (while (org-up-heading-safe)
  8289. (when (looking-at org-complex-heading-regexp)
  8290. (push (org-match-string-no-properties 4) rtn)))
  8291. rtn)))))
  8292. (defun org-format-outline-path (path &optional width prefix)
  8293. "Format the outlie path PATH for display.
  8294. Width is the maximum number of characters that is available.
  8295. Prefix is a prefix to be included in the returned string,
  8296. such as the file name."
  8297. (setq width (or width 79))
  8298. (if prefix (setq width (- width (length prefix))))
  8299. (if (not path)
  8300. (or prefix "")
  8301. (let* ((nsteps (length path))
  8302. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8303. (maxwidth (if (<= total-width width)
  8304. 10000 ;; everything fits
  8305. ;; we need to shorten the level headings
  8306. (/ (- width nsteps) nsteps)))
  8307. (org-odd-levels-only nil)
  8308. (n 0)
  8309. (total (1+ (length prefix))))
  8310. (setq maxwidth (max maxwidth 10))
  8311. (concat prefix
  8312. (mapconcat
  8313. (lambda (h)
  8314. (setq n (1+ n))
  8315. (if (and (= n nsteps) (< maxwidth 10000))
  8316. (setq maxwidth (- total-width total)))
  8317. (if (< (length h) maxwidth)
  8318. (progn (setq total (+ total (length h) 1)) h)
  8319. (setq h (substring h 0 (- maxwidth 2))
  8320. total (+ total maxwidth 1))
  8321. (if (string-match "[ \t]+\\'" h)
  8322. (setq h (substring h 0 (match-beginning 0))))
  8323. (setq h (concat h "..")))
  8324. (org-add-props h nil 'face
  8325. (nth (% (1- n) org-n-level-faces)
  8326. org-level-faces))
  8327. h)
  8328. path "/")))))
  8329. (defun org-display-outline-path (&optional file current)
  8330. "Display the current outline path in the echo area."
  8331. (interactive "P")
  8332. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8333. (case-fold-search nil)
  8334. (path (and (org-mode-p) (org-get-outline-path))))
  8335. (if current (setq path (append path
  8336. (save-excursion
  8337. (org-back-to-heading t)
  8338. (if (looking-at org-complex-heading-regexp)
  8339. (list (match-string 4)))))))
  8340. (message "%s"
  8341. (org-format-outline-path
  8342. path
  8343. (1- (frame-width))
  8344. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8345. (defvar org-refile-history nil
  8346. "History for refiling operations.")
  8347. (defvar org-after-refile-insert-hook nil
  8348. "Hook run after `org-refile' has inserted its stuff at the new location.
  8349. Note that this is still *before* the stuff will be removed from
  8350. the *old* location.")
  8351. (defun org-refile (&optional goto default-buffer rfloc)
  8352. "Move the entry at point to another heading.
  8353. The list of target headings is compiled using the information in
  8354. `org-refile-targets', which see. This list is created before each use
  8355. and will therefore always be up-to-date.
  8356. At the target location, the entry is filed as a subitem of the target heading.
  8357. Depending on `org-reverse-note-order', the new subitem will either be the
  8358. first or the last subitem.
  8359. If there is an active region, all entries in that region will be moved.
  8360. However, the region must fulfil the requirement that the first heading
  8361. is the first one sets the top-level of the moved text - at most siblings
  8362. below it are allowed.
  8363. With prefix arg GOTO, the command will only visit the target location,
  8364. not actually move anything.
  8365. With a double prefix `C-u C-u', go to the location where the last refiling
  8366. operation has put the subtree.
  8367. With a prefix argument of `2', refile to the running clock.
  8368. RFLOC can be a refile location obtained in a different way.
  8369. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8370. (interactive "P")
  8371. (let* ((cbuf (current-buffer))
  8372. (regionp (org-region-active-p))
  8373. (region-start (and regionp (region-beginning)))
  8374. (region-end (and regionp (region-end)))
  8375. (region-length (and regionp (- region-end region-start)))
  8376. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8377. pos it nbuf file re level reversed)
  8378. (setq last-command nil)
  8379. (when regionp
  8380. (goto-char region-start)
  8381. (or (bolp) (goto-char (point-at-bol)))
  8382. (setq region-start (point))
  8383. (unless (org-kill-is-subtree-p
  8384. (buffer-substring region-start region-end))
  8385. (error "The region is not a (sequence of) subtree(s)")))
  8386. (if (equal goto '(16))
  8387. (org-refile-goto-last-stored)
  8388. (when (or
  8389. (and (equal goto 2)
  8390. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8391. (prog1
  8392. (setq it (list (or org-clock-heading "running clock")
  8393. (buffer-file-name
  8394. (marker-buffer org-clock-hd-marker))
  8395. ""
  8396. (marker-position org-clock-hd-marker)))
  8397. (setq goto nil)))
  8398. (setq it (or rfloc
  8399. (save-excursion
  8400. (org-refile-get-location
  8401. (if goto "Goto: " "Refile to: ") default-buffer
  8402. org-refile-allow-creating-parent-nodes)))))
  8403. (setq file (nth 1 it)
  8404. re (nth 2 it)
  8405. pos (nth 3 it))
  8406. (if (and (not goto)
  8407. pos
  8408. (equal (buffer-file-name) file)
  8409. (if regionp
  8410. (and (>= pos region-start)
  8411. (<= pos region-end))
  8412. (and (>= pos (point))
  8413. (< pos (save-excursion
  8414. (org-end-of-subtree t t))))))
  8415. (error "Cannot refile to position inside the tree or region"))
  8416. (setq nbuf (or (find-buffer-visiting file)
  8417. (find-file-noselect file)))
  8418. (if goto
  8419. (progn
  8420. (switch-to-buffer nbuf)
  8421. (goto-char pos)
  8422. (org-show-context 'org-goto))
  8423. (if regionp
  8424. (progn
  8425. (org-kill-new (buffer-substring region-start region-end))
  8426. (org-save-markers-in-region region-start region-end))
  8427. (org-copy-subtree 1 nil t))
  8428. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8429. (find-file-noselect file)))
  8430. (setq reversed (org-notes-order-reversed-p))
  8431. (save-excursion
  8432. (save-restriction
  8433. (widen)
  8434. (if pos
  8435. (progn
  8436. (goto-char pos)
  8437. (looking-at outline-regexp)
  8438. (setq level (org-get-valid-level (funcall outline-level) 1))
  8439. (goto-char
  8440. (if reversed
  8441. (or (outline-next-heading) (point-max))
  8442. (or (save-excursion (org-get-next-sibling))
  8443. (org-end-of-subtree t t)
  8444. (point-max)))))
  8445. (setq level 1)
  8446. (if (not reversed)
  8447. (goto-char (point-max))
  8448. (goto-char (point-min))
  8449. (or (outline-next-heading) (goto-char (point-max)))))
  8450. (if (not (bolp)) (newline))
  8451. (bookmark-set "org-refile-last-stored")
  8452. (org-paste-subtree level)
  8453. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8454. (run-hooks 'org-after-refile-insert-hook))))
  8455. (if regionp
  8456. (delete-region (point) (+ (point) region-length))
  8457. (org-cut-subtree))
  8458. (when (featurep 'org-inlinetask)
  8459. (org-inlinetask-remove-END-maybe))
  8460. (setq org-markers-to-move nil)
  8461. (message "Refiled to \"%s\"" (car it))))))
  8462. (org-reveal))
  8463. (defun org-refile-goto-last-stored ()
  8464. "Go to the location where the last refile was stored."
  8465. (interactive)
  8466. (bookmark-jump "org-refile-last-stored")
  8467. (message "This is the location of the last refile"))
  8468. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8469. "Prompt the user for a refile location, using PROMPT."
  8470. (let ((org-refile-targets org-refile-targets)
  8471. (org-refile-use-outline-path org-refile-use-outline-path))
  8472. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8473. (unless org-refile-target-table
  8474. (error "No refile targets"))
  8475. (let* ((cbuf (current-buffer))
  8476. (partial-completion-mode nil)
  8477. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8478. (cfunc (if (and org-refile-use-outline-path
  8479. org-outline-path-complete-in-steps)
  8480. 'org-olpath-completing-read
  8481. 'org-icompleting-read))
  8482. (extra (if org-refile-use-outline-path "/" ""))
  8483. (filename (and cfn (expand-file-name cfn)))
  8484. (tbl (mapcar
  8485. (lambda (x)
  8486. (if (and (not (member org-refile-use-outline-path
  8487. '(file full-file-path)))
  8488. (not (equal filename (nth 1 x))))
  8489. (cons (concat (car x) extra " ("
  8490. (file-name-nondirectory (nth 1 x)) ")")
  8491. (cdr x))
  8492. (cons (concat (car x) extra) (cdr x))))
  8493. org-refile-target-table))
  8494. (completion-ignore-case t)
  8495. pa answ parent-target child parent old-hist)
  8496. (setq old-hist org-refile-history)
  8497. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8498. nil 'org-refile-history))
  8499. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8500. (if pa
  8501. (progn
  8502. (when (or (not org-refile-history)
  8503. (not (eq old-hist org-refile-history))
  8504. (not (equal (car pa) (car org-refile-history))))
  8505. (setq org-refile-history
  8506. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8507. org-refile-history
  8508. (cdr org-refile-history))))
  8509. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8510. (pop org-refile-history)))
  8511. pa)
  8512. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8513. (setq parent (match-string 1 answ)
  8514. child (match-string 2 answ))
  8515. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8516. (when (and parent-target
  8517. (or (eq new-nodes t)
  8518. (and (eq new-nodes 'confirm)
  8519. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8520. (org-refile-new-child parent-target child))))))
  8521. (defun org-refile-new-child (parent-target child)
  8522. "Use refile target PARENT-TARGET to add new CHILD below it."
  8523. (unless parent-target
  8524. (error "Cannot find parent for new node"))
  8525. (let ((file (nth 1 parent-target))
  8526. (pos (nth 3 parent-target))
  8527. level)
  8528. (with-current-buffer (or (find-buffer-visiting file)
  8529. (find-file-noselect file))
  8530. (save-excursion
  8531. (save-restriction
  8532. (widen)
  8533. (if pos
  8534. (goto-char pos)
  8535. (goto-char (point-max))
  8536. (if (not (bolp)) (newline)))
  8537. (when (looking-at outline-regexp)
  8538. (setq level (funcall outline-level))
  8539. (org-end-of-subtree t t))
  8540. (org-back-over-empty-lines)
  8541. (insert "\n" (make-string
  8542. (if pos (org-get-valid-level level 1) 1) ?*)
  8543. " " child "\n")
  8544. (beginning-of-line 0)
  8545. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8546. (defun org-olpath-completing-read (prompt collection &rest args)
  8547. "Read an outline path like a file name."
  8548. (let ((thetable collection)
  8549. (org-completion-use-ido nil) ; does not work with ido.
  8550. (org-completion-use-iswitchb nil)) ; or iswitchb
  8551. (apply
  8552. 'org-icompleting-read prompt
  8553. (lambda (string predicate &optional flag)
  8554. (let (rtn r f (l (length string)))
  8555. (cond
  8556. ((eq flag nil)
  8557. ;; try completion
  8558. (try-completion string thetable))
  8559. ((eq flag t)
  8560. ;; all-completions
  8561. (setq rtn (all-completions string thetable predicate))
  8562. (mapcar
  8563. (lambda (x)
  8564. (setq r (substring x l))
  8565. (if (string-match " ([^)]*)$" x)
  8566. (setq f (match-string 0 x))
  8567. (setq f ""))
  8568. (if (string-match "/" r)
  8569. (concat string (substring r 0 (match-end 0)) f)
  8570. x))
  8571. rtn))
  8572. ((eq flag 'lambda)
  8573. ;; exact match?
  8574. (assoc string thetable)))
  8575. ))
  8576. args)))
  8577. ;;;; Dynamic blocks
  8578. (defun org-find-dblock (name)
  8579. "Find the first dynamic block with name NAME in the buffer.
  8580. If not found, stay at current position and return nil."
  8581. (let (pos)
  8582. (save-excursion
  8583. (goto-char (point-min))
  8584. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8585. nil t)
  8586. (match-beginning 0))))
  8587. (if pos (goto-char pos))
  8588. pos))
  8589. (defconst org-dblock-start-re
  8590. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8591. "Matches the start line of a dynamic block, with parameters.")
  8592. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8593. "Matches the end of a dynamic block.")
  8594. (defun org-create-dblock (plist)
  8595. "Create a dynamic block section, with parameters taken from PLIST.
  8596. PLIST must contain a :name entry which is used as name of the block."
  8597. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8598. (end-of-line 1)
  8599. (newline))
  8600. (let ((col (current-column))
  8601. (name (plist-get plist :name)))
  8602. (insert "#+BEGIN: " name)
  8603. (while plist
  8604. (if (eq (car plist) :name)
  8605. (setq plist (cddr plist))
  8606. (insert " " (prin1-to-string (pop plist)))))
  8607. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8608. (beginning-of-line -2)))
  8609. (defun org-prepare-dblock ()
  8610. "Prepare dynamic block for refresh.
  8611. This empties the block, puts the cursor at the insert position and returns
  8612. the property list including an extra property :name with the block name."
  8613. (unless (looking-at org-dblock-start-re)
  8614. (error "Not at a dynamic block"))
  8615. (let* ((begdel (1+ (match-end 0)))
  8616. (name (org-no-properties (match-string 1)))
  8617. (params (append (list :name name)
  8618. (read (concat "(" (match-string 3) ")")))))
  8619. (save-excursion
  8620. (beginning-of-line 1)
  8621. (skip-chars-forward " \t")
  8622. (setq params (plist-put params :indentation-column (current-column))))
  8623. (unless (re-search-forward org-dblock-end-re nil t)
  8624. (error "Dynamic block not terminated"))
  8625. (setq params
  8626. (append params
  8627. (list :content (buffer-substring
  8628. begdel (match-beginning 0)))))
  8629. (delete-region begdel (match-beginning 0))
  8630. (goto-char begdel)
  8631. (open-line 1)
  8632. params))
  8633. (defun org-map-dblocks (&optional command)
  8634. "Apply COMMAND to all dynamic blocks in the current buffer.
  8635. If COMMAND is not given, use `org-update-dblock'."
  8636. (let ((cmd (or command 'org-update-dblock))
  8637. pos)
  8638. (save-excursion
  8639. (goto-char (point-min))
  8640. (while (re-search-forward org-dblock-start-re nil t)
  8641. (goto-char (setq pos (match-beginning 0)))
  8642. (condition-case nil
  8643. (funcall cmd)
  8644. (error (message "Error during update of dynamic block")))
  8645. (goto-char pos)
  8646. (unless (re-search-forward org-dblock-end-re nil t)
  8647. (error "Dynamic block not terminated"))))))
  8648. (defun org-dblock-update (&optional arg)
  8649. "User command for updating dynamic blocks.
  8650. Update the dynamic block at point. With prefix ARG, update all dynamic
  8651. blocks in the buffer."
  8652. (interactive "P")
  8653. (if arg
  8654. (org-update-all-dblocks)
  8655. (or (looking-at org-dblock-start-re)
  8656. (org-beginning-of-dblock))
  8657. (org-update-dblock)))
  8658. (defun org-update-dblock ()
  8659. "Update the dynamic block at point
  8660. This means to empty the block, parse for parameters and then call
  8661. the correct writing function."
  8662. (save-window-excursion
  8663. (let* ((pos (point))
  8664. (line (org-current-line))
  8665. (params (org-prepare-dblock))
  8666. (name (plist-get params :name))
  8667. (indent (plist-get params :indentation-column))
  8668. (cmd (intern (concat "org-dblock-write:" name))))
  8669. (message "Updating dynamic block `%s' at line %d..." name line)
  8670. (funcall cmd params)
  8671. (message "Updating dynamic block `%s' at line %d...done" name line)
  8672. (goto-char pos)
  8673. (when (and indent (> indent 0))
  8674. (setq indent (make-string indent ?\ ))
  8675. (save-excursion
  8676. (org-beginning-of-dblock)
  8677. (forward-line 1)
  8678. (while (not (looking-at org-dblock-end-re))
  8679. (insert indent)
  8680. (beginning-of-line 2))
  8681. (when (looking-at org-dblock-end-re)
  8682. (and (looking-at "[ \t]+")
  8683. (replace-match ""))
  8684. (insert indent)))))))
  8685. (defun org-beginning-of-dblock ()
  8686. "Find the beginning of the dynamic block at point.
  8687. Error if there is no such block at point."
  8688. (let ((pos (point))
  8689. beg)
  8690. (end-of-line 1)
  8691. (if (and (re-search-backward org-dblock-start-re nil t)
  8692. (setq beg (match-beginning 0))
  8693. (re-search-forward org-dblock-end-re nil t)
  8694. (> (match-end 0) pos))
  8695. (goto-char beg)
  8696. (goto-char pos)
  8697. (error "Not in a dynamic block"))))
  8698. (defun org-update-all-dblocks ()
  8699. "Update all dynamic blocks in the buffer.
  8700. This function can be used in a hook."
  8701. (when (org-mode-p)
  8702. (org-map-dblocks 'org-update-dblock)))
  8703. ;;;; Completion
  8704. (defconst org-additional-option-like-keywords
  8705. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8706. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8707. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8708. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8709. "BEGIN:" "END:"
  8710. "ORGTBL" "TBLFM:" "TBLNAME:"
  8711. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8712. "BEGIN_QUOTE" "END_QUOTE"
  8713. "BEGIN_VERSE" "END_VERSE"
  8714. "BEGIN_CENTER" "END_CENTER"
  8715. "BEGIN_SRC" "END_SRC"
  8716. "CATEGORY" "COLUMNS"
  8717. "CAPTION" "LABEL"
  8718. "SETUPFILE"
  8719. "BIND"
  8720. "MACRO"))
  8721. (defcustom org-structure-template-alist
  8722. '(
  8723. ("s" "#+begin_src ?\n\n#+end_src"
  8724. "<src lang=\"?\">\n\n</src>")
  8725. ("e" "#+begin_example\n?\n#+end_example"
  8726. "<example>\n?\n</example>")
  8727. ("q" "#+begin_quote\n?\n#+end_quote"
  8728. "<quote>\n?\n</quote>")
  8729. ("v" "#+begin_verse\n?\n#+end_verse"
  8730. "<verse>\n?\n/verse>")
  8731. ("c" "#+begin_center\n?\n#+end_center"
  8732. "<center>\n?\n/center>")
  8733. ("l" "#+begin_latex\n?\n#+end_latex"
  8734. "<literal style=\"latex\">\n?\n</literal>")
  8735. ("L" "#+latex: "
  8736. "<literal style=\"latex\">?</literal>")
  8737. ("h" "#+begin_html\n?\n#+end_html"
  8738. "<literal style=\"html\">\n?\n</literal>")
  8739. ("H" "#+html: "
  8740. "<literal style=\"html\">?</literal>")
  8741. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8742. ("A" "#+ascii: ")
  8743. ("i" "#+include %file ?"
  8744. "<include file=%file markup=\"?\">")
  8745. )
  8746. "Structure completion elements.
  8747. This is a list of abbreviation keys and values. The value gets inserted
  8748. it you type @samp{.} followed by the key and then the completion key,
  8749. usually `M-TAB'. %file will be replaced by a file name after prompting
  8750. for the file using completion.
  8751. There are two templates for each key, the first uses the original Org syntax,
  8752. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8753. the default when the /org-mtags.el/ module has been loaded. See also the
  8754. variable `org-mtags-prefer-muse-templates'.
  8755. This is an experimental feature, it is undecided if it is going to stay in."
  8756. :group 'org-completion
  8757. :type '(repeat
  8758. (string :tag "Key")
  8759. (string :tag "Template")
  8760. (string :tag "Muse Template")))
  8761. (defun org-try-structure-completion ()
  8762. "Try to complete a structure template before point.
  8763. This looks for strings like \"<e\" on an otherwise empty line and
  8764. expands them."
  8765. (let ((l (buffer-substring (point-at-bol) (point)))
  8766. a)
  8767. (when (and (looking-at "[ \t]*$")
  8768. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8769. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8770. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8771. (match-beginning 1)) a)
  8772. t)))
  8773. (defun org-complete-expand-structure-template (start cell)
  8774. "Expand a structure template."
  8775. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8776. (rpl (nth (if musep 2 1) cell))
  8777. (ind ""))
  8778. (delete-region start (point))
  8779. (when (string-match "\\`#\\+" rpl)
  8780. (cond
  8781. ((bolp))
  8782. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8783. (setq ind (buffer-substring (point-at-bol) (point))))
  8784. (t (newline))))
  8785. (setq start (point))
  8786. (if (string-match "%file" rpl)
  8787. (setq rpl (replace-match
  8788. (concat
  8789. "\""
  8790. (save-match-data
  8791. (abbreviate-file-name (read-file-name "Include file: ")))
  8792. "\"")
  8793. t t rpl)))
  8794. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8795. (concat "\n" ind)))
  8796. (insert rpl)
  8797. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8798. (defun org-complete (&optional arg)
  8799. "Perform completion on word at point.
  8800. At the beginning of a headline, this completes TODO keywords as given in
  8801. `org-todo-keywords'.
  8802. If the current word is preceded by a backslash, completes the TeX symbols
  8803. that are supported for HTML support.
  8804. If the current word is preceded by \"#+\", completes special words for
  8805. setting file options.
  8806. In the line after \"#+STARTUP:, complete valid keywords.\"
  8807. At all other locations, this simply calls the value of
  8808. `org-completion-fallback-command'."
  8809. (interactive "P")
  8810. (org-without-partial-completion
  8811. (catch 'exit
  8812. (let* ((a nil)
  8813. (end (point))
  8814. (beg1 (save-excursion
  8815. (skip-chars-backward (org-re "[:alnum:]_@"))
  8816. (point)))
  8817. (beg (save-excursion
  8818. (skip-chars-backward "a-zA-Z0-9_:$")
  8819. (point)))
  8820. (confirm (lambda (x) (stringp (car x))))
  8821. (searchhead (equal (char-before beg) ?*))
  8822. (struct
  8823. (when (and (member (char-before beg1) '(?. ?<))
  8824. (setq a (assoc (buffer-substring beg1 (point))
  8825. org-structure-template-alist)))
  8826. (org-complete-expand-structure-template (1- beg1) a)
  8827. (throw 'exit t)))
  8828. (tag (and (equal (char-before beg1) ?:)
  8829. (equal (char-after (point-at-bol)) ?*)))
  8830. (prop (and (equal (char-before beg1) ?:)
  8831. (not (equal (char-after (point-at-bol)) ?*))))
  8832. (texp (equal (char-before beg) ?\\))
  8833. (link (equal (char-before beg) ?\[))
  8834. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8835. beg)
  8836. "#+"))
  8837. (startup (string-match "^#\\+STARTUP:.*"
  8838. (buffer-substring (point-at-bol) (point))))
  8839. (completion-ignore-case opt)
  8840. (type nil)
  8841. (tbl nil)
  8842. (table (cond
  8843. (opt
  8844. (setq type :opt)
  8845. (require 'org-exp)
  8846. (append
  8847. (delq nil
  8848. (mapcar
  8849. (lambda (x)
  8850. (if (string-match
  8851. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8852. (cons (match-string 2 x)
  8853. (match-string 1 x))))
  8854. (org-split-string (org-get-current-options) "\n")))
  8855. (mapcar 'list org-additional-option-like-keywords)))
  8856. (startup
  8857. (setq type :startup)
  8858. org-startup-options)
  8859. (link (append org-link-abbrev-alist-local
  8860. org-link-abbrev-alist))
  8861. (texp
  8862. (setq type :tex)
  8863. org-html-entities)
  8864. ((string-match "\\`\\*+[ \t]+\\'"
  8865. (buffer-substring (point-at-bol) beg))
  8866. (setq type :todo)
  8867. (mapcar 'list org-todo-keywords-1))
  8868. (searchhead
  8869. (setq type :searchhead)
  8870. (save-excursion
  8871. (goto-char (point-min))
  8872. (while (re-search-forward org-todo-line-regexp nil t)
  8873. (push (list
  8874. (org-make-org-heading-search-string
  8875. (match-string 3) t))
  8876. tbl)))
  8877. tbl)
  8878. (tag (setq type :tag beg beg1)
  8879. (or org-tag-alist (org-get-buffer-tags)))
  8880. (prop (setq type :prop beg beg1)
  8881. (mapcar 'list (org-buffer-property-keys nil t t)))
  8882. (t (progn
  8883. (call-interactively org-completion-fallback-command)
  8884. (throw 'exit nil)))))
  8885. (pattern (buffer-substring-no-properties beg end))
  8886. (completion (try-completion pattern table confirm)))
  8887. (cond ((eq completion t)
  8888. (if (not (assoc (upcase pattern) table))
  8889. (message "Already complete")
  8890. (if (and (equal type :opt)
  8891. (not (member (car (assoc (upcase pattern) table))
  8892. org-additional-option-like-keywords)))
  8893. (insert (substring (cdr (assoc (upcase pattern) table))
  8894. (length pattern)))
  8895. (if (memq type '(:tag :prop)) (insert ":")))))
  8896. ((null completion)
  8897. (message "Can't find completion for \"%s\"" pattern)
  8898. (ding))
  8899. ((not (string= pattern completion))
  8900. (delete-region beg end)
  8901. (if (string-match " +$" completion)
  8902. (setq completion (replace-match "" t t completion)))
  8903. (insert completion)
  8904. (if (get-buffer-window "*Completions*")
  8905. (delete-window (get-buffer-window "*Completions*")))
  8906. (if (assoc completion table)
  8907. (if (eq type :todo) (insert " ")
  8908. (if (memq type '(:tag :prop)) (insert ":"))))
  8909. (if (and (equal type :opt) (assoc completion table))
  8910. (message "%s" (substitute-command-keys
  8911. "Press \\[org-complete] again to insert example settings"))))
  8912. (t
  8913. (message "Making completion list...")
  8914. (let ((list (sort (all-completions pattern table confirm)
  8915. 'string<)))
  8916. (with-output-to-temp-buffer "*Completions*"
  8917. (condition-case nil
  8918. ;; Protection needed for XEmacs and emacs 21
  8919. (display-completion-list list pattern)
  8920. (error (display-completion-list list)))))
  8921. (message "Making completion list...%s" "done")))))))
  8922. ;;;; TODO, DEADLINE, Comments
  8923. (defun org-toggle-comment ()
  8924. "Change the COMMENT state of an entry."
  8925. (interactive)
  8926. (save-excursion
  8927. (org-back-to-heading)
  8928. (let (case-fold-search)
  8929. (if (looking-at (concat outline-regexp
  8930. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8931. (replace-match "" t t nil 1)
  8932. (if (looking-at outline-regexp)
  8933. (progn
  8934. (goto-char (match-end 0))
  8935. (insert org-comment-string " ")))))))
  8936. (defvar org-last-todo-state-is-todo nil
  8937. "This is non-nil when the last TODO state change led to a TODO state.
  8938. If the last change removed the TODO tag or switched to DONE, then
  8939. this is nil.")
  8940. (defvar org-setting-tags nil) ; dynamically skipped
  8941. (defun org-parse-local-options (string var)
  8942. "Parse STRING for startup setting relevant for variable VAR."
  8943. (let ((rtn (symbol-value var))
  8944. e opts)
  8945. (save-match-data
  8946. (if (or (not string) (not (string-match "\\S-" string)))
  8947. rtn
  8948. (setq opts (delq nil (mapcar (lambda (x)
  8949. (setq e (assoc x org-startup-options))
  8950. (if (eq (nth 1 e) var) e nil))
  8951. (org-split-string string "[ \t]+"))))
  8952. (if (not opts)
  8953. rtn
  8954. (setq rtn nil)
  8955. (while (setq e (pop opts))
  8956. (if (not (nth 3 e))
  8957. (setq rtn (nth 2 e))
  8958. (if (not (listp rtn)) (setq rtn nil))
  8959. (push (nth 2 e) rtn)))
  8960. rtn)))))
  8961. (defvar org-todo-setup-filter-hook nil
  8962. "Hook for functions that pre-filter todo specs.
  8963. Each function takes a todo spec and returns either `nil' or the spec
  8964. transformed into canonical form." )
  8965. (defvar org-todo-get-default-hook nil
  8966. "Hook for functions that get a default item for todo.
  8967. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8968. `nil' or a string to be used for the todo mark." )
  8969. (defvar org-agenda-headline-snapshot-before-repeat)
  8970. (defun org-todo (&optional arg)
  8971. "Change the TODO state of an item.
  8972. The state of an item is given by a keyword at the start of the heading,
  8973. like
  8974. *** TODO Write paper
  8975. *** DONE Call mom
  8976. The different keywords are specified in the variable `org-todo-keywords'.
  8977. By default the available states are \"TODO\" and \"DONE\".
  8978. So for this example: when the item starts with TODO, it is changed to DONE.
  8979. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8980. DONE are present, add TODO at the beginning of the heading.
  8981. With C-u prefix arg, use completion to determine the new state.
  8982. With numeric prefix arg, switch to that state.
  8983. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8984. With a triple C-u prefix, circumvent any state blocking.
  8985. For calling through lisp, arg is also interpreted in the following way:
  8986. 'none -> empty state
  8987. \"\"(empty string) -> switch to empty state
  8988. 'done -> switch to DONE
  8989. 'nextset -> switch to the next set of keywords
  8990. 'previousset -> switch to the previous set of keywords
  8991. \"WAITING\" -> switch to the specified keyword, but only if it
  8992. really is a member of `org-todo-keywords'."
  8993. (interactive "P")
  8994. (if (equal arg '(16)) (setq arg 'nextset))
  8995. (let ((org-blocker-hook org-blocker-hook)
  8996. (case-fold-search nil))
  8997. (when (equal arg '(64))
  8998. (setq arg nil org-blocker-hook nil))
  8999. (when (and org-blocker-hook
  9000. (or org-inhibit-blocking
  9001. (org-entry-get nil "NOBLOCKING")))
  9002. (setq org-blocker-hook nil))
  9003. (save-excursion
  9004. (catch 'exit
  9005. (org-back-to-heading t)
  9006. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9007. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9008. (looking-at " *"))
  9009. (let* ((match-data (match-data))
  9010. (startpos (point-at-bol))
  9011. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9012. (org-log-done org-log-done)
  9013. (org-log-repeat org-log-repeat)
  9014. (org-todo-log-states org-todo-log-states)
  9015. (this (match-string 1))
  9016. (hl-pos (match-beginning 0))
  9017. (head (org-get-todo-sequence-head this))
  9018. (ass (assoc head org-todo-kwd-alist))
  9019. (interpret (nth 1 ass))
  9020. (done-word (nth 3 ass))
  9021. (final-done-word (nth 4 ass))
  9022. (last-state (or this ""))
  9023. (completion-ignore-case t)
  9024. (member (member this org-todo-keywords-1))
  9025. (tail (cdr member))
  9026. (state (cond
  9027. ((and org-todo-key-trigger
  9028. (or (and (equal arg '(4))
  9029. (eq org-use-fast-todo-selection 'prefix))
  9030. (and (not arg) org-use-fast-todo-selection
  9031. (not (eq org-use-fast-todo-selection
  9032. 'prefix)))))
  9033. ;; Use fast selection
  9034. (org-fast-todo-selection))
  9035. ((and (equal arg '(4))
  9036. (or (not org-use-fast-todo-selection)
  9037. (not org-todo-key-trigger)))
  9038. ;; Read a state with completion
  9039. (org-icompleting-read
  9040. "State: " (mapcar (lambda(x) (list x))
  9041. org-todo-keywords-1)
  9042. nil t))
  9043. ((eq arg 'right)
  9044. (if this
  9045. (if tail (car tail) nil)
  9046. (car org-todo-keywords-1)))
  9047. ((eq arg 'left)
  9048. (if (equal member org-todo-keywords-1)
  9049. nil
  9050. (if this
  9051. (nth (- (length org-todo-keywords-1)
  9052. (length tail) 2)
  9053. org-todo-keywords-1)
  9054. (org-last org-todo-keywords-1))))
  9055. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9056. (setq arg nil))) ; hack to fall back to cycling
  9057. (arg
  9058. ;; user or caller requests a specific state
  9059. (cond
  9060. ((equal arg "") nil)
  9061. ((eq arg 'none) nil)
  9062. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9063. ((eq arg 'nextset)
  9064. (or (car (cdr (member head org-todo-heads)))
  9065. (car org-todo-heads)))
  9066. ((eq arg 'previousset)
  9067. (let ((org-todo-heads (reverse org-todo-heads)))
  9068. (or (car (cdr (member head org-todo-heads)))
  9069. (car org-todo-heads))))
  9070. ((car (member arg org-todo-keywords-1)))
  9071. ((stringp arg)
  9072. (error "State `%s' not valid in this file" arg))
  9073. ((nth (1- (prefix-numeric-value arg))
  9074. org-todo-keywords-1))))
  9075. ((null member) (or head (car org-todo-keywords-1)))
  9076. ((equal this final-done-word) nil) ;; -> make empty
  9077. ((null tail) nil) ;; -> first entry
  9078. ((memq interpret '(type priority))
  9079. (if (eq this-command last-command)
  9080. (car tail)
  9081. (if (> (length tail) 0)
  9082. (or done-word (car org-done-keywords))
  9083. nil)))
  9084. (t
  9085. (car tail))))
  9086. (state (or
  9087. (run-hook-with-args-until-success
  9088. 'org-todo-get-default-hook state last-state)
  9089. state))
  9090. (next (if state (concat " " state " ") " "))
  9091. (change-plist (list :type 'todo-state-change :from this :to state
  9092. :position startpos))
  9093. dolog now-done-p)
  9094. (when org-blocker-hook
  9095. (setq org-last-todo-state-is-todo
  9096. (not (member this org-done-keywords)))
  9097. (unless (save-excursion
  9098. (save-match-data
  9099. (run-hook-with-args-until-failure
  9100. 'org-blocker-hook change-plist)))
  9101. (if (interactive-p)
  9102. (error "TODO state change from %s to %s blocked" this state)
  9103. ;; fail silently
  9104. (message "TODO state change from %s to %s blocked" this state)
  9105. (throw 'exit nil))))
  9106. (store-match-data match-data)
  9107. (replace-match next t t)
  9108. (unless (pos-visible-in-window-p hl-pos)
  9109. (message "TODO state changed to %s" (org-trim next)))
  9110. (unless head
  9111. (setq head (org-get-todo-sequence-head state)
  9112. ass (assoc head org-todo-kwd-alist)
  9113. interpret (nth 1 ass)
  9114. done-word (nth 3 ass)
  9115. final-done-word (nth 4 ass)))
  9116. (when (memq arg '(nextset previousset))
  9117. (message "Keyword-Set %d/%d: %s"
  9118. (- (length org-todo-sets) -1
  9119. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9120. (length org-todo-sets)
  9121. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9122. (setq org-last-todo-state-is-todo
  9123. (not (member state org-done-keywords)))
  9124. (setq now-done-p (and (member state org-done-keywords)
  9125. (not (member this org-done-keywords))))
  9126. (and logging (org-local-logging logging))
  9127. (when (and (or org-todo-log-states org-log-done)
  9128. (not (eq org-inhibit-logging t))
  9129. (not (memq arg '(nextset previousset))))
  9130. ;; we need to look at recording a time and note
  9131. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9132. (nth 2 (assoc this org-todo-log-states))))
  9133. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9134. (setq dolog 'time))
  9135. (when (and state
  9136. (member state org-not-done-keywords)
  9137. (not (member this org-not-done-keywords)))
  9138. ;; This is now a todo state and was not one before
  9139. ;; If there was a CLOSED time stamp, get rid of it.
  9140. (org-add-planning-info nil nil 'closed))
  9141. (when (and now-done-p org-log-done)
  9142. ;; It is now done, and it was not done before
  9143. (org-add-planning-info 'closed (org-current-time))
  9144. (if (and (not dolog) (eq 'note org-log-done))
  9145. (org-add-log-setup 'done state this 'findpos 'note)))
  9146. (when (and state dolog)
  9147. ;; This is a non-nil state, and we need to log it
  9148. (org-add-log-setup 'state state this 'findpos dolog)))
  9149. ;; Fixup tag positioning
  9150. (org-todo-trigger-tag-changes state)
  9151. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9152. (when org-provide-todo-statistics
  9153. (org-update-parent-todo-statistics))
  9154. (run-hooks 'org-after-todo-state-change-hook)
  9155. (if (and arg (not (member state org-done-keywords)))
  9156. (setq head (org-get-todo-sequence-head state)))
  9157. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9158. ;; Do we need to trigger a repeat?
  9159. (when now-done-p
  9160. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9161. ;; This is for the agenda, take a snapshot of the headline.
  9162. (save-match-data
  9163. (setq org-agenda-headline-snapshot-before-repeat
  9164. (org-get-heading))))
  9165. (org-auto-repeat-maybe state))
  9166. ;; Fixup cursor location if close to the keyword
  9167. (if (and (outline-on-heading-p)
  9168. (not (bolp))
  9169. (save-excursion (beginning-of-line 1)
  9170. (looking-at org-todo-line-regexp))
  9171. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9172. (progn
  9173. (goto-char (or (match-end 2) (match-end 1)))
  9174. (and (looking-at " ") (just-one-space))))
  9175. (when org-trigger-hook
  9176. (save-excursion
  9177. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9178. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9179. "Block turning an entry into a TODO, using the hierarchy.
  9180. This checks whether the current task should be blocked from state
  9181. changes. Such blocking occurs when:
  9182. 1. The task has children which are not all in a completed state.
  9183. 2. A task has a parent with the property :ORDERED:, and there
  9184. are siblings prior to the current task with incomplete
  9185. status.
  9186. 3. The parent of the task is blocked because it has siblings that should
  9187. be done first, or is child of a block grandparent TODO entry."
  9188. (catch 'dont-block
  9189. ;; If this is not a todo state change, or if this entry is already DONE,
  9190. ;; do not block
  9191. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9192. (member (plist-get change-plist :from)
  9193. (cons 'done org-done-keywords))
  9194. (member (plist-get change-plist :to)
  9195. (cons 'todo org-not-done-keywords))
  9196. (not (plist-get change-plist :to)))
  9197. (throw 'dont-block t))
  9198. ;; If this task has children, and any are undone, it's blocked
  9199. (save-excursion
  9200. (org-back-to-heading t)
  9201. (let ((this-level (funcall outline-level)))
  9202. (outline-next-heading)
  9203. (let ((child-level (funcall outline-level)))
  9204. (while (and (not (eobp))
  9205. (> child-level this-level))
  9206. ;; this todo has children, check whether they are all
  9207. ;; completed
  9208. (if (and (not (org-entry-is-done-p))
  9209. (org-entry-is-todo-p))
  9210. (throw 'dont-block nil))
  9211. (outline-next-heading)
  9212. (setq child-level (funcall outline-level))))))
  9213. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9214. ;; any previous siblings are undone, it's blocked
  9215. (save-excursion
  9216. (org-back-to-heading t)
  9217. (let* ((pos (point))
  9218. (parent-pos (and (org-up-heading-safe) (point))))
  9219. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9220. (when (and (org-entry-get (point) "ORDERED")
  9221. (forward-line 1)
  9222. (re-search-forward org-not-done-heading-regexp pos t))
  9223. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9224. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9225. (while t
  9226. (goto-char parent-pos)
  9227. (if (not (looking-at org-not-done-heading-regexp))
  9228. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9229. (setq pos (point))
  9230. (setq parent-pos (and (org-up-heading-safe) (point)))
  9231. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9232. (when (and (org-entry-get (point) "ORDERED")
  9233. (forward-line 1)
  9234. (re-search-forward org-not-done-heading-regexp pos t))
  9235. (throw 'dont-block nil))))))) ; block, older sibling not done.
  9236. (defcustom org-track-ordered-property-with-tag nil
  9237. "Should the ORDERED property also be shown as a tag?
  9238. The ORDERED property decides if an entry should require subtasks to be
  9239. completed in sequence. Since a property is not very visible, setting
  9240. this option means that toggling the ORDERED property with the command
  9241. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9242. not relevant for the behavior, but it makes things more visible.
  9243. Note that toggling the tag with tags commands will not change the property
  9244. and therefore not influence behavior!
  9245. This can be t, meaning the tag ORDERED should be used, It can also be a
  9246. string to select a different tag for this task."
  9247. :group 'org-todo
  9248. :type '(choice
  9249. (const :tag "No tracking" nil)
  9250. (const :tag "Track with ORDERED tag" t)
  9251. (string :tag "Use other tag")))
  9252. (defun org-toggle-ordered-property ()
  9253. "Toggle the ORDERED property of the current entry.
  9254. For better visibility, you can track the value of this property with a tag.
  9255. See variable `org-track-ordered-property-with-tag'."
  9256. (interactive)
  9257. (let* ((t1 org-track-ordered-property-with-tag)
  9258. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9259. (save-excursion
  9260. (org-back-to-heading)
  9261. (if (org-entry-get nil "ORDERED")
  9262. (progn
  9263. (org-delete-property "ORDERED")
  9264. (and tag (org-toggle-tag tag 'off))
  9265. (message "Subtasks can be completed in arbitrary order"))
  9266. (org-entry-put nil "ORDERED" "t")
  9267. (and tag (org-toggle-tag tag 'on))
  9268. (message "Subtasks must be completed in sequence")))))
  9269. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9270. (defun org-block-todo-from-checkboxes (change-plist)
  9271. "Block turning an entry into a TODO, using checkboxes.
  9272. This checks whether the current task should be blocked from state
  9273. changes because there are unchecked boxes in this entry."
  9274. (catch 'dont-block
  9275. ;; If this is not a todo state change, or if this entry is already DONE,
  9276. ;; do not block
  9277. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9278. (member (plist-get change-plist :from)
  9279. (cons 'done org-done-keywords))
  9280. (member (plist-get change-plist :to)
  9281. (cons 'todo org-not-done-keywords))
  9282. (not (plist-get change-plist :to)))
  9283. (throw 'dont-block t))
  9284. ;; If this task has checkboxes that are not checked, it's blocked
  9285. (save-excursion
  9286. (org-back-to-heading t)
  9287. (let ((beg (point)) end)
  9288. (outline-next-heading)
  9289. (setq end (point))
  9290. (goto-char beg)
  9291. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9292. end t)
  9293. (progn
  9294. (if (boundp 'org-blocked-by-checkboxes)
  9295. (setq org-blocked-by-checkboxes t))
  9296. (throw 'dont-block nil)))))
  9297. t)) ; do not block
  9298. (defun org-entry-blocked-p ()
  9299. "Is the current entry blocked?"
  9300. (if (org-entry-get nil "NOBLOCKING")
  9301. nil ;; Never block this entry
  9302. (not
  9303. (run-hook-with-args-until-failure
  9304. 'org-blocker-hook
  9305. (list :type 'todo-state-change
  9306. :position (point)
  9307. :from 'todo
  9308. :to 'done)))))
  9309. (defun org-update-statistics-cookies (all)
  9310. "Update the statistics cookie, either from TODO or from checkboxes.
  9311. This should be called with the cursor in a line with a statistics cookie."
  9312. (interactive "P")
  9313. (if all
  9314. (progn
  9315. (org-update-checkbox-count 'all)
  9316. (org-map-entries 'org-update-parent-todo-statistics))
  9317. (if (not (org-on-heading-p))
  9318. (org-update-checkbox-count)
  9319. (let ((pos (move-marker (make-marker) (point)))
  9320. end l1 l2)
  9321. (ignore-errors (org-back-to-heading t))
  9322. (if (not (org-on-heading-p))
  9323. (org-update-checkbox-count)
  9324. (setq l1 (org-outline-level))
  9325. (setq end (save-excursion
  9326. (outline-next-heading)
  9327. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9328. (point)))
  9329. (if (and (save-excursion
  9330. (re-search-forward
  9331. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9332. (not (save-excursion (re-search-forward
  9333. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9334. (org-update-checkbox-count)
  9335. (if (and l2 (> l2 l1))
  9336. (progn
  9337. (goto-char end)
  9338. (org-update-parent-todo-statistics))
  9339. (goto-char pos)
  9340. (beginning-of-line 1)
  9341. (while (re-search-forward
  9342. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9343. (point-at-eol) t)
  9344. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9345. (goto-char pos)
  9346. (move-marker pos nil)))))
  9347. (defvar org-entry-property-inherited-from) ;; defined below
  9348. (defun org-update-parent-todo-statistics ()
  9349. "Update any statistics cookie in the parent of the current headline.
  9350. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9351. the entire subtree and this will travel up the hierarchy and update
  9352. statistics everywhere."
  9353. (interactive)
  9354. (let* ((lim 0) prop
  9355. (recursive (or (not org-hierarchical-todo-statistics)
  9356. (string-match
  9357. "\\<recursive\\>"
  9358. (or (setq prop (org-entry-get
  9359. nil "COOKIE_DATA" 'inherit)) ""))))
  9360. (lim (or (and prop (marker-position
  9361. org-entry-property-inherited-from))
  9362. lim))
  9363. (first t)
  9364. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9365. level ltoggle l1 new ndel
  9366. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9367. (catch 'exit
  9368. (save-excursion
  9369. (beginning-of-line 1)
  9370. (if (org-at-heading-p)
  9371. (setq ltoggle (funcall outline-level))
  9372. (error "This should not happen"))
  9373. (while (and (setq level (org-up-heading-safe))
  9374. (or recursive first)
  9375. (>= (point) lim))
  9376. (setq first nil cookie-present nil)
  9377. (unless (and level
  9378. (not (string-match
  9379. "\\<checkbox\\>"
  9380. (downcase
  9381. (or (org-entry-get
  9382. nil "COOKIE_DATA")
  9383. "")))))
  9384. (throw 'exit nil))
  9385. (while (re-search-forward box-re (point-at-eol) t)
  9386. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9387. (setq is-percent (match-end 2))
  9388. (save-match-data
  9389. (unless (outline-next-heading) (throw 'exit nil))
  9390. (while (and (looking-at org-complex-heading-regexp)
  9391. (> (setq l1 (length (match-string 1))) level))
  9392. (setq kwd (and (or recursive (= l1 ltoggle))
  9393. (match-string 2)))
  9394. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9395. (and (listp org-provide-todo-statistics)
  9396. (or (member kwd org-provide-todo-statistics)
  9397. (member kwd org-done-keywords))))
  9398. (setq cnt-all (1+ cnt-all))
  9399. (if (eq org-provide-todo-statistics t)
  9400. (and kwd (setq cnt-all (1+ cnt-all)))))
  9401. (and (member kwd org-done-keywords)
  9402. (setq cnt-done (1+ cnt-done)))
  9403. (outline-next-heading)))
  9404. (setq new
  9405. (if is-percent
  9406. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9407. (format "[%d/%d]" cnt-done cnt-all))
  9408. ndel (- (match-end 0) (match-beginning 0)))
  9409. (goto-char (match-beginning 0))
  9410. (insert new)
  9411. (delete-region (point) (+ (point) ndel)))
  9412. (when cookie-present
  9413. (run-hook-with-args 'org-after-todo-statistics-hook
  9414. cnt-done (- cnt-all cnt-done))))))
  9415. (run-hooks 'org-todo-statistics-hook)))
  9416. (defvar org-after-todo-statistics-hook nil
  9417. "Hook that is called after a TODO statistics cookie has been updated.
  9418. Each function is called with two arguments: the number of not-done entries
  9419. and the number of done entries.
  9420. For example, the following function, when added to this hook, will switch
  9421. an entry to DONE when all children are done, and back to TODO when new
  9422. entries are set to a TODO status. Note that this hook is only called
  9423. when there is a statistics cookie in the headline!
  9424. (defun org-summary-todo (n-done n-not-done)
  9425. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9426. (let (org-log-done org-log-states) ; turn off logging
  9427. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9428. ")
  9429. (defvar org-todo-statistics-hook nil
  9430. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9431. This hook runs even if there is no statistics cookie present, in which case
  9432. `org-after-todo-statistics-hook' would not run.")
  9433. (defun org-todo-trigger-tag-changes (state)
  9434. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9435. (let ((l org-todo-state-tags-triggers)
  9436. changes)
  9437. (when (or (not state) (equal state ""))
  9438. (setq changes (append changes (cdr (assoc "" l)))))
  9439. (when (and (stringp state) (> (length state) 0))
  9440. (setq changes (append changes (cdr (assoc state l)))))
  9441. (when (member state org-not-done-keywords)
  9442. (setq changes (append changes (cdr (assoc 'todo l)))))
  9443. (when (member state org-done-keywords)
  9444. (setq changes (append changes (cdr (assoc 'done l)))))
  9445. (dolist (c changes)
  9446. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9447. (defun org-local-logging (value)
  9448. "Get logging settings from a property VALUE."
  9449. (let* (words w a)
  9450. ;; directly set the variables, they are already local.
  9451. (setq org-log-done nil
  9452. org-log-repeat nil
  9453. org-todo-log-states nil)
  9454. (setq words (org-split-string value))
  9455. (while (setq w (pop words))
  9456. (cond
  9457. ((setq a (assoc w org-startup-options))
  9458. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9459. (set (nth 1 a) (nth 2 a))))
  9460. ((setq a (org-extract-log-state-settings w))
  9461. (and (member (car a) org-todo-keywords-1)
  9462. (push a org-todo-log-states)))))))
  9463. (defun org-get-todo-sequence-head (kwd)
  9464. "Return the head of the TODO sequence to which KWD belongs.
  9465. If KWD is not set, check if there is a text property remembering the
  9466. right sequence."
  9467. (let (p)
  9468. (cond
  9469. ((not kwd)
  9470. (or (get-text-property (point-at-bol) 'org-todo-head)
  9471. (progn
  9472. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9473. nil (point-at-eol)))
  9474. (get-text-property p 'org-todo-head))))
  9475. ((not (member kwd org-todo-keywords-1))
  9476. (car org-todo-keywords-1))
  9477. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9478. (defun org-fast-todo-selection ()
  9479. "Fast TODO keyword selection with single keys.
  9480. Returns the new TODO keyword, or nil if no state change should occur."
  9481. (let* ((fulltable org-todo-key-alist)
  9482. (done-keywords org-done-keywords) ;; needed for the faces.
  9483. (maxlen (apply 'max (mapcar
  9484. (lambda (x)
  9485. (if (stringp (car x)) (string-width (car x)) 0))
  9486. fulltable)))
  9487. (expert nil)
  9488. (fwidth (+ maxlen 3 1 3))
  9489. (ncol (/ (- (window-width) 4) fwidth))
  9490. tg cnt e c tbl
  9491. groups ingroup)
  9492. (save-excursion
  9493. (save-window-excursion
  9494. (if expert
  9495. (set-buffer (get-buffer-create " *Org todo*"))
  9496. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9497. (erase-buffer)
  9498. (org-set-local 'org-done-keywords done-keywords)
  9499. (setq tbl fulltable cnt 0)
  9500. (while (setq e (pop tbl))
  9501. (cond
  9502. ((equal e '(:startgroup))
  9503. (push '() groups) (setq ingroup t)
  9504. (when (not (= cnt 0))
  9505. (setq cnt 0)
  9506. (insert "\n"))
  9507. (insert "{ "))
  9508. ((equal e '(:endgroup))
  9509. (setq ingroup nil cnt 0)
  9510. (insert "}\n"))
  9511. ((equal e '(:newline))
  9512. (when (not (= cnt 0))
  9513. (setq cnt 0)
  9514. (insert "\n")
  9515. (setq e (car tbl))
  9516. (while (equal (car tbl) '(:newline))
  9517. (insert "\n")
  9518. (setq tbl (cdr tbl)))))
  9519. (t
  9520. (setq tg (car e) c (cdr e))
  9521. (if ingroup (push tg (car groups)))
  9522. (setq tg (org-add-props tg nil 'face
  9523. (org-get-todo-face tg)))
  9524. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9525. (insert "[" c "] " tg (make-string
  9526. (- fwidth 4 (length tg)) ?\ ))
  9527. (when (= (setq cnt (1+ cnt)) ncol)
  9528. (insert "\n")
  9529. (if ingroup (insert " "))
  9530. (setq cnt 0)))))
  9531. (insert "\n")
  9532. (goto-char (point-min))
  9533. (if (not expert) (org-fit-window-to-buffer))
  9534. (message "[a-z..]:Set [SPC]:clear")
  9535. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9536. (cond
  9537. ((or (= c ?\C-g)
  9538. (and (= c ?q) (not (rassoc c fulltable))))
  9539. (setq quit-flag t))
  9540. ((= c ?\ ) nil)
  9541. ((setq e (rassoc c fulltable) tg (car e))
  9542. tg)
  9543. (t (setq quit-flag t)))))))
  9544. (defun org-entry-is-todo-p ()
  9545. (member (org-get-todo-state) org-not-done-keywords))
  9546. (defun org-entry-is-done-p ()
  9547. (member (org-get-todo-state) org-done-keywords))
  9548. (defun org-get-todo-state ()
  9549. (save-excursion
  9550. (org-back-to-heading t)
  9551. (and (looking-at org-todo-line-regexp)
  9552. (match-end 2)
  9553. (match-string 2))))
  9554. (defun org-at-date-range-p (&optional inactive-ok)
  9555. "Is the cursor inside a date range?"
  9556. (interactive)
  9557. (save-excursion
  9558. (catch 'exit
  9559. (let ((pos (point)))
  9560. (skip-chars-backward "^[<\r\n")
  9561. (skip-chars-backward "<[")
  9562. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9563. (>= (match-end 0) pos)
  9564. (throw 'exit t))
  9565. (skip-chars-backward "^<[\r\n")
  9566. (skip-chars-backward "<[")
  9567. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9568. (>= (match-end 0) pos)
  9569. (throw 'exit t)))
  9570. nil)))
  9571. (defun org-get-repeat (&optional tagline)
  9572. "Check if there is a deadline/schedule with repeater in this entry."
  9573. (save-match-data
  9574. (save-excursion
  9575. (org-back-to-heading t)
  9576. (and (re-search-forward (if tagline
  9577. (concat tagline "\\s-*" org-repeat-re)
  9578. org-repeat-re)
  9579. (org-entry-end-position) t)
  9580. (match-string-no-properties 1)))))
  9581. (defvar org-last-changed-timestamp)
  9582. (defvar org-last-inserted-timestamp)
  9583. (defvar org-log-post-message)
  9584. (defvar org-log-note-purpose)
  9585. (defvar org-log-note-how)
  9586. (defvar org-log-note-extra)
  9587. (defun org-auto-repeat-maybe (done-word)
  9588. "Check if the current headline contains a repeated deadline/schedule.
  9589. If yes, set TODO state back to what it was and change the base date
  9590. of repeating deadline/scheduled time stamps to new date.
  9591. This function is run automatically after each state change to a DONE state."
  9592. ;; last-state is dynamically scoped into this function
  9593. (let* ((repeat (org-get-repeat))
  9594. (aa (assoc last-state org-todo-kwd-alist))
  9595. (interpret (nth 1 aa))
  9596. (head (nth 2 aa))
  9597. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9598. (msg "Entry repeats: ")
  9599. (org-log-done nil)
  9600. (org-todo-log-states nil)
  9601. (nshiftmax 10) (nshift 0)
  9602. re type n what ts time)
  9603. (when repeat
  9604. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9605. (org-todo (if (eq interpret 'type) last-state head))
  9606. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9607. (org-time-stamp-format t t)))
  9608. (when org-log-repeat
  9609. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9610. (memq 'org-add-log-note post-command-hook))
  9611. ;; OK, we are already setup for some record
  9612. (if (eq org-log-repeat 'note)
  9613. ;; make sure we take a note, not only a time stamp
  9614. (setq org-log-note-how 'note))
  9615. ;; Set up for taking a record
  9616. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9617. last-state
  9618. 'findpos org-log-repeat)))
  9619. (org-back-to-heading t)
  9620. (org-add-planning-info nil nil 'closed)
  9621. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9622. org-deadline-time-regexp "\\)\\|\\("
  9623. org-ts-regexp "\\)"))
  9624. (while (re-search-forward
  9625. re (save-excursion (outline-next-heading) (point)) t)
  9626. (setq type (if (match-end 1) org-scheduled-string
  9627. (if (match-end 3) org-deadline-string "Plain:"))
  9628. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9629. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9630. (setq n (string-to-number (match-string 2 ts))
  9631. what (match-string 3 ts))
  9632. (if (equal what "w") (setq n (* n 7) what "d"))
  9633. ;; Preparation, see if we need to modify the start date for the change
  9634. (when (match-end 1)
  9635. (setq time (save-match-data (org-time-string-to-time ts)))
  9636. (cond
  9637. ((equal (match-string 1 ts) ".")
  9638. ;; Shift starting date to today
  9639. (org-timestamp-change
  9640. (- (time-to-days (current-time)) (time-to-days time))
  9641. 'day))
  9642. ((equal (match-string 1 ts) "+")
  9643. (while (or (= nshift 0)
  9644. (<= (time-to-days time) (time-to-days (current-time))))
  9645. (when (= (incf nshift) nshiftmax)
  9646. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9647. (error "Abort")))
  9648. (org-timestamp-change n (cdr (assoc what whata)))
  9649. (org-at-timestamp-p t)
  9650. (setq ts (match-string 1))
  9651. (setq time (save-match-data (org-time-string-to-time ts))))
  9652. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9653. ;; rematch, so that we have everything in place for the real shift
  9654. (org-at-timestamp-p t)
  9655. (setq ts (match-string 1))
  9656. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9657. (org-timestamp-change n (cdr (assoc what whata)))
  9658. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9659. (setq org-log-post-message msg)
  9660. (message "%s" msg))))
  9661. (defun org-show-todo-tree (arg)
  9662. "Make a compact tree which shows all headlines marked with TODO.
  9663. The tree will show the lines where the regexp matches, and all higher
  9664. headlines above the match.
  9665. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9666. With a numeric prefix N, construct a sparse tree for the Nth element
  9667. of `org-todo-keywords-1'."
  9668. (interactive "P")
  9669. (let ((case-fold-search nil)
  9670. (kwd-re
  9671. (cond ((null arg) org-not-done-regexp)
  9672. ((equal arg '(4))
  9673. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9674. (mapcar 'list org-todo-keywords-1))))
  9675. (concat "\\("
  9676. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9677. "\\)\\>")))
  9678. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9679. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9680. org-todo-keywords-1)))
  9681. (t (error "Invalid prefix argument: %s" arg)))))
  9682. (message "%d TODO entries found"
  9683. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9684. (defun org-deadline (&optional remove time)
  9685. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9686. With argument REMOVE, remove any deadline from the item.
  9687. When TIME is set, it should be an internal time specification, and the
  9688. scheduling will use the corresponding date."
  9689. (interactive "P")
  9690. (let ((old-date (org-entry-get nil "DEADLINE")))
  9691. (if remove
  9692. (progn
  9693. (when (and old-date org-log-redeadline)
  9694. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9695. org-log-redeadline))
  9696. (org-remove-timestamp-with-keyword org-deadline-string)
  9697. (message "Item no longer has a deadline."))
  9698. (if (org-get-repeat)
  9699. (error "Cannot change deadline on task with repeater, please do that by hand")
  9700. (org-add-planning-info 'deadline time 'closed)
  9701. (when (and old-date org-log-redeadline
  9702. (not (equal old-date
  9703. (substring org-last-inserted-timestamp 1 -1))))
  9704. (org-add-log-setup 'redeadline nil old-date 'findpos
  9705. org-log-redeadline))
  9706. (message "Deadline on %s" org-last-inserted-timestamp)))))
  9707. (defun org-schedule (&optional remove time)
  9708. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9709. With argument REMOVE, remove any scheduling date from the item.
  9710. When TIME is set, it should be an internal time specification, and the
  9711. scheduling will use the corresponding date."
  9712. (interactive "P")
  9713. (let ((old-date (org-entry-get nil "SCHEDULED")))
  9714. (if remove
  9715. (progn
  9716. (when (and old-date org-log-reschedule)
  9717. (org-add-log-setup 'delschedule nil old-date 'findpos
  9718. org-log-reschedule))
  9719. (org-remove-timestamp-with-keyword org-scheduled-string)
  9720. (message "Item is no longer scheduled."))
  9721. (if (org-get-repeat)
  9722. (error "Cannot reschedule task with repeater, please do that by hand")
  9723. (org-add-planning-info 'scheduled time 'closed)
  9724. (when (and old-date org-log-reschedule
  9725. (not (equal old-date
  9726. (substring org-last-inserted-timestamp 1 -1))))
  9727. (org-add-log-setup 'reschedule nil old-date 'findpos
  9728. org-log-reschedule))
  9729. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  9730. (defun org-get-scheduled-time (pom &optional inherit)
  9731. "Get the scheduled time as a time tuple, of a format suitable
  9732. for calling org-schedule with, or if there is no scheduling,
  9733. returns nil."
  9734. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9735. (when time
  9736. (apply 'encode-time (org-parse-time-string time)))))
  9737. (defun org-get-deadline-time (pom &optional inherit)
  9738. "Get the deadine as a time tuple, of a format suitable for
  9739. calling org-deadline with, or if there is no scheduling, returns
  9740. nil."
  9741. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9742. (when time
  9743. (apply 'encode-time (org-parse-time-string time)))))
  9744. (defun org-remove-timestamp-with-keyword (keyword)
  9745. "Remove all time stamps with KEYWORD in the current entry."
  9746. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9747. beg)
  9748. (save-excursion
  9749. (org-back-to-heading t)
  9750. (setq beg (point))
  9751. (outline-next-heading)
  9752. (while (re-search-backward re beg t)
  9753. (replace-match "")
  9754. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9755. (equal (char-before) ?\ ))
  9756. (backward-delete-char 1)
  9757. (if (string-match "^[ \t]*$" (buffer-substring
  9758. (point-at-bol) (point-at-eol)))
  9759. (delete-region (point-at-bol)
  9760. (min (point-max) (1+ (point-at-eol))))))))))
  9761. (defun org-add-planning-info (what &optional time &rest remove)
  9762. "Insert new timestamp with keyword in the line directly after the headline.
  9763. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9764. If non is given, the user is prompted for a date.
  9765. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9766. be removed."
  9767. (interactive)
  9768. (let (org-time-was-given org-end-time-was-given ts
  9769. end default-time default-input)
  9770. (catch 'exit
  9771. (when (and (not time) (memq what '(scheduled deadline)))
  9772. ;; Try to get a default date/time from existing timestamp
  9773. (save-excursion
  9774. (org-back-to-heading t)
  9775. (setq end (save-excursion (outline-next-heading) (point)))
  9776. (when (re-search-forward (if (eq what 'scheduled)
  9777. org-scheduled-time-regexp
  9778. org-deadline-time-regexp)
  9779. end t)
  9780. (setq ts (match-string 1)
  9781. default-time
  9782. (apply 'encode-time (org-parse-time-string ts))
  9783. default-input (and ts (org-get-compact-tod ts))))))
  9784. (when what
  9785. ;; If necessary, get the time from the user
  9786. (setq time (or time (org-read-date nil 'to-time nil nil
  9787. default-time default-input))))
  9788. (when (and org-insert-labeled-timestamps-at-point
  9789. (member what '(scheduled deadline)))
  9790. (insert
  9791. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9792. (org-insert-time-stamp time org-time-was-given
  9793. nil nil nil (list org-end-time-was-given))
  9794. (setq what nil))
  9795. (save-excursion
  9796. (save-restriction
  9797. (let (col list elt ts buffer-invisibility-spec)
  9798. (org-back-to-heading t)
  9799. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9800. (goto-char (match-end 1))
  9801. (setq col (current-column))
  9802. (goto-char (match-end 0))
  9803. (if (eobp) (insert "\n") (forward-char 1))
  9804. (when (and (not what)
  9805. (not (looking-at
  9806. (concat "[ \t]*"
  9807. org-keyword-time-not-clock-regexp))))
  9808. ;; Nothing to add, nothing to remove...... :-)
  9809. (throw 'exit nil))
  9810. (if (and (not (looking-at outline-regexp))
  9811. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9812. "[^\r\n]*"))
  9813. (not (equal (match-string 1) org-clock-string)))
  9814. (narrow-to-region (match-beginning 0) (match-end 0))
  9815. (insert-before-markers "\n")
  9816. (backward-char 1)
  9817. (narrow-to-region (point) (point))
  9818. (and org-adapt-indentation (org-indent-to-column col)))
  9819. ;; Check if we have to remove something.
  9820. (setq list (cons what remove))
  9821. (while list
  9822. (setq elt (pop list))
  9823. (goto-char (point-min))
  9824. (when (or (and (eq elt 'scheduled)
  9825. (re-search-forward org-scheduled-time-regexp nil t))
  9826. (and (eq elt 'deadline)
  9827. (re-search-forward org-deadline-time-regexp nil t))
  9828. (and (eq elt 'closed)
  9829. (re-search-forward org-closed-time-regexp nil t)))
  9830. (replace-match "")
  9831. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9832. (skip-chars-backward " ")
  9833. (if (looking-at " +") (replace-match ""))))
  9834. (goto-char (point-max))
  9835. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  9836. (when what
  9837. (insert
  9838. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9839. (cond ((eq what 'scheduled) org-scheduled-string)
  9840. ((eq what 'deadline) org-deadline-string)
  9841. ((eq what 'closed) org-closed-string))
  9842. " ")
  9843. (setq ts (org-insert-time-stamp
  9844. time
  9845. (or org-time-was-given
  9846. (and (eq what 'closed) org-log-done-with-time))
  9847. (eq what 'closed)
  9848. nil nil (list org-end-time-was-given)))
  9849. (end-of-line 1))
  9850. (goto-char (point-min))
  9851. (widen)
  9852. (if (and (looking-at "[ \t]+\n")
  9853. (equal (char-before) ?\n))
  9854. (delete-region (1- (point)) (point-at-eol)))
  9855. ts))))))
  9856. (defvar org-log-note-marker (make-marker))
  9857. (defvar org-log-note-purpose nil)
  9858. (defvar org-log-note-state nil)
  9859. (defvar org-log-note-previous-state nil)
  9860. (defvar org-log-note-how nil)
  9861. (defvar org-log-note-extra nil)
  9862. (defvar org-log-note-window-configuration nil)
  9863. (defvar org-log-note-return-to (make-marker))
  9864. (defvar org-log-post-message nil
  9865. "Message to be displayed after a log note has been stored.
  9866. The auto-repeater uses this.")
  9867. (defun org-add-note ()
  9868. "Add a note to the current entry.
  9869. This is done in the same way as adding a state change note."
  9870. (interactive)
  9871. (org-add-log-setup 'note nil nil 'findpos nil))
  9872. (defvar org-property-end-re)
  9873. (defun org-add-log-setup (&optional purpose state prev-state
  9874. findpos how &optional extra)
  9875. "Set up the post command hook to take a note.
  9876. If this is about to TODO state change, the new state is expected in STATE.
  9877. When FINDPOS is non-nil, find the correct position for the note in
  9878. the current entry. If not, assume that it can be inserted at point.
  9879. HOW is an indicator what kind of note should be created.
  9880. EXTRA is additional text that will be inserted into the notes buffer."
  9881. (let* ((org-log-into-drawer (org-log-into-drawer))
  9882. (drawer (cond ((stringp org-log-into-drawer)
  9883. org-log-into-drawer)
  9884. (org-log-into-drawer "LOGBOOK")
  9885. (t nil))))
  9886. (save-restriction
  9887. (save-excursion
  9888. (when findpos
  9889. (org-back-to-heading t)
  9890. (narrow-to-region (point) (save-excursion
  9891. (outline-next-heading) (point)))
  9892. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9893. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9894. "[^\r\n]*\\)?"))
  9895. (goto-char (match-end 0))
  9896. (cond
  9897. (drawer
  9898. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9899. nil t)
  9900. (progn
  9901. (goto-char (match-end 0))
  9902. (or org-log-states-order-reversed
  9903. (and (re-search-forward org-property-end-re nil t)
  9904. (goto-char (1- (match-beginning 0))))))
  9905. (insert "\n:" drawer ":\n:END:")
  9906. (beginning-of-line 0)
  9907. (org-indent-line-function)
  9908. (beginning-of-line 2)
  9909. (org-indent-line-function)
  9910. (end-of-line 0)))
  9911. ((and org-log-state-notes-insert-after-drawers
  9912. (save-excursion
  9913. (forward-line) (looking-at org-drawer-regexp)))
  9914. (forward-line)
  9915. (while (looking-at org-drawer-regexp)
  9916. (goto-char (match-end 0))
  9917. (re-search-forward org-property-end-re (point-max) t)
  9918. (forward-line))
  9919. (forward-line -1)))
  9920. (unless org-log-states-order-reversed
  9921. (and (= (char-after) ?\n) (forward-char 1))
  9922. (org-skip-over-state-notes)
  9923. (skip-chars-backward " \t\n\r")))
  9924. (move-marker org-log-note-marker (point))
  9925. (setq org-log-note-purpose purpose
  9926. org-log-note-state state
  9927. org-log-note-previous-state prev-state
  9928. org-log-note-how how
  9929. org-log-note-extra extra)
  9930. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9931. (defun org-skip-over-state-notes ()
  9932. "Skip past the list of State notes in an entry."
  9933. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9934. (while (looking-at "[ \t]*- State")
  9935. (condition-case nil
  9936. (org-next-item)
  9937. (error (org-end-of-item)))))
  9938. (defun org-add-log-note (&optional purpose)
  9939. "Pop up a window for taking a note, and add this note later at point."
  9940. (remove-hook 'post-command-hook 'org-add-log-note)
  9941. (setq org-log-note-window-configuration (current-window-configuration))
  9942. (delete-other-windows)
  9943. (move-marker org-log-note-return-to (point))
  9944. (switch-to-buffer (marker-buffer org-log-note-marker))
  9945. (goto-char org-log-note-marker)
  9946. (org-switch-to-buffer-other-window "*Org Note*")
  9947. (erase-buffer)
  9948. (if (memq org-log-note-how '(time state))
  9949. (let (current-prefix-arg) (org-store-log-note))
  9950. (let ((org-inhibit-startup t)) (org-mode))
  9951. (insert (format "# Insert note for %s.
  9952. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9953. (cond
  9954. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9955. ((eq org-log-note-purpose 'done) "closed todo item")
  9956. ((eq org-log-note-purpose 'state)
  9957. (format "state change from \"%s\" to \"%s\""
  9958. (or org-log-note-previous-state "")
  9959. (or org-log-note-state "")))
  9960. ((eq org-log-note-purpose 'reschedule)
  9961. "rescheduling")
  9962. ((eq org-log-note-purpose 'delschedule)
  9963. "no longer scheduled")
  9964. ((eq org-log-note-purpose 'redeadline)
  9965. "changing deadline")
  9966. ((eq org-log-note-purpose 'deldeadline)
  9967. "removing deadline")
  9968. ((eq org-log-note-purpose 'note)
  9969. "this entry")
  9970. (t (error "This should not happen")))))
  9971. (if org-log-note-extra (insert org-log-note-extra))
  9972. (org-set-local 'org-finish-function 'org-store-log-note)))
  9973. (defvar org-note-abort nil) ; dynamically scoped
  9974. (defun org-store-log-note ()
  9975. "Finish taking a log note, and insert it to where it belongs."
  9976. (let ((txt (buffer-string))
  9977. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9978. lines ind)
  9979. (kill-buffer (current-buffer))
  9980. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9981. (setq txt (replace-match "" t t txt)))
  9982. (if (string-match "\\s-+\\'" txt)
  9983. (setq txt (replace-match "" t t txt)))
  9984. (setq lines (org-split-string txt "\n"))
  9985. (when (and note (string-match "\\S-" note))
  9986. (setq note
  9987. (org-replace-escapes
  9988. note
  9989. (list (cons "%u" (user-login-name))
  9990. (cons "%U" user-full-name)
  9991. (cons "%t" (format-time-string
  9992. (org-time-stamp-format 'long 'inactive)
  9993. (current-time)))
  9994. (cons "%s" (if org-log-note-state
  9995. (concat "\"" org-log-note-state "\"")
  9996. ""))
  9997. (cons "%S" (if org-log-note-previous-state
  9998. (concat "\"" org-log-note-previous-state "\"")
  9999. "\"\"")))))
  10000. (if lines (setq note (concat note " \\\\")))
  10001. (push note lines))
  10002. (when (or current-prefix-arg org-note-abort)
  10003. (when org-log-into-drawer
  10004. (org-remove-empty-drawer-at
  10005. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10006. org-log-note-marker))
  10007. (setq lines nil))
  10008. (when lines
  10009. (with-current-buffer (marker-buffer org-log-note-marker)
  10010. (save-excursion
  10011. (goto-char org-log-note-marker)
  10012. (move-marker org-log-note-marker nil)
  10013. (end-of-line 1)
  10014. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10015. (insert "- " (pop lines))
  10016. (org-indent-line-function)
  10017. (beginning-of-line 1)
  10018. (looking-at "[ \t]*")
  10019. (setq ind (concat (match-string 0) " "))
  10020. (end-of-line 1)
  10021. (while lines (insert "\n" ind (pop lines)))
  10022. (message "Note stored")
  10023. (org-back-to-heading t)
  10024. (org-cycle-hide-drawers 'children)))))
  10025. (set-window-configuration org-log-note-window-configuration)
  10026. (with-current-buffer (marker-buffer org-log-note-return-to)
  10027. (goto-char org-log-note-return-to))
  10028. (move-marker org-log-note-return-to nil)
  10029. (and org-log-post-message (message "%s" org-log-post-message)))
  10030. (defun org-remove-empty-drawer-at (drawer pos)
  10031. "Remove an empty drawer DRAWER at position POS.
  10032. POS may also be a marker."
  10033. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10034. (save-excursion
  10035. (save-restriction
  10036. (widen)
  10037. (goto-char pos)
  10038. (if (org-in-regexp
  10039. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10040. (replace-match ""))))))
  10041. (defun org-sparse-tree (&optional arg)
  10042. "Create a sparse tree, prompt for the details.
  10043. This command can create sparse trees. You first need to select the type
  10044. of match used to create the tree:
  10045. t Show entries with a specific TODO keyword.
  10046. m Show entries selected by a tags/property match.
  10047. p Enter a property name and its value (both with completion on existing
  10048. names/values) and show entries with that property.
  10049. / Show entries matching a regular expression (`r' can be used as well)
  10050. d Show deadlines due within `org-deadline-warning-days'.
  10051. b Show deadlines and scheduled items before a date.
  10052. a Show deadlines and scheduled items after a date."
  10053. (interactive "P")
  10054. (let (ans kwd value)
  10055. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10056. (setq ans (read-char-exclusive))
  10057. (cond
  10058. ((equal ans ?d)
  10059. (call-interactively 'org-check-deadlines))
  10060. ((equal ans ?b)
  10061. (call-interactively 'org-check-before-date))
  10062. ((equal ans ?a)
  10063. (call-interactively 'org-check-after-date))
  10064. ((equal ans ?t)
  10065. (org-show-todo-tree '(4)))
  10066. ((member ans '(?T ?m))
  10067. (call-interactively 'org-match-sparse-tree))
  10068. ((member ans '(?p ?P))
  10069. (setq kwd (org-icompleting-read "Property: "
  10070. (mapcar 'list (org-buffer-property-keys))))
  10071. (setq value (org-icompleting-read "Value: "
  10072. (mapcar 'list (org-property-values kwd))))
  10073. (unless (string-match "\\`{.*}\\'" value)
  10074. (setq value (concat "\"" value "\"")))
  10075. (org-match-sparse-tree arg (concat kwd "=" value)))
  10076. ((member ans '(?r ?R ?/))
  10077. (call-interactively 'org-occur))
  10078. (t (error "No such sparse tree command \"%c\"" ans)))))
  10079. (defvar org-occur-highlights nil
  10080. "List of overlays used for occur matches.")
  10081. (make-variable-buffer-local 'org-occur-highlights)
  10082. (defvar org-occur-parameters nil
  10083. "Parameters of the active org-occur calls.
  10084. This is a list, each call to org-occur pushes as cons cell,
  10085. containing the regular expression and the callback, onto the list.
  10086. The list can contain several entries if `org-occur' has been called
  10087. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10088. will only contain one set of parameters. When the highlights are
  10089. removed (for example with `C-c C-c', or with the next edit (depending
  10090. on `org-remove-highlights-with-change'), this variable is emptied
  10091. as well.")
  10092. (make-variable-buffer-local 'org-occur-parameters)
  10093. (defun org-occur (regexp &optional keep-previous callback)
  10094. "Make a compact tree which shows all matches of REGEXP.
  10095. The tree will show the lines where the regexp matches, and all higher
  10096. headlines above the match. It will also show the heading after the match,
  10097. to make sure editing the matching entry is easy.
  10098. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10099. call to `org-occur' will be kept, to allow stacking of calls to this
  10100. command.
  10101. If CALLBACK is non-nil, it is a function which is called to confirm
  10102. that the match should indeed be shown."
  10103. (interactive "sRegexp: \nP")
  10104. (when (equal regexp "")
  10105. (error "Regexp cannot be empty"))
  10106. (unless keep-previous
  10107. (org-remove-occur-highlights nil nil t))
  10108. (push (cons regexp callback) org-occur-parameters)
  10109. (let ((cnt 0))
  10110. (save-excursion
  10111. (goto-char (point-min))
  10112. (if (or (not keep-previous) ; do not want to keep
  10113. (not org-occur-highlights)) ; no previous matches
  10114. ;; hide everything
  10115. (org-overview))
  10116. (while (re-search-forward regexp nil t)
  10117. (when (or (not callback)
  10118. (save-match-data (funcall callback)))
  10119. (setq cnt (1+ cnt))
  10120. (when org-highlight-sparse-tree-matches
  10121. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10122. (org-show-context 'occur-tree))))
  10123. (when org-remove-highlights-with-change
  10124. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10125. nil 'local))
  10126. (unless org-sparse-tree-open-archived-trees
  10127. (org-hide-archived-subtrees (point-min) (point-max)))
  10128. (run-hooks 'org-occur-hook)
  10129. (if (interactive-p)
  10130. (message "%d match(es) for regexp %s" cnt regexp))
  10131. cnt))
  10132. (defun org-show-context (&optional key)
  10133. "Make sure point and context and visible.
  10134. How much context is shown depends upon the variables
  10135. `org-show-hierarchy-above', `org-show-following-heading'. and
  10136. `org-show-siblings'."
  10137. (let ((heading-p (org-on-heading-p t))
  10138. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10139. (following-p (org-get-alist-option org-show-following-heading key))
  10140. (entry-p (org-get-alist-option org-show-entry-below key))
  10141. (siblings-p (org-get-alist-option org-show-siblings key)))
  10142. (catch 'exit
  10143. ;; Show heading or entry text
  10144. (if (and heading-p (not entry-p))
  10145. (org-flag-heading nil) ; only show the heading
  10146. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10147. (org-show-hidden-entry))) ; show entire entry
  10148. (when following-p
  10149. ;; Show next sibling, or heading below text
  10150. (save-excursion
  10151. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10152. (org-flag-heading nil))))
  10153. (when siblings-p (org-show-siblings))
  10154. (when hierarchy-p
  10155. ;; show all higher headings, possibly with siblings
  10156. (save-excursion
  10157. (while (and (condition-case nil
  10158. (progn (org-up-heading-all 1) t)
  10159. (error nil))
  10160. (not (bobp)))
  10161. (org-flag-heading nil)
  10162. (when siblings-p (org-show-siblings))))))))
  10163. (defun org-reveal (&optional siblings)
  10164. "Show current entry, hierarchy above it, and the following headline.
  10165. This can be used to show a consistent set of context around locations
  10166. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10167. not t for the search context.
  10168. With optional argument SIBLINGS, on each level of the hierarchy all
  10169. siblings are shown. This repairs the tree structure to what it would
  10170. look like when opened with hierarchical calls to `org-cycle'."
  10171. (interactive "P")
  10172. (let ((org-show-hierarchy-above t)
  10173. (org-show-following-heading t)
  10174. (org-show-siblings (if siblings t org-show-siblings)))
  10175. (org-show-context nil)))
  10176. (defun org-highlight-new-match (beg end)
  10177. "Highlight from BEG to END and mark the highlight is an occur headline."
  10178. (let ((ov (org-make-overlay beg end)))
  10179. (org-overlay-put ov 'face 'secondary-selection)
  10180. (push ov org-occur-highlights)))
  10181. (defun org-remove-occur-highlights (&optional beg end noremove)
  10182. "Remove the occur highlights from the buffer.
  10183. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10184. from the `before-change-functions' in the current buffer."
  10185. (interactive)
  10186. (unless org-inhibit-highlight-removal
  10187. (mapc 'org-delete-overlay org-occur-highlights)
  10188. (setq org-occur-highlights nil)
  10189. (setq org-occur-parameters nil)
  10190. (unless noremove
  10191. (remove-hook 'before-change-functions
  10192. 'org-remove-occur-highlights 'local))))
  10193. ;;;; Priorities
  10194. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10195. "Regular expression matching the priority indicator.")
  10196. (defvar org-remove-priority-next-time nil)
  10197. (defun org-priority-up ()
  10198. "Increase the priority of the current item."
  10199. (interactive)
  10200. (org-priority 'up))
  10201. (defun org-priority-down ()
  10202. "Decrease the priority of the current item."
  10203. (interactive)
  10204. (org-priority 'down))
  10205. (defun org-priority (&optional action)
  10206. "Change the priority of an item by ARG.
  10207. ACTION can be `set', `up', `down', or a character."
  10208. (interactive)
  10209. (unless org-enable-priority-commands
  10210. (error "Priority commands are disabled"))
  10211. (setq action (or action 'set))
  10212. (let (current new news have remove)
  10213. (save-excursion
  10214. (org-back-to-heading t)
  10215. (if (looking-at org-priority-regexp)
  10216. (setq current (string-to-char (match-string 2))
  10217. have t)
  10218. (setq current org-default-priority))
  10219. (cond
  10220. ((eq action 'remove)
  10221. (setq remove t new ?\ ))
  10222. ((or (eq action 'set)
  10223. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10224. (if (not (eq action 'set))
  10225. (setq new action)
  10226. (message "Priority %c-%c, SPC to remove: "
  10227. org-highest-priority org-lowest-priority)
  10228. (setq new (read-char-exclusive)))
  10229. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10230. (= (upcase org-lowest-priority) org-lowest-priority))
  10231. (setq new (upcase new)))
  10232. (cond ((equal new ?\ ) (setq remove t))
  10233. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10234. (error "Priority must be between `%c' and `%c'"
  10235. org-highest-priority org-lowest-priority))))
  10236. ((eq action 'up)
  10237. (if (and (not have) (eq last-command this-command))
  10238. (setq new org-lowest-priority)
  10239. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10240. org-default-priority (1- current)))))
  10241. ((eq action 'down)
  10242. (if (and (not have) (eq last-command this-command))
  10243. (setq new org-highest-priority)
  10244. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10245. org-default-priority (1+ current)))))
  10246. (t (error "Invalid action")))
  10247. (if (or (< (upcase new) org-highest-priority)
  10248. (> (upcase new) org-lowest-priority))
  10249. (setq remove t))
  10250. (setq news (format "%c" new))
  10251. (if have
  10252. (if remove
  10253. (replace-match "" t t nil 1)
  10254. (replace-match news t t nil 2))
  10255. (if remove
  10256. (error "No priority cookie found in line")
  10257. (let ((case-fold-search nil))
  10258. (looking-at org-todo-line-regexp))
  10259. (if (match-end 2)
  10260. (progn
  10261. (goto-char (match-end 2))
  10262. (insert " [#" news "]"))
  10263. (goto-char (match-beginning 3))
  10264. (insert "[#" news "] "))))
  10265. (org-preserve-lc (org-set-tags nil 'align)))
  10266. (if remove
  10267. (message "Priority removed")
  10268. (message "Priority of current item set to %s" news))))
  10269. (defun org-get-priority (s)
  10270. "Find priority cookie and return priority."
  10271. (save-match-data
  10272. (if (not (string-match org-priority-regexp s))
  10273. (* 1000 (- org-lowest-priority org-default-priority))
  10274. (* 1000 (- org-lowest-priority
  10275. (string-to-char (match-string 2 s)))))))
  10276. ;;;; Tags
  10277. (defvar org-agenda-archives-mode)
  10278. (defvar org-map-continue-from nil
  10279. "Position from where mapping should continue.
  10280. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10281. (defvar org-scanner-tags nil
  10282. "The current tag list while the tags scanner is running.")
  10283. (defvar org-trust-scanner-tags nil
  10284. "Should `org-get-tags-at' use the tags fro the scanner.
  10285. This is for internal dynamical scoping only.
  10286. When this is non-nil, the function `org-get-tags-at' will return the value
  10287. of `org-scanner-tags' instead of building the list by itself. This
  10288. can lead to large speed-ups when the tags scanner is used in a file with
  10289. many entries, and when the list of tags is retrieved, for example to
  10290. obtain a list of properties. Building the tags list for each entry in such
  10291. a file becomes an N^2 operation - but with this variable set, it scales
  10292. as N.")
  10293. (defun org-scan-tags (action matcher &optional todo-only)
  10294. "Scan headline tags with inheritance and produce output ACTION.
  10295. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10296. or `agenda' to produce an entry list for an agenda view. It can also be
  10297. a Lisp form or a function that should be called at each matched headline, in
  10298. this case the return value is a list of all return values from these calls.
  10299. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10300. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10301. only lines with a TODO keyword are included in the output."
  10302. (require 'org-agenda)
  10303. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10304. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10305. (org-re
  10306. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10307. (props (list 'face 'default
  10308. 'done-face 'org-agenda-done
  10309. 'undone-face 'default
  10310. 'mouse-face 'highlight
  10311. 'org-not-done-regexp org-not-done-regexp
  10312. 'org-todo-regexp org-todo-regexp
  10313. 'help-echo
  10314. (format "mouse-2 or RET jump to org file %s"
  10315. (abbreviate-file-name
  10316. (or (buffer-file-name (buffer-base-buffer))
  10317. (buffer-name (buffer-base-buffer)))))))
  10318. (case-fold-search nil)
  10319. (org-map-continue-from nil)
  10320. lspos tags tags-list
  10321. (tags-alist (list (cons 0 org-file-tags)))
  10322. (llast 0) rtn rtn1 level category i txt
  10323. todo marker entry priority)
  10324. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10325. (setq action (list 'lambda nil action)))
  10326. (save-excursion
  10327. (goto-char (point-min))
  10328. (when (eq action 'sparse-tree)
  10329. (org-overview)
  10330. (org-remove-occur-highlights))
  10331. (while (re-search-forward re nil t)
  10332. (catch :skip
  10333. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10334. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10335. (goto-char (setq lspos (match-beginning 0)))
  10336. (setq level (org-reduced-level (funcall outline-level))
  10337. category (org-get-category))
  10338. (setq i llast llast level)
  10339. ;; remove tag lists from same and sublevels
  10340. (while (>= i level)
  10341. (when (setq entry (assoc i tags-alist))
  10342. (setq tags-alist (delete entry tags-alist)))
  10343. (setq i (1- i)))
  10344. ;; add the next tags
  10345. (when tags
  10346. (setq tags (org-split-string tags ":")
  10347. tags-alist
  10348. (cons (cons level tags) tags-alist)))
  10349. ;; compile tags for current headline
  10350. (setq tags-list
  10351. (if org-use-tag-inheritance
  10352. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10353. tags)
  10354. org-scanner-tags tags-list)
  10355. (when org-use-tag-inheritance
  10356. (setcdr (car tags-alist)
  10357. (mapcar (lambda (x)
  10358. (setq x (copy-sequence x))
  10359. (org-add-prop-inherited x))
  10360. (cdar tags-alist))))
  10361. (when (and tags org-use-tag-inheritance
  10362. (or (not (eq t org-use-tag-inheritance))
  10363. org-tags-exclude-from-inheritance))
  10364. ;; selective inheritance, remove uninherited ones
  10365. (setcdr (car tags-alist)
  10366. (org-remove-uniherited-tags (cdar tags-alist))))
  10367. (when (and (or (not todo-only)
  10368. (and (member todo org-not-done-keywords)
  10369. (or (not org-agenda-tags-todo-honor-ignore-options)
  10370. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10371. (let ((case-fold-search t)) (eval matcher))
  10372. (or
  10373. (not (member org-archive-tag tags-list))
  10374. ;; we have an archive tag, should we use this anyway?
  10375. (or (not org-agenda-skip-archived-trees)
  10376. (and (eq action 'agenda) org-agenda-archives-mode))))
  10377. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10378. ;; select this headline
  10379. (cond
  10380. ((eq action 'sparse-tree)
  10381. (and org-highlight-sparse-tree-matches
  10382. (org-get-heading) (match-end 0)
  10383. (org-highlight-new-match
  10384. (match-beginning 0) (match-beginning 1)))
  10385. (org-show-context 'tags-tree))
  10386. ((eq action 'agenda)
  10387. (setq txt (org-format-agenda-item
  10388. ""
  10389. (concat
  10390. (if (eq org-tags-match-list-sublevels 'indented)
  10391. (make-string (1- level) ?.) "")
  10392. (org-get-heading))
  10393. category
  10394. tags-list
  10395. )
  10396. priority (org-get-priority txt))
  10397. (goto-char lspos)
  10398. (setq marker (org-agenda-new-marker))
  10399. (org-add-props txt props
  10400. 'org-marker marker 'org-hd-marker marker 'org-category category
  10401. 'todo-state todo
  10402. 'priority priority 'type "tagsmatch")
  10403. (push txt rtn))
  10404. ((functionp action)
  10405. (setq org-map-continue-from nil)
  10406. (save-excursion
  10407. (setq rtn1 (funcall action))
  10408. (push rtn1 rtn)))
  10409. (t (error "Invalid action")))
  10410. ;; if we are to skip sublevels, jump to end of subtree
  10411. (unless org-tags-match-list-sublevels
  10412. (org-end-of-subtree t)
  10413. (backward-char 1))))
  10414. ;; Get the correct position from where to continue
  10415. (if org-map-continue-from
  10416. (goto-char org-map-continue-from)
  10417. (and (= (point) lspos) (end-of-line 1)))))
  10418. (when (and (eq action 'sparse-tree)
  10419. (not org-sparse-tree-open-archived-trees))
  10420. (org-hide-archived-subtrees (point-min) (point-max)))
  10421. (nreverse rtn)))
  10422. (defun org-remove-uniherited-tags (tags)
  10423. "Remove all tags that are not inherited from the list TAGS."
  10424. (cond
  10425. ((eq org-use-tag-inheritance t)
  10426. (if org-tags-exclude-from-inheritance
  10427. (org-delete-all org-tags-exclude-from-inheritance tags)
  10428. tags))
  10429. ((not org-use-tag-inheritance) nil)
  10430. ((stringp org-use-tag-inheritance)
  10431. (delq nil (mapcar
  10432. (lambda (x)
  10433. (if (and (string-match org-use-tag-inheritance x)
  10434. (not (member x org-tags-exclude-from-inheritance)))
  10435. x nil))
  10436. tags)))
  10437. ((listp org-use-tag-inheritance)
  10438. (delq nil (mapcar
  10439. (lambda (x)
  10440. (if (member x org-use-tag-inheritance) x nil))
  10441. tags)))))
  10442. (defvar todo-only) ;; dynamically scoped
  10443. (defun org-match-sparse-tree (&optional todo-only match)
  10444. "Create a sparse tree according to tags string MATCH.
  10445. MATCH can contain positive and negative selection of tags, like
  10446. \"+WORK+URGENT-WITHBOSS\".
  10447. If optional argument TODO-ONLY is non-nil, only select lines that are
  10448. also TODO lines."
  10449. (interactive "P")
  10450. (org-prepare-agenda-buffers (list (current-buffer)))
  10451. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10452. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10453. (defvar org-cached-props nil)
  10454. (defun org-cached-entry-get (pom property)
  10455. (if (or (eq t org-use-property-inheritance)
  10456. (and (stringp org-use-property-inheritance)
  10457. (string-match org-use-property-inheritance property))
  10458. (and (listp org-use-property-inheritance)
  10459. (member property org-use-property-inheritance)))
  10460. ;; Caching is not possible, check it directly
  10461. (org-entry-get pom property 'inherit)
  10462. ;; Get all properties, so that we can do complicated checks easily
  10463. (cdr (assoc property (or org-cached-props
  10464. (setq org-cached-props
  10465. (org-entry-properties pom)))))))
  10466. (defun org-global-tags-completion-table (&optional files)
  10467. "Return the list of all tags in all agenda buffer/files."
  10468. (save-excursion
  10469. (org-uniquify
  10470. (delq nil
  10471. (apply 'append
  10472. (mapcar
  10473. (lambda (file)
  10474. (set-buffer (find-file-noselect file))
  10475. (append (org-get-buffer-tags)
  10476. (mapcar (lambda (x) (if (stringp (car-safe x))
  10477. (list (car-safe x)) nil))
  10478. org-tag-alist)))
  10479. (if (and files (car files))
  10480. files
  10481. (org-agenda-files))))))))
  10482. (defun org-make-tags-matcher (match)
  10483. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10484. ;; todo-only is scoped dynamically into this function, and the function
  10485. ;; may change it if the matcher asks for it.
  10486. (unless match
  10487. ;; Get a new match request, with completion
  10488. (let ((org-last-tags-completion-table
  10489. (org-global-tags-completion-table)))
  10490. (setq match (org-completing-read-no-i
  10491. "Match: " 'org-tags-completion-function nil nil nil
  10492. 'org-tags-history))))
  10493. ;; Parse the string and create a lisp form
  10494. (let ((match0 match)
  10495. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10496. minus tag mm
  10497. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10498. orterms term orlist re-p str-p level-p level-op time-p
  10499. prop-p pn pv po cat-p gv rest)
  10500. (if (string-match "/+" match)
  10501. ;; match contains also a todo-matching request
  10502. (progn
  10503. (setq tagsmatch (substring match 0 (match-beginning 0))
  10504. todomatch (substring match (match-end 0)))
  10505. (if (string-match "^!" todomatch)
  10506. (setq todo-only t todomatch (substring todomatch 1)))
  10507. (if (string-match "^\\s-*$" todomatch)
  10508. (setq todomatch nil)))
  10509. ;; only matching tags
  10510. (setq tagsmatch match todomatch nil))
  10511. ;; Make the tags matcher
  10512. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10513. (setq tagsmatcher t)
  10514. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10515. (while (setq term (pop orterms))
  10516. (while (and (equal (substring term -1) "\\") orterms)
  10517. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10518. (while (string-match re term)
  10519. (setq rest (substring term (match-end 0))
  10520. minus (and (match-end 1)
  10521. (equal (match-string 1 term) "-"))
  10522. tag (match-string 2 term)
  10523. re-p (equal (string-to-char tag) ?{)
  10524. level-p (match-end 4)
  10525. prop-p (match-end 5)
  10526. mm (cond
  10527. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10528. (level-p
  10529. (setq level-op (org-op-to-function (match-string 3 term)))
  10530. `(,level-op level ,(string-to-number
  10531. (match-string 4 term))))
  10532. (prop-p
  10533. (setq pn (match-string 5 term)
  10534. po (match-string 6 term)
  10535. pv (match-string 7 term)
  10536. cat-p (equal pn "CATEGORY")
  10537. re-p (equal (string-to-char pv) ?{)
  10538. str-p (equal (string-to-char pv) ?\")
  10539. time-p (save-match-data
  10540. (string-match "^\"[[<].*[]>]\"$" pv))
  10541. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10542. (if time-p (setq pv (org-matcher-time pv)))
  10543. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10544. (cond
  10545. ((equal pn "CATEGORY")
  10546. (setq gv '(get-text-property (point) 'org-category)))
  10547. ((equal pn "TODO")
  10548. (setq gv 'todo))
  10549. (t
  10550. (setq gv `(org-cached-entry-get nil ,pn))))
  10551. (if re-p
  10552. (if (eq po 'org<>)
  10553. `(not (string-match ,pv (or ,gv "")))
  10554. `(string-match ,pv (or ,gv "")))
  10555. (if str-p
  10556. `(,po (or ,gv "") ,pv)
  10557. `(,po (string-to-number (or ,gv ""))
  10558. ,(string-to-number pv) ))))
  10559. (t `(member ,tag tags-list)))
  10560. mm (if minus (list 'not mm) mm)
  10561. term rest)
  10562. (push mm tagsmatcher))
  10563. (push (if (> (length tagsmatcher) 1)
  10564. (cons 'and tagsmatcher)
  10565. (car tagsmatcher))
  10566. orlist)
  10567. (setq tagsmatcher nil))
  10568. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10569. (setq tagsmatcher
  10570. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10571. ;; Make the todo matcher
  10572. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10573. (setq todomatcher t)
  10574. (setq orterms (org-split-string todomatch "|") orlist nil)
  10575. (while (setq term (pop orterms))
  10576. (while (string-match re term)
  10577. (setq minus (and (match-end 1)
  10578. (equal (match-string 1 term) "-"))
  10579. kwd (match-string 2 term)
  10580. re-p (equal (string-to-char kwd) ?{)
  10581. term (substring term (match-end 0))
  10582. mm (if re-p
  10583. `(string-match ,(substring kwd 1 -1) todo)
  10584. (list 'equal 'todo kwd))
  10585. mm (if minus (list 'not mm) mm))
  10586. (push mm todomatcher))
  10587. (push (if (> (length todomatcher) 1)
  10588. (cons 'and todomatcher)
  10589. (car todomatcher))
  10590. orlist)
  10591. (setq todomatcher nil))
  10592. (setq todomatcher (if (> (length orlist) 1)
  10593. (cons 'or orlist) (car orlist))))
  10594. ;; Return the string and lisp forms of the matcher
  10595. (setq matcher (if todomatcher
  10596. (list 'and tagsmatcher todomatcher)
  10597. tagsmatcher))
  10598. (cons match0 matcher)))
  10599. (defun org-op-to-function (op &optional stringp)
  10600. "Turn an operator into the appropriate function."
  10601. (setq op
  10602. (cond
  10603. ((equal op "<" ) '(< string< org-time<))
  10604. ((equal op ">" ) '(> org-string> org-time>))
  10605. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10606. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10607. ((member op '("=" "==")) '(= string= org-time=))
  10608. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10609. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10610. (defun org<> (a b) (not (= a b)))
  10611. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10612. (defun org-string>= (a b) (not (string< a b)))
  10613. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10614. (defun org-string<> (a b) (not (string= a b)))
  10615. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10616. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10617. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10618. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10619. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10620. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10621. (defun org-2ft (s)
  10622. "Convert S to a floating point time.
  10623. If S is already a number, just return it. If it is a string, parse
  10624. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10625. (cond
  10626. ((numberp s) s)
  10627. ((stringp s)
  10628. (condition-case nil
  10629. (float-time (apply 'encode-time (org-parse-time-string s)))
  10630. (error 0.)))
  10631. (t 0.)))
  10632. (defun org-time-today ()
  10633. "Time in seconds today at 0:00.
  10634. Returns the float number of seconds since the beginning of the
  10635. epoch to the beginning of today (00:00)."
  10636. (float-time (apply 'encode-time
  10637. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10638. (defun org-matcher-time (s)
  10639. "Interpret a time comparison value."
  10640. (save-match-data
  10641. (cond
  10642. ((string= s "<now>") (float-time))
  10643. ((string= s "<today>") (org-time-today))
  10644. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10645. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10646. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10647. (+ (org-time-today)
  10648. (* (string-to-number (match-string 1 s))
  10649. (cdr (assoc (match-string 2 s)
  10650. '(("d" . 86400.0) ("w" . 604800.0)
  10651. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10652. (t (org-2ft s)))))
  10653. (defun org-match-any-p (re list)
  10654. "Does re match any element of list?"
  10655. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10656. (delq nil list))
  10657. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10658. (defvar org-tags-overlay (org-make-overlay 1 1))
  10659. (org-detach-overlay org-tags-overlay)
  10660. (defun org-get-local-tags-at (&optional pos)
  10661. "Get a list of tags defined in the current headline."
  10662. (org-get-tags-at pos 'local))
  10663. (defun org-get-local-tags ()
  10664. "Get a list of tags defined in the current headline."
  10665. (org-get-tags-at nil 'local))
  10666. (defun org-get-tags-at (&optional pos local)
  10667. "Get a list of all headline tags applicable at POS.
  10668. POS defaults to point. If tags are inherited, the list contains
  10669. the targets in the same sequence as the headlines appear, i.e.
  10670. the tags of the current headline come last.
  10671. When LOCAL is non-nil, only return tags from the current headline,
  10672. ignore inherited ones."
  10673. (interactive)
  10674. (if (and org-trust-scanner-tags
  10675. (or (not pos) (equal pos (point)))
  10676. (not local))
  10677. org-scanner-tags
  10678. (let (tags ltags lastpos parent)
  10679. (save-excursion
  10680. (save-restriction
  10681. (widen)
  10682. (goto-char (or pos (point)))
  10683. (save-match-data
  10684. (catch 'done
  10685. (condition-case nil
  10686. (progn
  10687. (org-back-to-heading t)
  10688. (while (not (equal lastpos (point)))
  10689. (setq lastpos (point))
  10690. (when (looking-at
  10691. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10692. (setq ltags (org-split-string
  10693. (org-match-string-no-properties 1) ":"))
  10694. (when parent
  10695. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10696. (setq tags (append
  10697. (if parent
  10698. (org-remove-uniherited-tags ltags)
  10699. ltags)
  10700. tags)))
  10701. (or org-use-tag-inheritance (throw 'done t))
  10702. (if local (throw 'done t))
  10703. (or (org-up-heading-safe) (error nil))
  10704. (setq parent t)))
  10705. (error nil)))))
  10706. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10707. (defun org-add-prop-inherited (s)
  10708. (add-text-properties 0 (length s) '(inherited t) s)
  10709. s)
  10710. (defun org-toggle-tag (tag &optional onoff)
  10711. "Toggle the tag TAG for the current line.
  10712. If ONOFF is `on' or `off', don't toggle but set to this state."
  10713. (let (res current)
  10714. (save-excursion
  10715. (org-back-to-heading t)
  10716. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10717. (point-at-eol) t)
  10718. (progn
  10719. (setq current (match-string 1))
  10720. (replace-match ""))
  10721. (setq current ""))
  10722. (setq current (nreverse (org-split-string current ":")))
  10723. (cond
  10724. ((eq onoff 'on)
  10725. (setq res t)
  10726. (or (member tag current) (push tag current)))
  10727. ((eq onoff 'off)
  10728. (or (not (member tag current)) (setq current (delete tag current))))
  10729. (t (if (member tag current)
  10730. (setq current (delete tag current))
  10731. (setq res t)
  10732. (push tag current))))
  10733. (end-of-line 1)
  10734. (if current
  10735. (progn
  10736. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10737. (org-set-tags nil t))
  10738. (delete-horizontal-space))
  10739. (run-hooks 'org-after-tags-change-hook))
  10740. res))
  10741. (defun org-align-tags-here (to-col)
  10742. ;; Assumes that this is a headline
  10743. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10744. (beginning-of-line 1)
  10745. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10746. (< pos (match-beginning 2)))
  10747. (progn
  10748. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10749. (goto-char (match-beginning 1))
  10750. (insert " ")
  10751. (delete-region (point) (1+ (match-beginning 2)))
  10752. (setq ncol (max (1+ (current-column))
  10753. (1+ col)
  10754. (if (> to-col 0)
  10755. to-col
  10756. (- (abs to-col) tags-l))))
  10757. (setq p (point))
  10758. (insert (make-string (- ncol (current-column)) ?\ ))
  10759. (setq ncol (current-column))
  10760. (when indent-tabs-mode (tabify p (point-at-eol)))
  10761. (org-move-to-column (min ncol col) t))
  10762. (goto-char pos))))
  10763. (defun org-set-tags-command (&optional arg just-align)
  10764. "Call the set-tags command for the current entry."
  10765. (interactive "P")
  10766. (if (org-on-heading-p)
  10767. (org-set-tags arg just-align)
  10768. (save-excursion
  10769. (org-back-to-heading t)
  10770. (org-set-tags arg just-align))))
  10771. (defun org-set-tags-to (data)
  10772. "Set the tags of the current entry to DATA, replacing the current tags.
  10773. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10774. If DATA is nil or the empty string, any tags will be removed."
  10775. (interactive "sTags: ")
  10776. (setq data
  10777. (cond
  10778. ((eq data nil) "")
  10779. ((equal data "") "")
  10780. ((stringp data)
  10781. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10782. ":"))
  10783. ((listp data)
  10784. (concat ":" (mapconcat 'identity data ":") ":"))
  10785. (t nil)))
  10786. (when data
  10787. (save-excursion
  10788. (org-back-to-heading t)
  10789. (when (looking-at org-complex-heading-regexp)
  10790. (if (match-end 5)
  10791. (progn
  10792. (goto-char (match-beginning 5))
  10793. (insert data)
  10794. (delete-region (point) (point-at-eol))
  10795. (org-set-tags nil 'align))
  10796. (goto-char (point-at-eol))
  10797. (insert " " data)
  10798. (org-set-tags nil 'align)))
  10799. (beginning-of-line 1)
  10800. (if (looking-at ".*?\\([ \t]+\\)$")
  10801. (delete-region (match-beginning 1) (match-end 1))))))
  10802. (defun org-set-tags (&optional arg just-align)
  10803. "Set the tags for the current headline.
  10804. With prefix ARG, realign all tags in headings in the current buffer."
  10805. (interactive "P")
  10806. (let* ((re (concat "^" outline-regexp))
  10807. (current (org-get-tags-string))
  10808. (col (current-column))
  10809. (org-setting-tags t)
  10810. table current-tags inherited-tags ; computed below when needed
  10811. tags p0 c0 c1 rpl)
  10812. (if arg
  10813. (save-excursion
  10814. (goto-char (point-min))
  10815. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10816. (while (re-search-forward re nil t)
  10817. (org-set-tags nil t)
  10818. (end-of-line 1)))
  10819. (message "All tags realigned to column %d" org-tags-column))
  10820. (if just-align
  10821. (setq tags current)
  10822. ;; Get a new set of tags from the user
  10823. (save-excursion
  10824. (setq table (append org-tag-persistent-alist
  10825. (or org-tag-alist (org-get-buffer-tags))
  10826. (and org-complete-tags-always-offer-all-agenda-tags
  10827. (org-global-tags-completion-table (org-agenda-files))))
  10828. org-last-tags-completion-table table
  10829. current-tags (org-split-string current ":")
  10830. inherited-tags (nreverse
  10831. (nthcdr (length current-tags)
  10832. (nreverse (org-get-tags-at))))
  10833. tags
  10834. (if (or (eq t org-use-fast-tag-selection)
  10835. (and org-use-fast-tag-selection
  10836. (delq nil (mapcar 'cdr table))))
  10837. (org-fast-tag-selection
  10838. current-tags inherited-tags table
  10839. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10840. (let ((org-add-colon-after-tag-completion t))
  10841. (org-trim
  10842. (org-without-partial-completion
  10843. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10844. nil nil current 'org-tags-history)))))))
  10845. (while (string-match "[-+&]+" tags)
  10846. ;; No boolean logic, just a list
  10847. (setq tags (replace-match ":" t t tags))))
  10848. (if org-tags-sort-function
  10849. (setq tags (mapconcat 'identity
  10850. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10851. org-tags-sort-function) ":")))
  10852. (if (string-match "\\`[\t ]*\\'" tags)
  10853. (setq tags "")
  10854. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10855. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10856. ;; Insert new tags at the correct column
  10857. (beginning-of-line 1)
  10858. (cond
  10859. ((and (equal current "") (equal tags "")))
  10860. ((re-search-forward
  10861. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10862. (point-at-eol) t)
  10863. (if (equal tags "")
  10864. (setq rpl "")
  10865. (goto-char (match-beginning 0))
  10866. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  10867. (1+ (point)) (point))
  10868. c1 (max (1+ c0) (if (> org-tags-column 0)
  10869. org-tags-column
  10870. (- (- org-tags-column) (length tags))))
  10871. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10872. (replace-match rpl t t)
  10873. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10874. tags)
  10875. (t (error "Tags alignment failed")))
  10876. (org-move-to-column col)
  10877. (unless just-align
  10878. (run-hooks 'org-after-tags-change-hook)))))
  10879. (defun org-change-tag-in-region (beg end tag off)
  10880. "Add or remove TAG for each entry in the region.
  10881. This works in the agenda, and also in an org-mode buffer."
  10882. (interactive
  10883. (list (region-beginning) (region-end)
  10884. (let ((org-last-tags-completion-table
  10885. (if (org-mode-p)
  10886. (org-get-buffer-tags)
  10887. (org-global-tags-completion-table))))
  10888. (org-icompleting-read
  10889. "Tag: " 'org-tags-completion-function nil nil nil
  10890. 'org-tags-history))
  10891. (progn
  10892. (message "[s]et or [r]emove? ")
  10893. (equal (read-char-exclusive) ?r))))
  10894. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10895. (let ((agendap (equal major-mode 'org-agenda-mode))
  10896. l1 l2 m buf pos newhead (cnt 0))
  10897. (goto-char end)
  10898. (setq l2 (1- (org-current-line)))
  10899. (goto-char beg)
  10900. (setq l1 (org-current-line))
  10901. (loop for l from l1 to l2 do
  10902. (org-goto-line l)
  10903. (setq m (get-text-property (point) 'org-hd-marker))
  10904. (when (or (and (org-mode-p) (org-on-heading-p))
  10905. (and agendap m))
  10906. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10907. pos (if agendap m (point)))
  10908. (with-current-buffer buf
  10909. (save-excursion
  10910. (save-restriction
  10911. (goto-char pos)
  10912. (setq cnt (1+ cnt))
  10913. (org-toggle-tag tag (if off 'off 'on))
  10914. (setq newhead (org-get-heading)))))
  10915. (and agendap (org-agenda-change-all-lines newhead m))))
  10916. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10917. (defun org-tags-completion-function (string predicate &optional flag)
  10918. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10919. (confirm (lambda (x) (stringp (car x)))))
  10920. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10921. (setq s1 (match-string 1 string)
  10922. s2 (match-string 2 string))
  10923. (setq s1 "" s2 string))
  10924. (cond
  10925. ((eq flag nil)
  10926. ;; try completion
  10927. (setq rtn (try-completion s2 ctable confirm))
  10928. (if (stringp rtn)
  10929. (setq rtn
  10930. (concat s1 s2 (substring rtn (length s2))
  10931. (if (and org-add-colon-after-tag-completion
  10932. (assoc rtn ctable))
  10933. ":" ""))))
  10934. rtn)
  10935. ((eq flag t)
  10936. ;; all-completions
  10937. (all-completions s2 ctable confirm)
  10938. )
  10939. ((eq flag 'lambda)
  10940. ;; exact match?
  10941. (assoc s2 ctable)))
  10942. ))
  10943. (defun org-fast-tag-insert (kwd tags face &optional end)
  10944. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10945. (insert (format "%-12s" (concat kwd ":"))
  10946. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10947. (or end "")))
  10948. (defun org-fast-tag-show-exit (flag)
  10949. (save-excursion
  10950. (org-goto-line 3)
  10951. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10952. (replace-match ""))
  10953. (when flag
  10954. (end-of-line 1)
  10955. (org-move-to-column (- (window-width) 19) t)
  10956. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10957. (defun org-set-current-tags-overlay (current prefix)
  10958. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10959. (if (featurep 'xemacs)
  10960. (org-overlay-display org-tags-overlay (concat prefix s)
  10961. 'secondary-selection)
  10962. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10963. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10964. (defvar org-last-tag-selection-key nil)
  10965. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10966. "Fast tag selection with single keys.
  10967. CURRENT is the current list of tags in the headline, INHERITED is the
  10968. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10969. possibly with grouping information. TODO-TABLE is a similar table with
  10970. TODO keywords, should these have keys assigned to them.
  10971. If the keys are nil, a-z are automatically assigned.
  10972. Returns the new tags string, or nil to not change the current settings."
  10973. (let* ((fulltable (append table todo-table))
  10974. (maxlen (apply 'max (mapcar
  10975. (lambda (x)
  10976. (if (stringp (car x)) (string-width (car x)) 0))
  10977. fulltable)))
  10978. (buf (current-buffer))
  10979. (expert (eq org-fast-tag-selection-single-key 'expert))
  10980. (buffer-tags nil)
  10981. (fwidth (+ maxlen 3 1 3))
  10982. (ncol (/ (- (window-width) 4) fwidth))
  10983. (i-face 'org-done)
  10984. (c-face 'org-todo)
  10985. tg cnt e c char c1 c2 ntable tbl rtn
  10986. ov-start ov-end ov-prefix
  10987. (exit-after-next org-fast-tag-selection-single-key)
  10988. (done-keywords org-done-keywords)
  10989. groups ingroup)
  10990. (save-excursion
  10991. (beginning-of-line 1)
  10992. (if (looking-at
  10993. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10994. (setq ov-start (match-beginning 1)
  10995. ov-end (match-end 1)
  10996. ov-prefix "")
  10997. (setq ov-start (1- (point-at-eol))
  10998. ov-end (1+ ov-start))
  10999. (skip-chars-forward "^\n\r")
  11000. (setq ov-prefix
  11001. (concat
  11002. (buffer-substring (1- (point)) (point))
  11003. (if (> (current-column) org-tags-column)
  11004. " "
  11005. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11006. (org-move-overlay org-tags-overlay ov-start ov-end)
  11007. (save-window-excursion
  11008. (if expert
  11009. (set-buffer (get-buffer-create " *Org tags*"))
  11010. (delete-other-windows)
  11011. (split-window-vertically)
  11012. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11013. (erase-buffer)
  11014. (org-set-local 'org-done-keywords done-keywords)
  11015. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11016. (org-fast-tag-insert "Current" current c-face "\n\n")
  11017. (org-fast-tag-show-exit exit-after-next)
  11018. (org-set-current-tags-overlay current ov-prefix)
  11019. (setq tbl fulltable char ?a cnt 0)
  11020. (while (setq e (pop tbl))
  11021. (cond
  11022. ((equal (car e) :startgroup)
  11023. (push '() groups) (setq ingroup t)
  11024. (when (not (= cnt 0))
  11025. (setq cnt 0)
  11026. (insert "\n"))
  11027. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11028. ((equal (car e) :endgroup)
  11029. (setq ingroup nil cnt 0)
  11030. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11031. ((equal e '(:newline))
  11032. (when (not (= cnt 0))
  11033. (setq cnt 0)
  11034. (insert "\n")
  11035. (setq e (car tbl))
  11036. (while (equal (car tbl) '(:newline))
  11037. (insert "\n")
  11038. (setq tbl (cdr tbl)))))
  11039. (t
  11040. (setq tg (copy-sequence (car e)) c2 nil)
  11041. (if (cdr e)
  11042. (setq c (cdr e))
  11043. ;; automatically assign a character.
  11044. (setq c1 (string-to-char
  11045. (downcase (substring
  11046. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11047. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11048. (while (or (rassoc char ntable) (rassoc char table))
  11049. (setq char (1+ char)))
  11050. (setq c2 c1))
  11051. (setq c (or c2 char)))
  11052. (if ingroup (push tg (car groups)))
  11053. (setq tg (org-add-props tg nil 'face
  11054. (cond
  11055. ((not (assoc tg table))
  11056. (org-get-todo-face tg))
  11057. ((member tg current) c-face)
  11058. ((member tg inherited) i-face)
  11059. (t nil))))
  11060. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11061. (insert "[" c "] " tg (make-string
  11062. (- fwidth 4 (length tg)) ?\ ))
  11063. (push (cons tg c) ntable)
  11064. (when (= (setq cnt (1+ cnt)) ncol)
  11065. (insert "\n")
  11066. (if ingroup (insert " "))
  11067. (setq cnt 0)))))
  11068. (setq ntable (nreverse ntable))
  11069. (insert "\n")
  11070. (goto-char (point-min))
  11071. (if (not expert) (org-fit-window-to-buffer))
  11072. (setq rtn
  11073. (catch 'exit
  11074. (while t
  11075. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11076. (if (not groups) "no " "")
  11077. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11078. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11079. (setq org-last-tag-selection-key c)
  11080. (cond
  11081. ((= c ?\r) (throw 'exit t))
  11082. ((= c ?!)
  11083. (setq groups (not groups))
  11084. (goto-char (point-min))
  11085. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11086. ((= c ?\C-c)
  11087. (if (not expert)
  11088. (org-fast-tag-show-exit
  11089. (setq exit-after-next (not exit-after-next)))
  11090. (setq expert nil)
  11091. (delete-other-windows)
  11092. (split-window-vertically)
  11093. (org-switch-to-buffer-other-window " *Org tags*")
  11094. (org-fit-window-to-buffer)))
  11095. ((or (= c ?\C-g)
  11096. (and (= c ?q) (not (rassoc c ntable))))
  11097. (org-detach-overlay org-tags-overlay)
  11098. (setq quit-flag t))
  11099. ((= c ?\ )
  11100. (setq current nil)
  11101. (if exit-after-next (setq exit-after-next 'now)))
  11102. ((= c ?\t)
  11103. (condition-case nil
  11104. (setq tg (org-icompleting-read
  11105. "Tag: "
  11106. (or buffer-tags
  11107. (with-current-buffer buf
  11108. (org-get-buffer-tags)))))
  11109. (quit (setq tg "")))
  11110. (when (string-match "\\S-" tg)
  11111. (add-to-list 'buffer-tags (list tg))
  11112. (if (member tg current)
  11113. (setq current (delete tg current))
  11114. (push tg current)))
  11115. (if exit-after-next (setq exit-after-next 'now)))
  11116. ((setq e (rassoc c todo-table) tg (car e))
  11117. (with-current-buffer buf
  11118. (save-excursion (org-todo tg)))
  11119. (if exit-after-next (setq exit-after-next 'now)))
  11120. ((setq e (rassoc c ntable) tg (car e))
  11121. (if (member tg current)
  11122. (setq current (delete tg current))
  11123. (loop for g in groups do
  11124. (if (member tg g)
  11125. (mapc (lambda (x)
  11126. (setq current (delete x current)))
  11127. g)))
  11128. (push tg current))
  11129. (if exit-after-next (setq exit-after-next 'now))))
  11130. ;; Create a sorted list
  11131. (setq current
  11132. (sort current
  11133. (lambda (a b)
  11134. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11135. (if (eq exit-after-next 'now) (throw 'exit t))
  11136. (goto-char (point-min))
  11137. (beginning-of-line 2)
  11138. (delete-region (point) (point-at-eol))
  11139. (org-fast-tag-insert "Current" current c-face)
  11140. (org-set-current-tags-overlay current ov-prefix)
  11141. (while (re-search-forward
  11142. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11143. (setq tg (match-string 1))
  11144. (add-text-properties
  11145. (match-beginning 1) (match-end 1)
  11146. (list 'face
  11147. (cond
  11148. ((member tg current) c-face)
  11149. ((member tg inherited) i-face)
  11150. (t (get-text-property (match-beginning 1) 'face))))))
  11151. (goto-char (point-min)))))
  11152. (org-detach-overlay org-tags-overlay)
  11153. (if rtn
  11154. (mapconcat 'identity current ":")
  11155. nil))))
  11156. (defun org-get-tags-string ()
  11157. "Get the TAGS string in the current headline."
  11158. (unless (org-on-heading-p t)
  11159. (error "Not on a heading"))
  11160. (save-excursion
  11161. (beginning-of-line 1)
  11162. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11163. (org-match-string-no-properties 1)
  11164. "")))
  11165. (defun org-get-tags ()
  11166. "Get the list of tags specified in the current headline."
  11167. (org-split-string (org-get-tags-string) ":"))
  11168. (defun org-get-buffer-tags ()
  11169. "Get a table of all tags used in the buffer, for completion."
  11170. (let (tags)
  11171. (save-excursion
  11172. (goto-char (point-min))
  11173. (while (re-search-forward
  11174. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11175. (when (equal (char-after (point-at-bol 0)) ?*)
  11176. (mapc (lambda (x) (add-to-list 'tags x))
  11177. (org-split-string (org-match-string-no-properties 1) ":")))))
  11178. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11179. (mapcar 'list tags)))
  11180. ;;;; The mapping API
  11181. ;;;###autoload
  11182. (defun org-map-entries (func &optional match scope &rest skip)
  11183. "Call FUNC at each headline selected by MATCH in SCOPE.
  11184. FUNC is a function or a lisp form. The function will be called without
  11185. arguments, with the cursor positioned at the beginning of the headline.
  11186. The return values of all calls to the function will be collected and
  11187. returned as a list.
  11188. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11189. does not need to preserve point. After evaluation, the cursor will be
  11190. moved to the end of the line (presumably of the headline of the
  11191. processed entry) and search continues from there. Under some
  11192. circumstances, this may not produce the wanted results. For example,
  11193. if you have removed (e.g. archived) the current (sub)tree it could
  11194. mean that the next entry will be skipped entirely. In such cases, you
  11195. can specify the position from where search should continue by making
  11196. FUNC set the variable `org-map-continue-from' to the desired buffer
  11197. position.
  11198. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11199. Only headlines that are matched by this query will be considered during
  11200. the iteration. When MATCH is nil or t, all headlines will be
  11201. visited by the iteration.
  11202. SCOPE determines the scope of this command. It can be any of:
  11203. nil The current buffer, respecting the restriction if any
  11204. tree The subtree started with the entry at point
  11205. file The current buffer, without restriction
  11206. file-with-archives
  11207. The current buffer, and any archives associated with it
  11208. agenda All agenda files
  11209. agenda-with-archives
  11210. All agenda files with any archive files associated with them
  11211. \(file1 file2 ...)
  11212. If this is a list, all files in the list will be scanned
  11213. The remaining args are treated as settings for the skipping facilities of
  11214. the scanner. The following items can be given here:
  11215. archive skip trees with the archive tag.
  11216. comment skip trees with the COMMENT keyword
  11217. function or Emacs Lisp form:
  11218. will be used as value for `org-agenda-skip-function', so whenever
  11219. the function returns t, FUNC will not be called for that
  11220. entry and search will continue from the point where the
  11221. function leaves it.
  11222. If your function needs to retrieve the tags including inherited tags
  11223. at the *current* entry, you can use the value of the variable
  11224. `org-scanner-tags' which will be much faster than getting the value
  11225. with `org-get-tags-at'. If your function gets properties with
  11226. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11227. to t around the call to `org-entry-properties' to get the same speedup.
  11228. Note that if your function moves around to retrieve tags and properties at
  11229. a *different* entry, you cannot use these techniques."
  11230. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11231. (org-agenda-skip-archived-trees (memq 'archive skip))
  11232. (org-agenda-skip-comment-trees (memq 'comment skip))
  11233. (org-agenda-skip-function
  11234. (car (org-delete-all '(comment archive) skip)))
  11235. (org-tags-match-list-sublevels t)
  11236. matcher file res
  11237. org-todo-keywords-for-agenda
  11238. org-done-keywords-for-agenda
  11239. org-todo-keyword-alist-for-agenda
  11240. org-drawers-for-agenda
  11241. org-tag-alist-for-agenda)
  11242. (cond
  11243. ((eq match t) (setq matcher t))
  11244. ((eq match nil) (setq matcher t))
  11245. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11246. (save-excursion
  11247. (save-restriction
  11248. (when (eq scope 'tree)
  11249. (org-back-to-heading t)
  11250. (org-narrow-to-subtree)
  11251. (setq scope nil))
  11252. (if (not scope)
  11253. (progn
  11254. (org-prepare-agenda-buffers
  11255. (list (buffer-file-name (current-buffer))))
  11256. (setq res (org-scan-tags func matcher)))
  11257. ;; Get the right scope
  11258. (cond
  11259. ((and scope (listp scope) (symbolp (car scope)))
  11260. (setq scope (eval scope)))
  11261. ((eq scope 'agenda)
  11262. (setq scope (org-agenda-files t)))
  11263. ((eq scope 'agenda-with-archives)
  11264. (setq scope (org-agenda-files t))
  11265. (setq scope (org-add-archive-files scope)))
  11266. ((eq scope 'file)
  11267. (setq scope (list (buffer-file-name))))
  11268. ((eq scope 'file-with-archives)
  11269. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11270. (org-prepare-agenda-buffers scope)
  11271. (while (setq file (pop scope))
  11272. (with-current-buffer (org-find-base-buffer-visiting file)
  11273. (save-excursion
  11274. (save-restriction
  11275. (widen)
  11276. (goto-char (point-min))
  11277. (setq res (append res (org-scan-tags func matcher))))))))))
  11278. res))
  11279. ;;;; Properties
  11280. ;;; Setting and retrieving properties
  11281. (defconst org-special-properties
  11282. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11283. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11284. "The special properties valid in Org-mode.
  11285. These are properties that are not defined in the property drawer,
  11286. but in some other way.")
  11287. (defconst org-default-properties
  11288. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11289. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11290. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11291. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11292. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  11293. "CLOCK_MODELINE_TOTAL" "STYLE")
  11294. "Some properties that are used by Org-mode for various purposes.
  11295. Being in this list makes sure that they are offered for completion.")
  11296. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11297. "Regular expression matching the first line of a property drawer.")
  11298. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11299. "Regular expression matching the first line of a property drawer.")
  11300. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11301. "Regular expression matching the first line of a property drawer.")
  11302. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11303. "Regular expression matching the first line of a property drawer.")
  11304. (defconst org-property-drawer-re
  11305. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11306. org-property-end-re "\\)\n?")
  11307. "Matches an entire property drawer.")
  11308. (defconst org-clock-drawer-re
  11309. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11310. org-property-end-re "\\)\n?")
  11311. "Matches an entire clock drawer.")
  11312. (defun org-property-action ()
  11313. "Do an action on properties."
  11314. (interactive)
  11315. (let (c)
  11316. (org-at-property-p)
  11317. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11318. (setq c (read-char-exclusive))
  11319. (cond
  11320. ((equal c ?s)
  11321. (call-interactively 'org-set-property))
  11322. ((equal c ?d)
  11323. (call-interactively 'org-delete-property))
  11324. ((equal c ?D)
  11325. (call-interactively 'org-delete-property-globally))
  11326. ((equal c ?c)
  11327. (call-interactively 'org-compute-property-at-point))
  11328. (t (error "No such property action %c" c)))))
  11329. (defun org-set-effort (&optional value)
  11330. "Set the effort property of the current entry.
  11331. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11332. allowed value."
  11333. (interactive "P")
  11334. (if (equal value 0) (setq value 10))
  11335. (let* ((completion-ignore-case t)
  11336. (prop org-effort-property)
  11337. (cur (org-entry-get nil prop))
  11338. (allowed (org-property-get-allowed-values nil prop 'table))
  11339. (existing (mapcar 'list (org-property-values prop)))
  11340. rpl
  11341. (val (cond
  11342. ((stringp value) value)
  11343. ((and allowed (integerp value))
  11344. (or (car (nth (1- value) allowed))
  11345. (car (org-last allowed))))
  11346. (allowed
  11347. (message "Select 1-9,0, [RET%s]: %s"
  11348. (if cur (concat "=" cur) "")
  11349. (mapconcat 'car allowed " "))
  11350. (setq rpl (read-char-exclusive))
  11351. (if (equal rpl ?\r)
  11352. cur
  11353. (setq rpl (- rpl ?0))
  11354. (if (equal rpl 0) (setq rpl 10))
  11355. (if (and (> rpl 0) (<= rpl (length allowed)))
  11356. (car (nth (1- rpl) allowed))
  11357. (org-completing-read "Effort: " allowed nil))))
  11358. (t
  11359. (let (org-completion-use-ido org-completion-use-iswitchb)
  11360. (org-completing-read
  11361. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11362. (concat "[" cur "]") "")
  11363. ": ")
  11364. existing nil nil "" nil cur))))))
  11365. (unless (equal (org-entry-get nil prop) val)
  11366. (org-entry-put nil prop val))
  11367. (message "%s is now %s" prop val)))
  11368. (defun org-at-property-p ()
  11369. "Is the cursor in a property line?"
  11370. ;; FIXME: Does not check if we are actually in the drawer.
  11371. ;; FIXME: also returns true on any drawers.....
  11372. ;; This is used by C-c C-c for property action.
  11373. (save-excursion
  11374. (beginning-of-line 1)
  11375. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  11376. (defun org-get-property-block (&optional beg end force)
  11377. "Return the (beg . end) range of the body of the property drawer.
  11378. BEG and END can be beginning and end of subtree, if not given
  11379. they will be found.
  11380. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11381. (catch 'exit
  11382. (save-excursion
  11383. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11384. (end (or end (progn (outline-next-heading) (point)))))
  11385. (goto-char beg)
  11386. (if (re-search-forward org-property-start-re end t)
  11387. (setq beg (1+ (match-end 0)))
  11388. (if force
  11389. (save-excursion
  11390. (org-insert-property-drawer)
  11391. (setq end (progn (outline-next-heading) (point))))
  11392. (throw 'exit nil))
  11393. (goto-char beg)
  11394. (if (re-search-forward org-property-start-re end t)
  11395. (setq beg (1+ (match-end 0)))))
  11396. (if (re-search-forward org-property-end-re end t)
  11397. (setq end (match-beginning 0))
  11398. (or force (throw 'exit nil))
  11399. (goto-char beg)
  11400. (setq end beg)
  11401. (org-indent-line-function)
  11402. (insert ":END:\n"))
  11403. (cons beg end)))))
  11404. (defun org-entry-properties (&optional pom which specific)
  11405. "Get all properties of the entry at point-or-marker POM.
  11406. This includes the TODO keyword, the tags, time strings for deadline,
  11407. scheduled, and clocking, and any additional properties defined in the
  11408. entry. The return value is an alist, keys may occur multiple times
  11409. if the property key was used several times.
  11410. POM may also be nil, in which case the current entry is used.
  11411. If WHICH is nil or `all', get all properties. If WHICH is
  11412. `special' or `standard', only get that subclass. If WHICH
  11413. is a string only get exactly this property. Specific can be a string, the
  11414. specific property we are interested in. Specifying it can speed
  11415. things up because then unnecessary parsing is avoided."
  11416. (setq which (or which 'all))
  11417. (org-with-point-at pom
  11418. (let ((clockstr (substring org-clock-string 0 -1))
  11419. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11420. (case-fold-search nil)
  11421. beg end range props sum-props key value string clocksum)
  11422. (save-excursion
  11423. (when (condition-case nil
  11424. (and (org-mode-p) (org-back-to-heading t))
  11425. (error nil))
  11426. (setq beg (point))
  11427. (setq sum-props (get-text-property (point) 'org-summaries))
  11428. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11429. (outline-next-heading)
  11430. (setq end (point))
  11431. (when (memq which '(all special))
  11432. ;; Get the special properties, like TODO and tags
  11433. (goto-char beg)
  11434. (when (and (or (not specific) (string= specific "TODO"))
  11435. (looking-at org-todo-line-regexp) (match-end 2))
  11436. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11437. (when (and (or (not specific) (string= specific "PRIORITY"))
  11438. (looking-at org-priority-regexp))
  11439. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11440. (when (and (or (not specific) (string= specific "TAGS"))
  11441. (setq value (org-get-tags-string))
  11442. (string-match "\\S-" value))
  11443. (push (cons "TAGS" value) props))
  11444. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11445. (setq value (org-get-tags-at)))
  11446. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11447. ":"))
  11448. props))
  11449. (when (or (not specific) (string= specific "BLOCKED"))
  11450. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11451. (when (or (not specific)
  11452. (member specific org-all-time-keywords)
  11453. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11454. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11455. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11456. string (if (equal key clockstr)
  11457. (org-no-properties
  11458. (org-trim
  11459. (buffer-substring
  11460. (match-beginning 3) (goto-char (point-at-eol)))))
  11461. (substring (org-match-string-no-properties 3) 1 -1)))
  11462. (unless key
  11463. (if (= (char-after (match-beginning 3)) ?\[)
  11464. (setq key "TIMESTAMP_IA")
  11465. (setq key "TIMESTAMP")))
  11466. (when (or (equal key clockstr) (not (assoc key props)))
  11467. (push (cons key string) props))))
  11468. )
  11469. (when (memq which '(all standard))
  11470. ;; Get the standard properties, like :PROP: ...
  11471. (setq range (org-get-property-block beg end))
  11472. (when range
  11473. (goto-char (car range))
  11474. (while (re-search-forward
  11475. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11476. (cdr range) t)
  11477. (setq key (org-match-string-no-properties 1)
  11478. value (org-trim (or (org-match-string-no-properties 2) "")))
  11479. (unless (member key excluded)
  11480. (push (cons key (or value "")) props)))))
  11481. (if clocksum
  11482. (push (cons "CLOCKSUM"
  11483. (org-columns-number-to-string (/ (float clocksum) 60.)
  11484. 'add_times))
  11485. props))
  11486. (unless (assoc "CATEGORY" props)
  11487. (setq value (or (org-get-category)
  11488. (progn (org-refresh-category-properties)
  11489. (org-get-category))))
  11490. (push (cons "CATEGORY" value) props))
  11491. (append sum-props (nreverse props)))))))
  11492. (defun org-entry-get (pom property &optional inherit)
  11493. "Get value of PROPERTY for entry at point-or-marker POM.
  11494. If INHERIT is non-nil and the entry does not have the property,
  11495. then also check higher levels of the hierarchy.
  11496. If INHERIT is the symbol `selective', use inheritance only if the setting
  11497. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11498. If the property is present but empty, the return value is the empty string.
  11499. If the property is not present at all, nil is returned."
  11500. (org-with-point-at pom
  11501. (if (and inherit (if (eq inherit 'selective)
  11502. (org-property-inherit-p property)
  11503. t))
  11504. (org-entry-get-with-inheritance property)
  11505. (if (member property org-special-properties)
  11506. ;; We need a special property. Use `org-entry-properties' to
  11507. ;; retrieve it, but specify the wanted property
  11508. (cdr (assoc property (org-entry-properties nil 'special property)))
  11509. (let ((range (org-get-property-block)))
  11510. (if (and range
  11511. (goto-char (car range))
  11512. (re-search-forward
  11513. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11514. (cdr range) t))
  11515. ;; Found the property, return it.
  11516. (if (match-end 1)
  11517. (org-match-string-no-properties 1)
  11518. "")))))))
  11519. (defun org-property-or-variable-value (var &optional inherit)
  11520. "Check if there is a property fixing the value of VAR.
  11521. If yes, return this value. If not, return the current value of the variable."
  11522. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11523. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11524. (read prop)
  11525. (symbol-value var))))
  11526. (defun org-entry-delete (pom property)
  11527. "Delete the property PROPERTY from entry at point-or-marker POM."
  11528. (org-with-point-at pom
  11529. (if (member property org-special-properties)
  11530. nil ; cannot delete these properties.
  11531. (let ((range (org-get-property-block)))
  11532. (if (and range
  11533. (goto-char (car range))
  11534. (re-search-forward
  11535. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11536. (cdr range) t))
  11537. (progn
  11538. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11539. t)
  11540. nil)))))
  11541. ;; Multi-values properties are properties that contain multiple values
  11542. ;; These values are assumed to be single words, separated by whitespace.
  11543. (defun org-entry-add-to-multivalued-property (pom property value)
  11544. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11545. (let* ((old (org-entry-get pom property))
  11546. (values (and old (org-split-string old "[ \t]"))))
  11547. (setq value (org-entry-protect-space value))
  11548. (unless (member value values)
  11549. (setq values (cons value values))
  11550. (org-entry-put pom property
  11551. (mapconcat 'identity values " ")))))
  11552. (defun org-entry-remove-from-multivalued-property (pom property value)
  11553. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11554. (let* ((old (org-entry-get pom property))
  11555. (values (and old (org-split-string old "[ \t]"))))
  11556. (setq value (org-entry-protect-space value))
  11557. (when (member value values)
  11558. (setq values (delete value values))
  11559. (org-entry-put pom property
  11560. (mapconcat 'identity values " ")))))
  11561. (defun org-entry-member-in-multivalued-property (pom property value)
  11562. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11563. (let* ((old (org-entry-get pom property))
  11564. (values (and old (org-split-string old "[ \t]"))))
  11565. (setq value (org-entry-protect-space value))
  11566. (member value values)))
  11567. (defun org-entry-get-multivalued-property (pom property)
  11568. "Return a list of values in a multivalued property."
  11569. (let* ((value (org-entry-get pom property))
  11570. (values (and value (org-split-string value "[ \t]"))))
  11571. (mapcar 'org-entry-restore-space values)))
  11572. (defun org-entry-put-multivalued-property (pom property &rest values)
  11573. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11574. VALUES should be a list of strings. Spaces will be protected."
  11575. (org-entry-put pom property
  11576. (mapconcat 'org-entry-protect-space values " "))
  11577. (let* ((value (org-entry-get pom property))
  11578. (values (and value (org-split-string value "[ \t]"))))
  11579. (mapcar 'org-entry-restore-space values)))
  11580. (defun org-entry-protect-space (s)
  11581. "Protect spaces and newline in string S."
  11582. (while (string-match " " s)
  11583. (setq s (replace-match "%20" t t s)))
  11584. (while (string-match "\n" s)
  11585. (setq s (replace-match "%0A" t t s)))
  11586. s)
  11587. (defun org-entry-restore-space (s)
  11588. "Restore spaces and newline in string S."
  11589. (while (string-match "%20" s)
  11590. (setq s (replace-match " " t t s)))
  11591. (while (string-match "%0A" s)
  11592. (setq s (replace-match "\n" t t s)))
  11593. s)
  11594. (defvar org-entry-property-inherited-from (make-marker)
  11595. "Marker pointing to the entry from where a property was inherited.
  11596. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11597. location of the entry where the inheritance search matched. If there was
  11598. no match, the marker will point nowhere.
  11599. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11600. is set.")
  11601. (defun org-entry-get-with-inheritance (property)
  11602. "Get entry property, and search higher levels if not present."
  11603. (move-marker org-entry-property-inherited-from nil)
  11604. (let (tmp)
  11605. (save-excursion
  11606. (save-restriction
  11607. (widen)
  11608. (catch 'ex
  11609. (while t
  11610. (when (setq tmp (org-entry-get nil property))
  11611. (org-back-to-heading t)
  11612. (move-marker org-entry-property-inherited-from (point))
  11613. (throw 'ex tmp))
  11614. (or (org-up-heading-safe) (throw 'ex nil)))))
  11615. (or tmp
  11616. (cdr (assoc property org-file-properties))
  11617. (cdr (assoc property org-global-properties))
  11618. (cdr (assoc property org-global-properties-fixed))))))
  11619. (defvar org-property-changed-functions nil
  11620. "Hook called when the value of a property has changed.
  11621. Each hook function should accept two arguments, the name of the property
  11622. and the new value.")
  11623. (defun org-entry-put (pom property value)
  11624. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11625. (org-with-point-at pom
  11626. (org-back-to-heading t)
  11627. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11628. range)
  11629. (cond
  11630. ((equal property "TODO")
  11631. (when (and (stringp value) (string-match "\\S-" value)
  11632. (not (member value org-todo-keywords-1)))
  11633. (error "\"%s\" is not a valid TODO state" value))
  11634. (if (or (not value)
  11635. (not (string-match "\\S-" value)))
  11636. (setq value 'none))
  11637. (org-todo value)
  11638. (org-set-tags nil 'align))
  11639. ((equal property "PRIORITY")
  11640. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11641. (string-to-char value) ?\ ))
  11642. (org-set-tags nil 'align))
  11643. ((equal property "SCHEDULED")
  11644. (if (re-search-forward org-scheduled-time-regexp end t)
  11645. (cond
  11646. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11647. ((eq value 'later) (org-timestamp-change 1 'day))
  11648. (t (call-interactively 'org-schedule)))
  11649. (call-interactively 'org-schedule)))
  11650. ((equal property "DEADLINE")
  11651. (if (re-search-forward org-deadline-time-regexp end t)
  11652. (cond
  11653. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11654. ((eq value 'later) (org-timestamp-change 1 'day))
  11655. (t (call-interactively 'org-deadline)))
  11656. (call-interactively 'org-deadline)))
  11657. ((member property org-special-properties)
  11658. (error "The %s property can not yet be set with `org-entry-put'"
  11659. property))
  11660. (t ; a non-special property
  11661. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11662. (setq range (org-get-property-block beg end 'force))
  11663. (goto-char (car range))
  11664. (if (re-search-forward
  11665. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11666. (progn
  11667. (delete-region (match-beginning 1) (match-end 1))
  11668. (goto-char (match-beginning 1)))
  11669. (goto-char (cdr range))
  11670. (insert "\n")
  11671. (backward-char 1)
  11672. (org-indent-line-function)
  11673. (insert ":" property ":"))
  11674. (and value (insert " " value))
  11675. (org-indent-line-function)))))
  11676. (run-hook-with-args 'org-property-changed-functions property value)))
  11677. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11678. "Get all property keys in the current buffer.
  11679. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11680. like tags and TODO state.
  11681. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11682. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11683. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11684. formats in the current buffer."
  11685. (let (rtn range cfmt s p)
  11686. (save-excursion
  11687. (save-restriction
  11688. (widen)
  11689. (goto-char (point-min))
  11690. (while (re-search-forward org-property-start-re nil t)
  11691. (setq range (org-get-property-block))
  11692. (goto-char (car range))
  11693. (while (re-search-forward
  11694. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11695. (cdr range) t)
  11696. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11697. (outline-next-heading))))
  11698. (when include-specials
  11699. (setq rtn (append org-special-properties rtn)))
  11700. (when include-defaults
  11701. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11702. (add-to-list 'rtn org-effort-property))
  11703. (when include-columns
  11704. (save-excursion
  11705. (save-restriction
  11706. (widen)
  11707. (goto-char (point-min))
  11708. (while (re-search-forward
  11709. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11710. nil t)
  11711. (setq cfmt (match-string 2) s 0)
  11712. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11713. cfmt s)
  11714. (setq s (match-end 0)
  11715. p (match-string 1 cfmt))
  11716. (unless (or (equal p "ITEM")
  11717. (member p org-special-properties))
  11718. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11719. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11720. (defun org-property-values (key)
  11721. "Return a list of all values of property KEY."
  11722. (save-excursion
  11723. (save-restriction
  11724. (widen)
  11725. (goto-char (point-min))
  11726. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11727. values)
  11728. (while (re-search-forward re nil t)
  11729. (add-to-list 'values (org-trim (match-string 1))))
  11730. (delete "" values)))))
  11731. (defun org-insert-property-drawer ()
  11732. "Insert a property drawer into the current entry."
  11733. (interactive)
  11734. (org-back-to-heading t)
  11735. (looking-at outline-regexp)
  11736. (let ((indent (if org-adapt-indentation
  11737. (- (match-end 0)(match-beginning 0))
  11738. 0))
  11739. (beg (point))
  11740. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11741. end hiddenp)
  11742. (outline-next-heading)
  11743. (setq end (point))
  11744. (goto-char beg)
  11745. (while (re-search-forward re end t))
  11746. (setq hiddenp (org-invisible-p))
  11747. (end-of-line 1)
  11748. (and (equal (char-after) ?\n) (forward-char 1))
  11749. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11750. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11751. ;; just skip this line
  11752. (beginning-of-line 2)
  11753. ;; Drawer start, find the end
  11754. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11755. (beginning-of-line 1)))
  11756. (org-skip-over-state-notes)
  11757. (skip-chars-backward " \t\n\r")
  11758. (if (eq (char-before) ?*) (forward-char 1))
  11759. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11760. (beginning-of-line 0)
  11761. (org-indent-to-column indent)
  11762. (beginning-of-line 2)
  11763. (org-indent-to-column indent)
  11764. (beginning-of-line 0)
  11765. (if hiddenp
  11766. (save-excursion
  11767. (org-back-to-heading t)
  11768. (hide-entry))
  11769. (org-flag-drawer t))))
  11770. (defun org-set-property (property value)
  11771. "In the current entry, set PROPERTY to VALUE.
  11772. When called interactively, this will prompt for a property name, offering
  11773. completion on existing and default properties. And then it will prompt
  11774. for a value, offering completion either on allowed values (via an inherited
  11775. xxx_ALL property) or on existing values in other instances of this property
  11776. in the current file."
  11777. (interactive
  11778. (let* ((completion-ignore-case t)
  11779. (keys (org-buffer-property-keys nil t t))
  11780. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11781. (prop (if (member prop0 keys)
  11782. prop0
  11783. (or (cdr (assoc (downcase prop0)
  11784. (mapcar (lambda (x) (cons (downcase x) x))
  11785. keys)))
  11786. prop0)))
  11787. (cur (org-entry-get nil prop))
  11788. (allowed (org-property-get-allowed-values nil prop 'table))
  11789. (existing (mapcar 'list (org-property-values prop)))
  11790. (val (if allowed
  11791. (org-completing-read "Value: " allowed nil
  11792. (not (get-text-property 0 'org-unrestricted
  11793. (caar allowed))))
  11794. (let (org-completion-use-ido org-completion-use-iswitchb)
  11795. (org-completing-read
  11796. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11797. (concat "[" cur "]") "")
  11798. ": ")
  11799. existing nil nil "" nil cur)))))
  11800. (list prop (if (equal val "") cur val))))
  11801. (unless (equal (org-entry-get nil property) value)
  11802. (org-entry-put nil property value)))
  11803. (defun org-delete-property (property)
  11804. "In the current entry, delete PROPERTY."
  11805. (interactive
  11806. (let* ((completion-ignore-case t)
  11807. (prop (org-icompleting-read
  11808. "Property: " (org-entry-properties nil 'standard))))
  11809. (list prop)))
  11810. (message "Property %s %s" property
  11811. (if (org-entry-delete nil property)
  11812. "deleted"
  11813. "was not present in the entry")))
  11814. (defun org-delete-property-globally (property)
  11815. "Remove PROPERTY globally, from all entries."
  11816. (interactive
  11817. (let* ((completion-ignore-case t)
  11818. (prop (org-icompleting-read
  11819. "Globally remove property: "
  11820. (mapcar 'list (org-buffer-property-keys)))))
  11821. (list prop)))
  11822. (save-excursion
  11823. (save-restriction
  11824. (widen)
  11825. (goto-char (point-min))
  11826. (let ((cnt 0))
  11827. (while (re-search-forward
  11828. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11829. nil t)
  11830. (setq cnt (1+ cnt))
  11831. (replace-match ""))
  11832. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11833. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11834. (defun org-compute-property-at-point ()
  11835. "Compute the property at point.
  11836. This looks for an enclosing column format, extracts the operator and
  11837. then applies it to the property in the column format's scope."
  11838. (interactive)
  11839. (unless (org-at-property-p)
  11840. (error "Not at a property"))
  11841. (let ((prop (org-match-string-no-properties 2)))
  11842. (org-columns-get-format-and-top-level)
  11843. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11844. (error "No operator defined for property %s" prop))
  11845. (org-columns-compute prop)))
  11846. (defvar org-property-allowed-value-functions nil
  11847. "Hook for functions supplying allowed values for a specific property.
  11848. The functions must take a single argument, the name of the property, and
  11849. return a flat list of allowed values. If \":ETC\" is one of
  11850. the values, this means that these values are intended as defaults for
  11851. completion, but that other values should be allowed too.
  11852. The functions must return nil if they are not responsible for this
  11853. property.")
  11854. (defun org-property-get-allowed-values (pom property &optional table)
  11855. "Get allowed values for the property PROPERTY.
  11856. When TABLE is non-nil, return an alist that can directly be used for
  11857. completion."
  11858. (let (vals)
  11859. (cond
  11860. ((equal property "TODO")
  11861. (setq vals (org-with-point-at pom
  11862. (append org-todo-keywords-1 '("")))))
  11863. ((equal property "PRIORITY")
  11864. (let ((n org-lowest-priority))
  11865. (while (>= n org-highest-priority)
  11866. (push (char-to-string n) vals)
  11867. (setq n (1- n)))))
  11868. ((member property org-special-properties))
  11869. ((setq vals (run-hook-with-args-until-success
  11870. 'org-property-allowed-value-functions property)))
  11871. (t
  11872. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11873. (when (and vals (string-match "\\S-" vals))
  11874. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11875. (setq vals (mapcar (lambda (x)
  11876. (cond ((stringp x) x)
  11877. ((numberp x) (number-to-string x))
  11878. ((symbolp x) (symbol-name x))
  11879. (t "???")))
  11880. vals)))))
  11881. (when (member ":ETC" vals)
  11882. (setq vals (remove ":ETC" vals))
  11883. (org-add-props (car vals) '(org-unrestricted t)))
  11884. (if table (mapcar 'list vals) vals)))
  11885. (defun org-property-previous-allowed-value (&optional previous)
  11886. "Switch to the next allowed value for this property."
  11887. (interactive)
  11888. (org-property-next-allowed-value t))
  11889. (defun org-property-next-allowed-value (&optional previous)
  11890. "Switch to the next allowed value for this property."
  11891. (interactive)
  11892. (unless (org-at-property-p)
  11893. (error "Not at a property"))
  11894. (let* ((key (match-string 2))
  11895. (value (match-string 3))
  11896. (allowed (or (org-property-get-allowed-values (point) key)
  11897. (and (member value '("[ ]" "[-]" "[X]"))
  11898. '("[ ]" "[X]"))))
  11899. nval)
  11900. (unless allowed
  11901. (error "Allowed values for this property have not been defined"))
  11902. (if previous (setq allowed (reverse allowed)))
  11903. (if (member value allowed)
  11904. (setq nval (car (cdr (member value allowed)))))
  11905. (setq nval (or nval (car allowed)))
  11906. (if (equal nval value)
  11907. (error "Only one allowed value for this property"))
  11908. (org-at-property-p)
  11909. (replace-match (concat " :" key ": " nval) t t)
  11910. (org-indent-line-function)
  11911. (beginning-of-line 1)
  11912. (skip-chars-forward " \t")
  11913. (run-hook-with-args 'org-property-changed-functions key nval)))
  11914. (defun org-find-entry-with-id (ident)
  11915. "Locate the entry that contains the ID property with exact value IDENT.
  11916. IDENT can be a string, a symbol or a number, this function will search for
  11917. the string representation of it.
  11918. Return the position where this entry starts, or nil if there is no such entry."
  11919. (interactive "sID: ")
  11920. (let ((id (cond
  11921. ((stringp ident) ident)
  11922. ((symbol-name ident) (symbol-name ident))
  11923. ((numberp ident) (number-to-string ident))
  11924. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11925. (case-fold-search nil))
  11926. (save-excursion
  11927. (save-restriction
  11928. (widen)
  11929. (goto-char (point-min))
  11930. (when (re-search-forward
  11931. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11932. nil t)
  11933. (org-back-to-heading t)
  11934. (point))))))
  11935. ;;;; Timestamps
  11936. (defvar org-last-changed-timestamp nil)
  11937. (defvar org-last-inserted-timestamp nil
  11938. "The last time stamp inserted with `org-insert-time-stamp'.")
  11939. (defvar org-time-was-given) ; dynamically scoped parameter
  11940. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11941. (defvar org-ts-what) ; dynamically scoped parameter
  11942. (defun org-time-stamp (arg &optional inactive)
  11943. "Prompt for a date/time and insert a time stamp.
  11944. If the user specifies a time like HH:MM, or if this command is called
  11945. with a prefix argument, the time stamp will contain date and time.
  11946. Otherwise, only the date will be included. All parts of a date not
  11947. specified by the user will be filled in from the current date/time.
  11948. So if you press just return without typing anything, the time stamp
  11949. will represent the current date/time. If there is already a timestamp
  11950. at the cursor, it will be modified."
  11951. (interactive "P")
  11952. (let* ((ts nil)
  11953. (default-time
  11954. ;; Default time is either today, or, when entering a range,
  11955. ;; the range start.
  11956. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11957. (save-excursion
  11958. (re-search-backward
  11959. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11960. (- (point) 20) t)))
  11961. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11962. (current-time)))
  11963. (default-input (and ts (org-get-compact-tod ts)))
  11964. org-time-was-given org-end-time-was-given time)
  11965. (cond
  11966. ((and (org-at-timestamp-p t)
  11967. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11968. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11969. (insert "--")
  11970. (setq time (let ((this-command this-command))
  11971. (org-read-date arg 'totime nil nil
  11972. default-time default-input)))
  11973. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11974. ((org-at-timestamp-p t)
  11975. (setq time (let ((this-command this-command))
  11976. (org-read-date arg 'totime nil nil default-time default-input)))
  11977. (when (org-at-timestamp-p t) ; just to get the match data
  11978. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11979. (replace-match "")
  11980. (setq org-last-changed-timestamp
  11981. (org-insert-time-stamp
  11982. time (or org-time-was-given arg)
  11983. inactive nil nil (list org-end-time-was-given))))
  11984. (message "Timestamp updated"))
  11985. (t
  11986. (setq time (let ((this-command this-command))
  11987. (org-read-date arg 'totime nil nil default-time default-input)))
  11988. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11989. nil nil (list org-end-time-was-given))))))
  11990. ;; FIXME: can we use this for something else, like computing time differences?
  11991. (defun org-get-compact-tod (s)
  11992. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11993. (let* ((t1 (match-string 1 s))
  11994. (h1 (string-to-number (match-string 2 s)))
  11995. (m1 (string-to-number (match-string 3 s)))
  11996. (t2 (and (match-end 4) (match-string 5 s)))
  11997. (h2 (and t2 (string-to-number (match-string 6 s))))
  11998. (m2 (and t2 (string-to-number (match-string 7 s))))
  11999. dh dm)
  12000. (if (not t2)
  12001. t1
  12002. (setq dh (- h2 h1) dm (- m2 m1))
  12003. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12004. (concat t1 "+" (number-to-string dh)
  12005. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12006. (defun org-time-stamp-inactive (&optional arg)
  12007. "Insert an inactive time stamp.
  12008. An inactive time stamp is enclosed in square brackets instead of angle
  12009. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12010. does not link to the calendar and cannot be changed with the S-cursor keys.
  12011. So these are more for recording a certain time/date."
  12012. (interactive "P")
  12013. (org-time-stamp arg 'inactive))
  12014. (defvar org-date-ovl (org-make-overlay 1 1))
  12015. (org-overlay-put org-date-ovl 'face 'org-warning)
  12016. (org-detach-overlay org-date-ovl)
  12017. (defvar org-ans1) ; dynamically scoped parameter
  12018. (defvar org-ans2) ; dynamically scoped parameter
  12019. (defvar org-plain-time-of-day-regexp) ; defined below
  12020. (defvar org-overriding-default-time nil) ; dynamically scoped
  12021. (defvar org-read-date-overlay nil)
  12022. (defvar org-dcst nil) ; dynamically scoped
  12023. (defvar org-read-date-history nil)
  12024. (defvar org-read-date-final-answer nil)
  12025. (defun org-read-date (&optional with-time to-time from-string prompt
  12026. default-time default-input)
  12027. "Read a date, possibly a time, and make things smooth for the user.
  12028. The prompt will suggest to enter an ISO date, but you can also enter anything
  12029. which will at least partially be understood by `parse-time-string'.
  12030. Unrecognized parts of the date will default to the current day, month, year,
  12031. hour and minute. If this command is called to replace a timestamp at point,
  12032. of to enter the second timestamp of a range, the default time is taken from the
  12033. existing stamp. For example,
  12034. 3-2-5 --> 2003-02-05
  12035. feb 15 --> currentyear-02-15
  12036. sep 12 9 --> 2009-09-12
  12037. 12:45 --> today 12:45
  12038. 22 sept 0:34 --> currentyear-09-22 0:34
  12039. 12 --> currentyear-currentmonth-12
  12040. Fri --> nearest Friday (today or later)
  12041. etc.
  12042. Furthermore you can specify a relative date by giving, as the *first* thing
  12043. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12044. change in days weeks, months, years.
  12045. With a single plus or minus, the date is relative to today. With a double
  12046. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12047. +4d --> four days from today
  12048. +4 --> same as above
  12049. +2w --> two weeks from today
  12050. ++5 --> five days from default date
  12051. The function understands only English month and weekday abbreviations,
  12052. but this can be configured with the variables `parse-time-months' and
  12053. `parse-time-weekdays'.
  12054. While prompting, a calendar is popped up - you can also select the
  12055. date with the mouse (button 1). The calendar shows a period of three
  12056. months. To scroll it to other months, use the keys `>' and `<'.
  12057. If you don't like the calendar, turn it off with
  12058. \(setq org-read-date-popup-calendar nil)
  12059. With optional argument TO-TIME, the date will immediately be converted
  12060. to an internal time.
  12061. With an optional argument WITH-TIME, the prompt will suggest to also
  12062. insert a time. Note that when WITH-TIME is not set, you can still
  12063. enter a time, and this function will inform the calling routine about
  12064. this change. The calling routine may then choose to change the format
  12065. used to insert the time stamp into the buffer to include the time.
  12066. With optional argument FROM-STRING, read from this string instead from
  12067. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12068. the time/date that is used for everything that is not specified by the
  12069. user."
  12070. (require 'parse-time)
  12071. (let* ((org-time-stamp-rounding-minutes
  12072. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12073. (org-dcst org-display-custom-times)
  12074. (ct (org-current-time))
  12075. (def (or org-overriding-default-time default-time ct))
  12076. (defdecode (decode-time def))
  12077. (dummy (progn
  12078. (when (< (nth 2 defdecode) org-extend-today-until)
  12079. (setcar (nthcdr 2 defdecode) -1)
  12080. (setcar (nthcdr 1 defdecode) 59)
  12081. (setq def (apply 'encode-time defdecode)
  12082. defdecode (decode-time def)))))
  12083. (calendar-frame-setup nil)
  12084. (calendar-move-hook nil)
  12085. (calendar-view-diary-initially-flag nil)
  12086. (view-diary-entries-initially nil)
  12087. (calendar-view-holidays-initially-flag nil)
  12088. (view-calendar-holidays-initially nil)
  12089. (timestr (format-time-string
  12090. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12091. (prompt (concat (if prompt (concat prompt " ") "")
  12092. (format "Date+time [%s]: " timestr)))
  12093. ans (org-ans0 "") org-ans1 org-ans2 final)
  12094. (cond
  12095. (from-string (setq ans from-string))
  12096. (org-read-date-popup-calendar
  12097. (save-excursion
  12098. (save-window-excursion
  12099. (calendar)
  12100. (calendar-forward-day (- (time-to-days def)
  12101. (calendar-absolute-from-gregorian
  12102. (calendar-current-date))))
  12103. (org-eval-in-calendar nil t)
  12104. (let* ((old-map (current-local-map))
  12105. (map (copy-keymap calendar-mode-map))
  12106. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12107. (org-defkey map (kbd "RET") 'org-calendar-select)
  12108. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  12109. 'org-calendar-select-mouse)
  12110. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  12111. 'org-calendar-select-mouse)
  12112. (org-defkey minibuffer-local-map [(meta shift left)]
  12113. (lambda () (interactive)
  12114. (org-eval-in-calendar '(calendar-backward-month 1))))
  12115. (org-defkey minibuffer-local-map [(meta shift right)]
  12116. (lambda () (interactive)
  12117. (org-eval-in-calendar '(calendar-forward-month 1))))
  12118. (org-defkey minibuffer-local-map [(meta shift up)]
  12119. (lambda () (interactive)
  12120. (org-eval-in-calendar '(calendar-backward-year 1))))
  12121. (org-defkey minibuffer-local-map [(meta shift down)]
  12122. (lambda () (interactive)
  12123. (org-eval-in-calendar '(calendar-forward-year 1))))
  12124. (org-defkey minibuffer-local-map [?\e (shift left)]
  12125. (lambda () (interactive)
  12126. (org-eval-in-calendar '(calendar-backward-month 1))))
  12127. (org-defkey minibuffer-local-map [?\e (shift right)]
  12128. (lambda () (interactive)
  12129. (org-eval-in-calendar '(calendar-forward-month 1))))
  12130. (org-defkey minibuffer-local-map [?\e (shift up)]
  12131. (lambda () (interactive)
  12132. (org-eval-in-calendar '(calendar-backward-year 1))))
  12133. (org-defkey minibuffer-local-map [?\e (shift down)]
  12134. (lambda () (interactive)
  12135. (org-eval-in-calendar '(calendar-forward-year 1))))
  12136. (org-defkey minibuffer-local-map [(shift up)]
  12137. (lambda () (interactive)
  12138. (org-eval-in-calendar '(calendar-backward-week 1))))
  12139. (org-defkey minibuffer-local-map [(shift down)]
  12140. (lambda () (interactive)
  12141. (org-eval-in-calendar '(calendar-forward-week 1))))
  12142. (org-defkey minibuffer-local-map [(shift left)]
  12143. (lambda () (interactive)
  12144. (org-eval-in-calendar '(calendar-backward-day 1))))
  12145. (org-defkey minibuffer-local-map [(shift right)]
  12146. (lambda () (interactive)
  12147. (org-eval-in-calendar '(calendar-forward-day 1))))
  12148. (org-defkey minibuffer-local-map ">"
  12149. (lambda () (interactive)
  12150. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12151. (org-defkey minibuffer-local-map "<"
  12152. (lambda () (interactive)
  12153. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12154. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12155. (unwind-protect
  12156. (progn
  12157. (use-local-map map)
  12158. (add-hook 'post-command-hook 'org-read-date-display)
  12159. (setq org-ans0 (read-string prompt default-input
  12160. 'org-read-date-history nil))
  12161. ;; org-ans0: from prompt
  12162. ;; org-ans1: from mouse click
  12163. ;; org-ans2: from calendar motion
  12164. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12165. (remove-hook 'post-command-hook 'org-read-date-display)
  12166. (use-local-map old-map)
  12167. (when org-read-date-overlay
  12168. (org-delete-overlay org-read-date-overlay)
  12169. (setq org-read-date-overlay nil)))))))
  12170. (t ; Naked prompt only
  12171. (unwind-protect
  12172. (setq ans (read-string prompt default-input
  12173. 'org-read-date-history timestr))
  12174. (when org-read-date-overlay
  12175. (org-delete-overlay org-read-date-overlay)
  12176. (setq org-read-date-overlay nil)))))
  12177. (setq final (org-read-date-analyze ans def defdecode))
  12178. (setq org-read-date-final-answer ans)
  12179. (if to-time
  12180. (apply 'encode-time final)
  12181. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12182. (format "%04d-%02d-%02d %02d:%02d"
  12183. (nth 5 final) (nth 4 final) (nth 3 final)
  12184. (nth 2 final) (nth 1 final))
  12185. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12186. (defvar def)
  12187. (defvar defdecode)
  12188. (defvar with-time)
  12189. (defvar org-read-date-analyze-futurep nil)
  12190. (defun org-read-date-display ()
  12191. "Display the current date prompt interpretation in the minibuffer."
  12192. (when org-read-date-display-live
  12193. (when org-read-date-overlay
  12194. (org-delete-overlay org-read-date-overlay))
  12195. (let ((p (point)))
  12196. (end-of-line 1)
  12197. (while (not (equal (buffer-substring
  12198. (max (point-min) (- (point) 4)) (point))
  12199. " "))
  12200. (insert " "))
  12201. (goto-char p))
  12202. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12203. " " (or org-ans1 org-ans2)))
  12204. (org-end-time-was-given nil)
  12205. (f (org-read-date-analyze ans def defdecode))
  12206. (fmts (if org-dcst
  12207. org-time-stamp-custom-formats
  12208. org-time-stamp-formats))
  12209. (fmt (if (or with-time
  12210. (and (boundp 'org-time-was-given) org-time-was-given))
  12211. (cdr fmts)
  12212. (car fmts)))
  12213. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12214. (when (and org-end-time-was-given
  12215. (string-match org-plain-time-of-day-regexp txt))
  12216. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12217. org-end-time-was-given
  12218. (substring txt (match-end 0)))))
  12219. (when org-read-date-analyze-futurep
  12220. (setq txt (concat txt " (=>F)")))
  12221. (setq org-read-date-overlay
  12222. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  12223. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12224. (defun org-read-date-analyze (ans def defdecode)
  12225. "Analyse the combined answer of the date prompt."
  12226. ;; FIXME: cleanup and comment
  12227. (let ((nowdecode (decode-time (current-time)))
  12228. delta deltan deltaw deltadef year month day
  12229. hour minute second wday pm h2 m2 tl wday1
  12230. iso-year iso-weekday iso-week iso-year iso-date futurep)
  12231. (setq org-read-date-analyze-futurep nil)
  12232. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12233. (setq ans "+0"))
  12234. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12235. (setq ans (replace-match "" t t ans)
  12236. deltan (car delta)
  12237. deltaw (nth 1 delta)
  12238. deltadef (nth 2 delta)))
  12239. ;; Check if there is an iso week date in there
  12240. ;; If yes, store the info and postpone interpreting it until the rest
  12241. ;; of the parsing is done
  12242. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12243. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  12244. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  12245. iso-week (string-to-number (match-string 2 ans)))
  12246. (setq ans (replace-match "" t t ans)))
  12247. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  12248. (when (string-match
  12249. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12250. (setq year (if (match-end 2)
  12251. (string-to-number (match-string 2 ans))
  12252. (string-to-number (format-time-string "%Y")))
  12253. month (string-to-number (match-string 3 ans))
  12254. day (string-to-number (match-string 4 ans)))
  12255. (if (< year 100) (setq year (+ 2000 year)))
  12256. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12257. t nil ans)))
  12258. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12259. ;; If there is a time with am/pm, and *no* time without it, we convert
  12260. ;; so that matching will be successful.
  12261. (loop for i from 1 to 2 do ; twice, for end time as well
  12262. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12263. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12264. (setq hour (string-to-number (match-string 1 ans))
  12265. minute (if (match-end 3)
  12266. (string-to-number (match-string 3 ans))
  12267. 0)
  12268. pm (equal ?p
  12269. (string-to-char (downcase (match-string 4 ans)))))
  12270. (if (and (= hour 12) (not pm))
  12271. (setq hour 0)
  12272. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12273. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12274. t t ans))))
  12275. ;; Check if a time range is given as a duration
  12276. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12277. (setq hour (string-to-number (match-string 1 ans))
  12278. h2 (+ hour (string-to-number (match-string 3 ans)))
  12279. minute (string-to-number (match-string 2 ans))
  12280. m2 (+ minute (if (match-end 5) (string-to-number
  12281. (match-string 5 ans))0)))
  12282. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12283. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12284. t t ans)))
  12285. ;; Check if there is a time range
  12286. (when (boundp 'org-end-time-was-given)
  12287. (setq org-time-was-given nil)
  12288. (when (and (string-match org-plain-time-of-day-regexp ans)
  12289. (match-end 8))
  12290. (setq org-end-time-was-given (match-string 8 ans))
  12291. (setq ans (concat (substring ans 0 (match-beginning 7))
  12292. (substring ans (match-end 7))))))
  12293. (setq tl (parse-time-string ans)
  12294. day (or (nth 3 tl) (nth 3 defdecode))
  12295. month (or (nth 4 tl)
  12296. (if (and org-read-date-prefer-future
  12297. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12298. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12299. (nth 4 defdecode)))
  12300. year (or (nth 5 tl)
  12301. (if (and org-read-date-prefer-future
  12302. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12303. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12304. (nth 5 defdecode)))
  12305. hour (or (nth 2 tl) (nth 2 defdecode))
  12306. minute (or (nth 1 tl) (nth 1 defdecode))
  12307. second (or (nth 0 tl) 0)
  12308. wday (nth 6 tl))
  12309. (when (and (eq org-read-date-prefer-future 'time)
  12310. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12311. (equal day (nth 3 nowdecode))
  12312. (equal month (nth 4 nowdecode))
  12313. (equal year (nth 5 nowdecode))
  12314. (nth 2 tl)
  12315. (or (< (nth 2 tl) (nth 2 nowdecode))
  12316. (and (= (nth 2 tl) (nth 2 nowdecode))
  12317. (nth 1 tl)
  12318. (< (nth 1 tl) (nth 1 nowdecode)))))
  12319. (setq day (1+ day)
  12320. futurep t))
  12321. ;; Special date definitions below
  12322. (cond
  12323. (iso-week
  12324. ;; There was an iso week
  12325. (setq futurep nil)
  12326. (setq year (or iso-year year)
  12327. day (or iso-weekday wday 1)
  12328. wday nil ; to make sure that the trigger below does not match
  12329. iso-date (calendar-gregorian-from-absolute
  12330. (calendar-absolute-from-iso
  12331. (list iso-week day year))))
  12332. ; FIXME: Should we also push ISO weeks into the future?
  12333. ; (when (and org-read-date-prefer-future
  12334. ; (not iso-year)
  12335. ; (< (calendar-absolute-from-gregorian iso-date)
  12336. ; (time-to-days (current-time))))
  12337. ; (setq year (1+ year)
  12338. ; iso-date (calendar-gregorian-from-absolute
  12339. ; (calendar-absolute-from-iso
  12340. ; (list iso-week day year)))))
  12341. (setq month (car iso-date)
  12342. year (nth 2 iso-date)
  12343. day (nth 1 iso-date)))
  12344. (deltan
  12345. (setq futurep nil)
  12346. (unless deltadef
  12347. (let ((now (decode-time (current-time))))
  12348. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12349. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12350. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12351. ((equal deltaw "m") (setq month (+ month deltan)))
  12352. ((equal deltaw "y") (setq year (+ year deltan)))))
  12353. ((and wday (not (nth 3 tl)))
  12354. (setq futurep nil)
  12355. ;; Weekday was given, but no day, so pick that day in the week
  12356. ;; on or after the derived date.
  12357. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12358. (unless (equal wday wday1)
  12359. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12360. (if (and (boundp 'org-time-was-given)
  12361. (nth 2 tl))
  12362. (setq org-time-was-given t))
  12363. (if (< year 100) (setq year (+ 2000 year)))
  12364. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12365. (setq org-read-date-analyze-futurep futurep)
  12366. (list second minute hour day month year)))
  12367. (defvar parse-time-weekdays)
  12368. (defun org-read-date-get-relative (s today default)
  12369. "Check string S for special relative date string.
  12370. TODAY and DEFAULT are internal times, for today and for a default.
  12371. Return shift list (N what def-flag)
  12372. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12373. N is the number of WHATs to shift.
  12374. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12375. the DEFAULT date rather than TODAY."
  12376. (when (and
  12377. (string-match
  12378. (concat
  12379. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12380. "\\([0-9]+\\)?"
  12381. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12382. "\\([ \t]\\|$\\)") s)
  12383. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12384. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12385. (string-to-char (substring (match-string 1 s) -1))
  12386. ?+))
  12387. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12388. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12389. (what (if (match-end 3) (match-string 3 s) "d"))
  12390. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12391. (date (if rel default today))
  12392. (wday (nth 6 (decode-time date)))
  12393. delta)
  12394. (if wday1
  12395. (progn
  12396. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12397. (if (= dir ?-) (setq delta (- delta 7)))
  12398. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12399. (list delta "d" rel))
  12400. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12401. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12402. "Turn a user-specified date into the internal representation.
  12403. The internal representation needed by the calendar is (month day year).
  12404. This is a wrapper to handle the brain-dead convention in calendar that
  12405. user function argument order change dependent on argument order."
  12406. (if (boundp 'calendar-date-style)
  12407. (cond
  12408. ((eq calendar-date-style 'american)
  12409. (list arg1 arg2 arg3))
  12410. ((eq calendar-date-style 'european)
  12411. (list arg2 arg1 arg3))
  12412. ((eq calendar-date-style 'iso)
  12413. (list arg2 arg3 arg1)))
  12414. (if (org-bound-and-true-p european-calendar-style)
  12415. (list arg2 arg1 arg3)
  12416. (list arg1 arg2 arg3))))
  12417. (defun org-eval-in-calendar (form &optional keepdate)
  12418. "Eval FORM in the calendar window and return to current window.
  12419. Also, store the cursor date in variable org-ans2."
  12420. (let ((sf (selected-frame))
  12421. (sw (selected-window)))
  12422. (select-window (get-buffer-window "*Calendar*" t))
  12423. (eval form)
  12424. (when (and (not keepdate) (calendar-cursor-to-date))
  12425. (let* ((date (calendar-cursor-to-date))
  12426. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12427. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12428. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12429. (select-window sw)
  12430. (org-select-frame-set-input-focus sf)))
  12431. (defun org-calendar-select ()
  12432. "Return to `org-read-date' with the date currently selected.
  12433. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12434. (interactive)
  12435. (when (calendar-cursor-to-date)
  12436. (let* ((date (calendar-cursor-to-date))
  12437. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12438. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12439. (if (active-minibuffer-window) (exit-minibuffer))))
  12440. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12441. "Insert a date stamp for the date given by the internal TIME.
  12442. WITH-HM means, use the stamp format that includes the time of the day.
  12443. INACTIVE means use square brackets instead of angular ones, so that the
  12444. stamp will not contribute to the agenda.
  12445. PRE and POST are optional strings to be inserted before and after the
  12446. stamp.
  12447. The command returns the inserted time stamp."
  12448. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12449. stamp)
  12450. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12451. (insert-before-markers (or pre ""))
  12452. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12453. (when (listp extra)
  12454. (setq extra (car extra))
  12455. (if (and (stringp extra)
  12456. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12457. (setq extra (format "-%02d:%02d"
  12458. (string-to-number (match-string 1 extra))
  12459. (string-to-number (match-string 2 extra))))
  12460. (setq extra nil)))
  12461. (when extra
  12462. (backward-char 1)
  12463. (insert-before-markers extra)
  12464. (forward-char 1))
  12465. (insert-before-markers (or post ""))
  12466. (setq org-last-inserted-timestamp stamp)))
  12467. (defun org-toggle-time-stamp-overlays ()
  12468. "Toggle the use of custom time stamp formats."
  12469. (interactive)
  12470. (setq org-display-custom-times (not org-display-custom-times))
  12471. (unless org-display-custom-times
  12472. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12473. (while (setq p (next-single-property-change p 'display))
  12474. (if (and (get-text-property p 'display)
  12475. (eq (get-text-property p 'face) 'org-date))
  12476. (remove-text-properties
  12477. p (setq p (next-single-property-change p 'display))
  12478. '(display t))))
  12479. (set-buffer-modified-p bmp)))
  12480. (if (featurep 'xemacs)
  12481. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12482. (org-restart-font-lock)
  12483. (setq org-table-may-need-update t)
  12484. (if org-display-custom-times
  12485. (message "Time stamps are overlayed with custom format")
  12486. (message "Time stamp overlays removed")))
  12487. (defun org-display-custom-time (beg end)
  12488. "Overlay modified time stamp format over timestamp between BEG and END."
  12489. (let* ((ts (buffer-substring beg end))
  12490. t1 w1 with-hm tf time str w2 (off 0))
  12491. (save-match-data
  12492. (setq t1 (org-parse-time-string ts t))
  12493. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12494. (setq off (- (match-end 0) (match-beginning 0)))))
  12495. (setq end (- end off))
  12496. (setq w1 (- end beg)
  12497. with-hm (and (nth 1 t1) (nth 2 t1))
  12498. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12499. time (org-fix-decoded-time t1)
  12500. str (org-add-props
  12501. (format-time-string
  12502. (substring tf 1 -1) (apply 'encode-time time))
  12503. nil 'mouse-face 'highlight)
  12504. w2 (length str))
  12505. (if (not (= w2 w1))
  12506. (add-text-properties (1+ beg) (+ 2 beg)
  12507. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12508. (if (featurep 'xemacs)
  12509. (progn
  12510. (put-text-property beg end 'invisible t)
  12511. (put-text-property beg end 'end-glyph (make-glyph str)))
  12512. (put-text-property beg end 'display str))))
  12513. (defun org-translate-time (string)
  12514. "Translate all timestamps in STRING to custom format.
  12515. But do this only if the variable `org-display-custom-times' is set."
  12516. (when org-display-custom-times
  12517. (save-match-data
  12518. (let* ((start 0)
  12519. (re org-ts-regexp-both)
  12520. t1 with-hm inactive tf time str beg end)
  12521. (while (setq start (string-match re string start))
  12522. (setq beg (match-beginning 0)
  12523. end (match-end 0)
  12524. t1 (save-match-data
  12525. (org-parse-time-string (substring string beg end) t))
  12526. with-hm (and (nth 1 t1) (nth 2 t1))
  12527. inactive (equal (substring string beg (1+ beg)) "[")
  12528. tf (funcall (if with-hm 'cdr 'car)
  12529. org-time-stamp-custom-formats)
  12530. time (org-fix-decoded-time t1)
  12531. str (format-time-string
  12532. (concat
  12533. (if inactive "[" "<") (substring tf 1 -1)
  12534. (if inactive "]" ">"))
  12535. (apply 'encode-time time))
  12536. string (replace-match str t t string)
  12537. start (+ start (length str)))))))
  12538. string)
  12539. (defun org-fix-decoded-time (time)
  12540. "Set 0 instead of nil for the first 6 elements of time.
  12541. Don't touch the rest."
  12542. (let ((n 0))
  12543. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12544. (defun org-days-to-time (timestamp-string)
  12545. "Difference between TIMESTAMP-STRING and now in days."
  12546. (- (time-to-days (org-time-string-to-time timestamp-string))
  12547. (time-to-days (current-time))))
  12548. (defun org-deadline-close (timestamp-string &optional ndays)
  12549. "Is the time in TIMESTAMP-STRING close to the current date?"
  12550. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12551. (and (< (org-days-to-time timestamp-string) ndays)
  12552. (not (org-entry-is-done-p))))
  12553. (defun org-get-wdays (ts)
  12554. "Get the deadline lead time appropriate for timestring TS."
  12555. (cond
  12556. ((<= org-deadline-warning-days 0)
  12557. ;; 0 or negative, enforce this value no matter what
  12558. (- org-deadline-warning-days))
  12559. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12560. ;; lead time is specified.
  12561. (floor (* (string-to-number (match-string 1 ts))
  12562. (cdr (assoc (match-string 2 ts)
  12563. '(("d" . 1) ("w" . 7)
  12564. ("m" . 30.4) ("y" . 365.25)))))))
  12565. ;; go for the default.
  12566. (t org-deadline-warning-days)))
  12567. (defun org-calendar-select-mouse (ev)
  12568. "Return to `org-read-date' with the date currently selected.
  12569. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12570. (interactive "e")
  12571. (mouse-set-point ev)
  12572. (when (calendar-cursor-to-date)
  12573. (let* ((date (calendar-cursor-to-date))
  12574. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12575. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12576. (if (active-minibuffer-window) (exit-minibuffer))))
  12577. (defun org-check-deadlines (ndays)
  12578. "Check if there are any deadlines due or past due.
  12579. A deadline is considered due if it happens within `org-deadline-warning-days'
  12580. days from today's date. If the deadline appears in an entry marked DONE,
  12581. it is not shown. The prefix arg NDAYS can be used to test that many
  12582. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12583. (interactive "P")
  12584. (let* ((org-warn-days
  12585. (cond
  12586. ((equal ndays '(4)) 100000)
  12587. (ndays (prefix-numeric-value ndays))
  12588. (t (abs org-deadline-warning-days))))
  12589. (case-fold-search nil)
  12590. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12591. (callback
  12592. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12593. (message "%d deadlines past-due or due within %d days"
  12594. (org-occur regexp nil callback)
  12595. org-warn-days)))
  12596. (defun org-check-before-date (date)
  12597. "Check if there are deadlines or scheduled entries before DATE."
  12598. (interactive (list (org-read-date)))
  12599. (let ((case-fold-search nil)
  12600. (regexp (concat "\\<\\(" org-deadline-string
  12601. "\\|" org-scheduled-string
  12602. "\\) *<\\([^>]+\\)>"))
  12603. (callback
  12604. (lambda () (time-less-p
  12605. (org-time-string-to-time (match-string 2))
  12606. (org-time-string-to-time date)))))
  12607. (message "%d entries before %s"
  12608. (org-occur regexp nil callback) date)))
  12609. (defun org-check-after-date (date)
  12610. "Check if there are deadlines or scheduled entries after DATE."
  12611. (interactive (list (org-read-date)))
  12612. (let ((case-fold-search nil)
  12613. (regexp (concat "\\<\\(" org-deadline-string
  12614. "\\|" org-scheduled-string
  12615. "\\) *<\\([^>]+\\)>"))
  12616. (callback
  12617. (lambda () (not
  12618. (time-less-p
  12619. (org-time-string-to-time (match-string 2))
  12620. (org-time-string-to-time date))))))
  12621. (message "%d entries after %s"
  12622. (org-occur regexp nil callback) date)))
  12623. (defun org-evaluate-time-range (&optional to-buffer)
  12624. "Evaluate a time range by computing the difference between start and end.
  12625. Normally the result is just printed in the echo area, but with prefix arg
  12626. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12627. If the time range is actually in a table, the result is inserted into the
  12628. next column.
  12629. For time difference computation, a year is assumed to be exactly 365
  12630. days in order to avoid rounding problems."
  12631. (interactive "P")
  12632. (or
  12633. (org-clock-update-time-maybe)
  12634. (save-excursion
  12635. (unless (org-at-date-range-p t)
  12636. (goto-char (point-at-bol))
  12637. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12638. (if (not (org-at-date-range-p t))
  12639. (error "Not at a time-stamp range, and none found in current line")))
  12640. (let* ((ts1 (match-string 1))
  12641. (ts2 (match-string 2))
  12642. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12643. (match-end (match-end 0))
  12644. (time1 (org-time-string-to-time ts1))
  12645. (time2 (org-time-string-to-time ts2))
  12646. (t1 (org-float-time time1))
  12647. (t2 (org-float-time time2))
  12648. (diff (abs (- t2 t1)))
  12649. (negative (< (- t2 t1) 0))
  12650. ;; (ys (floor (* 365 24 60 60)))
  12651. (ds (* 24 60 60))
  12652. (hs (* 60 60))
  12653. (fy "%dy %dd %02d:%02d")
  12654. (fy1 "%dy %dd")
  12655. (fd "%dd %02d:%02d")
  12656. (fd1 "%dd")
  12657. (fh "%02d:%02d")
  12658. y d h m align)
  12659. (if havetime
  12660. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12661. y 0
  12662. d (floor (/ diff ds)) diff (mod diff ds)
  12663. h (floor (/ diff hs)) diff (mod diff hs)
  12664. m (floor (/ diff 60)))
  12665. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12666. y 0
  12667. d (floor (+ (/ diff ds) 0.5))
  12668. h 0 m 0))
  12669. (if (not to-buffer)
  12670. (message "%s" (org-make-tdiff-string y d h m))
  12671. (if (org-at-table-p)
  12672. (progn
  12673. (goto-char match-end)
  12674. (setq align t)
  12675. (and (looking-at " *|") (goto-char (match-end 0))))
  12676. (goto-char match-end))
  12677. (if (looking-at
  12678. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12679. (replace-match ""))
  12680. (if negative (insert " -"))
  12681. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12682. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12683. (insert " " (format fh h m))))
  12684. (if align (org-table-align))
  12685. (message "Time difference inserted")))))
  12686. (defun org-make-tdiff-string (y d h m)
  12687. (let ((fmt "")
  12688. (l nil))
  12689. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12690. l (push y l)))
  12691. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12692. l (push d l)))
  12693. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12694. l (push h l)))
  12695. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12696. l (push m l)))
  12697. (apply 'format fmt (nreverse l))))
  12698. (defun org-time-string-to-time (s)
  12699. (apply 'encode-time (org-parse-time-string s)))
  12700. (defun org-time-string-to-seconds (s)
  12701. (org-float-time (org-time-string-to-time s)))
  12702. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12703. "Convert a time stamp to an absolute day number.
  12704. If there is a specifyer for a cyclic time stamp, get the closest date to
  12705. DAYNR.
  12706. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12707. the variable date is bound by the calendar when this is called."
  12708. (cond
  12709. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12710. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12711. daynr
  12712. (+ daynr 1000)))
  12713. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12714. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12715. (time-to-days (current-time))) (match-string 0 s)
  12716. prefer show-all))
  12717. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12718. (defun org-days-to-iso-week (days)
  12719. "Return the iso week number."
  12720. (require 'cal-iso)
  12721. (car (calendar-iso-from-absolute days)))
  12722. (defun org-small-year-to-year (year)
  12723. "Convert 2-digit years into 4-digit years.
  12724. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12725. The year 2000 cannot be abbreviated. Any year larger than 99
  12726. is returned unchanged."
  12727. (if (< year 38)
  12728. (setq year (+ 2000 year))
  12729. (if (< year 100)
  12730. (setq year (+ 1900 year))))
  12731. year)
  12732. (defun org-time-from-absolute (d)
  12733. "Return the time corresponding to date D.
  12734. D may be an absolute day number, or a calendar-type list (month day year)."
  12735. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12736. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12737. (defun org-calendar-holiday ()
  12738. "List of holidays, for Diary display in Org-mode."
  12739. (require 'holidays)
  12740. (let ((hl (funcall
  12741. (if (fboundp 'calendar-check-holidays)
  12742. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12743. (if hl (mapconcat 'identity hl "; "))))
  12744. (defun org-diary-sexp-entry (sexp entry date)
  12745. "Process a SEXP diary ENTRY for DATE."
  12746. (require 'diary-lib)
  12747. (let ((result (if calendar-debug-sexp
  12748. (let ((stack-trace-on-error t))
  12749. (eval (car (read-from-string sexp))))
  12750. (condition-case nil
  12751. (eval (car (read-from-string sexp)))
  12752. (error
  12753. (beep)
  12754. (message "Bad sexp at line %d in %s: %s"
  12755. (org-current-line)
  12756. (buffer-file-name) sexp)
  12757. (sleep-for 2))))))
  12758. (cond ((stringp result) result)
  12759. ((and (consp result)
  12760. (stringp (cdr result))) (cdr result))
  12761. (result entry)
  12762. (t nil))))
  12763. (defun org-diary-to-ical-string (frombuf)
  12764. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12765. This uses the icalendar.el library."
  12766. (let* ((tmpdir (if (featurep 'xemacs)
  12767. (temp-directory)
  12768. temporary-file-directory))
  12769. (tmpfile (make-temp-name
  12770. (expand-file-name "orgics" tmpdir)))
  12771. buf rtn b e)
  12772. (with-current-buffer frombuf
  12773. (icalendar-export-region (point-min) (point-max) tmpfile)
  12774. (setq buf (find-buffer-visiting tmpfile))
  12775. (set-buffer buf)
  12776. (goto-char (point-min))
  12777. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12778. (setq b (match-beginning 0)))
  12779. (goto-char (point-max))
  12780. (if (re-search-backward "^END:VEVENT" nil t)
  12781. (setq e (match-end 0)))
  12782. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12783. (kill-buffer buf)
  12784. (delete-file tmpfile)
  12785. rtn))
  12786. (defun org-closest-date (start current change prefer show-all)
  12787. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12788. When PREFER is `past' return a date that is either CURRENT or past.
  12789. When PREFER is `future', return a date that is either CURRENT or future.
  12790. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12791. ;; Make the proper lists from the dates
  12792. (catch 'exit
  12793. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12794. dn dw sday cday n1 n2 n0
  12795. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12796. (setq start (org-date-to-gregorian start)
  12797. current (org-date-to-gregorian
  12798. (if show-all
  12799. current
  12800. (time-to-days (current-time))))
  12801. sday (calendar-absolute-from-gregorian start)
  12802. cday (calendar-absolute-from-gregorian current))
  12803. (if (<= cday sday) (throw 'exit sday))
  12804. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12805. (setq dn (string-to-number (match-string 1 change))
  12806. dw (cdr (assoc (match-string 2 change) a1)))
  12807. (error "Invalid change specifyer: %s" change))
  12808. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12809. (cond
  12810. ((eq dw 'day)
  12811. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12812. n2 (+ n1 dn)))
  12813. ((eq dw 'year)
  12814. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12815. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12816. (setq date1 (list m d y1)
  12817. n1 (calendar-absolute-from-gregorian date1)
  12818. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12819. n2 (calendar-absolute-from-gregorian date2)))
  12820. ((eq dw 'month)
  12821. ;; approx number of month between the two dates
  12822. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12823. ;; How often does dn fit in there?
  12824. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12825. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12826. m (+ m nm)
  12827. ny (floor (/ m 12))
  12828. y (+ y ny)
  12829. m (- m (* ny 12)))
  12830. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12831. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12832. (setq m2 (+ m dn) y2 y)
  12833. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12834. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12835. (while (<= n2 cday)
  12836. (setq n1 n2 m m2 y y2)
  12837. (setq m2 (+ m dn) y2 y)
  12838. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12839. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12840. ;; Make sure n1 is the earlier date
  12841. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12842. (if show-all
  12843. (cond
  12844. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12845. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12846. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12847. (cond
  12848. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12849. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12850. (t (if (= cday n1) n1 n2)))))))
  12851. (defun org-date-to-gregorian (date)
  12852. "Turn any specification of DATE into a gregorian date for the calendar."
  12853. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12854. ((and (listp date) (= (length date) 3)) date)
  12855. ((stringp date)
  12856. (setq date (org-parse-time-string date))
  12857. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12858. ((listp date)
  12859. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12860. (defun org-parse-time-string (s &optional nodefault)
  12861. "Parse the standard Org-mode time string.
  12862. This should be a lot faster than the normal `parse-time-string'.
  12863. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12864. hour and minute fields will be nil if not given."
  12865. (if (string-match org-ts-regexp0 s)
  12866. (list 0
  12867. (if (or (match-beginning 8) (not nodefault))
  12868. (string-to-number (or (match-string 8 s) "0")))
  12869. (if (or (match-beginning 7) (not nodefault))
  12870. (string-to-number (or (match-string 7 s) "0")))
  12871. (string-to-number (match-string 4 s))
  12872. (string-to-number (match-string 3 s))
  12873. (string-to-number (match-string 2 s))
  12874. nil nil nil)
  12875. (error "Not a standard Org-mode time string: %s" s)))
  12876. (defun org-timestamp-up (&optional arg)
  12877. "Increase the date item at the cursor by one.
  12878. If the cursor is on the year, change the year. If it is on the month or
  12879. the day, change that.
  12880. With prefix ARG, change by that many units."
  12881. (interactive "p")
  12882. (org-timestamp-change (prefix-numeric-value arg)))
  12883. (defun org-timestamp-down (&optional arg)
  12884. "Decrease the date item at the cursor by one.
  12885. If the cursor is on the year, change the year. If it is on the month or
  12886. the day, change that.
  12887. With prefix ARG, change by that many units."
  12888. (interactive "p")
  12889. (org-timestamp-change (- (prefix-numeric-value arg))))
  12890. (defun org-timestamp-up-day (&optional arg)
  12891. "Increase the date in the time stamp by one day.
  12892. With prefix ARG, change that many days."
  12893. (interactive "p")
  12894. (if (and (not (org-at-timestamp-p t))
  12895. (org-on-heading-p))
  12896. (org-todo 'up)
  12897. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12898. (defun org-timestamp-down-day (&optional arg)
  12899. "Decrease the date in the time stamp by one day.
  12900. With prefix ARG, change that many days."
  12901. (interactive "p")
  12902. (if (and (not (org-at-timestamp-p t))
  12903. (org-on-heading-p))
  12904. (org-todo 'down)
  12905. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12906. (defun org-at-timestamp-p (&optional inactive-ok)
  12907. "Determine if the cursor is in or at a timestamp."
  12908. (interactive)
  12909. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12910. (pos (point))
  12911. (ans (or (looking-at tsr)
  12912. (save-excursion
  12913. (skip-chars-backward "^[<\n\r\t")
  12914. (if (> (point) (point-min)) (backward-char 1))
  12915. (and (looking-at tsr)
  12916. (> (- (match-end 0) pos) -1))))))
  12917. (and ans
  12918. (boundp 'org-ts-what)
  12919. (setq org-ts-what
  12920. (cond
  12921. ((= pos (match-beginning 0)) 'bracket)
  12922. ((= pos (1- (match-end 0))) 'bracket)
  12923. ((org-pos-in-match-range pos 2) 'year)
  12924. ((org-pos-in-match-range pos 3) 'month)
  12925. ((org-pos-in-match-range pos 7) 'hour)
  12926. ((org-pos-in-match-range pos 8) 'minute)
  12927. ((or (org-pos-in-match-range pos 4)
  12928. (org-pos-in-match-range pos 5)) 'day)
  12929. ((and (> pos (or (match-end 8) (match-end 5)))
  12930. (< pos (match-end 0)))
  12931. (- pos (or (match-end 8) (match-end 5))))
  12932. (t 'day))))
  12933. ans))
  12934. (defun org-toggle-timestamp-type ()
  12935. "Toggle the type (<active> or [inactive]) of a time stamp."
  12936. (interactive)
  12937. (when (org-at-timestamp-p t)
  12938. (let ((beg (match-beginning 0)) (end (match-end 0))
  12939. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12940. (save-excursion
  12941. (goto-char beg)
  12942. (while (re-search-forward "[][<>]" end t)
  12943. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12944. t t)))
  12945. (message "Timestamp is now %sactive"
  12946. (if (equal (char-after beg) ?<) "" "in")))))
  12947. (defun org-timestamp-change (n &optional what)
  12948. "Change the date in the time stamp at point.
  12949. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12950. `year', `minute', `second'. If WHAT is not given, the cursor position
  12951. in the timestamp determines what will be changed."
  12952. (let ((pos (point))
  12953. with-hm inactive
  12954. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12955. org-ts-what
  12956. extra rem
  12957. ts time time0)
  12958. (if (not (org-at-timestamp-p t))
  12959. (error "Not at a timestamp"))
  12960. (if (and (not what) (eq org-ts-what 'bracket))
  12961. (org-toggle-timestamp-type)
  12962. (if (and (not what) (not (eq org-ts-what 'day))
  12963. org-display-custom-times
  12964. (get-text-property (point) 'display)
  12965. (not (get-text-property (1- (point)) 'display)))
  12966. (setq org-ts-what 'day))
  12967. (setq org-ts-what (or what org-ts-what)
  12968. inactive (= (char-after (match-beginning 0)) ?\[)
  12969. ts (match-string 0))
  12970. (replace-match "")
  12971. (if (string-match
  12972. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  12973. ts)
  12974. (setq extra (match-string 1 ts)))
  12975. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12976. (setq with-hm t))
  12977. (setq time0 (org-parse-time-string ts))
  12978. (when (and (eq org-ts-what 'minute)
  12979. (eq current-prefix-arg nil))
  12980. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12981. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12982. (setcar (cdr time0) (+ (nth 1 time0)
  12983. (if (> n 0) (- rem) (- dm rem))))))
  12984. (setq time
  12985. (encode-time (or (car time0) 0)
  12986. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12987. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12988. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12989. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12990. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12991. (nthcdr 6 time0)))
  12992. (when (and (member org-ts-what '(hour minute))
  12993. extra
  12994. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12995. (setq extra (org-modify-ts-extra
  12996. extra
  12997. (if (eq org-ts-what 'hour) 2 5)
  12998. n dm)))
  12999. (when (integerp org-ts-what)
  13000. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13001. (if (eq what 'calendar)
  13002. (let ((cal-date (org-get-date-from-calendar)))
  13003. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13004. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13005. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13006. (setcar time0 (or (car time0) 0))
  13007. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13008. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13009. (setq time (apply 'encode-time time0))))
  13010. (setq org-last-changed-timestamp
  13011. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13012. (org-clock-update-time-maybe)
  13013. (goto-char pos)
  13014. ;; Try to recenter the calendar window, if any
  13015. (if (and org-calendar-follow-timestamp-change
  13016. (get-buffer-window "*Calendar*" t)
  13017. (memq org-ts-what '(day month year)))
  13018. (org-recenter-calendar (time-to-days time))))))
  13019. (defun org-modify-ts-extra (s pos n dm)
  13020. "Change the different parts of the lead-time and repeat fields in timestamp."
  13021. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13022. ng h m new rem)
  13023. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13024. (cond
  13025. ((or (org-pos-in-match-range pos 2)
  13026. (org-pos-in-match-range pos 3))
  13027. (setq m (string-to-number (match-string 3 s))
  13028. h (string-to-number (match-string 2 s)))
  13029. (if (org-pos-in-match-range pos 2)
  13030. (setq h (+ h n))
  13031. (setq n (* dm (org-no-warnings (signum n))))
  13032. (when (not (= 0 (setq rem (% m dm))))
  13033. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13034. (setq m (+ m n)))
  13035. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13036. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13037. (setq h (min 24 (max 0 h)))
  13038. (setq ng 1 new (format "-%02d:%02d" h m)))
  13039. ((org-pos-in-match-range pos 6)
  13040. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13041. ((org-pos-in-match-range pos 5)
  13042. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13043. ((org-pos-in-match-range pos 9)
  13044. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13045. ((org-pos-in-match-range pos 8)
  13046. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13047. (when ng
  13048. (setq s (concat
  13049. (substring s 0 (match-beginning ng))
  13050. new
  13051. (substring s (match-end ng))))))
  13052. s))
  13053. (defun org-recenter-calendar (date)
  13054. "If the calendar is visible, recenter it to DATE."
  13055. (let* ((win (selected-window))
  13056. (cwin (get-buffer-window "*Calendar*" t))
  13057. (calendar-move-hook nil))
  13058. (when cwin
  13059. (select-window cwin)
  13060. (calendar-goto-date (if (listp date) date
  13061. (calendar-gregorian-from-absolute date)))
  13062. (select-window win))))
  13063. (defun org-goto-calendar (&optional arg)
  13064. "Go to the Emacs calendar at the current date.
  13065. If there is a time stamp in the current line, go to that date.
  13066. A prefix ARG can be used to force the current date."
  13067. (interactive "P")
  13068. (let ((tsr org-ts-regexp) diff
  13069. (calendar-move-hook nil)
  13070. (calendar-view-holidays-initially-flag nil)
  13071. (view-calendar-holidays-initially nil)
  13072. (calendar-view-diary-initially-flag nil)
  13073. (view-diary-entries-initially nil))
  13074. (if (or (org-at-timestamp-p)
  13075. (save-excursion
  13076. (beginning-of-line 1)
  13077. (looking-at (concat ".*" tsr))))
  13078. (let ((d1 (time-to-days (current-time)))
  13079. (d2 (time-to-days
  13080. (org-time-string-to-time (match-string 1)))))
  13081. (setq diff (- d2 d1))))
  13082. (calendar)
  13083. (calendar-goto-today)
  13084. (if (and diff (not arg)) (calendar-forward-day diff))))
  13085. (defun org-get-date-from-calendar ()
  13086. "Return a list (month day year) of date at point in calendar."
  13087. (with-current-buffer "*Calendar*"
  13088. (save-match-data
  13089. (calendar-cursor-to-date))))
  13090. (defun org-date-from-calendar ()
  13091. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13092. If there is already a time stamp at the cursor position, update it."
  13093. (interactive)
  13094. (if (org-at-timestamp-p t)
  13095. (org-timestamp-change 0 'calendar)
  13096. (let ((cal-date (org-get-date-from-calendar)))
  13097. (org-insert-time-stamp
  13098. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13099. (defun org-minutes-to-hh:mm-string (m)
  13100. "Compute H:MM from a number of minutes."
  13101. (let ((h (/ m 60)))
  13102. (setq m (- m (* 60 h)))
  13103. (format org-time-clocksum-format h m)))
  13104. (defun org-hh:mm-string-to-minutes (s)
  13105. "Convert a string H:MM to a number of minutes.
  13106. If the string is just a number, interpret it as minutes.
  13107. In fact, the first hh:mm or number in the string will be taken,
  13108. there can be extra stuff in the string.
  13109. If no number is found, the return value is 0."
  13110. (cond
  13111. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13112. (+ (* (string-to-number (match-string 1 s)) 60)
  13113. (string-to-number (match-string 2 s))))
  13114. ((string-match "\\([0-9]+\\)" s)
  13115. (string-to-number (match-string 1 s)))
  13116. (t 0)))
  13117. ;;;; Files
  13118. (defun org-save-all-org-buffers ()
  13119. "Save all Org-mode buffers without user confirmation."
  13120. (interactive)
  13121. (message "Saving all Org-mode buffers...")
  13122. (save-some-buffers t 'org-mode-p)
  13123. (when (featurep 'org-id) (org-id-locations-save))
  13124. (message "Saving all Org-mode buffers... done"))
  13125. (defun org-revert-all-org-buffers ()
  13126. "Revert all Org-mode buffers.
  13127. Prompt for confirmation when there are unsaved changes.
  13128. Be sure you know what you are doing before letting this function
  13129. overwrite your changes.
  13130. This function is useful in a setup where one tracks org files
  13131. with a version control system, to revert on one machine after pulling
  13132. changes from another. I believe the procedure must be like this:
  13133. 1. M-x org-save-all-org-buffers
  13134. 2. Pull changes from the other machine, resolve conflicts
  13135. 3. M-x org-revert-all-org-buffers"
  13136. (interactive)
  13137. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13138. (error "Abort"))
  13139. (save-excursion
  13140. (save-window-excursion
  13141. (mapc
  13142. (lambda (b)
  13143. (when (and (with-current-buffer b (org-mode-p))
  13144. (with-current-buffer b buffer-file-name))
  13145. (switch-to-buffer b)
  13146. (revert-buffer t 'no-confirm)))
  13147. (buffer-list))
  13148. (when (and (featurep 'org-id) org-id-track-globally)
  13149. (org-id-locations-load)))))
  13150. ;;;; Agenda files
  13151. ;;;###autoload
  13152. (defun org-iswitchb (&optional arg)
  13153. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13154. With a prefix argument, restrict available to files.
  13155. With two prefix arguments, restrict available buffers to agenda files."
  13156. (interactive "P")
  13157. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13158. ((equal arg '(16)) (org-buffer-list 'agenda))
  13159. (t (org-buffer-list)))))
  13160. (switch-to-buffer
  13161. (org-icompleting-read "Org buffer: "
  13162. (mapcar 'list (mapcar 'buffer-name blist))
  13163. nil t))))
  13164. ;;;###autoload
  13165. (defalias 'org-ido-switchb 'org-iswitchb)
  13166. (defun org-buffer-list (&optional predicate exclude-tmp)
  13167. "Return a list of Org buffers.
  13168. PREDICATE can be `export', `files' or `agenda'.
  13169. export restrict the list to Export buffers.
  13170. files restrict the list to buffers visiting Org files.
  13171. agenda restrict the list to buffers visiting agenda files.
  13172. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13173. (let* ((bfn nil)
  13174. (agenda-files (and (eq predicate 'agenda)
  13175. (mapcar 'file-truename (org-agenda-files t))))
  13176. (filter
  13177. (cond
  13178. ((eq predicate 'files)
  13179. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13180. ((eq predicate 'export)
  13181. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13182. ((eq predicate 'agenda)
  13183. (lambda (b)
  13184. (with-current-buffer b
  13185. (and (eq major-mode 'org-mode)
  13186. (setq bfn (buffer-file-name b))
  13187. (member (file-truename bfn) agenda-files)))))
  13188. (t (lambda (b) (with-current-buffer b
  13189. (or (eq major-mode 'org-mode)
  13190. (string-match "\*Org .*Export"
  13191. (buffer-name b)))))))))
  13192. (delq nil
  13193. (mapcar
  13194. (lambda(b)
  13195. (if (and (funcall filter b)
  13196. (or (not exclude-tmp)
  13197. (not (string-match "tmp" (buffer-name b)))))
  13198. b
  13199. nil))
  13200. (buffer-list)))))
  13201. (defun org-agenda-files (&optional unrestricted archives)
  13202. "Get the list of agenda files.
  13203. Optional UNRESTRICTED means return the full list even if a restriction
  13204. is currently in place.
  13205. When ARCHIVES is t, include all archive files hat are really being
  13206. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13207. `org-agenda-archives-mode' is t."
  13208. (let ((files
  13209. (cond
  13210. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13211. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13212. ((listp org-agenda-files) org-agenda-files)
  13213. (t (error "Invalid value of `org-agenda-files'")))))
  13214. (setq files (apply 'append
  13215. (mapcar (lambda (f)
  13216. (if (file-directory-p f)
  13217. (directory-files
  13218. f t org-agenda-file-regexp)
  13219. (list f)))
  13220. files)))
  13221. (when org-agenda-skip-unavailable-files
  13222. (setq files (delq nil
  13223. (mapcar (function
  13224. (lambda (file)
  13225. (and (file-readable-p file) file)))
  13226. files))))
  13227. (when (or (eq archives t)
  13228. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13229. (setq files (org-add-archive-files files)))
  13230. files))
  13231. (defun org-edit-agenda-file-list ()
  13232. "Edit the list of agenda files.
  13233. Depending on setup, this either uses customize to edit the variable
  13234. `org-agenda-files', or it visits the file that is holding the list. In the
  13235. latter case, the buffer is set up in a way that saving it automatically kills
  13236. the buffer and restores the previous window configuration."
  13237. (interactive)
  13238. (if (stringp org-agenda-files)
  13239. (let ((cw (current-window-configuration)))
  13240. (find-file org-agenda-files)
  13241. (org-set-local 'org-window-configuration cw)
  13242. (org-add-hook 'after-save-hook
  13243. (lambda ()
  13244. (set-window-configuration
  13245. (prog1 org-window-configuration
  13246. (kill-buffer (current-buffer))))
  13247. (org-install-agenda-files-menu)
  13248. (message "New agenda file list installed"))
  13249. nil 'local)
  13250. (message "%s" (substitute-command-keys
  13251. "Edit list and finish with \\[save-buffer]")))
  13252. (customize-variable 'org-agenda-files)))
  13253. (defun org-store-new-agenda-file-list (list)
  13254. "Set new value for the agenda file list and save it correctly."
  13255. (if (stringp org-agenda-files)
  13256. (let ((f org-agenda-files) b)
  13257. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  13258. (with-temp-file f
  13259. (insert (mapconcat 'identity list "\n") "\n")))
  13260. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13261. (org-insert-mode-line-in-empty-file nil))
  13262. (setq org-agenda-files list)
  13263. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13264. (defun org-read-agenda-file-list ()
  13265. "Read the list of agenda files from a file."
  13266. (when (file-directory-p org-agenda-files)
  13267. (error "`org-agenda-files' cannot be a single directory"))
  13268. (when (stringp org-agenda-files)
  13269. (with-temp-buffer
  13270. (insert-file-contents org-agenda-files)
  13271. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  13272. ;;;###autoload
  13273. (defun org-cycle-agenda-files ()
  13274. "Cycle through the files in `org-agenda-files'.
  13275. If the current buffer visits an agenda file, find the next one in the list.
  13276. If the current buffer does not, find the first agenda file."
  13277. (interactive)
  13278. (let* ((fs (org-agenda-files t))
  13279. (files (append fs (list (car fs))))
  13280. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13281. file)
  13282. (unless files (error "No agenda files"))
  13283. (catch 'exit
  13284. (while (setq file (pop files))
  13285. (if (equal (file-truename file) tcf)
  13286. (when (car files)
  13287. (find-file (car files))
  13288. (throw 'exit t))))
  13289. (find-file (car fs)))
  13290. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13291. (defun org-agenda-file-to-front (&optional to-end)
  13292. "Move/add the current file to the top of the agenda file list.
  13293. If the file is not present in the list, it is added to the front. If it is
  13294. present, it is moved there. With optional argument TO-END, add/move to the
  13295. end of the list."
  13296. (interactive "P")
  13297. (let ((org-agenda-skip-unavailable-files nil)
  13298. (file-alist (mapcar (lambda (x)
  13299. (cons (file-truename x) x))
  13300. (org-agenda-files t)))
  13301. (ctf (file-truename buffer-file-name))
  13302. x had)
  13303. (setq x (assoc ctf file-alist) had x)
  13304. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13305. (if to-end
  13306. (setq file-alist (append (delq x file-alist) (list x)))
  13307. (setq file-alist (cons x (delq x file-alist))))
  13308. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13309. (org-install-agenda-files-menu)
  13310. (message "File %s to %s of agenda file list"
  13311. (if had "moved" "added") (if to-end "end" "front"))))
  13312. (defun org-remove-file (&optional file)
  13313. "Remove current file from the list of files in variable `org-agenda-files'.
  13314. These are the files which are being checked for agenda entries.
  13315. Optional argument FILE means, use this file instead of the current."
  13316. (interactive)
  13317. (let* ((org-agenda-skip-unavailable-files nil)
  13318. (file (or file buffer-file-name))
  13319. (true-file (file-truename file))
  13320. (afile (abbreviate-file-name file))
  13321. (files (delq nil (mapcar
  13322. (lambda (x)
  13323. (if (equal true-file
  13324. (file-truename x))
  13325. nil x))
  13326. (org-agenda-files t)))))
  13327. (if (not (= (length files) (length (org-agenda-files t))))
  13328. (progn
  13329. (org-store-new-agenda-file-list files)
  13330. (org-install-agenda-files-menu)
  13331. (message "Removed file: %s" afile))
  13332. (message "File was not in list: %s (not removed)" afile))))
  13333. (defun org-file-menu-entry (file)
  13334. (vector file (list 'find-file file) t))
  13335. (defun org-check-agenda-file (file)
  13336. "Make sure FILE exists. If not, ask user what to do."
  13337. (when (not (file-exists-p file))
  13338. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13339. (abbreviate-file-name file))
  13340. (let ((r (downcase (read-char-exclusive))))
  13341. (cond
  13342. ((equal r ?r)
  13343. (org-remove-file file)
  13344. (throw 'nextfile t))
  13345. (t (error "Abort"))))))
  13346. (defun org-get-agenda-file-buffer (file)
  13347. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13348. it to the list of buffers which might be released later."
  13349. (let ((buf (org-find-base-buffer-visiting file)))
  13350. (if buf
  13351. buf ; just return it
  13352. ;; Make a new buffer and remember it
  13353. (setq buf (find-file-noselect file))
  13354. (if buf (push buf org-agenda-new-buffers))
  13355. buf)))
  13356. (defun org-release-buffers (blist)
  13357. "Release all buffers in list, asking the user for confirmation when needed.
  13358. When a buffer is unmodified, it is just killed. When modified, it is saved
  13359. \(if the user agrees) and then killed."
  13360. (let (buf file)
  13361. (while (setq buf (pop blist))
  13362. (setq file (buffer-file-name buf))
  13363. (when (and (buffer-modified-p buf)
  13364. file
  13365. (y-or-n-p (format "Save file %s? " file)))
  13366. (with-current-buffer buf (save-buffer)))
  13367. (kill-buffer buf))))
  13368. (defun org-prepare-agenda-buffers (files)
  13369. "Create buffers for all agenda files, protect archived trees and comments."
  13370. (interactive)
  13371. (let ((pa '(:org-archived t))
  13372. (pc '(:org-comment t))
  13373. (pall '(:org-archived t :org-comment t))
  13374. (inhibit-read-only t)
  13375. (rea (concat ":" org-archive-tag ":"))
  13376. bmp file re)
  13377. (save-excursion
  13378. (save-restriction
  13379. (while (setq file (pop files))
  13380. (catch 'nextfile
  13381. (if (bufferp file)
  13382. (set-buffer file)
  13383. (org-check-agenda-file file)
  13384. (set-buffer (org-get-agenda-file-buffer file)))
  13385. (widen)
  13386. (setq bmp (buffer-modified-p))
  13387. (org-refresh-category-properties)
  13388. (setq org-todo-keywords-for-agenda
  13389. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13390. (setq org-done-keywords-for-agenda
  13391. (append org-done-keywords-for-agenda org-done-keywords))
  13392. (setq org-todo-keyword-alist-for-agenda
  13393. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13394. (setq org-drawers-for-agenda
  13395. (append org-drawers-for-agenda org-drawers))
  13396. (setq org-tag-alist-for-agenda
  13397. (append org-tag-alist-for-agenda org-tag-alist))
  13398. (save-excursion
  13399. (remove-text-properties (point-min) (point-max) pall)
  13400. (when org-agenda-skip-archived-trees
  13401. (goto-char (point-min))
  13402. (while (re-search-forward rea nil t)
  13403. (if (org-on-heading-p t)
  13404. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13405. (goto-char (point-min))
  13406. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13407. (while (re-search-forward re nil t)
  13408. (add-text-properties
  13409. (match-beginning 0) (org-end-of-subtree t) pc)))
  13410. (set-buffer-modified-p bmp)))))
  13411. (setq org-todo-keyword-alist-for-agenda
  13412. (org-uniquify org-todo-keyword-alist-for-agenda)
  13413. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13414. ;;;; Embedded LaTeX
  13415. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13416. "Keymap for the minor `org-cdlatex-mode'.")
  13417. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13418. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13419. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13420. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13421. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13422. (defvar org-cdlatex-texmathp-advice-is-done nil
  13423. "Flag remembering if we have applied the advice to texmathp already.")
  13424. (define-minor-mode org-cdlatex-mode
  13425. "Toggle the minor `org-cdlatex-mode'.
  13426. This mode supports entering LaTeX environment and math in LaTeX fragments
  13427. in Org-mode.
  13428. \\{org-cdlatex-mode-map}"
  13429. nil " OCDL" nil
  13430. (when org-cdlatex-mode (require 'cdlatex))
  13431. (unless org-cdlatex-texmathp-advice-is-done
  13432. (setq org-cdlatex-texmathp-advice-is-done t)
  13433. (defadvice texmathp (around org-math-always-on activate)
  13434. "Always return t in org-mode buffers.
  13435. This is because we want to insert math symbols without dollars even outside
  13436. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13437. an embedded LaTeX fragment, let texmathp do its job.
  13438. \\[org-cdlatex-mode-map]"
  13439. (interactive)
  13440. (let (p)
  13441. (cond
  13442. ((not (org-mode-p)) ad-do-it)
  13443. ((eq this-command 'cdlatex-math-symbol)
  13444. (setq ad-return-value t
  13445. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13446. (t
  13447. (let ((p (org-inside-LaTeX-fragment-p)))
  13448. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13449. (setq ad-return-value t
  13450. texmathp-why '("Org-mode embedded math" . 0))
  13451. (if p ad-do-it)))))))))
  13452. (defun turn-on-org-cdlatex ()
  13453. "Unconditionally turn on `org-cdlatex-mode'."
  13454. (org-cdlatex-mode 1))
  13455. (defun org-inside-LaTeX-fragment-p ()
  13456. "Test if point is inside a LaTeX fragment.
  13457. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13458. sequence appearing also before point.
  13459. Even though the matchers for math are configurable, this function assumes
  13460. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13461. delimiters are skipped when they have been removed by customization.
  13462. The return value is nil, or a cons cell with the delimiter and
  13463. and the position of this delimiter.
  13464. This function does a reasonably good job, but can locally be fooled by
  13465. for example currency specifications. For example it will assume being in
  13466. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13467. fragments that are properly closed, but during editing, we have to live
  13468. with the uncertainty caused by missing closing delimiters. This function
  13469. looks only before point, not after."
  13470. (catch 'exit
  13471. (let ((pos (point))
  13472. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13473. (lim (progn
  13474. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13475. (point)))
  13476. dd-on str (start 0) m re)
  13477. (goto-char pos)
  13478. (when dodollar
  13479. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13480. re (nth 1 (assoc "$" org-latex-regexps)))
  13481. (while (string-match re str start)
  13482. (cond
  13483. ((= (match-end 0) (length str))
  13484. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13485. ((= (match-end 0) (- (length str) 5))
  13486. (throw 'exit nil))
  13487. (t (setq start (match-end 0))))))
  13488. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13489. (goto-char pos)
  13490. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13491. (and (match-beginning 2) (throw 'exit nil))
  13492. ;; count $$
  13493. (while (re-search-backward "\\$\\$" lim t)
  13494. (setq dd-on (not dd-on)))
  13495. (goto-char pos)
  13496. (if dd-on (cons "$$" m))))))
  13497. (defun org-inside-latex-macro-p ()
  13498. "Is point inside a LaTeX macro or its arguments?"
  13499. (save-match-data
  13500. (org-in-regexp
  13501. "\\\\[a-zA-Z]+\\*?\\(\\[[^][\n{}]*\\]\\)?\\({[^{}\n]*}\\)?")))
  13502. (defun org-try-cdlatex-tab ()
  13503. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13504. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13505. - inside a LaTeX fragment, or
  13506. - after the first word in a line, where an abbreviation expansion could
  13507. insert a LaTeX environment."
  13508. (when org-cdlatex-mode
  13509. (cond
  13510. ((save-excursion
  13511. (skip-chars-backward "a-zA-Z0-9*")
  13512. (skip-chars-backward " \t")
  13513. (bolp))
  13514. (cdlatex-tab) t)
  13515. ((org-inside-LaTeX-fragment-p)
  13516. (cdlatex-tab) t)
  13517. (t nil))))
  13518. (defun org-cdlatex-underscore-caret (&optional arg)
  13519. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13520. Revert to the normal definition outside of these fragments."
  13521. (interactive "P")
  13522. (if (org-inside-LaTeX-fragment-p)
  13523. (call-interactively 'cdlatex-sub-superscript)
  13524. (let (org-cdlatex-mode)
  13525. (call-interactively (key-binding (vector last-input-event))))))
  13526. (defun org-cdlatex-math-modify (&optional arg)
  13527. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13528. Revert to the normal definition outside of these fragments."
  13529. (interactive "P")
  13530. (if (org-inside-LaTeX-fragment-p)
  13531. (call-interactively 'cdlatex-math-modify)
  13532. (let (org-cdlatex-mode)
  13533. (call-interactively (key-binding (vector last-input-event))))))
  13534. (defvar org-latex-fragment-image-overlays nil
  13535. "List of overlays carrying the images of latex fragments.")
  13536. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13537. (defun org-remove-latex-fragment-image-overlays ()
  13538. "Remove all overlays with LaTeX fragment images in current buffer."
  13539. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13540. (setq org-latex-fragment-image-overlays nil))
  13541. (defun org-preview-latex-fragment (&optional subtree)
  13542. "Preview the LaTeX fragment at point, or all locally or globally.
  13543. If the cursor is in a LaTeX fragment, create the image and overlay
  13544. it over the source code. If there is no fragment at point, display
  13545. all fragments in the current text, from one headline to the next. With
  13546. prefix SUBTREE, display all fragments in the current subtree. With a
  13547. double prefix `C-u C-u', or when the cursor is before the first headline,
  13548. display all fragments in the buffer.
  13549. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13550. (interactive "P")
  13551. (org-remove-latex-fragment-image-overlays)
  13552. (save-excursion
  13553. (save-restriction
  13554. (let (beg end at msg)
  13555. (cond
  13556. ((or (equal subtree '(16))
  13557. (not (save-excursion
  13558. (re-search-backward (concat "^" outline-regexp) nil t))))
  13559. (setq beg (point-min) end (point-max)
  13560. msg "Creating images for buffer...%s"))
  13561. ((equal subtree '(4))
  13562. (org-back-to-heading)
  13563. (setq beg (point) end (org-end-of-subtree t)
  13564. msg "Creating images for subtree...%s"))
  13565. (t
  13566. (if (setq at (org-inside-LaTeX-fragment-p))
  13567. (goto-char (max (point-min) (- (cdr at) 2)))
  13568. (org-back-to-heading))
  13569. (setq beg (point) end (progn (outline-next-heading) (point))
  13570. msg (if at "Creating image...%s"
  13571. "Creating images for entry...%s"))))
  13572. (message msg "")
  13573. (narrow-to-region beg end)
  13574. (goto-char beg)
  13575. (org-format-latex
  13576. (concat "ltxpng/" (file-name-sans-extension
  13577. (file-name-nondirectory
  13578. buffer-file-name)))
  13579. default-directory 'overlays msg at 'forbuffer)
  13580. (message msg "done. Use `C-c C-c' to remove images.")))))
  13581. (defvar org-latex-regexps
  13582. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13583. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13584. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13585. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13586. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13587. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13588. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13589. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13590. "Regular expressions for matching embedded LaTeX.")
  13591. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13592. "Replace LaTeX fragments with links to an image, and produce images.
  13593. Some of the options can be changed using the variable
  13594. `org-format-latex-options'."
  13595. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13596. (let* ((prefixnodir (file-name-nondirectory prefix))
  13597. (absprefix (expand-file-name prefix dir))
  13598. (todir (file-name-directory absprefix))
  13599. (opt org-format-latex-options)
  13600. (matchers (plist-get opt :matchers))
  13601. (re-list org-latex-regexps)
  13602. (org-format-latex-header-extra
  13603. (plist-get (org-infile-export-plist) :latex-header-extra))
  13604. (cnt 0) txt hash link beg end re e checkdir
  13605. executables-checked
  13606. m n block linkfile movefile ov)
  13607. ;; Check the different regular expressions
  13608. (while (setq e (pop re-list))
  13609. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13610. block (if (nth 3 e) "\n\n" ""))
  13611. (when (member m matchers)
  13612. (goto-char (point-min))
  13613. (while (re-search-forward re nil t)
  13614. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13615. (not (get-text-property (match-beginning n)
  13616. 'org-protected))
  13617. (or (not overlays)
  13618. (not (eq (get-char-property (match-beginning n)
  13619. 'org-overlay-type)
  13620. 'org-latex-overlay))))
  13621. (setq txt (match-string n)
  13622. beg (match-beginning n) end (match-end n)
  13623. cnt (1+ cnt))
  13624. (let (print-length print-level) ; make sure full list is printed
  13625. (setq hash (sha1 (prin1-to-string
  13626. (list org-format-latex-header
  13627. org-format-latex-header-extra
  13628. org-export-latex-packages-alist
  13629. org-format-latex-options
  13630. forbuffer txt)))
  13631. linkfile (format "%s_%s.png" prefix hash)
  13632. movefile (format "%s_%s.png" absprefix hash)))
  13633. (setq link (concat block "[[file:" linkfile "]]" block))
  13634. (if msg (message msg cnt))
  13635. (goto-char beg)
  13636. (unless checkdir ; make sure the directory exists
  13637. (setq checkdir t)
  13638. (or (file-directory-p todir) (make-directory todir)))
  13639. (unless executables-checked
  13640. (org-check-external-command
  13641. "latex" "needed to convert LaTeX fragments to images")
  13642. (org-check-external-command
  13643. "dvipng" "needed to convert LaTeX fragments to images")
  13644. (setq executables-checked t))
  13645. (unless (file-exists-p movefile)
  13646. (org-create-formula-image
  13647. txt movefile opt forbuffer))
  13648. (if overlays
  13649. (progn
  13650. (mapc (lambda (o)
  13651. (if (eq (org-overlay-get o 'org-overlay-type)
  13652. 'org-latex-overlay)
  13653. (org-delete-overlay o)))
  13654. (org-overlays-in beg end))
  13655. (setq ov (org-make-overlay beg end))
  13656. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13657. (if (featurep 'xemacs)
  13658. (progn
  13659. (org-overlay-put ov 'invisible t)
  13660. (org-overlay-put
  13661. ov 'end-glyph
  13662. (make-glyph (vector 'png :file movefile))))
  13663. (org-overlay-put
  13664. ov 'display
  13665. (list 'image :type 'png :file movefile :ascent 'center)))
  13666. (push ov org-latex-fragment-image-overlays)
  13667. (goto-char end))
  13668. (delete-region beg end)
  13669. (insert link))))))))
  13670. ;; This function borrows from Ganesh Swami's latex2png.el
  13671. (defun org-create-formula-image (string tofile options buffer)
  13672. "This calls dvipng."
  13673. (require 'org-latex)
  13674. (let* ((tmpdir (if (featurep 'xemacs)
  13675. (temp-directory)
  13676. temporary-file-directory))
  13677. (texfilebase (make-temp-name
  13678. (expand-file-name "orgtex" tmpdir)))
  13679. (texfile (concat texfilebase ".tex"))
  13680. (dvifile (concat texfilebase ".dvi"))
  13681. (pngfile (concat texfilebase ".png"))
  13682. (fnh (if (featurep 'xemacs)
  13683. (font-height (get-face-font 'default))
  13684. (face-attribute 'default :height nil)))
  13685. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13686. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13687. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13688. "Black"))
  13689. (bg (or (plist-get options (if buffer :background :html-background))
  13690. "Transparent")))
  13691. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13692. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13693. (with-temp-file texfile
  13694. (insert org-format-latex-header
  13695. (if org-export-latex-packages-alist
  13696. (concat "\n"
  13697. (mapconcat (lambda(p)
  13698. (if (equal "" (car p))
  13699. (format "\\usepackage{%s}" (cadr p))
  13700. (format "\\usepackage[%s]{%s}"
  13701. (car p) (cadr p))))
  13702. org-export-latex-packages-alist "\n"))
  13703. "")
  13704. (if org-format-latex-header-extra
  13705. (concat "\n" org-format-latex-header-extra)
  13706. "")
  13707. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13708. (let ((dir default-directory))
  13709. (condition-case nil
  13710. (progn
  13711. (cd tmpdir)
  13712. (call-process "latex" nil nil nil texfile))
  13713. (error nil))
  13714. (cd dir))
  13715. (if (not (file-exists-p dvifile))
  13716. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13717. (condition-case nil
  13718. (call-process "dvipng" nil nil nil
  13719. "-fg" fg "-bg" bg
  13720. "-D" dpi
  13721. ;;"-x" scale "-y" scale
  13722. "-T" "tight"
  13723. "-o" pngfile
  13724. dvifile)
  13725. (error nil))
  13726. (if (not (file-exists-p pngfile))
  13727. (progn (message "Failed to create png file from %s" texfile) nil)
  13728. ;; Use the requested file name and clean up
  13729. (copy-file pngfile tofile 'replace)
  13730. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13731. (delete-file (concat texfilebase e)))
  13732. pngfile))))
  13733. (defun org-dvipng-color (attr)
  13734. "Return an rgb color specification for dvipng."
  13735. (apply 'format "rgb %s %s %s"
  13736. (mapcar 'org-normalize-color
  13737. (color-values (face-attribute 'default attr nil)))))
  13738. (defun org-normalize-color (value)
  13739. "Return string to be used as color value for an RGB component."
  13740. (format "%g" (/ value 65535.0)))
  13741. ;;;; Key bindings
  13742. ;; Make `C-c C-x' a prefix key
  13743. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13744. ;; TAB key with modifiers
  13745. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13746. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13747. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13748. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13749. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13750. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13751. ;; The following line is necessary under Suse GNU/Linux
  13752. (unless (featurep 'xemacs)
  13753. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13754. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13755. (define-key org-mode-map [backtab] 'org-shifttab)
  13756. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13757. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13758. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13759. ;; Cursor keys with modifiers
  13760. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13761. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13762. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13763. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13764. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13765. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13766. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13767. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13768. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13769. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13770. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13771. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13772. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13773. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13774. ;;; Extra keys for tty access.
  13775. ;; We only set them when really needed because otherwise the
  13776. ;; menus don't show the simple keys
  13777. (when (or org-use-extra-keys
  13778. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13779. (not window-system))
  13780. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13781. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13782. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13783. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13784. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13785. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13786. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13787. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13788. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13789. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13790. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13791. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13792. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13793. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13794. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13795. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13796. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13797. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13798. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13799. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13800. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13801. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13802. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13803. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13804. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13805. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13806. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13807. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13808. ;; All the other keys
  13809. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13810. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13811. (if (boundp 'narrow-map)
  13812. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13813. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13814. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13815. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13816. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13817. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13818. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  13819. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13820. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13821. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13822. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13823. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13824. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13825. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13826. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13827. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13828. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13829. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13830. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13831. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13832. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13833. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13834. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13835. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13836. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13837. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13838. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13839. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13840. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13841. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13842. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13843. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13844. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13845. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13846. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13847. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13848. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13849. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13850. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13851. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13852. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13853. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13854. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13855. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13856. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13857. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13858. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13859. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13860. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13861. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13862. (org-defkey org-mode-map "\C-m" 'org-return)
  13863. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13864. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13865. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13866. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13867. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13868. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13869. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13870. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13871. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13872. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13873. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13874. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13875. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13876. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13877. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13878. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13879. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13880. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13881. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13882. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13883. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13884. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13885. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13886. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13887. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13888. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13889. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13890. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13891. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13892. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13893. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13894. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13895. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13896. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13897. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13898. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13899. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13900. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13901. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13902. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13903. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13904. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13905. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13906. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13907. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13908. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13909. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13910. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13911. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13912. (when (featurep 'xemacs)
  13913. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13914. (defconst org-speed-commands-default
  13915. '(
  13916. ("Outline Navigation")
  13917. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  13918. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  13919. ("f" . (org-speed-move-safe 'org-forward-same-level))
  13920. ("b" . (org-speed-move-safe 'org-backward-same-level))
  13921. ("u" . (org-speed-move-safe 'outline-up-heading))
  13922. ("j" . org-goto)
  13923. ("g" . (org-refile t))
  13924. ("Outline Visibility")
  13925. ("c" . org-cycle)
  13926. ("C" . org-shifttab)
  13927. (" " . org-display-outline-path)
  13928. ("Outline Structure Editing")
  13929. ("U" . org-shiftmetaup)
  13930. ("D" . org-shiftmetadown)
  13931. ("r" . org-metaright)
  13932. ("l" . org-metaleft)
  13933. ("R" . org-shiftmetaright)
  13934. ("L" . org-shiftmetaleft)
  13935. ("i" . (progn (forward-char 1) (call-interactively
  13936. 'org-insert-heading-respect-content)))
  13937. ("^" . org-sort)
  13938. ("w" . org-refile)
  13939. ("a" . org-archive-subtree-default-with-confirmation)
  13940. ("." . outline-mark-subtree)
  13941. ("Clock Commands")
  13942. ("I" . org-clock-in)
  13943. ("O" . org-clock-out)
  13944. ("Meta Data Editing")
  13945. ("t" . org-todo)
  13946. ("0" . (org-priority ?\ ))
  13947. ("1" . (org-priority ?A))
  13948. ("2" . (org-priority ?B))
  13949. ("3" . (org-priority ?C))
  13950. (";" . org-set-tags-command)
  13951. ("e" . org-set-effort)
  13952. ("Agenda Views etc")
  13953. ("v" . org-agenda)
  13954. ("/" . org-sparse-tree)
  13955. ("Misc")
  13956. ("o" . org-open-at-point)
  13957. ("?" . org-speed-command-help)
  13958. )
  13959. "The default speed commands.")
  13960. (defun org-print-speed-command (e)
  13961. (if (> (length (car e)) 1)
  13962. (progn
  13963. (princ "\n")
  13964. (princ (car e))
  13965. (princ "\n")
  13966. (princ (make-string (length (car e)) ?-))
  13967. (princ "\n"))
  13968. (princ (car e))
  13969. (princ " ")
  13970. (if (symbolp (cdr e))
  13971. (princ (symbol-name (cdr e)))
  13972. (prin1 (cdr e)))
  13973. (princ "\n")))
  13974. (defun org-speed-command-help ()
  13975. "Show the available speed commands."
  13976. (interactive)
  13977. (if (not org-use-speed-commands)
  13978. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  13979. (with-output-to-temp-buffer "*Help*"
  13980. (princ "User-defined Speed commands\n===========================\n")
  13981. (mapc 'org-print-speed-command org-speed-commands-user)
  13982. (princ "\n")
  13983. (princ "Built-in Speed commands\n=======================\n")
  13984. (mapc 'org-print-speed-command org-speed-commands-default))
  13985. (with-current-buffer "*Help*"
  13986. (setq truncate-lines t))))
  13987. (defun org-speed-move-safe (cmd)
  13988. "Execute CMD, but make sure that the cursor always ends up in a headline.
  13989. If not, return to the original position and throw an error."
  13990. (interactive)
  13991. (let ((pos (point)))
  13992. (call-interactively cmd)
  13993. (unless (and (bolp) (org-on-heading-p))
  13994. (goto-char pos)
  13995. (error "Boundary reached while executing %s" cmd))))
  13996. (defvar org-self-insert-command-undo-counter 0)
  13997. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13998. (defvar org-speed-command nil)
  13999. (defun org-self-insert-command (N)
  14000. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14001. If the cursor is in a table looking at whitespace, the whitespace is
  14002. overwritten, and the table is not marked as requiring realignment."
  14003. (interactive "p")
  14004. (cond
  14005. ((and org-use-speed-commands
  14006. (or (and (bolp) (looking-at outline-regexp))
  14007. (and (functionp org-use-speed-commands)
  14008. (funcall org-use-speed-commands)))
  14009. (setq
  14010. org-speed-command
  14011. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14012. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14013. (cond
  14014. ((commandp org-speed-command)
  14015. (setq this-command org-speed-command)
  14016. (call-interactively org-speed-command))
  14017. ((functionp org-speed-command)
  14018. (funcall org-speed-command))
  14019. ((and org-speed-command (listp org-speed-command))
  14020. (eval org-speed-command))
  14021. (t (let (org-use-speed-commands)
  14022. (call-interactively 'org-self-insert-command)))))
  14023. ((and
  14024. (org-table-p)
  14025. (progn
  14026. ;; check if we blank the field, and if that triggers align
  14027. (and (featurep 'org-table) org-table-auto-blank-field
  14028. (member last-command
  14029. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14030. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14031. ;; got extra space, this field does not determine column width
  14032. (let (org-table-may-need-update) (org-table-blank-field))
  14033. ;; no extra space, this field may determine column width
  14034. (org-table-blank-field)))
  14035. t)
  14036. (eq N 1)
  14037. (looking-at "[^|\n]* |"))
  14038. (let (org-table-may-need-update)
  14039. (goto-char (1- (match-end 0)))
  14040. (delete-backward-char 1)
  14041. (goto-char (match-beginning 0))
  14042. (self-insert-command N)))
  14043. (t
  14044. (setq org-table-may-need-update t)
  14045. (self-insert-command N)
  14046. (org-fix-tags-on-the-fly)
  14047. (if org-self-insert-cluster-for-undo
  14048. (if (not (eq last-command 'org-self-insert-command))
  14049. (setq org-self-insert-command-undo-counter 1)
  14050. (if (>= org-self-insert-command-undo-counter 20)
  14051. (setq org-self-insert-command-undo-counter 1)
  14052. (and (> org-self-insert-command-undo-counter 0)
  14053. buffer-undo-list
  14054. (not (cadr buffer-undo-list)) ; remove nil entry
  14055. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14056. (setq org-self-insert-command-undo-counter
  14057. (1+ org-self-insert-command-undo-counter))))))))
  14058. (defun org-fix-tags-on-the-fly ()
  14059. (when (and (equal (char-after (point-at-bol)) ?*)
  14060. (org-on-heading-p))
  14061. (org-align-tags-here org-tags-column)))
  14062. (defun org-delete-backward-char (N)
  14063. "Like `delete-backward-char', insert whitespace at field end in tables.
  14064. When deleting backwards, in tables this function will insert whitespace in
  14065. front of the next \"|\" separator, to keep the table aligned. The table will
  14066. still be marked for re-alignment if the field did fill the entire column,
  14067. because, in this case the deletion might narrow the column."
  14068. (interactive "p")
  14069. (if (and (org-table-p)
  14070. (eq N 1)
  14071. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14072. (looking-at ".*?|"))
  14073. (let ((pos (point))
  14074. (noalign (looking-at "[^|\n\r]* |"))
  14075. (c org-table-may-need-update))
  14076. (backward-delete-char N)
  14077. (skip-chars-forward "^|")
  14078. (insert " ")
  14079. (goto-char (1- pos))
  14080. ;; noalign: if there were two spaces at the end, this field
  14081. ;; does not determine the width of the column.
  14082. (if noalign (setq org-table-may-need-update c)))
  14083. (backward-delete-char N)
  14084. (org-fix-tags-on-the-fly)))
  14085. (defun org-delete-char (N)
  14086. "Like `delete-char', but insert whitespace at field end in tables.
  14087. When deleting characters, in tables this function will insert whitespace in
  14088. front of the next \"|\" separator, to keep the table aligned. The table will
  14089. still be marked for re-alignment if the field did fill the entire column,
  14090. because, in this case the deletion might narrow the column."
  14091. (interactive "p")
  14092. (if (and (org-table-p)
  14093. (not (bolp))
  14094. (not (= (char-after) ?|))
  14095. (eq N 1))
  14096. (if (looking-at ".*?|")
  14097. (let ((pos (point))
  14098. (noalign (looking-at "[^|\n\r]* |"))
  14099. (c org-table-may-need-update))
  14100. (replace-match (concat
  14101. (substring (match-string 0) 1 -1)
  14102. " |"))
  14103. (goto-char pos)
  14104. ;; noalign: if there were two spaces at the end, this field
  14105. ;; does not determine the width of the column.
  14106. (if noalign (setq org-table-may-need-update c)))
  14107. (delete-char N))
  14108. (delete-char N)
  14109. (org-fix-tags-on-the-fly)))
  14110. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14111. (put 'org-self-insert-command 'delete-selection t)
  14112. (put 'orgtbl-self-insert-command 'delete-selection t)
  14113. (put 'org-delete-char 'delete-selection 'supersede)
  14114. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14115. (put 'org-yank 'delete-selection 'yank)
  14116. ;; Make `flyspell-mode' delay after some commands
  14117. (put 'org-self-insert-command 'flyspell-delayed t)
  14118. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14119. (put 'org-delete-char 'flyspell-delayed t)
  14120. (put 'org-delete-backward-char 'flyspell-delayed t)
  14121. ;; Make pabbrev-mode expand after org-mode commands
  14122. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14123. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14124. ;; How to do this: Measure non-white length of current string
  14125. ;; If equal to column width, we should realign.
  14126. (defun org-remap (map &rest commands)
  14127. "In MAP, remap the functions given in COMMANDS.
  14128. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14129. (let (new old)
  14130. (while commands
  14131. (setq old (pop commands) new (pop commands))
  14132. (if (fboundp 'command-remapping)
  14133. (org-defkey map (vector 'remap old) new)
  14134. (substitute-key-definition old new map global-map)))))
  14135. (when (eq org-enable-table-editor 'optimized)
  14136. ;; If the user wants maximum table support, we need to hijack
  14137. ;; some standard editing functions
  14138. (org-remap org-mode-map
  14139. 'self-insert-command 'org-self-insert-command
  14140. 'delete-char 'org-delete-char
  14141. 'delete-backward-char 'org-delete-backward-char)
  14142. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14143. (defvar org-ctrl-c-ctrl-c-hook nil
  14144. "Hook for functions attaching themselves to `C-c C-c'.
  14145. This can be used to add additional functionality to the C-c C-c key which
  14146. executes context-dependent commands.
  14147. Each function will be called with no arguments. The function must check
  14148. if the context is appropriate for it to act. If yes, it should do its
  14149. thing and then return a non-nil value. If the context is wrong,
  14150. just do nothing and return nil.")
  14151. (defvar org-tab-first-hook nil
  14152. "Hook for functions to attach themselves to TAB.
  14153. See `org-ctrl-c-ctrl-c-hook' for more information.
  14154. This hook runs as the first action when TAB is pressed, even before
  14155. `org-cycle' messes around with the `outline-regexp' to cater for
  14156. inline tasks and plain list item folding.
  14157. If any function in this hook returns t, not other actions like table
  14158. field motion visibility cycling will be done.")
  14159. (defvar org-tab-after-check-for-table-hook nil
  14160. "Hook for functions to attach themselves to TAB.
  14161. See `org-ctrl-c-ctrl-c-hook' for more information.
  14162. This hook runs after it has been established that the cursor is not in a
  14163. table, but before checking if the cursor is in a headline or if global cycling
  14164. should be done.
  14165. If any function in this hook returns t, not other actions like visibility
  14166. cycling will be done.")
  14167. (defvar org-tab-after-check-for-cycling-hook nil
  14168. "Hook for functions to attach themselves to TAB.
  14169. See `org-ctrl-c-ctrl-c-hook' for more information.
  14170. This hook runs after it has been established that not table field motion and
  14171. not visibility should be done because of current context. This is probably
  14172. the place where a package like yasnippets can hook in.")
  14173. (defvar org-tab-before-tab-emulation-hook nil
  14174. "Hook for functions to attach themselves to TAB.
  14175. See `org-ctrl-c-ctrl-c-hook' for more information.
  14176. This hook runs after every other options for TAB have been exhausted, but
  14177. before indentation and \t insertion takes place.")
  14178. (defvar org-metaleft-hook nil
  14179. "Hook for functions attaching themselves to `M-left'.
  14180. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14181. (defvar org-metaright-hook nil
  14182. "Hook for functions attaching themselves to `M-right'.
  14183. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14184. (defvar org-metaup-hook nil
  14185. "Hook for functions attaching themselves to `M-up'.
  14186. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14187. (defvar org-metadown-hook nil
  14188. "Hook for functions attaching themselves to `M-down'.
  14189. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14190. (defvar org-shiftmetaleft-hook nil
  14191. "Hook for functions attaching themselves to `M-S-left'.
  14192. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14193. (defvar org-shiftmetaright-hook nil
  14194. "Hook for functions attaching themselves to `M-S-right'.
  14195. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14196. (defvar org-shiftmetaup-hook nil
  14197. "Hook for functions attaching themselves to `M-S-up'.
  14198. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14199. (defvar org-shiftmetadown-hook nil
  14200. "Hook for functions attaching themselves to `M-S-down'.
  14201. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14202. (defvar org-metareturn-hook nil
  14203. "Hook for functions attaching themselves to `M-RET'.
  14204. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14205. (defun org-modifier-cursor-error ()
  14206. "Throw an error, a modified cursor command was applied in wrong context."
  14207. (error "This command is active in special context like tables, headlines or items"))
  14208. (defun org-shiftselect-error ()
  14209. "Throw an error because Shift-Cursor command was applied in wrong context."
  14210. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14211. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14212. (error "This command works only in special context like headlines or timestamps")))
  14213. (defun org-call-for-shift-select (cmd)
  14214. (let ((this-command-keys-shift-translated t))
  14215. (call-interactively cmd)))
  14216. (defun org-shifttab (&optional arg)
  14217. "Global visibility cycling or move to previous table field.
  14218. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14219. on context.
  14220. See the individual commands for more information."
  14221. (interactive "P")
  14222. (cond
  14223. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14224. ((integerp arg)
  14225. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14226. (message "Content view to level: %d" arg)
  14227. (org-content (prefix-numeric-value arg2))
  14228. (setq org-cycle-global-status 'overview)))
  14229. (t (call-interactively 'org-global-cycle))))
  14230. (defun org-shiftmetaleft ()
  14231. "Promote subtree or delete table column.
  14232. Calls `org-promote-subtree', `org-outdent-item',
  14233. or `org-table-delete-column', depending on context.
  14234. See the individual commands for more information."
  14235. (interactive)
  14236. (cond
  14237. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14238. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14239. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14240. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14241. (t (org-modifier-cursor-error))))
  14242. (defun org-shiftmetaright ()
  14243. "Demote subtree or insert table column.
  14244. Calls `org-demote-subtree', `org-indent-item',
  14245. or `org-table-insert-column', depending on context.
  14246. See the individual commands for more information."
  14247. (interactive)
  14248. (cond
  14249. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14250. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14251. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14252. ((org-at-item-p) (call-interactively 'org-indent-item))
  14253. (t (org-modifier-cursor-error))))
  14254. (defun org-shiftmetaup (&optional arg)
  14255. "Move subtree up or kill table row.
  14256. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14257. `org-move-item-up' depending on context. See the individual commands
  14258. for more information."
  14259. (interactive "P")
  14260. (cond
  14261. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14262. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14263. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14264. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14265. (t (org-modifier-cursor-error))))
  14266. (defun org-shiftmetadown (&optional arg)
  14267. "Move subtree down or insert table row.
  14268. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14269. `org-move-item-down', depending on context. See the individual
  14270. commands for more information."
  14271. (interactive "P")
  14272. (cond
  14273. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14274. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14275. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14276. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14277. (t (org-modifier-cursor-error))))
  14278. (defun org-metaleft (&optional arg)
  14279. "Promote heading or move table column to left.
  14280. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14281. With no specific context, calls the Emacs default `backward-word'.
  14282. See the individual commands for more information."
  14283. (interactive "P")
  14284. (cond
  14285. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14286. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14287. ((or (org-on-heading-p)
  14288. (and (org-region-active-p)
  14289. (save-excursion
  14290. (goto-char (region-beginning))
  14291. (org-on-heading-p))))
  14292. (call-interactively 'org-do-promote))
  14293. ((or (org-at-item-p)
  14294. (and (org-region-active-p)
  14295. (save-excursion
  14296. (goto-char (region-beginning))
  14297. (org-at-item-p))))
  14298. (call-interactively 'org-outdent-item))
  14299. (t (call-interactively 'backward-word))))
  14300. (defun org-metaright (&optional arg)
  14301. "Demote subtree or move table column to right.
  14302. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14303. With no specific context, calls the Emacs default `forward-word'.
  14304. See the individual commands for more information."
  14305. (interactive "P")
  14306. (cond
  14307. ((run-hook-with-args-until-success 'org-metaright-hook))
  14308. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14309. ((or (org-on-heading-p)
  14310. (and (org-region-active-p)
  14311. (save-excursion
  14312. (goto-char (region-beginning))
  14313. (org-on-heading-p))))
  14314. (call-interactively 'org-do-demote))
  14315. ((or (org-at-item-p)
  14316. (and (org-region-active-p)
  14317. (save-excursion
  14318. (goto-char (region-beginning))
  14319. (org-at-item-p))))
  14320. (call-interactively 'org-indent-item))
  14321. (t (call-interactively 'forward-word))))
  14322. (defun org-metaup (&optional arg)
  14323. "Move subtree up or move table row up.
  14324. Calls `org-move-subtree-up' or `org-table-move-row' or
  14325. `org-move-item-up', depending on context. See the individual commands
  14326. for more information."
  14327. (interactive "P")
  14328. (cond
  14329. ((run-hook-with-args-until-success 'org-metaup-hook))
  14330. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14331. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14332. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14333. (t (transpose-lines 1) (beginning-of-line -1))))
  14334. (defun org-metadown (&optional arg)
  14335. "Move subtree down or move table row down.
  14336. Calls `org-move-subtree-down' or `org-table-move-row' or
  14337. `org-move-item-down', depending on context. See the individual
  14338. commands for more information."
  14339. (interactive "P")
  14340. (cond
  14341. ((run-hook-with-args-until-success 'org-metadown-hook))
  14342. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14343. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14344. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14345. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14346. (defun org-shiftup (&optional arg)
  14347. "Increase item in timestamp or increase priority of current headline.
  14348. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14349. depending on context. See the individual commands for more information."
  14350. (interactive "P")
  14351. (cond
  14352. ((and org-support-shift-select (org-region-active-p))
  14353. (org-call-for-shift-select 'previous-line))
  14354. ((org-at-timestamp-p t)
  14355. (call-interactively (if org-edit-timestamp-down-means-later
  14356. 'org-timestamp-down 'org-timestamp-up)))
  14357. ((and (not (eq org-support-shift-select 'always))
  14358. org-enable-priority-commands
  14359. (org-on-heading-p))
  14360. (call-interactively 'org-priority-up))
  14361. ((and (not org-support-shift-select) (org-at-item-p))
  14362. (call-interactively 'org-previous-item))
  14363. ((org-clocktable-try-shift 'up arg))
  14364. (org-support-shift-select
  14365. (org-call-for-shift-select 'previous-line))
  14366. (t (org-shiftselect-error))))
  14367. (defun org-shiftdown (&optional arg)
  14368. "Decrease item in timestamp or decrease priority of current headline.
  14369. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14370. depending on context. See the individual commands for more information."
  14371. (interactive "P")
  14372. (cond
  14373. ((and org-support-shift-select (org-region-active-p))
  14374. (org-call-for-shift-select 'next-line))
  14375. ((org-at-timestamp-p t)
  14376. (call-interactively (if org-edit-timestamp-down-means-later
  14377. 'org-timestamp-up 'org-timestamp-down)))
  14378. ((and (not (eq org-support-shift-select 'always))
  14379. org-enable-priority-commands
  14380. (org-on-heading-p))
  14381. (call-interactively 'org-priority-down))
  14382. ((and (not org-support-shift-select) (org-at-item-p))
  14383. (call-interactively 'org-next-item))
  14384. ((org-clocktable-try-shift 'down arg))
  14385. (org-support-shift-select
  14386. (org-call-for-shift-select 'next-line))
  14387. (t (org-shiftselect-error))))
  14388. (defun org-shiftright (&optional arg)
  14389. "Cycle the thing at point or in the current line, depending on context.
  14390. Depending on context, this does one of the following:
  14391. - switch a timestamp at point one day into the future
  14392. - on a headline, switch to the next TODO keyword.
  14393. - on an item, switch entire list to the next bullet type
  14394. - on a property line, switch to the next allowed value
  14395. - on a clocktable definition line, move time block into the future"
  14396. (interactive "P")
  14397. (cond
  14398. ((and org-support-shift-select (org-region-active-p))
  14399. (org-call-for-shift-select 'forward-char))
  14400. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14401. ((and (not (eq org-support-shift-select 'always))
  14402. (org-on-heading-p))
  14403. (let ((org-inhibit-logging
  14404. (not org-treat-S-cursor-todo-selection-as-state-change))
  14405. (org-inhibit-blocking
  14406. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14407. (org-call-with-arg 'org-todo 'right)))
  14408. ((or (and org-support-shift-select
  14409. (not (eq org-support-shift-select 'always))
  14410. (org-at-item-bullet-p))
  14411. (and (not org-support-shift-select) (org-at-item-p)))
  14412. (org-call-with-arg 'org-cycle-list-bullet nil))
  14413. ((and (not (eq org-support-shift-select 'always))
  14414. (org-at-property-p))
  14415. (call-interactively 'org-property-next-allowed-value))
  14416. ((org-clocktable-try-shift 'right arg))
  14417. (org-support-shift-select
  14418. (org-call-for-shift-select 'forward-char))
  14419. (t (org-shiftselect-error))))
  14420. (defun org-shiftleft (&optional arg)
  14421. "Cycle the thing at point or in the current line, depending on context.
  14422. Depending on context, this does one of the following:
  14423. - switch a timestamp at point one day into the past
  14424. - on a headline, switch to the previous TODO keyword.
  14425. - on an item, switch entire list to the previous bullet type
  14426. - on a property line, switch to the previous allowed value
  14427. - on a clocktable definition line, move time block into the past"
  14428. (interactive "P")
  14429. (cond
  14430. ((and org-support-shift-select (org-region-active-p))
  14431. (org-call-for-shift-select 'backward-char))
  14432. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14433. ((and (not (eq org-support-shift-select 'always))
  14434. (org-on-heading-p))
  14435. (let ((org-inhibit-logging
  14436. (not org-treat-S-cursor-todo-selection-as-state-change))
  14437. (org-inhibit-blocking
  14438. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14439. (org-call-with-arg 'org-todo 'left)))
  14440. ((or (and org-support-shift-select
  14441. (not (eq org-support-shift-select 'always))
  14442. (org-at-item-bullet-p))
  14443. (and (not org-support-shift-select) (org-at-item-p)))
  14444. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14445. ((and (not (eq org-support-shift-select 'always))
  14446. (org-at-property-p))
  14447. (call-interactively 'org-property-previous-allowed-value))
  14448. ((org-clocktable-try-shift 'left arg))
  14449. (org-support-shift-select
  14450. (org-call-for-shift-select 'backward-char))
  14451. (t (org-shiftselect-error))))
  14452. (defun org-shiftcontrolright ()
  14453. "Switch to next TODO set."
  14454. (interactive)
  14455. (cond
  14456. ((and org-support-shift-select (org-region-active-p))
  14457. (org-call-for-shift-select 'forward-word))
  14458. ((and (not (eq org-support-shift-select 'always))
  14459. (org-on-heading-p))
  14460. (org-call-with-arg 'org-todo 'nextset))
  14461. (org-support-shift-select
  14462. (org-call-for-shift-select 'forward-word))
  14463. (t (org-shiftselect-error))))
  14464. (defun org-shiftcontrolleft ()
  14465. "Switch to previous TODO set."
  14466. (interactive)
  14467. (cond
  14468. ((and org-support-shift-select (org-region-active-p))
  14469. (org-call-for-shift-select 'backward-word))
  14470. ((and (not (eq org-support-shift-select 'always))
  14471. (org-on-heading-p))
  14472. (org-call-with-arg 'org-todo 'previousset))
  14473. (org-support-shift-select
  14474. (org-call-for-shift-select 'backward-word))
  14475. (t (org-shiftselect-error))))
  14476. (defun org-ctrl-c-ret ()
  14477. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14478. (interactive)
  14479. (cond
  14480. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14481. (t (call-interactively 'org-insert-heading))))
  14482. (defun org-copy-special ()
  14483. "Copy region in table or copy current subtree.
  14484. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14485. See the individual commands for more information."
  14486. (interactive)
  14487. (call-interactively
  14488. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14489. (defun org-cut-special ()
  14490. "Cut region in table or cut current subtree.
  14491. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14492. See the individual commands for more information."
  14493. (interactive)
  14494. (call-interactively
  14495. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14496. (defun org-paste-special (arg)
  14497. "Paste rectangular region into table, or past subtree relative to level.
  14498. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14499. See the individual commands for more information."
  14500. (interactive "P")
  14501. (if (org-at-table-p)
  14502. (org-table-paste-rectangle)
  14503. (org-paste-subtree arg)))
  14504. (defun org-edit-special ()
  14505. "Call a special editor for the stuff at point.
  14506. When at a table, call the formula editor with `org-table-edit-formulas'.
  14507. When at the first line of an src example, call `org-edit-src-code'.
  14508. When in an #+include line, visit the include file. Otherwise call
  14509. `ffap' to visit the file at point."
  14510. (interactive)
  14511. (cond
  14512. ((org-at-table-p)
  14513. (call-interactively 'org-table-edit-formulas))
  14514. ((save-excursion
  14515. (beginning-of-line 1)
  14516. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14517. (find-file (org-trim (match-string 1))))
  14518. ((org-edit-src-code))
  14519. ((org-edit-fixed-width-region))
  14520. (t (call-interactively 'ffap))))
  14521. (defun org-ctrl-c-ctrl-c (&optional arg)
  14522. "Set tags in headline, or update according to changed information at point.
  14523. This command does many different things, depending on context:
  14524. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14525. this is what we do.
  14526. - If the cursor is on a statistics cookie, update it.
  14527. - If the cursor is in a headline, prompt for tags and insert them
  14528. into the current line, aligned to `org-tags-column'. When called
  14529. with prefix arg, realign all tags in the current buffer.
  14530. - If the cursor is in one of the special #+KEYWORD lines, this
  14531. triggers scanning the buffer for these lines and updating the
  14532. information.
  14533. - If the cursor is inside a table, realign the table. This command
  14534. works even if the automatic table editor has been turned off.
  14535. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14536. the entire table.
  14537. - If the cursor is at a footnote reference or definition, jump to
  14538. the corresponding definition or references, respectively.
  14539. - If the cursor is a the beginning of a dynamic block, update it.
  14540. - If the cursor is inside a table created by the table.el package,
  14541. activate that table.
  14542. - If the current buffer is a remember buffer, close note and file
  14543. it. A prefix argument of 1 files to the default location
  14544. without further interaction. A prefix argument of 2 files to
  14545. the currently clocking task.
  14546. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14547. links in this buffer.
  14548. - If the cursor is on a numbered item in a plain list, renumber the
  14549. ordered list.
  14550. - If the cursor is on a checkbox, toggle it."
  14551. (interactive "P")
  14552. (let ((org-enable-table-editor t))
  14553. (cond
  14554. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14555. org-occur-highlights
  14556. org-latex-fragment-image-overlays)
  14557. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14558. (org-remove-occur-highlights)
  14559. (org-remove-latex-fragment-image-overlays)
  14560. (message "Temporary highlights/overlays removed from current buffer"))
  14561. ((and (local-variable-p 'org-finish-function (current-buffer))
  14562. (fboundp org-finish-function))
  14563. (funcall org-finish-function))
  14564. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14565. ((org-at-property-p)
  14566. (call-interactively 'org-property-action))
  14567. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14568. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14569. (or (org-on-heading-p) (org-at-item-p)))
  14570. (call-interactively 'org-update-statistics-cookies))
  14571. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14572. ((org-at-table.el-p)
  14573. (require 'table)
  14574. (beginning-of-line 1)
  14575. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14576. (call-interactively 'table-recognize-table))
  14577. ((org-at-table-p)
  14578. (org-table-maybe-eval-formula)
  14579. (if arg
  14580. (call-interactively 'org-table-recalculate)
  14581. (org-table-maybe-recalculate-line))
  14582. (call-interactively 'org-table-align))
  14583. ((or (org-footnote-at-reference-p)
  14584. (org-footnote-at-definition-p))
  14585. (call-interactively 'org-footnote-action))
  14586. ((org-at-item-checkbox-p)
  14587. (call-interactively 'org-toggle-checkbox))
  14588. ((org-at-item-p)
  14589. (if arg
  14590. (call-interactively 'org-toggle-checkbox)
  14591. (call-interactively 'org-maybe-renumber-ordered-list)))
  14592. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14593. ;; Dynamic block
  14594. (beginning-of-line 1)
  14595. (save-excursion (org-update-dblock)))
  14596. ((save-excursion
  14597. (beginning-of-line 1)
  14598. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14599. (cond
  14600. ((equal (match-string 1) "TBLFM")
  14601. ;; Recalculate the table before this line
  14602. (save-excursion
  14603. (beginning-of-line 1)
  14604. (skip-chars-backward " \r\n\t")
  14605. (if (org-at-table-p)
  14606. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14607. (t
  14608. (let ((org-inhibit-startup-visibility-stuff t)
  14609. (org-startup-align-all-tables nil))
  14610. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  14611. (message "Local setup has been refreshed"))))
  14612. ((org-clock-update-time-maybe))
  14613. (t (error "C-c C-c can do nothing useful at this location")))))
  14614. (defun org-mode-restart ()
  14615. "Restart Org-mode, to scan again for special lines.
  14616. Also updates the keyword regular expressions."
  14617. (interactive)
  14618. (org-mode)
  14619. (message "Org-mode restarted"))
  14620. (defun org-kill-note-or-show-branches ()
  14621. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14622. (interactive)
  14623. (if (not org-finish-function)
  14624. (call-interactively 'show-branches)
  14625. (let ((org-note-abort t))
  14626. (funcall org-finish-function))))
  14627. (defun org-return (&optional indent)
  14628. "Goto next table row or insert a newline.
  14629. Calls `org-table-next-row' or `newline', depending on context.
  14630. See the individual commands for more information."
  14631. (interactive)
  14632. (cond
  14633. ((bobp) (if indent (newline-and-indent) (newline)))
  14634. ((org-at-table-p)
  14635. (org-table-justify-field-maybe)
  14636. (call-interactively 'org-table-next-row))
  14637. ((and org-return-follows-link
  14638. (eq (get-text-property (point) 'face) 'org-link))
  14639. (call-interactively 'org-open-at-point))
  14640. ((and (org-at-heading-p)
  14641. (looking-at
  14642. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14643. (org-show-entry)
  14644. (end-of-line 1)
  14645. (newline))
  14646. (t (if indent (newline-and-indent) (newline)))))
  14647. (defun org-return-indent ()
  14648. "Goto next table row or insert a newline and indent.
  14649. Calls `org-table-next-row' or `newline-and-indent', depending on
  14650. context. See the individual commands for more information."
  14651. (interactive)
  14652. (org-return t))
  14653. (defun org-ctrl-c-star ()
  14654. "Compute table, or change heading status of lines.
  14655. Calls `org-table-recalculate' or `org-toggle-heading',
  14656. depending on context."
  14657. (interactive)
  14658. (cond
  14659. ((org-at-table-p)
  14660. (call-interactively 'org-table-recalculate))
  14661. (t
  14662. ;; Convert all lines in region to list items
  14663. (call-interactively 'org-toggle-heading))))
  14664. (defun org-ctrl-c-minus ()
  14665. "Insert separator line in table or modify bullet status of line.
  14666. Also turns a plain line or a region of lines into list items.
  14667. Calls `org-table-insert-hline', `org-toggle-item', or
  14668. `org-cycle-list-bullet', depending on context."
  14669. (interactive)
  14670. (cond
  14671. ((org-at-table-p)
  14672. (call-interactively 'org-table-insert-hline))
  14673. ((org-region-active-p)
  14674. (call-interactively 'org-toggle-item))
  14675. ((org-in-item-p)
  14676. (call-interactively 'org-cycle-list-bullet))
  14677. (t
  14678. (call-interactively 'org-toggle-item))))
  14679. (defun org-toggle-item ()
  14680. "Convert headings or normal lines to items, items to normal lines.
  14681. If there is no active region, only the current line is considered.
  14682. If the first line in the region is a headline, convert all headlines to items.
  14683. If the first line in the region is an item, convert all items to normal lines.
  14684. If the first line is normal text, add an item bullet to each line."
  14685. (interactive)
  14686. (let (l2 l beg end)
  14687. (if (org-region-active-p)
  14688. (setq beg (region-beginning) end (region-end))
  14689. (setq beg (point-at-bol)
  14690. end (min (1+ (point-at-eol)) (point-max))))
  14691. (save-excursion
  14692. (goto-char end)
  14693. (setq l2 (org-current-line))
  14694. (goto-char beg)
  14695. (beginning-of-line 1)
  14696. (setq l (1- (org-current-line)))
  14697. (if (org-at-item-p)
  14698. ;; We already have items, de-itemize
  14699. (while (< (setq l (1+ l)) l2)
  14700. (when (org-at-item-p)
  14701. (goto-char (match-beginning 2))
  14702. (delete-region (match-beginning 2) (match-end 2))
  14703. (and (looking-at "[ \t]+") (replace-match "")))
  14704. (beginning-of-line 2))
  14705. (if (org-on-heading-p)
  14706. ;; Headings, convert to items
  14707. (while (< (setq l (1+ l)) l2)
  14708. (if (looking-at org-outline-regexp)
  14709. (replace-match "- " t t))
  14710. (beginning-of-line 2))
  14711. ;; normal lines, turn them into items
  14712. (while (< (setq l (1+ l)) l2)
  14713. (unless (org-at-item-p)
  14714. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14715. (replace-match "\\1- \\2")))
  14716. (beginning-of-line 2)))))))
  14717. (defun org-toggle-heading (&optional nstars)
  14718. "Convert headings to normal text, or items or text to headings.
  14719. If there is no active region, only the current line is considered.
  14720. If the first line is a heading, remove the stars from all headlines
  14721. in the region.
  14722. If the first line is a plain list item, turn all plain list items
  14723. into headings.
  14724. If the first line is a normal line, turn each and every line in the
  14725. region into a heading.
  14726. When converting a line into a heading, the number of stars is chosen
  14727. such that the lines become children of the current entry. However,
  14728. when a prefix argument is given, its value determines the number of
  14729. stars to add."
  14730. (interactive "P")
  14731. (let (l2 l itemp beg end)
  14732. (if (org-region-active-p)
  14733. (setq beg (region-beginning) end (region-end))
  14734. (setq beg (point-at-bol)
  14735. end (min (1+ (point-at-eol)) (point-max))))
  14736. (save-excursion
  14737. (goto-char end)
  14738. (setq l2 (org-current-line))
  14739. (goto-char beg)
  14740. (beginning-of-line 1)
  14741. (setq l (1- (org-current-line)))
  14742. (if (org-on-heading-p)
  14743. ;; We already have headlines, de-star them
  14744. (while (< (setq l (1+ l)) l2)
  14745. (when (org-on-heading-p t)
  14746. (and (looking-at outline-regexp) (replace-match "")))
  14747. (beginning-of-line 2))
  14748. (setq itemp (org-at-item-p))
  14749. (let* ((stars
  14750. (if nstars
  14751. (make-string (prefix-numeric-value current-prefix-arg)
  14752. ?*)
  14753. (save-excursion
  14754. (if (re-search-backward org-complex-heading-regexp nil t)
  14755. (match-string 1) ""))))
  14756. (add-stars (cond (nstars "")
  14757. ((equal stars "") "*")
  14758. (org-odd-levels-only "**")
  14759. (t "*")))
  14760. (rpl (concat stars add-stars " ")))
  14761. (while (< (setq l (1+ l)) l2)
  14762. (if itemp
  14763. (and (org-at-item-p) (replace-match rpl t t))
  14764. (unless (org-on-heading-p)
  14765. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14766. (replace-match (concat rpl (match-string 2))))))
  14767. (beginning-of-line 2)))))))
  14768. (defun org-meta-return (&optional arg)
  14769. "Insert a new heading or wrap a region in a table.
  14770. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14771. See the individual commands for more information."
  14772. (interactive "P")
  14773. (cond
  14774. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14775. ((org-at-table-p)
  14776. (call-interactively 'org-table-wrap-region))
  14777. (t (call-interactively 'org-insert-heading))))
  14778. ;;; Menu entries
  14779. ;; Define the Org-mode menus
  14780. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14781. '("Tbl"
  14782. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14783. ["Next Field" org-cycle (org-at-table-p)]
  14784. ["Previous Field" org-shifttab (org-at-table-p)]
  14785. ["Next Row" org-return (org-at-table-p)]
  14786. "--"
  14787. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14788. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14789. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14790. "--"
  14791. ("Column"
  14792. ["Move Column Left" org-metaleft (org-at-table-p)]
  14793. ["Move Column Right" org-metaright (org-at-table-p)]
  14794. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14795. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14796. ("Row"
  14797. ["Move Row Up" org-metaup (org-at-table-p)]
  14798. ["Move Row Down" org-metadown (org-at-table-p)]
  14799. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14800. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14801. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14802. "--"
  14803. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14804. ("Rectangle"
  14805. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14806. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14807. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14808. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14809. "--"
  14810. ("Calculate"
  14811. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14812. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14813. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14814. "--"
  14815. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14816. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14817. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14818. "--"
  14819. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14820. "--"
  14821. ["Sum Column/Rectangle" org-table-sum
  14822. (or (org-at-table-p) (org-region-active-p))]
  14823. ["Which Column?" org-table-current-column (org-at-table-p)])
  14824. ["Debug Formulas"
  14825. org-table-toggle-formula-debugger
  14826. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14827. ["Show Col/Row Numbers"
  14828. org-table-toggle-coordinate-overlays
  14829. :style toggle
  14830. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14831. "--"
  14832. ["Create" org-table-create (and (not (org-at-table-p))
  14833. org-enable-table-editor)]
  14834. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14835. ["Import from File" org-table-import (not (org-at-table-p))]
  14836. ["Export to File" org-table-export (org-at-table-p)]
  14837. "--"
  14838. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14839. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14840. '("Org"
  14841. ("Show/Hide"
  14842. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14843. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14844. ["Sparse Tree..." org-sparse-tree t]
  14845. ["Reveal Context" org-reveal t]
  14846. ["Show All" show-all t]
  14847. "--"
  14848. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14849. "--"
  14850. ["New Heading" org-insert-heading t]
  14851. ("Navigate Headings"
  14852. ["Up" outline-up-heading t]
  14853. ["Next" outline-next-visible-heading t]
  14854. ["Previous" outline-previous-visible-heading t]
  14855. ["Next Same Level" outline-forward-same-level t]
  14856. ["Previous Same Level" outline-backward-same-level t]
  14857. "--"
  14858. ["Jump" org-goto t])
  14859. ("Edit Structure"
  14860. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14861. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14862. "--"
  14863. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14864. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14865. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14866. "--"
  14867. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14868. "--"
  14869. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14870. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14871. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14872. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14873. "--"
  14874. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14875. "--"
  14876. ["Convert to odd levels" org-convert-to-odd-levels t]
  14877. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14878. ("Editing"
  14879. ["Emphasis..." org-emphasize t]
  14880. ["Edit Source Example" org-edit-special t]
  14881. "--"
  14882. ["Footnote new/jump" org-footnote-action t]
  14883. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14884. ("Archive"
  14885. ["Archive (default method)" org-archive-subtree-default t]
  14886. "--"
  14887. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  14888. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14889. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  14890. )
  14891. "--"
  14892. ("Hyperlinks"
  14893. ["Store Link (Global)" org-store-link t]
  14894. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14895. ["Insert Link" org-insert-link t]
  14896. ["Follow Link" org-open-at-point t]
  14897. "--"
  14898. ["Next link" org-next-link t]
  14899. ["Previous link" org-previous-link t]
  14900. "--"
  14901. ["Descriptive Links"
  14902. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14903. :style radio
  14904. :selected (member '(org-link) buffer-invisibility-spec)]
  14905. ["Literal Links"
  14906. (progn
  14907. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14908. :style radio
  14909. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14910. "--"
  14911. ("TODO Lists"
  14912. ["TODO/DONE/-" org-todo t]
  14913. ("Select keyword"
  14914. ["Next keyword" org-shiftright (org-on-heading-p)]
  14915. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14916. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14917. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14918. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14919. ["Show TODO Tree" org-show-todo-tree t]
  14920. ["Global TODO list" org-todo-list t]
  14921. "--"
  14922. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14923. :selected org-enforce-todo-dependencies :style toggle :active t]
  14924. "Settings for tree at point"
  14925. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14926. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14927. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14928. ["Do Children parallel" org-toggle-ordered-property :style radio
  14929. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14930. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14931. "--"
  14932. ["Set Priority" org-priority t]
  14933. ["Priority Up" org-shiftup t]
  14934. ["Priority Down" org-shiftdown t]
  14935. "--"
  14936. ["Get news from all feeds" org-feed-update-all t]
  14937. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14938. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14939. ("TAGS and Properties"
  14940. ["Set Tags" org-set-tags-command t]
  14941. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14942. "--"
  14943. ["Set property" org-set-property t]
  14944. ["Column view of properties" org-columns t]
  14945. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14946. ("Dates and Scheduling"
  14947. ["Timestamp" org-time-stamp t]
  14948. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14949. ("Change Date"
  14950. ["1 Day Later" org-shiftright t]
  14951. ["1 Day Earlier" org-shiftleft t]
  14952. ["1 ... Later" org-shiftup t]
  14953. ["1 ... Earlier" org-shiftdown t])
  14954. ["Compute Time Range" org-evaluate-time-range t]
  14955. ["Schedule Item" org-schedule t]
  14956. ["Deadline" org-deadline t]
  14957. "--"
  14958. ["Custom time format" org-toggle-time-stamp-overlays
  14959. :style radio :selected org-display-custom-times]
  14960. "--"
  14961. ["Goto Calendar" org-goto-calendar t]
  14962. ["Date from Calendar" org-date-from-calendar t]
  14963. "--"
  14964. ["Start/Restart Timer" org-timer-start t]
  14965. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14966. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14967. ["Insert Timer String" org-timer t]
  14968. ["Insert Timer Item" org-timer-item t])
  14969. ("Logging work"
  14970. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14971. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14972. ["Clock out" org-clock-out t]
  14973. ["Clock cancel" org-clock-cancel t]
  14974. "--"
  14975. ["Mark as default task" org-clock-mark-default-task t]
  14976. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14977. ["Goto running clock" org-clock-goto t]
  14978. "--"
  14979. ["Display times" org-clock-display t]
  14980. ["Create clock table" org-clock-report t]
  14981. "--"
  14982. ["Record DONE time"
  14983. (progn (setq org-log-done (not org-log-done))
  14984. (message "Switching to %s will %s record a timestamp"
  14985. (car org-done-keywords)
  14986. (if org-log-done "automatically" "not")))
  14987. :style toggle :selected org-log-done])
  14988. "--"
  14989. ["Agenda Command..." org-agenda t]
  14990. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14991. ("File List for Agenda")
  14992. ("Special views current file"
  14993. ["TODO Tree" org-show-todo-tree t]
  14994. ["Check Deadlines" org-check-deadlines t]
  14995. ["Timeline" org-timeline t]
  14996. ["Tags/Property tree" org-match-sparse-tree t])
  14997. "--"
  14998. ["Export/Publish..." org-export t]
  14999. ("LaTeX"
  15000. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15001. :selected org-cdlatex-mode]
  15002. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15003. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15004. ["Modify math symbol" org-cdlatex-math-modify
  15005. (org-inside-LaTeX-fragment-p)]
  15006. ["Insert citation" org-reftex-citation t]
  15007. "--"
  15008. ["Export LaTeX fragments as images"
  15009. (if (featurep 'org-exp)
  15010. (setq org-export-with-LaTeX-fragments
  15011. (not org-export-with-LaTeX-fragments))
  15012. (require 'org-exp))
  15013. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15014. org-export-with-LaTeX-fragments)]
  15015. "--"
  15016. ["Template for BEAMER" org-beamer-settings-template t])
  15017. "--"
  15018. ("MobileOrg"
  15019. ["Push Files and Views" org-mobile-push t]
  15020. ["Get Captured and Flagged" org-mobile-pull t]
  15021. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15022. "--"
  15023. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15024. "--"
  15025. ("Documentation"
  15026. ["Show Version" org-version t]
  15027. ["Info Documentation" org-info t])
  15028. ("Customize"
  15029. ["Browse Org Group" org-customize t]
  15030. "--"
  15031. ["Expand This Menu" org-create-customize-menu
  15032. (fboundp 'customize-menu-create)])
  15033. ["Send bug report" org-submit-bug-report t]
  15034. "--"
  15035. ("Refresh/Reload"
  15036. ["Refresh setup current buffer" org-mode-restart t]
  15037. ["Reload Org (after update)" org-reload t]
  15038. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15039. ))
  15040. (defun org-info (&optional node)
  15041. "Read documentation for Org-mode in the info system.
  15042. With optional NODE, go directly to that node."
  15043. (interactive)
  15044. (info (format "(org)%s" (or node ""))))
  15045. ;;;###autoload
  15046. (defun org-submit-bug-report ()
  15047. "Submit a bug report on Org-mode via mail.
  15048. Don't hesitate to report any problems or inaccurate documentation.
  15049. If you don't have setup sending mail from (X)Emacs, please copy the
  15050. output buffer into your mail program, as it gives us important
  15051. information about your Org-mode version and configuration."
  15052. (interactive)
  15053. (require 'reporter)
  15054. (org-load-modules-maybe)
  15055. (org-require-autoloaded-modules)
  15056. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15057. (reporter-submit-bug-report
  15058. "emacs-orgmode@gnu.org"
  15059. (org-version)
  15060. (let (list)
  15061. (save-window-excursion
  15062. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15063. (delete-other-windows)
  15064. (erase-buffer)
  15065. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15066. We would like to add your full Org-mode and Outline configuration to the
  15067. bug report. This greatly simplifies the work of the maintainer and
  15068. other experts on the mailing list.
  15069. HOWEVER, some variables you have customized may contain private
  15070. information. The names of customers, colleagues, or friends, might
  15071. appear in the form of file names, tags, todo states, or search strings.
  15072. If you answer yes to the prompt, you might want to check and remove
  15073. such private information before sending the email.")
  15074. (add-text-properties (point-min) (point-max) '(face org-warning))
  15075. (when (yes-or-no-p "Include your Org-mode configuration ")
  15076. (mapatoms
  15077. (lambda (v)
  15078. (and (boundp v)
  15079. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15080. (or (and (symbol-value v)
  15081. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15082. (and
  15083. (get v 'custom-type) (get v 'standard-value)
  15084. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15085. (push v list)))))
  15086. (kill-buffer (get-buffer "*Warn about privacy*"))
  15087. list))
  15088. nil nil
  15089. "Remember to cover the basics, that is, what you expected to happen and
  15090. what in fact did happen. You don't know how to make a good report? See
  15091. http://orgmode.org/manual/Feedback.html#Feedback
  15092. Your bug report will be posted to the Org-mode mailing list.
  15093. ------------------------------------------------------------------------")
  15094. (save-excursion
  15095. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15096. (replace-match "\\1Bug: \\3 [\\2]")))))
  15097. (defun org-install-agenda-files-menu ()
  15098. (let ((bl (buffer-list)))
  15099. (save-excursion
  15100. (while bl
  15101. (set-buffer (pop bl))
  15102. (if (org-mode-p) (setq bl nil)))
  15103. (when (org-mode-p)
  15104. (easy-menu-change
  15105. '("Org") "File List for Agenda"
  15106. (append
  15107. (list
  15108. ["Edit File List" (org-edit-agenda-file-list) t]
  15109. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15110. ["Remove Current File from List" org-remove-file t]
  15111. ["Cycle through agenda files" org-cycle-agenda-files t]
  15112. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15113. "--")
  15114. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15115. ;;;; Documentation
  15116. ;;;###autoload
  15117. (defun org-require-autoloaded-modules ()
  15118. (interactive)
  15119. (mapc 'require
  15120. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15121. org-docbook org-exp org-html org-icalendar
  15122. org-id org-latex
  15123. org-publish org-remember org-table
  15124. org-timer org-xoxo)))
  15125. ;;;###autoload
  15126. (defun org-reload (&optional uncompiled)
  15127. "Reload all org lisp files.
  15128. With prefix arg UNCOMPILED, load the uncompiled versions."
  15129. (interactive "P")
  15130. (require 'find-func)
  15131. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15132. (dir-org (file-name-directory (org-find-library-name "org")))
  15133. (dir-org-contrib (ignore-errors
  15134. (file-name-directory
  15135. (org-find-library-name "org-contribdir"))))
  15136. (files
  15137. (append (directory-files dir-org t file-re)
  15138. (and dir-org-contrib
  15139. (directory-files dir-org-contrib t file-re))))
  15140. (remove-re (concat (if (featurep 'xemacs)
  15141. "org-colview" "org-colview-xemacs")
  15142. "\\'")))
  15143. (setq files (mapcar 'file-name-sans-extension files))
  15144. (setq files (mapcar
  15145. (lambda (x) (if (string-match remove-re x) nil x))
  15146. files))
  15147. (setq files (delq nil files))
  15148. (mapc
  15149. (lambda (f)
  15150. (when (featurep (intern (file-name-nondirectory f)))
  15151. (if (and (not uncompiled)
  15152. (file-exists-p (concat f ".elc")))
  15153. (load (concat f ".elc") nil nil t)
  15154. (load (concat f ".el") nil nil t))))
  15155. files))
  15156. (org-version))
  15157. ;;;###autoload
  15158. (defun org-customize ()
  15159. "Call the customize function with org as argument."
  15160. (interactive)
  15161. (org-load-modules-maybe)
  15162. (org-require-autoloaded-modules)
  15163. (customize-browse 'org))
  15164. (defun org-create-customize-menu ()
  15165. "Create a full customization menu for Org-mode, insert it into the menu."
  15166. (interactive)
  15167. (org-load-modules-maybe)
  15168. (org-require-autoloaded-modules)
  15169. (if (fboundp 'customize-menu-create)
  15170. (progn
  15171. (easy-menu-change
  15172. '("Org") "Customize"
  15173. `(["Browse Org group" org-customize t]
  15174. "--"
  15175. ,(customize-menu-create 'org)
  15176. ["Set" Custom-set t]
  15177. ["Save" Custom-save t]
  15178. ["Reset to Current" Custom-reset-current t]
  15179. ["Reset to Saved" Custom-reset-saved t]
  15180. ["Reset to Standard Settings" Custom-reset-standard t]))
  15181. (message "\"Org\"-menu now contains full customization menu"))
  15182. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15183. ;;;; Miscellaneous stuff
  15184. ;;; Generally useful functions
  15185. (defun org-get-at-bol (property)
  15186. "Get text property PROPERTY at beginning of line."
  15187. (get-text-property (point-at-bol) property))
  15188. (defun org-find-text-property-in-string (prop s)
  15189. "Return the first non-nil value of property PROP in string S."
  15190. (or (get-text-property 0 prop s)
  15191. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15192. prop s)))
  15193. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15194. "Display the given MESSAGE as a warning."
  15195. (if (fboundp 'display-warning)
  15196. (display-warning 'org message
  15197. (if (featurep 'xemacs)
  15198. 'warning
  15199. :warning))
  15200. (let ((buf (get-buffer-create "*Org warnings*")))
  15201. (with-current-buffer buf
  15202. (goto-char (point-max))
  15203. (insert "Warning (Org): " message)
  15204. (unless (bolp)
  15205. (newline)))
  15206. (display-buffer buf)
  15207. (sit-for 0))))
  15208. (defun org-in-commented-line ()
  15209. "Is point in a line starting with `#'?"
  15210. (equal (char-after (point-at-bol)) ?#))
  15211. (defun org-in-verbatim-emphasis ()
  15212. (save-match-data
  15213. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15214. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15215. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15216. (if (and marker (marker-buffer marker)
  15217. (buffer-live-p (marker-buffer marker)))
  15218. (progn
  15219. (switch-to-buffer (marker-buffer marker))
  15220. (if (or (> marker (point-max)) (< marker (point-min)))
  15221. (widen))
  15222. (goto-char marker)
  15223. (org-show-context 'org-goto))
  15224. (if bookmark
  15225. (bookmark-jump bookmark)
  15226. (error "Cannot find location"))))
  15227. (defun org-quote-csv-field (s)
  15228. "Quote field for inclusion in CSV material."
  15229. (if (string-match "[\",]" s)
  15230. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15231. s))
  15232. (defun org-plist-delete (plist property)
  15233. "Delete PROPERTY from PLIST.
  15234. This is in contrast to merely setting it to 0."
  15235. (let (p)
  15236. (while plist
  15237. (if (not (eq property (car plist)))
  15238. (setq p (plist-put p (car plist) (nth 1 plist))))
  15239. (setq plist (cddr plist)))
  15240. p))
  15241. (defun org-force-self-insert (N)
  15242. "Needed to enforce self-insert under remapping."
  15243. (interactive "p")
  15244. (self-insert-command N))
  15245. (defun org-string-width (s)
  15246. "Compute width of string, ignoring invisible characters.
  15247. This ignores character with invisibility property `org-link', and also
  15248. characters with property `org-cwidth', because these will become invisible
  15249. upon the next fontification round."
  15250. (let (b l)
  15251. (when (or (eq t buffer-invisibility-spec)
  15252. (assq 'org-link buffer-invisibility-spec))
  15253. (while (setq b (text-property-any 0 (length s)
  15254. 'invisible 'org-link s))
  15255. (setq s (concat (substring s 0 b)
  15256. (substring s (or (next-single-property-change
  15257. b 'invisible s) (length s)))))))
  15258. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15259. (setq s (concat (substring s 0 b)
  15260. (substring s (or (next-single-property-change
  15261. b 'org-cwidth s) (length s))))))
  15262. (setq l (string-width s) b -1)
  15263. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15264. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15265. l))
  15266. (defun org-get-indentation (&optional line)
  15267. "Get the indentation of the current line, interpreting tabs.
  15268. When LINE is given, assume it represents a line and compute its indentation."
  15269. (if line
  15270. (if (string-match "^ *" (org-remove-tabs line))
  15271. (match-end 0))
  15272. (save-excursion
  15273. (beginning-of-line 1)
  15274. (skip-chars-forward " \t")
  15275. (current-column))))
  15276. (defun org-remove-tabs (s &optional width)
  15277. "Replace tabulators in S with spaces.
  15278. Assumes that s is a single line, starting in column 0."
  15279. (setq width (or width tab-width))
  15280. (while (string-match "\t" s)
  15281. (setq s (replace-match
  15282. (make-string
  15283. (- (* width (/ (+ (match-beginning 0) width) width))
  15284. (match-beginning 0)) ?\ )
  15285. t t s)))
  15286. s)
  15287. (defun org-fix-indentation (line ind)
  15288. "Fix indentation in LINE.
  15289. IND is a cons cell with target and minimum indentation.
  15290. If the current indentation in LINE is smaller than the minimum,
  15291. leave it alone. If it is larger than ind, set it to the target."
  15292. (let* ((l (org-remove-tabs line))
  15293. (i (org-get-indentation l))
  15294. (i1 (car ind)) (i2 (cdr ind)))
  15295. (if (>= i i2) (setq l (substring line i2)))
  15296. (if (> i1 0)
  15297. (concat (make-string i1 ?\ ) l)
  15298. l)))
  15299. (defun org-remove-indentation (code &optional n)
  15300. "Remove the maximum common indentation from the lines in CODE.
  15301. N may optionally be the number of spaces to remove."
  15302. (with-temp-buffer
  15303. (insert code)
  15304. (org-do-remove-indentation n)
  15305. (buffer-string)))
  15306. (defun org-do-remove-indentation (&optional n)
  15307. "Remove the maximum common indentation from the buffer."
  15308. (untabify (point-min) (point-max))
  15309. (let ((min 10000) re)
  15310. (if n
  15311. (setq min n)
  15312. (goto-char (point-min))
  15313. (while (re-search-forward "^ *[^ \n]" nil t)
  15314. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15315. (unless (or (= min 0) (= min 10000))
  15316. (setq re (format "^ \\{%d\\}" min))
  15317. (goto-char (point-min))
  15318. (while (re-search-forward re nil t)
  15319. (replace-match "")
  15320. (end-of-line 1))
  15321. min)))
  15322. (defun org-fill-template (template alist)
  15323. "Find each %key of ALIST in TEMPLATE and replace it."
  15324. (let ((case-fold-search nil)
  15325. entry key value)
  15326. (setq alist (sort (copy-sequence alist)
  15327. (lambda (a b) (< (length (car a)) (length (car b))))))
  15328. (while (setq entry (pop alist))
  15329. (setq template
  15330. (replace-regexp-in-string
  15331. (concat "%" (regexp-quote (car entry)))
  15332. (cdr entry) template t t)))
  15333. template))
  15334. (defun org-base-buffer (buffer)
  15335. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15336. (if (not buffer)
  15337. buffer
  15338. (or (buffer-base-buffer buffer)
  15339. buffer)))
  15340. (defun org-trim (s)
  15341. "Remove whitespace at beginning and end of string."
  15342. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15343. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15344. s)
  15345. (defun org-wrap (string &optional width lines)
  15346. "Wrap string to either a number of lines, or a width in characters.
  15347. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15348. that costs. If there is a word longer than WIDTH, the text is actually
  15349. wrapped to the length of that word.
  15350. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15351. many lines, whatever width that takes.
  15352. The return value is a list of lines, without newlines at the end."
  15353. (let* ((words (org-split-string string "[ \t\n]+"))
  15354. (maxword (apply 'max (mapcar 'org-string-width words)))
  15355. w ll)
  15356. (cond (width
  15357. (org-do-wrap words (max maxword width)))
  15358. (lines
  15359. (setq w maxword)
  15360. (setq ll (org-do-wrap words maxword))
  15361. (if (<= (length ll) lines)
  15362. ll
  15363. (setq ll words)
  15364. (while (> (length ll) lines)
  15365. (setq w (1+ w))
  15366. (setq ll (org-do-wrap words w)))
  15367. ll))
  15368. (t (error "Cannot wrap this")))))
  15369. (defun org-do-wrap (words width)
  15370. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15371. (let (lines line)
  15372. (while words
  15373. (setq line (pop words))
  15374. (while (and words (< (+ (length line) (length (car words))) width))
  15375. (setq line (concat line " " (pop words))))
  15376. (setq lines (push line lines)))
  15377. (nreverse lines)))
  15378. (defun org-split-string (string &optional separators)
  15379. "Splits STRING into substrings at SEPARATORS.
  15380. No empty strings are returned if there are matches at the beginning
  15381. and end of string."
  15382. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15383. (start 0)
  15384. notfirst
  15385. (list nil))
  15386. (while (and (string-match rexp string
  15387. (if (and notfirst
  15388. (= start (match-beginning 0))
  15389. (< start (length string)))
  15390. (1+ start) start))
  15391. (< (match-beginning 0) (length string)))
  15392. (setq notfirst t)
  15393. (or (eq (match-beginning 0) 0)
  15394. (and (eq (match-beginning 0) (match-end 0))
  15395. (eq (match-beginning 0) start))
  15396. (setq list
  15397. (cons (substring string start (match-beginning 0))
  15398. list)))
  15399. (setq start (match-end 0)))
  15400. (or (eq start (length string))
  15401. (setq list
  15402. (cons (substring string start)
  15403. list)))
  15404. (nreverse list)))
  15405. (defun org-quote-vert (s)
  15406. "Replace \"|\" with \"\\vert\"."
  15407. (while (string-match "|" s)
  15408. (setq s (replace-match "\\vert" t t s)))
  15409. s)
  15410. (defun org-uuidgen-p (s)
  15411. "Is S an ID created by UUIDGEN?"
  15412. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15413. (defun org-context ()
  15414. "Return a list of contexts of the current cursor position.
  15415. If several contexts apply, all are returned.
  15416. Each context entry is a list with a symbol naming the context, and
  15417. two positions indicating start and end of the context. Possible
  15418. contexts are:
  15419. :headline anywhere in a headline
  15420. :headline-stars on the leading stars in a headline
  15421. :todo-keyword on a TODO keyword (including DONE) in a headline
  15422. :tags on the TAGS in a headline
  15423. :priority on the priority cookie in a headline
  15424. :item on the first line of a plain list item
  15425. :item-bullet on the bullet/number of a plain list item
  15426. :checkbox on the checkbox in a plain list item
  15427. :table in an org-mode table
  15428. :table-special on a special filed in a table
  15429. :table-table in a table.el table
  15430. :link on a hyperlink
  15431. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15432. :target on a <<target>>
  15433. :radio-target on a <<<radio-target>>>
  15434. :latex-fragment on a LaTeX fragment
  15435. :latex-preview on a LaTeX fragment with overlayed preview image
  15436. This function expects the position to be visible because it uses font-lock
  15437. faces as a help to recognize the following contexts: :table-special, :link,
  15438. and :keyword."
  15439. (let* ((f (get-text-property (point) 'face))
  15440. (faces (if (listp f) f (list f)))
  15441. (p (point)) clist o)
  15442. ;; First the large context
  15443. (cond
  15444. ((org-on-heading-p t)
  15445. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15446. (when (progn
  15447. (beginning-of-line 1)
  15448. (looking-at org-todo-line-tags-regexp))
  15449. (push (org-point-in-group p 1 :headline-stars) clist)
  15450. (push (org-point-in-group p 2 :todo-keyword) clist)
  15451. (push (org-point-in-group p 4 :tags) clist))
  15452. (goto-char p)
  15453. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15454. (if (looking-at "\\[#[A-Z0-9]\\]")
  15455. (push (org-point-in-group p 0 :priority) clist)))
  15456. ((org-at-item-p)
  15457. (push (org-point-in-group p 2 :item-bullet) clist)
  15458. (push (list :item (point-at-bol)
  15459. (save-excursion (org-end-of-item) (point)))
  15460. clist)
  15461. (and (org-at-item-checkbox-p)
  15462. (push (org-point-in-group p 0 :checkbox) clist)))
  15463. ((org-at-table-p)
  15464. (push (list :table (org-table-begin) (org-table-end)) clist)
  15465. (if (memq 'org-formula faces)
  15466. (push (list :table-special
  15467. (previous-single-property-change p 'face)
  15468. (next-single-property-change p 'face)) clist)))
  15469. ((org-at-table-p 'any)
  15470. (push (list :table-table) clist)))
  15471. (goto-char p)
  15472. ;; Now the small context
  15473. (cond
  15474. ((org-at-timestamp-p)
  15475. (push (org-point-in-group p 0 :timestamp) clist))
  15476. ((memq 'org-link faces)
  15477. (push (list :link
  15478. (previous-single-property-change p 'face)
  15479. (next-single-property-change p 'face)) clist))
  15480. ((memq 'org-special-keyword faces)
  15481. (push (list :keyword
  15482. (previous-single-property-change p 'face)
  15483. (next-single-property-change p 'face)) clist))
  15484. ((org-on-target-p)
  15485. (push (org-point-in-group p 0 :target) clist)
  15486. (goto-char (1- (match-beginning 0)))
  15487. (if (looking-at org-radio-target-regexp)
  15488. (push (org-point-in-group p 0 :radio-target) clist))
  15489. (goto-char p))
  15490. ((setq o (car (delq nil
  15491. (mapcar
  15492. (lambda (x)
  15493. (if (memq x org-latex-fragment-image-overlays) x))
  15494. (org-overlays-at (point))))))
  15495. (push (list :latex-fragment
  15496. (org-overlay-start o) (org-overlay-end o)) clist)
  15497. (push (list :latex-preview
  15498. (org-overlay-start o) (org-overlay-end o)) clist))
  15499. ((org-inside-LaTeX-fragment-p)
  15500. ;; FIXME: positions wrong.
  15501. (push (list :latex-fragment (point) (point)) clist)))
  15502. (setq clist (nreverse (delq nil clist)))
  15503. clist))
  15504. ;; FIXME: Compare with at-regexp-p Do we need both?
  15505. (defun org-in-regexp (re &optional nlines visually)
  15506. "Check if point is inside a match of regexp.
  15507. Normally only the current line is checked, but you can include NLINES extra
  15508. lines both before and after point into the search.
  15509. If VISUALLY is set, require that the cursor is not after the match but
  15510. really on, so that the block visually is on the match."
  15511. (catch 'exit
  15512. (let ((pos (point))
  15513. (eol (point-at-eol (+ 1 (or nlines 0))))
  15514. (inc (if visually 1 0)))
  15515. (save-excursion
  15516. (beginning-of-line (- 1 (or nlines 0)))
  15517. (while (re-search-forward re eol t)
  15518. (if (and (<= (match-beginning 0) pos)
  15519. (>= (+ inc (match-end 0)) pos))
  15520. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15521. (defun org-at-regexp-p (regexp)
  15522. "Is point inside a match of REGEXP in the current line?"
  15523. (catch 'exit
  15524. (save-excursion
  15525. (let ((pos (point)) (end (point-at-eol)))
  15526. (beginning-of-line 1)
  15527. (while (re-search-forward regexp end t)
  15528. (if (and (<= (match-beginning 0) pos)
  15529. (>= (match-end 0) pos))
  15530. (throw 'exit t)))
  15531. nil))))
  15532. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15533. "Call `multi-occur' with buffers for all agenda files."
  15534. (interactive "sOrg-files matching: \np")
  15535. (let* ((files (org-agenda-files))
  15536. (tnames (mapcar 'file-truename files))
  15537. (extra org-agenda-text-search-extra-files)
  15538. f)
  15539. (when (eq (car extra) 'agenda-archives)
  15540. (setq extra (cdr extra))
  15541. (setq files (org-add-archive-files files)))
  15542. (while (setq f (pop extra))
  15543. (unless (member (file-truename f) tnames)
  15544. (add-to-list 'files f 'append)
  15545. (add-to-list 'tnames (file-truename f) 'append)))
  15546. (multi-occur
  15547. (mapcar (lambda (x)
  15548. (with-current-buffer
  15549. (or (get-file-buffer x) (find-file-noselect x))
  15550. (widen)
  15551. (current-buffer)))
  15552. files)
  15553. regexp)))
  15554. (if (boundp 'occur-mode-find-occurrence-hook)
  15555. ;; Emacs 23
  15556. (add-hook 'occur-mode-find-occurrence-hook
  15557. (lambda ()
  15558. (when (org-mode-p)
  15559. (org-reveal))))
  15560. ;; Emacs 22
  15561. (defadvice occur-mode-goto-occurrence
  15562. (after org-occur-reveal activate)
  15563. (and (org-mode-p) (org-reveal)))
  15564. (defadvice occur-mode-goto-occurrence-other-window
  15565. (after org-occur-reveal activate)
  15566. (and (org-mode-p) (org-reveal)))
  15567. (defadvice occur-mode-display-occurrence
  15568. (after org-occur-reveal activate)
  15569. (when (org-mode-p)
  15570. (let ((pos (occur-mode-find-occurrence)))
  15571. (with-current-buffer (marker-buffer pos)
  15572. (save-excursion
  15573. (goto-char pos)
  15574. (org-reveal)))))))
  15575. (defun org-occur-link-in-agenda-files ()
  15576. "Create a link and search for it in the agendas.
  15577. The link is not stored in `org-stored-links', it is just created
  15578. for the search purpose."
  15579. (interactive)
  15580. (let ((link (condition-case nil
  15581. (org-store-link nil)
  15582. (error "Unable to create a link to here"))))
  15583. (org-occur-in-agenda-files (regexp-quote link))))
  15584. (defun org-uniquify (list)
  15585. "Remove duplicate elements from LIST."
  15586. (let (res)
  15587. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15588. res))
  15589. (defun org-delete-all (elts list)
  15590. "Remove all elements in ELTS from LIST."
  15591. (while elts
  15592. (setq list (delete (pop elts) list)))
  15593. list)
  15594. (defun org-back-over-empty-lines ()
  15595. "Move backwards over whitespace, to the beginning of the first empty line.
  15596. Returns the number of empty lines passed."
  15597. (let ((pos (point)))
  15598. (skip-chars-backward " \t\n\r")
  15599. (beginning-of-line 2)
  15600. (goto-char (min (point) pos))
  15601. (count-lines (point) pos)))
  15602. (defun org-skip-whitespace ()
  15603. (skip-chars-forward " \t\n\r"))
  15604. (defun org-point-in-group (point group &optional context)
  15605. "Check if POINT is in match-group GROUP.
  15606. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15607. match. If the match group does ot exist or point is not inside it,
  15608. return nil."
  15609. (and (match-beginning group)
  15610. (>= point (match-beginning group))
  15611. (<= point (match-end group))
  15612. (if context
  15613. (list context (match-beginning group) (match-end group))
  15614. t)))
  15615. (defun org-switch-to-buffer-other-window (&rest args)
  15616. "Switch to buffer in a second window on the current frame.
  15617. In particular, do not allow pop-up frames."
  15618. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15619. special-display-function)
  15620. (apply 'switch-to-buffer-other-window args)))
  15621. (defun org-combine-plists (&rest plists)
  15622. "Create a single property list from all plists in PLISTS.
  15623. The process starts by copying the first list, and then setting properties
  15624. from the other lists. Settings in the last list are the most significant
  15625. ones and overrule settings in the other lists."
  15626. (let ((rtn (copy-sequence (pop plists)))
  15627. p v ls)
  15628. (while plists
  15629. (setq ls (pop plists))
  15630. (while ls
  15631. (setq p (pop ls) v (pop ls))
  15632. (setq rtn (plist-put rtn p v))))
  15633. rtn))
  15634. (defun org-move-line-down (arg)
  15635. "Move the current line down. With prefix argument, move it past ARG lines."
  15636. (interactive "p")
  15637. (let ((col (current-column))
  15638. beg end pos)
  15639. (beginning-of-line 1) (setq beg (point))
  15640. (beginning-of-line 2) (setq end (point))
  15641. (beginning-of-line (+ 1 arg))
  15642. (setq pos (move-marker (make-marker) (point)))
  15643. (insert (delete-and-extract-region beg end))
  15644. (goto-char pos)
  15645. (org-move-to-column col)))
  15646. (defun org-move-line-up (arg)
  15647. "Move the current line up. With prefix argument, move it past ARG lines."
  15648. (interactive "p")
  15649. (let ((col (current-column))
  15650. beg end pos)
  15651. (beginning-of-line 1) (setq beg (point))
  15652. (beginning-of-line 2) (setq end (point))
  15653. (beginning-of-line (- arg))
  15654. (setq pos (move-marker (make-marker) (point)))
  15655. (insert (delete-and-extract-region beg end))
  15656. (goto-char pos)
  15657. (org-move-to-column col)))
  15658. (defun org-replace-escapes (string table)
  15659. "Replace %-escapes in STRING with values in TABLE.
  15660. TABLE is an association list with keys like \"%a\" and string values.
  15661. The sequences in STRING may contain normal field width and padding information,
  15662. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15663. so values can contain further %-escapes if they are define later in TABLE."
  15664. (let ((case-fold-search nil)
  15665. e re rpl)
  15666. (while (setq e (pop table))
  15667. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15668. (while (string-match re string)
  15669. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15670. (cdr e)))
  15671. (setq string (replace-match rpl t t string))))
  15672. string))
  15673. (defun org-sublist (list start end)
  15674. "Return a section of LIST, from START to END.
  15675. Counting starts at 1."
  15676. (let (rtn (c start))
  15677. (setq list (nthcdr (1- start) list))
  15678. (while (and list (<= c end))
  15679. (push (pop list) rtn)
  15680. (setq c (1+ c)))
  15681. (nreverse rtn)))
  15682. (defun org-find-base-buffer-visiting (file)
  15683. "Like `find-buffer-visiting' but always return the base buffer and
  15684. not an indirect buffer."
  15685. (let ((buf (or (get-file-buffer file)
  15686. (find-buffer-visiting file))))
  15687. (if buf
  15688. (or (buffer-base-buffer buf) buf)
  15689. nil)))
  15690. (defun org-image-file-name-regexp (&optional extensions)
  15691. "Return regexp matching the file names of images.
  15692. If EXTENSIONS is given, only match these."
  15693. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15694. (image-file-name-regexp)
  15695. (let ((image-file-name-extensions
  15696. (or extensions
  15697. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15698. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15699. (concat "\\."
  15700. (regexp-opt (nconc (mapcar 'upcase
  15701. image-file-name-extensions)
  15702. image-file-name-extensions)
  15703. t)
  15704. "\\'"))))
  15705. (defun org-file-image-p (file &optional extensions)
  15706. "Return non-nil if FILE is an image."
  15707. (save-match-data
  15708. (string-match (org-image-file-name-regexp extensions) file)))
  15709. (defun org-get-cursor-date ()
  15710. "Return the date at cursor in as a time.
  15711. This works in the calendar and in the agenda, anywhere else it just
  15712. returns the current time."
  15713. (let (date day defd)
  15714. (cond
  15715. ((eq major-mode 'calendar-mode)
  15716. (setq date (calendar-cursor-to-date)
  15717. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15718. ((eq major-mode 'org-agenda-mode)
  15719. (setq day (get-text-property (point) 'day))
  15720. (if day
  15721. (setq date (calendar-gregorian-from-absolute day)
  15722. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15723. (nth 2 date))))))
  15724. (or defd (current-time))))
  15725. (defvar org-agenda-action-marker (make-marker)
  15726. "Marker pointing to the entry for the next agenda action.")
  15727. (defun org-mark-entry-for-agenda-action ()
  15728. "Mark the current entry as target of an agenda action.
  15729. Agenda actions are actions executed from the agenda with the key `k',
  15730. which make use of the date at the cursor."
  15731. (interactive)
  15732. (move-marker org-agenda-action-marker
  15733. (save-excursion (org-back-to-heading t) (point))
  15734. (current-buffer))
  15735. (message
  15736. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15737. ;;; Paragraph filling stuff.
  15738. ;; We want this to be just right, so use the full arsenal.
  15739. (defun org-indent-line-function ()
  15740. "Indent line like previous, but further if previous was headline or item."
  15741. (interactive)
  15742. (let* ((pos (point))
  15743. (itemp (org-at-item-p))
  15744. (case-fold-search t)
  15745. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15746. column bpos bcol tpos tcol bullet btype bullet-type)
  15747. ;; Find the previous relevant line
  15748. (beginning-of-line 1)
  15749. (cond
  15750. ((looking-at "#") (setq column 0))
  15751. ((looking-at "\\*+ ") (setq column 0))
  15752. ((and (looking-at "[ \t]*:END:")
  15753. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15754. (save-excursion
  15755. (goto-char (1- (match-beginning 1)))
  15756. (setq column (current-column))))
  15757. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15758. (save-excursion
  15759. (re-search-backward
  15760. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15761. (setq column (org-get-indentation (match-string 0))))
  15762. (t
  15763. (beginning-of-line 0)
  15764. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15765. (not (looking-at "[ \t]*:END:"))
  15766. (not (looking-at org-drawer-regexp)))
  15767. (beginning-of-line 0))
  15768. (cond
  15769. ((looking-at "\\*+[ \t]+")
  15770. (if (not org-adapt-indentation)
  15771. (setq column 0)
  15772. (goto-char (match-end 0))
  15773. (setq column (current-column))))
  15774. ((looking-at org-drawer-regexp)
  15775. (goto-char (1- (match-beginning 1)))
  15776. (setq column (current-column)))
  15777. ((looking-at "\\([ \t]*\\):END:")
  15778. (goto-char (match-end 1))
  15779. (setq column (current-column)))
  15780. ((org-in-item-p)
  15781. (org-beginning-of-item)
  15782. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  15783. (setq bpos (match-beginning 1) tpos (match-end 0)
  15784. bcol (progn (goto-char bpos) (current-column))
  15785. tcol (progn (goto-char tpos) (current-column))
  15786. bullet (match-string 1)
  15787. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  15788. (if (> tcol (+ bcol org-description-max-indent))
  15789. (setq tcol (+ bcol 5)))
  15790. (if (not itemp)
  15791. (setq column tcol)
  15792. (goto-char pos)
  15793. (beginning-of-line 1)
  15794. (if (looking-at "\\S-")
  15795. (progn
  15796. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  15797. (setq bullet (match-string 1)
  15798. btype (if (string-match "[0-9]" bullet) "n" bullet))
  15799. (setq column (if (equal btype bullet-type) bcol tcol)))
  15800. (setq column (org-get-indentation)))))
  15801. (t (setq column (org-get-indentation))))))
  15802. (goto-char pos)
  15803. (if (<= (current-column) (current-indentation))
  15804. (org-indent-line-to column)
  15805. (save-excursion (org-indent-line-to column)))
  15806. (setq column (current-column))
  15807. (beginning-of-line 1)
  15808. (if (looking-at
  15809. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  15810. (replace-match (concat (match-string 1)
  15811. (format org-property-format
  15812. (match-string 2) (match-string 3)))
  15813. t t))
  15814. (org-move-to-column column)))
  15815. (defun org-set-autofill-regexps ()
  15816. (interactive)
  15817. ;; In the paragraph separator we include headlines, because filling
  15818. ;; text in a line directly attached to a headline would otherwise
  15819. ;; fill the headline as well.
  15820. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15821. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15822. ;; The paragraph starter includes hand-formatted lists.
  15823. (org-set-local
  15824. 'paragraph-start
  15825. (concat
  15826. "\f" "\\|"
  15827. "[ ]*$" "\\|"
  15828. "\\*+ " "\\|"
  15829. "[ \t]*#" "\\|"
  15830. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15831. "[ \t]*[:|]" "\\|"
  15832. "\\$\\$" "\\|"
  15833. "\\\\\\(begin\\|end\\|[][]\\)"))
  15834. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15835. ;; But only if the user has not turned off tables or fixed-width regions
  15836. (org-set-local
  15837. 'auto-fill-inhibit-regexp
  15838. (concat "\\*+ \\|#\\+"
  15839. "\\|[ \t]*" org-keyword-time-regexp
  15840. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15841. (concat
  15842. "\\|[ \t]*["
  15843. (if org-enable-table-editor "|" "")
  15844. (if org-enable-fixed-width-editor ":" "")
  15845. "]"))))
  15846. ;; We use our own fill-paragraph function, to make sure that tables
  15847. ;; and fixed-width regions are not wrapped. That function will pass
  15848. ;; through to `fill-paragraph' when appropriate.
  15849. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15850. ; Adaptive filling: To get full control, first make sure that
  15851. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15852. (org-set-local 'adaptive-fill-regexp "\000")
  15853. (org-set-local 'adaptive-fill-function
  15854. 'org-adaptive-fill-function)
  15855. (org-set-local
  15856. 'align-mode-rules-list
  15857. '((org-in-buffer-settings
  15858. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15859. (modes . '(org-mode))))))
  15860. (defun org-fill-paragraph (&optional justify)
  15861. "Re-align a table, pass through to fill-paragraph if no table."
  15862. (let ((table-p (org-at-table-p))
  15863. (table.el-p (org-at-table.el-p)))
  15864. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15865. (save-excursion (goto-char (point-at-bol))
  15866. (looking-at outline-regexp)))
  15867. t) ; skip headlines
  15868. (table.el-p t) ; skip table.el tables
  15869. (table-p (org-table-align) t) ; align org-mode tables
  15870. (t nil)))) ; call paragraph-fill
  15871. ;; For reference, this is the default value of adaptive-fill-regexp
  15872. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15873. (defun org-adaptive-fill-function ()
  15874. "Return a fill prefix for org-mode files.
  15875. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15876. work correctly."
  15877. (cond ((looking-at "#[ \t]+")
  15878. (match-string 0))
  15879. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15880. (save-excursion
  15881. (if (> (match-end 1) (+ (match-beginning 1)
  15882. org-description-max-indent))
  15883. (goto-char (+ (match-beginning 1) 5))
  15884. (goto-char (match-end 0)))
  15885. (make-string (current-column) ?\ )))
  15886. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15887. (save-excursion
  15888. (goto-char (match-end 0))
  15889. (make-string (current-column) ?\ )))
  15890. (t nil)))
  15891. ;;; Other stuff.
  15892. (defun org-toggle-fixed-width-section (arg)
  15893. "Toggle the fixed-width export.
  15894. If there is no active region, the QUOTE keyword at the current headline is
  15895. inserted or removed. When present, it causes the text between this headline
  15896. and the next to be exported as fixed-width text, and unmodified.
  15897. If there is an active region, this command adds or removes a colon as the
  15898. first character of this line. If the first character of a line is a colon,
  15899. this line is also exported in fixed-width font."
  15900. (interactive "P")
  15901. (let* ((cc 0)
  15902. (regionp (org-region-active-p))
  15903. (beg (if regionp (region-beginning) (point)))
  15904. (end (if regionp (region-end)))
  15905. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15906. (case-fold-search nil)
  15907. (re "[ \t]*\\(: \\)")
  15908. off)
  15909. (if regionp
  15910. (save-excursion
  15911. (goto-char beg)
  15912. (setq cc (current-column))
  15913. (beginning-of-line 1)
  15914. (setq off (looking-at re))
  15915. (while (> nlines 0)
  15916. (setq nlines (1- nlines))
  15917. (beginning-of-line 1)
  15918. (cond
  15919. (arg
  15920. (org-move-to-column cc t)
  15921. (insert ": \n")
  15922. (forward-line -1))
  15923. ((and off (looking-at re))
  15924. (replace-match "" t t nil 1))
  15925. ((not off) (org-move-to-column cc t) (insert ": ")))
  15926. (forward-line 1)))
  15927. (save-excursion
  15928. (org-back-to-heading)
  15929. (if (looking-at (concat outline-regexp
  15930. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15931. (replace-match "" t t nil 1)
  15932. (if (looking-at outline-regexp)
  15933. (progn
  15934. (goto-char (match-end 0))
  15935. (insert org-quote-string " "))))))))
  15936. (defun org-reftex-citation ()
  15937. "Use reftex-citation to insert a citation into the buffer.
  15938. This looks for a line like
  15939. #+BIBLIOGRAPHY: foo plain option:-d
  15940. and derives from it that foo.bib is the bibliography file relevant
  15941. for this document. It then installs the necessary environment for RefTeX
  15942. to work in this buffer and calls `reftex-citation' to insert a citation
  15943. into the buffer.
  15944. Export of such citations to both LaTeX and HTML is handled by the contributed
  15945. package org-exp-bibtex by Taru Karttunen."
  15946. (interactive)
  15947. (let ((reftex-docstruct-symbol 'rds)
  15948. (reftex-cite-format "\\cite{%l}")
  15949. rds bib)
  15950. (save-excursion
  15951. (save-restriction
  15952. (widen)
  15953. (let ((case-fold-search t)
  15954. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15955. (if (not (save-excursion
  15956. (or (re-search-forward re nil t)
  15957. (re-search-backward re nil t))))
  15958. (error "No bibliography defined in file")
  15959. (setq bib (concat (match-string 1) ".bib")
  15960. rds (list (list 'bib bib)))))))
  15961. (call-interactively 'reftex-citation)))
  15962. ;;;; Functions extending outline functionality
  15963. (defun org-beginning-of-line (&optional arg)
  15964. "Go to the beginning of the current line. If that is invisible, continue
  15965. to a visible line beginning. This makes the function of C-a more intuitive.
  15966. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15967. first attempt, and only move to after the tags when the cursor is already
  15968. beyond the end of the headline."
  15969. (interactive "P")
  15970. (let ((pos (point))
  15971. (special (if (consp org-special-ctrl-a/e)
  15972. (car org-special-ctrl-a/e)
  15973. org-special-ctrl-a/e))
  15974. refpos)
  15975. (if (org-bound-and-true-p line-move-visual)
  15976. (beginning-of-visual-line 1)
  15977. (beginning-of-line 1))
  15978. (if (and arg (fboundp 'move-beginning-of-line))
  15979. (call-interactively 'move-beginning-of-line)
  15980. (if (bobp)
  15981. nil
  15982. (backward-char 1)
  15983. (if (org-invisible-p)
  15984. (while (and (not (bobp)) (org-invisible-p))
  15985. (backward-char 1)
  15986. (beginning-of-line 1))
  15987. (forward-char 1))))
  15988. (when special
  15989. (cond
  15990. ((and (looking-at org-complex-heading-regexp)
  15991. (= (char-after (match-end 1)) ?\ ))
  15992. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15993. (point-at-eol)))
  15994. (goto-char
  15995. (if (eq special t)
  15996. (cond ((> pos refpos) refpos)
  15997. ((= pos (point)) refpos)
  15998. (t (point)))
  15999. (cond ((> pos (point)) (point))
  16000. ((not (eq last-command this-command)) (point))
  16001. (t refpos)))))
  16002. ((org-at-item-p)
  16003. (goto-char
  16004. (if (eq special t)
  16005. (cond ((> pos (match-end 4)) (match-end 4))
  16006. ((= pos (point)) (match-end 4))
  16007. (t (point)))
  16008. (cond ((> pos (point)) (point))
  16009. ((not (eq last-command this-command)) (point))
  16010. (t (match-end 4))))))))
  16011. (org-no-warnings
  16012. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16013. (defun org-end-of-line (&optional arg)
  16014. "Go to the end of the line.
  16015. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16016. first attempt, and only move to after the tags when the cursor is already
  16017. beyond the end of the headline."
  16018. (interactive "P")
  16019. (let ((special (if (consp org-special-ctrl-a/e)
  16020. (cdr org-special-ctrl-a/e)
  16021. org-special-ctrl-a/e)))
  16022. (if (or (not special)
  16023. (not (org-on-heading-p))
  16024. arg)
  16025. (call-interactively
  16026. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16027. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16028. (t 'end-of-line)))
  16029. (let ((pos (point)))
  16030. (beginning-of-line 1)
  16031. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16032. (if (eq special t)
  16033. (if (or (< pos (match-beginning 1))
  16034. (= pos (match-end 0)))
  16035. (goto-char (match-beginning 1))
  16036. (goto-char (match-end 0)))
  16037. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16038. (goto-char (match-end 0))
  16039. (goto-char (match-beginning 1))))
  16040. (call-interactively (if (fboundp 'move-end-of-line)
  16041. 'move-end-of-line
  16042. 'end-of-line)))))
  16043. (org-no-warnings
  16044. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16045. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16046. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16047. (define-key org-mode-map [home] 'org-beginning-of-line)
  16048. (define-key org-mode-map [end] 'org-end-of-line)
  16049. (defun org-backward-sentence (&optional arg)
  16050. "Go to beginning of sentence, or beginning of table field.
  16051. This will call `backward-sentence' or `org-table-beginning-of-field',
  16052. depending on context."
  16053. (interactive "P")
  16054. (cond
  16055. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16056. (t (call-interactively 'backward-sentence))))
  16057. (defun org-forward-sentence (&optional arg)
  16058. "Go to end of sentence, or end of table field.
  16059. This will call `forward-sentence' or `org-table-end-of-field',
  16060. depending on context."
  16061. (interactive "P")
  16062. (cond
  16063. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16064. (t (call-interactively 'forward-sentence))))
  16065. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16066. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16067. (defun org-kill-line (&optional arg)
  16068. "Kill line, to tags or end of line."
  16069. (interactive "P")
  16070. (cond
  16071. ((or (not org-special-ctrl-k)
  16072. (bolp)
  16073. (not (org-on-heading-p)))
  16074. (call-interactively 'kill-line))
  16075. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16076. (kill-region (point) (match-beginning 1))
  16077. (org-set-tags nil t))
  16078. (t (kill-region (point) (point-at-eol)))))
  16079. (define-key org-mode-map "\C-k" 'org-kill-line)
  16080. (defun org-yank (&optional arg)
  16081. "Yank. If the kill is a subtree, treat it specially.
  16082. This command will look at the current kill and check if is a single
  16083. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16084. cursor is at the beginning of a line or after the stars of a currently
  16085. empty headline, then the yank is handled specially. How exactly depends
  16086. on the value of the following variables, both set by default.
  16087. org-yank-folded-subtrees
  16088. When set, the subtree(s) will be folded after insertion, but only
  16089. if doing so would now swallow text after the yanked text.
  16090. org-yank-adjusted-subtrees
  16091. When set, the subtree will be promoted or demoted in order to
  16092. fit into the local outline tree structure, which means that the level
  16093. will be adjusted so that it becomes the smaller one of the two
  16094. *visible* surrounding headings.
  16095. Any prefix to this command will cause `yank' to be called directly with
  16096. no special treatment. In particular, a simple `C-u' prefix will just
  16097. plainly yank the text as it is.
  16098. \[1] The test checks if the first non-white line is a heading
  16099. and if there are no other headings with fewer stars."
  16100. (interactive "P")
  16101. (org-yank-generic 'yank arg))
  16102. (defun org-yank-generic (command arg)
  16103. "Perform some yank-like command.
  16104. This function implements the behavior described in the `org-yank'
  16105. documentation. However, it has been generalized to work for any
  16106. interactive command with similar behavior."
  16107. ;; pretend to be command COMMAND
  16108. (setq this-command command)
  16109. (if arg
  16110. (call-interactively command)
  16111. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16112. (and (org-kill-is-subtree-p)
  16113. (or (bolp)
  16114. (and (looking-at "[ \t]*$")
  16115. (string-match
  16116. "\\`\\*+\\'"
  16117. (buffer-substring (point-at-bol) (point)))))))
  16118. swallowp)
  16119. (cond
  16120. ((and subtreep org-yank-folded-subtrees)
  16121. (let ((beg (point))
  16122. end)
  16123. (if (and subtreep org-yank-adjusted-subtrees)
  16124. (org-paste-subtree nil nil 'for-yank)
  16125. (call-interactively command))
  16126. (setq end (point))
  16127. (goto-char beg)
  16128. (when (and (bolp) subtreep
  16129. (not (setq swallowp
  16130. (org-yank-folding-would-swallow-text beg end))))
  16131. (or (looking-at outline-regexp)
  16132. (re-search-forward (concat "^" outline-regexp) end t))
  16133. (while (and (< (point) end) (looking-at outline-regexp))
  16134. (hide-subtree)
  16135. (org-cycle-show-empty-lines 'folded)
  16136. (condition-case nil
  16137. (outline-forward-same-level 1)
  16138. (error (goto-char end)))))
  16139. (when swallowp
  16140. (message
  16141. "Inserted text not folded because that would swallow text"))
  16142. (goto-char end)
  16143. (skip-chars-forward " \t\n\r")
  16144. (beginning-of-line 1)
  16145. (push-mark beg 'nomsg)))
  16146. ((and subtreep org-yank-adjusted-subtrees)
  16147. (let ((beg (point-at-bol)))
  16148. (org-paste-subtree nil nil 'for-yank)
  16149. (push-mark beg 'nomsg)))
  16150. (t
  16151. (call-interactively command))))))
  16152. (defun org-yank-folding-would-swallow-text (beg end)
  16153. "Would hide-subtree at BEG swallow any text after END?"
  16154. (let (level)
  16155. (save-excursion
  16156. (goto-char beg)
  16157. (when (or (looking-at outline-regexp)
  16158. (re-search-forward (concat "^" outline-regexp) end t))
  16159. (setq level (org-outline-level)))
  16160. (goto-char end)
  16161. (skip-chars-forward " \t\r\n\v\f")
  16162. (if (or (eobp)
  16163. (and (bolp) (looking-at org-outline-regexp)
  16164. (<= (org-outline-level) level)))
  16165. nil ; Nothing would be swallowed
  16166. t)))) ; something would swallow
  16167. (define-key org-mode-map "\C-y" 'org-yank)
  16168. (defun org-invisible-p ()
  16169. "Check if point is at a character currently not visible."
  16170. ;; Early versions of noutline don't have `outline-invisible-p'.
  16171. (if (fboundp 'outline-invisible-p)
  16172. (outline-invisible-p)
  16173. (get-char-property (point) 'invisible)))
  16174. (defun org-invisible-p2 ()
  16175. "Check if point is at a character currently not visible."
  16176. (save-excursion
  16177. (if (and (eolp) (not (bobp))) (backward-char 1))
  16178. ;; Early versions of noutline don't have `outline-invisible-p'.
  16179. (if (fboundp 'outline-invisible-p)
  16180. (outline-invisible-p)
  16181. (get-char-property (point) 'invisible))))
  16182. (defun org-back-to-heading (&optional invisible-ok)
  16183. "Call `outline-back-to-heading', but provide a better error message."
  16184. (condition-case nil
  16185. (outline-back-to-heading invisible-ok)
  16186. (error (error "Before first headline at position %d in buffer %s"
  16187. (point) (current-buffer)))))
  16188. (defun org-before-first-heading-p ()
  16189. "Before first heading?"
  16190. (save-excursion
  16191. (null (re-search-backward "^\\*+ " nil t))))
  16192. (defun org-on-heading-p (&optional ignored)
  16193. (outline-on-heading-p t))
  16194. (defun org-at-heading-p (&optional ignored)
  16195. (outline-on-heading-p t))
  16196. (defun org-at-heading-or-item-p ()
  16197. (or (org-on-heading-p) (org-at-item-p)))
  16198. (defun org-on-target-p ()
  16199. (or (org-in-regexp org-radio-target-regexp)
  16200. (org-in-regexp org-target-regexp)))
  16201. (defun org-up-heading-all (arg)
  16202. "Move to the heading line of which the present line is a subheading.
  16203. This function considers both visible and invisible heading lines.
  16204. With argument, move up ARG levels."
  16205. (if (fboundp 'outline-up-heading-all)
  16206. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16207. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16208. (defun org-up-heading-safe ()
  16209. "Move to the heading line of which the present line is a subheading.
  16210. This version will not throw an error. It will return the level of the
  16211. headline found, or nil if no higher level is found.
  16212. Also, this function will be a lot faster than `outline-up-heading',
  16213. because it relies on stars being the outline starters. This can really
  16214. make a significant difference in outlines with very many siblings."
  16215. (let (start-level re)
  16216. (org-back-to-heading t)
  16217. (setq start-level (funcall outline-level))
  16218. (if (equal start-level 1)
  16219. nil
  16220. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16221. (if (re-search-backward re nil t)
  16222. (funcall outline-level)))))
  16223. (defun org-first-sibling-p ()
  16224. "Is this heading the first child of its parents?"
  16225. (interactive)
  16226. (let ((re (concat "^" outline-regexp))
  16227. level l)
  16228. (unless (org-at-heading-p t)
  16229. (error "Not at a heading"))
  16230. (setq level (funcall outline-level))
  16231. (save-excursion
  16232. (if (not (re-search-backward re nil t))
  16233. t
  16234. (setq l (funcall outline-level))
  16235. (< l level)))))
  16236. (defun org-goto-sibling (&optional previous)
  16237. "Goto the next sibling, even if it is invisible.
  16238. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16239. when a sibling was found. When none is found, return nil and don't
  16240. move point."
  16241. (let ((fun (if previous 're-search-backward 're-search-forward))
  16242. (pos (point))
  16243. (re (concat "^" outline-regexp))
  16244. level l)
  16245. (when (condition-case nil (org-back-to-heading t) (error nil))
  16246. (setq level (funcall outline-level))
  16247. (catch 'exit
  16248. (or previous (forward-char 1))
  16249. (while (funcall fun re nil t)
  16250. (setq l (funcall outline-level))
  16251. (when (< l level) (goto-char pos) (throw 'exit nil))
  16252. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16253. (goto-char pos)
  16254. nil))))
  16255. (defun org-show-siblings ()
  16256. "Show all siblings of the current headline."
  16257. (save-excursion
  16258. (while (org-goto-sibling) (org-flag-heading nil)))
  16259. (save-excursion
  16260. (while (org-goto-sibling 'previous)
  16261. (org-flag-heading nil))))
  16262. (defun org-show-hidden-entry ()
  16263. "Show an entry where even the heading is hidden."
  16264. (save-excursion
  16265. (org-show-entry)))
  16266. (defun org-flag-heading (flag &optional entry)
  16267. "Flag the current heading. FLAG non-nil means make invisible.
  16268. When ENTRY is non-nil, show the entire entry."
  16269. (save-excursion
  16270. (org-back-to-heading t)
  16271. ;; Check if we should show the entire entry
  16272. (if entry
  16273. (progn
  16274. (org-show-entry)
  16275. (save-excursion
  16276. (and (outline-next-heading)
  16277. (org-flag-heading nil))))
  16278. (outline-flag-region (max (point-min) (1- (point)))
  16279. (save-excursion (outline-end-of-heading) (point))
  16280. flag))))
  16281. (defun org-get-next-sibling ()
  16282. "Move to next heading of the same level, and return point.
  16283. If there is no such heading, return nil.
  16284. This is like outline-next-sibling, but invisible headings are ok."
  16285. (let ((level (funcall outline-level)))
  16286. (outline-next-heading)
  16287. (while (and (not (eobp)) (> (funcall outline-level) level))
  16288. (outline-next-heading))
  16289. (if (or (eobp) (< (funcall outline-level) level))
  16290. nil
  16291. (point))))
  16292. (defun org-get-last-sibling ()
  16293. "Move to previous heading of the same level, and return point.
  16294. If there is no such heading, return nil."
  16295. (let ((opoint (point))
  16296. (level (funcall outline-level)))
  16297. (outline-previous-heading)
  16298. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16299. (while (and (> (funcall outline-level) level)
  16300. (not (bobp)))
  16301. (outline-previous-heading))
  16302. (if (< (funcall outline-level) level)
  16303. nil
  16304. (point)))))
  16305. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16306. ;; This contains an exact copy of the original function, but it uses
  16307. ;; `org-back-to-heading', to make it work also in invisible
  16308. ;; trees. And is uses an invisible-OK argument.
  16309. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16310. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16311. ;; with many children and grandchildren etc, this can be much faster
  16312. ;; than the outline version.
  16313. (org-back-to-heading invisible-OK)
  16314. (let ((first t)
  16315. (level (funcall outline-level)))
  16316. (if (and (org-mode-p) (< level 1000))
  16317. ;; A true heading (not a plain list item), in Org-mode
  16318. ;; This means we can easily find the end by looking
  16319. ;; only for the right number of stars. Using a regexp to do
  16320. ;; this is so much faster than using a Lisp loop.
  16321. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16322. (forward-char 1)
  16323. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16324. ;; something else, do it the slow way
  16325. (while (and (not (eobp))
  16326. (or first (> (funcall outline-level) level)))
  16327. (setq first nil)
  16328. (outline-next-heading)))
  16329. (unless to-heading
  16330. (if (memq (preceding-char) '(?\n ?\^M))
  16331. (progn
  16332. ;; Go to end of line before heading
  16333. (forward-char -1)
  16334. (if (memq (preceding-char) '(?\n ?\^M))
  16335. ;; leave blank line before heading
  16336. (forward-char -1))))))
  16337. (point))
  16338. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16339. "Use Org version in org-mode, for dramatic speed-up."
  16340. (if (eq major-mode 'org-mode)
  16341. (progn
  16342. (org-end-of-subtree nil t)
  16343. (unless (eobp) (backward-char 1)))
  16344. ad-do-it))
  16345. (defun org-forward-same-level (arg &optional invisible-ok)
  16346. "Move forward to the arg'th subheading at same level as this one.
  16347. Stop at the first and last subheadings of a superior heading."
  16348. (interactive "p")
  16349. (org-back-to-heading invisible-ok)
  16350. (org-on-heading-p)
  16351. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16352. (re (format "^\\*\\{1,%d\\} " level))
  16353. l)
  16354. (forward-char 1)
  16355. (while (> arg 0)
  16356. (while (and (re-search-forward re nil 'move)
  16357. (setq l (- (match-end 0) (match-beginning 0) 1))
  16358. (= l level)
  16359. (not invisible-ok)
  16360. (progn (backward-char 1) (org-invisible-p)))
  16361. (if (< l level) (setq arg 1)))
  16362. (setq arg (1- arg)))
  16363. (beginning-of-line 1)))
  16364. (defun org-backward-same-level (arg &optional invisible-ok)
  16365. "Move backward to the arg'th subheading at same level as this one.
  16366. Stop at the first and last subheadings of a superior heading."
  16367. (interactive "p")
  16368. (org-back-to-heading)
  16369. (org-on-heading-p)
  16370. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16371. (re (format "^\\*\\{1,%d\\} " level))
  16372. l)
  16373. (while (> arg 0)
  16374. (while (and (re-search-backward re nil 'move)
  16375. (setq l (- (match-end 0) (match-beginning 0) 1))
  16376. (= l level)
  16377. (not invisible-ok)
  16378. (org-invisible-p))
  16379. (if (< l level) (setq arg 1)))
  16380. (setq arg (1- arg)))))
  16381. (defun org-show-subtree ()
  16382. "Show everything after this heading at deeper levels."
  16383. (outline-flag-region
  16384. (point)
  16385. (save-excursion
  16386. (org-end-of-subtree t t))
  16387. nil))
  16388. (defun org-show-entry ()
  16389. "Show the body directly following this heading.
  16390. Show the heading too, if it is currently invisible."
  16391. (interactive)
  16392. (save-excursion
  16393. (condition-case nil
  16394. (progn
  16395. (org-back-to-heading t)
  16396. (outline-flag-region
  16397. (max (point-min) (1- (point)))
  16398. (save-excursion
  16399. (if (re-search-forward
  16400. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16401. (match-beginning 1)
  16402. (point-max)))
  16403. nil)
  16404. (org-cycle-hide-drawers 'children))
  16405. (error nil))))
  16406. (defun org-make-options-regexp (kwds &optional extra)
  16407. "Make a regular expression for keyword lines."
  16408. (concat
  16409. "^"
  16410. "#?[ \t]*\\+\\("
  16411. (mapconcat 'regexp-quote kwds "\\|")
  16412. (if extra (concat "\\|" extra))
  16413. "\\):[ \t]*"
  16414. "\\(.*\\)"))
  16415. ;; Make isearch reveal the necessary context
  16416. (defun org-isearch-end ()
  16417. "Reveal context after isearch exits."
  16418. (when isearch-success ; only if search was successful
  16419. (if (featurep 'xemacs)
  16420. ;; Under XEmacs, the hook is run in the correct place,
  16421. ;; we directly show the context.
  16422. (org-show-context 'isearch)
  16423. ;; In Emacs the hook runs *before* restoring the overlays.
  16424. ;; So we have to use a one-time post-command-hook to do this.
  16425. ;; (Emacs 22 has a special variable, see function `org-mode')
  16426. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16427. isearch-mode-end-hook-quit)
  16428. ;; Only when the isearch was not quitted.
  16429. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16430. 'append 'local)))))
  16431. (defun org-isearch-post-command ()
  16432. "Remove self from hook, and show context."
  16433. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16434. (org-show-context 'isearch))
  16435. ;;;; Integration with and fixes for other packages
  16436. ;;; Imenu support
  16437. (defvar org-imenu-markers nil
  16438. "All markers currently used by Imenu.")
  16439. (make-variable-buffer-local 'org-imenu-markers)
  16440. (defun org-imenu-new-marker (&optional pos)
  16441. "Return a new marker for use by Imenu, and remember the marker."
  16442. (let ((m (make-marker)))
  16443. (move-marker m (or pos (point)))
  16444. (push m org-imenu-markers)
  16445. m))
  16446. (defun org-imenu-get-tree ()
  16447. "Produce the index for Imenu."
  16448. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16449. (setq org-imenu-markers nil)
  16450. (let* ((n org-imenu-depth)
  16451. (re (concat "^" outline-regexp))
  16452. (subs (make-vector (1+ n) nil))
  16453. (last-level 0)
  16454. m level head)
  16455. (save-excursion
  16456. (save-restriction
  16457. (widen)
  16458. (goto-char (point-max))
  16459. (while (re-search-backward re nil t)
  16460. (setq level (org-reduced-level (funcall outline-level)))
  16461. (when (<= level n)
  16462. (looking-at org-complex-heading-regexp)
  16463. (setq head (org-link-display-format
  16464. (org-match-string-no-properties 4))
  16465. m (org-imenu-new-marker))
  16466. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16467. (if (>= level last-level)
  16468. (push (cons head m) (aref subs level))
  16469. (push (cons head (aref subs (1+ level))) (aref subs level))
  16470. (loop for i from (1+ level) to n do (aset subs i nil)))
  16471. (setq last-level level)))))
  16472. (aref subs 1)))
  16473. (eval-after-load "imenu"
  16474. '(progn
  16475. (add-hook 'imenu-after-jump-hook
  16476. (lambda ()
  16477. (if (eq major-mode 'org-mode)
  16478. (org-show-context 'org-goto))))))
  16479. (defun org-link-display-format (link)
  16480. "Replace a link with either the description, or the link target
  16481. if no description is present"
  16482. (save-match-data
  16483. (if (string-match org-bracket-link-analytic-regexp link)
  16484. (replace-match (if (match-end 5)
  16485. (match-string 5 link)
  16486. (concat (match-string 1 link)
  16487. (match-string 3 link)))
  16488. nil t link)
  16489. link)))
  16490. ;; Speedbar support
  16491. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16492. "Overlay marking the agenda restriction line in speedbar.")
  16493. (org-overlay-put org-speedbar-restriction-lock-overlay
  16494. 'face 'org-agenda-restriction-lock)
  16495. (org-overlay-put org-speedbar-restriction-lock-overlay
  16496. 'help-echo "Agendas are currently limited to this item.")
  16497. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16498. (defun org-speedbar-set-agenda-restriction ()
  16499. "Restrict future agenda commands to the location at point in speedbar.
  16500. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16501. (interactive)
  16502. (require 'org-agenda)
  16503. (let (p m tp np dir txt)
  16504. (cond
  16505. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16506. 'org-imenu t))
  16507. (setq m (get-text-property p 'org-imenu-marker))
  16508. (with-current-buffer (marker-buffer m)
  16509. (goto-char m)
  16510. (org-agenda-set-restriction-lock 'subtree)))
  16511. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16512. 'speedbar-function 'speedbar-find-file))
  16513. (setq tp (previous-single-property-change
  16514. (1+ p) 'speedbar-function)
  16515. np (next-single-property-change
  16516. tp 'speedbar-function)
  16517. dir (speedbar-line-directory)
  16518. txt (buffer-substring-no-properties (or tp (point-min))
  16519. (or np (point-max))))
  16520. (with-current-buffer (find-file-noselect
  16521. (let ((default-directory dir))
  16522. (expand-file-name txt)))
  16523. (unless (org-mode-p)
  16524. (error "Cannot restrict to non-Org-mode file"))
  16525. (org-agenda-set-restriction-lock 'file)))
  16526. (t (error "Don't know how to restrict Org-mode's agenda")))
  16527. (org-move-overlay org-speedbar-restriction-lock-overlay
  16528. (point-at-bol) (point-at-eol))
  16529. (setq current-prefix-arg nil)
  16530. (org-agenda-maybe-redo)))
  16531. (eval-after-load "speedbar"
  16532. '(progn
  16533. (speedbar-add-supported-extension ".org")
  16534. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16535. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16536. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16537. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16538. (add-hook 'speedbar-visiting-tag-hook
  16539. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16540. ;;; Fixes and Hacks for problems with other packages
  16541. ;; Make flyspell not check words in links, to not mess up our keymap
  16542. (defun org-mode-flyspell-verify ()
  16543. "Don't let flyspell put overlays at active buttons."
  16544. (and (not (get-text-property (point) 'keymap))
  16545. (not (get-text-property (point) 'org-no-flyspell))))
  16546. (defun org-remove-flyspell-overlays-in (beg end)
  16547. "Remove flyspell overlays in region."
  16548. (and (org-bound-and-true-p flyspell-mode)
  16549. (fboundp 'flyspell-delete-region-overlays)
  16550. (flyspell-delete-region-overlays beg end))
  16551. (add-text-properties beg end '(org-no-flyspell t)))
  16552. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16553. (eval-after-load "bookmark"
  16554. '(if (boundp 'bookmark-after-jump-hook)
  16555. ;; We can use the hook
  16556. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16557. ;; Hook not available, use advice
  16558. (defadvice bookmark-jump (after org-make-visible activate)
  16559. "Make the position visible."
  16560. (org-bookmark-jump-unhide))))
  16561. ;; Make sure saveplace shows the location if it was hidden
  16562. (eval-after-load "saveplace"
  16563. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16564. "Make the position visible."
  16565. (org-bookmark-jump-unhide)))
  16566. ;; Make sure ecb shows the location if it was hidden
  16567. (eval-after-load "ecb"
  16568. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16569. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16570. (if (eq major-mode 'org-mode)
  16571. (org-show-context))))
  16572. (defun org-bookmark-jump-unhide ()
  16573. "Unhide the current position, to show the bookmark location."
  16574. (and (org-mode-p)
  16575. (or (org-invisible-p)
  16576. (save-excursion (goto-char (max (point-min) (1- (point))))
  16577. (org-invisible-p)))
  16578. (org-show-context 'bookmark-jump)))
  16579. ;; Make session.el ignore our circular variable
  16580. (eval-after-load "session"
  16581. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16582. ;;;; Experimental code
  16583. (defun org-closed-in-range ()
  16584. "Sparse tree of items closed in a certain time range.
  16585. Still experimental, may disappear in the future."
  16586. (interactive)
  16587. ;; Get the time interval from the user.
  16588. (let* ((time1 (org-float-time
  16589. (org-read-date nil 'to-time nil "Starting date: ")))
  16590. (time2 (org-float-time
  16591. (org-read-date nil 'to-time nil "End date:")))
  16592. ;; callback function
  16593. (callback (lambda ()
  16594. (let ((time
  16595. (org-float-time
  16596. (apply 'encode-time
  16597. (org-parse-time-string
  16598. (match-string 1))))))
  16599. ;; check if time in interval
  16600. (and (>= time time1) (<= time time2))))))
  16601. ;; make tree, check each match with the callback
  16602. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16603. ;;;; Finish up
  16604. (provide 'org)
  16605. (run-hooks 'org-load-hook)
  16606. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16607. ;;; org.el ends here