org.el 675 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109
  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.34
  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.34"
  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 special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  214. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  215. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  216. (const :tag "C track: Keep up with Org-mode development" org-track)
  217. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  218. (defcustom org-support-shift-select nil
  219. "Non-nil means, make shift-cursor commands select text when possible.
  220. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  221. selecting a region, or enlarge thusly regions started in this way.
  222. In Org-mode, in special contexts, these same keys are used for other
  223. purposes, important enough to compete with shift selection. Org tries
  224. to balance these needs by supporting `shift-select-mode' outside these
  225. special contexts, under control of this variable.
  226. The default of this variable is nil, to avoid confusing behavior. Shifted
  227. cursor keys will then execute Org commands in the following contexts:
  228. - on a headline, changing TODO state (left/right) and priority (up/down)
  229. - on a time stamp, changing the time
  230. - in a plain list item, changing the bullet type
  231. - in a property definition line, switching between allowed values
  232. - in the BEGIN line of a clock table (changing the time block).
  233. Outside these contexts, the commands will throw an error.
  234. When this variable is t and the cursor is not in a special context,
  235. Org-mode will support shift-selection for making and enlarging regions.
  236. To make this more effective, the bullet cycling will no longer happen
  237. anywhere in an item line, but only if the cursor is exactly on the bullet.
  238. If you set this variable to the symbol `always', then the keys
  239. will not be special in headlines, property lines, and item lines, to make
  240. shift selection work there as well. If this is what you want, you can
  241. use the following alternative commands: `C-c C-t' and `C-c ,' to
  242. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  243. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  244. edited by hand or in column view.
  245. However, when the cursor is on a timestamp, shift-cursor commands
  246. will still edit the time stamp - this is just too good to give up.
  247. XEmacs user should have this variable set to nil, because shift-select-mode
  248. is Emacs 23 only."
  249. :group 'org
  250. :type '(choice
  251. (const :tag "Never" nil)
  252. (const :tag "When outside special context" t)
  253. (const :tag "Everywhere except timestamps" always)))
  254. (defgroup org-startup nil
  255. "Options concerning startup of Org-mode."
  256. :tag "Org Startup"
  257. :group 'org)
  258. (defcustom org-startup-folded t
  259. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  260. This can also be configured on a per-file basis by adding one of
  261. the following lines anywhere in the buffer:
  262. #+STARTUP: fold (or `overview', this is equivalent)
  263. #+STARTUP: nofold (or `showall', this is equivalent)
  264. #+STARTUP: content
  265. #+STARTUP: showeverything"
  266. :group 'org-startup
  267. :type '(choice
  268. (const :tag "nofold: show all" nil)
  269. (const :tag "fold: overview" t)
  270. (const :tag "content: all headlines" content)
  271. (const :tag "show everything, even drawers" showeverything)))
  272. (defcustom org-startup-truncated t
  273. "Non-nil means, entering Org-mode will set `truncate-lines'.
  274. This is useful since some lines containing links can be very long and
  275. uninteresting. Also tables look terrible when wrapped."
  276. :group 'org-startup
  277. :type 'boolean)
  278. (defcustom org-startup-indented nil
  279. "Non-nil means, turn on `org-indent-mode' on startup.
  280. This can also be configured on a per-file basis by adding one of
  281. the following lines anywhere in the buffer:
  282. #+STARTUP: indent
  283. #+STARTUP: noindent"
  284. :group 'org-structure
  285. :type '(choice
  286. (const :tag "Not" nil)
  287. (const :tag "Globally (slow on startup in large files)" t)))
  288. (defcustom org-startup-with-beamer-mode nil
  289. "Non-nil means, turn on `org-beamer-mode' on startup.
  290. This can also be configured on a per-file basis by adding one of
  291. the following lines anywhere in the buffer:
  292. #+STARTUP: beamer"
  293. :group 'org-startup
  294. :type 'boolean)
  295. (defcustom org-startup-align-all-tables nil
  296. "Non-nil means, align all tables when visiting a file.
  297. This is useful when the column width in tables is forced with <N> cookies
  298. in table fields. Such tables will look correct only after the first re-align.
  299. This can also be configured on a per-file basis by adding one of
  300. the following lines anywhere in the buffer:
  301. #+STARTUP: align
  302. #+STARTUP: noalign"
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-insert-mode-line-in-empty-file nil
  306. "Non-nil means insert the first line setting Org-mode in empty files.
  307. When the function `org-mode' is called interactively in an empty file, this
  308. normally means that the file name does not automatically trigger Org-mode.
  309. To ensure that the file will always be in Org-mode in the future, a
  310. line enforcing Org-mode will be inserted into the buffer, if this option
  311. has been set."
  312. :group 'org-startup
  313. :type 'boolean)
  314. (defcustom org-replace-disputed-keys nil
  315. "Non-nil means use alternative key bindings for some keys.
  316. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  317. These keys are also used by other packages like shift-selection-mode'
  318. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  319. If you want to use Org-mode together with one of these other modes,
  320. or more generally if you would like to move some Org-mode commands to
  321. other keys, set this variable and configure the keys with the variable
  322. `org-disputed-keys'.
  323. This option is only relevant at load-time of Org-mode, and must be set
  324. *before* org.el is loaded. Changing it requires a restart of Emacs to
  325. become effective."
  326. :group 'org-startup
  327. :type 'boolean)
  328. (defcustom org-use-extra-keys nil
  329. "Non-nil means use extra key sequence definitions for certain
  330. commands. This happens automatically if you run XEmacs or if
  331. window-system is nil. This variable lets you do the same
  332. manually. You must set it before loading org.
  333. Example: on Carbon Emacs 22 running graphically, with an external
  334. keyboard on a Powerbook, the default way of setting M-left might
  335. not work for either Alt or ESC. Setting this variable will make
  336. it work for ESC."
  337. :group 'org-startup
  338. :type 'boolean)
  339. (if (fboundp 'defvaralias)
  340. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  341. (defcustom org-disputed-keys
  342. '(([(shift up)] . [(meta p)])
  343. ([(shift down)] . [(meta n)])
  344. ([(shift left)] . [(meta -)])
  345. ([(shift right)] . [(meta +)])
  346. ([(control shift right)] . [(meta shift +)])
  347. ([(control shift left)] . [(meta shift -)]))
  348. "Keys for which Org-mode and other modes compete.
  349. This is an alist, cars are the default keys, second element specifies
  350. the alternative to use when `org-replace-disputed-keys' is t.
  351. Keys can be specified in any syntax supported by `define-key'.
  352. The value of this option takes effect only at Org-mode's startup,
  353. therefore you'll have to restart Emacs to apply it after changing."
  354. :group 'org-startup
  355. :type 'alist)
  356. (defun org-key (key)
  357. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  358. Or return the original if not disputed."
  359. (if org-replace-disputed-keys
  360. (let* ((nkey (key-description key))
  361. (x (org-find-if (lambda (x)
  362. (equal (key-description (car x)) nkey))
  363. org-disputed-keys)))
  364. (if x (cdr x) key))
  365. key))
  366. (defun org-find-if (predicate seq)
  367. (catch 'exit
  368. (while seq
  369. (if (funcall predicate (car seq))
  370. (throw 'exit (car seq))
  371. (pop seq)))))
  372. (defun org-defkey (keymap key def)
  373. "Define a key, possibly translated, as returned by `org-key'."
  374. (define-key keymap (org-key key) def))
  375. (defcustom org-ellipsis nil
  376. "The ellipsis to use in the Org-mode outline.
  377. When nil, just use the standard three dots. When a string, use that instead,
  378. When a face, use the standard 3 dots, but with the specified face.
  379. The change affects only Org-mode (which will then use its own display table).
  380. Changing this requires executing `M-x org-mode' in a buffer to become
  381. effective."
  382. :group 'org-startup
  383. :type '(choice (const :tag "Default" nil)
  384. (face :tag "Face" :value org-warning)
  385. (string :tag "String" :value "...#")))
  386. (defvar org-display-table nil
  387. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  388. (defgroup org-keywords nil
  389. "Keywords in Org-mode."
  390. :tag "Org Keywords"
  391. :group 'org)
  392. (defcustom org-deadline-string "DEADLINE:"
  393. "String to mark deadline entries.
  394. A deadline is this string, followed by a time stamp. Should be a word,
  395. terminated by a colon. You can insert a schedule keyword and
  396. a timestamp with \\[org-deadline].
  397. Changes become only effective after restarting Emacs."
  398. :group 'org-keywords
  399. :type 'string)
  400. (defcustom org-scheduled-string "SCHEDULED:"
  401. "String to mark scheduled TODO entries.
  402. A schedule is this string, followed by a time stamp. Should be a word,
  403. terminated by a colon. You can insert a schedule keyword and
  404. a timestamp with \\[org-schedule].
  405. Changes become only effective after restarting Emacs."
  406. :group 'org-keywords
  407. :type 'string)
  408. (defcustom org-closed-string "CLOSED:"
  409. "String used as the prefix for timestamps logging closing a TODO entry."
  410. :group 'org-keywords
  411. :type 'string)
  412. (defcustom org-clock-string "CLOCK:"
  413. "String used as prefix for timestamps clocking work hours on an item."
  414. :group 'org-keywords
  415. :type 'string)
  416. (defcustom org-comment-string "COMMENT"
  417. "Entries starting with this keyword will never be exported.
  418. An entry can be toggled between COMMENT and normal with
  419. \\[org-toggle-comment].
  420. Changes become only effective after restarting Emacs."
  421. :group 'org-keywords
  422. :type 'string)
  423. (defcustom org-quote-string "QUOTE"
  424. "Entries starting with this keyword will be exported in fixed-width font.
  425. Quoting applies only to the text in the entry following the headline, and does
  426. not extend beyond the next headline, even if that is lower level.
  427. An entry can be toggled between QUOTE and normal with
  428. \\[org-toggle-fixed-width-section]."
  429. :group 'org-keywords
  430. :type 'string)
  431. (defconst org-repeat-re
  432. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  433. "Regular expression for specifying repeated events.
  434. After a match, group 1 contains the repeat expression.")
  435. (defgroup org-structure nil
  436. "Options concerning the general structure of Org-mode files."
  437. :tag "Org Structure"
  438. :group 'org)
  439. (defgroup org-reveal-location nil
  440. "Options about how to make context of a location visible."
  441. :tag "Org Reveal Location"
  442. :group 'org-structure)
  443. (defconst org-context-choice
  444. '(choice
  445. (const :tag "Always" t)
  446. (const :tag "Never" nil)
  447. (repeat :greedy t :tag "Individual contexts"
  448. (cons
  449. (choice :tag "Context"
  450. (const agenda)
  451. (const org-goto)
  452. (const occur-tree)
  453. (const tags-tree)
  454. (const link-search)
  455. (const mark-goto)
  456. (const bookmark-jump)
  457. (const isearch)
  458. (const default))
  459. (boolean))))
  460. "Contexts for the reveal options.")
  461. (defcustom org-show-hierarchy-above '((default . t))
  462. "Non-nil means, show full hierarchy when revealing a location.
  463. Org-mode often shows locations in an org-mode file which might have
  464. been invisible before. When this is set, the hierarchy of headings
  465. above the exposed location is shown.
  466. Turning this off for example for sparse trees makes them very compact.
  467. Instead of t, this can also be an alist specifying this option for different
  468. contexts. Valid contexts are
  469. agenda when exposing an entry from the agenda
  470. org-goto when using the command `org-goto' on key C-c C-j
  471. occur-tree when using the command `org-occur' on key C-c /
  472. tags-tree when constructing a sparse tree based on tags matches
  473. link-search when exposing search matches associated with a link
  474. mark-goto when exposing the jump goal of a mark
  475. bookmark-jump when exposing a bookmark location
  476. isearch when exiting from an incremental search
  477. default default for all contexts not set explicitly"
  478. :group 'org-reveal-location
  479. :type org-context-choice)
  480. (defcustom org-show-following-heading '((default . nil))
  481. "Non-nil means, show following heading when revealing a location.
  482. Org-mode often shows locations in an org-mode file which might have
  483. been invisible before. When this is set, the heading following the
  484. match is shown.
  485. Turning this off for example for sparse trees makes them very compact,
  486. but makes it harder to edit the location of the match. In such a case,
  487. use the command \\[org-reveal] to show more context.
  488. Instead of t, this can also be an alist specifying this option for different
  489. contexts. See `org-show-hierarchy-above' for valid contexts."
  490. :group 'org-reveal-location
  491. :type org-context-choice)
  492. (defcustom org-show-siblings '((default . nil) (isearch t))
  493. "Non-nil means, show all sibling heading when revealing a location.
  494. Org-mode often shows locations in an org-mode file which might have
  495. been invisible before. When this is set, the sibling of the current entry
  496. heading are all made visible. If `org-show-hierarchy-above' is t,
  497. the same happens on each level of the hierarchy above the current entry.
  498. By default this is on for the isearch context, off for all other contexts.
  499. Turning this off for example for sparse trees makes them very compact,
  500. but makes it harder to edit the location of the match. In such a case,
  501. use the command \\[org-reveal] to show more context.
  502. Instead of t, this can also be an alist specifying this option for different
  503. contexts. See `org-show-hierarchy-above' for valid contexts."
  504. :group 'org-reveal-location
  505. :type org-context-choice)
  506. (defcustom org-show-entry-below '((default . nil))
  507. "Non-nil means, show the entry below a headline when revealing a location.
  508. Org-mode often shows locations in an org-mode file which might have
  509. been invisible before. When this is set, the text below the headline that is
  510. exposed is also shown.
  511. By default this is off for all contexts.
  512. Instead of t, this can also be an alist specifying this option for different
  513. contexts. See `org-show-hierarchy-above' for valid contexts."
  514. :group 'org-reveal-location
  515. :type org-context-choice)
  516. (defcustom org-indirect-buffer-display 'other-window
  517. "How should indirect tree buffers be displayed?
  518. This applies to indirect buffers created with the commands
  519. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  520. Valid values are:
  521. current-window Display in the current window
  522. other-window Just display in another window.
  523. dedicated-frame Create one new frame, and re-use it each time.
  524. new-frame Make a new frame each time. Note that in this case
  525. previously-made indirect buffers are kept, and you need to
  526. kill these buffers yourself."
  527. :group 'org-structure
  528. :group 'org-agenda-windows
  529. :type '(choice
  530. (const :tag "In current window" current-window)
  531. (const :tag "In current frame, other window" other-window)
  532. (const :tag "Each time a new frame" new-frame)
  533. (const :tag "One dedicated frame" dedicated-frame)))
  534. (defcustom org-use-speed-commands nil
  535. "Non-nil means, activate single letter commands at beginning of a headline.
  536. This may also be a function to test for appropriate locations where speed
  537. commands should be active."
  538. :group 'org-structure
  539. :type '(choice
  540. (const :tag "Never" nil)
  541. (const :tag "At beginning of headline stars" t)
  542. (function)))
  543. (defcustom org-speed-commands-user nil
  544. "Alist of additional speed commands.
  545. This list will be checked before `org-speed-commands-default'
  546. when the variable `org-use-speed-commands' is non-nil
  547. and when the cursor is at the beginning of a headline.
  548. The car if each entry is a string with a single letter, which must
  549. be assigned to `self-insert-command' in the global map.
  550. The cdr is either a command to be called interactively, a function
  551. to be called, or a form to be evaluated.
  552. An entry that is just a list with a single string will be interpreted
  553. as a descriptive headline that will be added when listing the speed
  554. copmmands in the Help buffer using the `?' speed command."
  555. :group 'org-structure
  556. :type '(repeat :value ("k" . ignore)
  557. (choice :value ("k" . ignore)
  558. (list :tag "Descriptive Headline" (string :tag "Headline"))
  559. (cons :tag "Letter and Command"
  560. (string :tag "Command letter")
  561. (choice
  562. (function)
  563. (sexp))))))
  564. (defgroup org-cycle nil
  565. "Options concerning visibility cycling in Org-mode."
  566. :tag "Org Cycle"
  567. :group 'org-structure)
  568. (defcustom org-cycle-skip-children-state-if-no-children t
  569. "Non-nil means, skip CHILDREN state in entries that don't have any."
  570. :group 'org-cycle
  571. :type 'boolean)
  572. (defcustom org-cycle-max-level nil
  573. "Maximum level which should still be subject to visibility cycling.
  574. Levels higher than this will, for cycling, be treated as text, not a headline.
  575. When `org-odd-levels-only' is set, a value of N in this variable actually
  576. means 2N-1 stars as the limiting headline.
  577. When nil, cycle all levels.
  578. Note that the limiting level of cycling is also influenced by
  579. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  580. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  581. than its value."
  582. :group 'org-cycle
  583. :type '(choice
  584. (const :tag "No limit" nil)
  585. (integer :tag "Maximum level")))
  586. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  587. "Names of drawers. Drawers are not opened by cycling on the headline above.
  588. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  589. this:
  590. :DRAWERNAME:
  591. .....
  592. :END:
  593. The drawer \"PROPERTIES\" is special for capturing properties through
  594. the property API.
  595. Drawers can be defined on the per-file basis with a line like:
  596. #+DRAWERS: HIDDEN STATE PROPERTIES"
  597. :group 'org-structure
  598. :group 'org-cycle
  599. :type '(repeat (string :tag "Drawer Name")))
  600. (defcustom org-hide-block-startup nil
  601. "Non-nil means, , entering Org-mode will fold all blocks.
  602. This can also be set in on a per-file basis with
  603. #+STARTUP: hideblocks
  604. #+STARTUP: showblocks"
  605. :group 'org-startup
  606. :group 'org-cycle
  607. :type 'boolean)
  608. (defcustom org-cycle-global-at-bob nil
  609. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  610. This makes it possible to do global cycling without having to use S-TAB or
  611. C-u TAB. For this special case to work, the first line of the buffer
  612. must not be a headline - it may be empty or some other text. When used in
  613. this way, `org-cycle-hook' is disables temporarily, to make sure the
  614. cursor stays at the beginning of the buffer.
  615. When this option is nil, don't do anything special at the beginning
  616. of the buffer."
  617. :group 'org-cycle
  618. :type 'boolean)
  619. (defcustom org-cycle-level-after-item/entry-creation t
  620. "Non-nil means, cycle entry level or item indentation in new empty entries.
  621. When the cursor is at the end of an empty headline, i.e with only stars
  622. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  623. and then all possible anchestor states, before returning to the original state.
  624. This makes data entry extremely fast: M-RET to create a new headline,
  625. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  626. When the cursor is at the end of an empty plain list item, one TAB will
  627. make it a subitem, two or more tabs will back up to make this an item
  628. higher up in the item hierarchy."
  629. :group 'org-cycle
  630. :type 'boolean)
  631. (defcustom org-cycle-emulate-tab t
  632. "Where should `org-cycle' emulate TAB.
  633. nil Never
  634. white Only in completely white lines
  635. whitestart Only at the beginning of lines, before the first non-white char
  636. t Everywhere except in headlines
  637. exc-hl-bol Everywhere except at the start of a headline
  638. If TAB is used in a place where it does not emulate TAB, the current subtree
  639. visibility is cycled."
  640. :group 'org-cycle
  641. :type '(choice (const :tag "Never" nil)
  642. (const :tag "Only in completely white lines" white)
  643. (const :tag "Before first char in a line" whitestart)
  644. (const :tag "Everywhere except in headlines" t)
  645. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  646. ))
  647. (defcustom org-cycle-separator-lines 2
  648. "Number of empty lines needed to keep an empty line between collapsed trees.
  649. If you leave an empty line between the end of a subtree and the following
  650. headline, this empty line is hidden when the subtree is folded.
  651. Org-mode will leave (exactly) one empty line visible if the number of
  652. empty lines is equal or larger to the number given in this variable.
  653. So the default 2 means, at least 2 empty lines after the end of a subtree
  654. are needed to produce free space between a collapsed subtree and the
  655. following headline.
  656. If the number is negative, and the number of empty lines is at least -N,
  657. all empty lines are shown.
  658. Special case: when 0, never leave empty lines in collapsed view."
  659. :group 'org-cycle
  660. :type 'integer)
  661. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  662. (defcustom org-pre-cycle-hook nil
  663. "Hook that is run before visibility cycling is happening.
  664. The function(s) in this hook must accept a single argument which indicates
  665. the new state that will be set right after running this hook. The
  666. argument is a symbol. Before a global state change, it can have the values
  667. `overview', `content', or `all'. Before a local state change, it can have
  668. the values `folded', `children', or `subtree'."
  669. :group 'org-cycle
  670. :type 'hook)
  671. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  672. org-cycle-hide-drawers
  673. org-cycle-show-empty-lines
  674. org-optimize-window-after-visibility-change)
  675. "Hook that is run after `org-cycle' has changed the buffer visibility.
  676. The function(s) in this hook must accept a single argument which indicates
  677. the new state that was set by the most recent `org-cycle' command. The
  678. argument is a symbol. After a global state change, it can have the values
  679. `overview', `content', or `all'. After a local state change, it can have
  680. the values `folded', `children', or `subtree'."
  681. :group 'org-cycle
  682. :type 'hook)
  683. (defgroup org-edit-structure nil
  684. "Options concerning structure editing in Org-mode."
  685. :tag "Org Edit Structure"
  686. :group 'org-structure)
  687. (defcustom org-odd-levels-only nil
  688. "Non-nil means, skip even levels and only use odd levels for the outline.
  689. This has the effect that two stars are being added/taken away in
  690. promotion/demotion commands. It also influences how levels are
  691. handled by the exporters.
  692. Changing it requires restart of `font-lock-mode' to become effective
  693. for fontification also in regions already fontified.
  694. You may also set this on a per-file basis by adding one of the following
  695. lines to the buffer:
  696. #+STARTUP: odd
  697. #+STARTUP: oddeven"
  698. :group 'org-edit-structure
  699. :group 'org-font-lock
  700. :type 'boolean)
  701. (defcustom org-adapt-indentation t
  702. "Non-nil means, adapt indentation to outline node level.
  703. When this variable is set, Org assumes that you write outlines by
  704. indenting text in each node to align with the headline (after the stars).
  705. The following issues are influenced by this variable:
  706. - When this is set and the *entire* text in an entry is indented, the
  707. indentation is increased by one space in a demotion command, and
  708. decreased by one in a promotion command. If any line in the entry
  709. body starts with text at column 0, indentation is not changed at all.
  710. - Property drawers and planning information is inserted indented when
  711. this variable s set. When nil, they will not be indented.
  712. - TAB indents a line relative to context. The lines below a headline
  713. will be indented when this variable is set.
  714. Note that this is all about true indentation, by adding and removing
  715. space characters. See also `org-indent.el' which does level-dependent
  716. indentation in a virtual way, i.e. at display time in Emacs."
  717. :group 'org-edit-structure
  718. :type 'boolean)
  719. (defcustom org-special-ctrl-a/e nil
  720. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  721. When t, `C-a' will bring back the cursor to the beginning of the
  722. headline text, i.e. after the stars and after a possible TODO keyword.
  723. In an item, this will be the position after the bullet.
  724. When the cursor is already at that position, another `C-a' will bring
  725. it to the beginning of the line.
  726. `C-e' will jump to the end of the headline, ignoring the presence of tags
  727. in the headline. A second `C-e' will then jump to the true end of the
  728. line, after any tags. This also means that, when this variable is
  729. non-nil, `C-e' also will never jump beyond the end of the heading of a
  730. folded section, i.e. not after the ellipses.
  731. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  732. going to the true line boundary first. Only a directly following, identical
  733. keypress will bring the cursor to the special positions.
  734. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  735. set separately."
  736. :group 'org-edit-structure
  737. :type '(choice
  738. (const :tag "off" nil)
  739. (const :tag "on: after stars/bullet and before tags first" t)
  740. (const :tag "reversed: true line boundary first" reversed)
  741. (cons :tag "Set C-a and C-e separately"
  742. (choice :tag "Special C-a"
  743. (const :tag "off" nil)
  744. (const :tag "on: after stars/bullet first" t)
  745. (const :tag "reversed: before stars/bullet first" reversed))
  746. (choice :tag "Special C-e"
  747. (const :tag "off" nil)
  748. (const :tag "on: before tags first" t)
  749. (const :tag "reversed: after tags first" reversed)))))
  750. (if (fboundp 'defvaralias)
  751. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  752. (defcustom org-special-ctrl-k nil
  753. "Non-nil means `C-k' will behave specially in headlines.
  754. When nil, `C-k' will call the default `kill-line' command.
  755. When t, the following will happen while the cursor is in the headline:
  756. - When the cursor is at the beginning of a headline, kill the entire
  757. line and possible the folded subtree below the line.
  758. - When in the middle of the headline text, kill the headline up to the tags.
  759. - When after the headline text, kill the tags."
  760. :group 'org-edit-structure
  761. :type 'boolean)
  762. (defcustom org-yank-folded-subtrees t
  763. "Non-nil means, when yanking subtrees, fold them.
  764. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  765. it starts with a heading and all other headings in it are either children
  766. or siblings, then fold all the subtrees. However, do this only if no
  767. text after the yank would be swallowed into a folded tree by this action."
  768. :group 'org-edit-structure
  769. :type 'boolean)
  770. (defcustom org-yank-adjusted-subtrees nil
  771. "Non-nil means, when yanking subtrees, adjust the level.
  772. With this setting, `org-paste-subtree' is used to insert the subtree, see
  773. this function for details."
  774. :group 'org-edit-structure
  775. :type 'boolean)
  776. (defcustom org-M-RET-may-split-line '((default . t))
  777. "Non-nil means, M-RET will split the line at the cursor position.
  778. When nil, it will go to the end of the line before making a
  779. new line.
  780. You may also set this option in a different way for different
  781. contexts. Valid contexts are:
  782. headline when creating a new headline
  783. item when creating a new item
  784. table in a table field
  785. default the value to be used for all contexts not explicitly
  786. customized"
  787. :group 'org-structure
  788. :group 'org-table
  789. :type '(choice
  790. (const :tag "Always" t)
  791. (const :tag "Never" nil)
  792. (repeat :greedy t :tag "Individual contexts"
  793. (cons
  794. (choice :tag "Context"
  795. (const headline)
  796. (const item)
  797. (const table)
  798. (const default))
  799. (boolean)))))
  800. (defcustom org-insert-heading-respect-content nil
  801. "Non-nil means, insert new headings after the current subtree.
  802. When nil, the new heading is created directly after the current line.
  803. The commands \\[org-insert-heading-respect-content] and
  804. \\[org-insert-todo-heading-respect-content] turn this variable on
  805. for the duration of the command."
  806. :group 'org-structure
  807. :type 'boolean)
  808. (defcustom org-blank-before-new-entry '((heading . auto)
  809. (plain-list-item . auto))
  810. "Should `org-insert-heading' leave a blank line before new heading/item?
  811. The value is an alist, with `heading' and `plain-list-item' as car,
  812. and a boolean flag as cdr. For plain lists, if the variable
  813. `org-empty-line-terminates-plain-lists' is set, the setting here
  814. is ignored and no empty line is inserted, to keep the list in tact."
  815. :group 'org-edit-structure
  816. :type '(list
  817. (cons (const heading)
  818. (choice (const :tag "Never" nil)
  819. (const :tag "Always" t)
  820. (const :tag "Auto" auto)))
  821. (cons (const plain-list-item)
  822. (choice (const :tag "Never" nil)
  823. (const :tag "Always" t)
  824. (const :tag "Auto" auto)))))
  825. (defcustom org-insert-heading-hook nil
  826. "Hook being run after inserting a new heading."
  827. :group 'org-edit-structure
  828. :type 'hook)
  829. (defcustom org-enable-fixed-width-editor t
  830. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  831. This currently only means, they are never auto-wrapped.
  832. When nil, such lines will be treated like ordinary lines.
  833. See also the QUOTE keyword."
  834. :group 'org-edit-structure
  835. :type 'boolean)
  836. (defcustom org-goto-auto-isearch t
  837. "Non-nil means, typing characters in org-goto starts incremental search."
  838. :group 'org-edit-structure
  839. :type 'boolean)
  840. (defgroup org-sparse-trees nil
  841. "Options concerning sparse trees in Org-mode."
  842. :tag "Org Sparse Trees"
  843. :group 'org-structure)
  844. (defcustom org-highlight-sparse-tree-matches t
  845. "Non-nil means, highlight all matches that define a sparse tree.
  846. The highlights will automatically disappear the next time the buffer is
  847. changed by an edit command."
  848. :group 'org-sparse-trees
  849. :type 'boolean)
  850. (defcustom org-remove-highlights-with-change t
  851. "Non-nil means, any change to the buffer will remove temporary highlights.
  852. Such highlights are created by `org-occur' and `org-clock-display'.
  853. When nil, `C-c C-c needs to be used to get rid of the highlights.
  854. The highlights created by `org-preview-latex-fragment' always need
  855. `C-c C-c' to be removed."
  856. :group 'org-sparse-trees
  857. :group 'org-time
  858. :type 'boolean)
  859. (defcustom org-occur-hook '(org-first-headline-recenter)
  860. "Hook that is run after `org-occur' has constructed a sparse tree.
  861. This can be used to recenter the window to show as much of the structure
  862. as possible."
  863. :group 'org-sparse-trees
  864. :type 'hook)
  865. (defgroup org-imenu-and-speedbar nil
  866. "Options concerning imenu and speedbar in Org-mode."
  867. :tag "Org Imenu and Speedbar"
  868. :group 'org-structure)
  869. (defcustom org-imenu-depth 2
  870. "The maximum level for Imenu access to Org-mode headlines.
  871. This also applied for speedbar access."
  872. :group 'org-imenu-and-speedbar
  873. :type 'integer)
  874. (defgroup org-table nil
  875. "Options concerning tables in Org-mode."
  876. :tag "Org Table"
  877. :group 'org)
  878. (defcustom org-enable-table-editor 'optimized
  879. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  880. When nil, such lines will be treated like ordinary lines.
  881. When equal to the symbol `optimized', the table editor will be optimized to
  882. do the following:
  883. - Automatic overwrite mode in front of whitespace in table fields.
  884. This makes the structure of the table stay in tact as long as the edited
  885. field does not exceed the column width.
  886. - Minimize the number of realigns. Normally, the table is aligned each time
  887. TAB or RET are pressed to move to another field. With optimization this
  888. happens only if changes to a field might have changed the column width.
  889. Optimization requires replacing the functions `self-insert-command',
  890. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  891. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  892. very good at guessing when a re-align will be necessary, but you can always
  893. force one with \\[org-ctrl-c-ctrl-c].
  894. If you would like to use the optimized version in Org-mode, but the
  895. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  896. This variable can be used to turn on and off the table editor during a session,
  897. but in order to toggle optimization, a restart is required.
  898. See also the variable `org-table-auto-blank-field'."
  899. :group 'org-table
  900. :type '(choice
  901. (const :tag "off" nil)
  902. (const :tag "on" t)
  903. (const :tag "on, optimized" optimized)))
  904. (defcustom org-self-insert-cluster-for-undo t
  905. "Non-nil means cluster self-insert commands for undo when possible.
  906. If this is set, then, like in the Emacs command loop, 20 consecutive
  907. characters will be undone together.
  908. This is configurable, because there is some impact on typing performance."
  909. :group 'org-table
  910. :type 'boolean)
  911. (defcustom org-table-tab-recognizes-table.el t
  912. "Non-nil means, TAB will automatically notice a table.el table.
  913. When it sees such a table, it moves point into it and - if necessary -
  914. calls `table-recognize-table'."
  915. :group 'org-table-editing
  916. :type 'boolean)
  917. (defgroup org-link nil
  918. "Options concerning links in Org-mode."
  919. :tag "Org Link"
  920. :group 'org)
  921. (defvar org-link-abbrev-alist-local nil
  922. "Buffer-local version of `org-link-abbrev-alist', which see.
  923. The value of this is taken from the #+LINK lines.")
  924. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  925. (defcustom org-link-abbrev-alist nil
  926. "Alist of link abbreviations.
  927. The car of each element is a string, to be replaced at the start of a link.
  928. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  929. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  930. [[linkkey:tag][description]]
  931. The 'linkkey' must be a word word, starting with a letter, followed
  932. by letters, numbers, '-' or '_'.
  933. If REPLACE is a string, the tag will simply be appended to create the link.
  934. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  935. the placeholder \"%h\" will cause a url-encoded version of the tag to
  936. be inserted at that point (see the function `url-hexify-string').
  937. REPLACE may also be a function that will be called with the tag as the
  938. only argument to create the link, which should be returned as a string.
  939. See the manual for examples."
  940. :group 'org-link
  941. :type '(repeat
  942. (cons
  943. (string :tag "Protocol")
  944. (choice
  945. (string :tag "Format")
  946. (function)))))
  947. (defcustom org-descriptive-links t
  948. "Non-nil means, hide link part and only show description of bracket links.
  949. Bracket links are like [[link][description]]. This variable sets the initial
  950. state in new org-mode buffers. The setting can then be toggled on a
  951. per-buffer basis from the Org->Hyperlinks menu."
  952. :group 'org-link
  953. :type 'boolean)
  954. (defcustom org-link-file-path-type 'adaptive
  955. "How the path name in file links should be stored.
  956. Valid values are:
  957. relative Relative to the current directory, i.e. the directory of the file
  958. into which the link is being inserted.
  959. absolute Absolute path, if possible with ~ for home directory.
  960. noabbrev Absolute path, no abbreviation of home directory.
  961. adaptive Use relative path for files in the current directory and sub-
  962. directories of it. For other files, use an absolute path."
  963. :group 'org-link
  964. :type '(choice
  965. (const relative)
  966. (const absolute)
  967. (const noabbrev)
  968. (const adaptive)))
  969. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  970. "Types of links that should be activated in Org-mode files.
  971. This is a list of symbols, each leading to the activation of a certain link
  972. type. In principle, it does not hurt to turn on most link types - there may
  973. be a small gain when turning off unused link types. The types are:
  974. bracket The recommended [[link][description]] or [[link]] links with hiding.
  975. angular Links in angular brackets that may contain whitespace like
  976. <bbdb:Carsten Dominik>.
  977. plain Plain links in normal text, no whitespace, like http://google.com.
  978. radio Text that is matched by a radio target, see manual for details.
  979. tag Tag settings in a headline (link to tag search).
  980. date Time stamps (link to calendar).
  981. footnote Footnote labels.
  982. Changing this variable requires a restart of Emacs to become effective."
  983. :group 'org-link
  984. :type '(set :greedy t
  985. (const :tag "Double bracket links (new style)" bracket)
  986. (const :tag "Angular bracket links (old style)" angular)
  987. (const :tag "Plain text links" plain)
  988. (const :tag "Radio target matches" radio)
  989. (const :tag "Tags" tag)
  990. (const :tag "Timestamps" date)
  991. (const :tag "Footnotes" footnote)))
  992. (defcustom org-make-link-description-function nil
  993. "Function to use to generate link descriptions from links. If
  994. nil the link location will be used. This function must take two
  995. parameters; the first is the link and the second the description
  996. org-insert-link has generated, and should return the description
  997. to use."
  998. :group 'org-link
  999. :type 'function)
  1000. (defgroup org-link-store nil
  1001. "Options concerning storing links in Org-mode."
  1002. :tag "Org Store Link"
  1003. :group 'org-link)
  1004. (defcustom org-email-link-description-format "Email %c: %.30s"
  1005. "Format of the description part of a link to an email or usenet message.
  1006. The following %-escapes will be replaced by corresponding information:
  1007. %F full \"From\" field
  1008. %f name, taken from \"From\" field, address if no name
  1009. %T full \"To\" field
  1010. %t first name in \"To\" field, address if no name
  1011. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1012. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1013. %s subject
  1014. %m message-id.
  1015. You may use normal field width specification between the % and the letter.
  1016. This is for example useful to limit the length of the subject.
  1017. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1018. :group 'org-link-store
  1019. :type 'string)
  1020. (defcustom org-from-is-user-regexp
  1021. (let (r1 r2)
  1022. (when (and user-mail-address (not (string= user-mail-address "")))
  1023. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1024. (when (and user-full-name (not (string= user-full-name "")))
  1025. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1026. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1027. "Regexp matched against the \"From:\" header of an email or usenet message.
  1028. It should match if the message is from the user him/herself."
  1029. :group 'org-link-store
  1030. :type 'regexp)
  1031. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1032. "Non-nil means, storing a link to an Org file will use entry IDs.
  1033. Note that before this variable is even considered, org-id must be loaded,
  1034. so please customize `org-modules' and turn it on.
  1035. The variable can have the following values:
  1036. t Create an ID if needed to make a link to the current entry.
  1037. create-if-interactive
  1038. If `org-store-link' is called directly (interactively, as a user
  1039. command), do create an ID to support the link. But when doing the
  1040. job for remember, only use the ID if it already exists. The
  1041. purpose of this setting is to avoid proliferation of unwanted
  1042. IDs, just because you happen to be in an Org file when you
  1043. call `org-remember' that automatically and preemptively
  1044. creates a link. If you do want to get an ID link in a remember
  1045. template to an entry not having an ID, create it first by
  1046. explicitly creating a link to it, using `C-c C-l' first.
  1047. create-if-interactive-and-no-custom-id
  1048. Like create-if-interactive, but do not create an ID if there is
  1049. a CUSTOM_ID property defined in the entry. This is the default.
  1050. use-existing
  1051. Use existing ID, do not create one.
  1052. nil Never use an ID to make a link, instead link using a text search for
  1053. the headline text."
  1054. :group 'org-link-store
  1055. :type '(choice
  1056. (const :tag "Create ID to make link" t)
  1057. (const :tag "Create if storing link interactively"
  1058. create-if-interactive)
  1059. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1060. create-if-interactive-and-no-custom-id)
  1061. (const :tag "Only use existing" use-existing)
  1062. (const :tag "Do not use ID to create link" nil)))
  1063. (defcustom org-context-in-file-links t
  1064. "Non-nil means, file links from `org-store-link' contain context.
  1065. A search string will be added to the file name with :: as separator and
  1066. used to find the context when the link is activated by the command
  1067. `org-open-at-point'.
  1068. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1069. negates this setting for the duration of the command."
  1070. :group 'org-link-store
  1071. :type 'boolean)
  1072. (defcustom org-keep-stored-link-after-insertion nil
  1073. "Non-nil means, keep link in list for entire session.
  1074. The command `org-store-link' adds a link pointing to the current
  1075. location to an internal list. These links accumulate during a session.
  1076. The command `org-insert-link' can be used to insert links into any
  1077. Org-mode file (offering completion for all stored links). When this
  1078. option is nil, every link which has been inserted once using \\[org-insert-link]
  1079. will be removed from the list, to make completing the unused links
  1080. more efficient."
  1081. :group 'org-link-store
  1082. :type 'boolean)
  1083. (defgroup org-link-follow nil
  1084. "Options concerning following links in Org-mode."
  1085. :tag "Org Follow Link"
  1086. :group 'org-link)
  1087. (defcustom org-link-translation-function nil
  1088. "Function to translate links with different syntax to Org syntax.
  1089. This can be used to translate links created for example by the Planner
  1090. or emacs-wiki packages to Org syntax.
  1091. The function must accept two parameters, a TYPE containing the link
  1092. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1093. which is everything after the link protocol. It should return a cons
  1094. with possibly modified values of type and path.
  1095. Org contains a function for this, so if you set this variable to
  1096. `org-translate-link-from-planner', you should be able follow many
  1097. links created by planner."
  1098. :group 'org-link-follow
  1099. :type 'function)
  1100. (defcustom org-follow-link-hook nil
  1101. "Hook that is run after a link has been followed."
  1102. :group 'org-link-follow
  1103. :type 'hook)
  1104. (defcustom org-tab-follows-link nil
  1105. "Non-nil means, on links TAB will follow the link.
  1106. Needs to be set before org.el is loaded.
  1107. This really should not be used, it does not make sense, and the
  1108. implementation is bad."
  1109. :group 'org-link-follow
  1110. :type 'boolean)
  1111. (defcustom org-return-follows-link nil
  1112. "Non-nil means, on links RET will follow the link.
  1113. Needs to be set before org.el is loaded."
  1114. :group 'org-link-follow
  1115. :type 'boolean)
  1116. (defcustom org-mouse-1-follows-link
  1117. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1118. "Non-nil means, mouse-1 on a link will follow the link.
  1119. A longer mouse click will still set point. Does not work on XEmacs.
  1120. Needs to be set before org.el is loaded."
  1121. :group 'org-link-follow
  1122. :type 'boolean)
  1123. (defcustom org-mark-ring-length 4
  1124. "Number of different positions to be recorded in the ring
  1125. Changing this requires a restart of Emacs to work correctly."
  1126. :group 'org-link-follow
  1127. :type 'integer)
  1128. (defcustom org-link-frame-setup
  1129. '((vm . vm-visit-folder-other-frame)
  1130. (gnus . gnus-other-frame)
  1131. (file . find-file-other-window))
  1132. "Setup the frame configuration for following links.
  1133. When following a link with Emacs, it may often be useful to display
  1134. this link in another window or frame. This variable can be used to
  1135. set this up for the different types of links.
  1136. For VM, use any of
  1137. `vm-visit-folder'
  1138. `vm-visit-folder-other-frame'
  1139. For Gnus, use any of
  1140. `gnus'
  1141. `gnus-other-frame'
  1142. `org-gnus-no-new-news'
  1143. For FILE, use any of
  1144. `find-file'
  1145. `find-file-other-window'
  1146. `find-file-other-frame'
  1147. For the calendar, use the variable `calendar-setup'.
  1148. For BBDB, it is currently only possible to display the matches in
  1149. another window."
  1150. :group 'org-link-follow
  1151. :type '(list
  1152. (cons (const vm)
  1153. (choice
  1154. (const vm-visit-folder)
  1155. (const vm-visit-folder-other-window)
  1156. (const vm-visit-folder-other-frame)))
  1157. (cons (const gnus)
  1158. (choice
  1159. (const gnus)
  1160. (const gnus-other-frame)
  1161. (const org-gnus-no-new-news)))
  1162. (cons (const file)
  1163. (choice
  1164. (const find-file)
  1165. (const find-file-other-window)
  1166. (const find-file-other-frame)))))
  1167. (defcustom org-display-internal-link-with-indirect-buffer nil
  1168. "Non-nil means, use indirect buffer to display infile links.
  1169. Activating internal links (from one location in a file to another location
  1170. in the same file) normally just jumps to the location. When the link is
  1171. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1172. another window. When this option is set, the other window actually displays
  1173. an indirect buffer clone of the current buffer, to avoid any visibility
  1174. changes to the current buffer."
  1175. :group 'org-link-follow
  1176. :type 'boolean)
  1177. (defcustom org-open-non-existing-files nil
  1178. "Non-nil means, `org-open-file' will open non-existing files.
  1179. When nil, an error will be generated.
  1180. This variable applies only to external applications because they
  1181. might choke on non-existing files. If the link is to a file that
  1182. will be opened in Emacs, the variable is ignored."
  1183. :group 'org-link-follow
  1184. :type 'boolean)
  1185. (defcustom org-open-directory-means-index-dot-org nil
  1186. "Non-nil means, a link to a directory really means to index.org.
  1187. When nil, following a directory link will run dired or open a finder/explorer
  1188. window on that directory."
  1189. :group 'org-link-follow
  1190. :type 'boolean)
  1191. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1192. "Function and arguments to call for following mailto links.
  1193. This is a list with the first element being a lisp function, and the
  1194. remaining elements being arguments to the function. In string arguments,
  1195. %a will be replaced by the address, and %s will be replaced by the subject
  1196. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1197. :group 'org-link-follow
  1198. :type '(choice
  1199. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1200. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1201. (const :tag "message-mail" (message-mail "%a" "%s"))
  1202. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1203. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1204. "Non-nil means, ask for confirmation before executing shell links.
  1205. Shell links can be dangerous: just think about a link
  1206. [[shell:rm -rf ~/*][Google Search]]
  1207. This link would show up in your Org-mode document as \"Google Search\",
  1208. but really it would remove your entire home directory.
  1209. Therefore we advise against setting this variable to nil.
  1210. Just change it to `y-or-n-p' if you want to confirm with a
  1211. single keystroke rather than having to type \"yes\"."
  1212. :group 'org-link-follow
  1213. :type '(choice
  1214. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1215. (const :tag "with y-or-n (faster)" y-or-n-p)
  1216. (const :tag "no confirmation (dangerous)" nil)))
  1217. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1218. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1219. Elisp links can be dangerous: just think about a link
  1220. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1221. This link would show up in your Org-mode document as \"Google Search\",
  1222. but really it would remove your entire home directory.
  1223. Therefore we advise against setting this variable to nil.
  1224. Just change it to `y-or-n-p' if you want to confirm with a
  1225. single keystroke rather than having to type \"yes\"."
  1226. :group 'org-link-follow
  1227. :type '(choice
  1228. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1229. (const :tag "with y-or-n (faster)" y-or-n-p)
  1230. (const :tag "no confirmation (dangerous)" nil)))
  1231. (defconst org-file-apps-defaults-gnu
  1232. '((remote . emacs)
  1233. (system . mailcap)
  1234. (t . mailcap))
  1235. "Default file applications on a UNIX or GNU/Linux system.
  1236. See `org-file-apps'.")
  1237. (defconst org-file-apps-defaults-macosx
  1238. '((remote . emacs)
  1239. (t . "open %s")
  1240. (system . "open %s")
  1241. ("ps.gz" . "gv %s")
  1242. ("eps.gz" . "gv %s")
  1243. ("dvi" . "xdvi %s")
  1244. ("fig" . "xfig %s"))
  1245. "Default file applications on a MacOS X system.
  1246. The system \"open\" is known as a default, but we use X11 applications
  1247. for some files for which the OS does not have a good default.
  1248. See `org-file-apps'.")
  1249. (defconst org-file-apps-defaults-windowsnt
  1250. (list
  1251. '(remote . emacs)
  1252. (cons t
  1253. (list (if (featurep 'xemacs)
  1254. 'mswindows-shell-execute
  1255. 'w32-shell-execute)
  1256. "open" 'file))
  1257. (cons 'system
  1258. (list (if (featurep 'xemacs)
  1259. 'mswindows-shell-execute
  1260. 'w32-shell-execute)
  1261. "open" 'file)))
  1262. "Default file applications on a Windows NT system.
  1263. The system \"open\" is used for most files.
  1264. See `org-file-apps'.")
  1265. (defcustom org-file-apps
  1266. '(
  1267. (auto-mode . emacs)
  1268. ("\\.mm\\'" . default)
  1269. ("\\.x?html?\\'" . default)
  1270. ("\\.pdf\\'" . default)
  1271. )
  1272. "External applications for opening `file:path' items in a document.
  1273. Org-mode uses system defaults for different file types, but
  1274. you can use this variable to set the application for a given file
  1275. extension. The entries in this list are cons cells where the car identifies
  1276. files and the cdr the corresponding command. Possible values for the
  1277. file identifier are
  1278. \"regex\" Regular expression matched against the file name. For backward
  1279. compatibility, this can also be a string with only alphanumeric
  1280. characters, which is then interpreted as an extension.
  1281. `directory' Matches a directory
  1282. `remote' Matches a remote file, accessible through tramp or efs.
  1283. Remote files most likely should be visited through Emacs
  1284. because external applications cannot handle such paths.
  1285. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1286. so all files Emacs knows how to handle. Using this with
  1287. command `emacs' will open most files in Emacs. Beware that this
  1288. will also open html files inside Emacs, unless you add
  1289. (\"html\" . default) to the list as well.
  1290. t Default for files not matched by any of the other options.
  1291. `system' The system command to open files, like `open' on Windows
  1292. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1293. that will be selected if you call `C-c C-o' with a double
  1294. `C-u C-u' prefix.
  1295. Possible values for the command are:
  1296. `emacs' The file will be visited by the current Emacs process.
  1297. `default' Use the default application for this file type, which is the
  1298. association for t in the list, most likely in the system-specific
  1299. part.
  1300. This can be used to overrule an unwanted setting in the
  1301. system-specific variable.
  1302. `system' Use the system command for opening files, like \"open\".
  1303. This command is specified by the entry whose car is `system'.
  1304. Most likely, the system-specific version of this variable
  1305. does define this command, but you can overrule/replace it
  1306. here.
  1307. string A command to be executed by a shell; %s will be replaced
  1308. by the path to the file.
  1309. sexp A Lisp form which will be evaluated. The file path will
  1310. be available in the Lisp variable `file'.
  1311. For more examples, see the system specific constants
  1312. `org-file-apps-defaults-macosx'
  1313. `org-file-apps-defaults-windowsnt'
  1314. `org-file-apps-defaults-gnu'."
  1315. :group 'org-link-follow
  1316. :type '(repeat
  1317. (cons (choice :value ""
  1318. (string :tag "Extension")
  1319. (const :tag "System command to open files" system)
  1320. (const :tag "Default for unrecognized files" t)
  1321. (const :tag "Remote file" remote)
  1322. (const :tag "Links to a directory" directory)
  1323. (const :tag "Any files that have Emacs modes"
  1324. auto-mode))
  1325. (choice :value ""
  1326. (const :tag "Visit with Emacs" emacs)
  1327. (const :tag "Use default" default)
  1328. (const :tag "Use the system command" system)
  1329. (string :tag "Command")
  1330. (sexp :tag "Lisp form")))))
  1331. (defgroup org-refile nil
  1332. "Options concerning refiling entries in Org-mode."
  1333. :tag "Org Refile"
  1334. :group 'org)
  1335. (defcustom org-directory "~/org"
  1336. "Directory with org files.
  1337. This is just a default location to look for Org files. There is no need
  1338. at all to put your files into this directory. It is only used in the
  1339. following situations:
  1340. 1. When a remember template specifies a target file that is not an
  1341. absolute path. The path will then be interpreted relative to
  1342. `org-directory'
  1343. 2. When a remember note is filed away in an interactive way (when exiting the
  1344. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1345. with `org-directory' as the default path."
  1346. :group 'org-refile
  1347. :group 'org-remember
  1348. :type 'directory)
  1349. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1350. "Default target for storing notes.
  1351. Used by the hooks for remember.el. This can be a string, or nil to mean
  1352. the value of `remember-data-file'.
  1353. You can set this on a per-template basis with the variable
  1354. `org-remember-templates'."
  1355. :group 'org-refile
  1356. :group 'org-remember
  1357. :type '(choice
  1358. (const :tag "Default from remember-data-file" nil)
  1359. file))
  1360. (defcustom org-goto-interface 'outline
  1361. "The default interface to be used for `org-goto'.
  1362. Allowed values are:
  1363. outline The interface shows an outline of the relevant file
  1364. and the correct heading is found by moving through
  1365. the outline or by searching with incremental search.
  1366. outline-path-completion Headlines in the current buffer are offered via
  1367. completion. This is the interface also used by
  1368. the refile command."
  1369. :group 'org-refile
  1370. :type '(choice
  1371. (const :tag "Outline" outline)
  1372. (const :tag "Outline-path-completion" outline-path-completion)))
  1373. (defcustom org-goto-max-level 5
  1374. "Maximum level to be considered when running org-goto with refile interface."
  1375. :group 'org-refile
  1376. :type 'integer)
  1377. (defcustom org-reverse-note-order nil
  1378. "Non-nil means, store new notes at the beginning of a file or entry.
  1379. When nil, new notes will be filed to the end of a file or entry.
  1380. This can also be a list with cons cells of regular expressions that
  1381. are matched against file names, and values."
  1382. :group 'org-remember
  1383. :group 'org-refile
  1384. :type '(choice
  1385. (const :tag "Reverse always" t)
  1386. (const :tag "Reverse never" nil)
  1387. (repeat :tag "By file name regexp"
  1388. (cons regexp boolean))))
  1389. (defcustom org-refile-targets nil
  1390. "Targets for refiling entries with \\[org-refile].
  1391. This is list of cons cells. Each cell contains:
  1392. - a specification of the files to be considered, either a list of files,
  1393. or a symbol whose function or variable value will be used to retrieve
  1394. a file name or a list of file names. If you use `org-agenda-files' for
  1395. that, all agenda files will be scanned for targets. Nil means, consider
  1396. headings in the current buffer.
  1397. - A specification of how to find candidate refile targets. This may be
  1398. any of:
  1399. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1400. This tag has to be present in all target headlines, inheritance will
  1401. not be considered.
  1402. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1403. todo keyword.
  1404. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1405. headlines that are refiling targets.
  1406. - a cons cell (:level . N). Any headline of level N is considered a target.
  1407. Note that, when `org-odd-levels-only' is set, level corresponds to
  1408. order in hierarchy, not to the number of stars.
  1409. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1410. Note that, when `org-odd-levels-only' is set, level corresponds to
  1411. order in hierarchy, not to the number of stars.
  1412. You can set the variable `org-refile-target-verify-function' to a function
  1413. to verify each headline found by the simple critery above.
  1414. When this variable is nil, all top-level headlines in the current buffer
  1415. are used, equivalent to the value `((nil . (:level . 1))'."
  1416. :group 'org-refile
  1417. :type '(repeat
  1418. (cons
  1419. (choice :value org-agenda-files
  1420. (const :tag "All agenda files" org-agenda-files)
  1421. (const :tag "Current buffer" nil)
  1422. (function) (variable) (file))
  1423. (choice :tag "Identify target headline by"
  1424. (cons :tag "Specific tag" (const :value :tag) (string))
  1425. (cons :tag "TODO keyword" (const :value :todo) (string))
  1426. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1427. (cons :tag "Level number" (const :value :level) (integer))
  1428. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1429. (defcustom org-refile-target-verify-function nil
  1430. "Function to verify if the headline at point should be a refile target.
  1431. The function will be called without arguments, with point at the
  1432. beginning of the headline. It should return t and leave point
  1433. where it is if the headline is a valid target for refiling.
  1434. If the target should not be selected, the function must return nil.
  1435. In addition to this, it may move point to a place from where the search
  1436. should be continued. For example, the function may decide that the entire
  1437. subtree of the current entry should be excluded and move point to the end
  1438. of the subtree."
  1439. :group 'org-refile
  1440. :type 'function)
  1441. (defcustom org-refile-use-outline-path nil
  1442. "Non-nil means, provide refile targets as paths.
  1443. So a level 3 headline will be available as level1/level2/level3.
  1444. When the value is `file', also include the file name (without directory)
  1445. into the path. In this case, you can also stop the completion after
  1446. the file name, to get entries inserted as top level in the file.
  1447. When `full-file-path', include the full file path."
  1448. :group 'org-refile
  1449. :type '(choice
  1450. (const :tag "Not" nil)
  1451. (const :tag "Yes" t)
  1452. (const :tag "Start with file name" file)
  1453. (const :tag "Start with full file path" full-file-path)))
  1454. (defcustom org-outline-path-complete-in-steps t
  1455. "Non-nil means, complete the outline path in hierarchical steps.
  1456. When Org-mode uses the refile interface to select an outline path
  1457. \(see variable `org-refile-use-outline-path'), the completion of
  1458. the path can be done is a single go, or if can be done in steps down
  1459. the headline hierarchy. Going in steps is probably the best if you
  1460. do not use a special completion package like `ido' or `icicles'.
  1461. However, when using these packages, going in one step can be very
  1462. fast, while still showing the whole path to the entry."
  1463. :group 'org-refile
  1464. :type 'boolean)
  1465. (defcustom org-refile-allow-creating-parent-nodes nil
  1466. "Non-nil means, allow to create new nodes as refile targets.
  1467. New nodes are then created by adding \"/new node name\" to the completion
  1468. of an existing node. When the value of this variable is `confirm',
  1469. new node creation must be confirmed by the user (recommended)
  1470. When nil, the completion must match an existing entry.
  1471. Note that, if the new heading is not seen by the criteria
  1472. listed in `org-refile-targets', multiple instances of the same
  1473. heading would be created by trying again to file under the new
  1474. heading."
  1475. :group 'org-refile
  1476. :type '(choice
  1477. (const :tag "Never" nil)
  1478. (const :tag "Always" t)
  1479. (const :tag "Prompt for confirmation" confirm)))
  1480. (defgroup org-todo nil
  1481. "Options concerning TODO items in Org-mode."
  1482. :tag "Org TODO"
  1483. :group 'org)
  1484. (defgroup org-progress nil
  1485. "Options concerning Progress logging in Org-mode."
  1486. :tag "Org Progress"
  1487. :group 'org-time)
  1488. (defvar org-todo-interpretation-widgets
  1489. '(
  1490. (:tag "Sequence (cycling hits every state)" sequence)
  1491. (:tag "Type (cycling directly to DONE)" type))
  1492. "The available interpretation symbols for customizing
  1493. `org-todo-keywords'.
  1494. Interested libraries should add to this list.")
  1495. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1496. "List of TODO entry keyword sequences and their interpretation.
  1497. \\<org-mode-map>This is a list of sequences.
  1498. Each sequence starts with a symbol, either `sequence' or `type',
  1499. indicating if the keywords should be interpreted as a sequence of
  1500. action steps, or as different types of TODO items. The first
  1501. keywords are states requiring action - these states will select a headline
  1502. for inclusion into the global TODO list Org-mode produces. If one of
  1503. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1504. signify that no further action is necessary. If \"|\" is not found,
  1505. the last keyword is treated as the only DONE state of the sequence.
  1506. The command \\[org-todo] cycles an entry through these states, and one
  1507. additional state where no keyword is present. For details about this
  1508. cycling, see the manual.
  1509. TODO keywords and interpretation can also be set on a per-file basis with
  1510. the special #+SEQ_TODO and #+TYP_TODO lines.
  1511. Each keyword can optionally specify a character for fast state selection
  1512. \(in combination with the variable `org-use-fast-todo-selection')
  1513. and specifiers for state change logging, using the same syntax
  1514. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1515. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1516. indicates to record a time stamp each time this state is selected.
  1517. Each keyword may also specify if a timestamp or a note should be
  1518. recorded when entering or leaving the state, by adding additional
  1519. characters in the parenthesis after the keyword. This looks like this:
  1520. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1521. record only the time of the state change. With X and Y being either
  1522. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1523. Y when leaving the state if and only if the *target* state does not
  1524. define X. You may omit any of the fast-selection key or X or /Y,
  1525. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1526. For backward compatibility, this variable may also be just a list
  1527. of keywords - in this case the interpretation (sequence or type) will be
  1528. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1529. :group 'org-todo
  1530. :group 'org-keywords
  1531. :type '(choice
  1532. (repeat :tag "Old syntax, just keywords"
  1533. (string :tag "Keyword"))
  1534. (repeat :tag "New syntax"
  1535. (cons
  1536. (choice
  1537. :tag "Interpretation"
  1538. ;;Quick and dirty way to see
  1539. ;;`org-todo-interpretations'. This takes the
  1540. ;;place of item arguments
  1541. :convert-widget
  1542. (lambda (widget)
  1543. (widget-put widget
  1544. :args (mapcar
  1545. #'(lambda (x)
  1546. (widget-convert
  1547. (cons 'const x)))
  1548. org-todo-interpretation-widgets))
  1549. widget))
  1550. (repeat
  1551. (string :tag "Keyword"))))))
  1552. (defvar org-todo-keywords-1 nil
  1553. "All TODO and DONE keywords active in a buffer.")
  1554. (make-variable-buffer-local 'org-todo-keywords-1)
  1555. (defvar org-todo-keywords-for-agenda nil)
  1556. (defvar org-done-keywords-for-agenda nil)
  1557. (defvar org-drawers-for-agenda nil)
  1558. (defvar org-todo-keyword-alist-for-agenda nil)
  1559. (defvar org-tag-alist-for-agenda nil)
  1560. (defvar org-agenda-contributing-files nil)
  1561. (defvar org-not-done-keywords nil)
  1562. (make-variable-buffer-local 'org-not-done-keywords)
  1563. (defvar org-done-keywords nil)
  1564. (make-variable-buffer-local 'org-done-keywords)
  1565. (defvar org-todo-heads nil)
  1566. (make-variable-buffer-local 'org-todo-heads)
  1567. (defvar org-todo-sets nil)
  1568. (make-variable-buffer-local 'org-todo-sets)
  1569. (defvar org-todo-log-states nil)
  1570. (make-variable-buffer-local 'org-todo-log-states)
  1571. (defvar org-todo-kwd-alist nil)
  1572. (make-variable-buffer-local 'org-todo-kwd-alist)
  1573. (defvar org-todo-key-alist nil)
  1574. (make-variable-buffer-local 'org-todo-key-alist)
  1575. (defvar org-todo-key-trigger nil)
  1576. (make-variable-buffer-local 'org-todo-key-trigger)
  1577. (defcustom org-todo-interpretation 'sequence
  1578. "Controls how TODO keywords are interpreted.
  1579. This variable is in principle obsolete and is only used for
  1580. backward compatibility, if the interpretation of todo keywords is
  1581. not given already in `org-todo-keywords'. See that variable for
  1582. more information."
  1583. :group 'org-todo
  1584. :group 'org-keywords
  1585. :type '(choice (const sequence)
  1586. (const type)))
  1587. (defcustom org-use-fast-todo-selection t
  1588. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1589. This variable describes if and under what circumstances the cycling
  1590. mechanism for TODO keywords will be replaced by a single-key, direct
  1591. selection scheme.
  1592. When nil, fast selection is never used.
  1593. When the symbol `prefix', it will be used when `org-todo' is called with
  1594. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1595. in an agenda buffer.
  1596. When t, fast selection is used by default. In this case, the prefix
  1597. argument forces cycling instead.
  1598. In all cases, the special interface is only used if access keys have actually
  1599. been assigned by the user, i.e. if keywords in the configuration are followed
  1600. by a letter in parenthesis, like TODO(t)."
  1601. :group 'org-todo
  1602. :type '(choice
  1603. (const :tag "Never" nil)
  1604. (const :tag "By default" t)
  1605. (const :tag "Only with C-u C-c C-t" prefix)))
  1606. (defcustom org-provide-todo-statistics t
  1607. "Non-nil means, update todo statistics after insert and toggle.
  1608. ALL-HEADLINES means update todo statistics by including headlines
  1609. with no TODO keyword as well, counting them as not done.
  1610. A list of TODO keywords means the same, but skip keywords that are
  1611. not in this list.
  1612. When this is set, todo statistics is updated in the parent of the
  1613. current entry each time a todo state is changed."
  1614. :group 'org-todo
  1615. :type '(choice
  1616. (const :tag "Yes, only for TODO entries" t)
  1617. (const :tag "Yes, including all entries" 'all-headlines)
  1618. (repeat :tag "Yes, for TODOs in this list"
  1619. (string :tag "TODO keyword"))
  1620. (other :tag "No TODO statistics" nil)))
  1621. (defcustom org-hierarchical-todo-statistics t
  1622. "Non-nil means, TODO statistics covers just direct children.
  1623. When nil, all entries in the subtree are considered.
  1624. This has only an effect if `org-provide-todo-statistics' is set.
  1625. To set this to nil for only a single subtree, use a COOKIE_DATA
  1626. property and include the word \"recursive\" into the value."
  1627. :group 'org-todo
  1628. :type 'boolean)
  1629. (defcustom org-after-todo-state-change-hook nil
  1630. "Hook which is run after the state of a TODO item was changed.
  1631. The new state (a string with a TODO keyword, or nil) is available in the
  1632. Lisp variable `state'."
  1633. :group 'org-todo
  1634. :type 'hook)
  1635. (defvar org-blocker-hook nil
  1636. "Hook for functions that are allowed to block a state change.
  1637. Each function gets as its single argument a property list, see
  1638. `org-trigger-hook' for more information about this list.
  1639. If any of the functions in this hook returns nil, the state change
  1640. is blocked.")
  1641. (defvar org-trigger-hook nil
  1642. "Hook for functions that are triggered by a state change.
  1643. Each function gets as its single argument a property list with at least
  1644. the following elements:
  1645. (:type type-of-change :position pos-at-entry-start
  1646. :from old-state :to new-state)
  1647. Depending on the type, more properties may be present.
  1648. This mechanism is currently implemented for:
  1649. TODO state changes
  1650. ------------------
  1651. :type todo-state-change
  1652. :from previous state (keyword as a string), or nil, or a symbol
  1653. 'todo' or 'done', to indicate the general type of state.
  1654. :to new state, like in :from")
  1655. (defcustom org-enforce-todo-dependencies nil
  1656. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1657. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1658. be blocked if any prior sibling is not yet done.
  1659. Finally, if the parent is blocked because of ordered siblings of its own,
  1660. the child will also be blocked.
  1661. This variable needs to be set before org.el is loaded, and you need to
  1662. restart Emacs after a change to make the change effective. The only way
  1663. to change is while Emacs is running is through the customize interface."
  1664. :set (lambda (var val)
  1665. (set var val)
  1666. (if val
  1667. (add-hook 'org-blocker-hook
  1668. 'org-block-todo-from-children-or-siblings-or-parent)
  1669. (remove-hook 'org-blocker-hook
  1670. 'org-block-todo-from-children-or-siblings-or-parent)))
  1671. :group 'org-todo
  1672. :type 'boolean)
  1673. (defcustom org-enforce-todo-checkbox-dependencies nil
  1674. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1675. When this is nil, checkboxes have no influence on switching TODO states.
  1676. When non-nil, you first need to check off all check boxes before the TODO
  1677. entry can be switched to DONE.
  1678. This variable needs to be set before org.el is loaded, and you need to
  1679. restart Emacs after a change to make the change effective. The only way
  1680. to change is while Emacs is running is through the customize interface."
  1681. :set (lambda (var val)
  1682. (set var val)
  1683. (if val
  1684. (add-hook 'org-blocker-hook
  1685. 'org-block-todo-from-checkboxes)
  1686. (remove-hook 'org-blocker-hook
  1687. 'org-block-todo-from-checkboxes)))
  1688. :group 'org-todo
  1689. :type 'boolean)
  1690. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1691. "Non-nil means, inserting a TODO heading is treated as state change.
  1692. So when the command \\[org-insert-todo-heading] is used, state change
  1693. logging will apply if appropriate. When nil, the new TODO item will
  1694. be inserted directly, and no logging will take place."
  1695. :group 'org-todo
  1696. :type 'boolean)
  1697. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1698. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1699. This is the default behavior. However, setting this to nil allows a
  1700. convenient way to select a TODO state and bypass any logging associated
  1701. with that."
  1702. :group 'org-todo
  1703. :type 'boolean)
  1704. (defcustom org-todo-state-tags-triggers nil
  1705. "Tag changes that should be triggered by TODO state changes.
  1706. This is a list. Each entry is
  1707. (state-change (tag . flag) .......)
  1708. State-change can be a string with a state, and empty string to indicate the
  1709. state that has no TODO keyword, or it can be one of the symbols `todo'
  1710. or `done', meaning any not-done or done state, respectively."
  1711. :group 'org-todo
  1712. :group 'org-tags
  1713. :type '(repeat
  1714. (cons (choice :tag "When changing to"
  1715. (const :tag "Not-done state" todo)
  1716. (const :tag "Done state" done)
  1717. (string :tag "State"))
  1718. (repeat
  1719. (cons :tag "Tag action"
  1720. (string :tag "Tag")
  1721. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1722. (defcustom org-log-done nil
  1723. "Information to record when a task moves to the DONE state.
  1724. Possible values are:
  1725. nil Don't add anything, just change the keyword
  1726. time Add a time stamp to the task
  1727. note Prompt for a note and add it with template `org-log-note-headings'
  1728. This option can also be set with on a per-file-basis with
  1729. #+STARTUP: nologdone
  1730. #+STARTUP: logdone
  1731. #+STARTUP: lognotedone
  1732. You can have local logging settings for a subtree by setting the LOGGING
  1733. property to one or more of these keywords."
  1734. :group 'org-todo
  1735. :group 'org-progress
  1736. :type '(choice
  1737. (const :tag "No logging" nil)
  1738. (const :tag "Record CLOSED timestamp" time)
  1739. (const :tag "Record CLOSED timestamp with note." note)))
  1740. ;; Normalize old uses of org-log-done.
  1741. (cond
  1742. ((eq org-log-done t) (setq org-log-done 'time))
  1743. ((and (listp org-log-done) (memq 'done org-log-done))
  1744. (setq org-log-done 'note)))
  1745. (defcustom org-log-reschedule nil
  1746. "Information to record when the scheduling date of a tasks is modified.
  1747. Possible values are:
  1748. nil Don't add anything, just change the date
  1749. time Add a time stamp to the task
  1750. note Prompt for a note and add it with template `org-log-note-headings'
  1751. This option can also be set with on a per-file-basis with
  1752. #+STARTUP: nologreschedule
  1753. #+STARTUP: logreschedule
  1754. #+STARTUP: lognotereschedule"
  1755. :group 'org-todo
  1756. :group 'org-progress
  1757. :type '(choice
  1758. (const :tag "No logging" nil)
  1759. (const :tag "Record timestamp" time)
  1760. (const :tag "Record timestamp with note." note)))
  1761. (defcustom org-log-redeadline nil
  1762. "Information to record when the deadline date of a tasks is modified.
  1763. Possible values are:
  1764. nil Don't add anything, just change the date
  1765. time Add a time stamp to the task
  1766. note Prompt for a note and add it with template `org-log-note-headings'
  1767. This option can also be set with on a per-file-basis with
  1768. #+STARTUP: nologredeadline
  1769. #+STARTUP: logredeadline
  1770. #+STARTUP: lognoteredeadline
  1771. You can have local logging settings for a subtree by setting the LOGGING
  1772. property to one or more of these keywords."
  1773. :group 'org-todo
  1774. :group 'org-progress
  1775. :type '(choice
  1776. (const :tag "No logging" nil)
  1777. (const :tag "Record timestamp" time)
  1778. (const :tag "Record timestamp with note." note)))
  1779. (defcustom org-log-note-clock-out nil
  1780. "Non-nil means, record a note when clocking out of an item.
  1781. This can also be configured on a per-file basis by adding one of
  1782. the following lines anywhere in the buffer:
  1783. #+STARTUP: lognoteclock-out
  1784. #+STARTUP: nolognoteclock-out"
  1785. :group 'org-todo
  1786. :group 'org-progress
  1787. :type 'boolean)
  1788. (defcustom org-log-done-with-time t
  1789. "Non-nil means, the CLOSED time stamp will contain date and time.
  1790. When nil, only the date will be recorded."
  1791. :group 'org-progress
  1792. :type 'boolean)
  1793. (defcustom org-log-note-headings
  1794. '((done . "CLOSING NOTE %t")
  1795. (state . "State %-12s from %-12S %t")
  1796. (note . "Note taken on %t")
  1797. (reschedule . "Rescheduled from %S on %t")
  1798. (delschedule . "Not scheduled, was %S on %t")
  1799. (redeadline . "New deadline from %S on %t")
  1800. (deldeadline . "Removed deadline, was %S on %t")
  1801. (clock-out . ""))
  1802. "Headings for notes added to entries.
  1803. The value is an alist, with the car being a symbol indicating the note
  1804. context, and the cdr is the heading to be used. The heading may also be the
  1805. empty string.
  1806. %t in the heading will be replaced by a time stamp.
  1807. %s will be replaced by the new TODO state, in double quotes.
  1808. %S will be replaced by the old TODO state, in double quotes.
  1809. %u will be replaced by the user name.
  1810. %U will be replaced by the full user name.
  1811. In fact, it is not a good idea to change the `state' entry, because
  1812. agenda log mode depends on the format of these entries."
  1813. :group 'org-todo
  1814. :group 'org-progress
  1815. :type '(list :greedy t
  1816. (cons (const :tag "Heading when closing an item" done) string)
  1817. (cons (const :tag
  1818. "Heading when changing todo state (todo sequence only)"
  1819. state) string)
  1820. (cons (const :tag "Heading when just taking a note" note) string)
  1821. (cons (const :tag "Heading when clocking out" clock-out) string)
  1822. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1823. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1824. (cons (const :tag "Heading when changing deadline" redeadline) string
  1825. (cons (const :tag "Heading when deleting a deadline" deldeadline) string))))
  1826. (unless (assq 'note org-log-note-headings)
  1827. (push '(note . "%t") org-log-note-headings))
  1828. (defcustom org-log-into-drawer nil
  1829. "Non-nil means, insert state change notes and time stamps into a drawer.
  1830. When nil, state changes notes will be inserted after the headline and
  1831. any scheduling and clock lines, but not inside a drawer.
  1832. The value of this variable should be the name of the drawer to use.
  1833. LOGBOOK is proposed at the default drawer for this purpose, you can
  1834. also set this to a string to define the drawer of your choice.
  1835. A value of t is also allowed, representing \"LOGBOOK\".
  1836. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1837. will be ignored.
  1838. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1839. a subtree."
  1840. :group 'org-todo
  1841. :group 'org-progress
  1842. :type '(choice
  1843. (const :tag "Not into a drawer" nil)
  1844. (const :tag "LOGBOOK" t)
  1845. (string :tag "Other")))
  1846. (if (fboundp 'defvaralias)
  1847. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1848. (defun org-log-into-drawer ()
  1849. "Return the value of `org-log-into-drawer', but let properties overrule.
  1850. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1851. used instead of the default value."
  1852. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1853. (cond
  1854. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1855. ((equal p "t") "LOGBOOK")
  1856. (t p))))
  1857. (defcustom org-log-state-notes-insert-after-drawers nil
  1858. "Non-nil means, insert state change notes after any drawers in entry.
  1859. Only the drawers that *immediately* follow the headline and the
  1860. deadline/scheduled line are skipped.
  1861. When nil, insert notes right after the heading and perhaps the line
  1862. with deadline/scheduling if present.
  1863. This variable will have no effect if `org-log-into-drawer' is
  1864. set."
  1865. :group 'org-todo
  1866. :group 'org-progress
  1867. :type 'boolean)
  1868. (defcustom org-log-states-order-reversed t
  1869. "Non-nil means, the latest state change note will be directly after heading.
  1870. When nil, the notes will be orderer according to time."
  1871. :group 'org-todo
  1872. :group 'org-progress
  1873. :type 'boolean)
  1874. (defcustom org-log-repeat 'time
  1875. "Non-nil means, record moving through the DONE state when triggering repeat.
  1876. An auto-repeating task is immediately switched back to TODO when
  1877. marked DONE. If you are not logging state changes (by adding \"@\"
  1878. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1879. record a closing note, there will be no record of the task moving
  1880. through DONE. This variable forces taking a note anyway.
  1881. nil Don't force a record
  1882. time Record a time stamp
  1883. note Record a note
  1884. This option can also be set with on a per-file-basis with
  1885. #+STARTUP: logrepeat
  1886. #+STARTUP: lognoterepeat
  1887. #+STARTUP: nologrepeat
  1888. You can have local logging settings for a subtree by setting the LOGGING
  1889. property to one or more of these keywords."
  1890. :group 'org-todo
  1891. :group 'org-progress
  1892. :type '(choice
  1893. (const :tag "Don't force a record" nil)
  1894. (const :tag "Force recording the DONE state" time)
  1895. (const :tag "Force recording a note with the DONE state" note)))
  1896. (defgroup org-priorities nil
  1897. "Priorities in Org-mode."
  1898. :tag "Org Priorities"
  1899. :group 'org-todo)
  1900. (defcustom org-enable-priority-commands t
  1901. "Non-nil means, priority commands are active.
  1902. When nil, these commands will be disabled, so that you never accidentally
  1903. set a priority."
  1904. :group 'org-priorities
  1905. :type 'boolean)
  1906. (defcustom org-highest-priority ?A
  1907. "The highest priority of TODO items. A character like ?A, ?B etc.
  1908. Must have a smaller ASCII number than `org-lowest-priority'."
  1909. :group 'org-priorities
  1910. :type 'character)
  1911. (defcustom org-lowest-priority ?C
  1912. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1913. Must have a larger ASCII number than `org-highest-priority'."
  1914. :group 'org-priorities
  1915. :type 'character)
  1916. (defcustom org-default-priority ?B
  1917. "The default priority of TODO items.
  1918. This is the priority an item get if no explicit priority is given."
  1919. :group 'org-priorities
  1920. :type 'character)
  1921. (defcustom org-priority-start-cycle-with-default t
  1922. "Non-nil means, start with default priority when starting to cycle.
  1923. When this is nil, the first step in the cycle will be (depending on the
  1924. command used) one higher or lower that the default priority."
  1925. :group 'org-priorities
  1926. :type 'boolean)
  1927. (defgroup org-time nil
  1928. "Options concerning time stamps and deadlines in Org-mode."
  1929. :tag "Org Time"
  1930. :group 'org)
  1931. (defcustom org-insert-labeled-timestamps-at-point nil
  1932. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1933. When nil, these labeled time stamps are forces into the second line of an
  1934. entry, just after the headline. When scheduling from the global TODO list,
  1935. the time stamp will always be forced into the second line."
  1936. :group 'org-time
  1937. :type 'boolean)
  1938. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1939. "Formats for `format-time-string' which are used for time stamps.
  1940. It is not recommended to change this constant.")
  1941. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1942. "Number of minutes to round time stamps to.
  1943. These are two values, the first applies when first creating a time stamp.
  1944. The second applies when changing it with the commands `S-up' and `S-down'.
  1945. When changing the time stamp, this means that it will change in steps
  1946. of N minutes, as given by the second value.
  1947. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1948. numbers should be factors of 60, so for example 5, 10, 15.
  1949. When this is larger than 1, you can still force an exact time-stamp by using
  1950. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1951. and by using a prefix arg to `S-up/down' to specify the exact number
  1952. of minutes to shift."
  1953. :group 'org-time
  1954. :get '(lambda (var) ; Make sure all entries have 5 elements
  1955. (if (integerp (default-value var))
  1956. (list (default-value var) 5)
  1957. (default-value var)))
  1958. :type '(list
  1959. (integer :tag "when inserting times")
  1960. (integer :tag "when modifying times")))
  1961. ;; Normalize old customizations of this variable.
  1962. (when (integerp org-time-stamp-rounding-minutes)
  1963. (setq org-time-stamp-rounding-minutes
  1964. (list org-time-stamp-rounding-minutes
  1965. org-time-stamp-rounding-minutes)))
  1966. (defcustom org-display-custom-times nil
  1967. "Non-nil means, overlay custom formats over all time stamps.
  1968. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1969. To turn this on on a per-file basis, insert anywhere in the file:
  1970. #+STARTUP: customtime"
  1971. :group 'org-time
  1972. :set 'set-default
  1973. :type 'sexp)
  1974. (make-variable-buffer-local 'org-display-custom-times)
  1975. (defcustom org-time-stamp-custom-formats
  1976. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1977. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1978. These are overlayed over the default ISO format if the variable
  1979. `org-display-custom-times' is set. Time like %H:%M should be at the
  1980. end of the second format. The custom formats are also honored by export
  1981. commands, if custom time display is turned on at the time of export."
  1982. :group 'org-time
  1983. :type 'sexp)
  1984. (defun org-time-stamp-format (&optional long inactive)
  1985. "Get the right format for a time string."
  1986. (let ((f (if long (cdr org-time-stamp-formats)
  1987. (car org-time-stamp-formats))))
  1988. (if inactive
  1989. (concat "[" (substring f 1 -1) "]")
  1990. f)))
  1991. (defcustom org-time-clocksum-format "%d:%02d"
  1992. "The format string used when creating CLOCKSUM lines, or when
  1993. org-mode generates a time duration."
  1994. :group 'org-time
  1995. :type 'string)
  1996. (defcustom org-time-clocksum-use-fractional nil
  1997. "If non-nil, \\[org-clock-display] uses fractional times.
  1998. org-mode generates a time duration."
  1999. :group 'org-time
  2000. :type 'boolean)
  2001. (defcustom org-time-clocksum-fractional-format "%.2f"
  2002. "The format string used when creating CLOCKSUM lines, or when
  2003. org-mode generates a time duration."
  2004. :group 'org-time
  2005. :type 'string)
  2006. (defcustom org-deadline-warning-days 14
  2007. "No. of days before expiration during which a deadline becomes active.
  2008. This variable governs the display in sparse trees and in the agenda.
  2009. When 0 or negative, it means use this number (the absolute value of it)
  2010. even if a deadline has a different individual lead time specified.
  2011. Custom commands can set this variable in the options section."
  2012. :group 'org-time
  2013. :group 'org-agenda-daily/weekly
  2014. :type 'integer)
  2015. (defcustom org-read-date-prefer-future t
  2016. "Non-nil means, assume future for incomplete date input from user.
  2017. This affects the following situations:
  2018. 1. The user gives a month but not a year.
  2019. For example, if it is april and you enter \"feb 2\", this will be read
  2020. as feb 2, *next* year. \"May 5\", however, will be this year.
  2021. 2. The user gives a day, but no month.
  2022. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2023. read this as the third of *next* month. However, if you enter \"17\",
  2024. it will be considered as *this* month.
  2025. If you set this variable to the symbol `time', then also the following
  2026. will work:
  2027. 3. If the user gives a time, but no day. If the time is before now,
  2028. to will be interpreted as tomorrow.
  2029. Currently none of this works for ISO week specifications.
  2030. When this option is nil, the current day, month and year will always be
  2031. used as defaults."
  2032. :group 'org-time
  2033. :type '(choice
  2034. (const :tag "Never" nil)
  2035. (const :tag "Check month and day" t)
  2036. (const :tag "Check month, day, and time" time)))
  2037. (defcustom org-read-date-display-live t
  2038. "Non-nil means, display current interpretation of date prompt live.
  2039. This display will be in an overlay, in the minibuffer."
  2040. :group 'org-time
  2041. :type 'boolean)
  2042. (defcustom org-read-date-popup-calendar t
  2043. "Non-nil means, pop up a calendar when prompting for a date.
  2044. In the calendar, the date can be selected with mouse-1. However, the
  2045. minibuffer will also be active, and you can simply enter the date as well.
  2046. When nil, only the minibuffer will be available."
  2047. :group 'org-time
  2048. :type 'boolean)
  2049. (if (fboundp 'defvaralias)
  2050. (defvaralias 'org-popup-calendar-for-date-prompt
  2051. 'org-read-date-popup-calendar))
  2052. (defcustom org-read-date-minibuffer-setup-hook nil
  2053. "Hook to be used to set up keys for the date/time interface.
  2054. Add key definitions to `minibuffer-local-map', which will be a temporary
  2055. copy."
  2056. :group 'org-time
  2057. :type 'hook)
  2058. (defcustom org-extend-today-until 0
  2059. "The hour when your day really ends. Must be an integer.
  2060. This has influence for the following applications:
  2061. - When switching the agenda to \"today\". It it is still earlier than
  2062. the time given here, the day recognized as TODAY is actually yesterday.
  2063. - When a date is read from the user and it is still before the time given
  2064. here, the current date and time will be assumed to be yesterday, 23:59.
  2065. Also, timestamps inserted in remember templates follow this rule.
  2066. IMPORTANT: This is a feature whose implementation is and likely will
  2067. remain incomplete. Really, it is only here because past midnight seems to
  2068. be the favorite working time of John Wiegley :-)"
  2069. :group 'org-time
  2070. :type 'integer)
  2071. (defcustom org-edit-timestamp-down-means-later nil
  2072. "Non-nil means, S-down will increase the time in a time stamp.
  2073. When nil, S-up will increase."
  2074. :group 'org-time
  2075. :type 'boolean)
  2076. (defcustom org-calendar-follow-timestamp-change t
  2077. "Non-nil means, make the calendar window follow timestamp changes.
  2078. When a timestamp is modified and the calendar window is visible, it will be
  2079. moved to the new date."
  2080. :group 'org-time
  2081. :type 'boolean)
  2082. (defgroup org-tags nil
  2083. "Options concerning tags in Org-mode."
  2084. :tag "Org Tags"
  2085. :group 'org)
  2086. (defcustom org-tag-alist nil
  2087. "List of tags allowed in Org-mode files.
  2088. When this list is nil, Org-mode will base TAG input on what is already in the
  2089. buffer.
  2090. The value of this variable is an alist, the car of each entry must be a
  2091. keyword as a string, the cdr may be a character that is used to select
  2092. that tag through the fast-tag-selection interface.
  2093. See the manual for details."
  2094. :group 'org-tags
  2095. :type '(repeat
  2096. (choice
  2097. (cons (string :tag "Tag name")
  2098. (character :tag "Access char"))
  2099. (list :tag "Start radio group"
  2100. (const :startgroup)
  2101. (option (string :tag "Group description")))
  2102. (list :tag "End radio group"
  2103. (const :endgroup)
  2104. (option (string :tag "Group description")))
  2105. (const :tag "New line" (:newline)))))
  2106. (defcustom org-tag-persistent-alist nil
  2107. "List of tags that will always appear in all Org-mode files.
  2108. This is in addition to any in buffer settings or customizations
  2109. of `org-tag-alist'.
  2110. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2111. The value of this variable is an alist, the car of each entry must be a
  2112. keyword as a string, the cdr may be a character that is used to select
  2113. that tag through the fast-tag-selection interface.
  2114. See the manual for details.
  2115. To disable these tags on a per-file basis, insert anywhere in the file:
  2116. #+STARTUP: noptag"
  2117. :group 'org-tags
  2118. :type '(repeat
  2119. (choice
  2120. (cons (string :tag "Tag name")
  2121. (character :tag "Access char"))
  2122. (const :tag "Start radio group" (:startgroup))
  2123. (const :tag "End radio group" (:endgroup))
  2124. (const :tag "New line" (:newline)))))
  2125. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2126. "If non-nil, always offer completion for all tags of all agenda files.
  2127. Instead of customizing this variable directly, you might want to
  2128. set it locally for remember buffers, because there no list of
  2129. tags in that file can be created dynamically (there are none).
  2130. (add-hook 'org-remember-mode-hook
  2131. (lambda ()
  2132. (set (make-local-variable
  2133. 'org-complete-tags-always-offer-all-agenda-tags)
  2134. t)))"
  2135. :group 'org-tags
  2136. :type 'boolean)
  2137. (defvar org-file-tags nil
  2138. "List of tags that can be inherited by all entries in the file.
  2139. The tags will be inherited if the variable `org-use-tag-inheritance'
  2140. says they should be.
  2141. This variable is populated from #+FILETAGS lines.")
  2142. (defcustom org-use-fast-tag-selection 'auto
  2143. "Non-nil means, use fast tag selection scheme.
  2144. This is a special interface to select and deselect tags with single keys.
  2145. When nil, fast selection is never used.
  2146. When the symbol `auto', fast selection is used if and only if selection
  2147. characters for tags have been configured, either through the variable
  2148. `org-tag-alist' or through a #+TAGS line in the buffer.
  2149. When t, fast selection is always used and selection keys are assigned
  2150. automatically if necessary."
  2151. :group 'org-tags
  2152. :type '(choice
  2153. (const :tag "Always" t)
  2154. (const :tag "Never" nil)
  2155. (const :tag "When selection characters are configured" 'auto)))
  2156. (defcustom org-fast-tag-selection-single-key nil
  2157. "Non-nil means, fast tag selection exits after first change.
  2158. When nil, you have to press RET to exit it.
  2159. During fast tag selection, you can toggle this flag with `C-c'.
  2160. This variable can also have the value `expert'. In this case, the window
  2161. displaying the tags menu is not even shown, until you press C-c again."
  2162. :group 'org-tags
  2163. :type '(choice
  2164. (const :tag "No" nil)
  2165. (const :tag "Yes" t)
  2166. (const :tag "Expert" expert)))
  2167. (defvar org-fast-tag-selection-include-todo nil
  2168. "Non-nil means, fast tags selection interface will also offer TODO states.
  2169. This is an undocumented feature, you should not rely on it.")
  2170. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2171. "The column to which tags should be indented in a headline.
  2172. If this number is positive, it specifies the column. If it is negative,
  2173. it means that the tags should be flushright to that column. For example,
  2174. -80 works well for a normal 80 character screen."
  2175. :group 'org-tags
  2176. :type 'integer)
  2177. (defcustom org-auto-align-tags t
  2178. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2179. These operations change the length of a headline and therefore shift
  2180. the tags around. With this options turned on, after each such operation
  2181. the tags are again aligned to `org-tags-column'."
  2182. :group 'org-tags
  2183. :type 'boolean)
  2184. (defcustom org-use-tag-inheritance t
  2185. "Non-nil means, tags in levels apply also for sublevels.
  2186. When nil, only the tags directly given in a specific line apply there.
  2187. This may also be a list of tags that should be inherited, or a regexp that
  2188. matches tags that should be inherited. Additional control is possible
  2189. with the variable `org-tags-exclude-from-inheritance' which gives an
  2190. explicit list of tags to be excluded from inheritance., even if the value of
  2191. `org-use-tag-inheritance' would select it for inheritance.
  2192. If this option is t, a match early-on in a tree can lead to a large
  2193. number of matches in the subtree when constructing the agenda or creating
  2194. a sparse tree. If you only want to see the first match in a tree during
  2195. a search, check out the variable `org-tags-match-list-sublevels'."
  2196. :group 'org-tags
  2197. :type '(choice
  2198. (const :tag "Not" nil)
  2199. (const :tag "Always" t)
  2200. (repeat :tag "Specific tags" (string :tag "Tag"))
  2201. (regexp :tag "Tags matched by regexp")))
  2202. (defcustom org-tags-exclude-from-inheritance nil
  2203. "List of tags that should never be inherited.
  2204. This is a way to exclude a few tags from inheritance. For way to do
  2205. the opposite, to actively allow inheritance for selected tags,
  2206. see the variable `org-use-tag-inheritance'."
  2207. :group 'org-tags
  2208. :type '(repeat (string :tag "Tag")))
  2209. (defun org-tag-inherit-p (tag)
  2210. "Check if TAG is one that should be inherited."
  2211. (cond
  2212. ((member tag org-tags-exclude-from-inheritance) nil)
  2213. ((eq org-use-tag-inheritance t) t)
  2214. ((not org-use-tag-inheritance) nil)
  2215. ((stringp org-use-tag-inheritance)
  2216. (string-match org-use-tag-inheritance tag))
  2217. ((listp org-use-tag-inheritance)
  2218. (member tag org-use-tag-inheritance))
  2219. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2220. (defcustom org-tags-match-list-sublevels t
  2221. "Non-nil means list also sublevels of headlines matching a search.
  2222. This variable applies to tags/property searches, and also to stuck
  2223. projects because this search is based on a tags match as well.
  2224. When set to the symbol `indented', sublevels are indented with
  2225. leading dots.
  2226. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2227. the sublevels of a headline matching a tag search often also match
  2228. the same search. Listing all of them can create very long lists.
  2229. Setting this variable to nil causes subtrees of a match to be skipped.
  2230. This variable is semi-obsolete and probably should always be true. It
  2231. is better to limit inheritance to certain tags using the variables
  2232. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2233. :group 'org-tags
  2234. :type '(choice
  2235. (const :tag "No, don't list them" nil)
  2236. (const :tag "Yes, do list them" t)
  2237. (const :tag "List them, indented with leading dots" indented)))
  2238. (defcustom org-tags-sort-function nil
  2239. "When set, tags are sorted using this function as a comparator"
  2240. :group 'org-tags
  2241. :type '(choice
  2242. (const :tag "No sorting" nil)
  2243. (const :tag "Alphabetical" string<)
  2244. (const :tag "Reverse alphabetical" string>)
  2245. (function :tag "Custom function" nil)))
  2246. (defvar org-tags-history nil
  2247. "History of minibuffer reads for tags.")
  2248. (defvar org-last-tags-completion-table nil
  2249. "The last used completion table for tags.")
  2250. (defvar org-after-tags-change-hook nil
  2251. "Hook that is run after the tags in a line have changed.")
  2252. (defgroup org-properties nil
  2253. "Options concerning properties in Org-mode."
  2254. :tag "Org Properties"
  2255. :group 'org)
  2256. (defcustom org-property-format "%-10s %s"
  2257. "How property key/value pairs should be formatted by `indent-line'.
  2258. When `indent-line' hits a property definition, it will format the line
  2259. according to this format, mainly to make sure that the values are
  2260. lined-up with respect to each other."
  2261. :group 'org-properties
  2262. :type 'string)
  2263. (defcustom org-use-property-inheritance nil
  2264. "Non-nil means, properties apply also for sublevels.
  2265. This setting is chiefly used during property searches. Turning it on can
  2266. cause significant overhead when doing a search, which is why it is not
  2267. on by default.
  2268. When nil, only the properties directly given in the current entry count.
  2269. When t, every property is inherited. The value may also be a list of
  2270. properties that should have inheritance, or a regular expression matching
  2271. properties that should be inherited.
  2272. However, note that some special properties use inheritance under special
  2273. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2274. and the properties ending in \"_ALL\" when they are used as descriptor
  2275. for valid values of a property.
  2276. Note for programmers:
  2277. When querying an entry with `org-entry-get', you can control if inheritance
  2278. should be used. By default, `org-entry-get' looks only at the local
  2279. properties. You can request inheritance by setting the inherit argument
  2280. to t (to force inheritance) or to `selective' (to respect the setting
  2281. in this variable)."
  2282. :group 'org-properties
  2283. :type '(choice
  2284. (const :tag "Not" nil)
  2285. (const :tag "Always" t)
  2286. (repeat :tag "Specific properties" (string :tag "Property"))
  2287. (regexp :tag "Properties matched by regexp")))
  2288. (defun org-property-inherit-p (property)
  2289. "Check if PROPERTY is one that should be inherited."
  2290. (cond
  2291. ((eq org-use-property-inheritance t) t)
  2292. ((not org-use-property-inheritance) nil)
  2293. ((stringp org-use-property-inheritance)
  2294. (string-match org-use-property-inheritance property))
  2295. ((listp org-use-property-inheritance)
  2296. (member property org-use-property-inheritance))
  2297. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2298. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2299. "The default column format, if no other format has been defined.
  2300. This variable can be set on the per-file basis by inserting a line
  2301. #+COLUMNS: %25ITEM ....."
  2302. :group 'org-properties
  2303. :type 'string)
  2304. (defcustom org-columns-ellipses ".."
  2305. "The ellipses to be used when a field in column view is truncated.
  2306. When this is the empty string, as many characters as possible are shown,
  2307. but then there will be no visual indication that the field has been truncated.
  2308. When this is a string of length N, the last N characters of a truncated
  2309. field are replaced by this string. If the column is narrower than the
  2310. ellipses string, only part of the ellipses string will be shown."
  2311. :group 'org-properties
  2312. :type 'string)
  2313. (defcustom org-columns-modify-value-for-display-function nil
  2314. "Function that modifies values for display in column view.
  2315. For example, it can be used to cut out a certain part from a time stamp.
  2316. The function must take 2 arguments:
  2317. column-title The title of the column (*not* the property name)
  2318. value The value that should be modified.
  2319. The function should return the value that should be displayed,
  2320. or nil if the normal value should be used."
  2321. :group 'org-properties
  2322. :type 'function)
  2323. (defcustom org-effort-property "Effort"
  2324. "The property that is being used to keep track of effort estimates.
  2325. Effort estimates given in this property need to have the format H:MM."
  2326. :group 'org-properties
  2327. :group 'org-progress
  2328. :type '(string :tag "Property"))
  2329. (defconst org-global-properties-fixed
  2330. '(("VISIBILITY_ALL" . "folded children content all")
  2331. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2332. "List of property/value pairs that can be inherited by any entry.
  2333. These are fixed values, for the preset properties. The user variable
  2334. that can be used to add to this list is `org-global-properties'.
  2335. The entries in this list are cons cells where the car is a property
  2336. name and cdr is a string with the value. If the value represents
  2337. multiple items like an \"_ALL\" property, separate the items by
  2338. spaces.")
  2339. (defcustom org-global-properties nil
  2340. "List of property/value pairs that can be inherited by any entry.
  2341. This list will be combined with the constant `org-global-properties-fixed'.
  2342. The entries in this list are cons cells where the car is a property
  2343. name and cdr is a string with the value.
  2344. You can set buffer-local values for the same purpose in the variable
  2345. `org-file-properties' this by adding lines like
  2346. #+PROPERTY: NAME VALUE"
  2347. :group 'org-properties
  2348. :type '(repeat
  2349. (cons (string :tag "Property")
  2350. (string :tag "Value"))))
  2351. (defvar org-file-properties nil
  2352. "List of property/value pairs that can be inherited by any entry.
  2353. Valid for the current buffer.
  2354. This variable is populated from #+PROPERTY lines.")
  2355. (make-variable-buffer-local 'org-file-properties)
  2356. (defgroup org-agenda nil
  2357. "Options concerning agenda views in Org-mode."
  2358. :tag "Org Agenda"
  2359. :group 'org)
  2360. (defvar org-category nil
  2361. "Variable used by org files to set a category for agenda display.
  2362. Such files should use a file variable to set it, for example
  2363. # -*- mode: org; org-category: \"ELisp\"
  2364. or contain a special line
  2365. #+CATEGORY: ELisp
  2366. If the file does not specify a category, then file's base name
  2367. is used instead.")
  2368. (make-variable-buffer-local 'org-category)
  2369. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2370. (defcustom org-agenda-files nil
  2371. "The files to be used for agenda display.
  2372. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2373. \\[org-remove-file]. You can also use customize to edit the list.
  2374. If an entry is a directory, all files in that directory that are matched by
  2375. `org-agenda-file-regexp' will be part of the file list.
  2376. If the value of the variable is not a list but a single file name, then
  2377. the list of agenda files is actually stored and maintained in that file, one
  2378. agenda file per line."
  2379. :group 'org-agenda
  2380. :type '(choice
  2381. (repeat :tag "List of files and directories" file)
  2382. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2383. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2384. "Regular expression to match files for `org-agenda-files'.
  2385. If any element in the list in that variable contains a directory instead
  2386. of a normal file, all files in that directory that are matched by this
  2387. regular expression will be included."
  2388. :group 'org-agenda
  2389. :type 'regexp)
  2390. (defcustom org-agenda-text-search-extra-files nil
  2391. "List of extra files to be searched by text search commands.
  2392. These files will be search in addition to the agenda files by the
  2393. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2394. Note that these files will only be searched for text search commands,
  2395. not for the other agenda views like todo lists, tag searches or the weekly
  2396. agenda. This variable is intended to list notes and possibly archive files
  2397. that should also be searched by these two commands.
  2398. In fact, if the first element in the list is the symbol `agenda-archives',
  2399. than all archive files of all agenda files will be added to the search
  2400. scope."
  2401. :group 'org-agenda
  2402. :type '(set :greedy t
  2403. (const :tag "Agenda Archives" agenda-archives)
  2404. (repeat :inline t (file))))
  2405. (if (fboundp 'defvaralias)
  2406. (defvaralias 'org-agenda-multi-occur-extra-files
  2407. 'org-agenda-text-search-extra-files))
  2408. (defcustom org-agenda-skip-unavailable-files nil
  2409. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2410. A nil value means to remove them, after a query, from the list."
  2411. :group 'org-agenda
  2412. :type 'boolean)
  2413. (defcustom org-calendar-to-agenda-key [?c]
  2414. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2415. The command `org-calendar-goto-agenda' will be bound to this key. The
  2416. default is the character `c' because then `c' can be used to switch back and
  2417. forth between agenda and calendar."
  2418. :group 'org-agenda
  2419. :type 'sexp)
  2420. (defcustom org-calendar-agenda-action-key [?k]
  2421. "The key to be installed in `calendar-mode-map' for agenda-action.
  2422. The command `org-agenda-action' will be bound to this key. The
  2423. default is the character `k' because we use the same key in the agenda."
  2424. :group 'org-agenda
  2425. :type 'sexp)
  2426. (defcustom org-calendar-insert-diary-entry-key [?i]
  2427. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2428. This option is irrelevant until `org-agenda-diary-file' has been configured
  2429. to point to an Org-mode file. When that is the case, the command
  2430. `org-agenda-diary-entry' will be bound to the key given here, by default
  2431. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2432. if you want to continue doing this, you need to change this to a different
  2433. key."
  2434. :group 'org-agenda
  2435. :type 'sexp)
  2436. (defcustom org-agenda-diary-file 'diary-file
  2437. "File to which to add new entries with the `i' key in agenda and calendar.
  2438. When this is the symbol `diary-file', the functionality in the Emacs
  2439. calendar will be used to add entries to the `diary-file'. But when this
  2440. points to a file, `org-agenda-diary-entry' will be used instead."
  2441. :group 'org-agenda
  2442. :type '(choice
  2443. (const :tag "The standard Emacs diary file" diary-file)
  2444. (file :tag "Special Org file diary entries")))
  2445. (eval-after-load "calendar"
  2446. '(progn
  2447. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2448. 'org-calendar-goto-agenda)
  2449. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2450. 'org-agenda-action)
  2451. (add-hook 'calendar-mode-hook
  2452. (lambda ()
  2453. (unless (eq org-agenda-diary-file 'diary-file)
  2454. (define-key calendar-mode-map
  2455. org-calendar-insert-diary-entry-key
  2456. 'org-agenda-diary-entry))))))
  2457. (defgroup org-latex nil
  2458. "Options for embedding LaTeX code into Org-mode."
  2459. :tag "Org LaTeX"
  2460. :group 'org)
  2461. (defcustom org-format-latex-options
  2462. '(:foreground default :background default :scale 1.0
  2463. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2464. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2465. "Options for creating images from LaTeX fragments.
  2466. This is a property list with the following properties:
  2467. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2468. `default' means use the foreground of the default face.
  2469. :background the background color, or \"Transparent\".
  2470. `default' means use the background of the default face.
  2471. :scale a scaling factor for the size of the images.
  2472. :html-foreground, :html-background, :html-scale
  2473. the same numbers for HTML export.
  2474. :matchers a list indicating which matchers should be used to
  2475. find LaTeX fragments. Valid members of this list are:
  2476. \"begin\" find environments
  2477. \"$1\" find single characters surrounded by $.$
  2478. \"$\" find math expressions surrounded by $...$
  2479. \"$$\" find math expressions surrounded by $$....$$
  2480. \"\\(\" find math expressions surrounded by \\(...\\)
  2481. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2482. :group 'org-latex
  2483. :type 'plist)
  2484. (defcustom org-format-latex-header "\\documentclass{article}
  2485. \\usepackage{amssymb}
  2486. \\usepackage[usenames]{color}
  2487. \\usepackage{amsmath}
  2488. \\usepackage{latexsym}
  2489. \\usepackage[mathscr]{eucal}
  2490. \\pagestyle{empty} % do not remove
  2491. % The settings below are copied from fullpage.sty
  2492. \\setlength{\\textwidth}{\\paperwidth}
  2493. \\addtolength{\\textwidth}{-3cm}
  2494. \\setlength{\\oddsidemargin}{1.5cm}
  2495. \\addtolength{\\oddsidemargin}{-2.54cm}
  2496. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2497. \\setlength{\\textheight}{\\paperheight}
  2498. \\addtolength{\\textheight}{-\\headheight}
  2499. \\addtolength{\\textheight}{-\\headsep}
  2500. \\addtolength{\\textheight}{-\\footskip}
  2501. \\addtolength{\\textheight}{-3cm}
  2502. \\setlength{\\topmargin}{1.5cm}
  2503. \\addtolength{\\topmargin}{-2.54cm}"
  2504. "The document header used for processing LaTeX fragments.
  2505. It is imperative that this header make sure that no page number
  2506. appears on the page."
  2507. :group 'org-latex
  2508. :type 'string)
  2509. (defvar org-format-latex-header-extra nil)
  2510. ;; The following variable is defined here because is it also used
  2511. ;; when formatting latex fragments. Originally it was part of the
  2512. ;; LaTeX exporter, which is why the name includes "export".
  2513. (defcustom org-export-latex-packages-alist nil
  2514. "Alist of packages to be inserted in the header.
  2515. Each cell is of the format \( \"option\" . \"package\" \)."
  2516. :group 'org-export-latex
  2517. :type '(repeat
  2518. (list
  2519. (string :tag "option")
  2520. (string :tag "package"))))
  2521. (defgroup org-font-lock nil
  2522. "Font-lock settings for highlighting in Org-mode."
  2523. :tag "Org Font Lock"
  2524. :group 'org)
  2525. (defcustom org-level-color-stars-only nil
  2526. "Non-nil means fontify only the stars in each headline.
  2527. When nil, the entire headline is fontified.
  2528. Changing it requires restart of `font-lock-mode' to become effective
  2529. also in regions already fontified."
  2530. :group 'org-font-lock
  2531. :type 'boolean)
  2532. (defcustom org-hide-leading-stars nil
  2533. "Non-nil means, hide the first N-1 stars in a headline.
  2534. This works by using the face `org-hide' for these stars. This
  2535. face is white for a light background, and black for a dark
  2536. background. You may have to customize the face `org-hide' to
  2537. make this work.
  2538. Changing it requires restart of `font-lock-mode' to become effective
  2539. also in regions already fontified.
  2540. You may also set this on a per-file basis by adding one of the following
  2541. lines to the buffer:
  2542. #+STARTUP: hidestars
  2543. #+STARTUP: showstars"
  2544. :group 'org-font-lock
  2545. :type 'boolean)
  2546. (defcustom org-fontify-done-headline nil
  2547. "Non-nil means, change the face of a headline if it is marked DONE.
  2548. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2549. When this is non-nil, the headline after the keyword is set to the
  2550. `org-headline-done' as an additional indication."
  2551. :group 'org-font-lock
  2552. :type 'boolean)
  2553. (defcustom org-fontify-emphasized-text t
  2554. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2555. Changing this variable requires a restart of Emacs to take effect."
  2556. :group 'org-font-lock
  2557. :type 'boolean)
  2558. (defcustom org-fontify-whole-heading-line nil
  2559. "Non-nil means fontify the whole line for headings.
  2560. This is useful when setting a background color for the
  2561. org-level-* faces."
  2562. :group 'org-font-lock
  2563. :type 'boolean)
  2564. (defcustom org-highlight-latex-fragments-and-specials nil
  2565. "Non-nil means, fontify what is treated specially by the exporters."
  2566. :group 'org-font-lock
  2567. :type 'boolean)
  2568. (defcustom org-hide-emphasis-markers nil
  2569. "Non-nil mean font-lock should hide the emphasis marker characters."
  2570. :group 'org-font-lock
  2571. :type 'boolean)
  2572. (defvar org-emph-re nil
  2573. "Regular expression for matching emphasis.")
  2574. (defvar org-verbatim-re nil
  2575. "Regular expression for matching verbatim text.")
  2576. (defvar org-emphasis-regexp-components) ; defined just below
  2577. (defvar org-emphasis-alist) ; defined just below
  2578. (defun org-set-emph-re (var val)
  2579. "Set variable and compute the emphasis regular expression."
  2580. (set var val)
  2581. (when (and (boundp 'org-emphasis-alist)
  2582. (boundp 'org-emphasis-regexp-components)
  2583. org-emphasis-alist org-emphasis-regexp-components)
  2584. (let* ((e org-emphasis-regexp-components)
  2585. (pre (car e))
  2586. (post (nth 1 e))
  2587. (border (nth 2 e))
  2588. (body (nth 3 e))
  2589. (nl (nth 4 e))
  2590. (body1 (concat body "*?"))
  2591. (markers (mapconcat 'car org-emphasis-alist ""))
  2592. (vmarkers (mapconcat
  2593. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2594. org-emphasis-alist "")))
  2595. ;; make sure special characters appear at the right position in the class
  2596. (if (string-match "\\^" markers)
  2597. (setq markers (concat (replace-match "" t t markers) "^")))
  2598. (if (string-match "-" markers)
  2599. (setq markers (concat (replace-match "" t t markers) "-")))
  2600. (if (string-match "\\^" vmarkers)
  2601. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2602. (if (string-match "-" vmarkers)
  2603. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2604. (if (> nl 0)
  2605. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2606. (int-to-string nl) "\\}")))
  2607. ;; Make the regexp
  2608. (setq org-emph-re
  2609. (concat "\\([" pre "]\\|^\\)"
  2610. "\\("
  2611. "\\([" markers "]\\)"
  2612. "\\("
  2613. "[^" border "]\\|"
  2614. "[^" border "]"
  2615. body1
  2616. "[^" border "]"
  2617. "\\)"
  2618. "\\3\\)"
  2619. "\\([" post "]\\|$\\)"))
  2620. (setq org-verbatim-re
  2621. (concat "\\([" pre "]\\|^\\)"
  2622. "\\("
  2623. "\\([" vmarkers "]\\)"
  2624. "\\("
  2625. "[^" border "]\\|"
  2626. "[^" border "]"
  2627. body1
  2628. "[^" border "]"
  2629. "\\)"
  2630. "\\3\\)"
  2631. "\\([" post "]\\|$\\)")))))
  2632. (defcustom org-emphasis-regexp-components
  2633. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2634. "Components used to build the regular expression for emphasis.
  2635. This is a list with 6 entries. Terminology: In an emphasis string
  2636. like \" *strong word* \", we call the initial space PREMATCH, the final
  2637. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2638. and \"trong wor\" is the body. The different components in this variable
  2639. specify what is allowed/forbidden in each part:
  2640. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2641. post Chars allowed as postmatch. End of line will be allowed too.
  2642. border The chars *forbidden* as border characters.
  2643. body-regexp A regexp like \".\" to match a body character. Don't use
  2644. non-shy groups here, and don't allow newline here.
  2645. newline The maximum number of newlines allowed in an emphasis exp.
  2646. Use customize to modify this, or restart Emacs after changing it."
  2647. :group 'org-font-lock
  2648. :set 'org-set-emph-re
  2649. :type '(list
  2650. (sexp :tag "Allowed chars in pre ")
  2651. (sexp :tag "Allowed chars in post ")
  2652. (sexp :tag "Forbidden chars in border ")
  2653. (sexp :tag "Regexp for body ")
  2654. (integer :tag "number of newlines allowed")
  2655. (option (boolean :tag "Please ignore this button"))))
  2656. (defcustom org-emphasis-alist
  2657. `(("*" bold "<b>" "</b>")
  2658. ("/" italic "<i>" "</i>")
  2659. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2660. ("=" org-code "<code>" "</code>" verbatim)
  2661. ("~" org-verbatim "<code>" "</code>" verbatim)
  2662. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2663. "<del>" "</del>")
  2664. )
  2665. "Special syntax for emphasized text.
  2666. Text starting and ending with a special character will be emphasized, for
  2667. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2668. characters, the face to be used by font-lock for highlighting in Org-mode
  2669. Emacs buffers, and the HTML tags to be used for this.
  2670. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2671. Use customize to modify this, or restart Emacs after changing it."
  2672. :group 'org-font-lock
  2673. :set 'org-set-emph-re
  2674. :type '(repeat
  2675. (list
  2676. (string :tag "Marker character")
  2677. (choice
  2678. (face :tag "Font-lock-face")
  2679. (plist :tag "Face property list"))
  2680. (string :tag "HTML start tag")
  2681. (string :tag "HTML end tag")
  2682. (option (const verbatim)))))
  2683. (defvar org-protecting-blocks
  2684. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2685. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2686. This is needed for font-lock setup.")
  2687. ;;; Miscellaneous options
  2688. (defgroup org-completion nil
  2689. "Completion in Org-mode."
  2690. :tag "Org Completion"
  2691. :group 'org)
  2692. (defcustom org-completion-use-ido nil
  2693. "Non-nil means, use ido completion wherever possible.
  2694. Note that `ido-mode' must be active for this variable to be relevant.
  2695. If you decide to turn this variable on, you might well want to turn off
  2696. `org-outline-path-complete-in-steps'.
  2697. See also `org-completion-use-iswitchb'."
  2698. :group 'org-completion
  2699. :type 'boolean)
  2700. (defcustom org-completion-use-iswitchb nil
  2701. "Non-nil means, use iswitchb completion wherever possible.
  2702. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2703. If you decide to turn this variable on, you might well want to turn off
  2704. `org-outline-path-complete-in-steps'.
  2705. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2706. :group 'org-completion
  2707. :type 'boolean)
  2708. (defcustom org-completion-fallback-command 'hippie-expand
  2709. "The expansion command called by \\[org-complete] in normal context.
  2710. Normal means, no org-mode-specific context."
  2711. :group 'org-completion
  2712. :type 'function)
  2713. ;;; Functions and variables from their packages
  2714. ;; Declared here to avoid compiler warnings
  2715. ;; XEmacs only
  2716. (defvar outline-mode-menu-heading)
  2717. (defvar outline-mode-menu-show)
  2718. (defvar outline-mode-menu-hide)
  2719. (defvar zmacs-regions) ; XEmacs regions
  2720. ;; Emacs only
  2721. (defvar mark-active)
  2722. ;; Various packages
  2723. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2724. (declare-function calendar-forward-day "cal-move" (arg))
  2725. (declare-function calendar-goto-date "cal-move" (date))
  2726. (declare-function calendar-goto-today "cal-move" ())
  2727. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2728. (defvar calc-embedded-close-formula)
  2729. (defvar calc-embedded-open-formula)
  2730. (declare-function cdlatex-tab "ext:cdlatex" ())
  2731. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2732. (defvar font-lock-unfontify-region-function)
  2733. (declare-function iswitchb-read-buffer "iswitchb"
  2734. (prompt &optional default require-match start matches-set))
  2735. (defvar iswitchb-temp-buflist)
  2736. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2737. (defvar org-agenda-tags-todo-honor-ignore-options)
  2738. (declare-function org-agenda-skip "org-agenda" ())
  2739. (declare-function
  2740. org-format-agenda-item "org-agenda"
  2741. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2742. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2743. (declare-function org-agenda-change-all-lines "org-agenda"
  2744. (newhead hdmarker &optional fixface just-this))
  2745. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2746. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2747. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2748. (beg end))
  2749. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2750. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2751. "org-agenda" (&optional end))
  2752. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2753. (declare-function org-indent-mode "org-indent" (&optional arg))
  2754. (declare-function parse-time-string "parse-time" (string))
  2755. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2756. (defvar remember-data-file)
  2757. (defvar texmathp-why)
  2758. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2759. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2760. (defvar w3m-current-url)
  2761. (defvar w3m-current-title)
  2762. (defvar org-latex-regexps)
  2763. ;;; Autoload and prepare some org modules
  2764. ;; Some table stuff that needs to be defined here, because it is used
  2765. ;; by the functions setting up org-mode or checking for table context.
  2766. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2767. "Detects an org-type or table-type table.")
  2768. (defconst org-table-line-regexp "^[ \t]*|"
  2769. "Detects an org-type table line.")
  2770. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2771. "Detects an org-type table line.")
  2772. (defconst org-table-hline-regexp "^[ \t]*|-"
  2773. "Detects an org-type table hline.")
  2774. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2775. "Detects a table-type table hline.")
  2776. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2777. "Searching from within a table (any type) this finds the first line
  2778. outside the table.")
  2779. ;; Autoload the functions in org-table.el that are needed by functions here.
  2780. (eval-and-compile
  2781. (org-autoload "org-table"
  2782. '(org-table-align org-table-begin org-table-blank-field
  2783. org-table-convert org-table-convert-region org-table-copy-down
  2784. org-table-copy-region org-table-create
  2785. org-table-create-or-convert-from-region
  2786. org-table-create-with-table.el org-table-current-dline
  2787. org-table-cut-region org-table-delete-column org-table-edit-field
  2788. org-table-edit-formulas org-table-end org-table-eval-formula
  2789. org-table-export org-table-field-info
  2790. org-table-get-stored-formulas org-table-goto-column
  2791. org-table-hline-and-move org-table-import org-table-insert-column
  2792. org-table-insert-hline org-table-insert-row org-table-iterate
  2793. org-table-justify-field-maybe org-table-kill-row
  2794. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2795. org-table-move-column org-table-move-column-left
  2796. org-table-move-column-right org-table-move-row
  2797. org-table-move-row-down org-table-move-row-up
  2798. org-table-next-field org-table-next-row org-table-paste-rectangle
  2799. org-table-previous-field org-table-recalculate
  2800. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2801. org-table-toggle-coordinate-overlays
  2802. org-table-toggle-formula-debugger org-table-wrap-region
  2803. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2804. (defun org-at-table-p (&optional table-type)
  2805. "Return t if the cursor is inside an org-type table.
  2806. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2807. (if org-enable-table-editor
  2808. (save-excursion
  2809. (beginning-of-line 1)
  2810. (looking-at (if table-type org-table-any-line-regexp
  2811. org-table-line-regexp)))
  2812. nil))
  2813. (defsubst org-table-p () (org-at-table-p))
  2814. (defun org-at-table.el-p ()
  2815. "Return t if and only if we are at a table.el table."
  2816. (and (org-at-table-p 'any)
  2817. (save-excursion
  2818. (goto-char (org-table-begin 'any))
  2819. (looking-at org-table1-hline-regexp))))
  2820. (defun org-table-recognize-table.el ()
  2821. "If there is a table.el table nearby, recognize it and move into it."
  2822. (if org-table-tab-recognizes-table.el
  2823. (if (org-at-table.el-p)
  2824. (progn
  2825. (beginning-of-line 1)
  2826. (if (looking-at org-table-dataline-regexp)
  2827. nil
  2828. (if (looking-at org-table1-hline-regexp)
  2829. (progn
  2830. (beginning-of-line 2)
  2831. (if (looking-at org-table-any-border-regexp)
  2832. (beginning-of-line -1)))))
  2833. (if (re-search-forward "|" (org-table-end t) t)
  2834. (progn
  2835. (require 'table)
  2836. (if (table--at-cell-p (point))
  2837. t
  2838. (message "recognizing table.el table...")
  2839. (table-recognize-table)
  2840. (message "recognizing table.el table...done")))
  2841. (error "This should not happen..."))
  2842. t)
  2843. nil)
  2844. nil))
  2845. (defun org-at-table-hline-p ()
  2846. "Return t if the cursor is inside a hline in a table."
  2847. (if org-enable-table-editor
  2848. (save-excursion
  2849. (beginning-of-line 1)
  2850. (looking-at org-table-hline-regexp))
  2851. nil))
  2852. (defvar org-table-clean-did-remove-column nil)
  2853. (defun org-table-map-tables (function)
  2854. "Apply FUNCTION to the start of all tables in the buffer."
  2855. (save-excursion
  2856. (save-restriction
  2857. (widen)
  2858. (goto-char (point-min))
  2859. (while (re-search-forward org-table-any-line-regexp nil t)
  2860. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2861. (beginning-of-line 1)
  2862. (when (looking-at org-table-line-regexp)
  2863. (save-excursion (funcall function))
  2864. (or (looking-at org-table-line-regexp)
  2865. (forward-char 1)))
  2866. (re-search-forward org-table-any-border-regexp nil 1))))
  2867. (message "Mapping tables: done"))
  2868. ;; Declare and autoload functions from org-exp.el & Co
  2869. (declare-function org-default-export-plist "org-exp")
  2870. (declare-function org-infile-export-plist "org-exp")
  2871. (declare-function org-get-current-options "org-exp")
  2872. (eval-and-compile
  2873. (org-autoload "org-exp"
  2874. '(org-export org-export-visible
  2875. org-insert-export-options-template
  2876. org-table-clean-before-export))
  2877. (org-autoload "org-ascii"
  2878. '(org-export-as-ascii org-export-ascii-preprocess
  2879. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2880. org-export-region-as-ascii))
  2881. (org-autoload "org-latex"
  2882. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  2883. org-replace-region-by-latex org-export-region-as-latex
  2884. org-export-as-latex org-export-as-pdf
  2885. org-export-as-pdf-and-open))
  2886. (org-autoload "org-html"
  2887. '(org-export-as-html-and-open
  2888. org-export-as-html-batch org-export-as-html-to-buffer
  2889. org-replace-region-by-html org-export-region-as-html
  2890. org-export-as-html))
  2891. (org-autoload "org-docbook"
  2892. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  2893. org-replace-region-by-docbook org-export-region-as-docbook
  2894. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  2895. org-export-as-docbook))
  2896. (org-autoload "org-icalendar"
  2897. '(org-export-icalendar-this-file
  2898. org-export-icalendar-all-agenda-files
  2899. org-export-icalendar-combine-agenda-files))
  2900. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  2901. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  2902. ;; Declare and autoload functions from org-agenda.el
  2903. (eval-and-compile
  2904. (org-autoload "org-agenda"
  2905. '(org-agenda org-agenda-list org-search-view
  2906. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2907. org-diary org-agenda-to-appt
  2908. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2909. ;; Autoload org-remember
  2910. (eval-and-compile
  2911. (org-autoload "org-remember"
  2912. '(org-remember-insinuate org-remember-annotation
  2913. org-remember-apply-template org-remember org-remember-handler)))
  2914. ;; Autoload org-clock.el
  2915. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2916. (beg end))
  2917. (declare-function org-clock-update-mode-line "org-clock" ())
  2918. (declare-function org-resolve-clocks "org-clock"
  2919. (&optional also-non-dangling-p prompt last-valid))
  2920. (defvar org-clock-start-time)
  2921. (defvar org-clock-marker (make-marker)
  2922. "Marker recording the last clock-in.")
  2923. (defvar org-clock-hd-marker (make-marker)
  2924. "Marker recording the last clock-in, but the headline position.")
  2925. (defvar org-clock-heading ""
  2926. "The heading of the current clock entry.")
  2927. (defun org-clock-is-active ()
  2928. "Return non-nil if clock is currently running.
  2929. The return value is actually the clock marker."
  2930. (marker-buffer org-clock-marker))
  2931. (eval-and-compile
  2932. (org-autoload
  2933. "org-clock"
  2934. '(org-clock-in org-clock-out org-clock-cancel
  2935. org-clock-goto org-clock-sum org-clock-display
  2936. org-clock-remove-overlays org-clock-report
  2937. org-clocktable-shift org-dblock-write:clocktable
  2938. org-get-clocktable org-resolve-clocks)))
  2939. (defun org-clock-update-time-maybe ()
  2940. "If this is a CLOCK line, update it and return t.
  2941. Otherwise, return nil."
  2942. (interactive)
  2943. (save-excursion
  2944. (beginning-of-line 1)
  2945. (skip-chars-forward " \t")
  2946. (when (looking-at org-clock-string)
  2947. (let ((re (concat "[ \t]*" org-clock-string
  2948. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2949. "\\([ \t]*=>.*\\)?\\)?"))
  2950. ts te h m s neg)
  2951. (cond
  2952. ((not (looking-at re))
  2953. nil)
  2954. ((not (match-end 2))
  2955. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2956. (> org-clock-marker (point))
  2957. (<= org-clock-marker (point-at-eol)))
  2958. ;; The clock is running here
  2959. (setq org-clock-start-time
  2960. (apply 'encode-time
  2961. (org-parse-time-string (match-string 1))))
  2962. (org-clock-update-mode-line)))
  2963. (t
  2964. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2965. (end-of-line 1)
  2966. (setq ts (match-string 1)
  2967. te (match-string 3))
  2968. (setq s (- (org-float-time
  2969. (apply 'encode-time (org-parse-time-string te)))
  2970. (org-float-time
  2971. (apply 'encode-time (org-parse-time-string ts))))
  2972. neg (< s 0)
  2973. s (abs s)
  2974. h (floor (/ s 3600))
  2975. s (- s (* 3600 h))
  2976. m (floor (/ s 60))
  2977. s (- s (* 60 s)))
  2978. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2979. t))))))
  2980. (defun org-check-running-clock ()
  2981. "Check if the current buffer contains the running clock.
  2982. If yes, offer to stop it and to save the buffer with the changes."
  2983. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2984. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2985. (buffer-name))))
  2986. (org-clock-out)
  2987. (when (y-or-n-p "Save changed buffer?")
  2988. (save-buffer))))
  2989. (defun org-clocktable-try-shift (dir n)
  2990. "Check if this line starts a clock table, if yes, shift the time block."
  2991. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2992. (org-clocktable-shift dir n)))
  2993. ;; Autoload org-timer.el
  2994. (eval-and-compile
  2995. (org-autoload
  2996. "org-timer"
  2997. '(org-timer-start org-timer org-timer-item
  2998. org-timer-change-times-in-region
  2999. org-timer-set-timer
  3000. org-timer-reset-timers
  3001. org-timer-show-remaining-time)))
  3002. ;; Autoload org-feed.el
  3003. (eval-and-compile
  3004. (org-autoload
  3005. "org-feed"
  3006. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3007. ;; Autoload org-indent.el
  3008. ;; Define the variable already here, to make sure we have it.
  3009. (defvar org-indent-mode nil
  3010. "Non-nil if Org-Indent mode is enabled.
  3011. Use the command `org-indent-mode' to change this variable.")
  3012. (eval-and-compile
  3013. (org-autoload
  3014. "org-indent"
  3015. '(org-indent-mode)))
  3016. ;; Autoload org-mobile.el
  3017. (eval-and-compile
  3018. (org-autoload
  3019. "org-mobile"
  3020. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3021. ;; Autoload archiving code
  3022. ;; The stuff that is needed for cycling and tags has to be defined here.
  3023. (defgroup org-archive nil
  3024. "Options concerning archiving in Org-mode."
  3025. :tag "Org Archive"
  3026. :group 'org-structure)
  3027. (defcustom org-archive-location "%s_archive::"
  3028. "The location where subtrees should be archived.
  3029. The value of this variable is a string, consisting of two parts,
  3030. separated by a double-colon. The first part is a filename and
  3031. the second part is a headline.
  3032. When the filename is omitted, archiving happens in the same file.
  3033. %s in the filename will be replaced by the current file
  3034. name (without the directory part). Archiving to a different file
  3035. is useful to keep archived entries from contributing to the
  3036. Org-mode Agenda.
  3037. The archived entries will be filed as subtrees of the specified
  3038. headline. When the headline is omitted, the subtrees are simply
  3039. filed away at the end of the file, as top-level entries. Also in
  3040. the heading you can use %s to represent the file name, this can be
  3041. useful when using the same archive for a number of different files.
  3042. Here are a few examples:
  3043. \"%s_archive::\"
  3044. If the current file is Projects.org, archive in file
  3045. Projects.org_archive, as top-level trees. This is the default.
  3046. \"::* Archived Tasks\"
  3047. Archive in the current file, under the top-level headline
  3048. \"* Archived Tasks\".
  3049. \"~/org/archive.org::\"
  3050. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3051. \"~/org/archive.org::From %s\"
  3052. Archive in file ~/org/archive.org (absolute path), under headlines
  3053. \"From FILENAME\" where file name is the current file name.
  3054. \"basement::** Finished Tasks\"
  3055. Archive in file ./basement (relative path), as level 3 trees
  3056. below the level 2 heading \"** Finished Tasks\".
  3057. You may set this option on a per-file basis by adding to the buffer a
  3058. line like
  3059. #+ARCHIVE: basement::** Finished Tasks
  3060. You may also define it locally for a subtree by setting an ARCHIVE property
  3061. in the entry. If such a property is found in an entry, or anywhere up
  3062. the hierarchy, it will be used."
  3063. :group 'org-archive
  3064. :type 'string)
  3065. (defcustom org-archive-tag "ARCHIVE"
  3066. "The tag that marks a subtree as archived.
  3067. An archived subtree does not open during visibility cycling, and does
  3068. not contribute to the agenda listings.
  3069. After changing this, font-lock must be restarted in the relevant buffers to
  3070. get the proper fontification."
  3071. :group 'org-archive
  3072. :group 'org-keywords
  3073. :type 'string)
  3074. (defcustom org-agenda-skip-archived-trees t
  3075. "Non-nil means, the agenda will skip any items located in archived trees.
  3076. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3077. variable is no longer recommended, you should leave it at the value t.
  3078. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3079. :group 'org-archive
  3080. :group 'org-agenda-skip
  3081. :type 'boolean)
  3082. (defcustom org-columns-skip-archived-trees t
  3083. "Non-nil means, ignore archived trees when creating column view."
  3084. :group 'org-archive
  3085. :group 'org-properties
  3086. :type 'boolean)
  3087. (defcustom org-cycle-open-archived-trees nil
  3088. "Non-nil means, `org-cycle' will open archived trees.
  3089. An archived tree is a tree marked with the tag ARCHIVE.
  3090. When nil, archived trees will stay folded. You can still open them with
  3091. normal outline commands like `show-all', but not with the cycling commands."
  3092. :group 'org-archive
  3093. :group 'org-cycle
  3094. :type 'boolean)
  3095. (defcustom org-sparse-tree-open-archived-trees nil
  3096. "Non-nil means sparse tree construction shows matches in archived trees.
  3097. When nil, matches in these trees are highlighted, but the trees are kept in
  3098. collapsed state."
  3099. :group 'org-archive
  3100. :group 'org-sparse-trees
  3101. :type 'boolean)
  3102. (defun org-cycle-hide-archived-subtrees (state)
  3103. "Re-hide all archived subtrees after a visibility state change."
  3104. (when (and (not org-cycle-open-archived-trees)
  3105. (not (memq state '(overview folded))))
  3106. (save-excursion
  3107. (let* ((globalp (memq state '(contents all)))
  3108. (beg (if globalp (point-min) (point)))
  3109. (end (if globalp (point-max) (org-end-of-subtree t))))
  3110. (org-hide-archived-subtrees beg end)
  3111. (goto-char beg)
  3112. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3113. (message "%s" (substitute-command-keys
  3114. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3115. (defun org-force-cycle-archived ()
  3116. "Cycle subtree even if it is archived."
  3117. (interactive)
  3118. (setq this-command 'org-cycle)
  3119. (let ((org-cycle-open-archived-trees t))
  3120. (call-interactively 'org-cycle)))
  3121. (defun org-hide-archived-subtrees (beg end)
  3122. "Re-hide all archived subtrees after a visibility state change."
  3123. (save-excursion
  3124. (let* ((re (concat ":" org-archive-tag ":")))
  3125. (goto-char beg)
  3126. (while (re-search-forward re end t)
  3127. (and (org-on-heading-p) (org-flag-subtree t))
  3128. (org-end-of-subtree t)))))
  3129. (defun org-flag-subtree (flag)
  3130. (save-excursion
  3131. (org-back-to-heading t)
  3132. (outline-end-of-heading)
  3133. (outline-flag-region (point)
  3134. (progn (org-end-of-subtree t) (point))
  3135. flag)))
  3136. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3137. (eval-and-compile
  3138. (org-autoload "org-archive"
  3139. '(org-add-archive-files org-archive-subtree
  3140. org-archive-to-archive-sibling org-toggle-archive-tag
  3141. org-archive-subtree-default
  3142. org-archive-subtree-default-with-confirmation)))
  3143. ;; Autoload Column View Code
  3144. (declare-function org-columns-number-to-string "org-colview")
  3145. (declare-function org-columns-get-format-and-top-level "org-colview")
  3146. (declare-function org-columns-compute "org-colview")
  3147. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3148. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3149. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3150. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3151. ;; Autoload ID code
  3152. (declare-function org-id-store-link "org-id")
  3153. (declare-function org-id-locations-load "org-id")
  3154. (declare-function org-id-locations-save "org-id")
  3155. (defvar org-id-track-globally)
  3156. (org-autoload "org-id"
  3157. '(org-id-get-create org-id-new org-id-copy org-id-get
  3158. org-id-get-with-outline-path-completion
  3159. org-id-get-with-outline-drilling
  3160. org-id-goto org-id-find org-id-store-link))
  3161. ;; Autoload Plotting Code
  3162. (org-autoload "org-plot"
  3163. '(org-plot/gnuplot))
  3164. ;;; Variables for pre-computed regular expressions, all buffer local
  3165. (defvar org-drawer-regexp nil
  3166. "Matches first line of a hidden block.")
  3167. (make-variable-buffer-local 'org-drawer-regexp)
  3168. (defvar org-todo-regexp nil
  3169. "Matches any of the TODO state keywords.")
  3170. (make-variable-buffer-local 'org-todo-regexp)
  3171. (defvar org-not-done-regexp nil
  3172. "Matches any of the TODO state keywords except the last one.")
  3173. (make-variable-buffer-local 'org-not-done-regexp)
  3174. (defvar org-not-done-heading-regexp nil
  3175. "Matches a TODO headline that is not done.")
  3176. (make-variable-buffer-local 'org-not-done-regexp)
  3177. (defvar org-todo-line-regexp nil
  3178. "Matches a headline and puts TODO state into group 2 if present.")
  3179. (make-variable-buffer-local 'org-todo-line-regexp)
  3180. (defvar org-complex-heading-regexp nil
  3181. "Matches a headline and puts everything into groups:
  3182. group 1: the stars
  3183. group 2: The todo keyword, maybe
  3184. group 3: Priority cookie
  3185. group 4: True headline
  3186. group 5: Tags")
  3187. (make-variable-buffer-local 'org-complex-heading-regexp)
  3188. (defvar org-complex-heading-regexp-format nil)
  3189. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3190. (defvar org-todo-line-tags-regexp nil
  3191. "Matches a headline and puts TODO state into group 2 if present.
  3192. Also put tags into group 4 if tags are present.")
  3193. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3194. (defvar org-nl-done-regexp nil
  3195. "Matches newline followed by a headline with the DONE keyword.")
  3196. (make-variable-buffer-local 'org-nl-done-regexp)
  3197. (defvar org-looking-at-done-regexp nil
  3198. "Matches the DONE keyword a point.")
  3199. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3200. (defvar org-ds-keyword-length 12
  3201. "Maximum length of the Deadline and SCHEDULED keywords.")
  3202. (make-variable-buffer-local 'org-ds-keyword-length)
  3203. (defvar org-deadline-regexp nil
  3204. "Matches the DEADLINE keyword.")
  3205. (make-variable-buffer-local 'org-deadline-regexp)
  3206. (defvar org-deadline-time-regexp nil
  3207. "Matches the DEADLINE keyword together with a time stamp.")
  3208. (make-variable-buffer-local 'org-deadline-time-regexp)
  3209. (defvar org-deadline-line-regexp nil
  3210. "Matches the DEADLINE keyword and the rest of the line.")
  3211. (make-variable-buffer-local 'org-deadline-line-regexp)
  3212. (defvar org-scheduled-regexp nil
  3213. "Matches the SCHEDULED keyword.")
  3214. (make-variable-buffer-local 'org-scheduled-regexp)
  3215. (defvar org-scheduled-time-regexp nil
  3216. "Matches the SCHEDULED keyword together with a time stamp.")
  3217. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3218. (defvar org-closed-time-regexp nil
  3219. "Matches the CLOSED keyword together with a time stamp.")
  3220. (make-variable-buffer-local 'org-closed-time-regexp)
  3221. (defvar org-keyword-time-regexp nil
  3222. "Matches any of the 4 keywords, together with the time stamp.")
  3223. (make-variable-buffer-local 'org-keyword-time-regexp)
  3224. (defvar org-keyword-time-not-clock-regexp nil
  3225. "Matches any of the 3 keywords, together with the time stamp.")
  3226. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3227. (defvar org-maybe-keyword-time-regexp nil
  3228. "Matches a timestamp, possibly preceeded by a keyword.")
  3229. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3230. (defvar org-planning-or-clock-line-re nil
  3231. "Matches a line with planning or clock info.")
  3232. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3233. (defvar org-all-time-keywords nil
  3234. "List of time keywords.")
  3235. (make-variable-buffer-local 'org-all-time-keywords)
  3236. (defconst org-plain-time-of-day-regexp
  3237. (concat
  3238. "\\(\\<[012]?[0-9]"
  3239. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3240. "\\(--?"
  3241. "\\(\\<[012]?[0-9]"
  3242. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3243. "\\)?")
  3244. "Regular expression to match a plain time or time range.
  3245. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3246. groups carry important information:
  3247. 0 the full match
  3248. 1 the first time, range or not
  3249. 8 the second time, if it is a range.")
  3250. (defconst org-plain-time-extension-regexp
  3251. (concat
  3252. "\\(\\<[012]?[0-9]"
  3253. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3254. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3255. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3256. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3257. groups carry important information:
  3258. 0 the full match
  3259. 7 hours of duration
  3260. 9 minutes of duration")
  3261. (defconst org-stamp-time-of-day-regexp
  3262. (concat
  3263. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3264. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3265. "\\(--?"
  3266. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3267. "Regular expression to match a timestamp time or time range.
  3268. After a match, the following groups carry important information:
  3269. 0 the full match
  3270. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3271. 2 the first time, range or not
  3272. 4 the second time, if it is a range.")
  3273. (defconst org-startup-options
  3274. '(("fold" org-startup-folded t)
  3275. ("overview" org-startup-folded t)
  3276. ("nofold" org-startup-folded nil)
  3277. ("showall" org-startup-folded nil)
  3278. ("showeverything" org-startup-folded showeverything)
  3279. ("content" org-startup-folded content)
  3280. ("indent" org-startup-indented t)
  3281. ("noindent" org-startup-indented nil)
  3282. ("hidestars" org-hide-leading-stars t)
  3283. ("showstars" org-hide-leading-stars nil)
  3284. ("odd" org-odd-levels-only t)
  3285. ("oddeven" org-odd-levels-only nil)
  3286. ("align" org-startup-align-all-tables t)
  3287. ("noalign" org-startup-align-all-tables nil)
  3288. ("customtime" org-display-custom-times t)
  3289. ("logdone" org-log-done time)
  3290. ("lognotedone" org-log-done note)
  3291. ("nologdone" org-log-done nil)
  3292. ("lognoteclock-out" org-log-note-clock-out t)
  3293. ("nolognoteclock-out" org-log-note-clock-out nil)
  3294. ("logrepeat" org-log-repeat state)
  3295. ("lognoterepeat" org-log-repeat note)
  3296. ("nologrepeat" org-log-repeat nil)
  3297. ("logreschedule" org-log-reschedule time)
  3298. ("lognotereschedule" org-log-reschedule note)
  3299. ("nologreschedule" org-log-reschedule nil)
  3300. ("logredeadline" org-log-redeadline time)
  3301. ("lognoteredeadline" org-log-redeadline note)
  3302. ("nologredeadline" org-log-redeadline nil)
  3303. ("fninline" org-footnote-define-inline t)
  3304. ("nofninline" org-footnote-define-inline nil)
  3305. ("fnlocal" org-footnote-section nil)
  3306. ("fnauto" org-footnote-auto-label t)
  3307. ("fnprompt" org-footnote-auto-label nil)
  3308. ("fnconfirm" org-footnote-auto-label confirm)
  3309. ("fnplain" org-footnote-auto-label plain)
  3310. ("fnadjust" org-footnote-auto-adjust t)
  3311. ("nofnadjust" org-footnote-auto-adjust nil)
  3312. ("constcgs" constants-unit-system cgs)
  3313. ("constSI" constants-unit-system SI)
  3314. ("noptag" org-tag-persistent-alist nil)
  3315. ("hideblocks" org-hide-block-startup t)
  3316. ("nohideblocks" org-hide-block-startup nil)
  3317. ("beamer" org-startup-with-beamer-mode t))
  3318. "Variable associated with STARTUP options for org-mode.
  3319. Each element is a list of three items: The startup options as written
  3320. in the #+STARTUP line, the corresponding variable, and the value to
  3321. set this variable to if the option is found. An optional forth element PUSH
  3322. means to push this value onto the list in the variable.")
  3323. (defun org-set-regexps-and-options ()
  3324. "Precompute regular expressions for current buffer."
  3325. (when (org-mode-p)
  3326. (org-set-local 'org-todo-kwd-alist nil)
  3327. (org-set-local 'org-todo-key-alist nil)
  3328. (org-set-local 'org-todo-key-trigger nil)
  3329. (org-set-local 'org-todo-keywords-1 nil)
  3330. (org-set-local 'org-done-keywords nil)
  3331. (org-set-local 'org-todo-heads nil)
  3332. (org-set-local 'org-todo-sets nil)
  3333. (org-set-local 'org-todo-log-states nil)
  3334. (org-set-local 'org-file-properties nil)
  3335. (org-set-local 'org-file-tags nil)
  3336. (let ((re (org-make-options-regexp
  3337. '("CATEGORY" "TODO" "COLUMNS"
  3338. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3339. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3340. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3341. (splitre "[ \t]+")
  3342. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3343. tail sep kws1 prio props ftags drawers beamer-p
  3344. ext-setup-or-nil setup-contents (start 0))
  3345. (save-excursion
  3346. (save-restriction
  3347. (widen)
  3348. (goto-char (point-min))
  3349. (while (or (and ext-setup-or-nil
  3350. (string-match re ext-setup-or-nil start)
  3351. (setq start (match-end 0)))
  3352. (and (setq ext-setup-or-nil nil start 0)
  3353. (re-search-forward re nil t)))
  3354. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3355. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3356. (cond
  3357. ((equal key "CATEGORY")
  3358. (if (string-match "[ \t]+$" value)
  3359. (setq value (replace-match "" t t value)))
  3360. (setq cat value))
  3361. ((member key '("SEQ_TODO" "TODO"))
  3362. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3363. ((equal key "TYP_TODO")
  3364. (push (cons 'type (org-split-string value splitre)) kwds))
  3365. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3366. ;; general TODO-like setup
  3367. (push (cons (intern (downcase (match-string 1 key)))
  3368. (org-split-string value splitre)) kwds))
  3369. ((equal key "TAGS")
  3370. (setq tags (append tags (if tags '("\\n") nil)
  3371. (org-split-string value splitre))))
  3372. ((equal key "COLUMNS")
  3373. (org-set-local 'org-columns-default-format value))
  3374. ((equal key "LINK")
  3375. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3376. (push (cons (match-string 1 value)
  3377. (org-trim (match-string 2 value)))
  3378. links)))
  3379. ((equal key "PRIORITIES")
  3380. (setq prio (org-split-string value " +")))
  3381. ((equal key "PROPERTY")
  3382. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3383. (push (cons (match-string 1 value) (match-string 2 value))
  3384. props)))
  3385. ((equal key "FILETAGS")
  3386. (when (string-match "\\S-" value)
  3387. (setq ftags
  3388. (append
  3389. ftags
  3390. (apply 'append
  3391. (mapcar (lambda (x) (org-split-string x ":"))
  3392. (org-split-string value)))))))
  3393. ((equal key "DRAWERS")
  3394. (setq drawers (org-split-string value splitre)))
  3395. ((equal key "CONSTANTS")
  3396. (setq const (append const (org-split-string value splitre))))
  3397. ((equal key "STARTUP")
  3398. (let ((opts (org-split-string value splitre))
  3399. l var val)
  3400. (while (setq l (pop opts))
  3401. (when (setq l (assoc l org-startup-options))
  3402. (setq var (nth 1 l) val (nth 2 l))
  3403. (if (not (nth 3 l))
  3404. (set (make-local-variable var) val)
  3405. (if (not (listp (symbol-value var)))
  3406. (set (make-local-variable var) nil))
  3407. (set (make-local-variable var) (symbol-value var))
  3408. (add-to-list var val))))))
  3409. ((equal key "ARCHIVE")
  3410. (string-match " *$" value)
  3411. (setq arch (replace-match "" t t value))
  3412. (remove-text-properties 0 (length arch)
  3413. '(face t fontified t) arch))
  3414. ((equal key "LATEX_CLASS")
  3415. (setq beamer-p (equal value "beamer")))
  3416. ((equal key "SETUPFILE")
  3417. (setq setup-contents (org-file-contents
  3418. (expand-file-name
  3419. (org-remove-double-quotes value))
  3420. 'noerror))
  3421. (if (not ext-setup-or-nil)
  3422. (setq ext-setup-or-nil setup-contents start 0)
  3423. (setq ext-setup-or-nil
  3424. (concat (substring ext-setup-or-nil 0 start)
  3425. "\n" setup-contents "\n"
  3426. (substring ext-setup-or-nil start)))))
  3427. ))))
  3428. (when cat
  3429. (org-set-local 'org-category (intern cat))
  3430. (push (cons "CATEGORY" cat) props))
  3431. (when prio
  3432. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3433. (setq prio (mapcar 'string-to-char prio))
  3434. (org-set-local 'org-highest-priority (nth 0 prio))
  3435. (org-set-local 'org-lowest-priority (nth 1 prio))
  3436. (org-set-local 'org-default-priority (nth 2 prio)))
  3437. (and props (org-set-local 'org-file-properties (nreverse props)))
  3438. (and ftags (org-set-local 'org-file-tags
  3439. (mapcar 'org-add-prop-inherited ftags)))
  3440. (and drawers (org-set-local 'org-drawers drawers))
  3441. (and arch (org-set-local 'org-archive-location arch))
  3442. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3443. ;; Process the TODO keywords
  3444. (unless kwds
  3445. ;; Use the global values as if they had been given locally.
  3446. (setq kwds (default-value 'org-todo-keywords))
  3447. (if (stringp (car kwds))
  3448. (setq kwds (list (cons org-todo-interpretation
  3449. (default-value 'org-todo-keywords)))))
  3450. (setq kwds (reverse kwds)))
  3451. (setq kwds (nreverse kwds))
  3452. (let (inter kws kw)
  3453. (while (setq kws (pop kwds))
  3454. (let ((kws (or
  3455. (run-hook-with-args-until-success
  3456. 'org-todo-setup-filter-hook kws)
  3457. kws)))
  3458. (setq inter (pop kws) sep (member "|" kws)
  3459. kws0 (delete "|" (copy-sequence kws))
  3460. kwsa nil
  3461. kws1 (mapcar
  3462. (lambda (x)
  3463. ;; 1 2
  3464. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3465. (progn
  3466. (setq kw (match-string 1 x)
  3467. key (and (match-end 2) (match-string 2 x))
  3468. log (org-extract-log-state-settings x))
  3469. (push (cons kw (and key (string-to-char key))) kwsa)
  3470. (and log (push log org-todo-log-states))
  3471. kw)
  3472. (error "Invalid TODO keyword %s" x)))
  3473. kws0)
  3474. kwsa (if kwsa (append '((:startgroup))
  3475. (nreverse kwsa)
  3476. '((:endgroup))))
  3477. hw (car kws1)
  3478. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3479. tail (list inter hw (car dws) (org-last dws))))
  3480. (add-to-list 'org-todo-heads hw 'append)
  3481. (push kws1 org-todo-sets)
  3482. (setq org-done-keywords (append org-done-keywords dws nil))
  3483. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3484. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3485. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3486. (setq org-todo-sets (nreverse org-todo-sets)
  3487. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3488. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3489. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3490. ;; Process the constants
  3491. (when const
  3492. (let (e cst)
  3493. (while (setq e (pop const))
  3494. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3495. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3496. (setq org-table-formula-constants-local cst)))
  3497. ;; Process the tags.
  3498. (when tags
  3499. (let (e tgs)
  3500. (while (setq e (pop tags))
  3501. (cond
  3502. ((equal e "{") (push '(:startgroup) tgs))
  3503. ((equal e "}") (push '(:endgroup) tgs))
  3504. ((equal e "\\n") (push '(:newline) tgs))
  3505. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3506. (push (cons (match-string 1 e)
  3507. (string-to-char (match-string 2 e)))
  3508. tgs))
  3509. (t (push (list e) tgs))))
  3510. (org-set-local 'org-tag-alist nil)
  3511. (while (setq e (pop tgs))
  3512. (or (and (stringp (car e))
  3513. (assoc (car e) org-tag-alist))
  3514. (push e org-tag-alist)))))
  3515. ;; Compute the regular expressions and other local variables
  3516. (if (not org-done-keywords)
  3517. (setq org-done-keywords (and org-todo-keywords-1
  3518. (list (org-last org-todo-keywords-1)))))
  3519. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3520. (length org-scheduled-string)
  3521. (length org-clock-string)
  3522. (length org-closed-string)))
  3523. org-drawer-regexp
  3524. (concat "^[ \t]*:\\("
  3525. (mapconcat 'regexp-quote org-drawers "\\|")
  3526. "\\):[ \t]*$")
  3527. org-not-done-keywords
  3528. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3529. org-todo-regexp
  3530. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3531. "\\|") "\\)\\>")
  3532. org-not-done-regexp
  3533. (concat "\\<\\("
  3534. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3535. "\\)\\>")
  3536. org-not-done-heading-regexp
  3537. (concat "^\\(\\*+\\)[ \t]+\\("
  3538. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3539. "\\)\\>")
  3540. org-todo-line-regexp
  3541. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3542. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3543. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3544. org-complex-heading-regexp
  3545. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3546. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3547. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3548. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3549. org-complex-heading-regexp-format
  3550. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3551. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3552. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3553. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3554. org-nl-done-regexp
  3555. (concat "\n\\*+[ \t]+"
  3556. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3557. "\\)" "\\>")
  3558. org-todo-line-tags-regexp
  3559. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3560. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3561. (org-re
  3562. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3563. org-looking-at-done-regexp
  3564. (concat "^" "\\(?:"
  3565. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3566. "\\>")
  3567. org-deadline-regexp (concat "\\<" org-deadline-string)
  3568. org-deadline-time-regexp
  3569. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3570. org-deadline-line-regexp
  3571. (concat "\\<\\(" org-deadline-string "\\).*")
  3572. org-scheduled-regexp
  3573. (concat "\\<" org-scheduled-string)
  3574. org-scheduled-time-regexp
  3575. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3576. org-closed-time-regexp
  3577. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3578. org-keyword-time-regexp
  3579. (concat "\\<\\(" org-scheduled-string
  3580. "\\|" org-deadline-string
  3581. "\\|" org-closed-string
  3582. "\\|" org-clock-string "\\)"
  3583. " *[[<]\\([^]>]+\\)[]>]")
  3584. org-keyword-time-not-clock-regexp
  3585. (concat "\\<\\(" org-scheduled-string
  3586. "\\|" org-deadline-string
  3587. "\\|" org-closed-string
  3588. "\\)"
  3589. " *[[<]\\([^]>]+\\)[]>]")
  3590. org-maybe-keyword-time-regexp
  3591. (concat "\\(\\<\\(" org-scheduled-string
  3592. "\\|" org-deadline-string
  3593. "\\|" org-closed-string
  3594. "\\|" org-clock-string "\\)\\)?"
  3595. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3596. org-planning-or-clock-line-re
  3597. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3598. "\\|" org-deadline-string
  3599. "\\|" org-closed-string "\\|" org-clock-string
  3600. "\\)\\>\\)")
  3601. org-all-time-keywords
  3602. (mapcar (lambda (w) (substring w 0 -1))
  3603. (list org-scheduled-string org-deadline-string
  3604. org-clock-string org-closed-string))
  3605. )
  3606. (org-compute-latex-and-specials-regexp)
  3607. (org-set-font-lock-defaults))))
  3608. (defun org-file-contents (file &optional noerror)
  3609. "Return the contents of FILE, as a string."
  3610. (if (or (not file)
  3611. (not (file-readable-p file)))
  3612. (if noerror
  3613. (progn
  3614. (message "Cannot read file %s" file)
  3615. (ding) (sit-for 2)
  3616. "")
  3617. (error "Cannot read file %s" file))
  3618. (with-temp-buffer
  3619. (insert-file-contents file)
  3620. (buffer-string))))
  3621. (defun org-extract-log-state-settings (x)
  3622. "Extract the log state setting from a TODO keyword string.
  3623. This will extract info from a string like \"WAIT(w@/!)\"."
  3624. (let (kw key log1 log2)
  3625. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3626. (setq kw (match-string 1 x)
  3627. key (and (match-end 2) (match-string 2 x))
  3628. log1 (and (match-end 3) (match-string 3 x))
  3629. log2 (and (match-end 4) (match-string 4 x)))
  3630. (and (or log1 log2)
  3631. (list kw
  3632. (and log1 (if (equal log1 "!") 'time 'note))
  3633. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3634. (defun org-remove-keyword-keys (list)
  3635. "Remove a pair of parenthesis at the end of each string in LIST."
  3636. (mapcar (lambda (x)
  3637. (if (string-match "(.*)$" x)
  3638. (substring x 0 (match-beginning 0))
  3639. x))
  3640. list))
  3641. ;; FIXME: this could be done much better, using second characters etc.
  3642. (defun org-assign-fast-keys (alist)
  3643. "Assign fast keys to a keyword-key alist.
  3644. Respect keys that are already there."
  3645. (let (new e k c c1 c2 (char ?a))
  3646. (while (setq e (pop alist))
  3647. (cond
  3648. ((equal e '(:startgroup)) (push e new))
  3649. ((equal e '(:endgroup)) (push e new))
  3650. ((equal e '(:newline)) (push e new))
  3651. (t
  3652. (setq k (car e) c2 nil)
  3653. (if (cdr e)
  3654. (setq c (cdr e))
  3655. ;; automatically assign a character.
  3656. (setq c1 (string-to-char
  3657. (downcase (substring
  3658. k (if (= (string-to-char k) ?@) 1 0)))))
  3659. (if (or (rassoc c1 new) (rassoc c1 alist))
  3660. (while (or (rassoc char new) (rassoc char alist))
  3661. (setq char (1+ char)))
  3662. (setq c2 c1))
  3663. (setq c (or c2 char)))
  3664. (push (cons k c) new))))
  3665. (nreverse new)))
  3666. ;;; Some variables used in various places
  3667. (defvar org-window-configuration nil
  3668. "Used in various places to store a window configuration.")
  3669. (defvar org-selected-window nil
  3670. "Used in various places to store a window configuration.")
  3671. (defvar org-finish-function nil
  3672. "Function to be called when `C-c C-c' is used.
  3673. This is for getting out of special buffers like remember.")
  3674. ;; FIXME: Occasionally check by commenting these, to make sure
  3675. ;; no other functions uses these, forgetting to let-bind them.
  3676. (defvar entry)
  3677. (defvar last-state)
  3678. (defvar date)
  3679. ;; Defined somewhere in this file, but used before definition.
  3680. (defvar org-html-entities)
  3681. (defvar org-struct-menu)
  3682. (defvar org-org-menu)
  3683. (defvar org-tbl-menu)
  3684. ;;;; Define the Org-mode
  3685. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3686. (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."))
  3687. ;; We use a before-change function to check if a table might need
  3688. ;; an update.
  3689. (defvar org-table-may-need-update t
  3690. "Indicates that a table might need an update.
  3691. This variable is set by `org-before-change-function'.
  3692. `org-table-align' sets it back to nil.")
  3693. (defun org-before-change-function (beg end)
  3694. "Every change indicates that a table might need an update."
  3695. (setq org-table-may-need-update t))
  3696. (defvar org-mode-map)
  3697. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3698. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3699. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3700. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3701. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3702. (defvar org-table-buffer-is-an nil)
  3703. (defconst org-outline-regexp "\\*+ ")
  3704. ;;;###autoload
  3705. (define-derived-mode org-mode outline-mode "Org"
  3706. "Outline-based notes management and organizer, alias
  3707. \"Carsten's outline-mode for keeping track of everything.\"
  3708. Org-mode develops organizational tasks around a NOTES file which
  3709. contains information about projects as plain text. Org-mode is
  3710. implemented on top of outline-mode, which is ideal to keep the content
  3711. of large files well structured. It supports ToDo items, deadlines and
  3712. time stamps, which magically appear in the diary listing of the Emacs
  3713. calendar. Tables are easily created with a built-in table editor.
  3714. Plain text URL-like links connect to websites, emails (VM), Usenet
  3715. messages (Gnus), BBDB entries, and any files related to the project.
  3716. For printing and sharing of notes, an Org-mode file (or a part of it)
  3717. can be exported as a structured ASCII or HTML file.
  3718. The following commands are available:
  3719. \\{org-mode-map}"
  3720. ;; Get rid of Outline menus, they are not needed
  3721. ;; Need to do this here because define-derived-mode sets up
  3722. ;; the keymap so late. Still, it is a waste to call this each time
  3723. ;; we switch another buffer into org-mode.
  3724. (if (featurep 'xemacs)
  3725. (when (boundp 'outline-mode-menu-heading)
  3726. ;; Assume this is Greg's port, it used easymenu
  3727. (easy-menu-remove outline-mode-menu-heading)
  3728. (easy-menu-remove outline-mode-menu-show)
  3729. (easy-menu-remove outline-mode-menu-hide))
  3730. (define-key org-mode-map [menu-bar headings] 'undefined)
  3731. (define-key org-mode-map [menu-bar hide] 'undefined)
  3732. (define-key org-mode-map [menu-bar show] 'undefined))
  3733. (org-load-modules-maybe)
  3734. (easy-menu-add org-org-menu)
  3735. (easy-menu-add org-tbl-menu)
  3736. (org-install-agenda-files-menu)
  3737. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3738. (org-add-to-invisibility-spec '(org-cwidth))
  3739. (org-add-to-invisibility-spec '(org-hide-block . t))
  3740. (when (featurep 'xemacs)
  3741. (org-set-local 'line-move-ignore-invisible t))
  3742. (org-set-local 'outline-regexp org-outline-regexp)
  3743. (org-set-local 'outline-level 'org-outline-level)
  3744. (when (and org-ellipsis
  3745. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3746. (fboundp 'make-glyph-code))
  3747. (unless org-display-table
  3748. (setq org-display-table (make-display-table)))
  3749. (set-display-table-slot
  3750. org-display-table 4
  3751. (vconcat (mapcar
  3752. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3753. org-ellipsis)))
  3754. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3755. (setq buffer-display-table org-display-table))
  3756. (org-set-regexps-and-options)
  3757. (when (and org-tag-faces (not org-tags-special-faces-re))
  3758. ;; tag faces set outside customize.... force initialization.
  3759. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3760. ;; Calc embedded
  3761. (org-set-local 'calc-embedded-open-mode "# ")
  3762. (modify-syntax-entry ?# "<")
  3763. (modify-syntax-entry ?@ "w")
  3764. (if org-startup-truncated (setq truncate-lines t))
  3765. (org-set-local 'font-lock-unfontify-region-function
  3766. 'org-unfontify-region)
  3767. ;; Activate before-change-function
  3768. (org-set-local 'org-table-may-need-update t)
  3769. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3770. 'local)
  3771. ;; Check for running clock before killing a buffer
  3772. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3773. ;; Paragraphs and auto-filling
  3774. (org-set-autofill-regexps)
  3775. (setq indent-line-function 'org-indent-line-function)
  3776. (org-update-radio-target-regexp)
  3777. ;; Make sure dependence stuff works reliably, even for users who set it
  3778. ;; too late :-(
  3779. (if org-enforce-todo-dependencies
  3780. (add-hook 'org-blocker-hook
  3781. 'org-block-todo-from-children-or-siblings-or-parent)
  3782. (remove-hook 'org-blocker-hook
  3783. 'org-block-todo-from-children-or-siblings-or-parent))
  3784. (if org-enforce-todo-checkbox-dependencies
  3785. (add-hook 'org-blocker-hook
  3786. 'org-block-todo-from-checkboxes)
  3787. (remove-hook 'org-blocker-hook
  3788. 'org-block-todo-from-checkboxes))
  3789. ;; Comment characters
  3790. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3791. (org-set-local 'comment-padding " ")
  3792. ;; Align options lines
  3793. (org-set-local
  3794. 'align-mode-rules-list
  3795. '((org-in-buffer-settings
  3796. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3797. (modes . '(org-mode)))))
  3798. ;; Imenu
  3799. (org-set-local 'imenu-create-index-function
  3800. 'org-imenu-get-tree)
  3801. ;; Make isearch reveal context
  3802. (if (or (featurep 'xemacs)
  3803. (not (boundp 'outline-isearch-open-invisible-function)))
  3804. ;; Emacs 21 and XEmacs make use of the hook
  3805. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3806. ;; Emacs 22 deals with this through a special variable
  3807. (org-set-local 'outline-isearch-open-invisible-function
  3808. (lambda (&rest ignore) (org-show-context 'isearch))))
  3809. ;; Turn on org-beamer-mode?
  3810. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3811. ;; If empty file that did not turn on org-mode automatically, make it to.
  3812. (if (and org-insert-mode-line-in-empty-file
  3813. (interactive-p)
  3814. (= (point-min) (point-max)))
  3815. (insert "# -*- mode: org -*-\n\n"))
  3816. (unless org-inhibit-startup
  3817. (when org-startup-align-all-tables
  3818. (let ((bmp (buffer-modified-p)))
  3819. (org-table-map-tables 'org-table-align)
  3820. (set-buffer-modified-p bmp)))
  3821. (when org-startup-indented
  3822. (require 'org-indent)
  3823. (org-indent-mode 1))
  3824. (unless org-inhibit-startup-visibility-stuff
  3825. (org-set-startup-visibility))))
  3826. (when (fboundp 'abbrev-table-put)
  3827. (abbrev-table-put org-mode-abbrev-table
  3828. :parents (list text-mode-abbrev-table)))
  3829. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3830. (defun org-current-time ()
  3831. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3832. (if (> (car org-time-stamp-rounding-minutes) 1)
  3833. (let ((r (car org-time-stamp-rounding-minutes))
  3834. (time (decode-time)))
  3835. (apply 'encode-time
  3836. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3837. (nthcdr 2 time))))
  3838. (current-time)))
  3839. ;;;; Font-Lock stuff, including the activators
  3840. (defvar org-mouse-map (make-sparse-keymap))
  3841. (org-defkey org-mouse-map
  3842. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3843. (org-defkey org-mouse-map
  3844. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3845. (when org-mouse-1-follows-link
  3846. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3847. (when org-tab-follows-link
  3848. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3849. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3850. (require 'font-lock)
  3851. (defconst org-non-link-chars "]\t\n\r<>")
  3852. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3853. "shell" "elisp"))
  3854. (defvar org-link-types-re nil
  3855. "Matches a link that has a url-like prefix like \"http:\"")
  3856. (defvar org-link-re-with-space nil
  3857. "Matches a link with spaces, optional angular brackets around it.")
  3858. (defvar org-link-re-with-space2 nil
  3859. "Matches a link with spaces, optional angular brackets around it.")
  3860. (defvar org-link-re-with-space3 nil
  3861. "Matches a link with spaces, only for internal part in bracket links.")
  3862. (defvar org-angle-link-re nil
  3863. "Matches link with angular brackets, spaces are allowed.")
  3864. (defvar org-plain-link-re nil
  3865. "Matches plain link, without spaces.")
  3866. (defvar org-bracket-link-regexp nil
  3867. "Matches a link in double brackets.")
  3868. (defvar org-bracket-link-analytic-regexp nil
  3869. "Regular expression used to analyze links.
  3870. Here is what the match groups contain after a match:
  3871. 1: http:
  3872. 2: http
  3873. 3: path
  3874. 4: [desc]
  3875. 5: desc")
  3876. (defvar org-bracket-link-analytic-regexp++ nil
  3877. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3878. (defvar org-any-link-re nil
  3879. "Regular expression matching any link.")
  3880. (defun org-make-link-regexps ()
  3881. "Update the link regular expressions.
  3882. This should be called after the variable `org-link-types' has changed."
  3883. (setq org-link-types-re
  3884. (concat
  3885. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  3886. org-link-re-with-space
  3887. (concat
  3888. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3889. "\\([^" org-non-link-chars " ]"
  3890. "[^" org-non-link-chars "]*"
  3891. "[^" org-non-link-chars " ]\\)>?")
  3892. org-link-re-with-space2
  3893. (concat
  3894. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3895. "\\([^" org-non-link-chars " ]"
  3896. "[^\t\n\r]*"
  3897. "[^" org-non-link-chars " ]\\)>?")
  3898. org-link-re-with-space3
  3899. (concat
  3900. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3901. "\\([^" org-non-link-chars " ]"
  3902. "[^\t\n\r]*\\)")
  3903. org-angle-link-re
  3904. (concat
  3905. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3906. "\\([^" org-non-link-chars " ]"
  3907. "[^" org-non-link-chars "]*"
  3908. "\\)>")
  3909. org-plain-link-re
  3910. (concat
  3911. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3912. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  3913. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3914. org-bracket-link-regexp
  3915. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3916. org-bracket-link-analytic-regexp
  3917. (concat
  3918. "\\[\\["
  3919. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  3920. "\\([^]]+\\)"
  3921. "\\]"
  3922. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3923. "\\]")
  3924. org-bracket-link-analytic-regexp++
  3925. (concat
  3926. "\\[\\["
  3927. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3928. "\\([^]]+\\)"
  3929. "\\]"
  3930. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3931. "\\]")
  3932. org-any-link-re
  3933. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3934. org-angle-link-re "\\)\\|\\("
  3935. org-plain-link-re "\\)")))
  3936. (org-make-link-regexps)
  3937. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3938. "Regular expression for fast time stamp matching.")
  3939. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3940. "Regular expression for fast time stamp matching.")
  3941. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3942. "Regular expression matching time strings for analysis.
  3943. This one does not require the space after the date, so it can be used
  3944. on a string that terminates immediately after the date.")
  3945. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3946. "Regular expression matching time strings for analysis.")
  3947. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3948. "Regular expression matching time stamps, with groups.")
  3949. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3950. "Regular expression matching time stamps (also [..]), with groups.")
  3951. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3952. "Regular expression matching a time stamp range.")
  3953. (defconst org-tr-regexp-both
  3954. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3955. "Regular expression matching a time stamp range.")
  3956. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3957. org-ts-regexp "\\)?")
  3958. "Regular expression matching a time stamp or time stamp range.")
  3959. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3960. org-ts-regexp-both "\\)?")
  3961. "Regular expression matching a time stamp or time stamp range.
  3962. The time stamps may be either active or inactive.")
  3963. (defvar org-emph-face nil)
  3964. (defun org-do-emphasis-faces (limit)
  3965. "Run through the buffer and add overlays to links."
  3966. (let (rtn a)
  3967. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3968. (if (not (= (char-after (match-beginning 3))
  3969. (char-after (match-beginning 4))))
  3970. (progn
  3971. (setq rtn t)
  3972. (setq a (assoc (match-string 3) org-emphasis-alist))
  3973. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3974. 'face
  3975. (nth 1 a))
  3976. (and (nth 4 a)
  3977. (org-remove-flyspell-overlays-in
  3978. (match-beginning 0) (match-end 0)))
  3979. (add-text-properties (match-beginning 2) (match-end 2)
  3980. '(font-lock-multiline t))
  3981. (when org-hide-emphasis-markers
  3982. (add-text-properties (match-end 4) (match-beginning 5)
  3983. '(invisible org-link))
  3984. (add-text-properties (match-beginning 3) (match-end 3)
  3985. '(invisible org-link)))))
  3986. (backward-char 1))
  3987. rtn))
  3988. (defun org-emphasize (&optional char)
  3989. "Insert or change an emphasis, i.e. a font like bold or italic.
  3990. If there is an active region, change that region to a new emphasis.
  3991. If there is no region, just insert the marker characters and position
  3992. the cursor between them.
  3993. CHAR should be either the marker character, or the first character of the
  3994. HTML tag associated with that emphasis. If CHAR is a space, the means
  3995. to remove the emphasis of the selected region.
  3996. If char is not given (for example in an interactive call) it
  3997. will be prompted for."
  3998. (interactive)
  3999. (let ((eal org-emphasis-alist) e det
  4000. (erc org-emphasis-regexp-components)
  4001. (prompt "")
  4002. (string "") beg end move tag c s)
  4003. (if (org-region-active-p)
  4004. (setq beg (region-beginning) end (region-end)
  4005. string (buffer-substring beg end))
  4006. (setq move t))
  4007. (while (setq e (pop eal))
  4008. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4009. c (aref tag 0))
  4010. (push (cons c (string-to-char (car e))) det)
  4011. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4012. (substring tag 1)))))
  4013. (setq det (nreverse det))
  4014. (unless char
  4015. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4016. (setq char (read-char-exclusive)))
  4017. (setq char (or (cdr (assoc char det)) char))
  4018. (if (equal char ?\ )
  4019. (setq s "" move nil)
  4020. (unless (assoc (char-to-string char) org-emphasis-alist)
  4021. (error "No such emphasis marker: \"%c\"" char))
  4022. (setq s (char-to-string char)))
  4023. (while (and (> (length string) 1)
  4024. (equal (substring string 0 1) (substring string -1))
  4025. (assoc (substring string 0 1) org-emphasis-alist))
  4026. (setq string (substring string 1 -1)))
  4027. (setq string (concat s string s))
  4028. (if beg (delete-region beg end))
  4029. (unless (or (bolp)
  4030. (string-match (concat "[" (nth 0 erc) "\n]")
  4031. (char-to-string (char-before (point)))))
  4032. (insert " "))
  4033. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4034. (char-to-string (char-after (point))))
  4035. (insert " ") (backward-char 1))
  4036. (insert string)
  4037. (and move (backward-char 1))))
  4038. (defconst org-nonsticky-props
  4039. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4040. (defsubst org-rear-nonsticky-at (pos)
  4041. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4042. (defun org-activate-plain-links (limit)
  4043. "Run through the buffer and add overlays to links."
  4044. (catch 'exit
  4045. (let (f)
  4046. (if (re-search-forward org-plain-link-re limit t)
  4047. (progn
  4048. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4049. (setq f (get-text-property (match-beginning 0) 'face))
  4050. (if (or (eq f 'org-tag)
  4051. (and (listp f) (memq 'org-tag f)))
  4052. nil
  4053. (add-text-properties (match-beginning 0) (match-end 0)
  4054. (list 'mouse-face 'highlight
  4055. 'face 'org-link
  4056. 'keymap org-mouse-map))
  4057. (org-rear-nonsticky-at (match-end 0)))
  4058. t)))))
  4059. (defun org-activate-code (limit)
  4060. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4061. (progn
  4062. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4063. (remove-text-properties (match-beginning 0) (match-end 0)
  4064. '(display t invisible t intangible t))
  4065. t)))
  4066. (defun org-fontify-meta-lines-and-blocks (limit)
  4067. "Fontify #+ lines and blocks, in the correct ways."
  4068. (let ((case-fold-search t))
  4069. (if (re-search-forward
  4070. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4071. limit t)
  4072. (let ((beg (match-beginning 0))
  4073. (beg1 (line-beginning-position 2))
  4074. (dc1 (downcase (match-string 2)))
  4075. (dc3 (downcase (match-string 3)))
  4076. end end1 quoting block-type)
  4077. (cond
  4078. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4079. ;; a single line of backend-specific content
  4080. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4081. (remove-text-properties (match-beginning 0) (match-end 0)
  4082. '(display t invisible t intangible t))
  4083. (add-text-properties (match-beginning 1) (match-end 3)
  4084. '(font-lock-fontified t face org-meta-line))
  4085. (add-text-properties (match-beginning 6) (match-end 6)
  4086. '(font-lock-fontified t face org-block))
  4087. t)
  4088. ((and (match-end 4) (equal dc3 "begin"))
  4089. ;; Truely a block
  4090. (setq block-type (downcase (match-string 5))
  4091. quoting (member block-type org-protecting-blocks))
  4092. (when (re-search-forward
  4093. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4094. nil t) ;; on purpose, we look further than LIMIT
  4095. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4096. (when quoting
  4097. (remove-text-properties beg end
  4098. '(display t invisible t intangible t)))
  4099. (add-text-properties
  4100. beg end
  4101. '(font-lock-fontified t font-lock-multiline t))
  4102. (add-text-properties beg beg1 '(face org-meta-line))
  4103. (add-text-properties end1 end '(face org-meta-line))
  4104. (cond
  4105. (quoting
  4106. (add-text-properties beg1 end1 '(face org-block)))
  4107. ((string= block-type "quote")
  4108. (add-text-properties beg1 end1 '(face org-quote)))
  4109. ((string= block-type "verse")
  4110. (add-text-properties beg1 end1 '(face org-verse))))
  4111. t))
  4112. ((not (member (char-after beg) '(?\ ?\t)))
  4113. ;; just any other in-buffer setting, but not indented
  4114. (add-text-properties
  4115. beg (match-end 0)
  4116. '(font-lock-fontified t face org-meta-line))
  4117. t)
  4118. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4119. "orgtbl:" "tblfm:" "tblname:"))
  4120. (and (match-end 4) (equal dc3 "attr")))
  4121. (add-text-properties
  4122. beg (match-end 0)
  4123. '(font-lock-fontified t face org-meta-line))
  4124. t)
  4125. ((member dc3 '(" " ""))
  4126. (add-text-properties
  4127. beg (match-end 0)
  4128. '(font-lock-fontified t face font-lock-comment-face)))
  4129. (t nil))))))
  4130. (defun org-activate-angle-links (limit)
  4131. "Run through the buffer and add overlays to links."
  4132. (if (re-search-forward org-angle-link-re limit t)
  4133. (progn
  4134. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4135. (add-text-properties (match-beginning 0) (match-end 0)
  4136. (list 'mouse-face 'highlight
  4137. 'keymap org-mouse-map))
  4138. (org-rear-nonsticky-at (match-end 0))
  4139. t)))
  4140. (defun org-activate-footnote-links (limit)
  4141. "Run through the buffer and add overlays to links."
  4142. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4143. limit t)
  4144. (progn
  4145. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4146. (add-text-properties (match-beginning 2) (match-end 2)
  4147. (list 'mouse-face 'highlight
  4148. 'keymap org-mouse-map
  4149. 'help-echo
  4150. (if (= (point-at-bol) (match-beginning 2))
  4151. "Footnote definition"
  4152. "Footnote reference")
  4153. ))
  4154. (org-rear-nonsticky-at (match-end 2))
  4155. t)))
  4156. (defun org-activate-bracket-links (limit)
  4157. "Run through the buffer and add overlays to bracketed links."
  4158. (if (re-search-forward org-bracket-link-regexp limit t)
  4159. (let* ((help (concat "LINK: "
  4160. (org-match-string-no-properties 1)))
  4161. ;; FIXME: above we should remove the escapes.
  4162. ;; but that requires another match, protecting match data,
  4163. ;; a lot of overhead for font-lock.
  4164. (ip (org-maybe-intangible
  4165. (list 'invisible 'org-link
  4166. 'keymap org-mouse-map 'mouse-face 'highlight
  4167. 'font-lock-multiline t 'help-echo help)))
  4168. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4169. 'font-lock-multiline t 'help-echo help)))
  4170. ;; We need to remove the invisible property here. Table narrowing
  4171. ;; may have made some of this invisible.
  4172. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4173. (remove-text-properties (match-beginning 0) (match-end 0)
  4174. '(invisible nil))
  4175. (if (match-end 3)
  4176. (progn
  4177. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4178. (org-rear-nonsticky-at (match-beginning 3))
  4179. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4180. (org-rear-nonsticky-at (match-end 3))
  4181. (add-text-properties (match-end 3) (match-end 0) ip)
  4182. (org-rear-nonsticky-at (match-end 0)))
  4183. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4184. (org-rear-nonsticky-at (match-beginning 1))
  4185. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4186. (org-rear-nonsticky-at (match-end 1))
  4187. (add-text-properties (match-end 1) (match-end 0) ip)
  4188. (org-rear-nonsticky-at (match-end 0)))
  4189. t)))
  4190. (defun org-activate-dates (limit)
  4191. "Run through the buffer and add overlays to dates."
  4192. (if (re-search-forward org-tsr-regexp-both limit t)
  4193. (progn
  4194. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4195. (add-text-properties (match-beginning 0) (match-end 0)
  4196. (list 'mouse-face 'highlight
  4197. 'keymap org-mouse-map))
  4198. (org-rear-nonsticky-at (match-end 0))
  4199. (when org-display-custom-times
  4200. (if (match-end 3)
  4201. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4202. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4203. t)))
  4204. (defvar org-target-link-regexp nil
  4205. "Regular expression matching radio targets in plain text.")
  4206. (make-variable-buffer-local 'org-target-link-regexp)
  4207. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4208. "Regular expression matching a link target.")
  4209. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4210. "Regular expression matching a radio target.")
  4211. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4212. "Regular expression matching any target.")
  4213. (defun org-activate-target-links (limit)
  4214. "Run through the buffer and add overlays to target matches."
  4215. (when org-target-link-regexp
  4216. (let ((case-fold-search t))
  4217. (if (re-search-forward org-target-link-regexp limit t)
  4218. (progn
  4219. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4220. (add-text-properties (match-beginning 0) (match-end 0)
  4221. (list 'mouse-face 'highlight
  4222. 'keymap org-mouse-map
  4223. 'help-echo "Radio target link"
  4224. 'org-linked-text t))
  4225. (org-rear-nonsticky-at (match-end 0))
  4226. t)))))
  4227. (defun org-update-radio-target-regexp ()
  4228. "Find all radio targets in this file and update the regular expression."
  4229. (interactive)
  4230. (when (memq 'radio org-activate-links)
  4231. (setq org-target-link-regexp
  4232. (org-make-target-link-regexp (org-all-targets 'radio)))
  4233. (org-restart-font-lock)))
  4234. (defun org-hide-wide-columns (limit)
  4235. (let (s e)
  4236. (setq s (text-property-any (point) (or limit (point-max))
  4237. 'org-cwidth t))
  4238. (when s
  4239. (setq e (next-single-property-change s 'org-cwidth))
  4240. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4241. (goto-char e)
  4242. t)))
  4243. (defvar org-latex-and-specials-regexp nil
  4244. "Regular expression for highlighting export special stuff.")
  4245. (defvar org-match-substring-regexp)
  4246. (defvar org-match-substring-with-braces-regexp)
  4247. ;; This should be with the exporter code, but we also use if for font-locking
  4248. (defconst org-export-html-special-string-regexps
  4249. '(("\\\\-" . "&shy;")
  4250. ("---\\([^-]\\)" . "&mdash;\\1")
  4251. ("--\\([^-]\\)" . "&ndash;\\1")
  4252. ("\\.\\.\\." . "&hellip;"))
  4253. "Regular expressions for special string conversion.")
  4254. (defun org-compute-latex-and-specials-regexp ()
  4255. "Compute regular expression for stuff treated specially by exporters."
  4256. (if (not org-highlight-latex-fragments-and-specials)
  4257. (org-set-local 'org-latex-and-specials-regexp nil)
  4258. (require 'org-exp)
  4259. (let*
  4260. ((matchers (plist-get org-format-latex-options :matchers))
  4261. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4262. org-latex-regexps)))
  4263. (org-export-allow-BIND nil)
  4264. (options (org-combine-plists (org-default-export-plist)
  4265. (org-infile-export-plist)))
  4266. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4267. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4268. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4269. (org-export-html-expand (plist-get options :expand-quoted-html))
  4270. (org-export-with-special-strings (plist-get options :special-strings))
  4271. (re-sub
  4272. (cond
  4273. ((equal org-export-with-sub-superscripts '{})
  4274. (list org-match-substring-with-braces-regexp))
  4275. (org-export-with-sub-superscripts
  4276. (list org-match-substring-regexp))
  4277. (t nil)))
  4278. (re-latex
  4279. (if org-export-with-LaTeX-fragments
  4280. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4281. (re-macros
  4282. (if org-export-with-TeX-macros
  4283. (list (concat "\\\\"
  4284. (regexp-opt
  4285. (append (mapcar 'car org-html-entities)
  4286. (if (boundp 'org-latex-entities)
  4287. (mapcar (lambda (x)
  4288. (or (car-safe x) x))
  4289. org-latex-entities)
  4290. nil))
  4291. 'words))) ; FIXME
  4292. ))
  4293. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4294. (re-special (if org-export-with-special-strings
  4295. (mapcar (lambda (x) (car x))
  4296. org-export-html-special-string-regexps)))
  4297. (re-rest
  4298. (delq nil
  4299. (list
  4300. (if org-export-html-expand "@<[^>\n]+>")
  4301. ))))
  4302. (org-set-local
  4303. 'org-latex-and-specials-regexp
  4304. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4305. re-rest) "\\|")))))
  4306. (defun org-do-latex-and-special-faces (limit)
  4307. "Run through the buffer and add overlays to links."
  4308. (when org-latex-and-specials-regexp
  4309. (let (rtn d)
  4310. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4311. limit t))
  4312. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4313. 'face))
  4314. '(org-code org-verbatim underline)))
  4315. (progn
  4316. (setq rtn t
  4317. d (cond ((member (char-after (1+ (match-beginning 0)))
  4318. '(?_ ?^)) 1)
  4319. (t 0)))
  4320. (font-lock-prepend-text-property
  4321. (+ d (match-beginning 0)) (match-end 0)
  4322. 'face 'org-latex-and-export-specials)
  4323. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4324. '(font-lock-multiline t)))))
  4325. rtn)))
  4326. (defun org-restart-font-lock ()
  4327. "Restart font-lock-mode, to force refontification."
  4328. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4329. (font-lock-mode -1)
  4330. (font-lock-mode 1)))
  4331. (defun org-all-targets (&optional radio)
  4332. "Return a list of all targets in this file.
  4333. With optional argument RADIO, only find radio targets."
  4334. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4335. rtn)
  4336. (save-excursion
  4337. (goto-char (point-min))
  4338. (while (re-search-forward re nil t)
  4339. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4340. rtn)))
  4341. (defun org-make-target-link-regexp (targets)
  4342. "Make regular expression matching all strings in TARGETS.
  4343. The regular expression finds the targets also if there is a line break
  4344. between words."
  4345. (and targets
  4346. (concat
  4347. "\\<\\("
  4348. (mapconcat
  4349. (lambda (x)
  4350. (while (string-match " +" x)
  4351. (setq x (replace-match "\\s-+" t t x)))
  4352. x)
  4353. targets
  4354. "\\|")
  4355. "\\)\\>")))
  4356. (defun org-activate-tags (limit)
  4357. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4358. (progn
  4359. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4360. (add-text-properties (match-beginning 1) (match-end 1)
  4361. (list 'mouse-face 'highlight
  4362. 'keymap org-mouse-map))
  4363. (org-rear-nonsticky-at (match-end 1))
  4364. t)))
  4365. (defun org-outline-level ()
  4366. "Compute the outline level of the heading at point.
  4367. This function assumes that the cursor is at the beginning of a line matched
  4368. by outline-regexp. Otherwise it returns garbage.
  4369. If this is called at a normal headline, the level is the number of stars.
  4370. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4371. For plain list items, if they are matched by `outline-regexp', this returns
  4372. 1000 plus the line indentation."
  4373. (save-excursion
  4374. (looking-at outline-regexp)
  4375. (if (match-beginning 1)
  4376. (+ (org-get-string-indentation (match-string 1)) 1000)
  4377. (1- (- (match-end 0) (match-beginning 0))))))
  4378. (defvar org-font-lock-keywords nil)
  4379. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4380. "Regular expression matching a property line.")
  4381. (defvar org-font-lock-hook nil
  4382. "Functions to be called for special font lock stuff.")
  4383. (defun org-font-lock-hook (limit)
  4384. (run-hook-with-args 'org-font-lock-hook limit))
  4385. (defun org-set-font-lock-defaults ()
  4386. (let* ((em org-fontify-emphasized-text)
  4387. (lk org-activate-links)
  4388. (org-font-lock-extra-keywords
  4389. (list
  4390. ;; Call the hook
  4391. '(org-font-lock-hook)
  4392. ;; Headlines
  4393. `(,(if org-fontify-whole-heading-line
  4394. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4395. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4396. (1 (org-get-level-face 1))
  4397. (2 (org-get-level-face 2))
  4398. (3 (org-get-level-face 3)))
  4399. ;; Table lines
  4400. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4401. (1 'org-table t))
  4402. ;; Table internals
  4403. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4404. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4405. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4406. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4407. ;; Drawers
  4408. (list org-drawer-regexp '(0 'org-special-keyword t))
  4409. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4410. ;; Properties
  4411. (list org-property-re
  4412. '(1 'org-special-keyword t)
  4413. '(3 'org-property-value t))
  4414. ;; Links
  4415. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4416. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4417. (if (memq 'plain lk) '(org-activate-plain-links))
  4418. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4419. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4420. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4421. (if (memq 'footnote lk) '(org-activate-footnote-links
  4422. (2 'org-footnote t)))
  4423. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4424. '(org-hide-wide-columns (0 nil append))
  4425. ;; TODO lines
  4426. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4427. '(1 (org-get-todo-face 1) t))
  4428. ;; DONE
  4429. (if org-fontify-done-headline
  4430. (list (concat "^[*]+ +\\<\\("
  4431. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4432. "\\)\\(.*\\)")
  4433. '(2 'org-headline-done t))
  4434. nil)
  4435. ;; Priorities
  4436. '(org-font-lock-add-priority-faces)
  4437. ;; Tags
  4438. '(org-font-lock-add-tag-faces)
  4439. ;; Special keywords
  4440. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4441. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4442. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4443. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4444. ;; Emphasis
  4445. (if em
  4446. (if (featurep 'xemacs)
  4447. '(org-do-emphasis-faces (0 nil append))
  4448. '(org-do-emphasis-faces)))
  4449. ;; Checkboxes
  4450. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4451. 2 'org-checkbox prepend)
  4452. (if org-provide-checkbox-statistics
  4453. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4454. (0 (org-get-checkbox-statistics-face) t)))
  4455. ;; Description list items
  4456. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4457. 2 'bold prepend)
  4458. ;; ARCHIVEd headings
  4459. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4460. '(1 'org-archived prepend))
  4461. ;; Specials
  4462. '(org-do-latex-and-special-faces)
  4463. ;; Code
  4464. '(org-activate-code (1 'org-code t))
  4465. ;; COMMENT
  4466. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4467. "\\|" org-quote-string "\\)\\>")
  4468. '(1 'org-special-keyword t))
  4469. '("^#.*" (0 'font-lock-comment-face t))
  4470. ;; Blocks and meta lines
  4471. '(org-fontify-meta-lines-and-blocks)
  4472. )))
  4473. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4474. ;; Now set the full font-lock-keywords
  4475. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4476. (org-set-local 'font-lock-defaults
  4477. '(org-font-lock-keywords t nil nil backward-paragraph))
  4478. (kill-local-variable 'font-lock-keywords) nil))
  4479. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4480. "Fontify string S like in Org-mode"
  4481. (with-temp-buffer
  4482. (insert s)
  4483. (let ((org-odd-levels-only odd-levels))
  4484. (org-mode)
  4485. (font-lock-fontify-buffer)
  4486. (buffer-string))))
  4487. (defvar org-m nil)
  4488. (defvar org-l nil)
  4489. (defvar org-f nil)
  4490. (defun org-get-level-face (n)
  4491. "Get the right face for match N in font-lock matching of headlines."
  4492. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4493. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4494. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4495. (cond
  4496. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4497. ((eq n 2) org-f)
  4498. (t (if org-level-color-stars-only nil org-f))))
  4499. (defun org-get-todo-face (kwd)
  4500. "Get the right face for a TODO keyword KWD.
  4501. If KWD is a number, get the corresponding match group."
  4502. (if (numberp kwd) (setq kwd (match-string kwd)))
  4503. (or (cdr (assoc kwd org-todo-keyword-faces))
  4504. (and (member kwd org-done-keywords) 'org-done)
  4505. 'org-todo))
  4506. (defun org-font-lock-add-tag-faces (limit)
  4507. "Add the special tag faces."
  4508. (when (and org-tag-faces org-tags-special-faces-re)
  4509. (while (re-search-forward org-tags-special-faces-re limit t)
  4510. (add-text-properties (match-beginning 1) (match-end 1)
  4511. (list 'face (org-get-tag-face 1)
  4512. 'font-lock-fontified t))
  4513. (backward-char 1))))
  4514. (defun org-font-lock-add-priority-faces (limit)
  4515. "Add the special priority faces."
  4516. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4517. (add-text-properties
  4518. (match-beginning 0) (match-end 0)
  4519. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4520. org-priority-faces))
  4521. 'org-special-keyword)
  4522. 'font-lock-fontified t))))
  4523. (defun org-get-tag-face (kwd)
  4524. "Get the right face for a TODO keyword KWD.
  4525. If KWD is a number, get the corresponding match group."
  4526. (if (numberp kwd) (setq kwd (match-string kwd)))
  4527. (or (cdr (assoc kwd org-tag-faces))
  4528. 'org-tag))
  4529. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4530. "Remove fontification and activation overlays from links."
  4531. (font-lock-default-unfontify-region beg end)
  4532. (let* ((buffer-undo-list t)
  4533. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4534. (inhibit-modification-hooks t)
  4535. deactivate-mark buffer-file-name buffer-file-truename)
  4536. (remove-text-properties
  4537. beg end
  4538. (if org-indent-mode
  4539. ;; also remove line-prefix and wrap-prefix properties
  4540. '(mouse-face t keymap t org-linked-text t
  4541. invisible t intangible t
  4542. line-prefix t wrap-prefix t
  4543. org-no-flyspell t)
  4544. '(mouse-face t keymap t org-linked-text t
  4545. invisible t intangible t
  4546. org-no-flyspell t)))))
  4547. ;;;; Visibility cycling, including org-goto and indirect buffer
  4548. ;;; Cycling
  4549. (defvar org-cycle-global-status nil)
  4550. (make-variable-buffer-local 'org-cycle-global-status)
  4551. (defvar org-cycle-subtree-status nil)
  4552. (make-variable-buffer-local 'org-cycle-subtree-status)
  4553. ;;;###autoload
  4554. (defvar org-inlinetask-min-level)
  4555. (defun org-cycle (&optional arg)
  4556. "TAB-action and visibility cycling for Org-mode.
  4557. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4558. is outline visibility cycling, but it also invokes other actions
  4559. in special contexts.
  4560. - When this function is called with a prefix argument, rotate the entire
  4561. buffer through 3 states (global cycling)
  4562. 1. OVERVIEW: Show only top-level headlines.
  4563. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4564. 3. SHOW ALL: Show everything.
  4565. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4566. determined by the variable `org-startup-folded', and by any VISIBILITY
  4567. properties in the buffer.
  4568. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4569. including any drawers.
  4570. - When inside a table, re-align the table and move to the next field.
  4571. - When point is at the beginning of a headline, rotate the subtree started
  4572. by this line through 3 different states (local cycling)
  4573. 1. FOLDED: Only the main headline is shown.
  4574. 2. CHILDREN: The main headline and the direct children are shown.
  4575. From this state, you can move to one of the children
  4576. and zoom in further.
  4577. 3. SUBTREE: Show the entire subtree, including body text.
  4578. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4579. - When there is a numeric prefix, go up to a heading with level ARG, do
  4580. a `show-subtree' and return to the previous cursor position. If ARG
  4581. is negative, go up that many levels.
  4582. - When point is not at the beginning of a headline, execute the global
  4583. binding for TAB, which is re-indenting the line. See the option
  4584. `org-cycle-emulate-tab' for details.
  4585. - Special case: if point is at the beginning of the buffer and there is
  4586. no headline in line 1, this function will act as if called with prefix arg.
  4587. But only if also the variable `org-cycle-global-at-bob' is t."
  4588. (interactive "P")
  4589. (org-load-modules-maybe)
  4590. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4591. (and org-cycle-level-after-item/entry-creation
  4592. (or (org-cycle-level)
  4593. (org-cycle-item-indentation))))
  4594. (let* ((limit-level
  4595. (or org-cycle-max-level
  4596. (and (boundp 'org-inlinetask-min-level)
  4597. org-inlinetask-min-level
  4598. (1- org-inlinetask-min-level))))
  4599. (nstars (and limit-level
  4600. (if org-odd-levels-only
  4601. (and limit-level (1- (* limit-level 2)))
  4602. limit-level)))
  4603. (outline-regexp
  4604. (cond
  4605. ((not (org-mode-p)) outline-regexp)
  4606. ((or (eq org-cycle-include-plain-lists 'integrate)
  4607. (and org-cycle-include-plain-lists (org-at-item-p)))
  4608. (concat "\\(?:\\*"
  4609. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4610. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4611. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4612. (bob-special (and org-cycle-global-at-bob (bobp)
  4613. (not (looking-at outline-regexp))))
  4614. (org-cycle-hook
  4615. (if bob-special
  4616. (delq 'org-optimize-window-after-visibility-change
  4617. (copy-sequence org-cycle-hook))
  4618. org-cycle-hook))
  4619. (pos (point)))
  4620. (if (or bob-special (equal arg '(4)))
  4621. ;; special case: use global cycling
  4622. (setq arg t))
  4623. (cond
  4624. ((equal arg '(16))
  4625. (org-set-startup-visibility)
  4626. (message "Startup visibility, plus VISIBILITY properties"))
  4627. ((equal arg '(64))
  4628. (show-all)
  4629. (message "Entire buffer visible, including drawers"))
  4630. ((org-at-table-p 'any)
  4631. ;; Enter the table or move to the next field in the table
  4632. (or (org-table-recognize-table.el)
  4633. (progn
  4634. (if arg (org-table-edit-field t)
  4635. (org-table-justify-field-maybe)
  4636. (call-interactively 'org-table-next-field)))))
  4637. ((run-hook-with-args-until-success
  4638. 'org-tab-after-check-for-table-hook))
  4639. ((eq arg t) ;; Global cycling
  4640. (org-cycle-internal-global))
  4641. ((and org-drawers org-drawer-regexp
  4642. (save-excursion
  4643. (beginning-of-line 1)
  4644. (looking-at org-drawer-regexp)))
  4645. ;; Toggle block visibility
  4646. (org-flag-drawer
  4647. (not (get-char-property (match-end 0) 'invisible))))
  4648. ((integerp arg)
  4649. ;; Show-subtree, ARG levels up from here.
  4650. (save-excursion
  4651. (org-back-to-heading)
  4652. (outline-up-heading (if (< arg 0) (- arg)
  4653. (- (funcall outline-level) arg)))
  4654. (org-show-subtree)))
  4655. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4656. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4657. (org-cycle-internal-local))
  4658. ;; TAB emulation and template completion
  4659. (buffer-read-only (org-back-to-heading))
  4660. ((run-hook-with-args-until-success
  4661. 'org-tab-after-check-for-cycling-hook))
  4662. ((org-try-structure-completion))
  4663. ((org-try-cdlatex-tab))
  4664. ((run-hook-with-args-until-success
  4665. 'org-tab-before-tab-emulation-hook))
  4666. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4667. (or (not (bolp))
  4668. (not (looking-at outline-regexp))))
  4669. (call-interactively (global-key-binding "\t")))
  4670. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4671. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4672. (or (and (eq org-cycle-emulate-tab 'white)
  4673. (= (match-end 0) (point-at-eol)))
  4674. (and (eq org-cycle-emulate-tab 'whitestart)
  4675. (>= (match-end 0) pos))))
  4676. t
  4677. (eq org-cycle-emulate-tab t))
  4678. (call-interactively (global-key-binding "\t")))
  4679. (t (save-excursion
  4680. (org-back-to-heading)
  4681. (org-cycle)))))))
  4682. (defun org-cycle-internal-global ()
  4683. "Do the global cycling action."
  4684. (cond
  4685. ((and (eq last-command this-command)
  4686. (eq org-cycle-global-status 'overview))
  4687. ;; We just created the overview - now do table of contents
  4688. ;; This can be slow in very large buffers, so indicate action
  4689. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4690. (message "CONTENTS...")
  4691. (org-content)
  4692. (message "CONTENTS...done")
  4693. (setq org-cycle-global-status 'contents)
  4694. (run-hook-with-args 'org-cycle-hook 'contents))
  4695. ((and (eq last-command this-command)
  4696. (eq org-cycle-global-status 'contents))
  4697. ;; We just showed the table of contents - now show everything
  4698. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4699. (show-all)
  4700. (message "SHOW ALL")
  4701. (setq org-cycle-global-status 'all)
  4702. (run-hook-with-args 'org-cycle-hook 'all))
  4703. (t
  4704. ;; Default action: go to overview
  4705. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4706. (org-overview)
  4707. (message "OVERVIEW")
  4708. (setq org-cycle-global-status 'overview)
  4709. (run-hook-with-args 'org-cycle-hook 'overview))))
  4710. (defun org-cycle-internal-local ()
  4711. "Do the local cycling action."
  4712. (org-back-to-heading)
  4713. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4714. ;; First, some boundaries
  4715. (save-excursion
  4716. (org-back-to-heading)
  4717. (setq level (funcall outline-level))
  4718. (save-excursion
  4719. (beginning-of-line 2)
  4720. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4721. ; XEmacs does not have `next-single-char-property-change'
  4722. ; I'm not sure about Emacs 21.
  4723. (while (and (not (eobp)) ;; this is like `next-line'
  4724. (get-char-property (1- (point)) 'invisible))
  4725. (beginning-of-line 2))
  4726. (while (and (not (eobp)) ;; this is like `next-line'
  4727. (get-char-property (1- (point)) 'invisible))
  4728. (goto-char (next-single-char-property-change (point) 'invisible))
  4729. ;;;??? (or (bolp) (beginning-of-line 2))))
  4730. (and (eolp) (beginning-of-line 2))))
  4731. (setq eol (point)))
  4732. (outline-end-of-heading) (setq eoh (point))
  4733. (save-excursion
  4734. (outline-next-heading)
  4735. (setq has-children (and (org-at-heading-p t)
  4736. (> (funcall outline-level) level))))
  4737. (org-end-of-subtree t)
  4738. (unless (eobp)
  4739. (skip-chars-forward " \t\n")
  4740. (beginning-of-line 1) ; in case this is an item
  4741. )
  4742. (setq eos (if (eobp) (point) (1- (point)))))
  4743. ;; Find out what to do next and set `this-command'
  4744. (cond
  4745. ((= eos eoh)
  4746. ;; Nothing is hidden behind this heading
  4747. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4748. (message "EMPTY ENTRY")
  4749. (setq org-cycle-subtree-status nil)
  4750. (save-excursion
  4751. (goto-char eos)
  4752. (outline-next-heading)
  4753. (if (org-invisible-p) (org-flag-heading nil))))
  4754. ((and (or (>= eol eos)
  4755. (not (string-match "\\S-" (buffer-substring eol eos))))
  4756. (or has-children
  4757. (not (setq children-skipped
  4758. org-cycle-skip-children-state-if-no-children))))
  4759. ;; Entire subtree is hidden in one line: children view
  4760. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4761. (org-show-entry)
  4762. (show-children)
  4763. (message "CHILDREN")
  4764. (save-excursion
  4765. (goto-char eos)
  4766. (outline-next-heading)
  4767. (if (org-invisible-p) (org-flag-heading nil)))
  4768. (setq org-cycle-subtree-status 'children)
  4769. (run-hook-with-args 'org-cycle-hook 'children))
  4770. ((or children-skipped
  4771. (and (eq last-command this-command)
  4772. (eq org-cycle-subtree-status 'children)))
  4773. ;; We just showed the children, or no children are there,
  4774. ;; now show everything.
  4775. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4776. (org-show-subtree)
  4777. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4778. (setq org-cycle-subtree-status 'subtree)
  4779. (run-hook-with-args 'org-cycle-hook 'subtree))
  4780. (t
  4781. ;; Default action: hide the subtree.
  4782. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4783. (hide-subtree)
  4784. (message "FOLDED")
  4785. (setq org-cycle-subtree-status 'folded)
  4786. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4787. ;;;###autoload
  4788. (defun org-global-cycle (&optional arg)
  4789. "Cycle the global visibility. For details see `org-cycle'.
  4790. With C-u prefix arg, switch to startup visibility.
  4791. With a numeric prefix, show all headlines up to that level."
  4792. (interactive "P")
  4793. (let ((org-cycle-include-plain-lists
  4794. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4795. (cond
  4796. ((integerp arg)
  4797. (show-all)
  4798. (hide-sublevels arg)
  4799. (setq org-cycle-global-status 'contents))
  4800. ((equal arg '(4))
  4801. (org-set-startup-visibility)
  4802. (message "Startup visibility, plus VISIBILITY properties."))
  4803. (t
  4804. (org-cycle '(4))))))
  4805. (defun org-set-startup-visibility ()
  4806. "Set the visibility required by startup options and properties."
  4807. (cond
  4808. ((eq org-startup-folded t)
  4809. (org-cycle '(4)))
  4810. ((eq org-startup-folded 'content)
  4811. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4812. (org-cycle '(4)) (org-cycle '(4)))))
  4813. (unless (eq org-startup-folded 'showeverything)
  4814. (if org-hide-block-startup (org-hide-block-all))
  4815. (org-set-visibility-according-to-property 'no-cleanup)
  4816. (org-cycle-hide-archived-subtrees 'all)
  4817. (org-cycle-hide-drawers 'all)
  4818. (org-cycle-show-empty-lines 'all)))
  4819. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4820. "Switch subtree visibilities according to :VISIBILITY: property."
  4821. (interactive)
  4822. (let (org-show-entry-below state)
  4823. (save-excursion
  4824. (goto-char (point-min))
  4825. (while (re-search-forward
  4826. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4827. nil t)
  4828. (setq state (match-string 1))
  4829. (save-excursion
  4830. (org-back-to-heading t)
  4831. (hide-subtree)
  4832. (org-reveal)
  4833. (cond
  4834. ((equal state '("fold" "folded"))
  4835. (hide-subtree))
  4836. ((equal state "children")
  4837. (org-show-hidden-entry)
  4838. (show-children))
  4839. ((equal state "content")
  4840. (save-excursion
  4841. (save-restriction
  4842. (org-narrow-to-subtree)
  4843. (org-content))))
  4844. ((member state '("all" "showall"))
  4845. (show-subtree)))))
  4846. (unless no-cleanup
  4847. (org-cycle-hide-archived-subtrees 'all)
  4848. (org-cycle-hide-drawers 'all)
  4849. (org-cycle-show-empty-lines 'all)))))
  4850. (defun org-overview ()
  4851. "Switch to overview mode, showing only top-level headlines.
  4852. Really, this shows all headlines with level equal or greater than the level
  4853. of the first headline in the buffer. This is important, because if the
  4854. first headline is not level one, then (hide-sublevels 1) gives confusing
  4855. results."
  4856. (interactive)
  4857. (let ((level (save-excursion
  4858. (goto-char (point-min))
  4859. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4860. (progn
  4861. (goto-char (match-beginning 0))
  4862. (funcall outline-level))))))
  4863. (and level (hide-sublevels level))))
  4864. (defun org-content (&optional arg)
  4865. "Show all headlines in the buffer, like a table of contents.
  4866. With numerical argument N, show content up to level N."
  4867. (interactive "P")
  4868. (save-excursion
  4869. ;; Visit all headings and show their offspring
  4870. (and (integerp arg) (org-overview))
  4871. (goto-char (point-max))
  4872. (catch 'exit
  4873. (while (and (progn (condition-case nil
  4874. (outline-previous-visible-heading 1)
  4875. (error (goto-char (point-min))))
  4876. t)
  4877. (looking-at outline-regexp))
  4878. (if (integerp arg)
  4879. (show-children (1- arg))
  4880. (show-branches))
  4881. (if (bobp) (throw 'exit nil))))))
  4882. (defun org-optimize-window-after-visibility-change (state)
  4883. "Adjust the window after a change in outline visibility.
  4884. This function is the default value of the hook `org-cycle-hook'."
  4885. (when (get-buffer-window (current-buffer))
  4886. (cond
  4887. ((eq state 'content) nil)
  4888. ((eq state 'all) nil)
  4889. ((eq state 'folded) nil)
  4890. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4891. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4892. (defun org-remove-empty-overlays-at (pos)
  4893. "Remove outline overlays that do not contain non-white stuff."
  4894. (mapc
  4895. (lambda (o)
  4896. (and (eq 'outline (org-overlay-get o 'invisible))
  4897. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4898. (org-overlay-end o))))
  4899. (org-delete-overlay o)))
  4900. (org-overlays-at pos)))
  4901. (defun org-clean-visibility-after-subtree-move ()
  4902. "Fix visibility issues after moving a subtree."
  4903. ;; First, find a reasonable region to look at:
  4904. ;; Start two siblings above, end three below
  4905. (let* ((beg (save-excursion
  4906. (and (org-get-last-sibling)
  4907. (org-get-last-sibling))
  4908. (point)))
  4909. (end (save-excursion
  4910. (and (org-get-next-sibling)
  4911. (org-get-next-sibling)
  4912. (org-get-next-sibling))
  4913. (if (org-at-heading-p)
  4914. (point-at-eol)
  4915. (point))))
  4916. (level (looking-at "\\*+"))
  4917. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4918. (save-excursion
  4919. (save-restriction
  4920. (narrow-to-region beg end)
  4921. (when re
  4922. ;; Properly fold already folded siblings
  4923. (goto-char (point-min))
  4924. (while (re-search-forward re nil t)
  4925. (if (and (not (org-invisible-p))
  4926. (save-excursion
  4927. (goto-char (point-at-eol)) (org-invisible-p)))
  4928. (hide-entry))))
  4929. (org-cycle-show-empty-lines 'overview)
  4930. (org-cycle-hide-drawers 'overview)))))
  4931. (defun org-cycle-show-empty-lines (state)
  4932. "Show empty lines above all visible headlines.
  4933. The region to be covered depends on STATE when called through
  4934. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4935. entire buffer covered. Note that an empty line is only shown if there
  4936. are at least `org-cycle-separator-lines' empty lines before the headline."
  4937. (when (not (= org-cycle-separator-lines 0))
  4938. (save-excursion
  4939. (let* ((n (abs org-cycle-separator-lines))
  4940. (re (cond
  4941. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4942. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4943. (t (let ((ns (number-to-string (- n 2))))
  4944. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4945. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4946. beg end b e)
  4947. (cond
  4948. ((memq state '(overview contents t))
  4949. (setq beg (point-min) end (point-max)))
  4950. ((memq state '(children folded))
  4951. (setq beg (point) end (progn (org-end-of-subtree t t)
  4952. (beginning-of-line 2)
  4953. (point)))))
  4954. (when beg
  4955. (goto-char beg)
  4956. (while (re-search-forward re end t)
  4957. (unless (get-char-property (match-end 1) 'invisible)
  4958. (setq e (match-end 1))
  4959. (if (< org-cycle-separator-lines 0)
  4960. (setq b (save-excursion
  4961. (goto-char (match-beginning 0))
  4962. (org-back-over-empty-lines)
  4963. (if (save-excursion
  4964. (goto-char (max (point-min) (1- (point))))
  4965. (org-on-heading-p))
  4966. (1- (point))
  4967. (point))))
  4968. (setq b (match-beginning 1)))
  4969. (outline-flag-region b e nil)))))))
  4970. ;; Never hide empty lines at the end of the file.
  4971. (save-excursion
  4972. (goto-char (point-max))
  4973. (outline-previous-heading)
  4974. (outline-end-of-heading)
  4975. (if (and (looking-at "[ \t\n]+")
  4976. (= (match-end 0) (point-max)))
  4977. (outline-flag-region (point) (match-end 0) nil))))
  4978. (defun org-show-empty-lines-in-parent ()
  4979. "Move to the parent and re-show empty lines before visible headlines."
  4980. (save-excursion
  4981. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4982. (org-cycle-show-empty-lines context))))
  4983. (defun org-files-list ()
  4984. "Return `org-agenda-files' list, plus all open org-mode files.
  4985. This is useful for operations that need to scan all of a user's
  4986. open and agenda-wise Org files."
  4987. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  4988. (dolist (buf (buffer-list))
  4989. (with-current-buffer buf
  4990. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  4991. (let ((file (expand-file-name (buffer-file-name))))
  4992. (unless (member file files)
  4993. (push file files))))))
  4994. files))
  4995. (defsubst org-entry-beginning-position ()
  4996. "Return the beginning position of the current entry."
  4997. (save-excursion (outline-back-to-heading t) (point)))
  4998. (defsubst org-entry-end-position ()
  4999. "Return the end position of the current entry."
  5000. (save-excursion (outline-next-heading) (point)))
  5001. (defun org-cycle-hide-drawers (state)
  5002. "Re-hide all drawers after a visibility state change."
  5003. (when (and (org-mode-p)
  5004. (not (memq state '(overview folded contents))))
  5005. (save-excursion
  5006. (let* ((globalp (memq state '(contents all)))
  5007. (beg (if globalp (point-min) (point)))
  5008. (end (if globalp (point-max)
  5009. (if (eq state 'children)
  5010. (save-excursion (outline-next-heading) (point))
  5011. (org-end-of-subtree t)))))
  5012. (goto-char beg)
  5013. (while (re-search-forward org-drawer-regexp end t)
  5014. (org-flag-drawer t))))))
  5015. (defun org-flag-drawer (flag)
  5016. (save-excursion
  5017. (beginning-of-line 1)
  5018. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5019. (let ((b (match-end 0))
  5020. (outline-regexp org-outline-regexp))
  5021. (if (re-search-forward
  5022. "^[ \t]*:END:"
  5023. (save-excursion (outline-next-heading) (point)) t)
  5024. (outline-flag-region b (point-at-eol) flag)
  5025. (error ":END: line missing at position %s" b))))))
  5026. (defun org-subtree-end-visible-p ()
  5027. "Is the end of the current subtree visible?"
  5028. (pos-visible-in-window-p
  5029. (save-excursion (org-end-of-subtree t) (point))))
  5030. (defun org-first-headline-recenter (&optional N)
  5031. "Move cursor to the first headline and recenter the headline.
  5032. Optional argument N means, put the headline into the Nth line of the window."
  5033. (goto-char (point-min))
  5034. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5035. (beginning-of-line)
  5036. (recenter (prefix-numeric-value N))))
  5037. ;;; Saving and restoring visibility
  5038. (defun org-outline-overlay-data (&optional use-markers)
  5039. "Return a list of the locations of all outline overlays.
  5040. The are overlays with the `invisible' property value `outline'.
  5041. The return valus is a list of cons cells, with start and stop
  5042. positions for each overlay.
  5043. If USE-MARKERS is set, return the positions as markers."
  5044. (let (beg end)
  5045. (save-excursion
  5046. (save-restriction
  5047. (widen)
  5048. (delq nil
  5049. (mapcar (lambda (o)
  5050. (when (eq (org-overlay-get o 'invisible) 'outline)
  5051. (setq beg (org-overlay-start o)
  5052. end (org-overlay-end o))
  5053. (and beg end (> end beg)
  5054. (if use-markers
  5055. (cons (move-marker (make-marker) beg)
  5056. (move-marker (make-marker) end))
  5057. (cons beg end)))))
  5058. (org-overlays-in (point-min) (point-max))))))))
  5059. (defun org-set-outline-overlay-data (data)
  5060. "Create visibility overlays for all positions in DATA.
  5061. DATA should have been made by `org-outline-overlay-data'."
  5062. (let (o)
  5063. (save-excursion
  5064. (save-restriction
  5065. (widen)
  5066. (show-all)
  5067. (mapc (lambda (c)
  5068. (setq o (org-make-overlay (car c) (cdr c)))
  5069. (org-overlay-put o 'invisible 'outline))
  5070. data)))))
  5071. (defmacro org-save-outline-visibility (use-markers &rest body)
  5072. "Save and restore outline visibility around BODY.
  5073. If USE-MARKERS is non-nil, use markers for the positions.
  5074. This means that the buffer may change while running BODY,
  5075. but it also means that the buffer should stay alive
  5076. during the operation, because otherwise all these markers will
  5077. point nowhere."
  5078. `(let ((data (org-outline-overlay-data ,use-markers)))
  5079. (unwind-protect
  5080. (progn
  5081. ,@body
  5082. (org-set-outline-overlay-data data))
  5083. (when ,use-markers
  5084. (mapc (lambda (c)
  5085. (and (markerp (car c)) (move-marker (car c) nil))
  5086. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5087. data)))))
  5088. ;;; Folding of blocks
  5089. (defconst org-block-regexp
  5090. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5091. "Regular expression for hiding blocks.")
  5092. (defvar org-hide-block-overlays nil
  5093. "Overlays hiding blocks.")
  5094. (make-variable-buffer-local 'org-hide-block-overlays)
  5095. (defun org-block-map (function &optional start end)
  5096. "Call func at the head of all source blocks in the current
  5097. buffer. Optional arguments START and END can be used to limit
  5098. the range."
  5099. (let ((start (or start (point-min)))
  5100. (end (or end (point-max))))
  5101. (save-excursion
  5102. (goto-char start)
  5103. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5104. (save-excursion
  5105. (save-match-data
  5106. (goto-char (match-beginning 0))
  5107. (funcall function)))))))
  5108. (defun org-hide-block-toggle-all ()
  5109. "Toggle the visibility of all blocks in the current buffer."
  5110. (org-block-map #'org-hide-block-toggle))
  5111. (defun org-hide-block-all ()
  5112. "Fold all blocks in the current buffer."
  5113. (interactive)
  5114. (org-show-block-all)
  5115. (org-block-map #'org-hide-block-toggle-maybe))
  5116. (defun org-show-block-all ()
  5117. "Unfold all blocks in the current buffer."
  5118. (mapc 'org-delete-overlay org-hide-block-overlays)
  5119. (setq org-hide-block-overlays nil))
  5120. (defun org-hide-block-toggle-maybe ()
  5121. "Toggle visibility of block at point."
  5122. (interactive)
  5123. (let ((case-fold-search t))
  5124. (if (save-excursion
  5125. (beginning-of-line 1)
  5126. (looking-at org-block-regexp))
  5127. (progn (org-hide-block-toggle)
  5128. t) ;; to signal that we took action
  5129. nil))) ;; to signal that we did not
  5130. (defun org-hide-block-toggle (&optional force)
  5131. "Toggle the visibility of the current block."
  5132. (interactive)
  5133. (save-excursion
  5134. (beginning-of-line)
  5135. (if (re-search-forward org-block-regexp nil t)
  5136. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5137. (end (match-end 0)) ;; end of entire body
  5138. ov)
  5139. (if (memq t (mapcar (lambda (overlay)
  5140. (eq (org-overlay-get overlay 'invisible)
  5141. 'org-hide-block))
  5142. (org-overlays-at start)))
  5143. (if (or (not force) (eq force 'off))
  5144. (mapc (lambda (ov)
  5145. (when (member ov org-hide-block-overlays)
  5146. (setq org-hide-block-overlays
  5147. (delq ov org-hide-block-overlays)))
  5148. (when (eq (org-overlay-get ov 'invisible)
  5149. 'org-hide-block)
  5150. (org-delete-overlay ov)))
  5151. (org-overlays-at start)))
  5152. (setq ov (org-make-overlay start end))
  5153. (org-overlay-put ov 'invisible 'org-hide-block)
  5154. ;; make the block accessible to isearch
  5155. (org-overlay-put
  5156. ov 'isearch-open-invisible
  5157. (lambda (ov)
  5158. (when (member ov org-hide-block-overlays)
  5159. (setq org-hide-block-overlays
  5160. (delq ov org-hide-block-overlays)))
  5161. (when (eq (org-overlay-get ov 'invisible)
  5162. 'org-hide-block)
  5163. (org-delete-overlay ov))))
  5164. (push ov org-hide-block-overlays)))
  5165. (error "Not looking at a source block"))))
  5166. ;; org-tab-after-check-for-cycling-hook
  5167. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5168. ;; Remove overlays when changing major mode
  5169. (add-hook 'org-mode-hook
  5170. (lambda () (org-add-hook 'change-major-mode-hook
  5171. 'org-show-block-all 'append 'local)))
  5172. ;;; Org-goto
  5173. (defvar org-goto-window-configuration nil)
  5174. (defvar org-goto-marker nil)
  5175. (defvar org-goto-map
  5176. (let ((map (make-sparse-keymap)))
  5177. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5178. (while (setq cmd (pop cmds))
  5179. (substitute-key-definition cmd cmd map global-map)))
  5180. (suppress-keymap map)
  5181. (org-defkey map "\C-m" 'org-goto-ret)
  5182. (org-defkey map [(return)] 'org-goto-ret)
  5183. (org-defkey map [(left)] 'org-goto-left)
  5184. (org-defkey map [(right)] 'org-goto-right)
  5185. (org-defkey map [(control ?g)] 'org-goto-quit)
  5186. (org-defkey map "\C-i" 'org-cycle)
  5187. (org-defkey map [(tab)] 'org-cycle)
  5188. (org-defkey map [(down)] 'outline-next-visible-heading)
  5189. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5190. (if org-goto-auto-isearch
  5191. (if (fboundp 'define-key-after)
  5192. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5193. nil)
  5194. (org-defkey map "q" 'org-goto-quit)
  5195. (org-defkey map "n" 'outline-next-visible-heading)
  5196. (org-defkey map "p" 'outline-previous-visible-heading)
  5197. (org-defkey map "f" 'outline-forward-same-level)
  5198. (org-defkey map "b" 'outline-backward-same-level)
  5199. (org-defkey map "u" 'outline-up-heading))
  5200. (org-defkey map "/" 'org-occur)
  5201. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5202. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5203. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5204. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5205. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5206. map))
  5207. (defconst org-goto-help
  5208. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5209. RET=jump to location [Q]uit and return to previous location
  5210. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5211. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5212. ;; FIXME: Docstring does not mention both interfaces
  5213. (defun org-goto (&optional alternative-interface)
  5214. "Look up a different location in the current file, keeping current visibility.
  5215. When you want look-up or go to a different location in a document, the
  5216. fastest way is often to fold the entire buffer and then dive into the tree.
  5217. This method has the disadvantage, that the previous location will be folded,
  5218. which may not be what you want.
  5219. This command works around this by showing a copy of the current buffer
  5220. in an indirect buffer, in overview mode. You can dive into the tree in
  5221. that copy, use org-occur and incremental search to find a location.
  5222. When pressing RET or `Q', the command returns to the original buffer in
  5223. which the visibility is still unchanged. After RET is will also jump to
  5224. the location selected in the indirect buffer and expose the
  5225. the headline hierarchy above."
  5226. (interactive "P")
  5227. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5228. (org-refile-use-outline-path t)
  5229. (org-refile-target-verify-function nil)
  5230. (interface
  5231. (if (not alternative-interface)
  5232. org-goto-interface
  5233. (if (eq org-goto-interface 'outline)
  5234. 'outline-path-completion
  5235. 'outline)))
  5236. (org-goto-start-pos (point))
  5237. (selected-point
  5238. (if (eq interface 'outline)
  5239. (car (org-get-location (current-buffer) org-goto-help))
  5240. (nth 3 (org-refile-get-location "Goto: ")))))
  5241. (if selected-point
  5242. (progn
  5243. (org-mark-ring-push org-goto-start-pos)
  5244. (goto-char selected-point)
  5245. (if (or (org-invisible-p) (org-invisible-p2))
  5246. (org-show-context 'org-goto)))
  5247. (message "Quit"))))
  5248. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5249. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5250. (defvar org-goto-local-auto-isearch-map) ; defined below
  5251. (defun org-get-location (buf help)
  5252. "Let the user select a location in the Org-mode buffer BUF.
  5253. This function uses a recursive edit. It returns the selected position
  5254. or nil."
  5255. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5256. (isearch-hide-immediately nil)
  5257. (isearch-search-fun-function
  5258. (lambda () 'org-goto-local-search-headings))
  5259. (org-goto-selected-point org-goto-exit-command))
  5260. (save-excursion
  5261. (save-window-excursion
  5262. (delete-other-windows)
  5263. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5264. (switch-to-buffer
  5265. (condition-case nil
  5266. (make-indirect-buffer (current-buffer) "*org-goto*")
  5267. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5268. (with-output-to-temp-buffer "*Help*"
  5269. (princ help))
  5270. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5271. (setq buffer-read-only nil)
  5272. (let ((org-startup-truncated t)
  5273. (org-startup-folded nil)
  5274. (org-startup-align-all-tables nil))
  5275. (org-mode)
  5276. (org-overview))
  5277. (setq buffer-read-only t)
  5278. (if (and (boundp 'org-goto-start-pos)
  5279. (integer-or-marker-p org-goto-start-pos))
  5280. (let ((org-show-hierarchy-above t)
  5281. (org-show-siblings t)
  5282. (org-show-following-heading t))
  5283. (goto-char org-goto-start-pos)
  5284. (and (org-invisible-p) (org-show-context)))
  5285. (goto-char (point-min)))
  5286. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5287. (message "Select location and press RET")
  5288. (use-local-map org-goto-map)
  5289. (recursive-edit)
  5290. ))
  5291. (kill-buffer "*org-goto*")
  5292. (cons org-goto-selected-point org-goto-exit-command)))
  5293. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5294. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5295. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5296. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5297. (defun org-goto-local-search-headings (string bound noerror)
  5298. "Search and make sure that any matches are in headlines."
  5299. (catch 'return
  5300. (while (if isearch-forward
  5301. (search-forward string bound noerror)
  5302. (search-backward string bound noerror))
  5303. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5304. (and (member :headline context)
  5305. (not (member :tags context))))
  5306. (throw 'return (point))))))
  5307. (defun org-goto-local-auto-isearch ()
  5308. "Start isearch."
  5309. (interactive)
  5310. (goto-char (point-min))
  5311. (let ((keys (this-command-keys)))
  5312. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5313. (isearch-mode t)
  5314. (isearch-process-search-char (string-to-char keys)))))
  5315. (defun org-goto-ret (&optional arg)
  5316. "Finish `org-goto' by going to the new location."
  5317. (interactive "P")
  5318. (setq org-goto-selected-point (point)
  5319. org-goto-exit-command 'return)
  5320. (throw 'exit nil))
  5321. (defun org-goto-left ()
  5322. "Finish `org-goto' by going to the new location."
  5323. (interactive)
  5324. (if (org-on-heading-p)
  5325. (progn
  5326. (beginning-of-line 1)
  5327. (setq org-goto-selected-point (point)
  5328. org-goto-exit-command 'left)
  5329. (throw 'exit nil))
  5330. (error "Not on a heading")))
  5331. (defun org-goto-right ()
  5332. "Finish `org-goto' by going to the new location."
  5333. (interactive)
  5334. (if (org-on-heading-p)
  5335. (progn
  5336. (setq org-goto-selected-point (point)
  5337. org-goto-exit-command 'right)
  5338. (throw 'exit nil))
  5339. (error "Not on a heading")))
  5340. (defun org-goto-quit ()
  5341. "Finish `org-goto' without cursor motion."
  5342. (interactive)
  5343. (setq org-goto-selected-point nil)
  5344. (setq org-goto-exit-command 'quit)
  5345. (throw 'exit nil))
  5346. ;;; Indirect buffer display of subtrees
  5347. (defvar org-indirect-dedicated-frame nil
  5348. "This is the frame being used for indirect tree display.")
  5349. (defvar org-last-indirect-buffer nil)
  5350. (defun org-tree-to-indirect-buffer (&optional arg)
  5351. "Create indirect buffer and narrow it to current subtree.
  5352. With numerical prefix ARG, go up to this level and then take that tree.
  5353. If ARG is negative, go up that many levels.
  5354. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5355. indirect buffer previously made with this command, to avoid proliferation of
  5356. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5357. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5358. is kept so that you can work with several indirect buffers at the same time.
  5359. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5360. requests that a new frame be made for the new buffer, so that the dedicated
  5361. frame is not changed."
  5362. (interactive "P")
  5363. (let ((cbuf (current-buffer))
  5364. (cwin (selected-window))
  5365. (pos (point))
  5366. beg end level heading ibuf)
  5367. (save-excursion
  5368. (org-back-to-heading t)
  5369. (when (numberp arg)
  5370. (setq level (org-outline-level))
  5371. (if (< arg 0) (setq arg (+ level arg)))
  5372. (while (> (setq level (org-outline-level)) arg)
  5373. (outline-up-heading 1 t)))
  5374. (setq beg (point)
  5375. heading (org-get-heading))
  5376. (org-end-of-subtree t t) (setq end (point)))
  5377. (if (and (buffer-live-p org-last-indirect-buffer)
  5378. (not (eq org-indirect-buffer-display 'new-frame))
  5379. (not arg))
  5380. (kill-buffer org-last-indirect-buffer))
  5381. (setq ibuf (org-get-indirect-buffer cbuf)
  5382. org-last-indirect-buffer ibuf)
  5383. (cond
  5384. ((or (eq org-indirect-buffer-display 'new-frame)
  5385. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5386. (select-frame (make-frame))
  5387. (delete-other-windows)
  5388. (switch-to-buffer ibuf)
  5389. (org-set-frame-title heading))
  5390. ((eq org-indirect-buffer-display 'dedicated-frame)
  5391. (raise-frame
  5392. (select-frame (or (and org-indirect-dedicated-frame
  5393. (frame-live-p org-indirect-dedicated-frame)
  5394. org-indirect-dedicated-frame)
  5395. (setq org-indirect-dedicated-frame (make-frame)))))
  5396. (delete-other-windows)
  5397. (switch-to-buffer ibuf)
  5398. (org-set-frame-title (concat "Indirect: " heading)))
  5399. ((eq org-indirect-buffer-display 'current-window)
  5400. (switch-to-buffer ibuf))
  5401. ((eq org-indirect-buffer-display 'other-window)
  5402. (pop-to-buffer ibuf))
  5403. (t (error "Invalid value")))
  5404. (if (featurep 'xemacs)
  5405. (save-excursion (org-mode) (turn-on-font-lock)))
  5406. (narrow-to-region beg end)
  5407. (show-all)
  5408. (goto-char pos)
  5409. (and (window-live-p cwin) (select-window cwin))))
  5410. (defun org-get-indirect-buffer (&optional buffer)
  5411. (setq buffer (or buffer (current-buffer)))
  5412. (let ((n 1) (base (buffer-name buffer)) bname)
  5413. (while (buffer-live-p
  5414. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5415. (setq n (1+ n)))
  5416. (condition-case nil
  5417. (make-indirect-buffer buffer bname 'clone)
  5418. (error (make-indirect-buffer buffer bname)))))
  5419. (defun org-set-frame-title (title)
  5420. "Set the title of the current frame to the string TITLE."
  5421. ;; FIXME: how to name a single frame in XEmacs???
  5422. (unless (featurep 'xemacs)
  5423. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5424. ;;;; Structure editing
  5425. ;;; Inserting headlines
  5426. (defun org-previous-line-empty-p ()
  5427. (save-excursion
  5428. (and (not (bobp))
  5429. (or (beginning-of-line 0) t)
  5430. (save-match-data
  5431. (looking-at "[ \t]*$")))))
  5432. (defun org-insert-heading (&optional force-heading)
  5433. "Insert a new heading or item with same depth at point.
  5434. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5435. If point is at the beginning of a headline, insert a sibling before the
  5436. current headline. If point is not at the beginning, do not split the line,
  5437. but create the new headline after the current line."
  5438. (interactive "P")
  5439. (if (or (= (buffer-size) 0)
  5440. (and (not (save-excursion (and (ignore-errors (org-back-to-heading))
  5441. (org-on-heading-p))))
  5442. (not (org-in-item-p))))
  5443. (insert "\n* ")
  5444. (when (or force-heading (not (org-insert-item)))
  5445. (let* ((empty-line-p nil)
  5446. (head (save-excursion
  5447. (condition-case nil
  5448. (progn
  5449. (org-back-to-heading)
  5450. (setq empty-line-p (org-previous-line-empty-p))
  5451. (match-string 0))
  5452. (error "*"))))
  5453. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5454. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5455. pos hide-previous previous-pos)
  5456. (cond
  5457. ((and (org-on-heading-p) (bolp)
  5458. (or (bobp)
  5459. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5460. ;; insert before the current line
  5461. (open-line (if blank 2 1)))
  5462. ((and (bolp)
  5463. (not org-insert-heading-respect-content)
  5464. (or (bobp)
  5465. (save-excursion
  5466. (backward-char 1) (not (org-invisible-p)))))
  5467. ;; insert right here
  5468. nil)
  5469. (t
  5470. ;; somewhere in the line
  5471. (save-excursion
  5472. (setq previous-pos (point-at-bol))
  5473. (end-of-line)
  5474. (setq hide-previous (org-invisible-p)))
  5475. (and org-insert-heading-respect-content (org-show-subtree))
  5476. (let ((split
  5477. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5478. (save-excursion
  5479. (let ((p (point)))
  5480. (goto-char (point-at-bol))
  5481. (and (looking-at org-complex-heading-regexp)
  5482. (> p (match-beginning 4)))))))
  5483. tags pos)
  5484. (cond
  5485. (org-insert-heading-respect-content
  5486. (org-end-of-subtree nil t)
  5487. (or (bolp) (newline))
  5488. (or (org-previous-line-empty-p)
  5489. (and blank (newline)))
  5490. (open-line 1))
  5491. ((org-on-heading-p)
  5492. (when hide-previous
  5493. (show-children)
  5494. (org-show-entry))
  5495. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5496. (setq tags (and (match-end 2) (match-string 2)))
  5497. (and (match-end 1)
  5498. (delete-region (match-beginning 1) (match-end 1)))
  5499. (setq pos (point-at-bol))
  5500. (or split (end-of-line 1))
  5501. (delete-horizontal-space)
  5502. (newline (if blank 2 1))
  5503. (when tags
  5504. (save-excursion
  5505. (goto-char pos)
  5506. (end-of-line 1)
  5507. (insert " " tags)
  5508. (org-set-tags nil 'align))))
  5509. (t
  5510. (or split (end-of-line 1))
  5511. (newline (if blank 2 1)))))))
  5512. (insert head) (just-one-space)
  5513. (setq pos (point))
  5514. (end-of-line 1)
  5515. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5516. (when (and org-insert-heading-respect-content hide-previous)
  5517. (save-excursion
  5518. (goto-char previous-pos)
  5519. (hide-subtree)))
  5520. (run-hooks 'org-insert-heading-hook)))))
  5521. (defun org-get-heading (&optional no-tags)
  5522. "Return the heading of the current entry, without the stars."
  5523. (save-excursion
  5524. (org-back-to-heading t)
  5525. (if (looking-at
  5526. (if no-tags
  5527. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5528. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5529. (match-string 1) "")))
  5530. (defun org-heading-components ()
  5531. "Return the components of the current heading.
  5532. This is a list with the following elements:
  5533. - the level as an integer
  5534. - the reduced level, different if `org-odd-levels-only' is set.
  5535. - the TODO keyword, or nil
  5536. - the priority character, like ?A, or nil if no priority is given
  5537. - the headline text itself, or the tags string if no headline text
  5538. - the tags string, or nil."
  5539. (save-excursion
  5540. (org-back-to-heading t)
  5541. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5542. (list (length (match-string 1))
  5543. (org-reduced-level (length (match-string 1)))
  5544. (org-match-string-no-properties 2)
  5545. (and (match-end 3) (aref (match-string 3) 2))
  5546. (org-match-string-no-properties 4)
  5547. (org-match-string-no-properties 5)))))
  5548. (defun org-get-entry ()
  5549. "Get the entry text, after heading, entire subtree."
  5550. (save-excursion
  5551. (org-back-to-heading t)
  5552. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5553. (defun org-insert-heading-after-current ()
  5554. "Insert a new heading with same level as current, after current subtree."
  5555. (interactive)
  5556. (org-back-to-heading)
  5557. (org-insert-heading)
  5558. (org-move-subtree-down)
  5559. (end-of-line 1))
  5560. (defun org-insert-heading-respect-content ()
  5561. (interactive)
  5562. (let ((org-insert-heading-respect-content t))
  5563. (org-insert-heading t)))
  5564. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5565. (interactive "P")
  5566. (let ((org-insert-heading-respect-content t))
  5567. (org-insert-todo-heading force-state t)))
  5568. (defun org-insert-todo-heading (arg &optional force-heading)
  5569. "Insert a new heading with the same level and TODO state as current heading.
  5570. If the heading has no TODO state, or if the state is DONE, use the first
  5571. state (TODO by default). Also with prefix arg, force first state."
  5572. (interactive "P")
  5573. (when (or force-heading (not (org-insert-item 'checkbox)))
  5574. (org-insert-heading force-heading)
  5575. (save-excursion
  5576. (org-back-to-heading)
  5577. (outline-previous-heading)
  5578. (looking-at org-todo-line-regexp))
  5579. (let*
  5580. ((new-mark-x
  5581. (if (or arg
  5582. (not (match-beginning 2))
  5583. (member (match-string 2) org-done-keywords))
  5584. (car org-todo-keywords-1)
  5585. (match-string 2)))
  5586. (new-mark
  5587. (or
  5588. (run-hook-with-args-until-success
  5589. 'org-todo-get-default-hook new-mark-x nil)
  5590. new-mark-x)))
  5591. (beginning-of-line 1)
  5592. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5593. (if org-treat-insert-todo-heading-as-state-change
  5594. (org-todo new-mark)
  5595. (insert new-mark " "))))
  5596. (when org-provide-todo-statistics
  5597. (org-update-parent-todo-statistics))))
  5598. (defun org-insert-subheading (arg)
  5599. "Insert a new subheading and demote it.
  5600. Works for outline headings and for plain lists alike."
  5601. (interactive "P")
  5602. (org-insert-heading arg)
  5603. (cond
  5604. ((org-on-heading-p) (org-do-demote))
  5605. ((org-at-item-p) (org-indent-item 1))))
  5606. (defun org-insert-todo-subheading (arg)
  5607. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5608. Works for outline headings and for plain lists alike."
  5609. (interactive "P")
  5610. (org-insert-todo-heading arg)
  5611. (cond
  5612. ((org-on-heading-p) (org-do-demote))
  5613. ((org-at-item-p) (org-indent-item 1))))
  5614. ;;; Promotion and Demotion
  5615. (defvar org-after-demote-entry-hook nil
  5616. "Hook run after an entry has been demoted.
  5617. The cursor will be at the beginning of the entry.
  5618. When a subtree is being demoted, the hook will be called for each node.")
  5619. (defvar org-after-promote-entry-hook nil
  5620. "Hook run after an entry has been promoted.
  5621. The cursor will be at the beginning of the entry.
  5622. When a subtree is being promoted, the hook will be called for each node.")
  5623. (defun org-promote-subtree ()
  5624. "Promote the entire subtree.
  5625. See also `org-promote'."
  5626. (interactive)
  5627. (save-excursion
  5628. (org-map-tree 'org-promote))
  5629. (org-fix-position-after-promote))
  5630. (defun org-demote-subtree ()
  5631. "Demote the entire subtree. See `org-demote'.
  5632. See also `org-promote'."
  5633. (interactive)
  5634. (save-excursion
  5635. (org-map-tree 'org-demote))
  5636. (org-fix-position-after-promote))
  5637. (defun org-do-promote ()
  5638. "Promote the current heading higher up the tree.
  5639. If the region is active in `transient-mark-mode', promote all headings
  5640. in the region."
  5641. (interactive)
  5642. (save-excursion
  5643. (if (org-region-active-p)
  5644. (org-map-region 'org-promote (region-beginning) (region-end))
  5645. (org-promote)))
  5646. (org-fix-position-after-promote))
  5647. (defun org-do-demote ()
  5648. "Demote the current heading lower down the tree.
  5649. If the region is active in `transient-mark-mode', demote all headings
  5650. in the region."
  5651. (interactive)
  5652. (save-excursion
  5653. (if (org-region-active-p)
  5654. (org-map-region 'org-demote (region-beginning) (region-end))
  5655. (org-demote)))
  5656. (org-fix-position-after-promote))
  5657. (defun org-fix-position-after-promote ()
  5658. "Make sure that after pro/demotion cursor position is right."
  5659. (let ((pos (point)))
  5660. (when (save-excursion
  5661. (beginning-of-line 1)
  5662. (looking-at org-todo-line-regexp)
  5663. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5664. (cond ((eobp) (insert " "))
  5665. ((eolp) (insert " "))
  5666. ((equal (char-after) ?\ ) (forward-char 1))))))
  5667. (defun org-current-level ()
  5668. "Return the level of the current entry, or nil if before the first headline.
  5669. The level is the number of stars at the beginning of the headline."
  5670. (save-excursion
  5671. (condition-case nil
  5672. (progn
  5673. (org-back-to-heading t)
  5674. (funcall outline-level))
  5675. (error nil))))
  5676. (defun org-reduced-level (l)
  5677. "Compute the effective level of a heading.
  5678. This takes into account the setting of `org-odd-levels-only'."
  5679. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5680. (defun org-get-valid-level (level &optional change)
  5681. "Rectify a level change under the influence of `org-odd-levels-only'
  5682. LEVEL is a current level, CHANGE is by how much the level should be
  5683. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5684. even level numbers will become the next higher odd number."
  5685. (if org-odd-levels-only
  5686. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5687. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5688. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5689. (max 1 (+ level (or change 0)))))
  5690. (if (boundp 'define-obsolete-function-alias)
  5691. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5692. (define-obsolete-function-alias 'org-get-legal-level
  5693. 'org-get-valid-level)
  5694. (define-obsolete-function-alias 'org-get-legal-level
  5695. 'org-get-valid-level "23.1")))
  5696. (defun org-promote ()
  5697. "Promote the current heading higher up the tree.
  5698. If the region is active in `transient-mark-mode', promote all headings
  5699. in the region."
  5700. (org-back-to-heading t)
  5701. (let* ((level (save-match-data (funcall outline-level)))
  5702. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5703. (diff (abs (- level (length up-head) -1))))
  5704. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5705. (replace-match up-head nil t)
  5706. ;; Fixup tag positioning
  5707. (and org-auto-align-tags (org-set-tags nil t))
  5708. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5709. (run-hooks 'org-after-promote-entry-hook)))
  5710. (defun org-demote ()
  5711. "Demote the current heading lower down the tree.
  5712. If the region is active in `transient-mark-mode', demote all headings
  5713. in the region."
  5714. (org-back-to-heading t)
  5715. (let* ((level (save-match-data (funcall outline-level)))
  5716. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5717. (diff (abs (- level (length down-head) -1))))
  5718. (replace-match down-head nil t)
  5719. ;; Fixup tag positioning
  5720. (and org-auto-align-tags (org-set-tags nil t))
  5721. (if org-adapt-indentation (org-fixup-indentation diff))
  5722. (run-hooks 'org-after-demote-entry-hook)))
  5723. (defvar org-tab-ind-state nil)
  5724. (defun org-cycle-level ()
  5725. (let ((org-adapt-indentation nil))
  5726. (when (and (looking-at "[ \t]*$")
  5727. (org-looking-back
  5728. (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp "\\)?[ \t]*")))
  5729. (setq this-command 'org-cycle-level)
  5730. (if (eq last-command 'org-cycle-level)
  5731. (condition-case nil
  5732. (progn (org-do-promote)
  5733. (if (equal org-tab-ind-state (org-current-level))
  5734. (org-do-promote)))
  5735. (error
  5736. (progn
  5737. (save-excursion
  5738. (beginning-of-line 1)
  5739. (and (looking-at "\\*+")
  5740. (replace-match
  5741. (make-string org-tab-ind-state ?*))))
  5742. (setq this-command 'org-cycle))))
  5743. (setq org-tab-ind-state (- (match-end 1) (match-beginning 1)))
  5744. (org-do-demote))
  5745. t)))
  5746. (defun org-map-tree (fun)
  5747. "Call FUN for every heading underneath the current one."
  5748. (org-back-to-heading)
  5749. (let ((level (funcall outline-level)))
  5750. (save-excursion
  5751. (funcall fun)
  5752. (while (and (progn
  5753. (outline-next-heading)
  5754. (> (funcall outline-level) level))
  5755. (not (eobp)))
  5756. (funcall fun)))))
  5757. (defun org-map-region (fun beg end)
  5758. "Call FUN for every heading between BEG and END."
  5759. (let ((org-ignore-region t))
  5760. (save-excursion
  5761. (setq end (copy-marker end))
  5762. (goto-char beg)
  5763. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5764. (< (point) end))
  5765. (funcall fun))
  5766. (while (and (progn
  5767. (outline-next-heading)
  5768. (< (point) end))
  5769. (not (eobp)))
  5770. (funcall fun)))))
  5771. (defun org-fixup-indentation (diff)
  5772. "Change the indentation in the current entry by DIFF
  5773. However, if any line in the current entry has no indentation, or if it
  5774. would end up with no indentation after the change, nothing at all is done."
  5775. (save-excursion
  5776. (let ((end (save-excursion (outline-next-heading)
  5777. (point-marker)))
  5778. (prohibit (if (> diff 0)
  5779. "^\\S-"
  5780. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5781. col)
  5782. (unless (save-excursion (end-of-line 1)
  5783. (re-search-forward prohibit end t))
  5784. (while (and (< (point) end)
  5785. (re-search-forward "^[ \t]+" end t))
  5786. (goto-char (match-end 0))
  5787. (setq col (current-column))
  5788. (if (< diff 0) (replace-match ""))
  5789. (org-indent-to-column (+ diff col))))
  5790. (move-marker end nil))))
  5791. (defun org-convert-to-odd-levels ()
  5792. "Convert an org-mode file with all levels allowed to one with odd levels.
  5793. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5794. level 5 etc."
  5795. (interactive)
  5796. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5797. (let ((outline-regexp org-outline-regexp)
  5798. (outline-level 'org-outline-level)
  5799. (org-odd-levels-only nil) n)
  5800. (save-excursion
  5801. (goto-char (point-min))
  5802. (while (re-search-forward "^\\*\\*+ " nil t)
  5803. (setq n (- (length (match-string 0)) 2))
  5804. (while (>= (setq n (1- n)) 0)
  5805. (org-demote))
  5806. (end-of-line 1))))))
  5807. (defun org-convert-to-oddeven-levels ()
  5808. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5809. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5810. section with an even level, conversion would destroy the structure of the file. An error
  5811. is signaled in this case."
  5812. (interactive)
  5813. (goto-char (point-min))
  5814. ;; First check if there are no even levels
  5815. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5816. (org-show-context t)
  5817. (error "Not all levels are odd in this file. Conversion not possible"))
  5818. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5819. (let ((outline-regexp org-outline-regexp)
  5820. (outline-level 'org-outline-level)
  5821. (org-odd-levels-only nil) n)
  5822. (save-excursion
  5823. (goto-char (point-min))
  5824. (while (re-search-forward "^\\*\\*+ " nil t)
  5825. (setq n (/ (1- (length (match-string 0))) 2))
  5826. (while (>= (setq n (1- n)) 0)
  5827. (org-promote))
  5828. (end-of-line 1))))))
  5829. (defun org-tr-level (n)
  5830. "Make N odd if required."
  5831. (if org-odd-levels-only (1+ (/ n 2)) n))
  5832. ;;; Vertical tree motion, cutting and pasting of subtrees
  5833. (defun org-move-subtree-up (&optional arg)
  5834. "Move the current subtree up past ARG headlines of the same level."
  5835. (interactive "p")
  5836. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5837. (defun org-move-subtree-down (&optional arg)
  5838. "Move the current subtree down past ARG headlines of the same level."
  5839. (interactive "p")
  5840. (setq arg (prefix-numeric-value arg))
  5841. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5842. 'org-get-last-sibling))
  5843. (ins-point (make-marker))
  5844. (cnt (abs arg))
  5845. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5846. ;; Select the tree
  5847. (org-back-to-heading)
  5848. (setq beg0 (point))
  5849. (save-excursion
  5850. (setq ne-beg (org-back-over-empty-lines))
  5851. (setq beg (point)))
  5852. (save-match-data
  5853. (save-excursion (outline-end-of-heading)
  5854. (setq folded (org-invisible-p)))
  5855. (outline-end-of-subtree))
  5856. (outline-next-heading)
  5857. (setq ne-end (org-back-over-empty-lines))
  5858. (setq end (point))
  5859. (goto-char beg0)
  5860. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5861. ;; include less whitespace
  5862. (save-excursion
  5863. (goto-char beg)
  5864. (forward-line (- ne-beg ne-end))
  5865. (setq beg (point))))
  5866. ;; Find insertion point, with error handling
  5867. (while (> cnt 0)
  5868. (or (and (funcall movfunc) (looking-at outline-regexp))
  5869. (progn (goto-char beg0)
  5870. (error "Cannot move past superior level or buffer limit")))
  5871. (setq cnt (1- cnt)))
  5872. (if (> arg 0)
  5873. ;; Moving forward - still need to move over subtree
  5874. (progn (org-end-of-subtree t t)
  5875. (save-excursion
  5876. (org-back-over-empty-lines)
  5877. (or (bolp) (newline)))))
  5878. (setq ne-ins (org-back-over-empty-lines))
  5879. (move-marker ins-point (point))
  5880. (setq txt (buffer-substring beg end))
  5881. (org-save-markers-in-region beg end)
  5882. (delete-region beg end)
  5883. (org-remove-empty-overlays-at beg)
  5884. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5885. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5886. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5887. (let ((bbb (point)))
  5888. (insert-before-markers txt)
  5889. (org-reinstall-markers-in-region bbb)
  5890. (move-marker ins-point bbb))
  5891. (or (bolp) (insert "\n"))
  5892. (setq ins-end (point))
  5893. (goto-char ins-point)
  5894. (org-skip-whitespace)
  5895. (when (and (< arg 0)
  5896. (org-first-sibling-p)
  5897. (> ne-ins ne-beg))
  5898. ;; Move whitespace back to beginning
  5899. (save-excursion
  5900. (goto-char ins-end)
  5901. (let ((kill-whole-line t))
  5902. (kill-line (- ne-ins ne-beg)) (point)))
  5903. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5904. (move-marker ins-point nil)
  5905. (if folded
  5906. (hide-subtree)
  5907. (org-show-entry)
  5908. (show-children)
  5909. (org-cycle-hide-drawers 'children))
  5910. (org-clean-visibility-after-subtree-move)))
  5911. (defvar org-subtree-clip ""
  5912. "Clipboard for cut and paste of subtrees.
  5913. This is actually only a copy of the kill, because we use the normal kill
  5914. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5915. (defvar org-subtree-clip-folded nil
  5916. "Was the last copied subtree folded?
  5917. This is used to fold the tree back after pasting.")
  5918. (defun org-cut-subtree (&optional n)
  5919. "Cut the current subtree into the clipboard.
  5920. With prefix arg N, cut this many sequential subtrees.
  5921. This is a short-hand for marking the subtree and then cutting it."
  5922. (interactive "p")
  5923. (org-copy-subtree n 'cut))
  5924. (defun org-copy-subtree (&optional n cut force-store-markers)
  5925. "Cut the current subtree into the clipboard.
  5926. With prefix arg N, cut this many sequential subtrees.
  5927. This is a short-hand for marking the subtree and then copying it.
  5928. If CUT is non-nil, actually cut the subtree.
  5929. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5930. of some markers in the region, even if CUT is non-nil. This is
  5931. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5932. (interactive "p")
  5933. (let (beg end folded (beg0 (point)))
  5934. (if (interactive-p)
  5935. (org-back-to-heading nil) ; take what looks like a subtree
  5936. (org-back-to-heading t)) ; take what is really there
  5937. (org-back-over-empty-lines)
  5938. (setq beg (point))
  5939. (skip-chars-forward " \t\r\n")
  5940. (save-match-data
  5941. (save-excursion (outline-end-of-heading)
  5942. (setq folded (org-invisible-p)))
  5943. (condition-case nil
  5944. (org-forward-same-level (1- n) t)
  5945. (error nil))
  5946. (org-end-of-subtree t t))
  5947. (org-back-over-empty-lines)
  5948. (setq end (point))
  5949. (goto-char beg0)
  5950. (when (> end beg)
  5951. (setq org-subtree-clip-folded folded)
  5952. (when (or cut force-store-markers)
  5953. (org-save-markers-in-region beg end))
  5954. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5955. (setq org-subtree-clip (current-kill 0))
  5956. (message "%s: Subtree(s) with %d characters"
  5957. (if cut "Cut" "Copied")
  5958. (length org-subtree-clip)))))
  5959. (defun org-paste-subtree (&optional level tree for-yank)
  5960. "Paste the clipboard as a subtree, with modification of headline level.
  5961. The entire subtree is promoted or demoted in order to match a new headline
  5962. level.
  5963. If the cursor is at the beginning of a headline, the same level as
  5964. that headline is used to paste the tree
  5965. If not, the new level is derived from the *visible* headings
  5966. before and after the insertion point, and taken to be the inferior headline
  5967. level of the two. So if the previous visible heading is level 3 and the
  5968. next is level 4 (or vice versa), level 4 will be used for insertion.
  5969. This makes sure that the subtree remains an independent subtree and does
  5970. not swallow low level entries.
  5971. You can also force a different level, either by using a numeric prefix
  5972. argument, or by inserting the heading marker by hand. For example, if the
  5973. cursor is after \"*****\", then the tree will be shifted to level 5.
  5974. If optional TREE is given, use this text instead of the kill ring.
  5975. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5976. move back over whitespace before inserting, and move point to the end of
  5977. the inserted text when done."
  5978. (interactive "P")
  5979. (setq tree (or tree (and kill-ring (current-kill 0))))
  5980. (unless (org-kill-is-subtree-p tree)
  5981. (error "%s"
  5982. (substitute-command-keys
  5983. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5984. (let* ((visp (not (org-invisible-p)))
  5985. (txt tree)
  5986. (^re (concat "^\\(" outline-regexp "\\)"))
  5987. (re (concat "\\(" outline-regexp "\\)"))
  5988. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5989. (old-level (if (string-match ^re txt)
  5990. (- (match-end 0) (match-beginning 0) 1)
  5991. -1))
  5992. (force-level (cond (level (prefix-numeric-value level))
  5993. ((and (looking-at "[ \t]*$")
  5994. (string-match
  5995. ^re_ (buffer-substring
  5996. (point-at-bol) (point))))
  5997. (- (match-end 1) (match-beginning 1)))
  5998. ((and (bolp)
  5999. (looking-at org-outline-regexp))
  6000. (- (match-end 0) (point) 1))
  6001. (t nil)))
  6002. (previous-level (save-excursion
  6003. (condition-case nil
  6004. (progn
  6005. (outline-previous-visible-heading 1)
  6006. (if (looking-at re)
  6007. (- (match-end 0) (match-beginning 0) 1)
  6008. 1))
  6009. (error 1))))
  6010. (next-level (save-excursion
  6011. (condition-case nil
  6012. (progn
  6013. (or (looking-at outline-regexp)
  6014. (outline-next-visible-heading 1))
  6015. (if (looking-at re)
  6016. (- (match-end 0) (match-beginning 0) 1)
  6017. 1))
  6018. (error 1))))
  6019. (new-level (or force-level (max previous-level next-level)))
  6020. (shift (if (or (= old-level -1)
  6021. (= new-level -1)
  6022. (= old-level new-level))
  6023. 0
  6024. (- new-level old-level)))
  6025. (delta (if (> shift 0) -1 1))
  6026. (func (if (> shift 0) 'org-demote 'org-promote))
  6027. (org-odd-levels-only nil)
  6028. beg end newend)
  6029. ;; Remove the forced level indicator
  6030. (if force-level
  6031. (delete-region (point-at-bol) (point)))
  6032. ;; Paste
  6033. (beginning-of-line 1)
  6034. (unless for-yank (org-back-over-empty-lines))
  6035. (setq beg (point))
  6036. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6037. (insert-before-markers txt)
  6038. (unless (string-match "\n\\'" txt) (insert "\n"))
  6039. (setq newend (point))
  6040. (org-reinstall-markers-in-region beg)
  6041. (setq end (point))
  6042. (goto-char beg)
  6043. (skip-chars-forward " \t\n\r")
  6044. (setq beg (point))
  6045. (if (and (org-invisible-p) visp)
  6046. (save-excursion (outline-show-heading)))
  6047. ;; Shift if necessary
  6048. (unless (= shift 0)
  6049. (save-restriction
  6050. (narrow-to-region beg end)
  6051. (while (not (= shift 0))
  6052. (org-map-region func (point-min) (point-max))
  6053. (setq shift (+ delta shift)))
  6054. (goto-char (point-min))
  6055. (setq newend (point-max))))
  6056. (when (or (interactive-p) for-yank)
  6057. (message "Clipboard pasted as level %d subtree" new-level))
  6058. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6059. kill-ring
  6060. (eq org-subtree-clip (current-kill 0))
  6061. org-subtree-clip-folded)
  6062. ;; The tree was folded before it was killed/copied
  6063. (hide-subtree))
  6064. (and for-yank (goto-char newend))))
  6065. (defun org-kill-is-subtree-p (&optional txt)
  6066. "Check if the current kill is an outline subtree, or a set of trees.
  6067. Returns nil if kill does not start with a headline, or if the first
  6068. headline level is not the largest headline level in the tree.
  6069. So this will actually accept several entries of equal levels as well,
  6070. which is OK for `org-paste-subtree'.
  6071. If optional TXT is given, check this string instead of the current kill."
  6072. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6073. (start-level (and kill
  6074. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6075. org-outline-regexp "\\)")
  6076. kill)
  6077. (- (match-end 2) (match-beginning 2) 1)))
  6078. (re (concat "^" org-outline-regexp))
  6079. (start (1+ (or (match-beginning 2) -1))))
  6080. (if (not start-level)
  6081. (progn
  6082. nil) ;; does not even start with a heading
  6083. (catch 'exit
  6084. (while (setq start (string-match re kill (1+ start)))
  6085. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6086. (throw 'exit nil)))
  6087. t))))
  6088. (defvar org-markers-to-move nil
  6089. "Markers that should be moved with a cut-and-paste operation.
  6090. Those markers are stored together with their positions relative to
  6091. the start of the region.")
  6092. (defun org-save-markers-in-region (beg end)
  6093. "Check markers in region.
  6094. If these markers are between BEG and END, record their position relative
  6095. to BEG, so that after moving the block of text, we can put the markers back
  6096. into place.
  6097. This function gets called just before an entry or tree gets cut from the
  6098. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6099. called immediately, to move the markers with the entries."
  6100. (setq org-markers-to-move nil)
  6101. (when (featurep 'org-clock)
  6102. (org-clock-save-markers-for-cut-and-paste beg end))
  6103. (when (featurep 'org-agenda)
  6104. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6105. (defun org-check-and-save-marker (marker beg end)
  6106. "Check if MARKER is between BEG and END.
  6107. If yes, remember the marker and the distance to BEG."
  6108. (when (and (marker-buffer marker)
  6109. (equal (marker-buffer marker) (current-buffer)))
  6110. (if (and (>= marker beg) (< marker end))
  6111. (push (cons marker (- marker beg)) org-markers-to-move))))
  6112. (defun org-reinstall-markers-in-region (beg)
  6113. "Move all remembered markers to their position relative to BEG."
  6114. (mapc (lambda (x)
  6115. (move-marker (car x) (+ beg (cdr x))))
  6116. org-markers-to-move)
  6117. (setq org-markers-to-move nil))
  6118. (defun org-narrow-to-subtree ()
  6119. "Narrow buffer to the current subtree."
  6120. (interactive)
  6121. (save-excursion
  6122. (save-match-data
  6123. (narrow-to-region
  6124. (progn (org-back-to-heading t) (point))
  6125. (progn (org-end-of-subtree t t) (point))))))
  6126. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6127. "Clone the task (subtree) at point N times.
  6128. The clones will be inserted as siblings.
  6129. In interactive use, the user will be prompted for the number of clones
  6130. to be produced, and for a time SHIFT, which may be a repeater as used
  6131. in time stamps, for example `+3d'.
  6132. When a valid repeater is given and the entry contains any time stamps,
  6133. the clones will become a sequence in time, with time stamps in the
  6134. subtree shifted for each clone produced. If SHIFT is nil or the
  6135. empty string, time stamps will be left alone.
  6136. If the original subtree did contain time stamps with a repeater,
  6137. the following will happen:
  6138. - the repeater will be removed in each clone
  6139. - an additional clone will be produced, with the current, unshifted
  6140. date(s) in the entry.
  6141. - the original entry will be placed *after* all the clones, with
  6142. repeater intact.
  6143. - the start days in the repeater in the original entry will be shifted
  6144. to past the last clone.
  6145. I this way you can spell out a number of instances of a repeating task,
  6146. and still retain the repeater to cover future instances of the task."
  6147. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6148. (let (beg end template task
  6149. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6150. (if (not (and (integerp n) (> n 0)))
  6151. (error "Invalid number of replications %s" n))
  6152. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6153. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6154. shift)))
  6155. (error "Invalid shift specification %s" shift))
  6156. (when doshift
  6157. (setq shift-n (string-to-number (match-string 1 shift))
  6158. shift-what (cdr (assoc (match-string 2 shift)
  6159. '(("d" . day) ("w" . week)
  6160. ("m" . month) ("y" . year))))))
  6161. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6162. (setq nmin 1 nmax n)
  6163. (org-back-to-heading t)
  6164. (setq beg (point))
  6165. (org-end-of-subtree t t)
  6166. (or (bolp) (insert "\n"))
  6167. (setq end (point))
  6168. (setq template (buffer-substring beg end))
  6169. (when (and doshift
  6170. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6171. (delete-region beg end)
  6172. (setq end beg)
  6173. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6174. (goto-char end)
  6175. (loop for n from nmin to nmax do
  6176. (if (not doshift)
  6177. (setq task template)
  6178. (with-temp-buffer
  6179. (insert template)
  6180. (org-mode)
  6181. (goto-char (point-min))
  6182. (while (re-search-forward org-ts-regexp-both nil t)
  6183. (org-timestamp-change (* n shift-n) shift-what))
  6184. (unless (= n n-no-remove)
  6185. (goto-char (point-min))
  6186. (while (re-search-forward org-ts-regexp nil t)
  6187. (save-excursion
  6188. (goto-char (match-beginning 0))
  6189. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6190. (delete-region (match-beginning 1) (match-end 1))))))
  6191. (setq task (buffer-string))))
  6192. (insert task))
  6193. (goto-char beg)))
  6194. ;;; Outline Sorting
  6195. (defun org-sort (with-case)
  6196. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6197. Optional argument WITH-CASE means sort case-sensitively.
  6198. With a double prefix argument, also remove duplicate entries."
  6199. (interactive "P")
  6200. (if (org-at-table-p)
  6201. (org-call-with-arg 'org-table-sort-lines with-case)
  6202. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6203. (defun org-sort-remove-invisible (s)
  6204. (remove-text-properties 0 (length s) org-rm-props s)
  6205. (while (string-match org-bracket-link-regexp s)
  6206. (setq s (replace-match (if (match-end 2)
  6207. (match-string 3 s)
  6208. (match-string 1 s)) t t s)))
  6209. s)
  6210. (defvar org-priority-regexp) ; defined later in the file
  6211. (defvar org-after-sorting-entries-or-items-hook nil
  6212. "Hook that is run after a bunch of entries or items have been sorted.
  6213. When children are sorted, the cursor is in the parent line when this
  6214. hook gets called. When a region or a plain list is sorted, the cursor
  6215. will be in the first entry of the sorted region/list.")
  6216. (defun org-sort-entries-or-items
  6217. (&optional with-case sorting-type getkey-func compare-func property)
  6218. "Sort entries on a certain level of an outline tree, or plain list items.
  6219. If there is an active region, the entries in the region are sorted.
  6220. Else, if the cursor is before the first entry, sort the top-level items.
  6221. Else, the children of the entry at point are sorted.
  6222. If the cursor is at the first item in a plain list, the list items will be
  6223. sorted.
  6224. Sorting can be alphabetically, numerically, by date/time as given by
  6225. a time stamp, by a property or by priority.
  6226. The command prompts for the sorting type unless it has been given to the
  6227. function through the SORTING-TYPE argument, which needs to a character,
  6228. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6229. precise meaning of each character:
  6230. n Numerically, by converting the beginning of the entry/item to a number.
  6231. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6232. t By date/time, either the first active time stamp in the entry, or, if
  6233. none exist, by the first inactive one.
  6234. In items, only the first line will be checked.
  6235. s By the scheduled date/time.
  6236. d By deadline date/time.
  6237. c By creation time, which is assumed to be the first inactive time stamp
  6238. at the beginning of a line.
  6239. p By priority according to the cookie.
  6240. r By the value of a property.
  6241. Capital letters will reverse the sort order.
  6242. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6243. called with point at the beginning of the record. It must return either
  6244. a string or a number that should serve as the sorting key for that record.
  6245. Comparing entries ignores case by default. However, with an optional argument
  6246. WITH-CASE, the sorting considers case as well."
  6247. (interactive "P")
  6248. (let ((case-func (if with-case 'identity 'downcase))
  6249. start beg end stars re re2
  6250. txt what tmp plain-list-p)
  6251. ;; Find beginning and end of region to sort
  6252. (cond
  6253. ((org-region-active-p)
  6254. ;; we will sort the region
  6255. (setq end (region-end)
  6256. what "region")
  6257. (goto-char (region-beginning))
  6258. (if (not (org-on-heading-p)) (outline-next-heading))
  6259. (setq start (point)))
  6260. ((org-at-item-p)
  6261. ;; we will sort this plain list
  6262. (org-beginning-of-item-list) (setq start (point))
  6263. (org-end-of-item-list)
  6264. (or (bolp) (insert "\n"))
  6265. (setq end (point))
  6266. (goto-char start)
  6267. (setq plain-list-p t
  6268. what "plain list"))
  6269. ((or (org-on-heading-p)
  6270. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6271. ;; we will sort the children of the current headline
  6272. (org-back-to-heading)
  6273. (setq start (point)
  6274. end (progn (org-end-of-subtree t t)
  6275. (or (bolp) (insert "\n"))
  6276. (org-back-over-empty-lines)
  6277. (point))
  6278. what "children")
  6279. (goto-char start)
  6280. (show-subtree)
  6281. (outline-next-heading))
  6282. (t
  6283. ;; we will sort the top-level entries in this file
  6284. (goto-char (point-min))
  6285. (or (org-on-heading-p) (outline-next-heading))
  6286. (setq start (point))
  6287. (goto-char (point-max))
  6288. (beginning-of-line 1)
  6289. (when (looking-at ".*?\\S-")
  6290. ;; File ends in a non-white line
  6291. (end-of-line 1)
  6292. (insert "\n"))
  6293. (setq end (point-max))
  6294. (setq what "top-level")
  6295. (goto-char start)
  6296. (show-all)))
  6297. (setq beg (point))
  6298. (if (>= beg end) (error "Nothing to sort"))
  6299. (unless plain-list-p
  6300. (looking-at "\\(\\*+\\)")
  6301. (setq stars (match-string 1)
  6302. re (concat "^" (regexp-quote stars) " +")
  6303. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6304. txt (buffer-substring beg end))
  6305. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6306. (if (and (not (equal stars "*")) (string-match re2 txt))
  6307. (error "Region to sort contains a level above the first entry")))
  6308. (unless sorting-type
  6309. (message
  6310. (if plain-list-p
  6311. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6312. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6313. [t]ime [s]cheduled [d]eadline [c]reated
  6314. A/N/T/S/D/C/P/O/F means reversed:")
  6315. what)
  6316. (setq sorting-type (read-char-exclusive))
  6317. (and (= (downcase sorting-type) ?f)
  6318. (setq getkey-func
  6319. (org-icompleting-read "Sort using function: "
  6320. obarray 'fboundp t nil nil))
  6321. (setq getkey-func (intern getkey-func)))
  6322. (and (= (downcase sorting-type) ?r)
  6323. (setq property
  6324. (org-icompleting-read "Property: "
  6325. (mapcar 'list (org-buffer-property-keys t))
  6326. nil t))))
  6327. (message "Sorting entries...")
  6328. (save-restriction
  6329. (narrow-to-region start end)
  6330. (let ((dcst (downcase sorting-type))
  6331. (case-fold-search nil)
  6332. (now (current-time)))
  6333. (sort-subr
  6334. (/= dcst sorting-type)
  6335. ;; This function moves to the beginning character of the "record" to
  6336. ;; be sorted.
  6337. (if plain-list-p
  6338. (lambda nil
  6339. (if (org-at-item-p) t (goto-char (point-max))))
  6340. (lambda nil
  6341. (if (re-search-forward re nil t)
  6342. (goto-char (match-beginning 0))
  6343. (goto-char (point-max)))))
  6344. ;; This function moves to the last character of the "record" being
  6345. ;; sorted.
  6346. (if plain-list-p
  6347. 'org-end-of-item
  6348. (lambda nil
  6349. (save-match-data
  6350. (condition-case nil
  6351. (outline-forward-same-level 1)
  6352. (error
  6353. (goto-char (point-max)))))))
  6354. ;; This function returns the value that gets sorted against.
  6355. (if plain-list-p
  6356. (lambda nil
  6357. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6358. (cond
  6359. ((= dcst ?n)
  6360. (string-to-number (buffer-substring (match-end 0)
  6361. (point-at-eol))))
  6362. ((= dcst ?a)
  6363. (buffer-substring (match-end 0) (point-at-eol)))
  6364. ((= dcst ?t)
  6365. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6366. (re-search-forward org-ts-regexp-both
  6367. (point-at-eol) t))
  6368. (org-time-string-to-seconds (match-string 0))
  6369. (org-float-time now)))
  6370. ((= dcst ?f)
  6371. (if getkey-func
  6372. (progn
  6373. (setq tmp (funcall getkey-func))
  6374. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6375. tmp)
  6376. (error "Invalid key function `%s'" getkey-func)))
  6377. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6378. (lambda nil
  6379. (cond
  6380. ((= dcst ?n)
  6381. (if (looking-at org-complex-heading-regexp)
  6382. (string-to-number (match-string 4))
  6383. nil))
  6384. ((= dcst ?a)
  6385. (if (looking-at org-complex-heading-regexp)
  6386. (funcall case-func (match-string 4))
  6387. nil))
  6388. ((= dcst ?t)
  6389. (let ((end (save-excursion (outline-next-heading) (point))))
  6390. (if (or (re-search-forward org-ts-regexp end t)
  6391. (re-search-forward org-ts-regexp-both end t))
  6392. (org-time-string-to-seconds (match-string 0))
  6393. (org-float-time now))))
  6394. ((= dcst ?c)
  6395. (let ((end (save-excursion (outline-next-heading) (point))))
  6396. (if (re-search-forward
  6397. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6398. end t)
  6399. (org-time-string-to-seconds (match-string 0))
  6400. (org-float-time now))))
  6401. ((= dcst ?s)
  6402. (let ((end (save-excursion (outline-next-heading) (point))))
  6403. (if (re-search-forward org-scheduled-time-regexp end t)
  6404. (org-time-string-to-seconds (match-string 1))
  6405. (org-float-time now))))
  6406. ((= dcst ?d)
  6407. (let ((end (save-excursion (outline-next-heading) (point))))
  6408. (if (re-search-forward org-deadline-time-regexp end t)
  6409. (org-time-string-to-seconds (match-string 1))
  6410. (org-float-time now))))
  6411. ((= dcst ?p)
  6412. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6413. (string-to-char (match-string 2))
  6414. org-default-priority))
  6415. ((= dcst ?r)
  6416. (or (org-entry-get nil property) ""))
  6417. ((= dcst ?o)
  6418. (if (looking-at org-complex-heading-regexp)
  6419. (- 9999 (length (member (match-string 2)
  6420. org-todo-keywords-1)))))
  6421. ((= dcst ?f)
  6422. (if getkey-func
  6423. (progn
  6424. (setq tmp (funcall getkey-func))
  6425. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6426. tmp)
  6427. (error "Invalid key function `%s'" getkey-func)))
  6428. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6429. nil
  6430. (cond
  6431. ((= dcst ?a) 'string<)
  6432. ((= dcst ?f) compare-func)
  6433. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6434. (t nil)))))
  6435. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6436. (message "Sorting entries...done")))
  6437. (defun org-do-sort (table what &optional with-case sorting-type)
  6438. "Sort TABLE of WHAT according to SORTING-TYPE.
  6439. The user will be prompted for the SORTING-TYPE if the call to this
  6440. function does not specify it. WHAT is only for the prompt, to indicate
  6441. what is being sorted. The sorting key will be extracted from
  6442. the car of the elements of the table.
  6443. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6444. (unless sorting-type
  6445. (message
  6446. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6447. what)
  6448. (setq sorting-type (read-char-exclusive)))
  6449. (let ((dcst (downcase sorting-type))
  6450. extractfun comparefun)
  6451. ;; Define the appropriate functions
  6452. (cond
  6453. ((= dcst ?n)
  6454. (setq extractfun 'string-to-number
  6455. comparefun (if (= dcst sorting-type) '< '>)))
  6456. ((= dcst ?a)
  6457. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6458. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6459. comparefun (if (= dcst sorting-type)
  6460. 'string<
  6461. (lambda (a b) (and (not (string< a b))
  6462. (not (string= a b)))))))
  6463. ((= dcst ?t)
  6464. (setq extractfun
  6465. (lambda (x)
  6466. (if (or (string-match org-ts-regexp x)
  6467. (string-match org-ts-regexp-both x))
  6468. (org-float-time
  6469. (org-time-string-to-time (match-string 0 x)))
  6470. 0))
  6471. comparefun (if (= dcst sorting-type) '< '>)))
  6472. (t (error "Invalid sorting type `%c'" sorting-type)))
  6473. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6474. table)
  6475. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6476. ;;; The orgstruct minor mode
  6477. ;; Define a minor mode which can be used in other modes in order to
  6478. ;; integrate the org-mode structure editing commands.
  6479. ;; This is really a hack, because the org-mode structure commands use
  6480. ;; keys which normally belong to the major mode. Here is how it
  6481. ;; works: The minor mode defines all the keys necessary to operate the
  6482. ;; structure commands, but wraps the commands into a function which
  6483. ;; tests if the cursor is currently at a headline or a plain list
  6484. ;; item. If that is the case, the structure command is used,
  6485. ;; temporarily setting many Org-mode variables like regular
  6486. ;; expressions for filling etc. However, when any of those keys is
  6487. ;; used at a different location, function uses `key-binding' to look
  6488. ;; up if the key has an associated command in another currently active
  6489. ;; keymap (minor modes, major mode, global), and executes that
  6490. ;; command. There might be problems if any of the keys is otherwise
  6491. ;; used as a prefix key.
  6492. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6493. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6494. ;; addresses this by checking explicitly for both bindings.
  6495. (defvar orgstruct-mode-map (make-sparse-keymap)
  6496. "Keymap for the minor `orgstruct-mode'.")
  6497. (defvar org-local-vars nil
  6498. "List of local variables, for use by `orgstruct-mode'")
  6499. ;;;###autoload
  6500. (define-minor-mode orgstruct-mode
  6501. "Toggle the minor more `orgstruct-mode'.
  6502. This mode is for using Org-mode structure commands in other modes.
  6503. The following key behave as if Org-mode was active, if the cursor
  6504. is on a headline, or on a plain list item (both in the definition
  6505. of Org-mode).
  6506. M-up Move entry/item up
  6507. M-down Move entry/item down
  6508. M-left Promote
  6509. M-right Demote
  6510. M-S-up Move entry/item up
  6511. M-S-down Move entry/item down
  6512. M-S-left Promote subtree
  6513. M-S-right Demote subtree
  6514. M-q Fill paragraph and items like in Org-mode
  6515. C-c ^ Sort entries
  6516. C-c - Cycle list bullet
  6517. TAB Cycle item visibility
  6518. M-RET Insert new heading/item
  6519. S-M-RET Insert new TODO heading / Checkbox item
  6520. C-c C-c Set tags / toggle checkbox"
  6521. nil " OrgStruct" nil
  6522. (org-load-modules-maybe)
  6523. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6524. ;;;###autoload
  6525. (defun turn-on-orgstruct ()
  6526. "Unconditionally turn on `orgstruct-mode'."
  6527. (orgstruct-mode 1))
  6528. (defun orgstruct++-mode (&optional arg)
  6529. "Toggle `orgstruct-mode', the enhanced version of it.
  6530. In addition to setting orgstruct-mode, this also exports all indentation
  6531. and autofilling variables from org-mode into the buffer. It will also
  6532. recognize item context in multiline items.
  6533. Note that turning off orgstruct-mode will *not* remove the
  6534. indentation/paragraph settings. This can only be done by refreshing the
  6535. major mode, for example with \\[normal-mode]."
  6536. (interactive "P")
  6537. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6538. (if (< arg 1)
  6539. (orgstruct-mode -1)
  6540. (orgstruct-mode 1)
  6541. (let (var val)
  6542. (mapc
  6543. (lambda (x)
  6544. (when (string-match
  6545. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6546. (symbol-name (car x)))
  6547. (setq var (car x) val (nth 1 x))
  6548. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6549. org-local-vars)
  6550. (org-set-local 'orgstruct-is-++ t))))
  6551. (defvar orgstruct-is-++ nil
  6552. "Is orgstruct-mode in ++ version in the current-buffer?")
  6553. (make-variable-buffer-local 'orgstruct-is-++)
  6554. ;;;###autoload
  6555. (defun turn-on-orgstruct++ ()
  6556. "Unconditionally turn on `orgstruct++-mode'."
  6557. (orgstruct++-mode 1))
  6558. (defun orgstruct-error ()
  6559. "Error when there is no default binding for a structure key."
  6560. (interactive)
  6561. (error "This key has no function outside structure elements"))
  6562. (defun orgstruct-setup ()
  6563. "Setup orgstruct keymaps."
  6564. (let ((nfunc 0)
  6565. (bindings
  6566. (list
  6567. '([(meta up)] org-metaup)
  6568. '([(meta down)] org-metadown)
  6569. '([(meta left)] org-metaleft)
  6570. '([(meta right)] org-metaright)
  6571. '([(meta shift up)] org-shiftmetaup)
  6572. '([(meta shift down)] org-shiftmetadown)
  6573. '([(meta shift left)] org-shiftmetaleft)
  6574. '([(meta shift right)] org-shiftmetaright)
  6575. '([?\e (up)] org-metaup)
  6576. '([?\e (down)] org-metadown)
  6577. '([?\e (left)] org-metaleft)
  6578. '([?\e (right)] org-metaright)
  6579. '([?\e (shift up)] org-shiftmetaup)
  6580. '([?\e (shift down)] org-shiftmetadown)
  6581. '([?\e (shift left)] org-shiftmetaleft)
  6582. '([?\e (shift right)] org-shiftmetaright)
  6583. '([(shift up)] org-shiftup)
  6584. '([(shift down)] org-shiftdown)
  6585. '([(shift left)] org-shiftleft)
  6586. '([(shift right)] org-shiftright)
  6587. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6588. '("\M-q" fill-paragraph)
  6589. '("\C-c^" org-sort)
  6590. '("\C-c-" org-cycle-list-bullet)))
  6591. elt key fun cmd)
  6592. (while (setq elt (pop bindings))
  6593. (setq nfunc (1+ nfunc))
  6594. (setq key (org-key (car elt))
  6595. fun (nth 1 elt)
  6596. cmd (orgstruct-make-binding fun nfunc key))
  6597. (org-defkey orgstruct-mode-map key cmd))
  6598. ;; Special treatment needed for TAB and RET
  6599. (org-defkey orgstruct-mode-map [(tab)]
  6600. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6601. (org-defkey orgstruct-mode-map "\C-i"
  6602. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6603. (org-defkey orgstruct-mode-map "\M-\C-m"
  6604. (orgstruct-make-binding 'org-insert-heading 105
  6605. "\M-\C-m" [(meta return)]))
  6606. (org-defkey orgstruct-mode-map [(meta return)]
  6607. (orgstruct-make-binding 'org-insert-heading 106
  6608. [(meta return)] "\M-\C-m"))
  6609. (org-defkey orgstruct-mode-map [(shift meta return)]
  6610. (orgstruct-make-binding 'org-insert-todo-heading 107
  6611. [(meta return)] "\M-\C-m"))
  6612. (org-defkey orgstruct-mode-map "\e\C-m"
  6613. (orgstruct-make-binding 'org-insert-heading 108
  6614. "\e\C-m" [?\e (return)]))
  6615. (org-defkey orgstruct-mode-map [?\e (return)]
  6616. (orgstruct-make-binding 'org-insert-heading 109
  6617. [?\e (return)] "\e\C-m"))
  6618. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6619. (orgstruct-make-binding 'org-insert-todo-heading 110
  6620. [?\e (return)] "\e\C-m"))
  6621. (unless org-local-vars
  6622. (setq org-local-vars (org-get-local-variables)))
  6623. t))
  6624. (defun orgstruct-make-binding (fun n &rest keys)
  6625. "Create a function for binding in the structure minor mode.
  6626. FUN is the command to call inside a table. N is used to create a unique
  6627. command name. KEYS are keys that should be checked in for a command
  6628. to execute outside of tables."
  6629. (eval
  6630. (list 'defun
  6631. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6632. '(arg)
  6633. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6634. "Outside of structure, run the binding of `"
  6635. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6636. "'.")
  6637. '(interactive "p")
  6638. (list 'if
  6639. `(org-context-p 'headline 'item
  6640. (and orgstruct-is-++
  6641. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6642. 'item-body))
  6643. (list 'org-run-like-in-org-mode (list 'quote fun))
  6644. (list 'let '(orgstruct-mode)
  6645. (list 'call-interactively
  6646. (append '(or)
  6647. (mapcar (lambda (k)
  6648. (list 'key-binding k))
  6649. keys)
  6650. '('orgstruct-error))))))))
  6651. (defun org-context-p (&rest contexts)
  6652. "Check if local context is any of CONTEXTS.
  6653. Possible values in the list of contexts are `table', `headline', and `item'."
  6654. (let ((pos (point)))
  6655. (goto-char (point-at-bol))
  6656. (prog1 (or (and (memq 'table contexts)
  6657. (looking-at "[ \t]*|"))
  6658. (and (memq 'headline contexts)
  6659. ;;????????? (looking-at "\\*+"))
  6660. (looking-at outline-regexp))
  6661. (and (memq 'item contexts)
  6662. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6663. (and (memq 'item-body contexts)
  6664. (org-in-item-p)))
  6665. (goto-char pos))))
  6666. (defun org-get-local-variables ()
  6667. "Return a list of all local variables in an org-mode buffer."
  6668. (let (varlist)
  6669. (with-current-buffer (get-buffer-create "*Org tmp*")
  6670. (erase-buffer)
  6671. (org-mode)
  6672. (setq varlist (buffer-local-variables)))
  6673. (kill-buffer "*Org tmp*")
  6674. (delq nil
  6675. (mapcar
  6676. (lambda (x)
  6677. (setq x
  6678. (if (symbolp x)
  6679. (list x)
  6680. (list (car x) (list 'quote (cdr x)))))
  6681. (if (string-match
  6682. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6683. (symbol-name (car x)))
  6684. x nil))
  6685. varlist))))
  6686. ;;;###autoload
  6687. (defun org-run-like-in-org-mode (cmd)
  6688. "Run a command, pretending that the current buffer is in Org-mode.
  6689. This will temporarily bind local variables that are typically bound in
  6690. Org-mode to the values they have in Org-mode, and then interactively
  6691. call CMD."
  6692. (org-load-modules-maybe)
  6693. (unless org-local-vars
  6694. (setq org-local-vars (org-get-local-variables)))
  6695. (eval (list 'let org-local-vars
  6696. (list 'call-interactively (list 'quote cmd)))))
  6697. ;;;; Archiving
  6698. (defun org-get-category (&optional pos)
  6699. "Get the category applying to position POS."
  6700. (get-text-property (or pos (point)) 'org-category))
  6701. (defun org-refresh-category-properties ()
  6702. "Refresh category text properties in the buffer."
  6703. (let ((def-cat (cond
  6704. ((null org-category)
  6705. (if buffer-file-name
  6706. (file-name-sans-extension
  6707. (file-name-nondirectory buffer-file-name))
  6708. "???"))
  6709. ((symbolp org-category) (symbol-name org-category))
  6710. (t org-category)))
  6711. beg end cat pos optionp)
  6712. (org-unmodified
  6713. (save-excursion
  6714. (save-restriction
  6715. (widen)
  6716. (goto-char (point-min))
  6717. (put-text-property (point) (point-max) 'org-category def-cat)
  6718. (while (re-search-forward
  6719. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6720. (setq pos (match-end 0)
  6721. optionp (equal (char-after (match-beginning 0)) ?#)
  6722. cat (org-trim (match-string 2)))
  6723. (if optionp
  6724. (setq beg (point-at-bol) end (point-max))
  6725. (org-back-to-heading t)
  6726. (setq beg (point) end (org-end-of-subtree t t)))
  6727. (put-text-property beg end 'org-category cat)
  6728. (goto-char pos)))))))
  6729. ;;;; Link Stuff
  6730. ;;; Link abbreviations
  6731. (defun org-link-expand-abbrev (link)
  6732. "Apply replacements as defined in `org-link-abbrev-alist."
  6733. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6734. (let* ((key (match-string 1 link))
  6735. (as (or (assoc key org-link-abbrev-alist-local)
  6736. (assoc key org-link-abbrev-alist)))
  6737. (tag (and (match-end 2) (match-string 3 link)))
  6738. rpl)
  6739. (if (not as)
  6740. link
  6741. (setq rpl (cdr as))
  6742. (cond
  6743. ((symbolp rpl) (funcall rpl tag))
  6744. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6745. ((string-match "%h" rpl)
  6746. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6747. (t (concat rpl tag)))))
  6748. link))
  6749. ;;; Storing and inserting links
  6750. (defvar org-insert-link-history nil
  6751. "Minibuffer history for links inserted with `org-insert-link'.")
  6752. (defvar org-stored-links nil
  6753. "Contains the links stored with `org-store-link'.")
  6754. (defvar org-store-link-plist nil
  6755. "Plist with info about the most recently link created with `org-store-link'.")
  6756. (defvar org-link-protocols nil
  6757. "Link protocols added to Org-mode using `org-add-link-type'.")
  6758. (defvar org-store-link-functions nil
  6759. "List of functions that are called to create and store a link.
  6760. Each function will be called in turn until one returns a non-nil
  6761. value. Each function should check if it is responsible for creating
  6762. this link (for example by looking at the major mode).
  6763. If not, it must exit and return nil.
  6764. If yes, it should return a non-nil value after a calling
  6765. `org-store-link-props' with a list of properties and values.
  6766. Special properties are:
  6767. :type The link prefix. like \"http\". This must be given.
  6768. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6769. This is obligatory as well.
  6770. :description Optional default description for the second pair
  6771. of brackets in an Org-mode link. The user can still change
  6772. this when inserting this link into an Org-mode buffer.
  6773. In addition to these, any additional properties can be specified
  6774. and then used in remember templates.")
  6775. (defun org-add-link-type (type &optional follow export)
  6776. "Add TYPE to the list of `org-link-types'.
  6777. Re-compute all regular expressions depending on `org-link-types'
  6778. FOLLOW and EXPORT are two functions.
  6779. FOLLOW should take the link path as the single argument and do whatever
  6780. is necessary to follow the link, for example find a file or display
  6781. a mail message.
  6782. EXPORT should format the link path for export to one of the export formats.
  6783. It should be a function accepting three arguments:
  6784. path the path of the link, the text after the prefix (like \"http:\")
  6785. desc the description of the link, if any, nil if there was no description
  6786. format the export format, a symbol like `html' or `latex'.
  6787. The function may use the FORMAT information to return different values
  6788. depending on the format. The return value will be put literally into
  6789. the exported file.
  6790. Org-mode has a built-in default for exporting links. If you are happy with
  6791. this default, there is no need to define an export function for the link
  6792. type. For a simple example of an export function, see `org-bbdb.el'."
  6793. (add-to-list 'org-link-types type t)
  6794. (org-make-link-regexps)
  6795. (if (assoc type org-link-protocols)
  6796. (setcdr (assoc type org-link-protocols) (list follow export))
  6797. (push (list type follow export) org-link-protocols)))
  6798. (defvar org-agenda-buffer-name)
  6799. ;;;###autoload
  6800. (defun org-store-link (arg)
  6801. "\\<org-mode-map>Store an org-link to the current location.
  6802. This link is added to `org-stored-links' and can later be inserted
  6803. into an org-buffer with \\[org-insert-link].
  6804. For some link types, a prefix arg is interpreted:
  6805. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6806. For file links, arg negates `org-context-in-file-links'."
  6807. (interactive "P")
  6808. (org-load-modules-maybe)
  6809. (setq org-store-link-plist nil) ; reset
  6810. (let ((outline-regexp (org-get-limited-outline-regexp))
  6811. link cpltxt desc description search txt custom-id)
  6812. (cond
  6813. ((run-hook-with-args-until-success 'org-store-link-functions)
  6814. (setq link (plist-get org-store-link-plist :link)
  6815. desc (or (plist-get org-store-link-plist :description) link)))
  6816. ((equal (buffer-name) "*Org Edit Src Example*")
  6817. (let (label gc)
  6818. (while (or (not label)
  6819. (save-excursion
  6820. (save-restriction
  6821. (widen)
  6822. (goto-char (point-min))
  6823. (re-search-forward
  6824. (regexp-quote (format org-coderef-label-format label))
  6825. nil t))))
  6826. (when label (message "Label exists already") (sit-for 2))
  6827. (setq label (read-string "Code line label: " label)))
  6828. (end-of-line 1)
  6829. (setq link (format org-coderef-label-format label))
  6830. (setq gc (- 79 (length link)))
  6831. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6832. (insert link)
  6833. (setq link (concat "(" label ")") desc nil)))
  6834. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6835. ;; We are in the agenda, link to referenced location
  6836. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6837. (get-text-property (point) 'org-marker))))
  6838. (when m
  6839. (org-with-point-at m
  6840. (call-interactively 'org-store-link)))))
  6841. ((eq major-mode 'calendar-mode)
  6842. (let ((cd (calendar-cursor-to-date)))
  6843. (setq link
  6844. (format-time-string
  6845. (car org-time-stamp-formats)
  6846. (apply 'encode-time
  6847. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6848. nil nil nil))))
  6849. (org-store-link-props :type "calendar" :date cd)))
  6850. ((eq major-mode 'w3-mode)
  6851. (setq cpltxt (if (and (buffer-name)
  6852. (not (string-match "Untitled" (buffer-name))))
  6853. (buffer-name)
  6854. (url-view-url t))
  6855. link (org-make-link (url-view-url t)))
  6856. (org-store-link-props :type "w3" :url (url-view-url t)))
  6857. ((eq major-mode 'w3m-mode)
  6858. (setq cpltxt (or w3m-current-title w3m-current-url)
  6859. link (org-make-link w3m-current-url))
  6860. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6861. ((setq search (run-hook-with-args-until-success
  6862. 'org-create-file-search-functions))
  6863. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6864. "::" search))
  6865. (setq cpltxt (or description link)))
  6866. ((eq major-mode 'image-mode)
  6867. (setq cpltxt (concat "file:"
  6868. (abbreviate-file-name buffer-file-name))
  6869. link (org-make-link cpltxt))
  6870. (org-store-link-props :type "image" :file buffer-file-name))
  6871. ((eq major-mode 'dired-mode)
  6872. ;; link to the file in the current line
  6873. (setq cpltxt (concat "file:"
  6874. (abbreviate-file-name
  6875. (expand-file-name
  6876. (dired-get-filename nil t))))
  6877. link (org-make-link cpltxt)))
  6878. ((and buffer-file-name (org-mode-p))
  6879. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6880. (cond
  6881. ((org-in-regexp "<<\\(.*?\\)>>")
  6882. (setq cpltxt
  6883. (concat "file:"
  6884. (abbreviate-file-name buffer-file-name)
  6885. "::" (match-string 1))
  6886. link (org-make-link cpltxt)))
  6887. ((and (featurep 'org-id)
  6888. (or (eq org-link-to-org-use-id t)
  6889. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6890. (interactive-p))
  6891. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6892. (interactive-p)
  6893. (not custom-id))
  6894. (and org-link-to-org-use-id
  6895. (condition-case nil
  6896. (org-entry-get nil "ID")
  6897. (error nil)))))
  6898. ;; We can make a link using the ID.
  6899. (setq link (condition-case nil
  6900. (prog1 (org-id-store-link)
  6901. (setq desc (plist-get org-store-link-plist
  6902. :description)))
  6903. (error
  6904. ;; probably before first headline, link to file only
  6905. (concat "file:"
  6906. (abbreviate-file-name buffer-file-name))))))
  6907. (t
  6908. ;; Just link to current headline
  6909. (setq cpltxt (concat "file:"
  6910. (abbreviate-file-name buffer-file-name)))
  6911. ;; Add a context search string
  6912. (when (org-xor org-context-in-file-links arg)
  6913. (setq txt (cond
  6914. ((org-on-heading-p) nil)
  6915. ((org-region-active-p)
  6916. (buffer-substring (region-beginning) (region-end)))
  6917. (t nil)))
  6918. (when (or (null txt) (string-match "\\S-" txt))
  6919. (setq cpltxt
  6920. (concat cpltxt "::"
  6921. (condition-case nil
  6922. (org-make-org-heading-search-string txt)
  6923. (error "")))
  6924. desc (or (nth 4 (ignore-errors
  6925. (org-heading-components))) "NONE"))))
  6926. (if (string-match "::\\'" cpltxt)
  6927. (setq cpltxt (substring cpltxt 0 -2)))
  6928. (setq link (org-make-link cpltxt)))))
  6929. ((buffer-file-name (buffer-base-buffer))
  6930. ;; Just link to this file here.
  6931. (setq cpltxt (concat "file:"
  6932. (abbreviate-file-name
  6933. (buffer-file-name (buffer-base-buffer)))))
  6934. ;; Add a context string
  6935. (when (org-xor org-context-in-file-links arg)
  6936. (setq txt (if (org-region-active-p)
  6937. (buffer-substring (region-beginning) (region-end))
  6938. (buffer-substring (point-at-bol) (point-at-eol))))
  6939. ;; Only use search option if there is some text.
  6940. (when (string-match "\\S-" txt)
  6941. (setq cpltxt
  6942. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6943. desc "NONE")))
  6944. (setq link (org-make-link cpltxt)))
  6945. ((interactive-p)
  6946. (error "Cannot link to a buffer which is not visiting a file"))
  6947. (t (setq link nil)))
  6948. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6949. (setq link (or link cpltxt)
  6950. desc (or desc cpltxt))
  6951. (if (equal desc "NONE") (setq desc nil))
  6952. (if (and (or (interactive-p) executing-kbd-macro) link)
  6953. (progn
  6954. (setq org-stored-links
  6955. (cons (list link desc) org-stored-links))
  6956. (message "Stored: %s" (or desc link))
  6957. (when custom-id
  6958. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6959. "::#" custom-id))
  6960. (setq org-stored-links
  6961. (cons (list link desc) org-stored-links))))
  6962. (and link (org-make-link-string link desc)))))
  6963. (defun org-store-link-props (&rest plist)
  6964. "Store link properties, extract names and addresses."
  6965. (let (x adr)
  6966. (when (setq x (plist-get plist :from))
  6967. (setq adr (mail-extract-address-components x))
  6968. (setq plist (plist-put plist :fromname (car adr)))
  6969. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6970. (when (setq x (plist-get plist :to))
  6971. (setq adr (mail-extract-address-components x))
  6972. (setq plist (plist-put plist :toname (car adr)))
  6973. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6974. (let ((from (plist-get plist :from))
  6975. (to (plist-get plist :to)))
  6976. (when (and from to org-from-is-user-regexp)
  6977. (setq plist
  6978. (plist-put plist :fromto
  6979. (if (string-match org-from-is-user-regexp from)
  6980. (concat "to %t")
  6981. (concat "from %f"))))))
  6982. (setq org-store-link-plist plist))
  6983. (defun org-add-link-props (&rest plist)
  6984. "Add these properties to the link property list."
  6985. (let (key value)
  6986. (while plist
  6987. (setq key (pop plist) value (pop plist))
  6988. (setq org-store-link-plist
  6989. (plist-put org-store-link-plist key value)))))
  6990. (defun org-email-link-description (&optional fmt)
  6991. "Return the description part of an email link.
  6992. This takes information from `org-store-link-plist' and formats it
  6993. according to FMT (default from `org-email-link-description-format')."
  6994. (setq fmt (or fmt org-email-link-description-format))
  6995. (let* ((p org-store-link-plist)
  6996. (to (plist-get p :toaddress))
  6997. (from (plist-get p :fromaddress))
  6998. (table
  6999. (list
  7000. (cons "%c" (plist-get p :fromto))
  7001. (cons "%F" (plist-get p :from))
  7002. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7003. (cons "%T" (plist-get p :to))
  7004. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7005. (cons "%s" (plist-get p :subject))
  7006. (cons "%m" (plist-get p :message-id)))))
  7007. (when (string-match "%c" fmt)
  7008. ;; Check if the user wrote this message
  7009. (if (and org-from-is-user-regexp from to
  7010. (save-match-data (string-match org-from-is-user-regexp from)))
  7011. (setq fmt (replace-match "to %t" t t fmt))
  7012. (setq fmt (replace-match "from %f" t t fmt))))
  7013. (org-replace-escapes fmt table)))
  7014. (defun org-make-org-heading-search-string (&optional string heading)
  7015. "Make search string for STRING or current headline."
  7016. (interactive)
  7017. (let ((s (or string (org-get-heading))))
  7018. (unless (and string (not heading))
  7019. ;; We are using a headline, clean up garbage in there.
  7020. (if (string-match org-todo-regexp s)
  7021. (setq s (replace-match "" t t s)))
  7022. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7023. (setq s (replace-match "" t t s)))
  7024. (setq s (org-trim s))
  7025. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7026. org-comment-string "\\)") s)
  7027. (setq s (replace-match "" t t s)))
  7028. (while (string-match org-ts-regexp s)
  7029. (setq s (replace-match "" t t s))))
  7030. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7031. (setq s (replace-match " " t t s)))
  7032. (or string (setq s (concat "*" s))) ; Add * for headlines
  7033. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7034. (defun org-make-link (&rest strings)
  7035. "Concatenate STRINGS."
  7036. (apply 'concat strings))
  7037. (defun org-make-link-string (link &optional description)
  7038. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7039. (unless (string-match "\\S-" link)
  7040. (error "Empty link"))
  7041. (when (and description
  7042. (stringp description)
  7043. (not (string-match "\\S-" description)))
  7044. (setq description nil))
  7045. (when (stringp description)
  7046. ;; Remove brackets from the description, they are fatal.
  7047. (while (string-match "\\[" description)
  7048. (setq description (replace-match "{" t t description)))
  7049. (while (string-match "\\]" description)
  7050. (setq description (replace-match "}" t t description))))
  7051. (when (equal (org-link-escape link) description)
  7052. ;; No description needed, it is identical
  7053. (setq description nil))
  7054. (when (and (not description)
  7055. (not (equal link (org-link-escape link))))
  7056. (setq description (org-extract-attributes link)))
  7057. (concat "[[" (org-link-escape link) "]"
  7058. (if description (concat "[" description "]") "")
  7059. "]"))
  7060. (defconst org-link-escape-chars
  7061. '((?\ . "%20")
  7062. (?\[ . "%5B")
  7063. (?\] . "%5D")
  7064. (?\340 . "%E0") ; `a
  7065. (?\342 . "%E2") ; ^a
  7066. (?\347 . "%E7") ; ,c
  7067. (?\350 . "%E8") ; `e
  7068. (?\351 . "%E9") ; 'e
  7069. (?\352 . "%EA") ; ^e
  7070. (?\356 . "%EE") ; ^i
  7071. (?\364 . "%F4") ; ^o
  7072. (?\371 . "%F9") ; `u
  7073. (?\373 . "%FB") ; ^u
  7074. (?\; . "%3B")
  7075. (?? . "%3F")
  7076. (?= . "%3D")
  7077. (?+ . "%2B")
  7078. )
  7079. "Association list of escapes for some characters problematic in links.
  7080. This is the list that is used for internal purposes.")
  7081. (defvar org-url-encoding-use-url-hexify nil)
  7082. (defconst org-link-escape-chars-browser
  7083. '((?\ . "%20")) ; 32 for the SPC char
  7084. "Association list of escapes for some characters problematic in links.
  7085. This is the list that is used before handing over to the browser.")
  7086. (defun org-link-escape (text &optional table)
  7087. "Escape characters in TEXT that are problematic for links."
  7088. (if org-url-encoding-use-url-hexify
  7089. (url-hexify-string text)
  7090. (setq table (or table org-link-escape-chars))
  7091. (when text
  7092. (let ((re (mapconcat (lambda (x) (regexp-quote
  7093. (char-to-string (car x))))
  7094. table "\\|")))
  7095. (while (string-match re text)
  7096. (setq text
  7097. (replace-match
  7098. (cdr (assoc (string-to-char (match-string 0 text))
  7099. table))
  7100. t t text)))
  7101. text))))
  7102. (defun org-link-unescape (text &optional table)
  7103. "Reverse the action of `org-link-escape'."
  7104. (if org-url-encoding-use-url-hexify
  7105. (url-unhex-string text)
  7106. (setq table (or table org-link-escape-chars))
  7107. (when text
  7108. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  7109. table "\\|")))
  7110. (while (string-match re text)
  7111. (setq text
  7112. (replace-match
  7113. (char-to-string (car (rassoc (match-string 0 text) table)))
  7114. t t text)))
  7115. text))))
  7116. (defun org-xor (a b)
  7117. "Exclusive or."
  7118. (if a (not b) b))
  7119. (defun org-fixup-message-id-for-http (s)
  7120. "Replace special characters in a message id, so it can be used in an http query."
  7121. (while (string-match "<" s)
  7122. (setq s (replace-match "%3C" t t s)))
  7123. (while (string-match ">" s)
  7124. (setq s (replace-match "%3E" t t s)))
  7125. (while (string-match "@" s)
  7126. (setq s (replace-match "%40" t t s)))
  7127. s)
  7128. ;;;###autoload
  7129. (defun org-insert-link-global ()
  7130. "Insert a link like Org-mode does.
  7131. This command can be called in any mode to insert a link in Org-mode syntax."
  7132. (interactive)
  7133. (org-load-modules-maybe)
  7134. (org-run-like-in-org-mode 'org-insert-link))
  7135. (defun org-insert-link (&optional complete-file link-location)
  7136. "Insert a link. At the prompt, enter the link.
  7137. Completion can be used to insert any of the link protocol prefixes like
  7138. http or ftp in use.
  7139. The history can be used to select a link previously stored with
  7140. `org-store-link'. When the empty string is entered (i.e. if you just
  7141. press RET at the prompt), the link defaults to the most recently
  7142. stored link. As SPC triggers completion in the minibuffer, you need to
  7143. use M-SPC or C-q SPC to force the insertion of a space character.
  7144. You will also be prompted for a description, and if one is given, it will
  7145. be displayed in the buffer instead of the link.
  7146. If there is already a link at point, this command will allow you to edit link
  7147. and description parts.
  7148. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7149. be selected using completion. The path to the file will be relative to the
  7150. current directory if the file is in the current directory or a subdirectory.
  7151. Otherwise, the link will be the absolute path as completed in the minibuffer
  7152. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7153. option `org-link-file-path-type'.
  7154. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7155. the current directory or below.
  7156. With three \\[universal-argument] prefixes, negate the meaning of
  7157. `org-keep-stored-link-after-insertion'.
  7158. If `org-make-link-description-function' is non-nil, this function will be
  7159. called with the link target, and the result will be the default
  7160. link description.
  7161. If the LINK-LOCATION parameter is non-nil, this value will be
  7162. used as the link location instead of reading one interactively."
  7163. (interactive "P")
  7164. (let* ((wcf (current-window-configuration))
  7165. (region (if (org-region-active-p)
  7166. (buffer-substring (region-beginning) (region-end))))
  7167. (remove (and region (list (region-beginning) (region-end))))
  7168. (desc region)
  7169. tmphist ; byte-compile incorrectly complains about this
  7170. (link link-location)
  7171. entry file all-prefixes)
  7172. (cond
  7173. (link-location) ; specified by arg, just use it.
  7174. ((org-in-regexp org-bracket-link-regexp 1)
  7175. ;; We do have a link at point, and we are going to edit it.
  7176. (setq remove (list (match-beginning 0) (match-end 0)))
  7177. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7178. (setq link (read-string "Link: "
  7179. (org-link-unescape
  7180. (org-match-string-no-properties 1)))))
  7181. ((or (org-in-regexp org-angle-link-re)
  7182. (org-in-regexp org-plain-link-re))
  7183. ;; Convert to bracket link
  7184. (setq remove (list (match-beginning 0) (match-end 0))
  7185. link (read-string "Link: "
  7186. (org-remove-angle-brackets (match-string 0)))))
  7187. ((member complete-file '((4) (16)))
  7188. ;; Completing read for file names.
  7189. (setq link (org-file-complete-link complete-file)))
  7190. (t
  7191. ;; Read link, with completion for stored links.
  7192. (with-output-to-temp-buffer "*Org Links*"
  7193. (princ "Insert a link.
  7194. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7195. (when org-stored-links
  7196. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7197. (princ (mapconcat
  7198. (lambda (x)
  7199. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7200. (reverse org-stored-links) "\n"))))
  7201. (let ((cw (selected-window)))
  7202. (select-window (get-buffer-window "*Org Links*"))
  7203. (setq truncate-lines t)
  7204. (unless (pos-visible-in-window-p (point-max))
  7205. (org-fit-window-to-buffer))
  7206. (and (window-live-p cw) (select-window cw)))
  7207. ;; Fake a link history, containing the stored links.
  7208. (setq tmphist (append (mapcar 'car org-stored-links)
  7209. org-insert-link-history))
  7210. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7211. (mapcar 'car org-link-abbrev-alist)
  7212. org-link-types))
  7213. (unwind-protect
  7214. (progn
  7215. (setq link
  7216. (let ((org-completion-use-ido nil)
  7217. (org-completion-use-iswitchb nil))
  7218. (org-completing-read
  7219. "Link: "
  7220. (append
  7221. (mapcar (lambda (x) (list (concat x ":")))
  7222. all-prefixes)
  7223. (mapcar 'car org-stored-links))
  7224. nil nil nil
  7225. 'tmphist
  7226. (car (car org-stored-links)))))
  7227. (if (not (string-match "\\S-" link))
  7228. (error "No link selected"))
  7229. (if (or (member link all-prefixes)
  7230. (and (equal ":" (substring link -1))
  7231. (member (substring link 0 -1) all-prefixes)
  7232. (setq link (substring link 0 -1))))
  7233. (setq link (org-link-try-special-completion link))))
  7234. (set-window-configuration wcf)
  7235. (kill-buffer "*Org Links*"))
  7236. (setq entry (assoc link org-stored-links))
  7237. (or entry (push link org-insert-link-history))
  7238. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7239. (not org-keep-stored-link-after-insertion))
  7240. (setq org-stored-links (delq (assoc link org-stored-links)
  7241. org-stored-links)))
  7242. (setq desc (or desc (nth 1 entry)))))
  7243. (if (string-match org-plain-link-re link)
  7244. ;; URL-like link, normalize the use of angular brackets.
  7245. (setq link (org-make-link (org-remove-angle-brackets link))))
  7246. ;; Check if we are linking to the current file with a search option
  7247. ;; If yes, simplify the link by using only the search option.
  7248. (when (and buffer-file-name
  7249. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7250. (let* ((path (match-string 1 link))
  7251. (case-fold-search nil)
  7252. (search (match-string 2 link)))
  7253. (save-match-data
  7254. (if (equal (file-truename buffer-file-name) (file-truename path))
  7255. ;; We are linking to this same file, with a search option
  7256. (setq link search)))))
  7257. ;; Check if we can/should use a relative path. If yes, simplify the link
  7258. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7259. (let* ((type (match-string 1 link))
  7260. (path (match-string 2 link))
  7261. (origpath path)
  7262. (case-fold-search nil))
  7263. (cond
  7264. ((or (eq org-link-file-path-type 'absolute)
  7265. (equal complete-file '(16)))
  7266. (setq path (abbreviate-file-name (expand-file-name path))))
  7267. ((eq org-link-file-path-type 'noabbrev)
  7268. (setq path (expand-file-name path)))
  7269. ((eq org-link-file-path-type 'relative)
  7270. (setq path (file-relative-name path)))
  7271. (t
  7272. (save-match-data
  7273. (if (string-match (concat "^" (regexp-quote
  7274. (file-name-as-directory
  7275. (expand-file-name "."))))
  7276. (expand-file-name path))
  7277. ;; We are linking a file with relative path name.
  7278. (setq path (substring (expand-file-name path)
  7279. (match-end 0)))
  7280. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7281. (setq link (concat type path))
  7282. (if (equal desc origpath)
  7283. (setq desc path))))
  7284. (if org-make-link-description-function
  7285. (setq desc (funcall org-make-link-description-function link desc)))
  7286. (setq desc (read-string "Description: " desc))
  7287. (unless (string-match "\\S-" desc) (setq desc nil))
  7288. (if remove (apply 'delete-region remove))
  7289. (insert (org-make-link-string link desc))))
  7290. (defun org-link-try-special-completion (type)
  7291. "If there is completion support for link type TYPE, offer it."
  7292. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7293. (if (functionp fun)
  7294. (funcall fun)
  7295. (read-string "Link (no completion support): " (concat type ":")))))
  7296. (defun org-file-complete-link (&optional arg)
  7297. "Create a file link using completion."
  7298. (let (file link)
  7299. (setq file (read-file-name "File: "))
  7300. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7301. (pwd1 (file-name-as-directory (abbreviate-file-name
  7302. (expand-file-name ".")))))
  7303. (cond
  7304. ((equal arg '(16))
  7305. (setq link (org-make-link
  7306. "file:"
  7307. (abbreviate-file-name (expand-file-name file)))))
  7308. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7309. (setq link (org-make-link "file:" (match-string 1 file))))
  7310. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7311. (expand-file-name file))
  7312. (setq link (org-make-link
  7313. "file:" (match-string 1 (expand-file-name file)))))
  7314. (t (setq link (org-make-link "file:" file)))))
  7315. link))
  7316. (defun org-completing-read (&rest args)
  7317. "Completing-read with SPACE being a normal character."
  7318. (let ((minibuffer-local-completion-map
  7319. (copy-keymap minibuffer-local-completion-map)))
  7320. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7321. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7322. (apply 'org-icompleting-read args)))
  7323. (defun org-completing-read-no-i (&rest args)
  7324. (let (org-completion-use-ido org-completion-use-iswitchb)
  7325. (apply 'org-completing-read args)))
  7326. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7327. "Use iswitch as a completing-read replacement to choose from choices.
  7328. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7329. from."
  7330. (let* ((iswitchb-use-virtual-buffers nil)
  7331. (iswitchb-make-buflist-hook
  7332. (lambda ()
  7333. (setq iswitchb-temp-buflist choices))))
  7334. (iswitchb-read-buffer prompt)))
  7335. (defun org-icompleting-read (&rest args)
  7336. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7337. (org-without-partial-completion
  7338. (if (and org-completion-use-ido
  7339. (fboundp 'ido-completing-read)
  7340. (boundp 'ido-mode) ido-mode
  7341. (listp (second args)))
  7342. (let ((ido-enter-matching-directory nil))
  7343. (apply 'ido-completing-read (concat (car args))
  7344. (if (consp (car (nth 1 args)))
  7345. (mapcar (lambda (x) (car x)) (nth 1 args))
  7346. (nth 1 args))
  7347. (cddr args)))
  7348. (if (and org-completion-use-iswitchb
  7349. (boundp 'iswitchb-mode) iswitchb-mode
  7350. (listp (second args)))
  7351. (apply 'org-iswitchb-completing-read (concat (car args))
  7352. (if (consp (car (nth 1 args)))
  7353. (mapcar (lambda (x) (car x)) (nth 1 args))
  7354. (nth 1 args))
  7355. (cddr args))
  7356. (apply 'completing-read args)))))
  7357. (defun org-extract-attributes (s)
  7358. "Extract the attributes cookie from a string and set as text property."
  7359. (let (a attr (start 0) key value)
  7360. (save-match-data
  7361. (when (string-match "{{\\([^}]+\\)}}$" s)
  7362. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7363. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7364. (setq key (match-string 1 a) value (match-string 2 a)
  7365. start (match-end 0)
  7366. attr (plist-put attr (intern key) value))))
  7367. (org-add-props s nil 'org-attr attr))
  7368. s))
  7369. (defun org-extract-attributes-from-string (tag)
  7370. (let (key value attr)
  7371. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7372. (setq key (match-string 1 tag) value (match-string 2 tag)
  7373. tag (replace-match "" t t tag)
  7374. attr (plist-put attr (intern key) value)))
  7375. (cons tag attr)))
  7376. (defun org-attributes-to-string (plist)
  7377. "Format a property list into an HTML attribute list."
  7378. (let ((s "") key value)
  7379. (while plist
  7380. (setq key (pop plist) value (pop plist))
  7381. (and value
  7382. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7383. s))
  7384. ;;; Opening/following a link
  7385. (defvar org-link-search-failed nil)
  7386. (defvar org-open-link-functions nil
  7387. "Hook for functions finding a plain text link.
  7388. These functions must take a single argument, the link content.
  7389. They will be called for links that look like [[link text][description]]
  7390. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7391. like a filename (e.g. \"./blue.png\").
  7392. These functions will be called *before* Org attempts to resolve the
  7393. link by doing text searches in the current buffer - so if you want a
  7394. link \"[[target]]\" to still find \"<<target>>\", your function should
  7395. handle this as a special case.
  7396. When the function does handle the link, it must return a non-nil value.
  7397. If it decides that it is not responsible for this link, it must return
  7398. nil to indicate that that Org-mode can continue with other options
  7399. like exact and fuzzy text search.")
  7400. (defun org-next-link ()
  7401. "Move forward to the next link.
  7402. If the link is in hidden text, expose it."
  7403. (interactive)
  7404. (when (and org-link-search-failed (eq this-command last-command))
  7405. (goto-char (point-min))
  7406. (message "Link search wrapped back to beginning of buffer"))
  7407. (setq org-link-search-failed nil)
  7408. (let* ((pos (point))
  7409. (ct (org-context))
  7410. (a (assoc :link ct)))
  7411. (if a (goto-char (nth 2 a)))
  7412. (if (re-search-forward org-any-link-re nil t)
  7413. (progn
  7414. (goto-char (match-beginning 0))
  7415. (if (org-invisible-p) (org-show-context)))
  7416. (goto-char pos)
  7417. (setq org-link-search-failed t)
  7418. (error "No further link found"))))
  7419. (defun org-previous-link ()
  7420. "Move backward to the previous link.
  7421. If the link is in hidden text, expose it."
  7422. (interactive)
  7423. (when (and org-link-search-failed (eq this-command last-command))
  7424. (goto-char (point-max))
  7425. (message "Link search wrapped back to end of buffer"))
  7426. (setq org-link-search-failed nil)
  7427. (let* ((pos (point))
  7428. (ct (org-context))
  7429. (a (assoc :link ct)))
  7430. (if a (goto-char (nth 1 a)))
  7431. (if (re-search-backward org-any-link-re nil t)
  7432. (progn
  7433. (goto-char (match-beginning 0))
  7434. (if (org-invisible-p) (org-show-context)))
  7435. (goto-char pos)
  7436. (setq org-link-search-failed t)
  7437. (error "No further link found"))))
  7438. (defun org-translate-link (s)
  7439. "Translate a link string if a translation function has been defined."
  7440. (if (and org-link-translation-function
  7441. (fboundp org-link-translation-function)
  7442. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7443. (progn
  7444. (setq s (funcall org-link-translation-function
  7445. (match-string 1) (match-string 2)))
  7446. (concat (car s) ":" (cdr s)))
  7447. s))
  7448. (defun org-translate-link-from-planner (type path)
  7449. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7450. This is still an experimental function, your mileage may vary."
  7451. (cond
  7452. ((member type '("http" "https" "news" "ftp"))
  7453. ;; standard Internet links are the same.
  7454. nil)
  7455. ((and (equal type "irc") (string-match "^//" path))
  7456. ;; Planner has two / at the beginning of an irc link, we have 1.
  7457. ;; We should have zero, actually....
  7458. (setq path (substring path 1)))
  7459. ((and (equal type "lisp") (string-match "^/" path))
  7460. ;; Planner has a slash, we do not.
  7461. (setq type "elisp" path (substring path 1)))
  7462. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7463. ;; A typical message link. Planner has the id after the final slash,
  7464. ;; we separate it with a hash mark
  7465. (setq path (concat (match-string 1 path) "#"
  7466. (org-remove-angle-brackets (match-string 2 path)))))
  7467. )
  7468. (cons type path))
  7469. (defun org-find-file-at-mouse (ev)
  7470. "Open file link or URL at mouse."
  7471. (interactive "e")
  7472. (mouse-set-point ev)
  7473. (org-open-at-point 'in-emacs))
  7474. (defun org-open-at-mouse (ev)
  7475. "Open file link or URL at mouse."
  7476. (interactive "e")
  7477. (mouse-set-point ev)
  7478. (if (eq major-mode 'org-agenda-mode)
  7479. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7480. (org-open-at-point))
  7481. (defvar org-window-config-before-follow-link nil
  7482. "The window configuration before following a link.
  7483. This is saved in case the need arises to restore it.")
  7484. (defvar org-open-link-marker (make-marker)
  7485. "Marker pointing to the location where `org-open-at-point; was called.")
  7486. ;;;###autoload
  7487. (defun org-open-at-point-global ()
  7488. "Follow a link like Org-mode does.
  7489. This command can be called in any mode to follow a link that has
  7490. Org-mode syntax."
  7491. (interactive)
  7492. (org-run-like-in-org-mode 'org-open-at-point))
  7493. ;;;###autoload
  7494. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7495. "Open a link in the string S, as if it was in Org-mode."
  7496. (interactive "sLink: \nP")
  7497. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7498. (with-temp-buffer
  7499. (let ((org-inhibit-startup t))
  7500. (org-mode)
  7501. (insert s)
  7502. (goto-char (point-min))
  7503. (org-open-at-point arg reference-buffer)))))
  7504. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7505. "Open link at or after point.
  7506. If there is no link at point, this function will search forward up to
  7507. the end of the current line.
  7508. Normally, files will be opened by an appropriate application. If the
  7509. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7510. With a double prefix argument, try to open outside of Emacs, in the
  7511. application the system uses for this file type."
  7512. (interactive "P")
  7513. (org-load-modules-maybe)
  7514. (move-marker org-open-link-marker (point))
  7515. (setq org-window-config-before-follow-link (current-window-configuration))
  7516. (org-remove-occur-highlights nil nil t)
  7517. (cond
  7518. ((and (org-on-heading-p)
  7519. (not (org-in-regexp
  7520. (concat org-plain-link-re "\\|"
  7521. org-bracket-link-regexp "\\|"
  7522. org-angle-link-re "\\|"
  7523. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7524. (or (org-offer-links-in-entry in-emacs)
  7525. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7526. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7527. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7528. (org-footnote-action))
  7529. (t
  7530. (let (type path link line search (pos (point)))
  7531. (catch 'match
  7532. (save-excursion
  7533. (skip-chars-forward "^]\n\r")
  7534. (when (org-in-regexp org-bracket-link-regexp 1)
  7535. (setq link (org-extract-attributes
  7536. (org-link-unescape (org-match-string-no-properties 1))))
  7537. (while (string-match " *\n *" link)
  7538. (setq link (replace-match " " t t link)))
  7539. (setq link (org-link-expand-abbrev link))
  7540. (cond
  7541. ((or (file-name-absolute-p link)
  7542. (string-match "^\\.\\.?/" link))
  7543. (setq type "file" path link))
  7544. ((string-match org-link-re-with-space3 link)
  7545. (setq type (match-string 1 link) path (match-string 2 link)))
  7546. (t (setq type "thisfile" path link)))
  7547. (throw 'match t)))
  7548. (when (get-text-property (point) 'org-linked-text)
  7549. (setq type "thisfile"
  7550. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7551. (1+ (point)) (point))
  7552. path (buffer-substring
  7553. (previous-single-property-change pos 'org-linked-text)
  7554. (next-single-property-change pos 'org-linked-text)))
  7555. (throw 'match t))
  7556. (save-excursion
  7557. (when (or (org-in-regexp org-angle-link-re)
  7558. (org-in-regexp org-plain-link-re))
  7559. (setq type (match-string 1) path (match-string 2))
  7560. (throw 'match t)))
  7561. (save-excursion
  7562. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7563. (setq type "tags"
  7564. path (match-string 1))
  7565. (while (string-match ":" path)
  7566. (setq path (replace-match "+" t t path)))
  7567. (throw 'match t)))
  7568. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7569. (setq type "tree-match"
  7570. path (match-string 1))
  7571. (throw 'match t)))
  7572. (unless path
  7573. (error "No link found"))
  7574. ;; switch back to reference buffer
  7575. ;; needed when if called in a temporary buffer through
  7576. ;; org-open-link-from-string
  7577. (with-current-buffer (or reference-buffer (current-buffer))
  7578. ;; Remove any trailing spaces in path
  7579. (if (string-match " +\\'" path)
  7580. (setq path (replace-match "" t t path)))
  7581. (if (and org-link-translation-function
  7582. (fboundp org-link-translation-function))
  7583. ;; Check if we need to translate the link
  7584. (let ((tmp (funcall org-link-translation-function type path)))
  7585. (setq type (car tmp) path (cdr tmp))))
  7586. (cond
  7587. ((assoc type org-link-protocols)
  7588. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7589. ((equal type "mailto")
  7590. (let ((cmd (car org-link-mailto-program))
  7591. (args (cdr org-link-mailto-program)) args1
  7592. (address path) (subject "") a)
  7593. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7594. (setq address (match-string 1 path)
  7595. subject (org-link-escape (match-string 2 path))))
  7596. (while args
  7597. (cond
  7598. ((not (stringp (car args))) (push (pop args) args1))
  7599. (t (setq a (pop args))
  7600. (if (string-match "%a" a)
  7601. (setq a (replace-match address t t a)))
  7602. (if (string-match "%s" a)
  7603. (setq a (replace-match subject t t a)))
  7604. (push a args1))))
  7605. (apply cmd (nreverse args1))))
  7606. ((member type '("http" "https" "ftp" "news"))
  7607. (browse-url (concat type ":" (org-link-escape
  7608. path org-link-escape-chars-browser))))
  7609. ((member type '("message"))
  7610. (browse-url (concat type ":" path)))
  7611. ((string= type "tags")
  7612. (org-tags-view in-emacs path))
  7613. ((string= type "tree-match")
  7614. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7615. ((string= type "file")
  7616. (if (string-match "::\\([0-9]+\\)\\'" path)
  7617. (setq line (string-to-number (match-string 1 path))
  7618. path (substring path 0 (match-beginning 0)))
  7619. (if (string-match "::\\(.+\\)\\'" path)
  7620. (setq search (match-string 1 path)
  7621. path (substring path 0 (match-beginning 0)))))
  7622. (if (string-match "[*?{]" (file-name-nondirectory path))
  7623. (dired path)
  7624. (org-open-file path in-emacs line search)))
  7625. ((string= type "news")
  7626. (require 'org-gnus)
  7627. (org-gnus-follow-link path))
  7628. ((string= type "shell")
  7629. (let ((cmd path))
  7630. (if (or (not org-confirm-shell-link-function)
  7631. (funcall org-confirm-shell-link-function
  7632. (format "Execute \"%s\" in shell? "
  7633. (org-add-props cmd nil
  7634. 'face 'org-warning))))
  7635. (progn
  7636. (message "Executing %s" cmd)
  7637. (shell-command cmd))
  7638. (error "Abort"))))
  7639. ((string= type "elisp")
  7640. (let ((cmd path))
  7641. (if (or (not org-confirm-elisp-link-function)
  7642. (funcall org-confirm-elisp-link-function
  7643. (format "Execute \"%s\" as elisp? "
  7644. (org-add-props cmd nil
  7645. 'face 'org-warning))))
  7646. (message "%s => %s" cmd
  7647. (if (equal (string-to-char cmd) ?\()
  7648. (eval (read cmd))
  7649. (call-interactively (read cmd))))
  7650. (error "Abort"))))
  7651. ((and (string= type "thisfile")
  7652. (run-hook-with-args-until-success
  7653. 'org-open-link-functions path)))
  7654. ((string= type "thisfile")
  7655. (if in-emacs
  7656. (switch-to-buffer-other-window
  7657. (org-get-buffer-for-internal-link (current-buffer)))
  7658. (org-mark-ring-push))
  7659. (let ((cmd `(org-link-search
  7660. ,path
  7661. ,(cond ((equal in-emacs '(4)) 'occur)
  7662. ((equal in-emacs '(16)) 'org-occur)
  7663. (t nil))
  7664. ,pos)))
  7665. (condition-case nil (eval cmd)
  7666. (error (progn (widen) (eval cmd))))))
  7667. (t
  7668. (browse-url-at-point)))))))
  7669. (move-marker org-open-link-marker nil)
  7670. (run-hook-with-args 'org-follow-link-hook))
  7671. (defun org-offer-links-in-entry (&optional nth zero)
  7672. "Offer links in the current entry and follow the selected link.
  7673. If there is only one link, follow it immediately as well.
  7674. If NTH is an integer, immediately pick the NTH link found.
  7675. If ZERO is a string, check also this string for a link, and if
  7676. there is one, offer it as link number zero."
  7677. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7678. "\\(" org-angle-link-re "\\)\\|"
  7679. "\\(" org-plain-link-re "\\)"))
  7680. (cnt ?0)
  7681. (in-emacs (if (integerp nth) nil nth))
  7682. have-zero end links link c)
  7683. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7684. (push (match-string 0 zero) links)
  7685. (setq cnt (1- cnt) have-zero t))
  7686. (save-excursion
  7687. (org-back-to-heading t)
  7688. (setq end (save-excursion (outline-next-heading) (point)))
  7689. (while (re-search-forward re end t)
  7690. (push (match-string 0) links))
  7691. (setq links (org-uniquify (reverse links))))
  7692. (cond
  7693. ((null links)
  7694. (message "No links"))
  7695. ((equal (length links) 1)
  7696. (setq link (list (car links))))
  7697. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7698. (setq link (nth (if have-zero nth (1- nth)) links)))
  7699. (t ; we have to select a link
  7700. (save-excursion
  7701. (save-window-excursion
  7702. (delete-other-windows)
  7703. (with-output-to-temp-buffer "*Select Link*"
  7704. (mapc (lambda (l)
  7705. (if (not (string-match org-bracket-link-regexp l))
  7706. (princ (format "[%c] %s\n" (incf cnt)
  7707. (org-remove-angle-brackets l)))
  7708. (if (match-end 3)
  7709. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7710. (match-string 3 l) (match-string 1 l)))
  7711. (princ (format "[%c] %s\n" (incf cnt)
  7712. (match-string 1 l))))))
  7713. links))
  7714. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7715. (message "Select link to open, RET to open all:")
  7716. (setq c (read-char-exclusive))
  7717. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7718. (when (equal c ?q) (error "Abort"))
  7719. (if (equal c ?\C-m)
  7720. (setq link links)
  7721. (setq nth (- c ?0))
  7722. (if have-zero (setq nth (1+ nth)))
  7723. (unless (and (integerp nth) (>= (length links) nth))
  7724. (error "Invalid link selection"))
  7725. (setq link (list (nth (1- nth) links))))))
  7726. (if link
  7727. (let ((buf (current-buffer)))
  7728. (dolist (l link)
  7729. (org-open-link-from-string l in-emacs buf))
  7730. t)
  7731. nil)))
  7732. ;; Add special file links that specify the way of opening
  7733. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7734. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7735. (defun org-open-file-with-system (path)
  7736. "Open file at PATH using the system way of opeing it."
  7737. (org-open-file path 'system))
  7738. (defun org-open-file-with-emacs (path)
  7739. "Open file at PATH in emacs."
  7740. (org-open-file path 'emacs))
  7741. (defun org-remove-file-link-modifiers ()
  7742. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7743. (goto-char (point-min))
  7744. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7745. (org-if-unprotected
  7746. (replace-match "file:" t t))))
  7747. (eval-after-load "org-exp"
  7748. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7749. 'org-remove-file-link-modifiers))
  7750. ;;;; Time estimates
  7751. (defun org-get-effort (&optional pom)
  7752. "Get the effort estimate for the current entry."
  7753. (org-entry-get pom org-effort-property))
  7754. ;;; File search
  7755. (defvar org-create-file-search-functions nil
  7756. "List of functions to construct the right search string for a file link.
  7757. These functions are called in turn with point at the location to
  7758. which the link should point.
  7759. A function in the hook should first test if it would like to
  7760. handle this file type, for example by checking the major-mode or
  7761. the file extension. If it decides not to handle this file, it
  7762. should just return nil to give other functions a chance. If it
  7763. does handle the file, it must return the search string to be used
  7764. when following the link. The search string will be part of the
  7765. file link, given after a double colon, and `org-open-at-point'
  7766. will automatically search for it. If special measures must be
  7767. taken to make the search successful, another function should be
  7768. added to the companion hook `org-execute-file-search-functions',
  7769. which see.
  7770. A function in this hook may also use `setq' to set the variable
  7771. `description' to provide a suggestion for the descriptive text to
  7772. be used for this link when it gets inserted into an Org-mode
  7773. buffer with \\[org-insert-link].")
  7774. (defvar org-execute-file-search-functions nil
  7775. "List of functions to execute a file search triggered by a link.
  7776. Functions added to this hook must accept a single argument, the
  7777. search string that was part of the file link, the part after the
  7778. double colon. The function must first check if it would like to
  7779. handle this search, for example by checking the major-mode or the
  7780. file extension. If it decides not to handle this search, it
  7781. should just return nil to give other functions a chance. If it
  7782. does handle the search, it must return a non-nil value to keep
  7783. other functions from trying.
  7784. Each function can access the current prefix argument through the
  7785. variable `current-prefix-argument'. Note that a single prefix is
  7786. used to force opening a link in Emacs, so it may be good to only
  7787. use a numeric or double prefix to guide the search function.
  7788. In case this is needed, a function in this hook can also restore
  7789. the window configuration before `org-open-at-point' was called using:
  7790. (set-window-configuration org-window-config-before-follow-link)")
  7791. (defun org-link-search (s &optional type avoid-pos)
  7792. "Search for a link search option.
  7793. If S is surrounded by forward slashes, it is interpreted as a
  7794. regular expression. In org-mode files, this will create an `org-occur'
  7795. sparse tree. In ordinary files, `occur' will be used to list matches.
  7796. If the current buffer is in `dired-mode', grep will be used to search
  7797. in all files. If AVOID-POS is given, ignore matches near that position."
  7798. (let ((case-fold-search t)
  7799. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7800. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7801. (append '(("") (" ") ("\t") ("\n"))
  7802. org-emphasis-alist)
  7803. "\\|") "\\)"))
  7804. (pos (point))
  7805. (pre nil) (post nil)
  7806. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7807. (cond
  7808. ;; First check if there are any special
  7809. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7810. ;; Now try the builtin stuff
  7811. ((and (equal (string-to-char s0) ?#)
  7812. (> (length s0) 1)
  7813. (save-excursion
  7814. (goto-char (point-min))
  7815. (and
  7816. (re-search-forward
  7817. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7818. (setq type 'dedicated
  7819. pos (match-beginning 0))))
  7820. ;; There is an exact target for this
  7821. (goto-char pos)
  7822. (org-back-to-heading t)))
  7823. ((save-excursion
  7824. (goto-char (point-min))
  7825. (and
  7826. (re-search-forward
  7827. (concat "<<" (regexp-quote s0) ">>") nil t)
  7828. (setq type 'dedicated
  7829. pos (match-beginning 0))))
  7830. ;; There is an exact target for this
  7831. (goto-char pos))
  7832. ((and (string-match "^(\\(.*\\))$" s0)
  7833. (save-excursion
  7834. (goto-char (point-min))
  7835. (and
  7836. (re-search-forward
  7837. (concat "[^[]" (regexp-quote
  7838. (format org-coderef-label-format
  7839. (match-string 1 s0))))
  7840. nil t)
  7841. (setq type 'dedicated
  7842. pos (1+ (match-beginning 0))))))
  7843. ;; There is a coderef target for this
  7844. (goto-char pos))
  7845. ((string-match "^/\\(.*\\)/$" s)
  7846. ;; A regular expression
  7847. (cond
  7848. ((org-mode-p)
  7849. (org-occur (match-string 1 s)))
  7850. ;;((eq major-mode 'dired-mode)
  7851. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7852. (t (org-do-occur (match-string 1 s)))))
  7853. (t
  7854. ;; A normal search strings
  7855. (when (equal (string-to-char s) ?*)
  7856. ;; Anchor on headlines, post may include tags.
  7857. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7858. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7859. s (substring s 1)))
  7860. (remove-text-properties
  7861. 0 (length s)
  7862. '(face nil mouse-face nil keymap nil fontified nil) s)
  7863. ;; Make a series of regular expressions to find a match
  7864. (setq words (org-split-string s "[ \n\r\t]+")
  7865. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7866. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7867. "\\)" markers)
  7868. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7869. re2a (concat "[ \t\r\n]" re2a_)
  7870. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7871. re4 (concat "[^a-zA-Z_]" re4_)
  7872. re1 (concat pre re2 post)
  7873. re3 (concat pre (if pre re4_ re4) post)
  7874. re5 (concat pre ".*" re4)
  7875. re2 (concat pre re2)
  7876. re2a (concat pre (if pre re2a_ re2a))
  7877. re4 (concat pre (if pre re4_ re4))
  7878. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7879. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7880. re5 "\\)"
  7881. ))
  7882. (cond
  7883. ((eq type 'org-occur) (org-occur reall))
  7884. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7885. (t (goto-char (point-min))
  7886. (setq type 'fuzzy)
  7887. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7888. (org-search-not-self 1 re1 nil t)
  7889. (org-search-not-self 1 re2 nil t)
  7890. (org-search-not-self 1 re2a nil t)
  7891. (org-search-not-self 1 re3 nil t)
  7892. (org-search-not-self 1 re4 nil t)
  7893. (org-search-not-self 1 re5 nil t)
  7894. )
  7895. (goto-char (match-beginning 1))
  7896. (goto-char pos)
  7897. (error "No match")))))
  7898. (t
  7899. ;; Normal string-search
  7900. (goto-char (point-min))
  7901. (if (search-forward s nil t)
  7902. (goto-char (match-beginning 0))
  7903. (error "No match"))))
  7904. (and (org-mode-p) (org-show-context 'link-search))
  7905. type))
  7906. (defun org-search-not-self (group &rest args)
  7907. "Execute `re-search-forward', but only accept matches that do not
  7908. enclose the position of `org-open-link-marker'."
  7909. (let ((m org-open-link-marker))
  7910. (catch 'exit
  7911. (while (apply 're-search-forward args)
  7912. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7913. (goto-char (match-end group))
  7914. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7915. (> (match-beginning 0) (marker-position m))
  7916. (< (match-end 0) (marker-position m)))
  7917. (save-match-data
  7918. (or (not (org-in-regexp
  7919. org-bracket-link-analytic-regexp 1))
  7920. (not (match-end 4)) ; no description
  7921. (and (<= (match-beginning 4) (point))
  7922. (>= (match-end 4) (point))))))
  7923. (throw 'exit (point))))))))
  7924. (defun org-get-buffer-for-internal-link (buffer)
  7925. "Return a buffer to be used for displaying the link target of internal links."
  7926. (cond
  7927. ((not org-display-internal-link-with-indirect-buffer)
  7928. buffer)
  7929. ((string-match "(Clone)$" (buffer-name buffer))
  7930. (message "Buffer is already a clone, not making another one")
  7931. ;; we also do not modify visibility in this case
  7932. buffer)
  7933. (t ; make a new indirect buffer for displaying the link
  7934. (let* ((bn (buffer-name buffer))
  7935. (ibn (concat bn "(Clone)"))
  7936. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7937. (with-current-buffer ib (org-overview))
  7938. ib))))
  7939. (defun org-do-occur (regexp &optional cleanup)
  7940. "Call the Emacs command `occur'.
  7941. If CLEANUP is non-nil, remove the printout of the regular expression
  7942. in the *Occur* buffer. This is useful if the regex is long and not useful
  7943. to read."
  7944. (occur regexp)
  7945. (when cleanup
  7946. (let ((cwin (selected-window)) win beg end)
  7947. (when (setq win (get-buffer-window "*Occur*"))
  7948. (select-window win))
  7949. (goto-char (point-min))
  7950. (when (re-search-forward "match[a-z]+" nil t)
  7951. (setq beg (match-end 0))
  7952. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7953. (setq end (1- (match-beginning 0)))))
  7954. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7955. (goto-char (point-min))
  7956. (select-window cwin))))
  7957. ;;; The mark ring for links jumps
  7958. (defvar org-mark-ring nil
  7959. "Mark ring for positions before jumps in Org-mode.")
  7960. (defvar org-mark-ring-last-goto nil
  7961. "Last position in the mark ring used to go back.")
  7962. ;; Fill and close the ring
  7963. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7964. (loop for i from 1 to org-mark-ring-length do
  7965. (push (make-marker) org-mark-ring))
  7966. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7967. org-mark-ring)
  7968. (defun org-mark-ring-push (&optional pos buffer)
  7969. "Put the current position or POS into the mark ring and rotate it."
  7970. (interactive)
  7971. (setq pos (or pos (point)))
  7972. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7973. (move-marker (car org-mark-ring)
  7974. (or pos (point))
  7975. (or buffer (current-buffer)))
  7976. (message "%s"
  7977. (substitute-command-keys
  7978. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7979. (defun org-mark-ring-goto (&optional n)
  7980. "Jump to the previous position in the mark ring.
  7981. With prefix arg N, jump back that many stored positions. When
  7982. called several times in succession, walk through the entire ring.
  7983. Org-mode commands jumping to a different position in the current file,
  7984. or to another Org-mode file, automatically push the old position
  7985. onto the ring."
  7986. (interactive "p")
  7987. (let (p m)
  7988. (if (eq last-command this-command)
  7989. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7990. (setq p org-mark-ring))
  7991. (setq org-mark-ring-last-goto p)
  7992. (setq m (car p))
  7993. (switch-to-buffer (marker-buffer m))
  7994. (goto-char m)
  7995. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7996. (defun org-remove-angle-brackets (s)
  7997. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7998. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7999. s)
  8000. (defun org-add-angle-brackets (s)
  8001. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8002. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8003. s)
  8004. (defun org-remove-double-quotes (s)
  8005. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8006. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8007. s)
  8008. ;;; Following specific links
  8009. (defun org-follow-timestamp-link ()
  8010. (cond
  8011. ((org-at-date-range-p t)
  8012. (let ((org-agenda-start-on-weekday)
  8013. (t1 (match-string 1))
  8014. (t2 (match-string 2)))
  8015. (setq t1 (time-to-days (org-time-string-to-time t1))
  8016. t2 (time-to-days (org-time-string-to-time t2)))
  8017. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8018. ((org-at-timestamp-p t)
  8019. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8020. (substring (match-string 1) 0 10)))
  8021. 1))
  8022. (t (error "This should not happen"))))
  8023. ;;; Following file links
  8024. (defvar org-wait nil)
  8025. (defun org-open-file (path &optional in-emacs line search)
  8026. "Open the file at PATH.
  8027. First, this expands any special file name abbreviations. Then the
  8028. configuration variable `org-file-apps' is checked if it contains an
  8029. entry for this file type, and if yes, the corresponding command is launched.
  8030. If no application is found, Emacs simply visits the file.
  8031. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8032. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8033. and to use an external application to visit the file.
  8034. Optional LINE specifies a line to go to, optional SEARCH a string to
  8035. search for. If LINE or SEARCH is given, the file will always be
  8036. opened in Emacs.
  8037. If the file does not exist, an error is thrown."
  8038. (setq in-emacs (or in-emacs line search))
  8039. (let* ((file (if (equal path "")
  8040. buffer-file-name
  8041. (substitute-in-file-name (expand-file-name path))))
  8042. (apps (append org-file-apps (org-default-apps)))
  8043. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8044. (dirp (if remp nil (file-directory-p file)))
  8045. (file (if (and dirp org-open-directory-means-index-dot-org)
  8046. (concat (file-name-as-directory file) "index.org")
  8047. file))
  8048. (a-m-a-p (assq 'auto-mode apps))
  8049. (dfile (downcase file))
  8050. (old-buffer (current-buffer))
  8051. (old-pos (point))
  8052. (old-mode major-mode)
  8053. ext cmd)
  8054. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8055. (setq ext (match-string 1 dfile))
  8056. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8057. (setq ext (match-string 1 dfile))))
  8058. (cond
  8059. ((member in-emacs '((16) system))
  8060. (setq cmd (cdr (assoc 'system apps))))
  8061. (in-emacs (setq cmd 'emacs))
  8062. (t
  8063. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8064. (and dirp (cdr (assoc 'directory apps)))
  8065. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8066. 'string-match)
  8067. (cdr (assoc ext apps))
  8068. (cdr (assoc t apps))))))
  8069. (when (eq cmd 'system)
  8070. (setq cmd (cdr (assoc 'system apps))))
  8071. (when (eq cmd 'default)
  8072. (setq cmd (cdr (assoc t apps))))
  8073. (when (eq cmd 'mailcap)
  8074. (require 'mailcap)
  8075. (mailcap-parse-mailcaps)
  8076. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8077. (command (mailcap-mime-info mime-type)))
  8078. (if (stringp command)
  8079. (setq cmd command)
  8080. (setq cmd 'emacs))))
  8081. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8082. (not (file-exists-p file))
  8083. (not org-open-non-existing-files))
  8084. (error "No such file: %s" file))
  8085. (cond
  8086. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8087. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8088. (while (string-match "['\"]%s['\"]" cmd)
  8089. (setq cmd (replace-match "%s" t t cmd)))
  8090. (while (string-match "%s" cmd)
  8091. (setq cmd (replace-match
  8092. (save-match-data
  8093. (shell-quote-argument
  8094. (convert-standard-filename file)))
  8095. t t cmd)))
  8096. (save-window-excursion
  8097. (start-process-shell-command cmd nil cmd)
  8098. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8099. ))
  8100. ((or (stringp cmd)
  8101. (eq cmd 'emacs))
  8102. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8103. (widen)
  8104. (if line (org-goto-line line)
  8105. (if search (org-link-search search))))
  8106. ((consp cmd)
  8107. (let ((file (convert-standard-filename file)))
  8108. (eval cmd)))
  8109. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8110. (and (org-mode-p) (eq old-mode 'org-mode)
  8111. (or (not (equal old-buffer (current-buffer)))
  8112. (not (equal old-pos (point))))
  8113. (org-mark-ring-push old-pos old-buffer))))
  8114. (defun org-default-apps ()
  8115. "Return the default applications for this operating system."
  8116. (cond
  8117. ((eq system-type 'darwin)
  8118. org-file-apps-defaults-macosx)
  8119. ((eq system-type 'windows-nt)
  8120. org-file-apps-defaults-windowsnt)
  8121. (t org-file-apps-defaults-gnu)))
  8122. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8123. "Convert extensions to regular expressions in the cars of LIST.
  8124. Also, weed out any non-string entries, because the return value is used
  8125. only for regexp matching.
  8126. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8127. point to the symbol `emacs', indicating that the file should
  8128. be opened in Emacs."
  8129. (append
  8130. (delq nil
  8131. (mapcar (lambda (x)
  8132. (if (not (stringp (car x)))
  8133. nil
  8134. (if (string-match "\\W" (car x))
  8135. x
  8136. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8137. list))
  8138. (if add-auto-mode
  8139. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8140. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8141. (defun org-file-remote-p (file)
  8142. "Test whether FILE specifies a location on a remote system.
  8143. Return non-nil if the location is indeed remote.
  8144. For example, the filename \"/user@host:/foo\" specifies a location
  8145. on the system \"/user@host:\"."
  8146. (cond ((fboundp 'file-remote-p)
  8147. (file-remote-p file))
  8148. ((fboundp 'tramp-handle-file-remote-p)
  8149. (tramp-handle-file-remote-p file))
  8150. ((and (boundp 'ange-ftp-name-format)
  8151. (string-match (car ange-ftp-name-format) file))
  8152. t)
  8153. (t nil)))
  8154. ;;;; Refiling
  8155. (defun org-get-org-file ()
  8156. "Read a filename, with default directory `org-directory'."
  8157. (let ((default (or org-default-notes-file remember-data-file)))
  8158. (read-file-name (format "File name [%s]: " default)
  8159. (file-name-as-directory org-directory)
  8160. default)))
  8161. (defun org-notes-order-reversed-p ()
  8162. "Check if the current file should receive notes in reversed order."
  8163. (cond
  8164. ((not org-reverse-note-order) nil)
  8165. ((eq t org-reverse-note-order) t)
  8166. ((not (listp org-reverse-note-order)) nil)
  8167. (t (catch 'exit
  8168. (let ((all org-reverse-note-order)
  8169. entry)
  8170. (while (setq entry (pop all))
  8171. (if (string-match (car entry) buffer-file-name)
  8172. (throw 'exit (cdr entry))))
  8173. nil)))))
  8174. (defvar org-refile-target-table nil
  8175. "The list of refile targets, created by `org-refile'.")
  8176. (defvar org-agenda-new-buffers nil
  8177. "Buffers created to visit agenda files.")
  8178. (defun org-get-refile-targets (&optional default-buffer)
  8179. "Produce a table with refile targets."
  8180. (let ((case-fold-search nil)
  8181. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8182. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8183. targets txt re files f desc descre fast-path-p level pos0)
  8184. (message "Getting targets...")
  8185. (with-current-buffer (or default-buffer (current-buffer))
  8186. (while (setq entry (pop entries))
  8187. (setq files (car entry) desc (cdr entry))
  8188. (setq fast-path-p nil)
  8189. (cond
  8190. ((null files) (setq files (list (current-buffer))))
  8191. ((eq files 'org-agenda-files)
  8192. (setq files (org-agenda-files 'unrestricted)))
  8193. ((and (symbolp files) (fboundp files))
  8194. (setq files (funcall files)))
  8195. ((and (symbolp files) (boundp files))
  8196. (setq files (symbol-value files))))
  8197. (if (stringp files) (setq files (list files)))
  8198. (cond
  8199. ((eq (car desc) :tag)
  8200. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8201. ((eq (car desc) :todo)
  8202. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8203. ((eq (car desc) :regexp)
  8204. (setq descre (cdr desc)))
  8205. ((eq (car desc) :level)
  8206. (setq descre (concat "^\\*\\{" (number-to-string
  8207. (if org-odd-levels-only
  8208. (1- (* 2 (cdr desc)))
  8209. (cdr desc)))
  8210. "\\}[ \t]")))
  8211. ((eq (car desc) :maxlevel)
  8212. (setq fast-path-p t)
  8213. (setq descre (concat "^\\*\\{1," (number-to-string
  8214. (if org-odd-levels-only
  8215. (1- (* 2 (cdr desc)))
  8216. (cdr desc)))
  8217. "\\}[ \t]")))
  8218. (t (error "Bad refiling target description %s" desc)))
  8219. (while (setq f (pop files))
  8220. (with-current-buffer
  8221. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8222. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8223. (setq f (and f (expand-file-name f)))
  8224. (if (eq org-refile-use-outline-path 'file)
  8225. (push (list (file-name-nondirectory f) f nil nil) targets))
  8226. (save-excursion
  8227. (save-restriction
  8228. (widen)
  8229. (goto-char (point-min))
  8230. (while (re-search-forward descre nil t)
  8231. (goto-char (setq pos0 (point-at-bol)))
  8232. (catch 'next
  8233. (when org-refile-target-verify-function
  8234. (save-match-data
  8235. (or (funcall org-refile-target-verify-function)
  8236. (throw 'next t))))
  8237. (when (looking-at org-complex-heading-regexp)
  8238. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8239. txt (org-link-display-format (match-string 4))
  8240. re (concat "^" (regexp-quote
  8241. (buffer-substring (match-beginning 1)
  8242. (match-end 4)))))
  8243. (if (match-end 5) (setq re (concat re "[ \t]+"
  8244. (regexp-quote
  8245. (match-string 5)))))
  8246. (setq re (concat re "[ \t]*$"))
  8247. (when org-refile-use-outline-path
  8248. (setq txt (mapconcat 'org-protect-slash
  8249. (append
  8250. (if (eq org-refile-use-outline-path 'file)
  8251. (list (file-name-nondirectory
  8252. (buffer-file-name (buffer-base-buffer))))
  8253. (if (eq org-refile-use-outline-path 'full-file-path)
  8254. (list (buffer-file-name (buffer-base-buffer)))))
  8255. (org-get-outline-path fast-path-p level txt)
  8256. (list txt))
  8257. "/")))
  8258. (push (list txt f re (point)) targets)))
  8259. (when (= (point) pos0)
  8260. ;; verification function has not moved point
  8261. (goto-char (point-at-eol))))))))))
  8262. (message "Getting targets...done")
  8263. (nreverse targets)))
  8264. (defun org-protect-slash (s)
  8265. (while (string-match "/" s)
  8266. (setq s (replace-match "\\" t t s)))
  8267. s)
  8268. (defvar org-olpa (make-vector 20 nil))
  8269. (defun org-get-outline-path (&optional fastp level heading)
  8270. "Return the outline path to the current entry, as a list.
  8271. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8272. routine which makes outline path derivations for an entire file,
  8273. avoiding backtracing."
  8274. (if fastp
  8275. (progn
  8276. (if (> level 19)
  8277. (error "Outline path failure, more than 19 levels."))
  8278. (loop for i from level upto 19 do
  8279. (aset org-olpa i nil))
  8280. (prog1
  8281. (delq nil (append org-olpa nil))
  8282. (aset org-olpa level heading)))
  8283. (let (rtn case-fold-search)
  8284. (save-excursion
  8285. (save-restriction
  8286. (widen)
  8287. (while (org-up-heading-safe)
  8288. (when (looking-at org-complex-heading-regexp)
  8289. (push (org-match-string-no-properties 4) rtn)))
  8290. rtn)))))
  8291. (defun org-format-outline-path (path &optional width prefix)
  8292. "Format the outlie path PATH for display.
  8293. Width is the maximum number of characters that is available.
  8294. Prefix is a prefix to be included in the returned string,
  8295. such as the file name."
  8296. (setq width (or width 79))
  8297. (if prefix (setq width (- width (length prefix))))
  8298. (if (not path)
  8299. (or prefix "")
  8300. (let* ((nsteps (length path))
  8301. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8302. (maxwidth (if (<= total-width width)
  8303. 10000 ;; everything fits
  8304. ;; we need to shorten the level headings
  8305. (/ (- width nsteps) nsteps)))
  8306. (org-odd-levels-only nil)
  8307. (n 0)
  8308. (total (1+ (length prefix))))
  8309. (setq maxwidth (max maxwidth 10))
  8310. (concat prefix
  8311. (mapconcat
  8312. (lambda (h)
  8313. (setq n (1+ n))
  8314. (if (and (= n nsteps) (< maxwidth 10000))
  8315. (setq maxwidth (- total-width total)))
  8316. (if (< (length h) maxwidth)
  8317. (progn (setq total (+ total (length h) 1)) h)
  8318. (setq h (substring h 0 (- maxwidth 2))
  8319. total (+ total maxwidth 1))
  8320. (if (string-match "[ \t]+\\'" h)
  8321. (setq h (substring h 0 (match-beginning 0))))
  8322. (setq h (concat h "..")))
  8323. (org-add-props h nil 'face
  8324. (nth (% (1- n) org-n-level-faces)
  8325. org-level-faces))
  8326. h)
  8327. path "/")))))
  8328. (defun org-display-outline-path (&optional file current)
  8329. "Display the current outline path in the echo area."
  8330. (interactive "P")
  8331. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8332. (case-fold-search nil)
  8333. (path (and (org-mode-p) (org-get-outline-path))))
  8334. (if current (setq path (append path
  8335. (save-excursion
  8336. (org-back-to-heading t)
  8337. (if (looking-at org-complex-heading-regexp)
  8338. (list (match-string 4)))))))
  8339. (message "%s"
  8340. (org-format-outline-path
  8341. path
  8342. (1- (frame-width))
  8343. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8344. (defvar org-refile-history nil
  8345. "History for refiling operations.")
  8346. (defvar org-after-refile-insert-hook nil
  8347. "Hook run after `org-refile' has inserted its stuff at the new location.
  8348. Note that this is still *before* the stuff will be removed from
  8349. the *old* location.")
  8350. (defun org-refile (&optional goto default-buffer rfloc)
  8351. "Move the entry at point to another heading.
  8352. The list of target headings is compiled using the information in
  8353. `org-refile-targets', which see. This list is created before each use
  8354. and will therefore always be up-to-date.
  8355. At the target location, the entry is filed as a subitem of the target heading.
  8356. Depending on `org-reverse-note-order', the new subitem will either be the
  8357. first or the last subitem.
  8358. If there is an active region, all entries in that region will be moved.
  8359. However, the region must fulfil the requirement that the first heading
  8360. is the first one sets the top-level of the moved text - at most siblings
  8361. below it are allowed.
  8362. With prefix arg GOTO, the command will only visit the target location,
  8363. not actually move anything.
  8364. With a double prefix `C-u C-u', go to the location where the last refiling
  8365. operation has put the subtree.
  8366. With a prefix argument of `2', refile to the running clock.
  8367. RFLOC can be a refile location obtained in a different way.
  8368. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8369. (interactive "P")
  8370. (let* ((cbuf (current-buffer))
  8371. (regionp (org-region-active-p))
  8372. (region-start (and regionp (region-beginning)))
  8373. (region-end (and regionp (region-end)))
  8374. (region-length (and regionp (- region-end region-start)))
  8375. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8376. pos it nbuf file re level reversed)
  8377. (setq last-command nil)
  8378. (when regionp
  8379. (goto-char region-start)
  8380. (or (bolp) (goto-char (point-at-bol)))
  8381. (setq region-start (point))
  8382. (unless (org-kill-is-subtree-p
  8383. (buffer-substring region-start region-end))
  8384. (error "The region is not a (sequence of) subtree(s)")))
  8385. (if (equal goto '(16))
  8386. (org-refile-goto-last-stored)
  8387. (when (or
  8388. (and (equal goto 2)
  8389. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8390. (prog1
  8391. (setq it (list (or org-clock-heading "running clock")
  8392. (buffer-file-name
  8393. (marker-buffer org-clock-hd-marker))
  8394. ""
  8395. (marker-position org-clock-hd-marker)))
  8396. (setq goto nil)))
  8397. (setq it (or rfloc
  8398. (save-excursion
  8399. (org-refile-get-location
  8400. (if goto "Goto: " "Refile to: ") default-buffer
  8401. org-refile-allow-creating-parent-nodes)))))
  8402. (setq file (nth 1 it)
  8403. re (nth 2 it)
  8404. pos (nth 3 it))
  8405. (if (and (not goto)
  8406. pos
  8407. (equal (buffer-file-name) file)
  8408. (if regionp
  8409. (and (>= pos region-start)
  8410. (<= pos region-end))
  8411. (and (>= pos (point))
  8412. (< pos (save-excursion
  8413. (org-end-of-subtree t t))))))
  8414. (error "Cannot refile to position inside the tree or region"))
  8415. (setq nbuf (or (find-buffer-visiting file)
  8416. (find-file-noselect file)))
  8417. (if goto
  8418. (progn
  8419. (switch-to-buffer nbuf)
  8420. (goto-char pos)
  8421. (org-show-context 'org-goto))
  8422. (if regionp
  8423. (progn
  8424. (org-kill-new (buffer-substring region-start region-end))
  8425. (org-save-markers-in-region region-start region-end))
  8426. (org-copy-subtree 1 nil t))
  8427. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8428. (find-file-noselect file)))
  8429. (setq reversed (org-notes-order-reversed-p))
  8430. (save-excursion
  8431. (save-restriction
  8432. (widen)
  8433. (if pos
  8434. (progn
  8435. (goto-char pos)
  8436. (looking-at outline-regexp)
  8437. (setq level (org-get-valid-level (funcall outline-level) 1))
  8438. (goto-char
  8439. (if reversed
  8440. (or (outline-next-heading) (point-max))
  8441. (or (save-excursion (org-get-next-sibling))
  8442. (org-end-of-subtree t t)
  8443. (point-max)))))
  8444. (setq level 1)
  8445. (if (not reversed)
  8446. (goto-char (point-max))
  8447. (goto-char (point-min))
  8448. (or (outline-next-heading) (goto-char (point-max)))))
  8449. (if (not (bolp)) (newline))
  8450. (bookmark-set "org-refile-last-stored")
  8451. (org-paste-subtree level)
  8452. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8453. (run-hooks 'org-after-refile-insert-hook))))
  8454. (if regionp
  8455. (delete-region (point) (+ (point) region-length))
  8456. (org-cut-subtree))
  8457. (when (featurep 'org-inlinetask)
  8458. (org-inlinetask-remove-END-maybe))
  8459. (setq org-markers-to-move nil)
  8460. (message "Refiled to \"%s\"" (car it))))))
  8461. (org-reveal))
  8462. (defun org-refile-goto-last-stored ()
  8463. "Go to the location where the last refile was stored."
  8464. (interactive)
  8465. (bookmark-jump "org-refile-last-stored")
  8466. (message "This is the location of the last refile"))
  8467. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8468. "Prompt the user for a refile location, using PROMPT."
  8469. (let ((org-refile-targets org-refile-targets)
  8470. (org-refile-use-outline-path org-refile-use-outline-path))
  8471. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8472. (unless org-refile-target-table
  8473. (error "No refile targets"))
  8474. (let* ((cbuf (current-buffer))
  8475. (partial-completion-mode nil)
  8476. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8477. (cfunc (if (and org-refile-use-outline-path
  8478. org-outline-path-complete-in-steps)
  8479. 'org-olpath-completing-read
  8480. 'org-icompleting-read))
  8481. (extra (if org-refile-use-outline-path "/" ""))
  8482. (filename (and cfn (expand-file-name cfn)))
  8483. (tbl (mapcar
  8484. (lambda (x)
  8485. (if (and (not (member org-refile-use-outline-path
  8486. '(file full-file-path)))
  8487. (not (equal filename (nth 1 x))))
  8488. (cons (concat (car x) extra " ("
  8489. (file-name-nondirectory (nth 1 x)) ")")
  8490. (cdr x))
  8491. (cons (concat (car x) extra) (cdr x))))
  8492. org-refile-target-table))
  8493. (completion-ignore-case t)
  8494. pa answ parent-target child parent old-hist)
  8495. (setq old-hist org-refile-history)
  8496. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8497. nil 'org-refile-history))
  8498. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8499. (if pa
  8500. (progn
  8501. (when (or (not org-refile-history)
  8502. (not (eq old-hist org-refile-history))
  8503. (not (equal (car pa) (car org-refile-history))))
  8504. (setq org-refile-history
  8505. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8506. org-refile-history
  8507. (cdr org-refile-history))))
  8508. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8509. (pop org-refile-history)))
  8510. pa)
  8511. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8512. (setq parent (match-string 1 answ)
  8513. child (match-string 2 answ))
  8514. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8515. (when (and parent-target
  8516. (or (eq new-nodes t)
  8517. (and (eq new-nodes 'confirm)
  8518. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8519. (org-refile-new-child parent-target child))))))
  8520. (defun org-refile-new-child (parent-target child)
  8521. "Use refile target PARENT-TARGET to add new CHILD below it."
  8522. (unless parent-target
  8523. (error "Cannot find parent for new node"))
  8524. (let ((file (nth 1 parent-target))
  8525. (pos (nth 3 parent-target))
  8526. level)
  8527. (with-current-buffer (or (find-buffer-visiting file)
  8528. (find-file-noselect file))
  8529. (save-excursion
  8530. (save-restriction
  8531. (widen)
  8532. (if pos
  8533. (goto-char pos)
  8534. (goto-char (point-max))
  8535. (if (not (bolp)) (newline)))
  8536. (when (looking-at outline-regexp)
  8537. (setq level (funcall outline-level))
  8538. (org-end-of-subtree t t))
  8539. (org-back-over-empty-lines)
  8540. (insert "\n" (make-string
  8541. (if pos (org-get-valid-level level 1) 1) ?*)
  8542. " " child "\n")
  8543. (beginning-of-line 0)
  8544. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8545. (defun org-olpath-completing-read (prompt collection &rest args)
  8546. "Read an outline path like a file name."
  8547. (let ((thetable collection)
  8548. (org-completion-use-ido nil) ; does not work with ido.
  8549. (org-completion-use-iswitchb nil)) ; or iswitchb
  8550. (apply
  8551. 'org-icompleting-read prompt
  8552. (lambda (string predicate &optional flag)
  8553. (let (rtn r f (l (length string)))
  8554. (cond
  8555. ((eq flag nil)
  8556. ;; try completion
  8557. (try-completion string thetable))
  8558. ((eq flag t)
  8559. ;; all-completions
  8560. (setq rtn (all-completions string thetable predicate))
  8561. (mapcar
  8562. (lambda (x)
  8563. (setq r (substring x l))
  8564. (if (string-match " ([^)]*)$" x)
  8565. (setq f (match-string 0 x))
  8566. (setq f ""))
  8567. (if (string-match "/" r)
  8568. (concat string (substring r 0 (match-end 0)) f)
  8569. x))
  8570. rtn))
  8571. ((eq flag 'lambda)
  8572. ;; exact match?
  8573. (assoc string thetable)))
  8574. ))
  8575. args)))
  8576. ;;;; Dynamic blocks
  8577. (defun org-find-dblock (name)
  8578. "Find the first dynamic block with name NAME in the buffer.
  8579. If not found, stay at current position and return nil."
  8580. (let (pos)
  8581. (save-excursion
  8582. (goto-char (point-min))
  8583. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8584. nil t)
  8585. (match-beginning 0))))
  8586. (if pos (goto-char pos))
  8587. pos))
  8588. (defconst org-dblock-start-re
  8589. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8590. "Matches the start line of a dynamic block, with parameters.")
  8591. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8592. "Matches the end of a dynamic block.")
  8593. (defun org-create-dblock (plist)
  8594. "Create a dynamic block section, with parameters taken from PLIST.
  8595. PLIST must contain a :name entry which is used as name of the block."
  8596. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8597. (end-of-line 1)
  8598. (newline))
  8599. (let ((col (current-column))
  8600. (name (plist-get plist :name)))
  8601. (insert "#+BEGIN: " name)
  8602. (while plist
  8603. (if (eq (car plist) :name)
  8604. (setq plist (cddr plist))
  8605. (insert " " (prin1-to-string (pop plist)))))
  8606. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8607. (beginning-of-line -2)))
  8608. (defun org-prepare-dblock ()
  8609. "Prepare dynamic block for refresh.
  8610. This empties the block, puts the cursor at the insert position and returns
  8611. the property list including an extra property :name with the block name."
  8612. (unless (looking-at org-dblock-start-re)
  8613. (error "Not at a dynamic block"))
  8614. (let* ((begdel (1+ (match-end 0)))
  8615. (name (org-no-properties (match-string 1)))
  8616. (params (append (list :name name)
  8617. (read (concat "(" (match-string 3) ")")))))
  8618. (save-excursion
  8619. (beginning-of-line 1)
  8620. (skip-chars-forward " \t")
  8621. (setq params (plist-put params :indentation-column (current-column))))
  8622. (unless (re-search-forward org-dblock-end-re nil t)
  8623. (error "Dynamic block not terminated"))
  8624. (setq params
  8625. (append params
  8626. (list :content (buffer-substring
  8627. begdel (match-beginning 0)))))
  8628. (delete-region begdel (match-beginning 0))
  8629. (goto-char begdel)
  8630. (open-line 1)
  8631. params))
  8632. (defun org-map-dblocks (&optional command)
  8633. "Apply COMMAND to all dynamic blocks in the current buffer.
  8634. If COMMAND is not given, use `org-update-dblock'."
  8635. (let ((cmd (or command 'org-update-dblock))
  8636. pos)
  8637. (save-excursion
  8638. (goto-char (point-min))
  8639. (while (re-search-forward org-dblock-start-re nil t)
  8640. (goto-char (setq pos (match-beginning 0)))
  8641. (condition-case nil
  8642. (funcall cmd)
  8643. (error (message "Error during update of dynamic block")))
  8644. (goto-char pos)
  8645. (unless (re-search-forward org-dblock-end-re nil t)
  8646. (error "Dynamic block not terminated"))))))
  8647. (defun org-dblock-update (&optional arg)
  8648. "User command for updating dynamic blocks.
  8649. Update the dynamic block at point. With prefix ARG, update all dynamic
  8650. blocks in the buffer."
  8651. (interactive "P")
  8652. (if arg
  8653. (org-update-all-dblocks)
  8654. (or (looking-at org-dblock-start-re)
  8655. (org-beginning-of-dblock))
  8656. (org-update-dblock)))
  8657. (defun org-update-dblock ()
  8658. "Update the dynamic block at point
  8659. This means to empty the block, parse for parameters and then call
  8660. the correct writing function."
  8661. (save-window-excursion
  8662. (let* ((pos (point))
  8663. (line (org-current-line))
  8664. (params (org-prepare-dblock))
  8665. (name (plist-get params :name))
  8666. (indent (plist-get params :indentation-column))
  8667. (cmd (intern (concat "org-dblock-write:" name))))
  8668. (message "Updating dynamic block `%s' at line %d..." name line)
  8669. (funcall cmd params)
  8670. (message "Updating dynamic block `%s' at line %d...done" name line)
  8671. (goto-char pos)
  8672. (when (and indent (> indent 0))
  8673. (setq indent (make-string indent ?\ ))
  8674. (save-excursion
  8675. (org-beginning-of-dblock)
  8676. (forward-line 1)
  8677. (while (not (looking-at org-dblock-end-re))
  8678. (insert indent)
  8679. (beginning-of-line 2))
  8680. (when (looking-at org-dblock-end-re)
  8681. (and (looking-at "[ \t]+")
  8682. (replace-match ""))
  8683. (insert indent)))))))
  8684. (defun org-beginning-of-dblock ()
  8685. "Find the beginning of the dynamic block at point.
  8686. Error if there is no such block at point."
  8687. (let ((pos (point))
  8688. beg)
  8689. (end-of-line 1)
  8690. (if (and (re-search-backward org-dblock-start-re nil t)
  8691. (setq beg (match-beginning 0))
  8692. (re-search-forward org-dblock-end-re nil t)
  8693. (> (match-end 0) pos))
  8694. (goto-char beg)
  8695. (goto-char pos)
  8696. (error "Not in a dynamic block"))))
  8697. (defun org-update-all-dblocks ()
  8698. "Update all dynamic blocks in the buffer.
  8699. This function can be used in a hook."
  8700. (when (org-mode-p)
  8701. (org-map-dblocks 'org-update-dblock)))
  8702. ;;;; Completion
  8703. (defconst org-additional-option-like-keywords
  8704. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8705. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8706. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8707. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8708. "BEGIN:" "END:"
  8709. "ORGTBL" "TBLFM:" "TBLNAME:"
  8710. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8711. "BEGIN_QUOTE" "END_QUOTE"
  8712. "BEGIN_VERSE" "END_VERSE"
  8713. "BEGIN_CENTER" "END_CENTER"
  8714. "BEGIN_SRC" "END_SRC"
  8715. "CATEGORY" "COLUMNS"
  8716. "CAPTION" "LABEL"
  8717. "SETUPFILE"
  8718. "BIND"
  8719. "MACRO"))
  8720. (defcustom org-structure-template-alist
  8721. '(
  8722. ("s" "#+begin_src ?\n\n#+end_src"
  8723. "<src lang=\"?\">\n\n</src>")
  8724. ("e" "#+begin_example\n?\n#+end_example"
  8725. "<example>\n?\n</example>")
  8726. ("q" "#+begin_quote\n?\n#+end_quote"
  8727. "<quote>\n?\n</quote>")
  8728. ("v" "#+begin_verse\n?\n#+end_verse"
  8729. "<verse>\n?\n/verse>")
  8730. ("c" "#+begin_center\n?\n#+end_center"
  8731. "<center>\n?\n/center>")
  8732. ("l" "#+begin_latex\n?\n#+end_latex"
  8733. "<literal style=\"latex\">\n?\n</literal>")
  8734. ("L" "#+latex: "
  8735. "<literal style=\"latex\">?</literal>")
  8736. ("h" "#+begin_html\n?\n#+end_html"
  8737. "<literal style=\"html\">\n?\n</literal>")
  8738. ("H" "#+html: "
  8739. "<literal style=\"html\">?</literal>")
  8740. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8741. ("A" "#+ascii: ")
  8742. ("i" "#+include %file ?"
  8743. "<include file=%file markup=\"?\">")
  8744. )
  8745. "Structure completion elements.
  8746. This is a list of abbreviation keys and values. The value gets inserted
  8747. it you type @samp{.} followed by the key and then the completion key,
  8748. usually `M-TAB'. %file will be replaced by a file name after prompting
  8749. for the file using completion.
  8750. There are two templates for each key, the first uses the original Org syntax,
  8751. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8752. the default when the /org-mtags.el/ module has been loaded. See also the
  8753. variable `org-mtags-prefer-muse-templates'.
  8754. This is an experimental feature, it is undecided if it is going to stay in."
  8755. :group 'org-completion
  8756. :type '(repeat
  8757. (string :tag "Key")
  8758. (string :tag "Template")
  8759. (string :tag "Muse Template")))
  8760. (defun org-try-structure-completion ()
  8761. "Try to complete a structure template before point.
  8762. This looks for strings like \"<e\" on an otherwise empty line and
  8763. expands them."
  8764. (let ((l (buffer-substring (point-at-bol) (point)))
  8765. a)
  8766. (when (and (looking-at "[ \t]*$")
  8767. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8768. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8769. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8770. (match-beginning 1)) a)
  8771. t)))
  8772. (defun org-complete-expand-structure-template (start cell)
  8773. "Expand a structure template."
  8774. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8775. (rpl (nth (if musep 2 1) cell))
  8776. (ind ""))
  8777. (delete-region start (point))
  8778. (when (string-match "\\`#\\+" rpl)
  8779. (cond
  8780. ((bolp))
  8781. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8782. (setq ind (buffer-substring (point-at-bol) (point))))
  8783. (t (newline))))
  8784. (setq start (point))
  8785. (if (string-match "%file" rpl)
  8786. (setq rpl (replace-match
  8787. (concat
  8788. "\""
  8789. (save-match-data
  8790. (abbreviate-file-name (read-file-name "Include file: ")))
  8791. "\"")
  8792. t t rpl)))
  8793. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8794. (concat "\n" ind)))
  8795. (insert rpl)
  8796. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8797. (defun org-complete (&optional arg)
  8798. "Perform completion on word at point.
  8799. At the beginning of a headline, this completes TODO keywords as given in
  8800. `org-todo-keywords'.
  8801. If the current word is preceded by a backslash, completes the TeX symbols
  8802. that are supported for HTML support.
  8803. If the current word is preceded by \"#+\", completes special words for
  8804. setting file options.
  8805. In the line after \"#+STARTUP:, complete valid keywords.\"
  8806. At all other locations, this simply calls the value of
  8807. `org-completion-fallback-command'."
  8808. (interactive "P")
  8809. (org-without-partial-completion
  8810. (catch 'exit
  8811. (let* ((a nil)
  8812. (end (point))
  8813. (beg1 (save-excursion
  8814. (skip-chars-backward (org-re "[:alnum:]_@"))
  8815. (point)))
  8816. (beg (save-excursion
  8817. (skip-chars-backward "a-zA-Z0-9_:$")
  8818. (point)))
  8819. (confirm (lambda (x) (stringp (car x))))
  8820. (searchhead (equal (char-before beg) ?*))
  8821. (struct
  8822. (when (and (member (char-before beg1) '(?. ?<))
  8823. (setq a (assoc (buffer-substring beg1 (point))
  8824. org-structure-template-alist)))
  8825. (org-complete-expand-structure-template (1- beg1) a)
  8826. (throw 'exit t)))
  8827. (tag (and (equal (char-before beg1) ?:)
  8828. (equal (char-after (point-at-bol)) ?*)))
  8829. (prop (and (equal (char-before beg1) ?:)
  8830. (not (equal (char-after (point-at-bol)) ?*))))
  8831. (texp (equal (char-before beg) ?\\))
  8832. (link (equal (char-before beg) ?\[))
  8833. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8834. beg)
  8835. "#+"))
  8836. (startup (string-match "^#\\+STARTUP:.*"
  8837. (buffer-substring (point-at-bol) (point))))
  8838. (completion-ignore-case opt)
  8839. (type nil)
  8840. (tbl nil)
  8841. (table (cond
  8842. (opt
  8843. (setq type :opt)
  8844. (require 'org-exp)
  8845. (append
  8846. (delq nil
  8847. (mapcar
  8848. (lambda (x)
  8849. (if (string-match
  8850. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8851. (cons (match-string 2 x)
  8852. (match-string 1 x))))
  8853. (org-split-string (org-get-current-options) "\n")))
  8854. (mapcar 'list org-additional-option-like-keywords)))
  8855. (startup
  8856. (setq type :startup)
  8857. org-startup-options)
  8858. (link (append org-link-abbrev-alist-local
  8859. org-link-abbrev-alist))
  8860. (texp
  8861. (setq type :tex)
  8862. org-html-entities)
  8863. ((string-match "\\`\\*+[ \t]+\\'"
  8864. (buffer-substring (point-at-bol) beg))
  8865. (setq type :todo)
  8866. (mapcar 'list org-todo-keywords-1))
  8867. (searchhead
  8868. (setq type :searchhead)
  8869. (save-excursion
  8870. (goto-char (point-min))
  8871. (while (re-search-forward org-todo-line-regexp nil t)
  8872. (push (list
  8873. (org-make-org-heading-search-string
  8874. (match-string 3) t))
  8875. tbl)))
  8876. tbl)
  8877. (tag (setq type :tag beg beg1)
  8878. (or org-tag-alist (org-get-buffer-tags)))
  8879. (prop (setq type :prop beg beg1)
  8880. (mapcar 'list (org-buffer-property-keys nil t t)))
  8881. (t (progn
  8882. (call-interactively org-completion-fallback-command)
  8883. (throw 'exit nil)))))
  8884. (pattern (buffer-substring-no-properties beg end))
  8885. (completion (try-completion pattern table confirm)))
  8886. (cond ((eq completion t)
  8887. (if (not (assoc (upcase pattern) table))
  8888. (message "Already complete")
  8889. (if (and (equal type :opt)
  8890. (not (member (car (assoc (upcase pattern) table))
  8891. org-additional-option-like-keywords)))
  8892. (insert (substring (cdr (assoc (upcase pattern) table))
  8893. (length pattern)))
  8894. (if (memq type '(:tag :prop)) (insert ":")))))
  8895. ((null completion)
  8896. (message "Can't find completion for \"%s\"" pattern)
  8897. (ding))
  8898. ((not (string= pattern completion))
  8899. (delete-region beg end)
  8900. (if (string-match " +$" completion)
  8901. (setq completion (replace-match "" t t completion)))
  8902. (insert completion)
  8903. (if (get-buffer-window "*Completions*")
  8904. (delete-window (get-buffer-window "*Completions*")))
  8905. (if (assoc completion table)
  8906. (if (eq type :todo) (insert " ")
  8907. (if (memq type '(:tag :prop)) (insert ":"))))
  8908. (if (and (equal type :opt) (assoc completion table))
  8909. (message "%s" (substitute-command-keys
  8910. "Press \\[org-complete] again to insert example settings"))))
  8911. (t
  8912. (message "Making completion list...")
  8913. (let ((list (sort (all-completions pattern table confirm)
  8914. 'string<)))
  8915. (with-output-to-temp-buffer "*Completions*"
  8916. (condition-case nil
  8917. ;; Protection needed for XEmacs and emacs 21
  8918. (display-completion-list list pattern)
  8919. (error (display-completion-list list)))))
  8920. (message "Making completion list...%s" "done")))))))
  8921. ;;;; TODO, DEADLINE, Comments
  8922. (defun org-toggle-comment ()
  8923. "Change the COMMENT state of an entry."
  8924. (interactive)
  8925. (save-excursion
  8926. (org-back-to-heading)
  8927. (let (case-fold-search)
  8928. (if (looking-at (concat outline-regexp
  8929. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8930. (replace-match "" t t nil 1)
  8931. (if (looking-at outline-regexp)
  8932. (progn
  8933. (goto-char (match-end 0))
  8934. (insert org-comment-string " ")))))))
  8935. (defvar org-last-todo-state-is-todo nil
  8936. "This is non-nil when the last TODO state change led to a TODO state.
  8937. If the last change removed the TODO tag or switched to DONE, then
  8938. this is nil.")
  8939. (defvar org-setting-tags nil) ; dynamically skipped
  8940. (defun org-parse-local-options (string var)
  8941. "Parse STRING for startup setting relevant for variable VAR."
  8942. (let ((rtn (symbol-value var))
  8943. e opts)
  8944. (save-match-data
  8945. (if (or (not string) (not (string-match "\\S-" string)))
  8946. rtn
  8947. (setq opts (delq nil (mapcar (lambda (x)
  8948. (setq e (assoc x org-startup-options))
  8949. (if (eq (nth 1 e) var) e nil))
  8950. (org-split-string string "[ \t]+"))))
  8951. (if (not opts)
  8952. rtn
  8953. (setq rtn nil)
  8954. (while (setq e (pop opts))
  8955. (if (not (nth 3 e))
  8956. (setq rtn (nth 2 e))
  8957. (if (not (listp rtn)) (setq rtn nil))
  8958. (push (nth 2 e) rtn)))
  8959. rtn)))))
  8960. (defvar org-todo-setup-filter-hook nil
  8961. "Hook for functions that pre-filter todo specs.
  8962. Each function takes a todo spec and returns either `nil' or the spec
  8963. transformed into canonical form." )
  8964. (defvar org-todo-get-default-hook nil
  8965. "Hook for functions that get a default item for todo.
  8966. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8967. `nil' or a string to be used for the todo mark." )
  8968. (defvar org-agenda-headline-snapshot-before-repeat)
  8969. (defun org-todo (&optional arg)
  8970. "Change the TODO state of an item.
  8971. The state of an item is given by a keyword at the start of the heading,
  8972. like
  8973. *** TODO Write paper
  8974. *** DONE Call mom
  8975. The different keywords are specified in the variable `org-todo-keywords'.
  8976. By default the available states are \"TODO\" and \"DONE\".
  8977. So for this example: when the item starts with TODO, it is changed to DONE.
  8978. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8979. DONE are present, add TODO at the beginning of the heading.
  8980. With C-u prefix arg, use completion to determine the new state.
  8981. With numeric prefix arg, switch to that state.
  8982. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8983. With a triple C-u prefix, circumvent any state blocking.
  8984. For calling through lisp, arg is also interpreted in the following way:
  8985. 'none -> empty state
  8986. \"\"(empty string) -> switch to empty state
  8987. 'done -> switch to DONE
  8988. 'nextset -> switch to the next set of keywords
  8989. 'previousset -> switch to the previous set of keywords
  8990. \"WAITING\" -> switch to the specified keyword, but only if it
  8991. really is a member of `org-todo-keywords'."
  8992. (interactive "P")
  8993. (if (equal arg '(16)) (setq arg 'nextset))
  8994. (let ((org-blocker-hook org-blocker-hook)
  8995. (case-fold-search nil))
  8996. (when (equal arg '(64))
  8997. (setq arg nil org-blocker-hook nil))
  8998. (when (and org-blocker-hook
  8999. (or org-inhibit-blocking
  9000. (org-entry-get nil "NOBLOCKING")))
  9001. (setq org-blocker-hook nil))
  9002. (save-excursion
  9003. (catch 'exit
  9004. (org-back-to-heading t)
  9005. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9006. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9007. (looking-at " *"))
  9008. (let* ((match-data (match-data))
  9009. (startpos (point-at-bol))
  9010. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9011. (org-log-done org-log-done)
  9012. (org-log-repeat org-log-repeat)
  9013. (org-todo-log-states org-todo-log-states)
  9014. (this (match-string 1))
  9015. (hl-pos (match-beginning 0))
  9016. (head (org-get-todo-sequence-head this))
  9017. (ass (assoc head org-todo-kwd-alist))
  9018. (interpret (nth 1 ass))
  9019. (done-word (nth 3 ass))
  9020. (final-done-word (nth 4 ass))
  9021. (last-state (or this ""))
  9022. (completion-ignore-case t)
  9023. (member (member this org-todo-keywords-1))
  9024. (tail (cdr member))
  9025. (state (cond
  9026. ((and org-todo-key-trigger
  9027. (or (and (equal arg '(4))
  9028. (eq org-use-fast-todo-selection 'prefix))
  9029. (and (not arg) org-use-fast-todo-selection
  9030. (not (eq org-use-fast-todo-selection
  9031. 'prefix)))))
  9032. ;; Use fast selection
  9033. (org-fast-todo-selection))
  9034. ((and (equal arg '(4))
  9035. (or (not org-use-fast-todo-selection)
  9036. (not org-todo-key-trigger)))
  9037. ;; Read a state with completion
  9038. (org-icompleting-read
  9039. "State: " (mapcar (lambda(x) (list x))
  9040. org-todo-keywords-1)
  9041. nil t))
  9042. ((eq arg 'right)
  9043. (if this
  9044. (if tail (car tail) nil)
  9045. (car org-todo-keywords-1)))
  9046. ((eq arg 'left)
  9047. (if (equal member org-todo-keywords-1)
  9048. nil
  9049. (if this
  9050. (nth (- (length org-todo-keywords-1)
  9051. (length tail) 2)
  9052. org-todo-keywords-1)
  9053. (org-last org-todo-keywords-1))))
  9054. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9055. (setq arg nil))) ; hack to fall back to cycling
  9056. (arg
  9057. ;; user or caller requests a specific state
  9058. (cond
  9059. ((equal arg "") nil)
  9060. ((eq arg 'none) nil)
  9061. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9062. ((eq arg 'nextset)
  9063. (or (car (cdr (member head org-todo-heads)))
  9064. (car org-todo-heads)))
  9065. ((eq arg 'previousset)
  9066. (let ((org-todo-heads (reverse org-todo-heads)))
  9067. (or (car (cdr (member head org-todo-heads)))
  9068. (car org-todo-heads))))
  9069. ((car (member arg org-todo-keywords-1)))
  9070. ((stringp arg)
  9071. (error "State `%s' not valid in this file" arg))
  9072. ((nth (1- (prefix-numeric-value arg))
  9073. org-todo-keywords-1))))
  9074. ((null member) (or head (car org-todo-keywords-1)))
  9075. ((equal this final-done-word) nil) ;; -> make empty
  9076. ((null tail) nil) ;; -> first entry
  9077. ((memq interpret '(type priority))
  9078. (if (eq this-command last-command)
  9079. (car tail)
  9080. (if (> (length tail) 0)
  9081. (or done-word (car org-done-keywords))
  9082. nil)))
  9083. (t
  9084. (car tail))))
  9085. (state (or
  9086. (run-hook-with-args-until-success
  9087. 'org-todo-get-default-hook state last-state)
  9088. state))
  9089. (next (if state (concat " " state " ") " "))
  9090. (change-plist (list :type 'todo-state-change :from this :to state
  9091. :position startpos))
  9092. dolog now-done-p)
  9093. (when org-blocker-hook
  9094. (setq org-last-todo-state-is-todo
  9095. (not (member this org-done-keywords)))
  9096. (unless (save-excursion
  9097. (save-match-data
  9098. (run-hook-with-args-until-failure
  9099. 'org-blocker-hook change-plist)))
  9100. (if (interactive-p)
  9101. (error "TODO state change from %s to %s blocked" this state)
  9102. ;; fail silently
  9103. (message "TODO state change from %s to %s blocked" this state)
  9104. (throw 'exit nil))))
  9105. (store-match-data match-data)
  9106. (replace-match next t t)
  9107. (unless (pos-visible-in-window-p hl-pos)
  9108. (message "TODO state changed to %s" (org-trim next)))
  9109. (unless head
  9110. (setq head (org-get-todo-sequence-head state)
  9111. ass (assoc head org-todo-kwd-alist)
  9112. interpret (nth 1 ass)
  9113. done-word (nth 3 ass)
  9114. final-done-word (nth 4 ass)))
  9115. (when (memq arg '(nextset previousset))
  9116. (message "Keyword-Set %d/%d: %s"
  9117. (- (length org-todo-sets) -1
  9118. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9119. (length org-todo-sets)
  9120. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9121. (setq org-last-todo-state-is-todo
  9122. (not (member state org-done-keywords)))
  9123. (setq now-done-p (and (member state org-done-keywords)
  9124. (not (member this org-done-keywords))))
  9125. (and logging (org-local-logging logging))
  9126. (when (and (or org-todo-log-states org-log-done)
  9127. (not (eq org-inhibit-logging t))
  9128. (not (memq arg '(nextset previousset))))
  9129. ;; we need to look at recording a time and note
  9130. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9131. (nth 2 (assoc this org-todo-log-states))))
  9132. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9133. (setq dolog 'time))
  9134. (when (and state
  9135. (member state org-not-done-keywords)
  9136. (not (member this org-not-done-keywords)))
  9137. ;; This is now a todo state and was not one before
  9138. ;; If there was a CLOSED time stamp, get rid of it.
  9139. (org-add-planning-info nil nil 'closed))
  9140. (when (and now-done-p org-log-done)
  9141. ;; It is now done, and it was not done before
  9142. (org-add-planning-info 'closed (org-current-time))
  9143. (if (and (not dolog) (eq 'note org-log-done))
  9144. (org-add-log-setup 'done state this 'findpos 'note)))
  9145. (when (and state dolog)
  9146. ;; This is a non-nil state, and we need to log it
  9147. (org-add-log-setup 'state state this 'findpos dolog)))
  9148. ;; Fixup tag positioning
  9149. (org-todo-trigger-tag-changes state)
  9150. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9151. (when org-provide-todo-statistics
  9152. (org-update-parent-todo-statistics))
  9153. (run-hooks 'org-after-todo-state-change-hook)
  9154. (if (and arg (not (member state org-done-keywords)))
  9155. (setq head (org-get-todo-sequence-head state)))
  9156. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9157. ;; Do we need to trigger a repeat?
  9158. (when now-done-p
  9159. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9160. ;; This is for the agenda, take a snapshot of the headline.
  9161. (save-match-data
  9162. (setq org-agenda-headline-snapshot-before-repeat
  9163. (org-get-heading))))
  9164. (org-auto-repeat-maybe state))
  9165. ;; Fixup cursor location if close to the keyword
  9166. (if (and (outline-on-heading-p)
  9167. (not (bolp))
  9168. (save-excursion (beginning-of-line 1)
  9169. (looking-at org-todo-line-regexp))
  9170. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9171. (progn
  9172. (goto-char (or (match-end 2) (match-end 1)))
  9173. (and (looking-at " ") (just-one-space))))
  9174. (when org-trigger-hook
  9175. (save-excursion
  9176. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9177. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9178. "Block turning an entry into a TODO, using the hierarchy.
  9179. This checks whether the current task should be blocked from state
  9180. changes. Such blocking occurs when:
  9181. 1. The task has children which are not all in a completed state.
  9182. 2. A task has a parent with the property :ORDERED:, and there
  9183. are siblings prior to the current task with incomplete
  9184. status.
  9185. 3. The parent of the task is blocked because it has siblings that should
  9186. be done first, or is child of a block grandparent TODO entry."
  9187. (catch 'dont-block
  9188. ;; If this is not a todo state change, or if this entry is already DONE,
  9189. ;; do not block
  9190. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9191. (member (plist-get change-plist :from)
  9192. (cons 'done org-done-keywords))
  9193. (member (plist-get change-plist :to)
  9194. (cons 'todo org-not-done-keywords))
  9195. (not (plist-get change-plist :to)))
  9196. (throw 'dont-block t))
  9197. ;; If this task has children, and any are undone, it's blocked
  9198. (save-excursion
  9199. (org-back-to-heading t)
  9200. (let ((this-level (funcall outline-level)))
  9201. (outline-next-heading)
  9202. (let ((child-level (funcall outline-level)))
  9203. (while (and (not (eobp))
  9204. (> child-level this-level))
  9205. ;; this todo has children, check whether they are all
  9206. ;; completed
  9207. (if (and (not (org-entry-is-done-p))
  9208. (org-entry-is-todo-p))
  9209. (throw 'dont-block nil))
  9210. (outline-next-heading)
  9211. (setq child-level (funcall outline-level))))))
  9212. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9213. ;; any previous siblings are undone, it's blocked
  9214. (save-excursion
  9215. (org-back-to-heading t)
  9216. (let* ((pos (point))
  9217. (parent-pos (and (org-up-heading-safe) (point))))
  9218. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9219. (when (and (org-entry-get (point) "ORDERED")
  9220. (forward-line 1)
  9221. (re-search-forward org-not-done-heading-regexp pos t))
  9222. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9223. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9224. (while t
  9225. (goto-char parent-pos)
  9226. (if (not (looking-at org-not-done-heading-regexp))
  9227. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9228. (setq pos (point))
  9229. (setq parent-pos (and (org-up-heading-safe) (point)))
  9230. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9231. (when (and (org-entry-get (point) "ORDERED")
  9232. (forward-line 1)
  9233. (re-search-forward org-not-done-heading-regexp pos t))
  9234. (throw 'dont-block nil))))))) ; block, older sibling not done.
  9235. (defcustom org-track-ordered-property-with-tag nil
  9236. "Should the ORDERED property also be shown as a tag?
  9237. The ORDERED property decides if an entry should require subtasks to be
  9238. completed in sequence. Since a property is not very visible, setting
  9239. this option means that toggling the ORDERED property with the command
  9240. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9241. not relevant for the behavior, but it makes things more visible.
  9242. Note that toggling the tag with tags commands will not change the property
  9243. and therefore not influence behavior!
  9244. This can be t, meaning the tag ORDERED should be used, It can also be a
  9245. string to select a different tag for this task."
  9246. :group 'org-todo
  9247. :type '(choice
  9248. (const :tag "No tracking" nil)
  9249. (const :tag "Track with ORDERED tag" t)
  9250. (string :tag "Use other tag")))
  9251. (defun org-toggle-ordered-property ()
  9252. "Toggle the ORDERED property of the current entry.
  9253. For better visibility, you can track the value of this property with a tag.
  9254. See variable `org-track-ordered-property-with-tag'."
  9255. (interactive)
  9256. (let* ((t1 org-track-ordered-property-with-tag)
  9257. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9258. (save-excursion
  9259. (org-back-to-heading)
  9260. (if (org-entry-get nil "ORDERED")
  9261. (progn
  9262. (org-delete-property "ORDERED")
  9263. (and tag (org-toggle-tag tag 'off))
  9264. (message "Subtasks can be completed in arbitrary order"))
  9265. (org-entry-put nil "ORDERED" "t")
  9266. (and tag (org-toggle-tag tag 'on))
  9267. (message "Subtasks must be completed in sequence")))))
  9268. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9269. (defun org-block-todo-from-checkboxes (change-plist)
  9270. "Block turning an entry into a TODO, using checkboxes.
  9271. This checks whether the current task should be blocked from state
  9272. changes because there are unchecked boxes in this entry."
  9273. (catch 'dont-block
  9274. ;; If this is not a todo state change, or if this entry is already DONE,
  9275. ;; do not block
  9276. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9277. (member (plist-get change-plist :from)
  9278. (cons 'done org-done-keywords))
  9279. (member (plist-get change-plist :to)
  9280. (cons 'todo org-not-done-keywords))
  9281. (not (plist-get change-plist :to)))
  9282. (throw 'dont-block t))
  9283. ;; If this task has checkboxes that are not checked, it's blocked
  9284. (save-excursion
  9285. (org-back-to-heading t)
  9286. (let ((beg (point)) end)
  9287. (outline-next-heading)
  9288. (setq end (point))
  9289. (goto-char beg)
  9290. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9291. end t)
  9292. (progn
  9293. (if (boundp 'org-blocked-by-checkboxes)
  9294. (setq org-blocked-by-checkboxes t))
  9295. (throw 'dont-block nil)))))
  9296. t)) ; do not block
  9297. (defun org-entry-blocked-p ()
  9298. "Is the current entry blocked?"
  9299. (if (org-entry-get nil "NOBLOCKING")
  9300. nil ;; Never block this entry
  9301. (not
  9302. (run-hook-with-args-until-failure
  9303. 'org-blocker-hook
  9304. (list :type 'todo-state-change
  9305. :position (point)
  9306. :from 'todo
  9307. :to 'done)))))
  9308. (defun org-update-statistics-cookies (all)
  9309. "Update the statistics cookie, either from TODO or from checkboxes.
  9310. This should be called with the cursor in a line with a statistics cookie."
  9311. (interactive "P")
  9312. (if all
  9313. (progn
  9314. (org-update-checkbox-count 'all)
  9315. (org-map-entries 'org-update-parent-todo-statistics))
  9316. (if (not (org-on-heading-p))
  9317. (org-update-checkbox-count)
  9318. (let ((pos (move-marker (make-marker) (point)))
  9319. end l1 l2)
  9320. (ignore-errors (org-back-to-heading t))
  9321. (if (not (org-on-heading-p))
  9322. (org-update-checkbox-count)
  9323. (setq l1 (org-outline-level))
  9324. (setq end (save-excursion
  9325. (outline-next-heading)
  9326. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9327. (point)))
  9328. (if (and (save-excursion
  9329. (re-search-forward
  9330. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9331. (not (save-excursion (re-search-forward
  9332. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9333. (org-update-checkbox-count)
  9334. (if (and l2 (> l2 l1))
  9335. (progn
  9336. (goto-char end)
  9337. (org-update-parent-todo-statistics))
  9338. (goto-char pos)
  9339. (beginning-of-line 1)
  9340. (while (re-search-forward
  9341. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9342. (point-at-eol) t)
  9343. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9344. (goto-char pos)
  9345. (move-marker pos nil)))))
  9346. (defvar org-entry-property-inherited-from) ;; defined below
  9347. (defun org-update-parent-todo-statistics ()
  9348. "Update any statistics cookie in the parent of the current headline.
  9349. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9350. the entire subtree and this will travel up the hierarchy and update
  9351. statistics everywhere."
  9352. (interactive)
  9353. (let* ((lim 0) prop
  9354. (recursive (or (not org-hierarchical-todo-statistics)
  9355. (string-match
  9356. "\\<recursive\\>"
  9357. (or (setq prop (org-entry-get
  9358. nil "COOKIE_DATA" 'inherit)) ""))))
  9359. (lim (or (and prop (marker-position
  9360. org-entry-property-inherited-from))
  9361. lim))
  9362. (first t)
  9363. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9364. level ltoggle l1 new ndel
  9365. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9366. (catch 'exit
  9367. (save-excursion
  9368. (beginning-of-line 1)
  9369. (if (org-at-heading-p)
  9370. (setq ltoggle (funcall outline-level))
  9371. (error "This should not happen"))
  9372. (while (and (setq level (org-up-heading-safe))
  9373. (or recursive first)
  9374. (>= (point) lim))
  9375. (setq first nil cookie-present nil)
  9376. (unless (and level
  9377. (not (string-match
  9378. "\\<checkbox\\>"
  9379. (downcase
  9380. (or (org-entry-get
  9381. nil "COOKIE_DATA")
  9382. "")))))
  9383. (throw 'exit nil))
  9384. (while (re-search-forward box-re (point-at-eol) t)
  9385. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9386. (setq is-percent (match-end 2))
  9387. (save-match-data
  9388. (unless (outline-next-heading) (throw 'exit nil))
  9389. (while (and (looking-at org-complex-heading-regexp)
  9390. (> (setq l1 (length (match-string 1))) level))
  9391. (setq kwd (and (or recursive (= l1 ltoggle))
  9392. (match-string 2)))
  9393. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9394. (and (listp org-provide-todo-statistics)
  9395. (or (member kwd org-provide-todo-statistics)
  9396. (member kwd org-done-keywords))))
  9397. (setq cnt-all (1+ cnt-all))
  9398. (if (eq org-provide-todo-statistics t)
  9399. (and kwd (setq cnt-all (1+ cnt-all)))))
  9400. (and (member kwd org-done-keywords)
  9401. (setq cnt-done (1+ cnt-done)))
  9402. (outline-next-heading)))
  9403. (setq new
  9404. (if is-percent
  9405. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9406. (format "[%d/%d]" cnt-done cnt-all))
  9407. ndel (- (match-end 0) (match-beginning 0)))
  9408. (goto-char (match-beginning 0))
  9409. (insert new)
  9410. (delete-region (point) (+ (point) ndel)))
  9411. (when cookie-present
  9412. (run-hook-with-args 'org-after-todo-statistics-hook
  9413. cnt-done (- cnt-all cnt-done))))))
  9414. (run-hooks 'org-todo-statistics-hook)))
  9415. (defvar org-after-todo-statistics-hook nil
  9416. "Hook that is called after a TODO statistics cookie has been updated.
  9417. Each function is called with two arguments: the number of not-done entries
  9418. and the number of done entries.
  9419. For example, the following function, when added to this hook, will switch
  9420. an entry to DONE when all children are done, and back to TODO when new
  9421. entries are set to a TODO status. Note that this hook is only called
  9422. when there is a statistics cookie in the headline!
  9423. (defun org-summary-todo (n-done n-not-done)
  9424. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9425. (let (org-log-done org-log-states) ; turn off logging
  9426. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9427. ")
  9428. (defvar org-todo-statistics-hook nil
  9429. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9430. This hook runs even if there is no statistics cookie present, in which case
  9431. `org-after-todo-statistics-hook' would not run.")
  9432. (defun org-todo-trigger-tag-changes (state)
  9433. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9434. (let ((l org-todo-state-tags-triggers)
  9435. changes)
  9436. (when (or (not state) (equal state ""))
  9437. (setq changes (append changes (cdr (assoc "" l)))))
  9438. (when (and (stringp state) (> (length state) 0))
  9439. (setq changes (append changes (cdr (assoc state l)))))
  9440. (when (member state org-not-done-keywords)
  9441. (setq changes (append changes (cdr (assoc 'todo l)))))
  9442. (when (member state org-done-keywords)
  9443. (setq changes (append changes (cdr (assoc 'done l)))))
  9444. (dolist (c changes)
  9445. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9446. (defun org-local-logging (value)
  9447. "Get logging settings from a property VALUE."
  9448. (let* (words w a)
  9449. ;; directly set the variables, they are already local.
  9450. (setq org-log-done nil
  9451. org-log-repeat nil
  9452. org-todo-log-states nil)
  9453. (setq words (org-split-string value))
  9454. (while (setq w (pop words))
  9455. (cond
  9456. ((setq a (assoc w org-startup-options))
  9457. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9458. (set (nth 1 a) (nth 2 a))))
  9459. ((setq a (org-extract-log-state-settings w))
  9460. (and (member (car a) org-todo-keywords-1)
  9461. (push a org-todo-log-states)))))))
  9462. (defun org-get-todo-sequence-head (kwd)
  9463. "Return the head of the TODO sequence to which KWD belongs.
  9464. If KWD is not set, check if there is a text property remembering the
  9465. right sequence."
  9466. (let (p)
  9467. (cond
  9468. ((not kwd)
  9469. (or (get-text-property (point-at-bol) 'org-todo-head)
  9470. (progn
  9471. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9472. nil (point-at-eol)))
  9473. (get-text-property p 'org-todo-head))))
  9474. ((not (member kwd org-todo-keywords-1))
  9475. (car org-todo-keywords-1))
  9476. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9477. (defun org-fast-todo-selection ()
  9478. "Fast TODO keyword selection with single keys.
  9479. Returns the new TODO keyword, or nil if no state change should occur."
  9480. (let* ((fulltable org-todo-key-alist)
  9481. (done-keywords org-done-keywords) ;; needed for the faces.
  9482. (maxlen (apply 'max (mapcar
  9483. (lambda (x)
  9484. (if (stringp (car x)) (string-width (car x)) 0))
  9485. fulltable)))
  9486. (expert nil)
  9487. (fwidth (+ maxlen 3 1 3))
  9488. (ncol (/ (- (window-width) 4) fwidth))
  9489. tg cnt e c tbl
  9490. groups ingroup)
  9491. (save-excursion
  9492. (save-window-excursion
  9493. (if expert
  9494. (set-buffer (get-buffer-create " *Org todo*"))
  9495. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9496. (erase-buffer)
  9497. (org-set-local 'org-done-keywords done-keywords)
  9498. (setq tbl fulltable cnt 0)
  9499. (while (setq e (pop tbl))
  9500. (cond
  9501. ((equal e '(:startgroup))
  9502. (push '() groups) (setq ingroup t)
  9503. (when (not (= cnt 0))
  9504. (setq cnt 0)
  9505. (insert "\n"))
  9506. (insert "{ "))
  9507. ((equal e '(:endgroup))
  9508. (setq ingroup nil cnt 0)
  9509. (insert "}\n"))
  9510. ((equal e '(:newline))
  9511. (when (not (= cnt 0))
  9512. (setq cnt 0)
  9513. (insert "\n")
  9514. (setq e (car tbl))
  9515. (while (equal (car tbl) '(:newline))
  9516. (insert "\n")
  9517. (setq tbl (cdr tbl)))))
  9518. (t
  9519. (setq tg (car e) c (cdr e))
  9520. (if ingroup (push tg (car groups)))
  9521. (setq tg (org-add-props tg nil 'face
  9522. (org-get-todo-face tg)))
  9523. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9524. (insert "[" c "] " tg (make-string
  9525. (- fwidth 4 (length tg)) ?\ ))
  9526. (when (= (setq cnt (1+ cnt)) ncol)
  9527. (insert "\n")
  9528. (if ingroup (insert " "))
  9529. (setq cnt 0)))))
  9530. (insert "\n")
  9531. (goto-char (point-min))
  9532. (if (not expert) (org-fit-window-to-buffer))
  9533. (message "[a-z..]:Set [SPC]:clear")
  9534. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9535. (cond
  9536. ((or (= c ?\C-g)
  9537. (and (= c ?q) (not (rassoc c fulltable))))
  9538. (setq quit-flag t))
  9539. ((= c ?\ ) nil)
  9540. ((setq e (rassoc c fulltable) tg (car e))
  9541. tg)
  9542. (t (setq quit-flag t)))))))
  9543. (defun org-entry-is-todo-p ()
  9544. (member (org-get-todo-state) org-not-done-keywords))
  9545. (defun org-entry-is-done-p ()
  9546. (member (org-get-todo-state) org-done-keywords))
  9547. (defun org-get-todo-state ()
  9548. (save-excursion
  9549. (org-back-to-heading t)
  9550. (and (looking-at org-todo-line-regexp)
  9551. (match-end 2)
  9552. (match-string 2))))
  9553. (defun org-at-date-range-p (&optional inactive-ok)
  9554. "Is the cursor inside a date range?"
  9555. (interactive)
  9556. (save-excursion
  9557. (catch 'exit
  9558. (let ((pos (point)))
  9559. (skip-chars-backward "^[<\r\n")
  9560. (skip-chars-backward "<[")
  9561. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9562. (>= (match-end 0) pos)
  9563. (throw 'exit t))
  9564. (skip-chars-backward "^<[\r\n")
  9565. (skip-chars-backward "<[")
  9566. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9567. (>= (match-end 0) pos)
  9568. (throw 'exit t)))
  9569. nil)))
  9570. (defun org-get-repeat (&optional tagline)
  9571. "Check if there is a deadline/schedule with repeater in this entry."
  9572. (save-match-data
  9573. (save-excursion
  9574. (org-back-to-heading t)
  9575. (and (re-search-forward (if tagline
  9576. (concat tagline "\\s-*" org-repeat-re)
  9577. org-repeat-re)
  9578. (org-entry-end-position) t)
  9579. (match-string-no-properties 1)))))
  9580. (defvar org-last-changed-timestamp)
  9581. (defvar org-last-inserted-timestamp)
  9582. (defvar org-log-post-message)
  9583. (defvar org-log-note-purpose)
  9584. (defvar org-log-note-how)
  9585. (defvar org-log-note-extra)
  9586. (defun org-auto-repeat-maybe (done-word)
  9587. "Check if the current headline contains a repeated deadline/schedule.
  9588. If yes, set TODO state back to what it was and change the base date
  9589. of repeating deadline/scheduled time stamps to new date.
  9590. This function is run automatically after each state change to a DONE state."
  9591. ;; last-state is dynamically scoped into this function
  9592. (let* ((repeat (org-get-repeat))
  9593. (aa (assoc last-state org-todo-kwd-alist))
  9594. (interpret (nth 1 aa))
  9595. (head (nth 2 aa))
  9596. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9597. (msg "Entry repeats: ")
  9598. (org-log-done nil)
  9599. (org-todo-log-states nil)
  9600. (nshiftmax 10) (nshift 0)
  9601. re type n what ts time)
  9602. (when repeat
  9603. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9604. (org-todo (if (eq interpret 'type) last-state head))
  9605. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9606. (org-time-stamp-format t t)))
  9607. (when org-log-repeat
  9608. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9609. (memq 'org-add-log-note post-command-hook))
  9610. ;; OK, we are already setup for some record
  9611. (if (eq org-log-repeat 'note)
  9612. ;; make sure we take a note, not only a time stamp
  9613. (setq org-log-note-how 'note))
  9614. ;; Set up for taking a record
  9615. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9616. last-state
  9617. 'findpos org-log-repeat)))
  9618. (org-back-to-heading t)
  9619. (org-add-planning-info nil nil 'closed)
  9620. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9621. org-deadline-time-regexp "\\)\\|\\("
  9622. org-ts-regexp "\\)"))
  9623. (while (re-search-forward
  9624. re (save-excursion (outline-next-heading) (point)) t)
  9625. (setq type (if (match-end 1) org-scheduled-string
  9626. (if (match-end 3) org-deadline-string "Plain:"))
  9627. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9628. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9629. (setq n (string-to-number (match-string 2 ts))
  9630. what (match-string 3 ts))
  9631. (if (equal what "w") (setq n (* n 7) what "d"))
  9632. ;; Preparation, see if we need to modify the start date for the change
  9633. (when (match-end 1)
  9634. (setq time (save-match-data (org-time-string-to-time ts)))
  9635. (cond
  9636. ((equal (match-string 1 ts) ".")
  9637. ;; Shift starting date to today
  9638. (org-timestamp-change
  9639. (- (time-to-days (current-time)) (time-to-days time))
  9640. 'day))
  9641. ((equal (match-string 1 ts) "+")
  9642. (while (or (= nshift 0)
  9643. (<= (time-to-days time) (time-to-days (current-time))))
  9644. (when (= (incf nshift) nshiftmax)
  9645. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9646. (error "Abort")))
  9647. (org-timestamp-change n (cdr (assoc what whata)))
  9648. (org-at-timestamp-p t)
  9649. (setq ts (match-string 1))
  9650. (setq time (save-match-data (org-time-string-to-time ts))))
  9651. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9652. ;; rematch, so that we have everything in place for the real shift
  9653. (org-at-timestamp-p t)
  9654. (setq ts (match-string 1))
  9655. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9656. (org-timestamp-change n (cdr (assoc what whata)))
  9657. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9658. (setq org-log-post-message msg)
  9659. (message "%s" msg))))
  9660. (defun org-show-todo-tree (arg)
  9661. "Make a compact tree which shows all headlines marked with TODO.
  9662. The tree will show the lines where the regexp matches, and all higher
  9663. headlines above the match.
  9664. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9665. With a numeric prefix N, construct a sparse tree for the Nth element
  9666. of `org-todo-keywords-1'."
  9667. (interactive "P")
  9668. (let ((case-fold-search nil)
  9669. (kwd-re
  9670. (cond ((null arg) org-not-done-regexp)
  9671. ((equal arg '(4))
  9672. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9673. (mapcar 'list org-todo-keywords-1))))
  9674. (concat "\\("
  9675. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9676. "\\)\\>")))
  9677. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9678. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9679. org-todo-keywords-1)))
  9680. (t (error "Invalid prefix argument: %s" arg)))))
  9681. (message "%d TODO entries found"
  9682. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9683. (defun org-deadline (&optional remove time)
  9684. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9685. With argument REMOVE, remove any deadline from the item.
  9686. When TIME is set, it should be an internal time specification, and the
  9687. scheduling will use the corresponding date."
  9688. (interactive "P")
  9689. (let ((old-date (org-entry-get nil "DEADLINE")))
  9690. (if remove
  9691. (progn
  9692. (when (and old-date org-log-redeadline)
  9693. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9694. org-log-redeadline))
  9695. (org-remove-timestamp-with-keyword org-deadline-string)
  9696. (message "Item no longer has a deadline."))
  9697. (if (org-get-repeat)
  9698. (error "Cannot change deadline on task with repeater, please do that by hand")
  9699. (org-add-planning-info 'deadline time 'closed)
  9700. (when (and old-date org-log-redeadline
  9701. (not (equal old-date
  9702. (substring org-last-inserted-timestamp 1 -1))))
  9703. (org-add-log-setup 'redeadline nil old-date 'findpos
  9704. org-log-redeadline))
  9705. (message "Deadline on %s" org-last-inserted-timestamp)))))
  9706. (defun org-schedule (&optional remove time)
  9707. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9708. With argument REMOVE, remove any scheduling date from the item.
  9709. When TIME is set, it should be an internal time specification, and the
  9710. scheduling will use the corresponding date."
  9711. (interactive "P")
  9712. (let ((old-date (org-entry-get nil "SCHEDULED")))
  9713. (if remove
  9714. (progn
  9715. (when (and old-date org-log-reschedule)
  9716. (org-add-log-setup 'delschedule nil old-date 'findpos
  9717. org-log-reschedule))
  9718. (org-remove-timestamp-with-keyword org-scheduled-string)
  9719. (message "Item is no longer scheduled."))
  9720. (if (org-get-repeat)
  9721. (error "Cannot reschedule task with repeater, please do that by hand")
  9722. (org-add-planning-info 'scheduled time 'closed)
  9723. (when (and old-date org-log-reschedule
  9724. (not (equal old-date
  9725. (substring org-last-inserted-timestamp 1 -1))))
  9726. (org-add-log-setup 'reschedule nil old-date 'findpos
  9727. org-log-reschedule))
  9728. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  9729. (defun org-get-scheduled-time (pom &optional inherit)
  9730. "Get the scheduled time as a time tuple, of a format suitable
  9731. for calling org-schedule with, or if there is no scheduling,
  9732. returns nil."
  9733. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9734. (when time
  9735. (apply 'encode-time (org-parse-time-string time)))))
  9736. (defun org-get-deadline-time (pom &optional inherit)
  9737. "Get the deadine as a time tuple, of a format suitable for
  9738. calling org-deadline with, or if there is no scheduling, returns
  9739. nil."
  9740. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9741. (when time
  9742. (apply 'encode-time (org-parse-time-string time)))))
  9743. (defun org-remove-timestamp-with-keyword (keyword)
  9744. "Remove all time stamps with KEYWORD in the current entry."
  9745. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9746. beg)
  9747. (save-excursion
  9748. (org-back-to-heading t)
  9749. (setq beg (point))
  9750. (outline-next-heading)
  9751. (while (re-search-backward re beg t)
  9752. (replace-match "")
  9753. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9754. (equal (char-before) ?\ ))
  9755. (backward-delete-char 1)
  9756. (if (string-match "^[ \t]*$" (buffer-substring
  9757. (point-at-bol) (point-at-eol)))
  9758. (delete-region (point-at-bol)
  9759. (min (point-max) (1+ (point-at-eol))))))))))
  9760. (defun org-add-planning-info (what &optional time &rest remove)
  9761. "Insert new timestamp with keyword in the line directly after the headline.
  9762. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9763. If non is given, the user is prompted for a date.
  9764. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9765. be removed."
  9766. (interactive)
  9767. (let (org-time-was-given org-end-time-was-given ts
  9768. end default-time default-input)
  9769. (catch 'exit
  9770. (when (and (not time) (memq what '(scheduled deadline)))
  9771. ;; Try to get a default date/time from existing timestamp
  9772. (save-excursion
  9773. (org-back-to-heading t)
  9774. (setq end (save-excursion (outline-next-heading) (point)))
  9775. (when (re-search-forward (if (eq what 'scheduled)
  9776. org-scheduled-time-regexp
  9777. org-deadline-time-regexp)
  9778. end t)
  9779. (setq ts (match-string 1)
  9780. default-time
  9781. (apply 'encode-time (org-parse-time-string ts))
  9782. default-input (and ts (org-get-compact-tod ts))))))
  9783. (when what
  9784. ;; If necessary, get the time from the user
  9785. (setq time (or time (org-read-date nil 'to-time nil nil
  9786. default-time default-input))))
  9787. (when (and org-insert-labeled-timestamps-at-point
  9788. (member what '(scheduled deadline)))
  9789. (insert
  9790. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9791. (org-insert-time-stamp time org-time-was-given
  9792. nil nil nil (list org-end-time-was-given))
  9793. (setq what nil))
  9794. (save-excursion
  9795. (save-restriction
  9796. (let (col list elt ts buffer-invisibility-spec)
  9797. (org-back-to-heading t)
  9798. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9799. (goto-char (match-end 1))
  9800. (setq col (current-column))
  9801. (goto-char (match-end 0))
  9802. (if (eobp) (insert "\n") (forward-char 1))
  9803. (when (and (not what)
  9804. (not (looking-at
  9805. (concat "[ \t]*"
  9806. org-keyword-time-not-clock-regexp))))
  9807. ;; Nothing to add, nothing to remove...... :-)
  9808. (throw 'exit nil))
  9809. (if (and (not (looking-at outline-regexp))
  9810. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9811. "[^\r\n]*"))
  9812. (not (equal (match-string 1) org-clock-string)))
  9813. (narrow-to-region (match-beginning 0) (match-end 0))
  9814. (insert-before-markers "\n")
  9815. (backward-char 1)
  9816. (narrow-to-region (point) (point))
  9817. (and org-adapt-indentation (org-indent-to-column col)))
  9818. ;; Check if we have to remove something.
  9819. (setq list (cons what remove))
  9820. (while list
  9821. (setq elt (pop list))
  9822. (goto-char (point-min))
  9823. (when (or (and (eq elt 'scheduled)
  9824. (re-search-forward org-scheduled-time-regexp nil t))
  9825. (and (eq elt 'deadline)
  9826. (re-search-forward org-deadline-time-regexp nil t))
  9827. (and (eq elt 'closed)
  9828. (re-search-forward org-closed-time-regexp nil t)))
  9829. (replace-match "")
  9830. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9831. (skip-chars-backward " ")
  9832. (if (looking-at " +") (replace-match ""))))
  9833. (goto-char (point-max))
  9834. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  9835. (when what
  9836. (insert
  9837. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9838. (cond ((eq what 'scheduled) org-scheduled-string)
  9839. ((eq what 'deadline) org-deadline-string)
  9840. ((eq what 'closed) org-closed-string))
  9841. " ")
  9842. (setq ts (org-insert-time-stamp
  9843. time
  9844. (or org-time-was-given
  9845. (and (eq what 'closed) org-log-done-with-time))
  9846. (eq what 'closed)
  9847. nil nil (list org-end-time-was-given)))
  9848. (end-of-line 1))
  9849. (goto-char (point-min))
  9850. (widen)
  9851. (if (and (looking-at "[ \t]+\n")
  9852. (equal (char-before) ?\n))
  9853. (delete-region (1- (point)) (point-at-eol)))
  9854. ts))))))
  9855. (defvar org-log-note-marker (make-marker))
  9856. (defvar org-log-note-purpose nil)
  9857. (defvar org-log-note-state nil)
  9858. (defvar org-log-note-previous-state nil)
  9859. (defvar org-log-note-how nil)
  9860. (defvar org-log-note-extra nil)
  9861. (defvar org-log-note-window-configuration nil)
  9862. (defvar org-log-note-return-to (make-marker))
  9863. (defvar org-log-post-message nil
  9864. "Message to be displayed after a log note has been stored.
  9865. The auto-repeater uses this.")
  9866. (defun org-add-note ()
  9867. "Add a note to the current entry.
  9868. This is done in the same way as adding a state change note."
  9869. (interactive)
  9870. (org-add-log-setup 'note nil nil 'findpos nil))
  9871. (defvar org-property-end-re)
  9872. (defun org-add-log-setup (&optional purpose state prev-state
  9873. findpos how &optional extra)
  9874. "Set up the post command hook to take a note.
  9875. If this is about to TODO state change, the new state is expected in STATE.
  9876. When FINDPOS is non-nil, find the correct position for the note in
  9877. the current entry. If not, assume that it can be inserted at point.
  9878. HOW is an indicator what kind of note should be created.
  9879. EXTRA is additional text that will be inserted into the notes buffer."
  9880. (let* ((org-log-into-drawer (org-log-into-drawer))
  9881. (drawer (cond ((stringp org-log-into-drawer)
  9882. org-log-into-drawer)
  9883. (org-log-into-drawer "LOGBOOK")
  9884. (t nil))))
  9885. (save-restriction
  9886. (save-excursion
  9887. (when findpos
  9888. (org-back-to-heading t)
  9889. (narrow-to-region (point) (save-excursion
  9890. (outline-next-heading) (point)))
  9891. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9892. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9893. "[^\r\n]*\\)?"))
  9894. (goto-char (match-end 0))
  9895. (cond
  9896. (drawer
  9897. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9898. nil t)
  9899. (progn
  9900. (goto-char (match-end 0))
  9901. (or org-log-states-order-reversed
  9902. (and (re-search-forward org-property-end-re nil t)
  9903. (goto-char (1- (match-beginning 0))))))
  9904. (insert "\n:" drawer ":\n:END:")
  9905. (beginning-of-line 0)
  9906. (org-indent-line-function)
  9907. (beginning-of-line 2)
  9908. (org-indent-line-function)
  9909. (end-of-line 0)))
  9910. ((and org-log-state-notes-insert-after-drawers
  9911. (save-excursion
  9912. (forward-line) (looking-at org-drawer-regexp)))
  9913. (forward-line)
  9914. (while (looking-at org-drawer-regexp)
  9915. (goto-char (match-end 0))
  9916. (re-search-forward org-property-end-re (point-max) t)
  9917. (forward-line))
  9918. (forward-line -1)))
  9919. (unless org-log-states-order-reversed
  9920. (and (= (char-after) ?\n) (forward-char 1))
  9921. (org-skip-over-state-notes)
  9922. (skip-chars-backward " \t\n\r")))
  9923. (move-marker org-log-note-marker (point))
  9924. (setq org-log-note-purpose purpose
  9925. org-log-note-state state
  9926. org-log-note-previous-state prev-state
  9927. org-log-note-how how
  9928. org-log-note-extra extra)
  9929. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9930. (defun org-skip-over-state-notes ()
  9931. "Skip past the list of State notes in an entry."
  9932. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9933. (while (looking-at "[ \t]*- State")
  9934. (condition-case nil
  9935. (org-next-item)
  9936. (error (org-end-of-item)))))
  9937. (defun org-add-log-note (&optional purpose)
  9938. "Pop up a window for taking a note, and add this note later at point."
  9939. (remove-hook 'post-command-hook 'org-add-log-note)
  9940. (setq org-log-note-window-configuration (current-window-configuration))
  9941. (delete-other-windows)
  9942. (move-marker org-log-note-return-to (point))
  9943. (switch-to-buffer (marker-buffer org-log-note-marker))
  9944. (goto-char org-log-note-marker)
  9945. (org-switch-to-buffer-other-window "*Org Note*")
  9946. (erase-buffer)
  9947. (if (memq org-log-note-how '(time state))
  9948. (let (current-prefix-arg) (org-store-log-note))
  9949. (let ((org-inhibit-startup t)) (org-mode))
  9950. (insert (format "# Insert note for %s.
  9951. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9952. (cond
  9953. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9954. ((eq org-log-note-purpose 'done) "closed todo item")
  9955. ((eq org-log-note-purpose 'state)
  9956. (format "state change from \"%s\" to \"%s\""
  9957. (or org-log-note-previous-state "")
  9958. (or org-log-note-state "")))
  9959. ((eq org-log-note-purpose 'reschedule)
  9960. "rescheduling")
  9961. ((eq org-log-note-purpose 'delschedule)
  9962. "no longer scheduled")
  9963. ((eq org-log-note-purpose 'redeadline)
  9964. "changing deadline")
  9965. ((eq org-log-note-purpose 'deldeadline)
  9966. "removing deadline")
  9967. ((eq org-log-note-purpose 'note)
  9968. "this entry")
  9969. (t (error "This should not happen")))))
  9970. (if org-log-note-extra (insert org-log-note-extra))
  9971. (org-set-local 'org-finish-function 'org-store-log-note)))
  9972. (defvar org-note-abort nil) ; dynamically scoped
  9973. (defun org-store-log-note ()
  9974. "Finish taking a log note, and insert it to where it belongs."
  9975. (let ((txt (buffer-string))
  9976. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9977. lines ind)
  9978. (kill-buffer (current-buffer))
  9979. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9980. (setq txt (replace-match "" t t txt)))
  9981. (if (string-match "\\s-+\\'" txt)
  9982. (setq txt (replace-match "" t t txt)))
  9983. (setq lines (org-split-string txt "\n"))
  9984. (when (and note (string-match "\\S-" note))
  9985. (setq note
  9986. (org-replace-escapes
  9987. note
  9988. (list (cons "%u" (user-login-name))
  9989. (cons "%U" user-full-name)
  9990. (cons "%t" (format-time-string
  9991. (org-time-stamp-format 'long 'inactive)
  9992. (current-time)))
  9993. (cons "%s" (if org-log-note-state
  9994. (concat "\"" org-log-note-state "\"")
  9995. ""))
  9996. (cons "%S" (if org-log-note-previous-state
  9997. (concat "\"" org-log-note-previous-state "\"")
  9998. "\"\"")))))
  9999. (if lines (setq note (concat note " \\\\")))
  10000. (push note lines))
  10001. (when (or current-prefix-arg org-note-abort)
  10002. (when org-log-into-drawer
  10003. (org-remove-empty-drawer-at
  10004. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10005. org-log-note-marker))
  10006. (setq lines nil))
  10007. (when lines
  10008. (with-current-buffer (marker-buffer org-log-note-marker)
  10009. (save-excursion
  10010. (goto-char org-log-note-marker)
  10011. (move-marker org-log-note-marker nil)
  10012. (end-of-line 1)
  10013. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10014. (insert "- " (pop lines))
  10015. (org-indent-line-function)
  10016. (beginning-of-line 1)
  10017. (looking-at "[ \t]*")
  10018. (setq ind (concat (match-string 0) " "))
  10019. (end-of-line 1)
  10020. (while lines (insert "\n" ind (pop lines)))
  10021. (message "Note stored")
  10022. (org-back-to-heading t)
  10023. (org-cycle-hide-drawers 'children)))))
  10024. (set-window-configuration org-log-note-window-configuration)
  10025. (with-current-buffer (marker-buffer org-log-note-return-to)
  10026. (goto-char org-log-note-return-to))
  10027. (move-marker org-log-note-return-to nil)
  10028. (and org-log-post-message (message "%s" org-log-post-message)))
  10029. (defun org-remove-empty-drawer-at (drawer pos)
  10030. "Remove an empty drawer DRAWER at position POS.
  10031. POS may also be a marker."
  10032. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10033. (save-excursion
  10034. (save-restriction
  10035. (widen)
  10036. (goto-char pos)
  10037. (if (org-in-regexp
  10038. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10039. (replace-match ""))))))
  10040. (defun org-sparse-tree (&optional arg)
  10041. "Create a sparse tree, prompt for the details.
  10042. This command can create sparse trees. You first need to select the type
  10043. of match used to create the tree:
  10044. t Show entries with a specific TODO keyword.
  10045. m Show entries selected by a tags/property match.
  10046. p Enter a property name and its value (both with completion on existing
  10047. names/values) and show entries with that property.
  10048. / Show entries matching a regular expression (`r' can be used as well)
  10049. d Show deadlines due within `org-deadline-warning-days'.
  10050. b Show deadlines and scheduled items before a date.
  10051. a Show deadlines and scheduled items after a date."
  10052. (interactive "P")
  10053. (let (ans kwd value)
  10054. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10055. (setq ans (read-char-exclusive))
  10056. (cond
  10057. ((equal ans ?d)
  10058. (call-interactively 'org-check-deadlines))
  10059. ((equal ans ?b)
  10060. (call-interactively 'org-check-before-date))
  10061. ((equal ans ?a)
  10062. (call-interactively 'org-check-after-date))
  10063. ((equal ans ?t)
  10064. (org-show-todo-tree '(4)))
  10065. ((member ans '(?T ?m))
  10066. (call-interactively 'org-match-sparse-tree))
  10067. ((member ans '(?p ?P))
  10068. (setq kwd (org-icompleting-read "Property: "
  10069. (mapcar 'list (org-buffer-property-keys))))
  10070. (setq value (org-icompleting-read "Value: "
  10071. (mapcar 'list (org-property-values kwd))))
  10072. (unless (string-match "\\`{.*}\\'" value)
  10073. (setq value (concat "\"" value "\"")))
  10074. (org-match-sparse-tree arg (concat kwd "=" value)))
  10075. ((member ans '(?r ?R ?/))
  10076. (call-interactively 'org-occur))
  10077. (t (error "No such sparse tree command \"%c\"" ans)))))
  10078. (defvar org-occur-highlights nil
  10079. "List of overlays used for occur matches.")
  10080. (make-variable-buffer-local 'org-occur-highlights)
  10081. (defvar org-occur-parameters nil
  10082. "Parameters of the active org-occur calls.
  10083. This is a list, each call to org-occur pushes as cons cell,
  10084. containing the regular expression and the callback, onto the list.
  10085. The list can contain several entries if `org-occur' has been called
  10086. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10087. will only contain one set of parameters. When the highlights are
  10088. removed (for example with `C-c C-c', or with the next edit (depending
  10089. on `org-remove-highlights-with-change'), this variable is emptied
  10090. as well.")
  10091. (make-variable-buffer-local 'org-occur-parameters)
  10092. (defun org-occur (regexp &optional keep-previous callback)
  10093. "Make a compact tree which shows all matches of REGEXP.
  10094. The tree will show the lines where the regexp matches, and all higher
  10095. headlines above the match. It will also show the heading after the match,
  10096. to make sure editing the matching entry is easy.
  10097. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10098. call to `org-occur' will be kept, to allow stacking of calls to this
  10099. command.
  10100. If CALLBACK is non-nil, it is a function which is called to confirm
  10101. that the match should indeed be shown."
  10102. (interactive "sRegexp: \nP")
  10103. (when (equal regexp "")
  10104. (error "Regexp cannot be empty"))
  10105. (unless keep-previous
  10106. (org-remove-occur-highlights nil nil t))
  10107. (push (cons regexp callback) org-occur-parameters)
  10108. (let ((cnt 0))
  10109. (save-excursion
  10110. (goto-char (point-min))
  10111. (if (or (not keep-previous) ; do not want to keep
  10112. (not org-occur-highlights)) ; no previous matches
  10113. ;; hide everything
  10114. (org-overview))
  10115. (while (re-search-forward regexp nil t)
  10116. (when (or (not callback)
  10117. (save-match-data (funcall callback)))
  10118. (setq cnt (1+ cnt))
  10119. (when org-highlight-sparse-tree-matches
  10120. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10121. (org-show-context 'occur-tree))))
  10122. (when org-remove-highlights-with-change
  10123. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10124. nil 'local))
  10125. (unless org-sparse-tree-open-archived-trees
  10126. (org-hide-archived-subtrees (point-min) (point-max)))
  10127. (run-hooks 'org-occur-hook)
  10128. (if (interactive-p)
  10129. (message "%d match(es) for regexp %s" cnt regexp))
  10130. cnt))
  10131. (defun org-show-context (&optional key)
  10132. "Make sure point and context and visible.
  10133. How much context is shown depends upon the variables
  10134. `org-show-hierarchy-above', `org-show-following-heading'. and
  10135. `org-show-siblings'."
  10136. (let ((heading-p (org-on-heading-p t))
  10137. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10138. (following-p (org-get-alist-option org-show-following-heading key))
  10139. (entry-p (org-get-alist-option org-show-entry-below key))
  10140. (siblings-p (org-get-alist-option org-show-siblings key)))
  10141. (catch 'exit
  10142. ;; Show heading or entry text
  10143. (if (and heading-p (not entry-p))
  10144. (org-flag-heading nil) ; only show the heading
  10145. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10146. (org-show-hidden-entry))) ; show entire entry
  10147. (when following-p
  10148. ;; Show next sibling, or heading below text
  10149. (save-excursion
  10150. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10151. (org-flag-heading nil))))
  10152. (when siblings-p (org-show-siblings))
  10153. (when hierarchy-p
  10154. ;; show all higher headings, possibly with siblings
  10155. (save-excursion
  10156. (while (and (condition-case nil
  10157. (progn (org-up-heading-all 1) t)
  10158. (error nil))
  10159. (not (bobp)))
  10160. (org-flag-heading nil)
  10161. (when siblings-p (org-show-siblings))))))))
  10162. (defun org-reveal (&optional siblings)
  10163. "Show current entry, hierarchy above it, and the following headline.
  10164. This can be used to show a consistent set of context around locations
  10165. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10166. not t for the search context.
  10167. With optional argument SIBLINGS, on each level of the hierarchy all
  10168. siblings are shown. This repairs the tree structure to what it would
  10169. look like when opened with hierarchical calls to `org-cycle'."
  10170. (interactive "P")
  10171. (let ((org-show-hierarchy-above t)
  10172. (org-show-following-heading t)
  10173. (org-show-siblings (if siblings t org-show-siblings)))
  10174. (org-show-context nil)))
  10175. (defun org-highlight-new-match (beg end)
  10176. "Highlight from BEG to END and mark the highlight is an occur headline."
  10177. (let ((ov (org-make-overlay beg end)))
  10178. (org-overlay-put ov 'face 'secondary-selection)
  10179. (push ov org-occur-highlights)))
  10180. (defun org-remove-occur-highlights (&optional beg end noremove)
  10181. "Remove the occur highlights from the buffer.
  10182. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10183. from the `before-change-functions' in the current buffer."
  10184. (interactive)
  10185. (unless org-inhibit-highlight-removal
  10186. (mapc 'org-delete-overlay org-occur-highlights)
  10187. (setq org-occur-highlights nil)
  10188. (setq org-occur-parameters nil)
  10189. (unless noremove
  10190. (remove-hook 'before-change-functions
  10191. 'org-remove-occur-highlights 'local))))
  10192. ;;;; Priorities
  10193. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10194. "Regular expression matching the priority indicator.")
  10195. (defvar org-remove-priority-next-time nil)
  10196. (defun org-priority-up ()
  10197. "Increase the priority of the current item."
  10198. (interactive)
  10199. (org-priority 'up))
  10200. (defun org-priority-down ()
  10201. "Decrease the priority of the current item."
  10202. (interactive)
  10203. (org-priority 'down))
  10204. (defun org-priority (&optional action)
  10205. "Change the priority of an item by ARG.
  10206. ACTION can be `set', `up', `down', or a character."
  10207. (interactive)
  10208. (unless org-enable-priority-commands
  10209. (error "Priority commands are disabled"))
  10210. (setq action (or action 'set))
  10211. (let (current new news have remove)
  10212. (save-excursion
  10213. (org-back-to-heading t)
  10214. (if (looking-at org-priority-regexp)
  10215. (setq current (string-to-char (match-string 2))
  10216. have t)
  10217. (setq current org-default-priority))
  10218. (cond
  10219. ((eq action 'remove)
  10220. (setq remove t new ?\ ))
  10221. ((or (eq action 'set)
  10222. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10223. (if (not (eq action 'set))
  10224. (setq new action)
  10225. (message "Priority %c-%c, SPC to remove: "
  10226. org-highest-priority org-lowest-priority)
  10227. (setq new (read-char-exclusive)))
  10228. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10229. (= (upcase org-lowest-priority) org-lowest-priority))
  10230. (setq new (upcase new)))
  10231. (cond ((equal new ?\ ) (setq remove t))
  10232. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10233. (error "Priority must be between `%c' and `%c'"
  10234. org-highest-priority org-lowest-priority))))
  10235. ((eq action 'up)
  10236. (if (and (not have) (eq last-command this-command))
  10237. (setq new org-lowest-priority)
  10238. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10239. org-default-priority (1- current)))))
  10240. ((eq action 'down)
  10241. (if (and (not have) (eq last-command this-command))
  10242. (setq new org-highest-priority)
  10243. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10244. org-default-priority (1+ current)))))
  10245. (t (error "Invalid action")))
  10246. (if (or (< (upcase new) org-highest-priority)
  10247. (> (upcase new) org-lowest-priority))
  10248. (setq remove t))
  10249. (setq news (format "%c" new))
  10250. (if have
  10251. (if remove
  10252. (replace-match "" t t nil 1)
  10253. (replace-match news t t nil 2))
  10254. (if remove
  10255. (error "No priority cookie found in line")
  10256. (let ((case-fold-search nil))
  10257. (looking-at org-todo-line-regexp))
  10258. (if (match-end 2)
  10259. (progn
  10260. (goto-char (match-end 2))
  10261. (insert " [#" news "]"))
  10262. (goto-char (match-beginning 3))
  10263. (insert "[#" news "] "))))
  10264. (org-preserve-lc (org-set-tags nil 'align)))
  10265. (if remove
  10266. (message "Priority removed")
  10267. (message "Priority of current item set to %s" news))))
  10268. (defun org-get-priority (s)
  10269. "Find priority cookie and return priority."
  10270. (save-match-data
  10271. (if (not (string-match org-priority-regexp s))
  10272. (* 1000 (- org-lowest-priority org-default-priority))
  10273. (* 1000 (- org-lowest-priority
  10274. (string-to-char (match-string 2 s)))))))
  10275. ;;;; Tags
  10276. (defvar org-agenda-archives-mode)
  10277. (defvar org-map-continue-from nil
  10278. "Position from where mapping should continue.
  10279. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10280. (defvar org-scanner-tags nil
  10281. "The current tag list while the tags scanner is running.")
  10282. (defvar org-trust-scanner-tags nil
  10283. "Should `org-get-tags-at' use the tags fro the scanner.
  10284. This is for internal dynamical scoping only.
  10285. When this is non-nil, the function `org-get-tags-at' will return the value
  10286. of `org-scanner-tags' instead of building the list by itself. This
  10287. can lead to large speed-ups when the tags scanner is used in a file with
  10288. many entries, and when the list of tags is retrieved, for example to
  10289. obtain a list of properties. Building the tags list for each entry in such
  10290. a file becomes an N^2 operation - but with this variable set, it scales
  10291. as N.")
  10292. (defun org-scan-tags (action matcher &optional todo-only)
  10293. "Scan headline tags with inheritance and produce output ACTION.
  10294. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10295. or `agenda' to produce an entry list for an agenda view. It can also be
  10296. a Lisp form or a function that should be called at each matched headline, in
  10297. this case the return value is a list of all return values from these calls.
  10298. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10299. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10300. only lines with a TODO keyword are included in the output."
  10301. (require 'org-agenda)
  10302. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10303. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10304. (org-re
  10305. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10306. (props (list 'face 'default
  10307. 'done-face 'org-agenda-done
  10308. 'undone-face 'default
  10309. 'mouse-face 'highlight
  10310. 'org-not-done-regexp org-not-done-regexp
  10311. 'org-todo-regexp org-todo-regexp
  10312. 'help-echo
  10313. (format "mouse-2 or RET jump to org file %s"
  10314. (abbreviate-file-name
  10315. (or (buffer-file-name (buffer-base-buffer))
  10316. (buffer-name (buffer-base-buffer)))))))
  10317. (case-fold-search nil)
  10318. (org-map-continue-from nil)
  10319. lspos tags tags-list
  10320. (tags-alist (list (cons 0 org-file-tags)))
  10321. (llast 0) rtn rtn1 level category i txt
  10322. todo marker entry priority)
  10323. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10324. (setq action (list 'lambda nil action)))
  10325. (save-excursion
  10326. (goto-char (point-min))
  10327. (when (eq action 'sparse-tree)
  10328. (org-overview)
  10329. (org-remove-occur-highlights))
  10330. (while (re-search-forward re nil t)
  10331. (catch :skip
  10332. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10333. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10334. (goto-char (setq lspos (match-beginning 0)))
  10335. (setq level (org-reduced-level (funcall outline-level))
  10336. category (org-get-category))
  10337. (setq i llast llast level)
  10338. ;; remove tag lists from same and sublevels
  10339. (while (>= i level)
  10340. (when (setq entry (assoc i tags-alist))
  10341. (setq tags-alist (delete entry tags-alist)))
  10342. (setq i (1- i)))
  10343. ;; add the next tags
  10344. (when tags
  10345. (setq tags (org-split-string tags ":")
  10346. tags-alist
  10347. (cons (cons level tags) tags-alist)))
  10348. ;; compile tags for current headline
  10349. (setq tags-list
  10350. (if org-use-tag-inheritance
  10351. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10352. tags)
  10353. org-scanner-tags tags-list)
  10354. (when org-use-tag-inheritance
  10355. (setcdr (car tags-alist)
  10356. (mapcar (lambda (x)
  10357. (setq x (copy-sequence x))
  10358. (org-add-prop-inherited x))
  10359. (cdar tags-alist))))
  10360. (when (and tags org-use-tag-inheritance
  10361. (or (not (eq t org-use-tag-inheritance))
  10362. org-tags-exclude-from-inheritance))
  10363. ;; selective inheritance, remove uninherited ones
  10364. (setcdr (car tags-alist)
  10365. (org-remove-uniherited-tags (cdar tags-alist))))
  10366. (when (and (or (not todo-only)
  10367. (and (member todo org-not-done-keywords)
  10368. (or (not org-agenda-tags-todo-honor-ignore-options)
  10369. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10370. (let ((case-fold-search t)) (eval matcher))
  10371. (or
  10372. (not (member org-archive-tag tags-list))
  10373. ;; we have an archive tag, should we use this anyway?
  10374. (or (not org-agenda-skip-archived-trees)
  10375. (and (eq action 'agenda) org-agenda-archives-mode))))
  10376. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10377. ;; select this headline
  10378. (cond
  10379. ((eq action 'sparse-tree)
  10380. (and org-highlight-sparse-tree-matches
  10381. (org-get-heading) (match-end 0)
  10382. (org-highlight-new-match
  10383. (match-beginning 0) (match-beginning 1)))
  10384. (org-show-context 'tags-tree))
  10385. ((eq action 'agenda)
  10386. (setq txt (org-format-agenda-item
  10387. ""
  10388. (concat
  10389. (if (eq org-tags-match-list-sublevels 'indented)
  10390. (make-string (1- level) ?.) "")
  10391. (org-get-heading))
  10392. category
  10393. tags-list
  10394. )
  10395. priority (org-get-priority txt))
  10396. (goto-char lspos)
  10397. (setq marker (org-agenda-new-marker))
  10398. (org-add-props txt props
  10399. 'org-marker marker 'org-hd-marker marker 'org-category category
  10400. 'todo-state todo
  10401. 'priority priority 'type "tagsmatch")
  10402. (push txt rtn))
  10403. ((functionp action)
  10404. (setq org-map-continue-from nil)
  10405. (save-excursion
  10406. (setq rtn1 (funcall action))
  10407. (push rtn1 rtn)))
  10408. (t (error "Invalid action")))
  10409. ;; if we are to skip sublevels, jump to end of subtree
  10410. (unless org-tags-match-list-sublevels
  10411. (org-end-of-subtree t)
  10412. (backward-char 1))))
  10413. ;; Get the correct position from where to continue
  10414. (if org-map-continue-from
  10415. (goto-char org-map-continue-from)
  10416. (and (= (point) lspos) (end-of-line 1)))))
  10417. (when (and (eq action 'sparse-tree)
  10418. (not org-sparse-tree-open-archived-trees))
  10419. (org-hide-archived-subtrees (point-min) (point-max)))
  10420. (nreverse rtn)))
  10421. (defun org-remove-uniherited-tags (tags)
  10422. "Remove all tags that are not inherited from the list TAGS."
  10423. (cond
  10424. ((eq org-use-tag-inheritance t)
  10425. (if org-tags-exclude-from-inheritance
  10426. (org-delete-all org-tags-exclude-from-inheritance tags)
  10427. tags))
  10428. ((not org-use-tag-inheritance) nil)
  10429. ((stringp org-use-tag-inheritance)
  10430. (delq nil (mapcar
  10431. (lambda (x)
  10432. (if (and (string-match org-use-tag-inheritance x)
  10433. (not (member x org-tags-exclude-from-inheritance)))
  10434. x nil))
  10435. tags)))
  10436. ((listp org-use-tag-inheritance)
  10437. (delq nil (mapcar
  10438. (lambda (x)
  10439. (if (member x org-use-tag-inheritance) x nil))
  10440. tags)))))
  10441. (defvar todo-only) ;; dynamically scoped
  10442. (defun org-match-sparse-tree (&optional todo-only match)
  10443. "Create a sparse tree according to tags string MATCH.
  10444. MATCH can contain positive and negative selection of tags, like
  10445. \"+WORK+URGENT-WITHBOSS\".
  10446. If optional argument TODO-ONLY is non-nil, only select lines that are
  10447. also TODO lines."
  10448. (interactive "P")
  10449. (org-prepare-agenda-buffers (list (current-buffer)))
  10450. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10451. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10452. (defvar org-cached-props nil)
  10453. (defun org-cached-entry-get (pom property)
  10454. (if (or (eq t org-use-property-inheritance)
  10455. (and (stringp org-use-property-inheritance)
  10456. (string-match org-use-property-inheritance property))
  10457. (and (listp org-use-property-inheritance)
  10458. (member property org-use-property-inheritance)))
  10459. ;; Caching is not possible, check it directly
  10460. (org-entry-get pom property 'inherit)
  10461. ;; Get all properties, so that we can do complicated checks easily
  10462. (cdr (assoc property (or org-cached-props
  10463. (setq org-cached-props
  10464. (org-entry-properties pom)))))))
  10465. (defun org-global-tags-completion-table (&optional files)
  10466. "Return the list of all tags in all agenda buffer/files."
  10467. (save-excursion
  10468. (org-uniquify
  10469. (delq nil
  10470. (apply 'append
  10471. (mapcar
  10472. (lambda (file)
  10473. (set-buffer (find-file-noselect file))
  10474. (append (org-get-buffer-tags)
  10475. (mapcar (lambda (x) (if (stringp (car-safe x))
  10476. (list (car-safe x)) nil))
  10477. org-tag-alist)))
  10478. (if (and files (car files))
  10479. files
  10480. (org-agenda-files))))))))
  10481. (defun org-make-tags-matcher (match)
  10482. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10483. ;; todo-only is scoped dynamically into this function, and the function
  10484. ;; may change it if the matcher asks for it.
  10485. (unless match
  10486. ;; Get a new match request, with completion
  10487. (let ((org-last-tags-completion-table
  10488. (org-global-tags-completion-table)))
  10489. (setq match (org-completing-read-no-i
  10490. "Match: " 'org-tags-completion-function nil nil nil
  10491. 'org-tags-history))))
  10492. ;; Parse the string and create a lisp form
  10493. (let ((match0 match)
  10494. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10495. minus tag mm
  10496. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10497. orterms term orlist re-p str-p level-p level-op time-p
  10498. prop-p pn pv po cat-p gv rest)
  10499. (if (string-match "/+" match)
  10500. ;; match contains also a todo-matching request
  10501. (progn
  10502. (setq tagsmatch (substring match 0 (match-beginning 0))
  10503. todomatch (substring match (match-end 0)))
  10504. (if (string-match "^!" todomatch)
  10505. (setq todo-only t todomatch (substring todomatch 1)))
  10506. (if (string-match "^\\s-*$" todomatch)
  10507. (setq todomatch nil)))
  10508. ;; only matching tags
  10509. (setq tagsmatch match todomatch nil))
  10510. ;; Make the tags matcher
  10511. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10512. (setq tagsmatcher t)
  10513. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10514. (while (setq term (pop orterms))
  10515. (while (and (equal (substring term -1) "\\") orterms)
  10516. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10517. (while (string-match re term)
  10518. (setq rest (substring term (match-end 0))
  10519. minus (and (match-end 1)
  10520. (equal (match-string 1 term) "-"))
  10521. tag (match-string 2 term)
  10522. re-p (equal (string-to-char tag) ?{)
  10523. level-p (match-end 4)
  10524. prop-p (match-end 5)
  10525. mm (cond
  10526. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10527. (level-p
  10528. (setq level-op (org-op-to-function (match-string 3 term)))
  10529. `(,level-op level ,(string-to-number
  10530. (match-string 4 term))))
  10531. (prop-p
  10532. (setq pn (match-string 5 term)
  10533. po (match-string 6 term)
  10534. pv (match-string 7 term)
  10535. cat-p (equal pn "CATEGORY")
  10536. re-p (equal (string-to-char pv) ?{)
  10537. str-p (equal (string-to-char pv) ?\")
  10538. time-p (save-match-data
  10539. (string-match "^\"[[<].*[]>]\"$" pv))
  10540. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10541. (if time-p (setq pv (org-matcher-time pv)))
  10542. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10543. (cond
  10544. ((equal pn "CATEGORY")
  10545. (setq gv '(get-text-property (point) 'org-category)))
  10546. ((equal pn "TODO")
  10547. (setq gv 'todo))
  10548. (t
  10549. (setq gv `(org-cached-entry-get nil ,pn))))
  10550. (if re-p
  10551. (if (eq po 'org<>)
  10552. `(not (string-match ,pv (or ,gv "")))
  10553. `(string-match ,pv (or ,gv "")))
  10554. (if str-p
  10555. `(,po (or ,gv "") ,pv)
  10556. `(,po (string-to-number (or ,gv ""))
  10557. ,(string-to-number pv) ))))
  10558. (t `(member ,tag tags-list)))
  10559. mm (if minus (list 'not mm) mm)
  10560. term rest)
  10561. (push mm tagsmatcher))
  10562. (push (if (> (length tagsmatcher) 1)
  10563. (cons 'and tagsmatcher)
  10564. (car tagsmatcher))
  10565. orlist)
  10566. (setq tagsmatcher nil))
  10567. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10568. (setq tagsmatcher
  10569. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10570. ;; Make the todo matcher
  10571. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10572. (setq todomatcher t)
  10573. (setq orterms (org-split-string todomatch "|") orlist nil)
  10574. (while (setq term (pop orterms))
  10575. (while (string-match re term)
  10576. (setq minus (and (match-end 1)
  10577. (equal (match-string 1 term) "-"))
  10578. kwd (match-string 2 term)
  10579. re-p (equal (string-to-char kwd) ?{)
  10580. term (substring term (match-end 0))
  10581. mm (if re-p
  10582. `(string-match ,(substring kwd 1 -1) todo)
  10583. (list 'equal 'todo kwd))
  10584. mm (if minus (list 'not mm) mm))
  10585. (push mm todomatcher))
  10586. (push (if (> (length todomatcher) 1)
  10587. (cons 'and todomatcher)
  10588. (car todomatcher))
  10589. orlist)
  10590. (setq todomatcher nil))
  10591. (setq todomatcher (if (> (length orlist) 1)
  10592. (cons 'or orlist) (car orlist))))
  10593. ;; Return the string and lisp forms of the matcher
  10594. (setq matcher (if todomatcher
  10595. (list 'and tagsmatcher todomatcher)
  10596. tagsmatcher))
  10597. (cons match0 matcher)))
  10598. (defun org-op-to-function (op &optional stringp)
  10599. "Turn an operator into the appropriate function."
  10600. (setq op
  10601. (cond
  10602. ((equal op "<" ) '(< string< org-time<))
  10603. ((equal op ">" ) '(> org-string> org-time>))
  10604. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10605. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10606. ((member op '("=" "==")) '(= string= org-time=))
  10607. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10608. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10609. (defun org<> (a b) (not (= a b)))
  10610. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10611. (defun org-string>= (a b) (not (string< a b)))
  10612. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10613. (defun org-string<> (a b) (not (string= a b)))
  10614. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= 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) (org<> a b)))
  10620. (defun org-2ft (s)
  10621. "Convert S to a floating point time.
  10622. If S is already a number, just return it. If it is a string, parse
  10623. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10624. (cond
  10625. ((numberp s) s)
  10626. ((stringp s)
  10627. (condition-case nil
  10628. (float-time (apply 'encode-time (org-parse-time-string s)))
  10629. (error 0.)))
  10630. (t 0.)))
  10631. (defun org-time-today ()
  10632. "Time in seconds today at 0:00.
  10633. Returns the float number of seconds since the beginning of the
  10634. epoch to the beginning of today (00:00)."
  10635. (float-time (apply 'encode-time
  10636. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10637. (defun org-matcher-time (s)
  10638. "Interpret a time comparison value."
  10639. (save-match-data
  10640. (cond
  10641. ((string= s "<now>") (float-time))
  10642. ((string= s "<today>") (org-time-today))
  10643. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10644. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10645. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10646. (+ (org-time-today)
  10647. (* (string-to-number (match-string 1 s))
  10648. (cdr (assoc (match-string 2 s)
  10649. '(("d" . 86400.0) ("w" . 604800.0)
  10650. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10651. (t (org-2ft s)))))
  10652. (defun org-match-any-p (re list)
  10653. "Does re match any element of list?"
  10654. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10655. (delq nil list))
  10656. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10657. (defvar org-tags-overlay (org-make-overlay 1 1))
  10658. (org-detach-overlay org-tags-overlay)
  10659. (defun org-get-local-tags-at (&optional pos)
  10660. "Get a list of tags defined in the current headline."
  10661. (org-get-tags-at pos 'local))
  10662. (defun org-get-local-tags ()
  10663. "Get a list of tags defined in the current headline."
  10664. (org-get-tags-at nil 'local))
  10665. (defun org-get-tags-at (&optional pos local)
  10666. "Get a list of all headline tags applicable at POS.
  10667. POS defaults to point. If tags are inherited, the list contains
  10668. the targets in the same sequence as the headlines appear, i.e.
  10669. the tags of the current headline come last.
  10670. When LOCAL is non-nil, only return tags from the current headline,
  10671. ignore inherited ones."
  10672. (interactive)
  10673. (if (and org-trust-scanner-tags
  10674. (or (not pos) (equal pos (point)))
  10675. (not local))
  10676. org-scanner-tags
  10677. (let (tags ltags lastpos parent)
  10678. (save-excursion
  10679. (save-restriction
  10680. (widen)
  10681. (goto-char (or pos (point)))
  10682. (save-match-data
  10683. (catch 'done
  10684. (condition-case nil
  10685. (progn
  10686. (org-back-to-heading t)
  10687. (while (not (equal lastpos (point)))
  10688. (setq lastpos (point))
  10689. (when (looking-at
  10690. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10691. (setq ltags (org-split-string
  10692. (org-match-string-no-properties 1) ":"))
  10693. (when parent
  10694. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10695. (setq tags (append
  10696. (if parent
  10697. (org-remove-uniherited-tags ltags)
  10698. ltags)
  10699. tags)))
  10700. (or org-use-tag-inheritance (throw 'done t))
  10701. (if local (throw 'done t))
  10702. (or (org-up-heading-safe) (error nil))
  10703. (setq parent t)))
  10704. (error nil)))))
  10705. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10706. (defun org-add-prop-inherited (s)
  10707. (add-text-properties 0 (length s) '(inherited t) s)
  10708. s)
  10709. (defun org-toggle-tag (tag &optional onoff)
  10710. "Toggle the tag TAG for the current line.
  10711. If ONOFF is `on' or `off', don't toggle but set to this state."
  10712. (let (res current)
  10713. (save-excursion
  10714. (org-back-to-heading t)
  10715. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10716. (point-at-eol) t)
  10717. (progn
  10718. (setq current (match-string 1))
  10719. (replace-match ""))
  10720. (setq current ""))
  10721. (setq current (nreverse (org-split-string current ":")))
  10722. (cond
  10723. ((eq onoff 'on)
  10724. (setq res t)
  10725. (or (member tag current) (push tag current)))
  10726. ((eq onoff 'off)
  10727. (or (not (member tag current)) (setq current (delete tag current))))
  10728. (t (if (member tag current)
  10729. (setq current (delete tag current))
  10730. (setq res t)
  10731. (push tag current))))
  10732. (end-of-line 1)
  10733. (if current
  10734. (progn
  10735. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10736. (org-set-tags nil t))
  10737. (delete-horizontal-space))
  10738. (run-hooks 'org-after-tags-change-hook))
  10739. res))
  10740. (defun org-align-tags-here (to-col)
  10741. ;; Assumes that this is a headline
  10742. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10743. (beginning-of-line 1)
  10744. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10745. (< pos (match-beginning 2)))
  10746. (progn
  10747. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10748. (goto-char (match-beginning 1))
  10749. (insert " ")
  10750. (delete-region (point) (1+ (match-beginning 2)))
  10751. (setq ncol (max (1+ (current-column))
  10752. (1+ col)
  10753. (if (> to-col 0)
  10754. to-col
  10755. (- (abs to-col) tags-l))))
  10756. (setq p (point))
  10757. (insert (make-string (- ncol (current-column)) ?\ ))
  10758. (setq ncol (current-column))
  10759. (when indent-tabs-mode (tabify p (point-at-eol)))
  10760. (org-move-to-column (min ncol col) t))
  10761. (goto-char pos))))
  10762. (defun org-set-tags-command (&optional arg just-align)
  10763. "Call the set-tags command for the current entry."
  10764. (interactive "P")
  10765. (if (org-on-heading-p)
  10766. (org-set-tags arg just-align)
  10767. (save-excursion
  10768. (org-back-to-heading t)
  10769. (org-set-tags arg just-align))))
  10770. (defun org-set-tags-to (data)
  10771. "Set the tags of the current entry to DATA, replacing the current tags.
  10772. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  10773. If DATA is nil or the empty string, any tags will be removed."
  10774. (interactive "sTags: ")
  10775. (setq data
  10776. (cond
  10777. ((eq data nil) "")
  10778. ((equal data "") "")
  10779. ((stringp data)
  10780. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  10781. ":"))
  10782. ((listp data)
  10783. (concat ":" (mapconcat 'identity data ":") ":"))
  10784. (t nil)))
  10785. (when data
  10786. (save-excursion
  10787. (org-back-to-heading t)
  10788. (when (looking-at org-complex-heading-regexp)
  10789. (if (match-end 5)
  10790. (progn
  10791. (goto-char (match-beginning 5))
  10792. (insert data)
  10793. (delete-region (point) (point-at-eol))
  10794. (org-set-tags nil 'align))
  10795. (goto-char (point-at-eol))
  10796. (insert " " data)
  10797. (org-set-tags nil 'align)))
  10798. (beginning-of-line 1)
  10799. (if (looking-at ".*?\\([ \t]+\\)$")
  10800. (delete-region (match-beginning 1) (match-end 1))))))
  10801. (defun org-set-tags (&optional arg just-align)
  10802. "Set the tags for the current headline.
  10803. With prefix ARG, realign all tags in headings in the current buffer."
  10804. (interactive "P")
  10805. (let* ((re (concat "^" outline-regexp))
  10806. (current (org-get-tags-string))
  10807. (col (current-column))
  10808. (org-setting-tags t)
  10809. table current-tags inherited-tags ; computed below when needed
  10810. tags p0 c0 c1 rpl)
  10811. (if arg
  10812. (save-excursion
  10813. (goto-char (point-min))
  10814. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10815. (while (re-search-forward re nil t)
  10816. (org-set-tags nil t)
  10817. (end-of-line 1)))
  10818. (message "All tags realigned to column %d" org-tags-column))
  10819. (if just-align
  10820. (setq tags current)
  10821. ;; Get a new set of tags from the user
  10822. (save-excursion
  10823. (setq table (append org-tag-persistent-alist
  10824. (or org-tag-alist (org-get-buffer-tags))
  10825. (and org-complete-tags-always-offer-all-agenda-tags
  10826. (org-global-tags-completion-table (org-agenda-files))))
  10827. org-last-tags-completion-table table
  10828. current-tags (org-split-string current ":")
  10829. inherited-tags (nreverse
  10830. (nthcdr (length current-tags)
  10831. (nreverse (org-get-tags-at))))
  10832. tags
  10833. (if (or (eq t org-use-fast-tag-selection)
  10834. (and org-use-fast-tag-selection
  10835. (delq nil (mapcar 'cdr table))))
  10836. (org-fast-tag-selection
  10837. current-tags inherited-tags table
  10838. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10839. (let ((org-add-colon-after-tag-completion t))
  10840. (org-trim
  10841. (org-without-partial-completion
  10842. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10843. nil nil current 'org-tags-history)))))))
  10844. (while (string-match "[-+&]+" tags)
  10845. ;; No boolean logic, just a list
  10846. (setq tags (replace-match ":" t t tags))))
  10847. (if org-tags-sort-function
  10848. (setq tags (mapconcat 'identity
  10849. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10850. org-tags-sort-function) ":")))
  10851. (if (string-match "\\`[\t ]*\\'" tags)
  10852. (setq tags "")
  10853. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10854. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10855. ;; Insert new tags at the correct column
  10856. (beginning-of-line 1)
  10857. (cond
  10858. ((and (equal current "") (equal tags "")))
  10859. ((re-search-forward
  10860. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10861. (point-at-eol) t)
  10862. (if (equal tags "")
  10863. (setq rpl "")
  10864. (goto-char (match-beginning 0))
  10865. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  10866. (1+ (point)) (point))
  10867. c1 (max (1+ c0) (if (> org-tags-column 0)
  10868. org-tags-column
  10869. (- (- org-tags-column) (length tags))))
  10870. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10871. (replace-match rpl t t)
  10872. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10873. tags)
  10874. (t (error "Tags alignment failed")))
  10875. (org-move-to-column col)
  10876. (unless just-align
  10877. (run-hooks 'org-after-tags-change-hook)))))
  10878. (defun org-change-tag-in-region (beg end tag off)
  10879. "Add or remove TAG for each entry in the region.
  10880. This works in the agenda, and also in an org-mode buffer."
  10881. (interactive
  10882. (list (region-beginning) (region-end)
  10883. (let ((org-last-tags-completion-table
  10884. (if (org-mode-p)
  10885. (org-get-buffer-tags)
  10886. (org-global-tags-completion-table))))
  10887. (org-icompleting-read
  10888. "Tag: " 'org-tags-completion-function nil nil nil
  10889. 'org-tags-history))
  10890. (progn
  10891. (message "[s]et or [r]emove? ")
  10892. (equal (read-char-exclusive) ?r))))
  10893. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10894. (let ((agendap (equal major-mode 'org-agenda-mode))
  10895. l1 l2 m buf pos newhead (cnt 0))
  10896. (goto-char end)
  10897. (setq l2 (1- (org-current-line)))
  10898. (goto-char beg)
  10899. (setq l1 (org-current-line))
  10900. (loop for l from l1 to l2 do
  10901. (org-goto-line l)
  10902. (setq m (get-text-property (point) 'org-hd-marker))
  10903. (when (or (and (org-mode-p) (org-on-heading-p))
  10904. (and agendap m))
  10905. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10906. pos (if agendap m (point)))
  10907. (with-current-buffer buf
  10908. (save-excursion
  10909. (save-restriction
  10910. (goto-char pos)
  10911. (setq cnt (1+ cnt))
  10912. (org-toggle-tag tag (if off 'off 'on))
  10913. (setq newhead (org-get-heading)))))
  10914. (and agendap (org-agenda-change-all-lines newhead m))))
  10915. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10916. (defun org-tags-completion-function (string predicate &optional flag)
  10917. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10918. (confirm (lambda (x) (stringp (car x)))))
  10919. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10920. (setq s1 (match-string 1 string)
  10921. s2 (match-string 2 string))
  10922. (setq s1 "" s2 string))
  10923. (cond
  10924. ((eq flag nil)
  10925. ;; try completion
  10926. (setq rtn (try-completion s2 ctable confirm))
  10927. (if (stringp rtn)
  10928. (setq rtn
  10929. (concat s1 s2 (substring rtn (length s2))
  10930. (if (and org-add-colon-after-tag-completion
  10931. (assoc rtn ctable))
  10932. ":" ""))))
  10933. rtn)
  10934. ((eq flag t)
  10935. ;; all-completions
  10936. (all-completions s2 ctable confirm)
  10937. )
  10938. ((eq flag 'lambda)
  10939. ;; exact match?
  10940. (assoc s2 ctable)))
  10941. ))
  10942. (defun org-fast-tag-insert (kwd tags face &optional end)
  10943. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10944. (insert (format "%-12s" (concat kwd ":"))
  10945. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10946. (or end "")))
  10947. (defun org-fast-tag-show-exit (flag)
  10948. (save-excursion
  10949. (org-goto-line 3)
  10950. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10951. (replace-match ""))
  10952. (when flag
  10953. (end-of-line 1)
  10954. (org-move-to-column (- (window-width) 19) t)
  10955. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10956. (defun org-set-current-tags-overlay (current prefix)
  10957. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10958. (if (featurep 'xemacs)
  10959. (org-overlay-display org-tags-overlay (concat prefix s)
  10960. 'secondary-selection)
  10961. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10962. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10963. (defvar org-last-tag-selection-key nil)
  10964. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10965. "Fast tag selection with single keys.
  10966. CURRENT is the current list of tags in the headline, INHERITED is the
  10967. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10968. possibly with grouping information. TODO-TABLE is a similar table with
  10969. TODO keywords, should these have keys assigned to them.
  10970. If the keys are nil, a-z are automatically assigned.
  10971. Returns the new tags string, or nil to not change the current settings."
  10972. (let* ((fulltable (append table todo-table))
  10973. (maxlen (apply 'max (mapcar
  10974. (lambda (x)
  10975. (if (stringp (car x)) (string-width (car x)) 0))
  10976. fulltable)))
  10977. (buf (current-buffer))
  10978. (expert (eq org-fast-tag-selection-single-key 'expert))
  10979. (buffer-tags nil)
  10980. (fwidth (+ maxlen 3 1 3))
  10981. (ncol (/ (- (window-width) 4) fwidth))
  10982. (i-face 'org-done)
  10983. (c-face 'org-todo)
  10984. tg cnt e c char c1 c2 ntable tbl rtn
  10985. ov-start ov-end ov-prefix
  10986. (exit-after-next org-fast-tag-selection-single-key)
  10987. (done-keywords org-done-keywords)
  10988. groups ingroup)
  10989. (save-excursion
  10990. (beginning-of-line 1)
  10991. (if (looking-at
  10992. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10993. (setq ov-start (match-beginning 1)
  10994. ov-end (match-end 1)
  10995. ov-prefix "")
  10996. (setq ov-start (1- (point-at-eol))
  10997. ov-end (1+ ov-start))
  10998. (skip-chars-forward "^\n\r")
  10999. (setq ov-prefix
  11000. (concat
  11001. (buffer-substring (1- (point)) (point))
  11002. (if (> (current-column) org-tags-column)
  11003. " "
  11004. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11005. (org-move-overlay org-tags-overlay ov-start ov-end)
  11006. (save-window-excursion
  11007. (if expert
  11008. (set-buffer (get-buffer-create " *Org tags*"))
  11009. (delete-other-windows)
  11010. (split-window-vertically)
  11011. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11012. (erase-buffer)
  11013. (org-set-local 'org-done-keywords done-keywords)
  11014. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11015. (org-fast-tag-insert "Current" current c-face "\n\n")
  11016. (org-fast-tag-show-exit exit-after-next)
  11017. (org-set-current-tags-overlay current ov-prefix)
  11018. (setq tbl fulltable char ?a cnt 0)
  11019. (while (setq e (pop tbl))
  11020. (cond
  11021. ((equal (car e) :startgroup)
  11022. (push '() groups) (setq ingroup t)
  11023. (when (not (= cnt 0))
  11024. (setq cnt 0)
  11025. (insert "\n"))
  11026. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11027. ((equal (car e) :endgroup)
  11028. (setq ingroup nil cnt 0)
  11029. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11030. ((equal e '(:newline))
  11031. (when (not (= cnt 0))
  11032. (setq cnt 0)
  11033. (insert "\n")
  11034. (setq e (car tbl))
  11035. (while (equal (car tbl) '(:newline))
  11036. (insert "\n")
  11037. (setq tbl (cdr tbl)))))
  11038. (t
  11039. (setq tg (copy-sequence (car e)) c2 nil)
  11040. (if (cdr e)
  11041. (setq c (cdr e))
  11042. ;; automatically assign a character.
  11043. (setq c1 (string-to-char
  11044. (downcase (substring
  11045. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11046. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11047. (while (or (rassoc char ntable) (rassoc char table))
  11048. (setq char (1+ char)))
  11049. (setq c2 c1))
  11050. (setq c (or c2 char)))
  11051. (if ingroup (push tg (car groups)))
  11052. (setq tg (org-add-props tg nil 'face
  11053. (cond
  11054. ((not (assoc tg table))
  11055. (org-get-todo-face tg))
  11056. ((member tg current) c-face)
  11057. ((member tg inherited) i-face)
  11058. (t nil))))
  11059. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11060. (insert "[" c "] " tg (make-string
  11061. (- fwidth 4 (length tg)) ?\ ))
  11062. (push (cons tg c) ntable)
  11063. (when (= (setq cnt (1+ cnt)) ncol)
  11064. (insert "\n")
  11065. (if ingroup (insert " "))
  11066. (setq cnt 0)))))
  11067. (setq ntable (nreverse ntable))
  11068. (insert "\n")
  11069. (goto-char (point-min))
  11070. (if (not expert) (org-fit-window-to-buffer))
  11071. (setq rtn
  11072. (catch 'exit
  11073. (while t
  11074. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11075. (if (not groups) "no " "")
  11076. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11077. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11078. (setq org-last-tag-selection-key c)
  11079. (cond
  11080. ((= c ?\r) (throw 'exit t))
  11081. ((= c ?!)
  11082. (setq groups (not groups))
  11083. (goto-char (point-min))
  11084. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11085. ((= c ?\C-c)
  11086. (if (not expert)
  11087. (org-fast-tag-show-exit
  11088. (setq exit-after-next (not exit-after-next)))
  11089. (setq expert nil)
  11090. (delete-other-windows)
  11091. (split-window-vertically)
  11092. (org-switch-to-buffer-other-window " *Org tags*")
  11093. (org-fit-window-to-buffer)))
  11094. ((or (= c ?\C-g)
  11095. (and (= c ?q) (not (rassoc c ntable))))
  11096. (org-detach-overlay org-tags-overlay)
  11097. (setq quit-flag t))
  11098. ((= c ?\ )
  11099. (setq current nil)
  11100. (if exit-after-next (setq exit-after-next 'now)))
  11101. ((= c ?\t)
  11102. (condition-case nil
  11103. (setq tg (org-icompleting-read
  11104. "Tag: "
  11105. (or buffer-tags
  11106. (with-current-buffer buf
  11107. (org-get-buffer-tags)))))
  11108. (quit (setq tg "")))
  11109. (when (string-match "\\S-" tg)
  11110. (add-to-list 'buffer-tags (list tg))
  11111. (if (member tg current)
  11112. (setq current (delete tg current))
  11113. (push tg current)))
  11114. (if exit-after-next (setq exit-after-next 'now)))
  11115. ((setq e (rassoc c todo-table) tg (car e))
  11116. (with-current-buffer buf
  11117. (save-excursion (org-todo tg)))
  11118. (if exit-after-next (setq exit-after-next 'now)))
  11119. ((setq e (rassoc c ntable) tg (car e))
  11120. (if (member tg current)
  11121. (setq current (delete tg current))
  11122. (loop for g in groups do
  11123. (if (member tg g)
  11124. (mapc (lambda (x)
  11125. (setq current (delete x current)))
  11126. g)))
  11127. (push tg current))
  11128. (if exit-after-next (setq exit-after-next 'now))))
  11129. ;; Create a sorted list
  11130. (setq current
  11131. (sort current
  11132. (lambda (a b)
  11133. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11134. (if (eq exit-after-next 'now) (throw 'exit t))
  11135. (goto-char (point-min))
  11136. (beginning-of-line 2)
  11137. (delete-region (point) (point-at-eol))
  11138. (org-fast-tag-insert "Current" current c-face)
  11139. (org-set-current-tags-overlay current ov-prefix)
  11140. (while (re-search-forward
  11141. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11142. (setq tg (match-string 1))
  11143. (add-text-properties
  11144. (match-beginning 1) (match-end 1)
  11145. (list 'face
  11146. (cond
  11147. ((member tg current) c-face)
  11148. ((member tg inherited) i-face)
  11149. (t (get-text-property (match-beginning 1) 'face))))))
  11150. (goto-char (point-min)))))
  11151. (org-detach-overlay org-tags-overlay)
  11152. (if rtn
  11153. (mapconcat 'identity current ":")
  11154. nil))))
  11155. (defun org-get-tags-string ()
  11156. "Get the TAGS string in the current headline."
  11157. (unless (org-on-heading-p t)
  11158. (error "Not on a heading"))
  11159. (save-excursion
  11160. (beginning-of-line 1)
  11161. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11162. (org-match-string-no-properties 1)
  11163. "")))
  11164. (defun org-get-tags ()
  11165. "Get the list of tags specified in the current headline."
  11166. (org-split-string (org-get-tags-string) ":"))
  11167. (defun org-get-buffer-tags ()
  11168. "Get a table of all tags used in the buffer, for completion."
  11169. (let (tags)
  11170. (save-excursion
  11171. (goto-char (point-min))
  11172. (while (re-search-forward
  11173. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11174. (when (equal (char-after (point-at-bol 0)) ?*)
  11175. (mapc (lambda (x) (add-to-list 'tags x))
  11176. (org-split-string (org-match-string-no-properties 1) ":")))))
  11177. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11178. (mapcar 'list tags)))
  11179. ;;;; The mapping API
  11180. ;;;###autoload
  11181. (defun org-map-entries (func &optional match scope &rest skip)
  11182. "Call FUNC at each headline selected by MATCH in SCOPE.
  11183. FUNC is a function or a lisp form. The function will be called without
  11184. arguments, with the cursor positioned at the beginning of the headline.
  11185. The return values of all calls to the function will be collected and
  11186. returned as a list.
  11187. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11188. does not need to preserve point. After evaluation, the cursor will be
  11189. moved to the end of the line (presumably of the headline of the
  11190. processed entry) and search continues from there. Under some
  11191. circumstances, this may not produce the wanted results. For example,
  11192. if you have removed (e.g. archived) the current (sub)tree it could
  11193. mean that the next entry will be skipped entirely. In such cases, you
  11194. can specify the position from where search should continue by making
  11195. FUNC set the variable `org-map-continue-from' to the desired buffer
  11196. position.
  11197. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11198. Only headlines that are matched by this query will be considered during
  11199. the iteration. When MATCH is nil or t, all headlines will be
  11200. visited by the iteration.
  11201. SCOPE determines the scope of this command. It can be any of:
  11202. nil The current buffer, respecting the restriction if any
  11203. tree The subtree started with the entry at point
  11204. file The current buffer, without restriction
  11205. file-with-archives
  11206. The current buffer, and any archives associated with it
  11207. agenda All agenda files
  11208. agenda-with-archives
  11209. All agenda files with any archive files associated with them
  11210. \(file1 file2 ...)
  11211. If this is a list, all files in the list will be scanned
  11212. The remaining args are treated as settings for the skipping facilities of
  11213. the scanner. The following items can be given here:
  11214. archive skip trees with the archive tag.
  11215. comment skip trees with the COMMENT keyword
  11216. function or Emacs Lisp form:
  11217. will be used as value for `org-agenda-skip-function', so whenever
  11218. the function returns t, FUNC will not be called for that
  11219. entry and search will continue from the point where the
  11220. function leaves it.
  11221. If your function needs to retrieve the tags including inherited tags
  11222. at the *current* entry, you can use the value of the variable
  11223. `org-scanner-tags' which will be much faster than getting the value
  11224. with `org-get-tags-at'. If your function gets properties with
  11225. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11226. to t around the call to `org-entry-properties' to get the same speedup.
  11227. Note that if your function moves around to retrieve tags and properties at
  11228. a *different* entry, you cannot use these techniques."
  11229. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11230. (org-agenda-skip-archived-trees (memq 'archive skip))
  11231. (org-agenda-skip-comment-trees (memq 'comment skip))
  11232. (org-agenda-skip-function
  11233. (car (org-delete-all '(comment archive) skip)))
  11234. (org-tags-match-list-sublevels t)
  11235. matcher file res
  11236. org-todo-keywords-for-agenda
  11237. org-done-keywords-for-agenda
  11238. org-todo-keyword-alist-for-agenda
  11239. org-drawers-for-agenda
  11240. org-tag-alist-for-agenda)
  11241. (cond
  11242. ((eq match t) (setq matcher t))
  11243. ((eq match nil) (setq matcher t))
  11244. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11245. (save-excursion
  11246. (save-restriction
  11247. (when (eq scope 'tree)
  11248. (org-back-to-heading t)
  11249. (org-narrow-to-subtree)
  11250. (setq scope nil))
  11251. (if (not scope)
  11252. (progn
  11253. (org-prepare-agenda-buffers
  11254. (list (buffer-file-name (current-buffer))))
  11255. (setq res (org-scan-tags func matcher)))
  11256. ;; Get the right scope
  11257. (cond
  11258. ((and scope (listp scope) (symbolp (car scope)))
  11259. (setq scope (eval scope)))
  11260. ((eq scope 'agenda)
  11261. (setq scope (org-agenda-files t)))
  11262. ((eq scope 'agenda-with-archives)
  11263. (setq scope (org-agenda-files t))
  11264. (setq scope (org-add-archive-files scope)))
  11265. ((eq scope 'file)
  11266. (setq scope (list (buffer-file-name))))
  11267. ((eq scope 'file-with-archives)
  11268. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11269. (org-prepare-agenda-buffers scope)
  11270. (while (setq file (pop scope))
  11271. (with-current-buffer (org-find-base-buffer-visiting file)
  11272. (save-excursion
  11273. (save-restriction
  11274. (widen)
  11275. (goto-char (point-min))
  11276. (setq res (append res (org-scan-tags func matcher))))))))))
  11277. res))
  11278. ;;;; Properties
  11279. ;;; Setting and retrieving properties
  11280. (defconst org-special-properties
  11281. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11282. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11283. "The special properties valid in Org-mode.
  11284. These are properties that are not defined in the property drawer,
  11285. but in some other way.")
  11286. (defconst org-default-properties
  11287. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11288. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11289. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11290. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11291. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  11292. "CLOCK_MODELINE_TOTAL" "STYLE")
  11293. "Some properties that are used by Org-mode for various purposes.
  11294. Being in this list makes sure that they are offered for completion.")
  11295. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11296. "Regular expression matching the first line of a property drawer.")
  11297. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11298. "Regular expression matching the first line of a property drawer.")
  11299. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11300. "Regular expression matching the first line of a property drawer.")
  11301. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11302. "Regular expression matching the first line of a property drawer.")
  11303. (defconst org-property-drawer-re
  11304. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11305. org-property-end-re "\\)\n?")
  11306. "Matches an entire property drawer.")
  11307. (defconst org-clock-drawer-re
  11308. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11309. org-property-end-re "\\)\n?")
  11310. "Matches an entire clock drawer.")
  11311. (defun org-property-action ()
  11312. "Do an action on properties."
  11313. (interactive)
  11314. (let (c)
  11315. (org-at-property-p)
  11316. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11317. (setq c (read-char-exclusive))
  11318. (cond
  11319. ((equal c ?s)
  11320. (call-interactively 'org-set-property))
  11321. ((equal c ?d)
  11322. (call-interactively 'org-delete-property))
  11323. ((equal c ?D)
  11324. (call-interactively 'org-delete-property-globally))
  11325. ((equal c ?c)
  11326. (call-interactively 'org-compute-property-at-point))
  11327. (t (error "No such property action %c" c)))))
  11328. (defun org-set-effort (&optional value)
  11329. "Set the effort property of the current entry.
  11330. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11331. allowed value."
  11332. (interactive "P")
  11333. (if (equal value 0) (setq value 10))
  11334. (let* ((completion-ignore-case t)
  11335. (prop org-effort-property)
  11336. (cur (org-entry-get nil prop))
  11337. (allowed (org-property-get-allowed-values nil prop 'table))
  11338. (existing (mapcar 'list (org-property-values prop)))
  11339. rpl
  11340. (val (cond
  11341. ((stringp value) value)
  11342. ((and allowed (integerp value))
  11343. (or (car (nth (1- value) allowed))
  11344. (car (org-last allowed))))
  11345. (allowed
  11346. (message "Select 1-9,0, [RET%s]: %s"
  11347. (if cur (concat "=" cur) "")
  11348. (mapconcat 'car allowed " "))
  11349. (setq rpl (read-char-exclusive))
  11350. (if (equal rpl ?\r)
  11351. cur
  11352. (setq rpl (- rpl ?0))
  11353. (if (equal rpl 0) (setq rpl 10))
  11354. (if (and (> rpl 0) (<= rpl (length allowed)))
  11355. (car (nth (1- rpl) allowed))
  11356. (org-completing-read "Effort: " allowed nil))))
  11357. (t
  11358. (let (org-completion-use-ido org-completion-use-iswitchb)
  11359. (org-completing-read
  11360. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11361. (concat "[" cur "]") "")
  11362. ": ")
  11363. existing nil nil "" nil cur))))))
  11364. (unless (equal (org-entry-get nil prop) val)
  11365. (org-entry-put nil prop val))
  11366. (message "%s is now %s" prop val)))
  11367. (defun org-at-property-p ()
  11368. "Is the cursor in a property line?"
  11369. ;; FIXME: Does not check if we are actually in the drawer.
  11370. ;; FIXME: also returns true on any drawers.....
  11371. ;; This is used by C-c C-c for property action.
  11372. (save-excursion
  11373. (beginning-of-line 1)
  11374. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  11375. (defun org-get-property-block (&optional beg end force)
  11376. "Return the (beg . end) range of the body of the property drawer.
  11377. BEG and END can be beginning and end of subtree, if not given
  11378. they will be found.
  11379. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11380. (catch 'exit
  11381. (save-excursion
  11382. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11383. (end (or end (progn (outline-next-heading) (point)))))
  11384. (goto-char beg)
  11385. (if (re-search-forward org-property-start-re end t)
  11386. (setq beg (1+ (match-end 0)))
  11387. (if force
  11388. (save-excursion
  11389. (org-insert-property-drawer)
  11390. (setq end (progn (outline-next-heading) (point))))
  11391. (throw 'exit nil))
  11392. (goto-char beg)
  11393. (if (re-search-forward org-property-start-re end t)
  11394. (setq beg (1+ (match-end 0)))))
  11395. (if (re-search-forward org-property-end-re end t)
  11396. (setq end (match-beginning 0))
  11397. (or force (throw 'exit nil))
  11398. (goto-char beg)
  11399. (setq end beg)
  11400. (org-indent-line-function)
  11401. (insert ":END:\n"))
  11402. (cons beg end)))))
  11403. (defun org-entry-properties (&optional pom which specific)
  11404. "Get all properties of the entry at point-or-marker POM.
  11405. This includes the TODO keyword, the tags, time strings for deadline,
  11406. scheduled, and clocking, and any additional properties defined in the
  11407. entry. The return value is an alist, keys may occur multiple times
  11408. if the property key was used several times.
  11409. POM may also be nil, in which case the current entry is used.
  11410. If WHICH is nil or `all', get all properties. If WHICH is
  11411. `special' or `standard', only get that subclass. If WHICH
  11412. is a string only get exactly this property. Specific can be a sting, the
  11413. specific property we are interested in. Specifying it can speed
  11414. things up because then unnecessary parsing is avoided."
  11415. (setq which (or which 'all))
  11416. (org-with-point-at pom
  11417. (let ((clockstr (substring org-clock-string 0 -1))
  11418. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  11419. beg end range props sum-props key value string clocksum)
  11420. (save-excursion
  11421. (when (condition-case nil
  11422. (and (org-mode-p) (org-back-to-heading t))
  11423. (error nil))
  11424. (setq beg (point))
  11425. (setq sum-props (get-text-property (point) 'org-summaries))
  11426. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11427. (outline-next-heading)
  11428. (setq end (point))
  11429. (when (memq which '(all special))
  11430. ;; Get the special properties, like TODO and tags
  11431. (goto-char beg)
  11432. (when (and (or (not specific) (string= specific "TODO"))
  11433. (looking-at org-todo-line-regexp) (match-end 2))
  11434. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11435. (when (and (or (not specific) (string= specific "PRIORITY"))
  11436. (looking-at org-priority-regexp))
  11437. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11438. (when (and (or (not specific) (string= specific "TAGS"))
  11439. (setq value (org-get-tags-string))
  11440. (string-match "\\S-" value))
  11441. (push (cons "TAGS" value) props))
  11442. (when (and (or (not specific) (string= specific "TAGS"))
  11443. (setq value (org-get-tags-at)))
  11444. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11445. ":"))
  11446. props))
  11447. (when (or (not specific) (string= specific "TAGS"))
  11448. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11449. (when (or (not specific)
  11450. (member specific org-all-time-keywords)
  11451. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11452. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11453. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11454. string (if (equal key clockstr)
  11455. (org-no-properties
  11456. (org-trim
  11457. (buffer-substring
  11458. (match-beginning 3) (goto-char (point-at-eol)))))
  11459. (substring (org-match-string-no-properties 3) 1 -1)))
  11460. (unless key
  11461. (if (= (char-after (match-beginning 3)) ?\[)
  11462. (setq key "TIMESTAMP_IA")
  11463. (setq key "TIMESTAMP")))
  11464. (when (or (equal key clockstr) (not (assoc key props)))
  11465. (push (cons key string) props))))
  11466. )
  11467. (when (memq which '(all standard))
  11468. ;; Get the standard properties, like :PROP: ...
  11469. (setq range (org-get-property-block beg end))
  11470. (when range
  11471. (goto-char (car range))
  11472. (while (re-search-forward
  11473. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11474. (cdr range) t)
  11475. (setq key (org-match-string-no-properties 1)
  11476. value (org-trim (or (org-match-string-no-properties 2) "")))
  11477. (unless (member key excluded)
  11478. (push (cons key (or value "")) props)))))
  11479. (if clocksum
  11480. (push (cons "CLOCKSUM"
  11481. (org-columns-number-to-string (/ (float clocksum) 60.)
  11482. 'add_times))
  11483. props))
  11484. (unless (assoc "CATEGORY" props)
  11485. (setq value (or (org-get-category)
  11486. (progn (org-refresh-category-properties)
  11487. (org-get-category))))
  11488. (push (cons "CATEGORY" value) props))
  11489. (append sum-props (nreverse props)))))))
  11490. (defun org-entry-get (pom property &optional inherit)
  11491. "Get value of PROPERTY for entry at point-or-marker POM.
  11492. If INHERIT is non-nil and the entry does not have the property,
  11493. then also check higher levels of the hierarchy.
  11494. If INHERIT is the symbol `selective', use inheritance only if the setting
  11495. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11496. If the property is present but empty, the return value is the empty string.
  11497. If the property is not present at all, nil is returned."
  11498. (org-with-point-at pom
  11499. (if (and inherit (if (eq inherit 'selective)
  11500. (org-property-inherit-p property)
  11501. t))
  11502. (org-entry-get-with-inheritance property)
  11503. (if (member property org-special-properties)
  11504. ;; We need a special property. Use `org-entry-properties' to
  11505. ;; retrieve it, but specify the wanted property
  11506. (cdr (assoc property (org-entry-properties nil 'special property)))
  11507. (let ((range (org-get-property-block)))
  11508. (if (and range
  11509. (goto-char (car range))
  11510. (re-search-forward
  11511. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11512. (cdr range) t))
  11513. ;; Found the property, return it.
  11514. (if (match-end 1)
  11515. (org-match-string-no-properties 1)
  11516. "")))))))
  11517. (defun org-property-or-variable-value (var &optional inherit)
  11518. "Check if there is a property fixing the value of VAR.
  11519. If yes, return this value. If not, return the current value of the variable."
  11520. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11521. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11522. (read prop)
  11523. (symbol-value var))))
  11524. (defun org-entry-delete (pom property)
  11525. "Delete the property PROPERTY from entry at point-or-marker POM."
  11526. (org-with-point-at pom
  11527. (if (member property org-special-properties)
  11528. nil ; cannot delete these properties.
  11529. (let ((range (org-get-property-block)))
  11530. (if (and range
  11531. (goto-char (car range))
  11532. (re-search-forward
  11533. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11534. (cdr range) t))
  11535. (progn
  11536. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11537. t)
  11538. nil)))))
  11539. ;; Multi-values properties are properties that contain multiple values
  11540. ;; These values are assumed to be single words, separated by whitespace.
  11541. (defun org-entry-add-to-multivalued-property (pom property value)
  11542. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11543. (let* ((old (org-entry-get pom property))
  11544. (values (and old (org-split-string old "[ \t]"))))
  11545. (setq value (org-entry-protect-space value))
  11546. (unless (member value values)
  11547. (setq values (cons value values))
  11548. (org-entry-put pom property
  11549. (mapconcat 'identity values " ")))))
  11550. (defun org-entry-remove-from-multivalued-property (pom property value)
  11551. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11552. (let* ((old (org-entry-get pom property))
  11553. (values (and old (org-split-string old "[ \t]"))))
  11554. (setq value (org-entry-protect-space value))
  11555. (when (member value values)
  11556. (setq values (delete value values))
  11557. (org-entry-put pom property
  11558. (mapconcat 'identity values " ")))))
  11559. (defun org-entry-member-in-multivalued-property (pom property value)
  11560. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11561. (let* ((old (org-entry-get pom property))
  11562. (values (and old (org-split-string old "[ \t]"))))
  11563. (setq value (org-entry-protect-space value))
  11564. (member value values)))
  11565. (defun org-entry-get-multivalued-property (pom property)
  11566. "Return a list of values in a multivalued property."
  11567. (let* ((value (org-entry-get pom property))
  11568. (values (and value (org-split-string value "[ \t]"))))
  11569. (mapcar 'org-entry-restore-space values)))
  11570. (defun org-entry-put-multivalued-property (pom property &rest values)
  11571. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11572. VALUES should be a list of strings. Spaces will be protected."
  11573. (org-entry-put pom property
  11574. (mapconcat 'org-entry-protect-space values " "))
  11575. (let* ((value (org-entry-get pom property))
  11576. (values (and value (org-split-string value "[ \t]"))))
  11577. (mapcar 'org-entry-restore-space values)))
  11578. (defun org-entry-protect-space (s)
  11579. "Protect spaces and newline in string S."
  11580. (while (string-match " " s)
  11581. (setq s (replace-match "%20" t t s)))
  11582. (while (string-match "\n" s)
  11583. (setq s (replace-match "%0A" t t s)))
  11584. s)
  11585. (defun org-entry-restore-space (s)
  11586. "Restore spaces and newline in string S."
  11587. (while (string-match "%20" s)
  11588. (setq s (replace-match " " t t s)))
  11589. (while (string-match "%0A" s)
  11590. (setq s (replace-match "\n" t t s)))
  11591. s)
  11592. (defvar org-entry-property-inherited-from (make-marker)
  11593. "Marker pointing to the entry from where a property was inherited.
  11594. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11595. location of the entry where the inheritance search matched. If there was
  11596. no match, the marker will point nowhere.
  11597. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11598. is set.")
  11599. (defun org-entry-get-with-inheritance (property)
  11600. "Get entry property, and search higher levels if not present."
  11601. (move-marker org-entry-property-inherited-from nil)
  11602. (let (tmp)
  11603. (save-excursion
  11604. (save-restriction
  11605. (widen)
  11606. (catch 'ex
  11607. (while t
  11608. (when (setq tmp (org-entry-get nil property))
  11609. (org-back-to-heading t)
  11610. (move-marker org-entry-property-inherited-from (point))
  11611. (throw 'ex tmp))
  11612. (or (org-up-heading-safe) (throw 'ex nil)))))
  11613. (or tmp
  11614. (cdr (assoc property org-file-properties))
  11615. (cdr (assoc property org-global-properties))
  11616. (cdr (assoc property org-global-properties-fixed))))))
  11617. (defvar org-property-changed-functions nil
  11618. "Hook called when the value of a property has changed.
  11619. Each hook function should accept two arguments, the name of the property
  11620. and the new value.")
  11621. (defun org-entry-put (pom property value)
  11622. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11623. (org-with-point-at pom
  11624. (org-back-to-heading t)
  11625. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11626. range)
  11627. (cond
  11628. ((equal property "TODO")
  11629. (when (and (stringp value) (string-match "\\S-" value)
  11630. (not (member value org-todo-keywords-1)))
  11631. (error "\"%s\" is not a valid TODO state" value))
  11632. (if (or (not value)
  11633. (not (string-match "\\S-" value)))
  11634. (setq value 'none))
  11635. (org-todo value)
  11636. (org-set-tags nil 'align))
  11637. ((equal property "PRIORITY")
  11638. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11639. (string-to-char value) ?\ ))
  11640. (org-set-tags nil 'align))
  11641. ((equal property "SCHEDULED")
  11642. (if (re-search-forward org-scheduled-time-regexp end t)
  11643. (cond
  11644. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11645. ((eq value 'later) (org-timestamp-change 1 'day))
  11646. (t (call-interactively 'org-schedule)))
  11647. (call-interactively 'org-schedule)))
  11648. ((equal property "DEADLINE")
  11649. (if (re-search-forward org-deadline-time-regexp end t)
  11650. (cond
  11651. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11652. ((eq value 'later) (org-timestamp-change 1 'day))
  11653. (t (call-interactively 'org-deadline)))
  11654. (call-interactively 'org-deadline)))
  11655. ((member property org-special-properties)
  11656. (error "The %s property can not yet be set with `org-entry-put'"
  11657. property))
  11658. (t ; a non-special property
  11659. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11660. (setq range (org-get-property-block beg end 'force))
  11661. (goto-char (car range))
  11662. (if (re-search-forward
  11663. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11664. (progn
  11665. (delete-region (match-beginning 1) (match-end 1))
  11666. (goto-char (match-beginning 1)))
  11667. (goto-char (cdr range))
  11668. (insert "\n")
  11669. (backward-char 1)
  11670. (org-indent-line-function)
  11671. (insert ":" property ":"))
  11672. (and value (insert " " value))
  11673. (org-indent-line-function)))))
  11674. (run-hook-with-args 'org-property-changed-functions property value)))
  11675. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11676. "Get all property keys in the current buffer.
  11677. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11678. like tags and TODO state.
  11679. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11680. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11681. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11682. formats in the current buffer."
  11683. (let (rtn range cfmt s p)
  11684. (save-excursion
  11685. (save-restriction
  11686. (widen)
  11687. (goto-char (point-min))
  11688. (while (re-search-forward org-property-start-re nil t)
  11689. (setq range (org-get-property-block))
  11690. (goto-char (car range))
  11691. (while (re-search-forward
  11692. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11693. (cdr range) t)
  11694. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11695. (outline-next-heading))))
  11696. (when include-specials
  11697. (setq rtn (append org-special-properties rtn)))
  11698. (when include-defaults
  11699. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11700. (add-to-list 'rtn org-effort-property))
  11701. (when include-columns
  11702. (save-excursion
  11703. (save-restriction
  11704. (widen)
  11705. (goto-char (point-min))
  11706. (while (re-search-forward
  11707. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11708. nil t)
  11709. (setq cfmt (match-string 2) s 0)
  11710. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11711. cfmt s)
  11712. (setq s (match-end 0)
  11713. p (match-string 1 cfmt))
  11714. (unless (or (equal p "ITEM")
  11715. (member p org-special-properties))
  11716. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11717. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11718. (defun org-property-values (key)
  11719. "Return a list of all values of property KEY."
  11720. (save-excursion
  11721. (save-restriction
  11722. (widen)
  11723. (goto-char (point-min))
  11724. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11725. values)
  11726. (while (re-search-forward re nil t)
  11727. (add-to-list 'values (org-trim (match-string 1))))
  11728. (delete "" values)))))
  11729. (defun org-insert-property-drawer ()
  11730. "Insert a property drawer into the current entry."
  11731. (interactive)
  11732. (org-back-to-heading t)
  11733. (looking-at outline-regexp)
  11734. (let ((indent (if org-adapt-indentation
  11735. (- (match-end 0)(match-beginning 0))
  11736. 0))
  11737. (beg (point))
  11738. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11739. end hiddenp)
  11740. (outline-next-heading)
  11741. (setq end (point))
  11742. (goto-char beg)
  11743. (while (re-search-forward re end t))
  11744. (setq hiddenp (org-invisible-p))
  11745. (end-of-line 1)
  11746. (and (equal (char-after) ?\n) (forward-char 1))
  11747. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11748. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11749. ;; just skip this line
  11750. (beginning-of-line 2)
  11751. ;; Drawer start, find the end
  11752. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11753. (beginning-of-line 1)))
  11754. (org-skip-over-state-notes)
  11755. (skip-chars-backward " \t\n\r")
  11756. (if (eq (char-before) ?*) (forward-char 1))
  11757. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11758. (beginning-of-line 0)
  11759. (org-indent-to-column indent)
  11760. (beginning-of-line 2)
  11761. (org-indent-to-column indent)
  11762. (beginning-of-line 0)
  11763. (if hiddenp
  11764. (save-excursion
  11765. (org-back-to-heading t)
  11766. (hide-entry))
  11767. (org-flag-drawer t))))
  11768. (defun org-set-property (property value)
  11769. "In the current entry, set PROPERTY to VALUE.
  11770. When called interactively, this will prompt for a property name, offering
  11771. completion on existing and default properties. And then it will prompt
  11772. for a value, offering completion either on allowed values (via an inherited
  11773. xxx_ALL property) or on existing values in other instances of this property
  11774. in the current file."
  11775. (interactive
  11776. (let* ((completion-ignore-case t)
  11777. (keys (org-buffer-property-keys nil t t))
  11778. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11779. (prop (if (member prop0 keys)
  11780. prop0
  11781. (or (cdr (assoc (downcase prop0)
  11782. (mapcar (lambda (x) (cons (downcase x) x))
  11783. keys)))
  11784. prop0)))
  11785. (cur (org-entry-get nil prop))
  11786. (allowed (org-property-get-allowed-values nil prop 'table))
  11787. (existing (mapcar 'list (org-property-values prop)))
  11788. (val (if allowed
  11789. (org-completing-read "Value: " allowed nil
  11790. (not (get-text-property 0 'org-unrestricted
  11791. (caar allowed))))
  11792. (let (org-completion-use-ido org-completion-use-iswitchb)
  11793. (org-completing-read
  11794. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11795. (concat "[" cur "]") "")
  11796. ": ")
  11797. existing nil nil "" nil cur)))))
  11798. (list prop (if (equal val "") cur val))))
  11799. (unless (equal (org-entry-get nil property) value)
  11800. (org-entry-put nil property value)))
  11801. (defun org-delete-property (property)
  11802. "In the current entry, delete PROPERTY."
  11803. (interactive
  11804. (let* ((completion-ignore-case t)
  11805. (prop (org-icompleting-read
  11806. "Property: " (org-entry-properties nil 'standard))))
  11807. (list prop)))
  11808. (message "Property %s %s" property
  11809. (if (org-entry-delete nil property)
  11810. "deleted"
  11811. "was not present in the entry")))
  11812. (defun org-delete-property-globally (property)
  11813. "Remove PROPERTY globally, from all entries."
  11814. (interactive
  11815. (let* ((completion-ignore-case t)
  11816. (prop (org-icompleting-read
  11817. "Globally remove property: "
  11818. (mapcar 'list (org-buffer-property-keys)))))
  11819. (list prop)))
  11820. (save-excursion
  11821. (save-restriction
  11822. (widen)
  11823. (goto-char (point-min))
  11824. (let ((cnt 0))
  11825. (while (re-search-forward
  11826. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11827. nil t)
  11828. (setq cnt (1+ cnt))
  11829. (replace-match ""))
  11830. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11831. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11832. (defun org-compute-property-at-point ()
  11833. "Compute the property at point.
  11834. This looks for an enclosing column format, extracts the operator and
  11835. then applies it to the property in the column format's scope."
  11836. (interactive)
  11837. (unless (org-at-property-p)
  11838. (error "Not at a property"))
  11839. (let ((prop (org-match-string-no-properties 2)))
  11840. (org-columns-get-format-and-top-level)
  11841. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11842. (error "No operator defined for property %s" prop))
  11843. (org-columns-compute prop)))
  11844. (defvar org-property-allowed-value-functions nil
  11845. "Hook for functions supplying allowed values for a specific property.
  11846. The functions must take a single argument, the name of the property, and
  11847. return a flat list of allowed values. If \":ETC\" is one of
  11848. the values, this means that these values are intended as defaults for
  11849. completion, but that other values should be allowed too.
  11850. The functions must return nil if they are not responsible for this
  11851. property.")
  11852. (defun org-property-get-allowed-values (pom property &optional table)
  11853. "Get allowed values for the property PROPERTY.
  11854. When TABLE is non-nil, return an alist that can directly be used for
  11855. completion."
  11856. (let (vals)
  11857. (cond
  11858. ((equal property "TODO")
  11859. (setq vals (org-with-point-at pom
  11860. (append org-todo-keywords-1 '("")))))
  11861. ((equal property "PRIORITY")
  11862. (let ((n org-lowest-priority))
  11863. (while (>= n org-highest-priority)
  11864. (push (char-to-string n) vals)
  11865. (setq n (1- n)))))
  11866. ((member property org-special-properties))
  11867. ((setq vals (run-hook-with-args-until-success
  11868. 'org-property-allowed-value-functions property)))
  11869. (t
  11870. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11871. (when (and vals (string-match "\\S-" vals))
  11872. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11873. (setq vals (mapcar (lambda (x)
  11874. (cond ((stringp x) x)
  11875. ((numberp x) (number-to-string x))
  11876. ((symbolp x) (symbol-name x))
  11877. (t "???")))
  11878. vals)))))
  11879. (when (member ":ETC" vals)
  11880. (setq vals (remove ":ETC" vals))
  11881. (org-add-props (car vals) '(org-unrestricted t)))
  11882. (if table (mapcar 'list vals) vals)))
  11883. (defun org-property-previous-allowed-value (&optional previous)
  11884. "Switch to the next allowed value for this property."
  11885. (interactive)
  11886. (org-property-next-allowed-value t))
  11887. (defun org-property-next-allowed-value (&optional previous)
  11888. "Switch to the next allowed value for this property."
  11889. (interactive)
  11890. (unless (org-at-property-p)
  11891. (error "Not at a property"))
  11892. (let* ((key (match-string 2))
  11893. (value (match-string 3))
  11894. (allowed (or (org-property-get-allowed-values (point) key)
  11895. (and (member value '("[ ]" "[-]" "[X]"))
  11896. '("[ ]" "[X]"))))
  11897. nval)
  11898. (unless allowed
  11899. (error "Allowed values for this property have not been defined"))
  11900. (if previous (setq allowed (reverse allowed)))
  11901. (if (member value allowed)
  11902. (setq nval (car (cdr (member value allowed)))))
  11903. (setq nval (or nval (car allowed)))
  11904. (if (equal nval value)
  11905. (error "Only one allowed value for this property"))
  11906. (org-at-property-p)
  11907. (replace-match (concat " :" key ": " nval) t t)
  11908. (org-indent-line-function)
  11909. (beginning-of-line 1)
  11910. (skip-chars-forward " \t")
  11911. (run-hook-with-args 'org-property-changed-functions key nval)))
  11912. (defun org-find-entry-with-id (ident)
  11913. "Locate the entry that contains the ID property with exact value IDENT.
  11914. IDENT can be a string, a symbol or a number, this function will search for
  11915. the string representation of it.
  11916. Return the position where this entry starts, or nil if there is no such entry."
  11917. (interactive "sID: ")
  11918. (let ((id (cond
  11919. ((stringp ident) ident)
  11920. ((symbol-name ident) (symbol-name ident))
  11921. ((numberp ident) (number-to-string ident))
  11922. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11923. (case-fold-search nil))
  11924. (save-excursion
  11925. (save-restriction
  11926. (widen)
  11927. (goto-char (point-min))
  11928. (when (re-search-forward
  11929. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11930. nil t)
  11931. (org-back-to-heading t)
  11932. (point))))))
  11933. ;;;; Timestamps
  11934. (defvar org-last-changed-timestamp nil)
  11935. (defvar org-last-inserted-timestamp nil
  11936. "The last time stamp inserted with `org-insert-time-stamp'.")
  11937. (defvar org-time-was-given) ; dynamically scoped parameter
  11938. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11939. (defvar org-ts-what) ; dynamically scoped parameter
  11940. (defun org-time-stamp (arg &optional inactive)
  11941. "Prompt for a date/time and insert a time stamp.
  11942. If the user specifies a time like HH:MM, or if this command is called
  11943. with a prefix argument, the time stamp will contain date and time.
  11944. Otherwise, only the date will be included. All parts of a date not
  11945. specified by the user will be filled in from the current date/time.
  11946. So if you press just return without typing anything, the time stamp
  11947. will represent the current date/time. If there is already a timestamp
  11948. at the cursor, it will be modified."
  11949. (interactive "P")
  11950. (let* ((ts nil)
  11951. (default-time
  11952. ;; Default time is either today, or, when entering a range,
  11953. ;; the range start.
  11954. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11955. (save-excursion
  11956. (re-search-backward
  11957. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11958. (- (point) 20) t)))
  11959. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11960. (current-time)))
  11961. (default-input (and ts (org-get-compact-tod ts)))
  11962. org-time-was-given org-end-time-was-given time)
  11963. (cond
  11964. ((and (org-at-timestamp-p t)
  11965. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11966. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11967. (insert "--")
  11968. (setq time (let ((this-command this-command))
  11969. (org-read-date arg 'totime nil nil
  11970. default-time default-input)))
  11971. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11972. ((org-at-timestamp-p t)
  11973. (setq time (let ((this-command this-command))
  11974. (org-read-date arg 'totime nil nil default-time default-input)))
  11975. (when (org-at-timestamp-p t) ; just to get the match data
  11976. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11977. (replace-match "")
  11978. (setq org-last-changed-timestamp
  11979. (org-insert-time-stamp
  11980. time (or org-time-was-given arg)
  11981. inactive nil nil (list org-end-time-was-given))))
  11982. (message "Timestamp updated"))
  11983. (t
  11984. (setq time (let ((this-command this-command))
  11985. (org-read-date arg 'totime nil nil default-time default-input)))
  11986. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11987. nil nil (list org-end-time-was-given))))))
  11988. ;; FIXME: can we use this for something else, like computing time differences?
  11989. (defun org-get-compact-tod (s)
  11990. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11991. (let* ((t1 (match-string 1 s))
  11992. (h1 (string-to-number (match-string 2 s)))
  11993. (m1 (string-to-number (match-string 3 s)))
  11994. (t2 (and (match-end 4) (match-string 5 s)))
  11995. (h2 (and t2 (string-to-number (match-string 6 s))))
  11996. (m2 (and t2 (string-to-number (match-string 7 s))))
  11997. dh dm)
  11998. (if (not t2)
  11999. t1
  12000. (setq dh (- h2 h1) dm (- m2 m1))
  12001. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12002. (concat t1 "+" (number-to-string dh)
  12003. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12004. (defun org-time-stamp-inactive (&optional arg)
  12005. "Insert an inactive time stamp.
  12006. An inactive time stamp is enclosed in square brackets instead of angle
  12007. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12008. does not link to the calendar and cannot be changed with the S-cursor keys.
  12009. So these are more for recording a certain time/date."
  12010. (interactive "P")
  12011. (org-time-stamp arg 'inactive))
  12012. (defvar org-date-ovl (org-make-overlay 1 1))
  12013. (org-overlay-put org-date-ovl 'face 'org-warning)
  12014. (org-detach-overlay org-date-ovl)
  12015. (defvar org-ans1) ; dynamically scoped parameter
  12016. (defvar org-ans2) ; dynamically scoped parameter
  12017. (defvar org-plain-time-of-day-regexp) ; defined below
  12018. (defvar org-overriding-default-time nil) ; dynamically scoped
  12019. (defvar org-read-date-overlay nil)
  12020. (defvar org-dcst nil) ; dynamically scoped
  12021. (defvar org-read-date-history nil)
  12022. (defvar org-read-date-final-answer nil)
  12023. (defun org-read-date (&optional with-time to-time from-string prompt
  12024. default-time default-input)
  12025. "Read a date, possibly a time, and make things smooth for the user.
  12026. The prompt will suggest to enter an ISO date, but you can also enter anything
  12027. which will at least partially be understood by `parse-time-string'.
  12028. Unrecognized parts of the date will default to the current day, month, year,
  12029. hour and minute. If this command is called to replace a timestamp at point,
  12030. of to enter the second timestamp of a range, the default time is taken from the
  12031. existing stamp. For example,
  12032. 3-2-5 --> 2003-02-05
  12033. feb 15 --> currentyear-02-15
  12034. sep 12 9 --> 2009-09-12
  12035. 12:45 --> today 12:45
  12036. 22 sept 0:34 --> currentyear-09-22 0:34
  12037. 12 --> currentyear-currentmonth-12
  12038. Fri --> nearest Friday (today or later)
  12039. etc.
  12040. Furthermore you can specify a relative date by giving, as the *first* thing
  12041. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12042. change in days weeks, months, years.
  12043. With a single plus or minus, the date is relative to today. With a double
  12044. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12045. +4d --> four days from today
  12046. +4 --> same as above
  12047. +2w --> two weeks from today
  12048. ++5 --> five days from default date
  12049. The function understands only English month and weekday abbreviations,
  12050. but this can be configured with the variables `parse-time-months' and
  12051. `parse-time-weekdays'.
  12052. While prompting, a calendar is popped up - you can also select the
  12053. date with the mouse (button 1). The calendar shows a period of three
  12054. months. To scroll it to other months, use the keys `>' and `<'.
  12055. If you don't like the calendar, turn it off with
  12056. \(setq org-read-date-popup-calendar nil)
  12057. With optional argument TO-TIME, the date will immediately be converted
  12058. to an internal time.
  12059. With an optional argument WITH-TIME, the prompt will suggest to also
  12060. insert a time. Note that when WITH-TIME is not set, you can still
  12061. enter a time, and this function will inform the calling routine about
  12062. this change. The calling routine may then choose to change the format
  12063. used to insert the time stamp into the buffer to include the time.
  12064. With optional argument FROM-STRING, read from this string instead from
  12065. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12066. the time/date that is used for everything that is not specified by the
  12067. user."
  12068. (require 'parse-time)
  12069. (let* ((org-time-stamp-rounding-minutes
  12070. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12071. (org-dcst org-display-custom-times)
  12072. (ct (org-current-time))
  12073. (def (or org-overriding-default-time default-time ct))
  12074. (defdecode (decode-time def))
  12075. (dummy (progn
  12076. (when (< (nth 2 defdecode) org-extend-today-until)
  12077. (setcar (nthcdr 2 defdecode) -1)
  12078. (setcar (nthcdr 1 defdecode) 59)
  12079. (setq def (apply 'encode-time defdecode)
  12080. defdecode (decode-time def)))))
  12081. (calendar-frame-setup nil)
  12082. (calendar-move-hook nil)
  12083. (calendar-view-diary-initially-flag nil)
  12084. (view-diary-entries-initially nil)
  12085. (calendar-view-holidays-initially-flag nil)
  12086. (view-calendar-holidays-initially nil)
  12087. (timestr (format-time-string
  12088. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12089. (prompt (concat (if prompt (concat prompt " ") "")
  12090. (format "Date+time [%s]: " timestr)))
  12091. ans (org-ans0 "") org-ans1 org-ans2 final)
  12092. (cond
  12093. (from-string (setq ans from-string))
  12094. (org-read-date-popup-calendar
  12095. (save-excursion
  12096. (save-window-excursion
  12097. (calendar)
  12098. (calendar-forward-day (- (time-to-days def)
  12099. (calendar-absolute-from-gregorian
  12100. (calendar-current-date))))
  12101. (org-eval-in-calendar nil t)
  12102. (let* ((old-map (current-local-map))
  12103. (map (copy-keymap calendar-mode-map))
  12104. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12105. (org-defkey map (kbd "RET") 'org-calendar-select)
  12106. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  12107. 'org-calendar-select-mouse)
  12108. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  12109. 'org-calendar-select-mouse)
  12110. (org-defkey minibuffer-local-map [(meta shift left)]
  12111. (lambda () (interactive)
  12112. (org-eval-in-calendar '(calendar-backward-month 1))))
  12113. (org-defkey minibuffer-local-map [(meta shift right)]
  12114. (lambda () (interactive)
  12115. (org-eval-in-calendar '(calendar-forward-month 1))))
  12116. (org-defkey minibuffer-local-map [(meta shift up)]
  12117. (lambda () (interactive)
  12118. (org-eval-in-calendar '(calendar-backward-year 1))))
  12119. (org-defkey minibuffer-local-map [(meta shift down)]
  12120. (lambda () (interactive)
  12121. (org-eval-in-calendar '(calendar-forward-year 1))))
  12122. (org-defkey minibuffer-local-map [?\e (shift left)]
  12123. (lambda () (interactive)
  12124. (org-eval-in-calendar '(calendar-backward-month 1))))
  12125. (org-defkey minibuffer-local-map [?\e (shift right)]
  12126. (lambda () (interactive)
  12127. (org-eval-in-calendar '(calendar-forward-month 1))))
  12128. (org-defkey minibuffer-local-map [?\e (shift up)]
  12129. (lambda () (interactive)
  12130. (org-eval-in-calendar '(calendar-backward-year 1))))
  12131. (org-defkey minibuffer-local-map [?\e (shift down)]
  12132. (lambda () (interactive)
  12133. (org-eval-in-calendar '(calendar-forward-year 1))))
  12134. (org-defkey minibuffer-local-map [(shift up)]
  12135. (lambda () (interactive)
  12136. (org-eval-in-calendar '(calendar-backward-week 1))))
  12137. (org-defkey minibuffer-local-map [(shift down)]
  12138. (lambda () (interactive)
  12139. (org-eval-in-calendar '(calendar-forward-week 1))))
  12140. (org-defkey minibuffer-local-map [(shift left)]
  12141. (lambda () (interactive)
  12142. (org-eval-in-calendar '(calendar-backward-day 1))))
  12143. (org-defkey minibuffer-local-map [(shift right)]
  12144. (lambda () (interactive)
  12145. (org-eval-in-calendar '(calendar-forward-day 1))))
  12146. (org-defkey minibuffer-local-map ">"
  12147. (lambda () (interactive)
  12148. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12149. (org-defkey minibuffer-local-map "<"
  12150. (lambda () (interactive)
  12151. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12152. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12153. (unwind-protect
  12154. (progn
  12155. (use-local-map map)
  12156. (add-hook 'post-command-hook 'org-read-date-display)
  12157. (setq org-ans0 (read-string prompt default-input
  12158. 'org-read-date-history nil))
  12159. ;; org-ans0: from prompt
  12160. ;; org-ans1: from mouse click
  12161. ;; org-ans2: from calendar motion
  12162. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12163. (remove-hook 'post-command-hook 'org-read-date-display)
  12164. (use-local-map old-map)
  12165. (when org-read-date-overlay
  12166. (org-delete-overlay org-read-date-overlay)
  12167. (setq org-read-date-overlay nil)))))))
  12168. (t ; Naked prompt only
  12169. (unwind-protect
  12170. (setq ans (read-string prompt default-input
  12171. 'org-read-date-history timestr))
  12172. (when org-read-date-overlay
  12173. (org-delete-overlay org-read-date-overlay)
  12174. (setq org-read-date-overlay nil)))))
  12175. (setq final (org-read-date-analyze ans def defdecode))
  12176. (setq org-read-date-final-answer ans)
  12177. (if to-time
  12178. (apply 'encode-time final)
  12179. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12180. (format "%04d-%02d-%02d %02d:%02d"
  12181. (nth 5 final) (nth 4 final) (nth 3 final)
  12182. (nth 2 final) (nth 1 final))
  12183. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12184. (defvar def)
  12185. (defvar defdecode)
  12186. (defvar with-time)
  12187. (defvar org-read-date-analyze-futurep nil)
  12188. (defun org-read-date-display ()
  12189. "Display the current date prompt interpretation in the minibuffer."
  12190. (when org-read-date-display-live
  12191. (when org-read-date-overlay
  12192. (org-delete-overlay org-read-date-overlay))
  12193. (let ((p (point)))
  12194. (end-of-line 1)
  12195. (while (not (equal (buffer-substring
  12196. (max (point-min) (- (point) 4)) (point))
  12197. " "))
  12198. (insert " "))
  12199. (goto-char p))
  12200. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12201. " " (or org-ans1 org-ans2)))
  12202. (org-end-time-was-given nil)
  12203. (f (org-read-date-analyze ans def defdecode))
  12204. (fmts (if org-dcst
  12205. org-time-stamp-custom-formats
  12206. org-time-stamp-formats))
  12207. (fmt (if (or with-time
  12208. (and (boundp 'org-time-was-given) org-time-was-given))
  12209. (cdr fmts)
  12210. (car fmts)))
  12211. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12212. (when (and org-end-time-was-given
  12213. (string-match org-plain-time-of-day-regexp txt))
  12214. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12215. org-end-time-was-given
  12216. (substring txt (match-end 0)))))
  12217. (when org-read-date-analyze-futurep
  12218. (setq txt (concat txt " (=>F)")))
  12219. (setq org-read-date-overlay
  12220. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  12221. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12222. (defun org-read-date-analyze (ans def defdecode)
  12223. "Analyse the combined answer of the date prompt."
  12224. ;; FIXME: cleanup and comment
  12225. (let ((nowdecode (decode-time (current-time)))
  12226. delta deltan deltaw deltadef year month day
  12227. hour minute second wday pm h2 m2 tl wday1
  12228. iso-year iso-weekday iso-week iso-year iso-date futurep)
  12229. (setq org-read-date-analyze-futurep nil)
  12230. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12231. (setq ans "+0"))
  12232. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12233. (setq ans (replace-match "" t t ans)
  12234. deltan (car delta)
  12235. deltaw (nth 1 delta)
  12236. deltadef (nth 2 delta)))
  12237. ;; Check if there is an iso week date in there
  12238. ;; If yes, store the info and postpone interpreting it until the rest
  12239. ;; of the parsing is done
  12240. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12241. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  12242. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  12243. iso-week (string-to-number (match-string 2 ans)))
  12244. (setq ans (replace-match "" t t ans)))
  12245. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  12246. (when (string-match
  12247. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12248. (setq year (if (match-end 2)
  12249. (string-to-number (match-string 2 ans))
  12250. (string-to-number (format-time-string "%Y")))
  12251. month (string-to-number (match-string 3 ans))
  12252. day (string-to-number (match-string 4 ans)))
  12253. (if (< year 100) (setq year (+ 2000 year)))
  12254. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12255. t nil ans)))
  12256. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12257. ;; If there is a time with am/pm, and *no* time without it, we convert
  12258. ;; so that matching will be successful.
  12259. (loop for i from 1 to 2 do ; twice, for end time as well
  12260. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12261. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12262. (setq hour (string-to-number (match-string 1 ans))
  12263. minute (if (match-end 3)
  12264. (string-to-number (match-string 3 ans))
  12265. 0)
  12266. pm (equal ?p
  12267. (string-to-char (downcase (match-string 4 ans)))))
  12268. (if (and (= hour 12) (not pm))
  12269. (setq hour 0)
  12270. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12271. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12272. t t ans))))
  12273. ;; Check if a time range is given as a duration
  12274. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12275. (setq hour (string-to-number (match-string 1 ans))
  12276. h2 (+ hour (string-to-number (match-string 3 ans)))
  12277. minute (string-to-number (match-string 2 ans))
  12278. m2 (+ minute (if (match-end 5) (string-to-number
  12279. (match-string 5 ans))0)))
  12280. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12281. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12282. t t ans)))
  12283. ;; Check if there is a time range
  12284. (when (boundp 'org-end-time-was-given)
  12285. (setq org-time-was-given nil)
  12286. (when (and (string-match org-plain-time-of-day-regexp ans)
  12287. (match-end 8))
  12288. (setq org-end-time-was-given (match-string 8 ans))
  12289. (setq ans (concat (substring ans 0 (match-beginning 7))
  12290. (substring ans (match-end 7))))))
  12291. (setq tl (parse-time-string ans)
  12292. day (or (nth 3 tl) (nth 3 defdecode))
  12293. month (or (nth 4 tl)
  12294. (if (and org-read-date-prefer-future
  12295. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12296. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12297. (nth 4 defdecode)))
  12298. year (or (nth 5 tl)
  12299. (if (and org-read-date-prefer-future
  12300. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12301. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12302. (nth 5 defdecode)))
  12303. hour (or (nth 2 tl) (nth 2 defdecode))
  12304. minute (or (nth 1 tl) (nth 1 defdecode))
  12305. second (or (nth 0 tl) 0)
  12306. wday (nth 6 tl))
  12307. (when (and (eq org-read-date-prefer-future 'time)
  12308. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12309. (equal day (nth 3 nowdecode))
  12310. (equal month (nth 4 nowdecode))
  12311. (equal year (nth 5 nowdecode))
  12312. (nth 2 tl)
  12313. (or (< (nth 2 tl) (nth 2 nowdecode))
  12314. (and (= (nth 2 tl) (nth 2 nowdecode))
  12315. (nth 1 tl)
  12316. (< (nth 1 tl) (nth 1 nowdecode)))))
  12317. (setq day (1+ day)
  12318. futurep t))
  12319. ;; Special date definitions below
  12320. (cond
  12321. (iso-week
  12322. ;; There was an iso week
  12323. (setq futurep nil)
  12324. (setq year (or iso-year year)
  12325. day (or iso-weekday wday 1)
  12326. wday nil ; to make sure that the trigger below does not match
  12327. iso-date (calendar-gregorian-from-absolute
  12328. (calendar-absolute-from-iso
  12329. (list iso-week day year))))
  12330. ; FIXME: Should we also push ISO weeks into the future?
  12331. ; (when (and org-read-date-prefer-future
  12332. ; (not iso-year)
  12333. ; (< (calendar-absolute-from-gregorian iso-date)
  12334. ; (time-to-days (current-time))))
  12335. ; (setq year (1+ year)
  12336. ; iso-date (calendar-gregorian-from-absolute
  12337. ; (calendar-absolute-from-iso
  12338. ; (list iso-week day year)))))
  12339. (setq month (car iso-date)
  12340. year (nth 2 iso-date)
  12341. day (nth 1 iso-date)))
  12342. (deltan
  12343. (setq futurep nil)
  12344. (unless deltadef
  12345. (let ((now (decode-time (current-time))))
  12346. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12347. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12348. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12349. ((equal deltaw "m") (setq month (+ month deltan)))
  12350. ((equal deltaw "y") (setq year (+ year deltan)))))
  12351. ((and wday (not (nth 3 tl)))
  12352. (setq futurep nil)
  12353. ;; Weekday was given, but no day, so pick that day in the week
  12354. ;; on or after the derived date.
  12355. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12356. (unless (equal wday wday1)
  12357. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12358. (if (and (boundp 'org-time-was-given)
  12359. (nth 2 tl))
  12360. (setq org-time-was-given t))
  12361. (if (< year 100) (setq year (+ 2000 year)))
  12362. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12363. (setq org-read-date-analyze-futurep futurep)
  12364. (list second minute hour day month year)))
  12365. (defvar parse-time-weekdays)
  12366. (defun org-read-date-get-relative (s today default)
  12367. "Check string S for special relative date string.
  12368. TODAY and DEFAULT are internal times, for today and for a default.
  12369. Return shift list (N what def-flag)
  12370. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12371. N is the number of WHATs to shift.
  12372. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12373. the DEFAULT date rather than TODAY."
  12374. (when (and
  12375. (string-match
  12376. (concat
  12377. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12378. "\\([0-9]+\\)?"
  12379. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12380. "\\([ \t]\\|$\\)") s)
  12381. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12382. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12383. (string-to-char (substring (match-string 1 s) -1))
  12384. ?+))
  12385. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12386. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12387. (what (if (match-end 3) (match-string 3 s) "d"))
  12388. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12389. (date (if rel default today))
  12390. (wday (nth 6 (decode-time date)))
  12391. delta)
  12392. (if wday1
  12393. (progn
  12394. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12395. (if (= dir ?-) (setq delta (- delta 7)))
  12396. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12397. (list delta "d" rel))
  12398. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12399. (defun org-eval-in-calendar (form &optional keepdate)
  12400. "Eval FORM in the calendar window and return to current window.
  12401. Also, store the cursor date in variable org-ans2."
  12402. (let ((sf (selected-frame))
  12403. (sw (selected-window)))
  12404. (select-window (get-buffer-window "*Calendar*" t))
  12405. (eval form)
  12406. (when (and (not keepdate) (calendar-cursor-to-date))
  12407. (let* ((date (calendar-cursor-to-date))
  12408. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12409. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12410. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12411. (select-window sw)
  12412. (org-select-frame-set-input-focus sf)))
  12413. (defun org-calendar-select ()
  12414. "Return to `org-read-date' with the date currently selected.
  12415. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12416. (interactive)
  12417. (when (calendar-cursor-to-date)
  12418. (let* ((date (calendar-cursor-to-date))
  12419. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12420. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12421. (if (active-minibuffer-window) (exit-minibuffer))))
  12422. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12423. "Insert a date stamp for the date given by the internal TIME.
  12424. WITH-HM means, use the stamp format that includes the time of the day.
  12425. INACTIVE means use square brackets instead of angular ones, so that the
  12426. stamp will not contribute to the agenda.
  12427. PRE and POST are optional strings to be inserted before and after the
  12428. stamp.
  12429. The command returns the inserted time stamp."
  12430. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12431. stamp)
  12432. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12433. (insert-before-markers (or pre ""))
  12434. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12435. (when (listp extra)
  12436. (setq extra (car extra))
  12437. (if (and (stringp extra)
  12438. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12439. (setq extra (format "-%02d:%02d"
  12440. (string-to-number (match-string 1 extra))
  12441. (string-to-number (match-string 2 extra))))
  12442. (setq extra nil)))
  12443. (when extra
  12444. (backward-char 1)
  12445. (insert-before-markers extra)
  12446. (forward-char 1))
  12447. (insert-before-markers (or post ""))
  12448. (setq org-last-inserted-timestamp stamp)))
  12449. (defun org-toggle-time-stamp-overlays ()
  12450. "Toggle the use of custom time stamp formats."
  12451. (interactive)
  12452. (setq org-display-custom-times (not org-display-custom-times))
  12453. (unless org-display-custom-times
  12454. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12455. (while (setq p (next-single-property-change p 'display))
  12456. (if (and (get-text-property p 'display)
  12457. (eq (get-text-property p 'face) 'org-date))
  12458. (remove-text-properties
  12459. p (setq p (next-single-property-change p 'display))
  12460. '(display t))))
  12461. (set-buffer-modified-p bmp)))
  12462. (if (featurep 'xemacs)
  12463. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12464. (org-restart-font-lock)
  12465. (setq org-table-may-need-update t)
  12466. (if org-display-custom-times
  12467. (message "Time stamps are overlayed with custom format")
  12468. (message "Time stamp overlays removed")))
  12469. (defun org-display-custom-time (beg end)
  12470. "Overlay modified time stamp format over timestamp between BEG and END."
  12471. (let* ((ts (buffer-substring beg end))
  12472. t1 w1 with-hm tf time str w2 (off 0))
  12473. (save-match-data
  12474. (setq t1 (org-parse-time-string ts t))
  12475. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12476. (setq off (- (match-end 0) (match-beginning 0)))))
  12477. (setq end (- end off))
  12478. (setq w1 (- end beg)
  12479. with-hm (and (nth 1 t1) (nth 2 t1))
  12480. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12481. time (org-fix-decoded-time t1)
  12482. str (org-add-props
  12483. (format-time-string
  12484. (substring tf 1 -1) (apply 'encode-time time))
  12485. nil 'mouse-face 'highlight)
  12486. w2 (length str))
  12487. (if (not (= w2 w1))
  12488. (add-text-properties (1+ beg) (+ 2 beg)
  12489. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12490. (if (featurep 'xemacs)
  12491. (progn
  12492. (put-text-property beg end 'invisible t)
  12493. (put-text-property beg end 'end-glyph (make-glyph str)))
  12494. (put-text-property beg end 'display str))))
  12495. (defun org-translate-time (string)
  12496. "Translate all timestamps in STRING to custom format.
  12497. But do this only if the variable `org-display-custom-times' is set."
  12498. (when org-display-custom-times
  12499. (save-match-data
  12500. (let* ((start 0)
  12501. (re org-ts-regexp-both)
  12502. t1 with-hm inactive tf time str beg end)
  12503. (while (setq start (string-match re string start))
  12504. (setq beg (match-beginning 0)
  12505. end (match-end 0)
  12506. t1 (save-match-data
  12507. (org-parse-time-string (substring string beg end) t))
  12508. with-hm (and (nth 1 t1) (nth 2 t1))
  12509. inactive (equal (substring string beg (1+ beg)) "[")
  12510. tf (funcall (if with-hm 'cdr 'car)
  12511. org-time-stamp-custom-formats)
  12512. time (org-fix-decoded-time t1)
  12513. str (format-time-string
  12514. (concat
  12515. (if inactive "[" "<") (substring tf 1 -1)
  12516. (if inactive "]" ">"))
  12517. (apply 'encode-time time))
  12518. string (replace-match str t t string)
  12519. start (+ start (length str)))))))
  12520. string)
  12521. (defun org-fix-decoded-time (time)
  12522. "Set 0 instead of nil for the first 6 elements of time.
  12523. Don't touch the rest."
  12524. (let ((n 0))
  12525. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12526. (defun org-days-to-time (timestamp-string)
  12527. "Difference between TIMESTAMP-STRING and now in days."
  12528. (- (time-to-days (org-time-string-to-time timestamp-string))
  12529. (time-to-days (current-time))))
  12530. (defun org-deadline-close (timestamp-string &optional ndays)
  12531. "Is the time in TIMESTAMP-STRING close to the current date?"
  12532. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12533. (and (< (org-days-to-time timestamp-string) ndays)
  12534. (not (org-entry-is-done-p))))
  12535. (defun org-get-wdays (ts)
  12536. "Get the deadline lead time appropriate for timestring TS."
  12537. (cond
  12538. ((<= org-deadline-warning-days 0)
  12539. ;; 0 or negative, enforce this value no matter what
  12540. (- org-deadline-warning-days))
  12541. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12542. ;; lead time is specified.
  12543. (floor (* (string-to-number (match-string 1 ts))
  12544. (cdr (assoc (match-string 2 ts)
  12545. '(("d" . 1) ("w" . 7)
  12546. ("m" . 30.4) ("y" . 365.25)))))))
  12547. ;; go for the default.
  12548. (t org-deadline-warning-days)))
  12549. (defun org-calendar-select-mouse (ev)
  12550. "Return to `org-read-date' with the date currently selected.
  12551. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12552. (interactive "e")
  12553. (mouse-set-point ev)
  12554. (when (calendar-cursor-to-date)
  12555. (let* ((date (calendar-cursor-to-date))
  12556. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12557. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12558. (if (active-minibuffer-window) (exit-minibuffer))))
  12559. (defun org-check-deadlines (ndays)
  12560. "Check if there are any deadlines due or past due.
  12561. A deadline is considered due if it happens within `org-deadline-warning-days'
  12562. days from today's date. If the deadline appears in an entry marked DONE,
  12563. it is not shown. The prefix arg NDAYS can be used to test that many
  12564. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12565. (interactive "P")
  12566. (let* ((org-warn-days
  12567. (cond
  12568. ((equal ndays '(4)) 100000)
  12569. (ndays (prefix-numeric-value ndays))
  12570. (t (abs org-deadline-warning-days))))
  12571. (case-fold-search nil)
  12572. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12573. (callback
  12574. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12575. (message "%d deadlines past-due or due within %d days"
  12576. (org-occur regexp nil callback)
  12577. org-warn-days)))
  12578. (defun org-check-before-date (date)
  12579. "Check if there are deadlines or scheduled entries before DATE."
  12580. (interactive (list (org-read-date)))
  12581. (let ((case-fold-search nil)
  12582. (regexp (concat "\\<\\(" org-deadline-string
  12583. "\\|" org-scheduled-string
  12584. "\\) *<\\([^>]+\\)>"))
  12585. (callback
  12586. (lambda () (time-less-p
  12587. (org-time-string-to-time (match-string 2))
  12588. (org-time-string-to-time date)))))
  12589. (message "%d entries before %s"
  12590. (org-occur regexp nil callback) date)))
  12591. (defun org-check-after-date (date)
  12592. "Check if there are deadlines or scheduled entries after DATE."
  12593. (interactive (list (org-read-date)))
  12594. (let ((case-fold-search nil)
  12595. (regexp (concat "\\<\\(" org-deadline-string
  12596. "\\|" org-scheduled-string
  12597. "\\) *<\\([^>]+\\)>"))
  12598. (callback
  12599. (lambda () (not
  12600. (time-less-p
  12601. (org-time-string-to-time (match-string 2))
  12602. (org-time-string-to-time date))))))
  12603. (message "%d entries after %s"
  12604. (org-occur regexp nil callback) date)))
  12605. (defun org-evaluate-time-range (&optional to-buffer)
  12606. "Evaluate a time range by computing the difference between start and end.
  12607. Normally the result is just printed in the echo area, but with prefix arg
  12608. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12609. If the time range is actually in a table, the result is inserted into the
  12610. next column.
  12611. For time difference computation, a year is assumed to be exactly 365
  12612. days in order to avoid rounding problems."
  12613. (interactive "P")
  12614. (or
  12615. (org-clock-update-time-maybe)
  12616. (save-excursion
  12617. (unless (org-at-date-range-p t)
  12618. (goto-char (point-at-bol))
  12619. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12620. (if (not (org-at-date-range-p t))
  12621. (error "Not at a time-stamp range, and none found in current line")))
  12622. (let* ((ts1 (match-string 1))
  12623. (ts2 (match-string 2))
  12624. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12625. (match-end (match-end 0))
  12626. (time1 (org-time-string-to-time ts1))
  12627. (time2 (org-time-string-to-time ts2))
  12628. (t1 (org-float-time time1))
  12629. (t2 (org-float-time time2))
  12630. (diff (abs (- t2 t1)))
  12631. (negative (< (- t2 t1) 0))
  12632. ;; (ys (floor (* 365 24 60 60)))
  12633. (ds (* 24 60 60))
  12634. (hs (* 60 60))
  12635. (fy "%dy %dd %02d:%02d")
  12636. (fy1 "%dy %dd")
  12637. (fd "%dd %02d:%02d")
  12638. (fd1 "%dd")
  12639. (fh "%02d:%02d")
  12640. y d h m align)
  12641. (if havetime
  12642. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12643. y 0
  12644. d (floor (/ diff ds)) diff (mod diff ds)
  12645. h (floor (/ diff hs)) diff (mod diff hs)
  12646. m (floor (/ diff 60)))
  12647. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12648. y 0
  12649. d (floor (+ (/ diff ds) 0.5))
  12650. h 0 m 0))
  12651. (if (not to-buffer)
  12652. (message "%s" (org-make-tdiff-string y d h m))
  12653. (if (org-at-table-p)
  12654. (progn
  12655. (goto-char match-end)
  12656. (setq align t)
  12657. (and (looking-at " *|") (goto-char (match-end 0))))
  12658. (goto-char match-end))
  12659. (if (looking-at
  12660. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12661. (replace-match ""))
  12662. (if negative (insert " -"))
  12663. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12664. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12665. (insert " " (format fh h m))))
  12666. (if align (org-table-align))
  12667. (message "Time difference inserted")))))
  12668. (defun org-make-tdiff-string (y d h m)
  12669. (let ((fmt "")
  12670. (l nil))
  12671. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12672. l (push y l)))
  12673. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12674. l (push d l)))
  12675. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12676. l (push h l)))
  12677. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12678. l (push m l)))
  12679. (apply 'format fmt (nreverse l))))
  12680. (defun org-time-string-to-time (s)
  12681. (apply 'encode-time (org-parse-time-string s)))
  12682. (defun org-time-string-to-seconds (s)
  12683. (org-float-time (org-time-string-to-time s)))
  12684. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12685. "Convert a time stamp to an absolute day number.
  12686. If there is a specifyer for a cyclic time stamp, get the closest date to
  12687. DAYNR.
  12688. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12689. the variable date is bound by the calendar when this is called."
  12690. (cond
  12691. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12692. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12693. daynr
  12694. (+ daynr 1000)))
  12695. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12696. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12697. (time-to-days (current-time))) (match-string 0 s)
  12698. prefer show-all))
  12699. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12700. (defun org-days-to-iso-week (days)
  12701. "Return the iso week number."
  12702. (require 'cal-iso)
  12703. (car (calendar-iso-from-absolute days)))
  12704. (defun org-small-year-to-year (year)
  12705. "Convert 2-digit years into 4-digit years.
  12706. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12707. The year 2000 cannot be abbreviated. Any year larger than 99
  12708. is returned unchanged."
  12709. (if (< year 38)
  12710. (setq year (+ 2000 year))
  12711. (if (< year 100)
  12712. (setq year (+ 1900 year))))
  12713. year)
  12714. (defun org-time-from-absolute (d)
  12715. "Return the time corresponding to date D.
  12716. D may be an absolute day number, or a calendar-type list (month day year)."
  12717. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12718. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12719. (defun org-calendar-holiday ()
  12720. "List of holidays, for Diary display in Org-mode."
  12721. (require 'holidays)
  12722. (let ((hl (funcall
  12723. (if (fboundp 'calendar-check-holidays)
  12724. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12725. (if hl (mapconcat 'identity hl "; "))))
  12726. (defun org-diary-sexp-entry (sexp entry date)
  12727. "Process a SEXP diary ENTRY for DATE."
  12728. (require 'diary-lib)
  12729. (let ((result (if calendar-debug-sexp
  12730. (let ((stack-trace-on-error t))
  12731. (eval (car (read-from-string sexp))))
  12732. (condition-case nil
  12733. (eval (car (read-from-string sexp)))
  12734. (error
  12735. (beep)
  12736. (message "Bad sexp at line %d in %s: %s"
  12737. (org-current-line)
  12738. (buffer-file-name) sexp)
  12739. (sleep-for 2))))))
  12740. (cond ((stringp result) result)
  12741. ((and (consp result)
  12742. (stringp (cdr result))) (cdr result))
  12743. (result entry)
  12744. (t nil))))
  12745. (defun org-diary-to-ical-string (frombuf)
  12746. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12747. This uses the icalendar.el library."
  12748. (let* ((tmpdir (if (featurep 'xemacs)
  12749. (temp-directory)
  12750. temporary-file-directory))
  12751. (tmpfile (make-temp-name
  12752. (expand-file-name "orgics" tmpdir)))
  12753. buf rtn b e)
  12754. (with-current-buffer frombuf
  12755. (icalendar-export-region (point-min) (point-max) tmpfile)
  12756. (setq buf (find-buffer-visiting tmpfile))
  12757. (set-buffer buf)
  12758. (goto-char (point-min))
  12759. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12760. (setq b (match-beginning 0)))
  12761. (goto-char (point-max))
  12762. (if (re-search-backward "^END:VEVENT" nil t)
  12763. (setq e (match-end 0)))
  12764. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12765. (kill-buffer buf)
  12766. (delete-file tmpfile)
  12767. rtn))
  12768. (defun org-closest-date (start current change prefer show-all)
  12769. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12770. When PREFER is `past' return a date that is either CURRENT or past.
  12771. When PREFER is `future', return a date that is either CURRENT or future.
  12772. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12773. ;; Make the proper lists from the dates
  12774. (catch 'exit
  12775. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12776. dn dw sday cday n1 n2 n0
  12777. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12778. (setq start (org-date-to-gregorian start)
  12779. current (org-date-to-gregorian
  12780. (if show-all
  12781. current
  12782. (time-to-days (current-time))))
  12783. sday (calendar-absolute-from-gregorian start)
  12784. cday (calendar-absolute-from-gregorian current))
  12785. (if (<= cday sday) (throw 'exit sday))
  12786. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12787. (setq dn (string-to-number (match-string 1 change))
  12788. dw (cdr (assoc (match-string 2 change) a1)))
  12789. (error "Invalid change specifyer: %s" change))
  12790. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12791. (cond
  12792. ((eq dw 'day)
  12793. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12794. n2 (+ n1 dn)))
  12795. ((eq dw 'year)
  12796. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12797. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12798. (setq date1 (list m d y1)
  12799. n1 (calendar-absolute-from-gregorian date1)
  12800. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12801. n2 (calendar-absolute-from-gregorian date2)))
  12802. ((eq dw 'month)
  12803. ;; approx number of month between the two dates
  12804. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12805. ;; How often does dn fit in there?
  12806. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12807. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12808. m (+ m nm)
  12809. ny (floor (/ m 12))
  12810. y (+ y ny)
  12811. m (- m (* ny 12)))
  12812. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12813. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12814. (setq m2 (+ m dn) y2 y)
  12815. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12816. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12817. (while (<= n2 cday)
  12818. (setq n1 n2 m m2 y y2)
  12819. (setq m2 (+ m dn) y2 y)
  12820. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12821. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12822. ;; Make sure n1 is the earlier date
  12823. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12824. (if show-all
  12825. (cond
  12826. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12827. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12828. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12829. (cond
  12830. ((eq prefer 'past) (if (= cday n2) n2 n1))
  12831. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12832. (t (if (= cday n1) n1 n2)))))))
  12833. (defun org-date-to-gregorian (date)
  12834. "Turn any specification of DATE into a gregorian date for the calendar."
  12835. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12836. ((and (listp date) (= (length date) 3)) date)
  12837. ((stringp date)
  12838. (setq date (org-parse-time-string date))
  12839. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12840. ((listp date)
  12841. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12842. (defun org-parse-time-string (s &optional nodefault)
  12843. "Parse the standard Org-mode time string.
  12844. This should be a lot faster than the normal `parse-time-string'.
  12845. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12846. hour and minute fields will be nil if not given."
  12847. (if (string-match org-ts-regexp0 s)
  12848. (list 0
  12849. (if (or (match-beginning 8) (not nodefault))
  12850. (string-to-number (or (match-string 8 s) "0")))
  12851. (if (or (match-beginning 7) (not nodefault))
  12852. (string-to-number (or (match-string 7 s) "0")))
  12853. (string-to-number (match-string 4 s))
  12854. (string-to-number (match-string 3 s))
  12855. (string-to-number (match-string 2 s))
  12856. nil nil nil)
  12857. (error "Not a standard Org-mode time string: %s" s)))
  12858. (defun org-timestamp-up (&optional arg)
  12859. "Increase the date item at the cursor by one.
  12860. If the cursor is on the year, change the year. If it is on the month or
  12861. the day, change that.
  12862. With prefix ARG, change by that many units."
  12863. (interactive "p")
  12864. (org-timestamp-change (prefix-numeric-value arg)))
  12865. (defun org-timestamp-down (&optional arg)
  12866. "Decrease the date item at the cursor by one.
  12867. If the cursor is on the year, change the year. If it is on the month or
  12868. the day, change that.
  12869. With prefix ARG, change by that many units."
  12870. (interactive "p")
  12871. (org-timestamp-change (- (prefix-numeric-value arg))))
  12872. (defun org-timestamp-up-day (&optional arg)
  12873. "Increase the date in the time stamp by one day.
  12874. With prefix ARG, change that many days."
  12875. (interactive "p")
  12876. (if (and (not (org-at-timestamp-p t))
  12877. (org-on-heading-p))
  12878. (org-todo 'up)
  12879. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12880. (defun org-timestamp-down-day (&optional arg)
  12881. "Decrease the date in the time stamp by one day.
  12882. With prefix ARG, change that many days."
  12883. (interactive "p")
  12884. (if (and (not (org-at-timestamp-p t))
  12885. (org-on-heading-p))
  12886. (org-todo 'down)
  12887. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12888. (defun org-at-timestamp-p (&optional inactive-ok)
  12889. "Determine if the cursor is in or at a timestamp."
  12890. (interactive)
  12891. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12892. (pos (point))
  12893. (ans (or (looking-at tsr)
  12894. (save-excursion
  12895. (skip-chars-backward "^[<\n\r\t")
  12896. (if (> (point) (point-min)) (backward-char 1))
  12897. (and (looking-at tsr)
  12898. (> (- (match-end 0) pos) -1))))))
  12899. (and ans
  12900. (boundp 'org-ts-what)
  12901. (setq org-ts-what
  12902. (cond
  12903. ((= pos (match-beginning 0)) 'bracket)
  12904. ((= pos (1- (match-end 0))) 'bracket)
  12905. ((org-pos-in-match-range pos 2) 'year)
  12906. ((org-pos-in-match-range pos 3) 'month)
  12907. ((org-pos-in-match-range pos 7) 'hour)
  12908. ((org-pos-in-match-range pos 8) 'minute)
  12909. ((or (org-pos-in-match-range pos 4)
  12910. (org-pos-in-match-range pos 5)) 'day)
  12911. ((and (> pos (or (match-end 8) (match-end 5)))
  12912. (< pos (match-end 0)))
  12913. (- pos (or (match-end 8) (match-end 5))))
  12914. (t 'day))))
  12915. ans))
  12916. (defun org-toggle-timestamp-type ()
  12917. "Toggle the type (<active> or [inactive]) of a time stamp."
  12918. (interactive)
  12919. (when (org-at-timestamp-p t)
  12920. (let ((beg (match-beginning 0)) (end (match-end 0))
  12921. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12922. (save-excursion
  12923. (goto-char beg)
  12924. (while (re-search-forward "[][<>]" end t)
  12925. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12926. t t)))
  12927. (message "Timestamp is now %sactive"
  12928. (if (equal (char-after beg) ?<) "" "in")))))
  12929. (defun org-timestamp-change (n &optional what)
  12930. "Change the date in the time stamp at point.
  12931. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12932. `year', `minute', `second'. If WHAT is not given, the cursor position
  12933. in the timestamp determines what will be changed."
  12934. (let ((pos (point))
  12935. with-hm inactive
  12936. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12937. org-ts-what
  12938. extra rem
  12939. ts time time0)
  12940. (if (not (org-at-timestamp-p t))
  12941. (error "Not at a timestamp"))
  12942. (if (and (not what) (eq org-ts-what 'bracket))
  12943. (org-toggle-timestamp-type)
  12944. (if (and (not what) (not (eq org-ts-what 'day))
  12945. org-display-custom-times
  12946. (get-text-property (point) 'display)
  12947. (not (get-text-property (1- (point)) 'display)))
  12948. (setq org-ts-what 'day))
  12949. (setq org-ts-what (or what org-ts-what)
  12950. inactive (= (char-after (match-beginning 0)) ?\[)
  12951. ts (match-string 0))
  12952. (replace-match "")
  12953. (if (string-match
  12954. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  12955. ts)
  12956. (setq extra (match-string 1 ts)))
  12957. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12958. (setq with-hm t))
  12959. (setq time0 (org-parse-time-string ts))
  12960. (when (and (eq org-ts-what 'minute)
  12961. (eq current-prefix-arg nil))
  12962. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12963. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12964. (setcar (cdr time0) (+ (nth 1 time0)
  12965. (if (> n 0) (- rem) (- dm rem))))))
  12966. (setq time
  12967. (encode-time (or (car time0) 0)
  12968. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12969. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12970. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12971. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12972. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12973. (nthcdr 6 time0)))
  12974. (when (and (member org-ts-what '(hour minute))
  12975. extra
  12976. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12977. (setq extra (org-modify-ts-extra
  12978. extra
  12979. (if (eq org-ts-what 'hour) 2 5)
  12980. n dm)))
  12981. (when (integerp org-ts-what)
  12982. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12983. (if (eq what 'calendar)
  12984. (let ((cal-date (org-get-date-from-calendar)))
  12985. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12986. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12987. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12988. (setcar time0 (or (car time0) 0))
  12989. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12990. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12991. (setq time (apply 'encode-time time0))))
  12992. (setq org-last-changed-timestamp
  12993. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12994. (org-clock-update-time-maybe)
  12995. (goto-char pos)
  12996. ;; Try to recenter the calendar window, if any
  12997. (if (and org-calendar-follow-timestamp-change
  12998. (get-buffer-window "*Calendar*" t)
  12999. (memq org-ts-what '(day month year)))
  13000. (org-recenter-calendar (time-to-days time))))))
  13001. (defun org-modify-ts-extra (s pos n dm)
  13002. "Change the different parts of the lead-time and repeat fields in timestamp."
  13003. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13004. ng h m new rem)
  13005. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13006. (cond
  13007. ((or (org-pos-in-match-range pos 2)
  13008. (org-pos-in-match-range pos 3))
  13009. (setq m (string-to-number (match-string 3 s))
  13010. h (string-to-number (match-string 2 s)))
  13011. (if (org-pos-in-match-range pos 2)
  13012. (setq h (+ h n))
  13013. (setq n (* dm (org-no-warnings (signum n))))
  13014. (when (not (= 0 (setq rem (% m dm))))
  13015. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13016. (setq m (+ m n)))
  13017. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13018. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13019. (setq h (min 24 (max 0 h)))
  13020. (setq ng 1 new (format "-%02d:%02d" h m)))
  13021. ((org-pos-in-match-range pos 6)
  13022. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13023. ((org-pos-in-match-range pos 5)
  13024. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13025. ((org-pos-in-match-range pos 9)
  13026. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13027. ((org-pos-in-match-range pos 8)
  13028. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13029. (when ng
  13030. (setq s (concat
  13031. (substring s 0 (match-beginning ng))
  13032. new
  13033. (substring s (match-end ng))))))
  13034. s))
  13035. (defun org-recenter-calendar (date)
  13036. "If the calendar is visible, recenter it to DATE."
  13037. (let* ((win (selected-window))
  13038. (cwin (get-buffer-window "*Calendar*" t))
  13039. (calendar-move-hook nil))
  13040. (when cwin
  13041. (select-window cwin)
  13042. (calendar-goto-date (if (listp date) date
  13043. (calendar-gregorian-from-absolute date)))
  13044. (select-window win))))
  13045. (defun org-goto-calendar (&optional arg)
  13046. "Go to the Emacs calendar at the current date.
  13047. If there is a time stamp in the current line, go to that date.
  13048. A prefix ARG can be used to force the current date."
  13049. (interactive "P")
  13050. (let ((tsr org-ts-regexp) diff
  13051. (calendar-move-hook nil)
  13052. (calendar-view-holidays-initially-flag nil)
  13053. (view-calendar-holidays-initially nil)
  13054. (calendar-view-diary-initially-flag nil)
  13055. (view-diary-entries-initially nil))
  13056. (if (or (org-at-timestamp-p)
  13057. (save-excursion
  13058. (beginning-of-line 1)
  13059. (looking-at (concat ".*" tsr))))
  13060. (let ((d1 (time-to-days (current-time)))
  13061. (d2 (time-to-days
  13062. (org-time-string-to-time (match-string 1)))))
  13063. (setq diff (- d2 d1))))
  13064. (calendar)
  13065. (calendar-goto-today)
  13066. (if (and diff (not arg)) (calendar-forward-day diff))))
  13067. (defun org-get-date-from-calendar ()
  13068. "Return a list (month day year) of date at point in calendar."
  13069. (with-current-buffer "*Calendar*"
  13070. (save-match-data
  13071. (calendar-cursor-to-date))))
  13072. (defun org-date-from-calendar ()
  13073. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13074. If there is already a time stamp at the cursor position, update it."
  13075. (interactive)
  13076. (if (org-at-timestamp-p t)
  13077. (org-timestamp-change 0 'calendar)
  13078. (let ((cal-date (org-get-date-from-calendar)))
  13079. (org-insert-time-stamp
  13080. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13081. (defun org-minutes-to-hh:mm-string (m)
  13082. "Compute H:MM from a number of minutes."
  13083. (let ((h (/ m 60)))
  13084. (setq m (- m (* 60 h)))
  13085. (format org-time-clocksum-format h m)))
  13086. (defun org-hh:mm-string-to-minutes (s)
  13087. "Convert a string H:MM to a number of minutes.
  13088. If the string is just a number, interpret it as minutes.
  13089. In fact, the first hh:mm or number in the string will be taken,
  13090. there can be extra stuff in the string.
  13091. If no number is found, the return value is 0."
  13092. (cond
  13093. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13094. (+ (* (string-to-number (match-string 1 s)) 60)
  13095. (string-to-number (match-string 2 s))))
  13096. ((string-match "\\([0-9]+\\)" s)
  13097. (string-to-number (match-string 1 s)))
  13098. (t 0)))
  13099. ;;;; Files
  13100. (defun org-save-all-org-buffers ()
  13101. "Save all Org-mode buffers without user confirmation."
  13102. (interactive)
  13103. (message "Saving all Org-mode buffers...")
  13104. (save-some-buffers t 'org-mode-p)
  13105. (when (featurep 'org-id) (org-id-locations-save))
  13106. (message "Saving all Org-mode buffers... done"))
  13107. (defun org-revert-all-org-buffers ()
  13108. "Revert all Org-mode buffers.
  13109. Prompt for confirmation when there are unsaved changes.
  13110. Be sure you know what you are doing before letting this function
  13111. overwrite your changes.
  13112. This function is useful in a setup where one tracks org files
  13113. with a version control system, to revert on one machine after pulling
  13114. changes from another. I believe the procedure must be like this:
  13115. 1. M-x org-save-all-org-buffers
  13116. 2. Pull changes from the other machine, resolve conflicts
  13117. 3. M-x org-revert-all-org-buffers"
  13118. (interactive)
  13119. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13120. (error "Abort"))
  13121. (save-excursion
  13122. (save-window-excursion
  13123. (mapc
  13124. (lambda (b)
  13125. (when (and (with-current-buffer b (org-mode-p))
  13126. (with-current-buffer b buffer-file-name))
  13127. (switch-to-buffer b)
  13128. (revert-buffer t 'no-confirm)))
  13129. (buffer-list))
  13130. (when (and (featurep 'org-id) org-id-track-globally)
  13131. (org-id-locations-load)))))
  13132. ;;;; Agenda files
  13133. ;;;###autoload
  13134. (defun org-iswitchb (&optional arg)
  13135. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13136. With a prefix argument, restrict available to files.
  13137. With two prefix arguments, restrict available buffers to agenda files."
  13138. (interactive "P")
  13139. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13140. ((equal arg '(16)) (org-buffer-list 'agenda))
  13141. (t (org-buffer-list)))))
  13142. (switch-to-buffer
  13143. (org-icompleting-read "Org buffer: "
  13144. (mapcar 'list (mapcar 'buffer-name blist))
  13145. nil t))))
  13146. ;;;###autoload
  13147. (defalias 'org-ido-switchb 'org-iswitchb)
  13148. (defun org-buffer-list (&optional predicate exclude-tmp)
  13149. "Return a list of Org buffers.
  13150. PREDICATE can be `export', `files' or `agenda'.
  13151. export restrict the list to Export buffers.
  13152. files restrict the list to buffers visiting Org files.
  13153. agenda restrict the list to buffers visiting agenda files.
  13154. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13155. (let* ((bfn nil)
  13156. (agenda-files (and (eq predicate 'agenda)
  13157. (mapcar 'file-truename (org-agenda-files t))))
  13158. (filter
  13159. (cond
  13160. ((eq predicate 'files)
  13161. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13162. ((eq predicate 'export)
  13163. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13164. ((eq predicate 'agenda)
  13165. (lambda (b)
  13166. (with-current-buffer b
  13167. (and (eq major-mode 'org-mode)
  13168. (setq bfn (buffer-file-name b))
  13169. (member (file-truename bfn) agenda-files)))))
  13170. (t (lambda (b) (with-current-buffer b
  13171. (or (eq major-mode 'org-mode)
  13172. (string-match "\*Org .*Export"
  13173. (buffer-name b)))))))))
  13174. (delq nil
  13175. (mapcar
  13176. (lambda(b)
  13177. (if (and (funcall filter b)
  13178. (or (not exclude-tmp)
  13179. (not (string-match "tmp" (buffer-name b)))))
  13180. b
  13181. nil))
  13182. (buffer-list)))))
  13183. (defun org-agenda-files (&optional unrestricted archives)
  13184. "Get the list of agenda files.
  13185. Optional UNRESTRICTED means return the full list even if a restriction
  13186. is currently in place.
  13187. When ARCHIVES is t, include all archive files hat are really being
  13188. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13189. `org-agenda-archives-mode' is t."
  13190. (let ((files
  13191. (cond
  13192. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13193. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13194. ((listp org-agenda-files) org-agenda-files)
  13195. (t (error "Invalid value of `org-agenda-files'")))))
  13196. (setq files (apply 'append
  13197. (mapcar (lambda (f)
  13198. (if (file-directory-p f)
  13199. (directory-files
  13200. f t org-agenda-file-regexp)
  13201. (list f)))
  13202. files)))
  13203. (when org-agenda-skip-unavailable-files
  13204. (setq files (delq nil
  13205. (mapcar (function
  13206. (lambda (file)
  13207. (and (file-readable-p file) file)))
  13208. files))))
  13209. (when (or (eq archives t)
  13210. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13211. (setq files (org-add-archive-files files)))
  13212. files))
  13213. (defun org-edit-agenda-file-list ()
  13214. "Edit the list of agenda files.
  13215. Depending on setup, this either uses customize to edit the variable
  13216. `org-agenda-files', or it visits the file that is holding the list. In the
  13217. latter case, the buffer is set up in a way that saving it automatically kills
  13218. the buffer and restores the previous window configuration."
  13219. (interactive)
  13220. (if (stringp org-agenda-files)
  13221. (let ((cw (current-window-configuration)))
  13222. (find-file org-agenda-files)
  13223. (org-set-local 'org-window-configuration cw)
  13224. (org-add-hook 'after-save-hook
  13225. (lambda ()
  13226. (set-window-configuration
  13227. (prog1 org-window-configuration
  13228. (kill-buffer (current-buffer))))
  13229. (org-install-agenda-files-menu)
  13230. (message "New agenda file list installed"))
  13231. nil 'local)
  13232. (message "%s" (substitute-command-keys
  13233. "Edit list and finish with \\[save-buffer]")))
  13234. (customize-variable 'org-agenda-files)))
  13235. (defun org-store-new-agenda-file-list (list)
  13236. "Set new value for the agenda file list and save it correctly."
  13237. (if (stringp org-agenda-files)
  13238. (let ((f org-agenda-files) b)
  13239. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  13240. (with-temp-file f
  13241. (insert (mapconcat 'identity list "\n") "\n")))
  13242. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13243. (org-insert-mode-line-in-empty-file nil))
  13244. (setq org-agenda-files list)
  13245. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13246. (defun org-read-agenda-file-list ()
  13247. "Read the list of agenda files from a file."
  13248. (when (file-directory-p org-agenda-files)
  13249. (error "`org-agenda-files' cannot be a single directory"))
  13250. (when (stringp org-agenda-files)
  13251. (with-temp-buffer
  13252. (insert-file-contents org-agenda-files)
  13253. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  13254. ;;;###autoload
  13255. (defun org-cycle-agenda-files ()
  13256. "Cycle through the files in `org-agenda-files'.
  13257. If the current buffer visits an agenda file, find the next one in the list.
  13258. If the current buffer does not, find the first agenda file."
  13259. (interactive)
  13260. (let* ((fs (org-agenda-files t))
  13261. (files (append fs (list (car fs))))
  13262. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13263. file)
  13264. (unless files (error "No agenda files"))
  13265. (catch 'exit
  13266. (while (setq file (pop files))
  13267. (if (equal (file-truename file) tcf)
  13268. (when (car files)
  13269. (find-file (car files))
  13270. (throw 'exit t))))
  13271. (find-file (car fs)))
  13272. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13273. (defun org-agenda-file-to-front (&optional to-end)
  13274. "Move/add the current file to the top of the agenda file list.
  13275. If the file is not present in the list, it is added to the front. If it is
  13276. present, it is moved there. With optional argument TO-END, add/move to the
  13277. end of the list."
  13278. (interactive "P")
  13279. (let ((org-agenda-skip-unavailable-files nil)
  13280. (file-alist (mapcar (lambda (x)
  13281. (cons (file-truename x) x))
  13282. (org-agenda-files t)))
  13283. (ctf (file-truename buffer-file-name))
  13284. x had)
  13285. (setq x (assoc ctf file-alist) had x)
  13286. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13287. (if to-end
  13288. (setq file-alist (append (delq x file-alist) (list x)))
  13289. (setq file-alist (cons x (delq x file-alist))))
  13290. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13291. (org-install-agenda-files-menu)
  13292. (message "File %s to %s of agenda file list"
  13293. (if had "moved" "added") (if to-end "end" "front"))))
  13294. (defun org-remove-file (&optional file)
  13295. "Remove current file from the list of files in variable `org-agenda-files'.
  13296. These are the files which are being checked for agenda entries.
  13297. Optional argument FILE means, use this file instead of the current."
  13298. (interactive)
  13299. (let* ((org-agenda-skip-unavailable-files nil)
  13300. (file (or file buffer-file-name))
  13301. (true-file (file-truename file))
  13302. (afile (abbreviate-file-name file))
  13303. (files (delq nil (mapcar
  13304. (lambda (x)
  13305. (if (equal true-file
  13306. (file-truename x))
  13307. nil x))
  13308. (org-agenda-files t)))))
  13309. (if (not (= (length files) (length (org-agenda-files t))))
  13310. (progn
  13311. (org-store-new-agenda-file-list files)
  13312. (org-install-agenda-files-menu)
  13313. (message "Removed file: %s" afile))
  13314. (message "File was not in list: %s (not removed)" afile))))
  13315. (defun org-file-menu-entry (file)
  13316. (vector file (list 'find-file file) t))
  13317. (defun org-check-agenda-file (file)
  13318. "Make sure FILE exists. If not, ask user what to do."
  13319. (when (not (file-exists-p file))
  13320. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13321. (abbreviate-file-name file))
  13322. (let ((r (downcase (read-char-exclusive))))
  13323. (cond
  13324. ((equal r ?r)
  13325. (org-remove-file file)
  13326. (throw 'nextfile t))
  13327. (t (error "Abort"))))))
  13328. (defun org-get-agenda-file-buffer (file)
  13329. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13330. it to the list of buffers which might be released later."
  13331. (let ((buf (org-find-base-buffer-visiting file)))
  13332. (if buf
  13333. buf ; just return it
  13334. ;; Make a new buffer and remember it
  13335. (setq buf (find-file-noselect file))
  13336. (if buf (push buf org-agenda-new-buffers))
  13337. buf)))
  13338. (defun org-release-buffers (blist)
  13339. "Release all buffers in list, asking the user for confirmation when needed.
  13340. When a buffer is unmodified, it is just killed. When modified, it is saved
  13341. \(if the user agrees) and then killed."
  13342. (let (buf file)
  13343. (while (setq buf (pop blist))
  13344. (setq file (buffer-file-name buf))
  13345. (when (and (buffer-modified-p buf)
  13346. file
  13347. (y-or-n-p (format "Save file %s? " file)))
  13348. (with-current-buffer buf (save-buffer)))
  13349. (kill-buffer buf))))
  13350. (defun org-prepare-agenda-buffers (files)
  13351. "Create buffers for all agenda files, protect archived trees and comments."
  13352. (interactive)
  13353. (let ((pa '(:org-archived t))
  13354. (pc '(:org-comment t))
  13355. (pall '(:org-archived t :org-comment t))
  13356. (inhibit-read-only t)
  13357. (rea (concat ":" org-archive-tag ":"))
  13358. bmp file re)
  13359. (save-excursion
  13360. (save-restriction
  13361. (while (setq file (pop files))
  13362. (catch 'nextfile
  13363. (if (bufferp file)
  13364. (set-buffer file)
  13365. (org-check-agenda-file file)
  13366. (set-buffer (org-get-agenda-file-buffer file)))
  13367. (widen)
  13368. (setq bmp (buffer-modified-p))
  13369. (org-refresh-category-properties)
  13370. (setq org-todo-keywords-for-agenda
  13371. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13372. (setq org-done-keywords-for-agenda
  13373. (append org-done-keywords-for-agenda org-done-keywords))
  13374. (setq org-todo-keyword-alist-for-agenda
  13375. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13376. (setq org-drawers-for-agenda
  13377. (append org-drawers-for-agenda org-drawers))
  13378. (setq org-tag-alist-for-agenda
  13379. (append org-tag-alist-for-agenda org-tag-alist))
  13380. (save-excursion
  13381. (remove-text-properties (point-min) (point-max) pall)
  13382. (when org-agenda-skip-archived-trees
  13383. (goto-char (point-min))
  13384. (while (re-search-forward rea nil t)
  13385. (if (org-on-heading-p t)
  13386. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13387. (goto-char (point-min))
  13388. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13389. (while (re-search-forward re nil t)
  13390. (add-text-properties
  13391. (match-beginning 0) (org-end-of-subtree t) pc)))
  13392. (set-buffer-modified-p bmp)))))
  13393. (setq org-todo-keyword-alist-for-agenda
  13394. (org-uniquify org-todo-keyword-alist-for-agenda)
  13395. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13396. ;;;; Embedded LaTeX
  13397. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13398. "Keymap for the minor `org-cdlatex-mode'.")
  13399. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13400. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13401. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13402. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13403. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13404. (defvar org-cdlatex-texmathp-advice-is-done nil
  13405. "Flag remembering if we have applied the advice to texmathp already.")
  13406. (define-minor-mode org-cdlatex-mode
  13407. "Toggle the minor `org-cdlatex-mode'.
  13408. This mode supports entering LaTeX environment and math in LaTeX fragments
  13409. in Org-mode.
  13410. \\{org-cdlatex-mode-map}"
  13411. nil " OCDL" nil
  13412. (when org-cdlatex-mode (require 'cdlatex))
  13413. (unless org-cdlatex-texmathp-advice-is-done
  13414. (setq org-cdlatex-texmathp-advice-is-done t)
  13415. (defadvice texmathp (around org-math-always-on activate)
  13416. "Always return t in org-mode buffers.
  13417. This is because we want to insert math symbols without dollars even outside
  13418. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13419. an embedded LaTeX fragment, let texmathp do its job.
  13420. \\[org-cdlatex-mode-map]"
  13421. (interactive)
  13422. (let (p)
  13423. (cond
  13424. ((not (org-mode-p)) ad-do-it)
  13425. ((eq this-command 'cdlatex-math-symbol)
  13426. (setq ad-return-value t
  13427. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13428. (t
  13429. (let ((p (org-inside-LaTeX-fragment-p)))
  13430. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13431. (setq ad-return-value t
  13432. texmathp-why '("Org-mode embedded math" . 0))
  13433. (if p ad-do-it)))))))))
  13434. (defun turn-on-org-cdlatex ()
  13435. "Unconditionally turn on `org-cdlatex-mode'."
  13436. (org-cdlatex-mode 1))
  13437. (defun org-inside-LaTeX-fragment-p ()
  13438. "Test if point is inside a LaTeX fragment.
  13439. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13440. sequence appearing also before point.
  13441. Even though the matchers for math are configurable, this function assumes
  13442. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13443. delimiters are skipped when they have been removed by customization.
  13444. The return value is nil, or a cons cell with the delimiter and
  13445. and the position of this delimiter.
  13446. This function does a reasonably good job, but can locally be fooled by
  13447. for example currency specifications. For example it will assume being in
  13448. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13449. fragments that are properly closed, but during editing, we have to live
  13450. with the uncertainty caused by missing closing delimiters. This function
  13451. looks only before point, not after."
  13452. (catch 'exit
  13453. (let ((pos (point))
  13454. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13455. (lim (progn
  13456. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13457. (point)))
  13458. dd-on str (start 0) m re)
  13459. (goto-char pos)
  13460. (when dodollar
  13461. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13462. re (nth 1 (assoc "$" org-latex-regexps)))
  13463. (while (string-match re str start)
  13464. (cond
  13465. ((= (match-end 0) (length str))
  13466. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13467. ((= (match-end 0) (- (length str) 5))
  13468. (throw 'exit nil))
  13469. (t (setq start (match-end 0))))))
  13470. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13471. (goto-char pos)
  13472. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13473. (and (match-beginning 2) (throw 'exit nil))
  13474. ;; count $$
  13475. (while (re-search-backward "\\$\\$" lim t)
  13476. (setq dd-on (not dd-on)))
  13477. (goto-char pos)
  13478. (if dd-on (cons "$$" m))))))
  13479. (defun org-inside-latex-macro-p ()
  13480. "Is point inside a LaTeX macro or its arguments?"
  13481. (save-match-data
  13482. (org-in-regexp
  13483. "\\\\[a-zA-Z]+\\*?\\(\\[[^][\n{}]*\\]\\)?\\({[^{}\n]*}\\)?")))
  13484. (defun org-try-cdlatex-tab ()
  13485. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13486. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13487. - inside a LaTeX fragment, or
  13488. - after the first word in a line, where an abbreviation expansion could
  13489. insert a LaTeX environment."
  13490. (when org-cdlatex-mode
  13491. (cond
  13492. ((save-excursion
  13493. (skip-chars-backward "a-zA-Z0-9*")
  13494. (skip-chars-backward " \t")
  13495. (bolp))
  13496. (cdlatex-tab) t)
  13497. ((org-inside-LaTeX-fragment-p)
  13498. (cdlatex-tab) t)
  13499. (t nil))))
  13500. (defun org-cdlatex-underscore-caret (&optional arg)
  13501. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13502. Revert to the normal definition outside of these fragments."
  13503. (interactive "P")
  13504. (if (org-inside-LaTeX-fragment-p)
  13505. (call-interactively 'cdlatex-sub-superscript)
  13506. (let (org-cdlatex-mode)
  13507. (call-interactively (key-binding (vector last-input-event))))))
  13508. (defun org-cdlatex-math-modify (&optional arg)
  13509. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13510. Revert to the normal definition outside of these fragments."
  13511. (interactive "P")
  13512. (if (org-inside-LaTeX-fragment-p)
  13513. (call-interactively 'cdlatex-math-modify)
  13514. (let (org-cdlatex-mode)
  13515. (call-interactively (key-binding (vector last-input-event))))))
  13516. (defvar org-latex-fragment-image-overlays nil
  13517. "List of overlays carrying the images of latex fragments.")
  13518. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13519. (defun org-remove-latex-fragment-image-overlays ()
  13520. "Remove all overlays with LaTeX fragment images in current buffer."
  13521. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13522. (setq org-latex-fragment-image-overlays nil))
  13523. (defun org-preview-latex-fragment (&optional subtree)
  13524. "Preview the LaTeX fragment at point, or all locally or globally.
  13525. If the cursor is in a LaTeX fragment, create the image and overlay
  13526. it over the source code. If there is no fragment at point, display
  13527. all fragments in the current text, from one headline to the next. With
  13528. prefix SUBTREE, display all fragments in the current subtree. With a
  13529. double prefix `C-u C-u', or when the cursor is before the first headline,
  13530. display all fragments in the buffer.
  13531. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13532. (interactive "P")
  13533. (org-remove-latex-fragment-image-overlays)
  13534. (save-excursion
  13535. (save-restriction
  13536. (let (beg end at msg)
  13537. (cond
  13538. ((or (equal subtree '(16))
  13539. (not (save-excursion
  13540. (re-search-backward (concat "^" outline-regexp) nil t))))
  13541. (setq beg (point-min) end (point-max)
  13542. msg "Creating images for buffer...%s"))
  13543. ((equal subtree '(4))
  13544. (org-back-to-heading)
  13545. (setq beg (point) end (org-end-of-subtree t)
  13546. msg "Creating images for subtree...%s"))
  13547. (t
  13548. (if (setq at (org-inside-LaTeX-fragment-p))
  13549. (goto-char (max (point-min) (- (cdr at) 2)))
  13550. (org-back-to-heading))
  13551. (setq beg (point) end (progn (outline-next-heading) (point))
  13552. msg (if at "Creating image...%s"
  13553. "Creating images for entry...%s"))))
  13554. (message msg "")
  13555. (narrow-to-region beg end)
  13556. (goto-char beg)
  13557. (org-format-latex
  13558. (concat "ltxpng/" (file-name-sans-extension
  13559. (file-name-nondirectory
  13560. buffer-file-name)))
  13561. default-directory 'overlays msg at 'forbuffer)
  13562. (message msg "done. Use `C-c C-c' to remove images.")))))
  13563. (defvar org-latex-regexps
  13564. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13565. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13566. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13567. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13568. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13569. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13570. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13571. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13572. "Regular expressions for matching embedded LaTeX.")
  13573. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13574. "Replace LaTeX fragments with links to an image, and produce images.
  13575. Some of the options can be changed using the variable
  13576. `org-format-latex-options'."
  13577. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13578. (let* ((prefixnodir (file-name-nondirectory prefix))
  13579. (absprefix (expand-file-name prefix dir))
  13580. (todir (file-name-directory absprefix))
  13581. (opt org-format-latex-options)
  13582. (matchers (plist-get opt :matchers))
  13583. (re-list org-latex-regexps)
  13584. (org-format-latex-header-extra
  13585. (plist-get (org-infile-export-plist) :latex-header-extra))
  13586. (cnt 0) txt hash link beg end re e checkdir
  13587. executables-checked
  13588. m n block linkfile movefile ov)
  13589. ;; Check the different regular expressions
  13590. (while (setq e (pop re-list))
  13591. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13592. block (if (nth 3 e) "\n\n" ""))
  13593. (when (member m matchers)
  13594. (goto-char (point-min))
  13595. (while (re-search-forward re nil t)
  13596. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13597. (not (get-text-property (match-beginning n)
  13598. 'org-protected))
  13599. (or (not overlays)
  13600. (not (eq (get-char-property (match-beginning n)
  13601. 'org-overlay-type)
  13602. 'org-latex-overlay))))
  13603. (setq txt (match-string n)
  13604. beg (match-beginning n) end (match-end n)
  13605. cnt (1+ cnt))
  13606. (let (print-length print-level) ; make sure full list is printed
  13607. (setq hash (sha1 (prin1-to-string
  13608. (list org-format-latex-header
  13609. org-format-latex-header-extra
  13610. org-export-latex-packages-alist
  13611. org-format-latex-options
  13612. forbuffer txt)))
  13613. linkfile (format "%s_%s.png" prefix hash)
  13614. movefile (format "%s_%s.png" absprefix hash)))
  13615. (setq link (concat block "[[file:" linkfile "]]" block))
  13616. (if msg (message msg cnt))
  13617. (goto-char beg)
  13618. (unless checkdir ; make sure the directory exists
  13619. (setq checkdir t)
  13620. (or (file-directory-p todir) (make-directory todir)))
  13621. (unless executables-checked
  13622. (org-check-external-command
  13623. "latex" "needed to convert LaTeX fragments to images")
  13624. (org-check-external-command
  13625. "dvipng" "needed to convert LaTeX fragments to images")
  13626. (setq executables-checked t))
  13627. (unless (file-exists-p movefile)
  13628. (org-create-formula-image
  13629. txt movefile opt forbuffer))
  13630. (if overlays
  13631. (progn
  13632. (mapc (lambda (o)
  13633. (if (eq (org-overlay-get o 'org-overlay-type)
  13634. 'org-latex-overlay)
  13635. (org-delete-overlay o)))
  13636. (org-overlays-in beg end))
  13637. (setq ov (org-make-overlay beg end))
  13638. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13639. (if (featurep 'xemacs)
  13640. (progn
  13641. (org-overlay-put ov 'invisible t)
  13642. (org-overlay-put
  13643. ov 'end-glyph
  13644. (make-glyph (vector 'png :file movefile))))
  13645. (org-overlay-put
  13646. ov 'display
  13647. (list 'image :type 'png :file movefile :ascent 'center)))
  13648. (push ov org-latex-fragment-image-overlays)
  13649. (goto-char end))
  13650. (delete-region beg end)
  13651. (insert link))))))))
  13652. ;; This function borrows from Ganesh Swami's latex2png.el
  13653. (defun org-create-formula-image (string tofile options buffer)
  13654. "This calls dvipng."
  13655. (require 'org-latex)
  13656. (let* ((tmpdir (if (featurep 'xemacs)
  13657. (temp-directory)
  13658. temporary-file-directory))
  13659. (texfilebase (make-temp-name
  13660. (expand-file-name "orgtex" tmpdir)))
  13661. (texfile (concat texfilebase ".tex"))
  13662. (dvifile (concat texfilebase ".dvi"))
  13663. (pngfile (concat texfilebase ".png"))
  13664. (fnh (if (featurep 'xemacs)
  13665. (font-height (get-face-font 'default))
  13666. (face-attribute 'default :height nil)))
  13667. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13668. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13669. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13670. "Black"))
  13671. (bg (or (plist-get options (if buffer :background :html-background))
  13672. "Transparent")))
  13673. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13674. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13675. (with-temp-file texfile
  13676. (insert org-format-latex-header
  13677. (if org-export-latex-packages-alist
  13678. (concat "\n"
  13679. (mapconcat (lambda(p)
  13680. (if (equal "" (car p))
  13681. (format "\\usepackage{%s}" (cadr p))
  13682. (format "\\usepackage[%s]{%s}"
  13683. (car p) (cadr p))))
  13684. org-export-latex-packages-alist "\n"))
  13685. "")
  13686. (if org-format-latex-header-extra
  13687. (concat "\n" org-format-latex-header-extra)
  13688. "")
  13689. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  13690. (let ((dir default-directory))
  13691. (condition-case nil
  13692. (progn
  13693. (cd tmpdir)
  13694. (call-process "latex" nil nil nil texfile))
  13695. (error nil))
  13696. (cd dir))
  13697. (if (not (file-exists-p dvifile))
  13698. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13699. (condition-case nil
  13700. (call-process "dvipng" nil nil nil
  13701. "-fg" fg "-bg" bg
  13702. "-D" dpi
  13703. ;;"-x" scale "-y" scale
  13704. "-T" "tight"
  13705. "-o" pngfile
  13706. dvifile)
  13707. (error nil))
  13708. (if (not (file-exists-p pngfile))
  13709. (progn (message "Failed to create png file from %s" texfile) nil)
  13710. ;; Use the requested file name and clean up
  13711. (copy-file pngfile tofile 'replace)
  13712. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13713. (delete-file (concat texfilebase e)))
  13714. pngfile))))
  13715. (defun org-dvipng-color (attr)
  13716. "Return an rgb color specification for dvipng."
  13717. (apply 'format "rgb %s %s %s"
  13718. (mapcar 'org-normalize-color
  13719. (color-values (face-attribute 'default attr nil)))))
  13720. (defun org-normalize-color (value)
  13721. "Return string to be used as color value for an RGB component."
  13722. (format "%g" (/ value 65535.0)))
  13723. ;;;; Key bindings
  13724. ;; Make `C-c C-x' a prefix key
  13725. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13726. ;; TAB key with modifiers
  13727. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13728. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13729. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13730. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13731. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13732. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13733. ;; The following line is necessary under Suse GNU/Linux
  13734. (unless (featurep 'xemacs)
  13735. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13736. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13737. (define-key org-mode-map [backtab] 'org-shifttab)
  13738. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13739. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13740. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13741. ;; Cursor keys with modifiers
  13742. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13743. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13744. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13745. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13746. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13747. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13748. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13749. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13750. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13751. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13752. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13753. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13754. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13755. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13756. ;;; Extra keys for tty access.
  13757. ;; We only set them when really needed because otherwise the
  13758. ;; menus don't show the simple keys
  13759. (when (or org-use-extra-keys
  13760. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13761. (not window-system))
  13762. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13763. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13764. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13765. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13766. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13767. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13768. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13769. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13770. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13771. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13772. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13773. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13774. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13775. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13776. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13777. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13778. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13779. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13780. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13781. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13782. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13783. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13784. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13785. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13786. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13787. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13788. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13789. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13790. ;; All the other keys
  13791. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13792. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13793. (if (boundp 'narrow-map)
  13794. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13795. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13796. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13797. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13798. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13799. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13800. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  13801. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13802. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13803. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13804. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13805. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13806. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13807. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13808. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13809. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13810. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13811. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13812. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13813. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13814. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13815. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13816. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13817. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13818. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13819. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13820. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13821. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13822. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13823. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13824. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13825. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13826. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13827. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13828. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13829. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13830. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13831. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13832. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13833. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13834. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13835. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13836. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13837. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13838. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13839. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13840. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13841. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13842. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13843. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13844. (org-defkey org-mode-map "\C-m" 'org-return)
  13845. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13846. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13847. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13848. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13849. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13850. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13851. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13852. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13853. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13854. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13855. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13856. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13857. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13858. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13859. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13860. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13861. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13862. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  13863. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  13864. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13865. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13866. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13867. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13868. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13869. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13870. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13871. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13872. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13873. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13874. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13875. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13876. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13877. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13878. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13879. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13880. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13881. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13882. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13883. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13884. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13885. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13886. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13887. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13888. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13889. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13890. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13891. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13892. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13893. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13894. (when (featurep 'xemacs)
  13895. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13896. (defconst org-speed-commands-default
  13897. '(
  13898. ("Outline Navigation")
  13899. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  13900. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  13901. ("f" . (org-speed-move-safe 'org-forward-same-level))
  13902. ("b" . (org-speed-move-safe 'org-backward-same-level))
  13903. ("u" . (org-speed-move-safe 'outline-up-heading))
  13904. ("j" . org-goto)
  13905. ("g" . (org-refile t))
  13906. ("Outline Visibility")
  13907. ("c" . org-cycle)
  13908. ("C" . org-shifttab)
  13909. (" " . org-display-outline-path)
  13910. ("Outline Structure Editing")
  13911. ("U" . org-shiftmetaup)
  13912. ("D" . org-shiftmetadown)
  13913. ("r" . org-metaright)
  13914. ("l" . org-metaleft)
  13915. ("R" . org-shiftmetaright)
  13916. ("L" . org-shiftmetaleft)
  13917. ("i" . (progn (forward-char 1) (call-interactively
  13918. 'org-insert-heading-respect-content)))
  13919. ("^" . org-sort)
  13920. ("w" . org-refile)
  13921. ("a" . org-archive-subtree-default-with-confirmation)
  13922. ("." . outline-mark-subtree)
  13923. ("Clock Commands")
  13924. ("I" . org-clock-in)
  13925. ("O" . org-clock-out)
  13926. ("Meta Data Editing")
  13927. ("t" . org-todo)
  13928. ("0" . (org-priority ?\ ))
  13929. ("1" . (org-priority ?A))
  13930. ("2" . (org-priority ?B))
  13931. ("3" . (org-priority ?C))
  13932. (";" . org-set-tags-command)
  13933. ("e" . org-set-effort)
  13934. ("Agenda Views etc")
  13935. ("v" . org-agenda)
  13936. ("/" . org-sparse-tree)
  13937. ("Misc")
  13938. ("o" . org-open-at-point)
  13939. ("?" . org-speed-command-help)
  13940. )
  13941. "The default speed commands.")
  13942. (defun org-print-speed-command (e)
  13943. (if (> (length (car e)) 1)
  13944. (progn
  13945. (princ "\n")
  13946. (princ (car e))
  13947. (princ "\n")
  13948. (princ (make-string (length (car e)) ?-))
  13949. (princ "\n"))
  13950. (princ (car e))
  13951. (princ " ")
  13952. (if (symbolp (cdr e))
  13953. (princ (symbol-name (cdr e)))
  13954. (prin1 (cdr e)))
  13955. (princ "\n")))
  13956. (defun org-speed-command-help ()
  13957. "Show the available speed commands."
  13958. (interactive)
  13959. (if (not org-use-speed-commands)
  13960. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  13961. (with-output-to-temp-buffer "*Help*"
  13962. (princ "User-defined Speed commands\n===========================\n")
  13963. (mapc 'org-print-speed-command org-speed-commands-user)
  13964. (princ "\n")
  13965. (princ "Built-in Speed commands\n=======================\n")
  13966. (mapc 'org-print-speed-command org-speed-commands-default))
  13967. (with-current-buffer "*Help*"
  13968. (setq truncate-lines t))))
  13969. (defun org-speed-move-safe (cmd)
  13970. "Execute CMD, but make sure that the cursor always ends up in a headline.
  13971. If not, return to the original position and throw an error."
  13972. (interactive)
  13973. (let ((pos (point)))
  13974. (call-interactively cmd)
  13975. (unless (and (bolp) (org-on-heading-p))
  13976. (goto-char pos)
  13977. (error "Boundary reached while executing %s" cmd))))
  13978. (defvar org-self-insert-command-undo-counter 0)
  13979. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13980. (defvar org-speed-command nil)
  13981. (defun org-self-insert-command (N)
  13982. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13983. If the cursor is in a table looking at whitespace, the whitespace is
  13984. overwritten, and the table is not marked as requiring realignment."
  13985. (interactive "p")
  13986. (cond
  13987. ((and org-use-speed-commands
  13988. (or (and (bolp) (looking-at outline-regexp))
  13989. (and (functionp org-use-speed-commands)
  13990. (funcall org-use-speed-commands)))
  13991. (setq
  13992. org-speed-command
  13993. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  13994. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  13995. (cond
  13996. ((commandp org-speed-command)
  13997. (setq this-command org-speed-command)
  13998. (call-interactively org-speed-command))
  13999. ((functionp org-speed-command)
  14000. (funcall org-speed-command))
  14001. ((and org-speed-command (listp org-speed-command))
  14002. (eval org-speed-command))
  14003. (t (let (org-use-speed-commands)
  14004. (call-interactively 'org-self-insert-command)))))
  14005. ((and
  14006. (org-table-p)
  14007. (progn
  14008. ;; check if we blank the field, and if that triggers align
  14009. (and (featurep 'org-table) org-table-auto-blank-field
  14010. (member last-command
  14011. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14012. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14013. ;; got extra space, this field does not determine column width
  14014. (let (org-table-may-need-update) (org-table-blank-field))
  14015. ;; no extra space, this field may determine column width
  14016. (org-table-blank-field)))
  14017. t)
  14018. (eq N 1)
  14019. (looking-at "[^|\n]* |"))
  14020. (let (org-table-may-need-update)
  14021. (goto-char (1- (match-end 0)))
  14022. (delete-backward-char 1)
  14023. (goto-char (match-beginning 0))
  14024. (self-insert-command N)))
  14025. (t
  14026. (setq org-table-may-need-update t)
  14027. (self-insert-command N)
  14028. (org-fix-tags-on-the-fly)
  14029. (if org-self-insert-cluster-for-undo
  14030. (if (not (eq last-command 'org-self-insert-command))
  14031. (setq org-self-insert-command-undo-counter 1)
  14032. (if (>= org-self-insert-command-undo-counter 20)
  14033. (setq org-self-insert-command-undo-counter 1)
  14034. (and (> org-self-insert-command-undo-counter 0)
  14035. buffer-undo-list
  14036. (not (cadr buffer-undo-list)) ; remove nil entry
  14037. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14038. (setq org-self-insert-command-undo-counter
  14039. (1+ org-self-insert-command-undo-counter))))))))
  14040. (defun org-fix-tags-on-the-fly ()
  14041. (when (and (equal (char-after (point-at-bol)) ?*)
  14042. (org-on-heading-p))
  14043. (org-align-tags-here org-tags-column)))
  14044. (defun org-delete-backward-char (N)
  14045. "Like `delete-backward-char', insert whitespace at field end in tables.
  14046. When deleting backwards, in tables this function will insert whitespace in
  14047. front of the next \"|\" separator, to keep the table aligned. The table will
  14048. still be marked for re-alignment if the field did fill the entire column,
  14049. because, in this case the deletion might narrow the column."
  14050. (interactive "p")
  14051. (if (and (org-table-p)
  14052. (eq N 1)
  14053. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14054. (looking-at ".*?|"))
  14055. (let ((pos (point))
  14056. (noalign (looking-at "[^|\n\r]* |"))
  14057. (c org-table-may-need-update))
  14058. (backward-delete-char N)
  14059. (skip-chars-forward "^|")
  14060. (insert " ")
  14061. (goto-char (1- pos))
  14062. ;; noalign: if there were two spaces at the end, this field
  14063. ;; does not determine the width of the column.
  14064. (if noalign (setq org-table-may-need-update c)))
  14065. (backward-delete-char N)
  14066. (org-fix-tags-on-the-fly)))
  14067. (defun org-delete-char (N)
  14068. "Like `delete-char', but insert whitespace at field end in tables.
  14069. When deleting characters, in tables this function will insert whitespace in
  14070. front of the next \"|\" separator, to keep the table aligned. The table will
  14071. still be marked for re-alignment if the field did fill the entire column,
  14072. because, in this case the deletion might narrow the column."
  14073. (interactive "p")
  14074. (if (and (org-table-p)
  14075. (not (bolp))
  14076. (not (= (char-after) ?|))
  14077. (eq N 1))
  14078. (if (looking-at ".*?|")
  14079. (let ((pos (point))
  14080. (noalign (looking-at "[^|\n\r]* |"))
  14081. (c org-table-may-need-update))
  14082. (replace-match (concat
  14083. (substring (match-string 0) 1 -1)
  14084. " |"))
  14085. (goto-char pos)
  14086. ;; noalign: if there were two spaces at the end, this field
  14087. ;; does not determine the width of the column.
  14088. (if noalign (setq org-table-may-need-update c)))
  14089. (delete-char N))
  14090. (delete-char N)
  14091. (org-fix-tags-on-the-fly)))
  14092. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14093. (put 'org-self-insert-command 'delete-selection t)
  14094. (put 'orgtbl-self-insert-command 'delete-selection t)
  14095. (put 'org-delete-char 'delete-selection 'supersede)
  14096. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14097. (put 'org-yank 'delete-selection 'yank)
  14098. ;; Make `flyspell-mode' delay after some commands
  14099. (put 'org-self-insert-command 'flyspell-delayed t)
  14100. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14101. (put 'org-delete-char 'flyspell-delayed t)
  14102. (put 'org-delete-backward-char 'flyspell-delayed t)
  14103. ;; Make pabbrev-mode expand after org-mode commands
  14104. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14105. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14106. ;; How to do this: Measure non-white length of current string
  14107. ;; If equal to column width, we should realign.
  14108. (defun org-remap (map &rest commands)
  14109. "In MAP, remap the functions given in COMMANDS.
  14110. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14111. (let (new old)
  14112. (while commands
  14113. (setq old (pop commands) new (pop commands))
  14114. (if (fboundp 'command-remapping)
  14115. (org-defkey map (vector 'remap old) new)
  14116. (substitute-key-definition old new map global-map)))))
  14117. (when (eq org-enable-table-editor 'optimized)
  14118. ;; If the user wants maximum table support, we need to hijack
  14119. ;; some standard editing functions
  14120. (org-remap org-mode-map
  14121. 'self-insert-command 'org-self-insert-command
  14122. 'delete-char 'org-delete-char
  14123. 'delete-backward-char 'org-delete-backward-char)
  14124. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14125. (defvar org-ctrl-c-ctrl-c-hook nil
  14126. "Hook for functions attaching themselves to `C-c C-c'.
  14127. This can be used to add additional functionality to the C-c C-c key which
  14128. executes context-dependent commands.
  14129. Each function will be called with no arguments. The function must check
  14130. if the context is appropriate for it to act. If yes, it should do its
  14131. thing and then return a non-nil value. If the context is wrong,
  14132. just do nothing and return nil.")
  14133. (defvar org-tab-first-hook nil
  14134. "Hook for functions to attach themselves to TAB.
  14135. See `org-ctrl-c-ctrl-c-hook' for more information.
  14136. This hook runs as the first action when TAB is pressed, even before
  14137. `org-cycle' messes around with the `outline-regexp' to cater for
  14138. inline tasks and plain list item folding.
  14139. If any function in this hook returns t, not other actions like table
  14140. field motion visibility cycling will be done.")
  14141. (defvar org-tab-after-check-for-table-hook nil
  14142. "Hook for functions to attach themselves to TAB.
  14143. See `org-ctrl-c-ctrl-c-hook' for more information.
  14144. This hook runs after it has been established that the cursor is not in a
  14145. table, but before checking if the cursor is in a headline or if global cycling
  14146. should be done.
  14147. If any function in this hook returns t, not other actions like visibility
  14148. cycling will be done.")
  14149. (defvar org-tab-after-check-for-cycling-hook nil
  14150. "Hook for functions to attach themselves to TAB.
  14151. See `org-ctrl-c-ctrl-c-hook' for more information.
  14152. This hook runs after it has been established that not table field motion and
  14153. not visibility should be done because of current context. This is probably
  14154. the place where a package like yasnippets can hook in.")
  14155. (defvar org-tab-before-tab-emulation-hook nil
  14156. "Hook for functions to attach themselves to TAB.
  14157. See `org-ctrl-c-ctrl-c-hook' for more information.
  14158. This hook runs after every other options for TAB have been exhausted, but
  14159. before indentation and \t insertion takes place.")
  14160. (defvar org-metaleft-hook nil
  14161. "Hook for functions attaching themselves to `M-left'.
  14162. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14163. (defvar org-metaright-hook nil
  14164. "Hook for functions attaching themselves to `M-right'.
  14165. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14166. (defvar org-metaup-hook nil
  14167. "Hook for functions attaching themselves to `M-up'.
  14168. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14169. (defvar org-metadown-hook nil
  14170. "Hook for functions attaching themselves to `M-down'.
  14171. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14172. (defvar org-shiftmetaleft-hook nil
  14173. "Hook for functions attaching themselves to `M-S-left'.
  14174. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14175. (defvar org-shiftmetaright-hook nil
  14176. "Hook for functions attaching themselves to `M-S-right'.
  14177. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14178. (defvar org-shiftmetaup-hook nil
  14179. "Hook for functions attaching themselves to `M-S-up'.
  14180. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14181. (defvar org-shiftmetadown-hook nil
  14182. "Hook for functions attaching themselves to `M-S-down'.
  14183. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14184. (defvar org-metareturn-hook nil
  14185. "Hook for functions attaching themselves to `M-RET'.
  14186. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14187. (defun org-modifier-cursor-error ()
  14188. "Throw an error, a modified cursor command was applied in wrong context."
  14189. (error "This command is active in special context like tables, headlines or items"))
  14190. (defun org-shiftselect-error ()
  14191. "Throw an error because Shift-Cursor command was applied in wrong context."
  14192. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14193. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14194. (error "This command works only in special context like headlines or timestamps")))
  14195. (defun org-call-for-shift-select (cmd)
  14196. (let ((this-command-keys-shift-translated t))
  14197. (call-interactively cmd)))
  14198. (defun org-shifttab (&optional arg)
  14199. "Global visibility cycling or move to previous table field.
  14200. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14201. on context.
  14202. See the individual commands for more information."
  14203. (interactive "P")
  14204. (cond
  14205. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14206. ((integerp arg)
  14207. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14208. (message "Content view to level: %d" arg)
  14209. (org-content (prefix-numeric-value arg2))
  14210. (setq org-cycle-global-status 'overview)))
  14211. (t (call-interactively 'org-global-cycle))))
  14212. (defun org-shiftmetaleft ()
  14213. "Promote subtree or delete table column.
  14214. Calls `org-promote-subtree', `org-outdent-item',
  14215. or `org-table-delete-column', depending on context.
  14216. See the individual commands for more information."
  14217. (interactive)
  14218. (cond
  14219. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14220. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14221. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14222. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14223. (t (org-modifier-cursor-error))))
  14224. (defun org-shiftmetaright ()
  14225. "Demote subtree or insert table column.
  14226. Calls `org-demote-subtree', `org-indent-item',
  14227. or `org-table-insert-column', depending on context.
  14228. See the individual commands for more information."
  14229. (interactive)
  14230. (cond
  14231. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14232. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14233. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14234. ((org-at-item-p) (call-interactively 'org-indent-item))
  14235. (t (org-modifier-cursor-error))))
  14236. (defun org-shiftmetaup (&optional arg)
  14237. "Move subtree up or kill table row.
  14238. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14239. `org-move-item-up' depending on context. See the individual commands
  14240. for more information."
  14241. (interactive "P")
  14242. (cond
  14243. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14244. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14245. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14246. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14247. (t (org-modifier-cursor-error))))
  14248. (defun org-shiftmetadown (&optional arg)
  14249. "Move subtree down or insert table row.
  14250. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14251. `org-move-item-down', depending on context. See the individual
  14252. commands for more information."
  14253. (interactive "P")
  14254. (cond
  14255. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14256. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14257. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14258. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14259. (t (org-modifier-cursor-error))))
  14260. (defun org-metaleft (&optional arg)
  14261. "Promote heading or move table column to left.
  14262. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14263. With no specific context, calls the Emacs default `backward-word'.
  14264. See the individual commands for more information."
  14265. (interactive "P")
  14266. (cond
  14267. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14268. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14269. ((or (org-on-heading-p)
  14270. (and (org-region-active-p)
  14271. (save-excursion
  14272. (goto-char (region-beginning))
  14273. (org-on-heading-p))))
  14274. (call-interactively 'org-do-promote))
  14275. ((or (org-at-item-p)
  14276. (and (org-region-active-p)
  14277. (save-excursion
  14278. (goto-char (region-beginning))
  14279. (org-at-item-p))))
  14280. (call-interactively 'org-outdent-item))
  14281. (t (call-interactively 'backward-word))))
  14282. (defun org-metaright (&optional arg)
  14283. "Demote subtree or move table column to right.
  14284. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14285. With no specific context, calls the Emacs default `forward-word'.
  14286. See the individual commands for more information."
  14287. (interactive "P")
  14288. (cond
  14289. ((run-hook-with-args-until-success 'org-metaright-hook))
  14290. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14291. ((or (org-on-heading-p)
  14292. (and (org-region-active-p)
  14293. (save-excursion
  14294. (goto-char (region-beginning))
  14295. (org-on-heading-p))))
  14296. (call-interactively 'org-do-demote))
  14297. ((or (org-at-item-p)
  14298. (and (org-region-active-p)
  14299. (save-excursion
  14300. (goto-char (region-beginning))
  14301. (org-at-item-p))))
  14302. (call-interactively 'org-indent-item))
  14303. (t (call-interactively 'forward-word))))
  14304. (defun org-metaup (&optional arg)
  14305. "Move subtree up or move table row up.
  14306. Calls `org-move-subtree-up' or `org-table-move-row' or
  14307. `org-move-item-up', depending on context. See the individual commands
  14308. for more information."
  14309. (interactive "P")
  14310. (cond
  14311. ((run-hook-with-args-until-success 'org-metaup-hook))
  14312. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14313. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14314. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14315. (t (transpose-lines 1) (beginning-of-line -1))))
  14316. (defun org-metadown (&optional arg)
  14317. "Move subtree down or move table row down.
  14318. Calls `org-move-subtree-down' or `org-table-move-row' or
  14319. `org-move-item-down', depending on context. See the individual
  14320. commands for more information."
  14321. (interactive "P")
  14322. (cond
  14323. ((run-hook-with-args-until-success 'org-metadown-hook))
  14324. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14325. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14326. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14327. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14328. (defun org-shiftup (&optional arg)
  14329. "Increase item in timestamp or increase priority of current headline.
  14330. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14331. depending on context. See the individual commands for more information."
  14332. (interactive "P")
  14333. (cond
  14334. ((and org-support-shift-select (org-region-active-p))
  14335. (org-call-for-shift-select 'previous-line))
  14336. ((org-at-timestamp-p t)
  14337. (call-interactively (if org-edit-timestamp-down-means-later
  14338. 'org-timestamp-down 'org-timestamp-up)))
  14339. ((and (not (eq org-support-shift-select 'always))
  14340. org-enable-priority-commands
  14341. (org-on-heading-p))
  14342. (call-interactively 'org-priority-up))
  14343. ((and (not org-support-shift-select) (org-at-item-p))
  14344. (call-interactively 'org-previous-item))
  14345. ((org-clocktable-try-shift 'up arg))
  14346. (org-support-shift-select
  14347. (org-call-for-shift-select 'previous-line))
  14348. (t (org-shiftselect-error))))
  14349. (defun org-shiftdown (&optional arg)
  14350. "Decrease item in timestamp or decrease priority of current headline.
  14351. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14352. depending on context. See the individual commands for more information."
  14353. (interactive "P")
  14354. (cond
  14355. ((and org-support-shift-select (org-region-active-p))
  14356. (org-call-for-shift-select 'next-line))
  14357. ((org-at-timestamp-p t)
  14358. (call-interactively (if org-edit-timestamp-down-means-later
  14359. 'org-timestamp-up 'org-timestamp-down)))
  14360. ((and (not (eq org-support-shift-select 'always))
  14361. org-enable-priority-commands
  14362. (org-on-heading-p))
  14363. (call-interactively 'org-priority-down))
  14364. ((and (not org-support-shift-select) (org-at-item-p))
  14365. (call-interactively 'org-next-item))
  14366. ((org-clocktable-try-shift 'down arg))
  14367. (org-support-shift-select
  14368. (org-call-for-shift-select 'next-line))
  14369. (t (org-shiftselect-error))))
  14370. (defun org-shiftright (&optional arg)
  14371. "Cycle the thing at point or in the current line, depending on context.
  14372. Depending on context, this does one of the following:
  14373. - switch a timestamp at point one day into the future
  14374. - on a headline, switch to the next TODO keyword.
  14375. - on an item, switch entire list to the next bullet type
  14376. - on a property line, switch to the next allowed value
  14377. - on a clocktable definition line, move time block into the future"
  14378. (interactive "P")
  14379. (cond
  14380. ((and org-support-shift-select (org-region-active-p))
  14381. (org-call-for-shift-select 'forward-char))
  14382. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14383. ((and (not (eq org-support-shift-select 'always))
  14384. (org-on-heading-p))
  14385. (let ((org-inhibit-logging
  14386. (not org-treat-S-cursor-todo-selection-as-state-change))
  14387. (org-inhibit-blocking
  14388. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14389. (org-call-with-arg 'org-todo 'right)))
  14390. ((or (and org-support-shift-select
  14391. (not (eq org-support-shift-select 'always))
  14392. (org-at-item-bullet-p))
  14393. (and (not org-support-shift-select) (org-at-item-p)))
  14394. (org-call-with-arg 'org-cycle-list-bullet nil))
  14395. ((and (not (eq org-support-shift-select 'always))
  14396. (org-at-property-p))
  14397. (call-interactively 'org-property-next-allowed-value))
  14398. ((org-clocktable-try-shift 'right arg))
  14399. (org-support-shift-select
  14400. (org-call-for-shift-select 'forward-char))
  14401. (t (org-shiftselect-error))))
  14402. (defun org-shiftleft (&optional arg)
  14403. "Cycle the thing at point or in the current line, depending on context.
  14404. Depending on context, this does one of the following:
  14405. - switch a timestamp at point one day into the past
  14406. - on a headline, switch to the previous TODO keyword.
  14407. - on an item, switch entire list to the previous bullet type
  14408. - on a property line, switch to the previous allowed value
  14409. - on a clocktable definition line, move time block into the past"
  14410. (interactive "P")
  14411. (cond
  14412. ((and org-support-shift-select (org-region-active-p))
  14413. (org-call-for-shift-select 'backward-char))
  14414. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14415. ((and (not (eq org-support-shift-select 'always))
  14416. (org-on-heading-p))
  14417. (let ((org-inhibit-logging
  14418. (not org-treat-S-cursor-todo-selection-as-state-change))
  14419. (org-inhibit-blocking
  14420. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14421. (org-call-with-arg 'org-todo 'left)))
  14422. ((or (and org-support-shift-select
  14423. (not (eq org-support-shift-select 'always))
  14424. (org-at-item-bullet-p))
  14425. (and (not org-support-shift-select) (org-at-item-p)))
  14426. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14427. ((and (not (eq org-support-shift-select 'always))
  14428. (org-at-property-p))
  14429. (call-interactively 'org-property-previous-allowed-value))
  14430. ((org-clocktable-try-shift 'left arg))
  14431. (org-support-shift-select
  14432. (org-call-for-shift-select 'backward-char))
  14433. (t (org-shiftselect-error))))
  14434. (defun org-shiftcontrolright ()
  14435. "Switch to next TODO set."
  14436. (interactive)
  14437. (cond
  14438. ((and org-support-shift-select (org-region-active-p))
  14439. (org-call-for-shift-select 'forward-word))
  14440. ((and (not (eq org-support-shift-select 'always))
  14441. (org-on-heading-p))
  14442. (org-call-with-arg 'org-todo 'nextset))
  14443. (org-support-shift-select
  14444. (org-call-for-shift-select 'forward-word))
  14445. (t (org-shiftselect-error))))
  14446. (defun org-shiftcontrolleft ()
  14447. "Switch to previous TODO set."
  14448. (interactive)
  14449. (cond
  14450. ((and org-support-shift-select (org-region-active-p))
  14451. (org-call-for-shift-select 'backward-word))
  14452. ((and (not (eq org-support-shift-select 'always))
  14453. (org-on-heading-p))
  14454. (org-call-with-arg 'org-todo 'previousset))
  14455. (org-support-shift-select
  14456. (org-call-for-shift-select 'backward-word))
  14457. (t (org-shiftselect-error))))
  14458. (defun org-ctrl-c-ret ()
  14459. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14460. (interactive)
  14461. (cond
  14462. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14463. (t (call-interactively 'org-insert-heading))))
  14464. (defun org-copy-special ()
  14465. "Copy region in table or copy current subtree.
  14466. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14467. See the individual commands for more information."
  14468. (interactive)
  14469. (call-interactively
  14470. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14471. (defun org-cut-special ()
  14472. "Cut region in table or cut current subtree.
  14473. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14474. See the individual commands for more information."
  14475. (interactive)
  14476. (call-interactively
  14477. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14478. (defun org-paste-special (arg)
  14479. "Paste rectangular region into table, or past subtree relative to level.
  14480. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14481. See the individual commands for more information."
  14482. (interactive "P")
  14483. (if (org-at-table-p)
  14484. (org-table-paste-rectangle)
  14485. (org-paste-subtree arg)))
  14486. (defun org-edit-special ()
  14487. "Call a special editor for the stuff at point.
  14488. When at a table, call the formula editor with `org-table-edit-formulas'.
  14489. When at the first line of an src example, call `org-edit-src-code'.
  14490. When in an #+include line, visit the include file. Otherwise call
  14491. `ffap' to visit the file at point."
  14492. (interactive)
  14493. (cond
  14494. ((org-at-table-p)
  14495. (call-interactively 'org-table-edit-formulas))
  14496. ((save-excursion
  14497. (beginning-of-line 1)
  14498. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14499. (find-file (org-trim (match-string 1))))
  14500. ((org-edit-src-code))
  14501. ((org-edit-fixed-width-region))
  14502. (t (call-interactively 'ffap))))
  14503. (defun org-ctrl-c-ctrl-c (&optional arg)
  14504. "Set tags in headline, or update according to changed information at point.
  14505. This command does many different things, depending on context:
  14506. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14507. this is what we do.
  14508. - If the cursor is on a statistics cookie, update it.
  14509. - If the cursor is in a headline, prompt for tags and insert them
  14510. into the current line, aligned to `org-tags-column'. When called
  14511. with prefix arg, realign all tags in the current buffer.
  14512. - If the cursor is in one of the special #+KEYWORD lines, this
  14513. triggers scanning the buffer for these lines and updating the
  14514. information.
  14515. - If the cursor is inside a table, realign the table. This command
  14516. works even if the automatic table editor has been turned off.
  14517. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14518. the entire table.
  14519. - If the cursor is at a footnote reference or definition, jump to
  14520. the corresponding definition or references, respectively.
  14521. - If the cursor is a the beginning of a dynamic block, update it.
  14522. - If the cursor is inside a table created by the table.el package,
  14523. activate that table.
  14524. - If the current buffer is a remember buffer, close note and file
  14525. it. A prefix argument of 1 files to the default location
  14526. without further interaction. A prefix argument of 2 files to
  14527. the currently clocking task.
  14528. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14529. links in this buffer.
  14530. - If the cursor is on a numbered item in a plain list, renumber the
  14531. ordered list.
  14532. - If the cursor is on a checkbox, toggle it."
  14533. (interactive "P")
  14534. (let ((org-enable-table-editor t))
  14535. (cond
  14536. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14537. org-occur-highlights
  14538. org-latex-fragment-image-overlays)
  14539. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14540. (org-remove-occur-highlights)
  14541. (org-remove-latex-fragment-image-overlays)
  14542. (message "Temporary highlights/overlays removed from current buffer"))
  14543. ((and (local-variable-p 'org-finish-function (current-buffer))
  14544. (fboundp org-finish-function))
  14545. (funcall org-finish-function))
  14546. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14547. ((org-at-property-p)
  14548. (call-interactively 'org-property-action))
  14549. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14550. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14551. (or (org-on-heading-p) (org-at-item-p)))
  14552. (call-interactively 'org-update-statistics-cookies))
  14553. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14554. ((org-at-table.el-p)
  14555. (require 'table)
  14556. (beginning-of-line 1)
  14557. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14558. (call-interactively 'table-recognize-table))
  14559. ((org-at-table-p)
  14560. (org-table-maybe-eval-formula)
  14561. (if arg
  14562. (call-interactively 'org-table-recalculate)
  14563. (org-table-maybe-recalculate-line))
  14564. (call-interactively 'org-table-align))
  14565. ((or (org-footnote-at-reference-p)
  14566. (org-footnote-at-definition-p))
  14567. (call-interactively 'org-footnote-action))
  14568. ((org-at-item-checkbox-p)
  14569. (call-interactively 'org-toggle-checkbox))
  14570. ((org-at-item-p)
  14571. (if arg
  14572. (call-interactively 'org-toggle-checkbox)
  14573. (call-interactively 'org-maybe-renumber-ordered-list)))
  14574. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14575. ;; Dynamic block
  14576. (beginning-of-line 1)
  14577. (save-excursion (org-update-dblock)))
  14578. ((save-excursion
  14579. (beginning-of-line 1)
  14580. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14581. (cond
  14582. ((equal (match-string 1) "TBLFM")
  14583. ;; Recalculate the table before this line
  14584. (save-excursion
  14585. (beginning-of-line 1)
  14586. (skip-chars-backward " \r\n\t")
  14587. (if (org-at-table-p)
  14588. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14589. (t
  14590. (let ((org-inhibit-startup-visibility-stuff t)
  14591. (org-startup-align-all-tables nil))
  14592. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  14593. (message "Local setup has been refreshed"))))
  14594. ((org-clock-update-time-maybe))
  14595. (t (error "C-c C-c can do nothing useful at this location")))))
  14596. (defun org-mode-restart ()
  14597. "Restart Org-mode, to scan again for special lines.
  14598. Also updates the keyword regular expressions."
  14599. (interactive)
  14600. (org-mode)
  14601. (message "Org-mode restarted"))
  14602. (defun org-kill-note-or-show-branches ()
  14603. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14604. (interactive)
  14605. (if (not org-finish-function)
  14606. (call-interactively 'show-branches)
  14607. (let ((org-note-abort t))
  14608. (funcall org-finish-function))))
  14609. (defun org-return (&optional indent)
  14610. "Goto next table row or insert a newline.
  14611. Calls `org-table-next-row' or `newline', depending on context.
  14612. See the individual commands for more information."
  14613. (interactive)
  14614. (cond
  14615. ((bobp) (if indent (newline-and-indent) (newline)))
  14616. ((org-at-table-p)
  14617. (org-table-justify-field-maybe)
  14618. (call-interactively 'org-table-next-row))
  14619. ((and org-return-follows-link
  14620. (eq (get-text-property (point) 'face) 'org-link))
  14621. (call-interactively 'org-open-at-point))
  14622. ((and (org-at-heading-p)
  14623. (looking-at
  14624. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14625. (org-show-entry)
  14626. (end-of-line 1)
  14627. (newline))
  14628. (t (if indent (newline-and-indent) (newline)))))
  14629. (defun org-return-indent ()
  14630. "Goto next table row or insert a newline and indent.
  14631. Calls `org-table-next-row' or `newline-and-indent', depending on
  14632. context. See the individual commands for more information."
  14633. (interactive)
  14634. (org-return t))
  14635. (defun org-ctrl-c-star ()
  14636. "Compute table, or change heading status of lines.
  14637. Calls `org-table-recalculate' or `org-toggle-heading',
  14638. depending on context."
  14639. (interactive)
  14640. (cond
  14641. ((org-at-table-p)
  14642. (call-interactively 'org-table-recalculate))
  14643. (t
  14644. ;; Convert all lines in region to list items
  14645. (call-interactively 'org-toggle-heading))))
  14646. (defun org-ctrl-c-minus ()
  14647. "Insert separator line in table or modify bullet status of line.
  14648. Also turns a plain line or a region of lines into list items.
  14649. Calls `org-table-insert-hline', `org-toggle-item', or
  14650. `org-cycle-list-bullet', depending on context."
  14651. (interactive)
  14652. (cond
  14653. ((org-at-table-p)
  14654. (call-interactively 'org-table-insert-hline))
  14655. ((org-region-active-p)
  14656. (call-interactively 'org-toggle-item))
  14657. ((org-in-item-p)
  14658. (call-interactively 'org-cycle-list-bullet))
  14659. (t
  14660. (call-interactively 'org-toggle-item))))
  14661. (defun org-toggle-item ()
  14662. "Convert headings or normal lines to items, items to normal lines.
  14663. If there is no active region, only the current line is considered.
  14664. If the first line in the region is a headline, convert all headlines to items.
  14665. If the first line in the region is an item, convert all items to normal lines.
  14666. If the first line is normal text, add an item bullet to each line."
  14667. (interactive)
  14668. (let (l2 l beg end)
  14669. (if (org-region-active-p)
  14670. (setq beg (region-beginning) end (region-end))
  14671. (setq beg (point-at-bol)
  14672. end (min (1+ (point-at-eol)) (point-max))))
  14673. (save-excursion
  14674. (goto-char end)
  14675. (setq l2 (org-current-line))
  14676. (goto-char beg)
  14677. (beginning-of-line 1)
  14678. (setq l (1- (org-current-line)))
  14679. (if (org-at-item-p)
  14680. ;; We already have items, de-itemize
  14681. (while (< (setq l (1+ l)) l2)
  14682. (when (org-at-item-p)
  14683. (goto-char (match-beginning 2))
  14684. (delete-region (match-beginning 2) (match-end 2))
  14685. (and (looking-at "[ \t]+") (replace-match "")))
  14686. (beginning-of-line 2))
  14687. (if (org-on-heading-p)
  14688. ;; Headings, convert to items
  14689. (while (< (setq l (1+ l)) l2)
  14690. (if (looking-at org-outline-regexp)
  14691. (replace-match "- " t t))
  14692. (beginning-of-line 2))
  14693. ;; normal lines, turn them into items
  14694. (while (< (setq l (1+ l)) l2)
  14695. (unless (org-at-item-p)
  14696. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14697. (replace-match "\\1- \\2")))
  14698. (beginning-of-line 2)))))))
  14699. (defun org-toggle-heading (&optional nstars)
  14700. "Convert headings to normal text, or items or text to headings.
  14701. If there is no active region, only the current line is considered.
  14702. If the first line is a heading, remove the stars from all headlines
  14703. in the region.
  14704. If the first line is a plain list item, turn all plain list items
  14705. into headings.
  14706. If the first line is a normal line, turn each and every line in the
  14707. region into a heading.
  14708. When converting a line into a heading, the number of stars is chosen
  14709. such that the lines become children of the current entry. However,
  14710. when a prefix argument is given, its value determines the number of
  14711. stars to add."
  14712. (interactive "P")
  14713. (let (l2 l itemp beg end)
  14714. (if (org-region-active-p)
  14715. (setq beg (region-beginning) end (region-end))
  14716. (setq beg (point-at-bol)
  14717. end (min (1+ (point-at-eol)) (point-max))))
  14718. (save-excursion
  14719. (goto-char end)
  14720. (setq l2 (org-current-line))
  14721. (goto-char beg)
  14722. (beginning-of-line 1)
  14723. (setq l (1- (org-current-line)))
  14724. (if (org-on-heading-p)
  14725. ;; We already have headlines, de-star them
  14726. (while (< (setq l (1+ l)) l2)
  14727. (when (org-on-heading-p t)
  14728. (and (looking-at outline-regexp) (replace-match "")))
  14729. (beginning-of-line 2))
  14730. (setq itemp (org-at-item-p))
  14731. (let* ((stars
  14732. (if nstars
  14733. (make-string (prefix-numeric-value current-prefix-arg)
  14734. ?*)
  14735. (save-excursion
  14736. (if (re-search-backward org-complex-heading-regexp nil t)
  14737. (match-string 1) ""))))
  14738. (add-stars (cond (nstars "")
  14739. ((equal stars "") "*")
  14740. (org-odd-levels-only "**")
  14741. (t "*")))
  14742. (rpl (concat stars add-stars " ")))
  14743. (while (< (setq l (1+ l)) l2)
  14744. (if itemp
  14745. (and (org-at-item-p) (replace-match rpl t t))
  14746. (unless (org-on-heading-p)
  14747. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  14748. (replace-match (concat rpl (match-string 2))))))
  14749. (beginning-of-line 2)))))))
  14750. (defun org-meta-return (&optional arg)
  14751. "Insert a new heading or wrap a region in a table.
  14752. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14753. See the individual commands for more information."
  14754. (interactive "P")
  14755. (cond
  14756. ((run-hook-with-args-until-success 'org-metareturn-hook))
  14757. ((org-at-table-p)
  14758. (call-interactively 'org-table-wrap-region))
  14759. (t (call-interactively 'org-insert-heading))))
  14760. ;;; Menu entries
  14761. ;; Define the Org-mode menus
  14762. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14763. '("Tbl"
  14764. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  14765. ["Next Field" org-cycle (org-at-table-p)]
  14766. ["Previous Field" org-shifttab (org-at-table-p)]
  14767. ["Next Row" org-return (org-at-table-p)]
  14768. "--"
  14769. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14770. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14771. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14772. "--"
  14773. ("Column"
  14774. ["Move Column Left" org-metaleft (org-at-table-p)]
  14775. ["Move Column Right" org-metaright (org-at-table-p)]
  14776. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14777. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  14778. ("Row"
  14779. ["Move Row Up" org-metaup (org-at-table-p)]
  14780. ["Move Row Down" org-metadown (org-at-table-p)]
  14781. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14782. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14783. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14784. "--"
  14785. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  14786. ("Rectangle"
  14787. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14788. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14789. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14790. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14791. "--"
  14792. ("Calculate"
  14793. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14794. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14795. ["Edit Formulas" org-edit-special (org-at-table-p)]
  14796. "--"
  14797. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14798. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14799. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  14800. "--"
  14801. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14802. "--"
  14803. ["Sum Column/Rectangle" org-table-sum
  14804. (or (org-at-table-p) (org-region-active-p))]
  14805. ["Which Column?" org-table-current-column (org-at-table-p)])
  14806. ["Debug Formulas"
  14807. org-table-toggle-formula-debugger
  14808. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  14809. ["Show Col/Row Numbers"
  14810. org-table-toggle-coordinate-overlays
  14811. :style toggle
  14812. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14813. "--"
  14814. ["Create" org-table-create (and (not (org-at-table-p))
  14815. org-enable-table-editor)]
  14816. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14817. ["Import from File" org-table-import (not (org-at-table-p))]
  14818. ["Export to File" org-table-export (org-at-table-p)]
  14819. "--"
  14820. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14821. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14822. '("Org"
  14823. ("Show/Hide"
  14824. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14825. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14826. ["Sparse Tree..." org-sparse-tree t]
  14827. ["Reveal Context" org-reveal t]
  14828. ["Show All" show-all t]
  14829. "--"
  14830. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14831. "--"
  14832. ["New Heading" org-insert-heading t]
  14833. ("Navigate Headings"
  14834. ["Up" outline-up-heading t]
  14835. ["Next" outline-next-visible-heading t]
  14836. ["Previous" outline-previous-visible-heading t]
  14837. ["Next Same Level" outline-forward-same-level t]
  14838. ["Previous Same Level" outline-backward-same-level t]
  14839. "--"
  14840. ["Jump" org-goto t])
  14841. ("Edit Structure"
  14842. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14843. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14844. "--"
  14845. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14846. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14847. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14848. "--"
  14849. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14850. "--"
  14851. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14852. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14853. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14854. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14855. "--"
  14856. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14857. "--"
  14858. ["Convert to odd levels" org-convert-to-odd-levels t]
  14859. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14860. ("Editing"
  14861. ["Emphasis..." org-emphasize t]
  14862. ["Edit Source Example" org-edit-special t]
  14863. "--"
  14864. ["Footnote new/jump" org-footnote-action t]
  14865. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14866. ("Archive"
  14867. ["Archive (default method)" org-archive-subtree-default t]
  14868. "--"
  14869. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  14870. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14871. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  14872. )
  14873. "--"
  14874. ("Hyperlinks"
  14875. ["Store Link (Global)" org-store-link t]
  14876. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14877. ["Insert Link" org-insert-link t]
  14878. ["Follow Link" org-open-at-point t]
  14879. "--"
  14880. ["Next link" org-next-link t]
  14881. ["Previous link" org-previous-link t]
  14882. "--"
  14883. ["Descriptive Links"
  14884. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14885. :style radio
  14886. :selected (member '(org-link) buffer-invisibility-spec)]
  14887. ["Literal Links"
  14888. (progn
  14889. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14890. :style radio
  14891. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14892. "--"
  14893. ("TODO Lists"
  14894. ["TODO/DONE/-" org-todo t]
  14895. ("Select keyword"
  14896. ["Next keyword" org-shiftright (org-on-heading-p)]
  14897. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14898. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14899. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14900. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14901. ["Show TODO Tree" org-show-todo-tree t]
  14902. ["Global TODO list" org-todo-list t]
  14903. "--"
  14904. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14905. :selected org-enforce-todo-dependencies :style toggle :active t]
  14906. "Settings for tree at point"
  14907. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14908. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14909. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14910. ["Do Children parallel" org-toggle-ordered-property :style radio
  14911. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14912. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14913. "--"
  14914. ["Set Priority" org-priority t]
  14915. ["Priority Up" org-shiftup t]
  14916. ["Priority Down" org-shiftdown t]
  14917. "--"
  14918. ["Get news from all feeds" org-feed-update-all t]
  14919. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14920. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14921. ("TAGS and Properties"
  14922. ["Set Tags" org-set-tags-command t]
  14923. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14924. "--"
  14925. ["Set property" org-set-property t]
  14926. ["Column view of properties" org-columns t]
  14927. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14928. ("Dates and Scheduling"
  14929. ["Timestamp" org-time-stamp t]
  14930. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14931. ("Change Date"
  14932. ["1 Day Later" org-shiftright t]
  14933. ["1 Day Earlier" org-shiftleft t]
  14934. ["1 ... Later" org-shiftup t]
  14935. ["1 ... Earlier" org-shiftdown t])
  14936. ["Compute Time Range" org-evaluate-time-range t]
  14937. ["Schedule Item" org-schedule t]
  14938. ["Deadline" org-deadline t]
  14939. "--"
  14940. ["Custom time format" org-toggle-time-stamp-overlays
  14941. :style radio :selected org-display-custom-times]
  14942. "--"
  14943. ["Goto Calendar" org-goto-calendar t]
  14944. ["Date from Calendar" org-date-from-calendar t]
  14945. "--"
  14946. ["Start/Restart Timer" org-timer-start t]
  14947. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14948. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14949. ["Insert Timer String" org-timer t]
  14950. ["Insert Timer Item" org-timer-item t])
  14951. ("Logging work"
  14952. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14953. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14954. ["Clock out" org-clock-out t]
  14955. ["Clock cancel" org-clock-cancel t]
  14956. "--"
  14957. ["Mark as default task" org-clock-mark-default-task t]
  14958. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14959. ["Goto running clock" org-clock-goto t]
  14960. "--"
  14961. ["Display times" org-clock-display t]
  14962. ["Create clock table" org-clock-report t]
  14963. "--"
  14964. ["Record DONE time"
  14965. (progn (setq org-log-done (not org-log-done))
  14966. (message "Switching to %s will %s record a timestamp"
  14967. (car org-done-keywords)
  14968. (if org-log-done "automatically" "not")))
  14969. :style toggle :selected org-log-done])
  14970. "--"
  14971. ["Agenda Command..." org-agenda t]
  14972. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14973. ("File List for Agenda")
  14974. ("Special views current file"
  14975. ["TODO Tree" org-show-todo-tree t]
  14976. ["Check Deadlines" org-check-deadlines t]
  14977. ["Timeline" org-timeline t]
  14978. ["Tags/Property tree" org-match-sparse-tree t])
  14979. "--"
  14980. ["Export/Publish..." org-export t]
  14981. ("LaTeX"
  14982. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14983. :selected org-cdlatex-mode]
  14984. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14985. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14986. ["Modify math symbol" org-cdlatex-math-modify
  14987. (org-inside-LaTeX-fragment-p)]
  14988. ["Insert citation" org-reftex-citation t]
  14989. "--"
  14990. ["Export LaTeX fragments as images"
  14991. (if (featurep 'org-exp)
  14992. (setq org-export-with-LaTeX-fragments
  14993. (not org-export-with-LaTeX-fragments))
  14994. (require 'org-exp))
  14995. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14996. org-export-with-LaTeX-fragments)]
  14997. "--"
  14998. ["Template for BEAMER" org-beamer-settings-template t])
  14999. "--"
  15000. ("MobileOrg"
  15001. ["Push Files and Views" org-mobile-push t]
  15002. ["Get Captured and Flagged" org-mobile-pull t]
  15003. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15004. "--"
  15005. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15006. "--"
  15007. ("Documentation"
  15008. ["Show Version" org-version t]
  15009. ["Info Documentation" org-info t])
  15010. ("Customize"
  15011. ["Browse Org Group" org-customize t]
  15012. "--"
  15013. ["Expand This Menu" org-create-customize-menu
  15014. (fboundp 'customize-menu-create)])
  15015. ["Send bug report" org-submit-bug-report t]
  15016. "--"
  15017. ("Refresh/Reload"
  15018. ["Refresh setup current buffer" org-mode-restart t]
  15019. ["Reload Org (after update)" org-reload t]
  15020. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15021. ))
  15022. (defun org-info (&optional node)
  15023. "Read documentation for Org-mode in the info system.
  15024. With optional NODE, go directly to that node."
  15025. (interactive)
  15026. (info (format "(org)%s" (or node ""))))
  15027. ;;;###autoload
  15028. (defun org-submit-bug-report ()
  15029. "Submit a bug report on Org-mode via mail.
  15030. Don't hesitate to report any problems or inaccurate documentation.
  15031. If you don't have setup sending mail from (X)Emacs, please copy the
  15032. output buffer into your mail program, as it gives us important
  15033. information about your Org-mode version and configuration."
  15034. (interactive)
  15035. (require 'reporter)
  15036. (org-load-modules-maybe)
  15037. (org-require-autoloaded-modules)
  15038. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15039. (reporter-submit-bug-report
  15040. "emacs-orgmode@gnu.org"
  15041. (org-version)
  15042. (let (list)
  15043. (save-window-excursion
  15044. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15045. (delete-other-windows)
  15046. (erase-buffer)
  15047. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15048. We would like to add your full Org-mode and Outline configuration to the
  15049. bug report. This greatly simplifies the work of the maintainer and
  15050. other experts on the mailing list.
  15051. HOWEVER, some variables you have customized may contain private
  15052. information. The names of customers, colleagues, or friends, might
  15053. appear in the form of file names, tags, todo states, or search strings.
  15054. If you answer yes to the prompt, you might want to check and remove
  15055. such private information before sending the email.")
  15056. (add-text-properties (point-min) (point-max) '(face org-warning))
  15057. (when (yes-or-no-p "Include your Org-mode configuration ")
  15058. (mapatoms
  15059. (lambda (v)
  15060. (and (boundp v)
  15061. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15062. (or (and (symbol-value v)
  15063. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15064. (and
  15065. (get v 'custom-type) (get v 'standard-value)
  15066. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15067. (push v list)))))
  15068. (kill-buffer (get-buffer "*Warn about privacy*"))
  15069. list))
  15070. nil nil
  15071. "Remember to cover the basics, that is, what you expected to happen and
  15072. what in fact did happen. You don't know how to make a good report? See
  15073. http://orgmode.org/manual/Feedback.html#Feedback
  15074. Your bug report will be posted to the Org-mode mailing list.
  15075. ------------------------------------------------------------------------")
  15076. (save-excursion
  15077. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15078. (replace-match "\\1Bug: \\3 [\\2]")))))
  15079. (defun org-install-agenda-files-menu ()
  15080. (let ((bl (buffer-list)))
  15081. (save-excursion
  15082. (while bl
  15083. (set-buffer (pop bl))
  15084. (if (org-mode-p) (setq bl nil)))
  15085. (when (org-mode-p)
  15086. (easy-menu-change
  15087. '("Org") "File List for Agenda"
  15088. (append
  15089. (list
  15090. ["Edit File List" (org-edit-agenda-file-list) t]
  15091. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15092. ["Remove Current File from List" org-remove-file t]
  15093. ["Cycle through agenda files" org-cycle-agenda-files t]
  15094. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15095. "--")
  15096. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15097. ;;;; Documentation
  15098. ;;;###autoload
  15099. (defun org-require-autoloaded-modules ()
  15100. (interactive)
  15101. (mapc 'require
  15102. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15103. org-docbook org-exp org-html org-icalendar
  15104. org-id org-latex
  15105. org-publish org-remember org-table
  15106. org-timer org-xoxo)))
  15107. ;;;###autoload
  15108. (defun org-reload (&optional uncompiled)
  15109. "Reload all org lisp files.
  15110. With prefix arg UNCOMPILED, load the uncompiled versions."
  15111. (interactive "P")
  15112. (require 'find-func)
  15113. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15114. (dir-org (file-name-directory (org-find-library-name "org")))
  15115. (dir-org-contrib (ignore-errors
  15116. (file-name-directory
  15117. (org-find-library-name "org-contribdir"))))
  15118. (files
  15119. (append (directory-files dir-org t file-re)
  15120. (and dir-org-contrib
  15121. (directory-files dir-org-contrib t file-re))))
  15122. (remove-re (concat (if (featurep 'xemacs)
  15123. "org-colview" "org-colview-xemacs")
  15124. "\\'")))
  15125. (setq files (mapcar 'file-name-sans-extension files))
  15126. (setq files (mapcar
  15127. (lambda (x) (if (string-match remove-re x) nil x))
  15128. files))
  15129. (setq files (delq nil files))
  15130. (mapc
  15131. (lambda (f)
  15132. (when (featurep (intern (file-name-nondirectory f)))
  15133. (if (and (not uncompiled)
  15134. (file-exists-p (concat f ".elc")))
  15135. (load (concat f ".elc") nil nil t)
  15136. (load (concat f ".el") nil nil t))))
  15137. files))
  15138. (org-version))
  15139. ;;;###autoload
  15140. (defun org-customize ()
  15141. "Call the customize function with org as argument."
  15142. (interactive)
  15143. (org-load-modules-maybe)
  15144. (org-require-autoloaded-modules)
  15145. (customize-browse 'org))
  15146. (defun org-create-customize-menu ()
  15147. "Create a full customization menu for Org-mode, insert it into the menu."
  15148. (interactive)
  15149. (org-load-modules-maybe)
  15150. (org-require-autoloaded-modules)
  15151. (if (fboundp 'customize-menu-create)
  15152. (progn
  15153. (easy-menu-change
  15154. '("Org") "Customize"
  15155. `(["Browse Org group" org-customize t]
  15156. "--"
  15157. ,(customize-menu-create 'org)
  15158. ["Set" Custom-set t]
  15159. ["Save" Custom-save t]
  15160. ["Reset to Current" Custom-reset-current t]
  15161. ["Reset to Saved" Custom-reset-saved t]
  15162. ["Reset to Standard Settings" Custom-reset-standard t]))
  15163. (message "\"Org\"-menu now contains full customization menu"))
  15164. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15165. ;;;; Miscellaneous stuff
  15166. ;;; Generally useful functions
  15167. (defun org-get-at-bol (property)
  15168. "Get text property PROPERTY at beginning of line."
  15169. (get-text-property (point-at-bol) property))
  15170. (defun org-find-text-property-in-string (prop s)
  15171. "Return the first non-nil value of property PROP in string S."
  15172. (or (get-text-property 0 prop s)
  15173. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15174. prop s)))
  15175. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15176. "Display the given MESSAGE as a warning."
  15177. (if (fboundp 'display-warning)
  15178. (display-warning 'org message
  15179. (if (featurep 'xemacs)
  15180. 'warning
  15181. :warning))
  15182. (let ((buf (get-buffer-create "*Org warnings*")))
  15183. (with-current-buffer buf
  15184. (goto-char (point-max))
  15185. (insert "Warning (Org): " message)
  15186. (unless (bolp)
  15187. (newline)))
  15188. (display-buffer buf)
  15189. (sit-for 0))))
  15190. (defun org-in-commented-line ()
  15191. "Is point in a line starting with `#'?"
  15192. (equal (char-after (point-at-bol)) ?#))
  15193. (defun org-in-verbatim-emphasis ()
  15194. (save-match-data
  15195. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15196. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15197. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15198. (if (and marker (marker-buffer marker)
  15199. (buffer-live-p (marker-buffer marker)))
  15200. (progn
  15201. (switch-to-buffer (marker-buffer marker))
  15202. (if (or (> marker (point-max)) (< marker (point-min)))
  15203. (widen))
  15204. (goto-char marker)
  15205. (org-show-context 'org-goto))
  15206. (if bookmark
  15207. (bookmark-jump bookmark)
  15208. (error "Cannot find location"))))
  15209. (defun org-quote-csv-field (s)
  15210. "Quote field for inclusion in CSV material."
  15211. (if (string-match "[\",]" s)
  15212. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15213. s))
  15214. (defun org-plist-delete (plist property)
  15215. "Delete PROPERTY from PLIST.
  15216. This is in contrast to merely setting it to 0."
  15217. (let (p)
  15218. (while plist
  15219. (if (not (eq property (car plist)))
  15220. (setq p (plist-put p (car plist) (nth 1 plist))))
  15221. (setq plist (cddr plist)))
  15222. p))
  15223. (defun org-force-self-insert (N)
  15224. "Needed to enforce self-insert under remapping."
  15225. (interactive "p")
  15226. (self-insert-command N))
  15227. (defun org-string-width (s)
  15228. "Compute width of string, ignoring invisible characters.
  15229. This ignores character with invisibility property `org-link', and also
  15230. characters with property `org-cwidth', because these will become invisible
  15231. upon the next fontification round."
  15232. (let (b l)
  15233. (when (or (eq t buffer-invisibility-spec)
  15234. (assq 'org-link buffer-invisibility-spec))
  15235. (while (setq b (text-property-any 0 (length s)
  15236. 'invisible 'org-link s))
  15237. (setq s (concat (substring s 0 b)
  15238. (substring s (or (next-single-property-change
  15239. b 'invisible s) (length s)))))))
  15240. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15241. (setq s (concat (substring s 0 b)
  15242. (substring s (or (next-single-property-change
  15243. b 'org-cwidth s) (length s))))))
  15244. (setq l (string-width s) b -1)
  15245. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15246. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15247. l))
  15248. (defun org-get-indentation (&optional line)
  15249. "Get the indentation of the current line, interpreting tabs.
  15250. When LINE is given, assume it represents a line and compute its indentation."
  15251. (if line
  15252. (if (string-match "^ *" (org-remove-tabs line))
  15253. (match-end 0))
  15254. (save-excursion
  15255. (beginning-of-line 1)
  15256. (skip-chars-forward " \t")
  15257. (current-column))))
  15258. (defun org-remove-tabs (s &optional width)
  15259. "Replace tabulators in S with spaces.
  15260. Assumes that s is a single line, starting in column 0."
  15261. (setq width (or width tab-width))
  15262. (while (string-match "\t" s)
  15263. (setq s (replace-match
  15264. (make-string
  15265. (- (* width (/ (+ (match-beginning 0) width) width))
  15266. (match-beginning 0)) ?\ )
  15267. t t s)))
  15268. s)
  15269. (defun org-fix-indentation (line ind)
  15270. "Fix indentation in LINE.
  15271. IND is a cons cell with target and minimum indentation.
  15272. If the current indentation in LINE is smaller than the minimum,
  15273. leave it alone. If it is larger than ind, set it to the target."
  15274. (let* ((l (org-remove-tabs line))
  15275. (i (org-get-indentation l))
  15276. (i1 (car ind)) (i2 (cdr ind)))
  15277. (if (>= i i2) (setq l (substring line i2)))
  15278. (if (> i1 0)
  15279. (concat (make-string i1 ?\ ) l)
  15280. l)))
  15281. (defun org-remove-indentation (code &optional n)
  15282. "Remove the maximum common indentation from the lines in CODE.
  15283. N may optionally be the number of spaces to remove."
  15284. (with-temp-buffer
  15285. (insert code)
  15286. (org-do-remove-indentation n)
  15287. (buffer-string)))
  15288. (defun org-do-remove-indentation (&optional n)
  15289. "Remove the maximum common indentation from the buffer."
  15290. (untabify (point-min) (point-max))
  15291. (let ((min 10000) re)
  15292. (if n
  15293. (setq min n)
  15294. (goto-char (point-min))
  15295. (while (re-search-forward "^ *[^ \n]" nil t)
  15296. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15297. (unless (or (= min 0) (= min 10000))
  15298. (setq re (format "^ \\{%d\\}" min))
  15299. (goto-char (point-min))
  15300. (while (re-search-forward re nil t)
  15301. (replace-match "")
  15302. (end-of-line 1))
  15303. min)))
  15304. (defun org-fill-template (template alist)
  15305. "Find each %key of ALIST in TEMPLATE and replace it."
  15306. (let ((case-fold-search nil)
  15307. entry key value)
  15308. (setq alist (sort (copy-sequence alist)
  15309. (lambda (a b) (< (length (car a)) (length (car b))))))
  15310. (while (setq entry (pop alist))
  15311. (setq template
  15312. (replace-regexp-in-string
  15313. (concat "%" (regexp-quote (car entry)))
  15314. (cdr entry) template t t)))
  15315. template))
  15316. (defun org-base-buffer (buffer)
  15317. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15318. (if (not buffer)
  15319. buffer
  15320. (or (buffer-base-buffer buffer)
  15321. buffer)))
  15322. (defun org-trim (s)
  15323. "Remove whitespace at beginning and end of string."
  15324. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15325. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15326. s)
  15327. (defun org-wrap (string &optional width lines)
  15328. "Wrap string to either a number of lines, or a width in characters.
  15329. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15330. that costs. If there is a word longer than WIDTH, the text is actually
  15331. wrapped to the length of that word.
  15332. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15333. many lines, whatever width that takes.
  15334. The return value is a list of lines, without newlines at the end."
  15335. (let* ((words (org-split-string string "[ \t\n]+"))
  15336. (maxword (apply 'max (mapcar 'org-string-width words)))
  15337. w ll)
  15338. (cond (width
  15339. (org-do-wrap words (max maxword width)))
  15340. (lines
  15341. (setq w maxword)
  15342. (setq ll (org-do-wrap words maxword))
  15343. (if (<= (length ll) lines)
  15344. ll
  15345. (setq ll words)
  15346. (while (> (length ll) lines)
  15347. (setq w (1+ w))
  15348. (setq ll (org-do-wrap words w)))
  15349. ll))
  15350. (t (error "Cannot wrap this")))))
  15351. (defun org-do-wrap (words width)
  15352. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15353. (let (lines line)
  15354. (while words
  15355. (setq line (pop words))
  15356. (while (and words (< (+ (length line) (length (car words))) width))
  15357. (setq line (concat line " " (pop words))))
  15358. (setq lines (push line lines)))
  15359. (nreverse lines)))
  15360. (defun org-split-string (string &optional separators)
  15361. "Splits STRING into substrings at SEPARATORS.
  15362. No empty strings are returned if there are matches at the beginning
  15363. and end of string."
  15364. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15365. (start 0)
  15366. notfirst
  15367. (list nil))
  15368. (while (and (string-match rexp string
  15369. (if (and notfirst
  15370. (= start (match-beginning 0))
  15371. (< start (length string)))
  15372. (1+ start) start))
  15373. (< (match-beginning 0) (length string)))
  15374. (setq notfirst t)
  15375. (or (eq (match-beginning 0) 0)
  15376. (and (eq (match-beginning 0) (match-end 0))
  15377. (eq (match-beginning 0) start))
  15378. (setq list
  15379. (cons (substring string start (match-beginning 0))
  15380. list)))
  15381. (setq start (match-end 0)))
  15382. (or (eq start (length string))
  15383. (setq list
  15384. (cons (substring string start)
  15385. list)))
  15386. (nreverse list)))
  15387. (defun org-quote-vert (s)
  15388. "Replace \"|\" with \"\\vert\"."
  15389. (while (string-match "|" s)
  15390. (setq s (replace-match "\\vert" t t s)))
  15391. s)
  15392. (defun org-uuidgen-p (s)
  15393. "Is S an ID created by UUIDGEN?"
  15394. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15395. (defun org-context ()
  15396. "Return a list of contexts of the current cursor position.
  15397. If several contexts apply, all are returned.
  15398. Each context entry is a list with a symbol naming the context, and
  15399. two positions indicating start and end of the context. Possible
  15400. contexts are:
  15401. :headline anywhere in a headline
  15402. :headline-stars on the leading stars in a headline
  15403. :todo-keyword on a TODO keyword (including DONE) in a headline
  15404. :tags on the TAGS in a headline
  15405. :priority on the priority cookie in a headline
  15406. :item on the first line of a plain list item
  15407. :item-bullet on the bullet/number of a plain list item
  15408. :checkbox on the checkbox in a plain list item
  15409. :table in an org-mode table
  15410. :table-special on a special filed in a table
  15411. :table-table in a table.el table
  15412. :link on a hyperlink
  15413. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15414. :target on a <<target>>
  15415. :radio-target on a <<<radio-target>>>
  15416. :latex-fragment on a LaTeX fragment
  15417. :latex-preview on a LaTeX fragment with overlayed preview image
  15418. This function expects the position to be visible because it uses font-lock
  15419. faces as a help to recognize the following contexts: :table-special, :link,
  15420. and :keyword."
  15421. (let* ((f (get-text-property (point) 'face))
  15422. (faces (if (listp f) f (list f)))
  15423. (p (point)) clist o)
  15424. ;; First the large context
  15425. (cond
  15426. ((org-on-heading-p t)
  15427. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15428. (when (progn
  15429. (beginning-of-line 1)
  15430. (looking-at org-todo-line-tags-regexp))
  15431. (push (org-point-in-group p 1 :headline-stars) clist)
  15432. (push (org-point-in-group p 2 :todo-keyword) clist)
  15433. (push (org-point-in-group p 4 :tags) clist))
  15434. (goto-char p)
  15435. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15436. (if (looking-at "\\[#[A-Z0-9]\\]")
  15437. (push (org-point-in-group p 0 :priority) clist)))
  15438. ((org-at-item-p)
  15439. (push (org-point-in-group p 2 :item-bullet) clist)
  15440. (push (list :item (point-at-bol)
  15441. (save-excursion (org-end-of-item) (point)))
  15442. clist)
  15443. (and (org-at-item-checkbox-p)
  15444. (push (org-point-in-group p 0 :checkbox) clist)))
  15445. ((org-at-table-p)
  15446. (push (list :table (org-table-begin) (org-table-end)) clist)
  15447. (if (memq 'org-formula faces)
  15448. (push (list :table-special
  15449. (previous-single-property-change p 'face)
  15450. (next-single-property-change p 'face)) clist)))
  15451. ((org-at-table-p 'any)
  15452. (push (list :table-table) clist)))
  15453. (goto-char p)
  15454. ;; Now the small context
  15455. (cond
  15456. ((org-at-timestamp-p)
  15457. (push (org-point-in-group p 0 :timestamp) clist))
  15458. ((memq 'org-link faces)
  15459. (push (list :link
  15460. (previous-single-property-change p 'face)
  15461. (next-single-property-change p 'face)) clist))
  15462. ((memq 'org-special-keyword faces)
  15463. (push (list :keyword
  15464. (previous-single-property-change p 'face)
  15465. (next-single-property-change p 'face)) clist))
  15466. ((org-on-target-p)
  15467. (push (org-point-in-group p 0 :target) clist)
  15468. (goto-char (1- (match-beginning 0)))
  15469. (if (looking-at org-radio-target-regexp)
  15470. (push (org-point-in-group p 0 :radio-target) clist))
  15471. (goto-char p))
  15472. ((setq o (car (delq nil
  15473. (mapcar
  15474. (lambda (x)
  15475. (if (memq x org-latex-fragment-image-overlays) x))
  15476. (org-overlays-at (point))))))
  15477. (push (list :latex-fragment
  15478. (org-overlay-start o) (org-overlay-end o)) clist)
  15479. (push (list :latex-preview
  15480. (org-overlay-start o) (org-overlay-end o)) clist))
  15481. ((org-inside-LaTeX-fragment-p)
  15482. ;; FIXME: positions wrong.
  15483. (push (list :latex-fragment (point) (point)) clist)))
  15484. (setq clist (nreverse (delq nil clist)))
  15485. clist))
  15486. ;; FIXME: Compare with at-regexp-p Do we need both?
  15487. (defun org-in-regexp (re &optional nlines visually)
  15488. "Check if point is inside a match of regexp.
  15489. Normally only the current line is checked, but you can include NLINES extra
  15490. lines both before and after point into the search.
  15491. If VISUALLY is set, require that the cursor is not after the match but
  15492. really on, so that the block visually is on the match."
  15493. (catch 'exit
  15494. (let ((pos (point))
  15495. (eol (point-at-eol (+ 1 (or nlines 0))))
  15496. (inc (if visually 1 0)))
  15497. (save-excursion
  15498. (beginning-of-line (- 1 (or nlines 0)))
  15499. (while (re-search-forward re eol t)
  15500. (if (and (<= (match-beginning 0) pos)
  15501. (>= (+ inc (match-end 0)) pos))
  15502. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15503. (defun org-at-regexp-p (regexp)
  15504. "Is point inside a match of REGEXP in the current line?"
  15505. (catch 'exit
  15506. (save-excursion
  15507. (let ((pos (point)) (end (point-at-eol)))
  15508. (beginning-of-line 1)
  15509. (while (re-search-forward regexp end t)
  15510. (if (and (<= (match-beginning 0) pos)
  15511. (>= (match-end 0) pos))
  15512. (throw 'exit t)))
  15513. nil))))
  15514. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15515. "Call `multi-occur' with buffers for all agenda files."
  15516. (interactive "sOrg-files matching: \np")
  15517. (let* ((files (org-agenda-files))
  15518. (tnames (mapcar 'file-truename files))
  15519. (extra org-agenda-text-search-extra-files)
  15520. f)
  15521. (when (eq (car extra) 'agenda-archives)
  15522. (setq extra (cdr extra))
  15523. (setq files (org-add-archive-files files)))
  15524. (while (setq f (pop extra))
  15525. (unless (member (file-truename f) tnames)
  15526. (add-to-list 'files f 'append)
  15527. (add-to-list 'tnames (file-truename f) 'append)))
  15528. (multi-occur
  15529. (mapcar (lambda (x)
  15530. (with-current-buffer
  15531. (or (get-file-buffer x) (find-file-noselect x))
  15532. (widen)
  15533. (current-buffer)))
  15534. files)
  15535. regexp)))
  15536. (if (boundp 'occur-mode-find-occurrence-hook)
  15537. ;; Emacs 23
  15538. (add-hook 'occur-mode-find-occurrence-hook
  15539. (lambda ()
  15540. (when (org-mode-p)
  15541. (org-reveal))))
  15542. ;; Emacs 22
  15543. (defadvice occur-mode-goto-occurrence
  15544. (after org-occur-reveal activate)
  15545. (and (org-mode-p) (org-reveal)))
  15546. (defadvice occur-mode-goto-occurrence-other-window
  15547. (after org-occur-reveal activate)
  15548. (and (org-mode-p) (org-reveal)))
  15549. (defadvice occur-mode-display-occurrence
  15550. (after org-occur-reveal activate)
  15551. (when (org-mode-p)
  15552. (let ((pos (occur-mode-find-occurrence)))
  15553. (with-current-buffer (marker-buffer pos)
  15554. (save-excursion
  15555. (goto-char pos)
  15556. (org-reveal)))))))
  15557. (defun org-occur-link-in-agenda-files ()
  15558. "Create a link and search for it in the agendas.
  15559. The link is not stored in `org-stored-links', it is just created
  15560. for the search purpose."
  15561. (interactive)
  15562. (let ((link (condition-case nil
  15563. (org-store-link nil)
  15564. (error "Unable to create a link to here"))))
  15565. (org-occur-in-agenda-files (regexp-quote link))))
  15566. (defun org-uniquify (list)
  15567. "Remove duplicate elements from LIST."
  15568. (let (res)
  15569. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15570. res))
  15571. (defun org-delete-all (elts list)
  15572. "Remove all elements in ELTS from LIST."
  15573. (while elts
  15574. (setq list (delete (pop elts) list)))
  15575. list)
  15576. (defun org-back-over-empty-lines ()
  15577. "Move backwards over whitespace, to the beginning of the first empty line.
  15578. Returns the number of empty lines passed."
  15579. (let ((pos (point)))
  15580. (skip-chars-backward " \t\n\r")
  15581. (beginning-of-line 2)
  15582. (goto-char (min (point) pos))
  15583. (count-lines (point) pos)))
  15584. (defun org-skip-whitespace ()
  15585. (skip-chars-forward " \t\n\r"))
  15586. (defun org-point-in-group (point group &optional context)
  15587. "Check if POINT is in match-group GROUP.
  15588. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15589. match. If the match group does ot exist or point is not inside it,
  15590. return nil."
  15591. (and (match-beginning group)
  15592. (>= point (match-beginning group))
  15593. (<= point (match-end group))
  15594. (if context
  15595. (list context (match-beginning group) (match-end group))
  15596. t)))
  15597. (defun org-switch-to-buffer-other-window (&rest args)
  15598. "Switch to buffer in a second window on the current frame.
  15599. In particular, do not allow pop-up frames."
  15600. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15601. special-display-function)
  15602. (apply 'switch-to-buffer-other-window args)))
  15603. (defun org-combine-plists (&rest plists)
  15604. "Create a single property list from all plists in PLISTS.
  15605. The process starts by copying the first list, and then setting properties
  15606. from the other lists. Settings in the last list are the most significant
  15607. ones and overrule settings in the other lists."
  15608. (let ((rtn (copy-sequence (pop plists)))
  15609. p v ls)
  15610. (while plists
  15611. (setq ls (pop plists))
  15612. (while ls
  15613. (setq p (pop ls) v (pop ls))
  15614. (setq rtn (plist-put rtn p v))))
  15615. rtn))
  15616. (defun org-move-line-down (arg)
  15617. "Move the current line down. With prefix argument, move it past ARG lines."
  15618. (interactive "p")
  15619. (let ((col (current-column))
  15620. beg end pos)
  15621. (beginning-of-line 1) (setq beg (point))
  15622. (beginning-of-line 2) (setq end (point))
  15623. (beginning-of-line (+ 1 arg))
  15624. (setq pos (move-marker (make-marker) (point)))
  15625. (insert (delete-and-extract-region beg end))
  15626. (goto-char pos)
  15627. (org-move-to-column col)))
  15628. (defun org-move-line-up (arg)
  15629. "Move the current line up. With prefix argument, move it past ARG lines."
  15630. (interactive "p")
  15631. (let ((col (current-column))
  15632. beg end pos)
  15633. (beginning-of-line 1) (setq beg (point))
  15634. (beginning-of-line 2) (setq end (point))
  15635. (beginning-of-line (- arg))
  15636. (setq pos (move-marker (make-marker) (point)))
  15637. (insert (delete-and-extract-region beg end))
  15638. (goto-char pos)
  15639. (org-move-to-column col)))
  15640. (defun org-replace-escapes (string table)
  15641. "Replace %-escapes in STRING with values in TABLE.
  15642. TABLE is an association list with keys like \"%a\" and string values.
  15643. The sequences in STRING may contain normal field width and padding information,
  15644. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15645. so values can contain further %-escapes if they are define later in TABLE."
  15646. (let ((case-fold-search nil)
  15647. e re rpl)
  15648. (while (setq e (pop table))
  15649. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15650. (while (string-match re string)
  15651. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15652. (cdr e)))
  15653. (setq string (replace-match rpl t t string))))
  15654. string))
  15655. (defun org-sublist (list start end)
  15656. "Return a section of LIST, from START to END.
  15657. Counting starts at 1."
  15658. (let (rtn (c start))
  15659. (setq list (nthcdr (1- start) list))
  15660. (while (and list (<= c end))
  15661. (push (pop list) rtn)
  15662. (setq c (1+ c)))
  15663. (nreverse rtn)))
  15664. (defun org-find-base-buffer-visiting (file)
  15665. "Like `find-buffer-visiting' but always return the base buffer and
  15666. not an indirect buffer."
  15667. (let ((buf (or (get-file-buffer file)
  15668. (find-buffer-visiting file))))
  15669. (if buf
  15670. (or (buffer-base-buffer buf) buf)
  15671. nil)))
  15672. (defun org-image-file-name-regexp (&optional extensions)
  15673. "Return regexp matching the file names of images.
  15674. If EXTENSIONS is given, only match these."
  15675. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  15676. (image-file-name-regexp)
  15677. (let ((image-file-name-extensions
  15678. (or extensions
  15679. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15680. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  15681. (concat "\\."
  15682. (regexp-opt (nconc (mapcar 'upcase
  15683. image-file-name-extensions)
  15684. image-file-name-extensions)
  15685. t)
  15686. "\\'"))))
  15687. (defun org-file-image-p (file &optional extensions)
  15688. "Return non-nil if FILE is an image."
  15689. (save-match-data
  15690. (string-match (org-image-file-name-regexp extensions) file)))
  15691. (defun org-get-cursor-date ()
  15692. "Return the date at cursor in as a time.
  15693. This works in the calendar and in the agenda, anywhere else it just
  15694. returns the current time."
  15695. (let (date day defd)
  15696. (cond
  15697. ((eq major-mode 'calendar-mode)
  15698. (setq date (calendar-cursor-to-date)
  15699. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15700. ((eq major-mode 'org-agenda-mode)
  15701. (setq day (get-text-property (point) 'day))
  15702. (if day
  15703. (setq date (calendar-gregorian-from-absolute day)
  15704. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  15705. (nth 2 date))))))
  15706. (or defd (current-time))))
  15707. (defvar org-agenda-action-marker (make-marker)
  15708. "Marker pointing to the entry for the next agenda action.")
  15709. (defun org-mark-entry-for-agenda-action ()
  15710. "Mark the current entry as target of an agenda action.
  15711. Agenda actions are actions executed from the agenda with the key `k',
  15712. which make use of the date at the cursor."
  15713. (interactive)
  15714. (move-marker org-agenda-action-marker
  15715. (save-excursion (org-back-to-heading t) (point))
  15716. (current-buffer))
  15717. (message
  15718. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  15719. ;;; Paragraph filling stuff.
  15720. ;; We want this to be just right, so use the full arsenal.
  15721. (defun org-indent-line-function ()
  15722. "Indent line like previous, but further if previous was headline or item."
  15723. (interactive)
  15724. (let* ((pos (point))
  15725. (itemp (org-at-item-p))
  15726. (case-fold-search t)
  15727. (org-drawer-regexp (or org-drawer-regexp "\000"))
  15728. column bpos bcol tpos tcol bullet btype bullet-type)
  15729. ;; Find the previous relevant line
  15730. (beginning-of-line 1)
  15731. (cond
  15732. ((looking-at "#") (setq column 0))
  15733. ((looking-at "\\*+ ") (setq column 0))
  15734. ((and (looking-at "[ \t]*:END:")
  15735. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  15736. (save-excursion
  15737. (goto-char (1- (match-beginning 1)))
  15738. (setq column (current-column))))
  15739. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  15740. (save-excursion
  15741. (re-search-backward
  15742. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  15743. (setq column (org-get-indentation (match-string 0))))
  15744. (t
  15745. (beginning-of-line 0)
  15746. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  15747. (not (looking-at "[ \t]*:END:"))
  15748. (not (looking-at org-drawer-regexp)))
  15749. (beginning-of-line 0))
  15750. (cond
  15751. ((looking-at "\\*+[ \t]+")
  15752. (if (not org-adapt-indentation)
  15753. (setq column 0)
  15754. (goto-char (match-end 0))
  15755. (setq column (current-column))))
  15756. ((looking-at org-drawer-regexp)
  15757. (goto-char (1- (match-beginning 1)))
  15758. (setq column (current-column)))
  15759. ((looking-at "\\([ \t]*\\):END:")
  15760. (goto-char (match-end 1))
  15761. (setq column (current-column)))
  15762. ((org-in-item-p)
  15763. (org-beginning-of-item)
  15764. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  15765. (setq bpos (match-beginning 1) tpos (match-end 0)
  15766. bcol (progn (goto-char bpos) (current-column))
  15767. tcol (progn (goto-char tpos) (current-column))
  15768. bullet (match-string 1)
  15769. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  15770. (if (> tcol (+ bcol org-description-max-indent))
  15771. (setq tcol (+ bcol 5)))
  15772. (if (not itemp)
  15773. (setq column tcol)
  15774. (goto-char pos)
  15775. (beginning-of-line 1)
  15776. (if (looking-at "\\S-")
  15777. (progn
  15778. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  15779. (setq bullet (match-string 1)
  15780. btype (if (string-match "[0-9]" bullet) "n" bullet))
  15781. (setq column (if (equal btype bullet-type) bcol tcol)))
  15782. (setq column (org-get-indentation)))))
  15783. (t (setq column (org-get-indentation))))))
  15784. (goto-char pos)
  15785. (if (<= (current-column) (current-indentation))
  15786. (org-indent-line-to column)
  15787. (save-excursion (org-indent-line-to column)))
  15788. (setq column (current-column))
  15789. (beginning-of-line 1)
  15790. (if (looking-at
  15791. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  15792. (replace-match (concat (match-string 1)
  15793. (format org-property-format
  15794. (match-string 2) (match-string 3)))
  15795. t t))
  15796. (org-move-to-column column)))
  15797. (defun org-set-autofill-regexps ()
  15798. (interactive)
  15799. ;; In the paragraph separator we include headlines, because filling
  15800. ;; text in a line directly attached to a headline would otherwise
  15801. ;; fill the headline as well.
  15802. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15803. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  15804. ;; The paragraph starter includes hand-formatted lists.
  15805. (org-set-local
  15806. 'paragraph-start
  15807. (concat
  15808. "\f" "\\|"
  15809. "[ ]*$" "\\|"
  15810. "\\*+ " "\\|"
  15811. "[ \t]*#" "\\|"
  15812. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  15813. "[ \t]*[:|]" "\\|"
  15814. "\\$\\$" "\\|"
  15815. "\\\\\\(begin\\|end\\|[][]\\)"))
  15816. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15817. ;; But only if the user has not turned off tables or fixed-width regions
  15818. (org-set-local
  15819. 'auto-fill-inhibit-regexp
  15820. (concat "\\*+ \\|#\\+"
  15821. "\\|[ \t]*" org-keyword-time-regexp
  15822. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15823. (concat
  15824. "\\|[ \t]*["
  15825. (if org-enable-table-editor "|" "")
  15826. (if org-enable-fixed-width-editor ":" "")
  15827. "]"))))
  15828. ;; We use our own fill-paragraph function, to make sure that tables
  15829. ;; and fixed-width regions are not wrapped. That function will pass
  15830. ;; through to `fill-paragraph' when appropriate.
  15831. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15832. ; Adaptive filling: To get full control, first make sure that
  15833. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15834. (org-set-local 'adaptive-fill-regexp "\000")
  15835. (org-set-local 'adaptive-fill-function
  15836. 'org-adaptive-fill-function)
  15837. (org-set-local
  15838. 'align-mode-rules-list
  15839. '((org-in-buffer-settings
  15840. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15841. (modes . '(org-mode))))))
  15842. (defun org-fill-paragraph (&optional justify)
  15843. "Re-align a table, pass through to fill-paragraph if no table."
  15844. (let ((table-p (org-at-table-p))
  15845. (table.el-p (org-at-table.el-p)))
  15846. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15847. (save-excursion (goto-char (point-at-bol))
  15848. (looking-at outline-regexp)))
  15849. t) ; skip headlines
  15850. (table.el-p t) ; skip table.el tables
  15851. (table-p (org-table-align) t) ; align org-mode tables
  15852. (t nil)))) ; call paragraph-fill
  15853. ;; For reference, this is the default value of adaptive-fill-regexp
  15854. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15855. (defun org-adaptive-fill-function ()
  15856. "Return a fill prefix for org-mode files.
  15857. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15858. work correctly."
  15859. (cond ((looking-at "#[ \t]+")
  15860. (match-string 0))
  15861. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15862. (save-excursion
  15863. (if (> (match-end 1) (+ (match-beginning 1)
  15864. org-description-max-indent))
  15865. (goto-char (+ (match-beginning 1) 5))
  15866. (goto-char (match-end 0)))
  15867. (make-string (current-column) ?\ )))
  15868. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15869. (save-excursion
  15870. (goto-char (match-end 0))
  15871. (make-string (current-column) ?\ )))
  15872. (t nil)))
  15873. ;;; Other stuff.
  15874. (defun org-toggle-fixed-width-section (arg)
  15875. "Toggle the fixed-width export.
  15876. If there is no active region, the QUOTE keyword at the current headline is
  15877. inserted or removed. When present, it causes the text between this headline
  15878. and the next to be exported as fixed-width text, and unmodified.
  15879. If there is an active region, this command adds or removes a colon as the
  15880. first character of this line. If the first character of a line is a colon,
  15881. this line is also exported in fixed-width font."
  15882. (interactive "P")
  15883. (let* ((cc 0)
  15884. (regionp (org-region-active-p))
  15885. (beg (if regionp (region-beginning) (point)))
  15886. (end (if regionp (region-end)))
  15887. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15888. (case-fold-search nil)
  15889. (re "[ \t]*\\(: \\)")
  15890. off)
  15891. (if regionp
  15892. (save-excursion
  15893. (goto-char beg)
  15894. (setq cc (current-column))
  15895. (beginning-of-line 1)
  15896. (setq off (looking-at re))
  15897. (while (> nlines 0)
  15898. (setq nlines (1- nlines))
  15899. (beginning-of-line 1)
  15900. (cond
  15901. (arg
  15902. (org-move-to-column cc t)
  15903. (insert ": \n")
  15904. (forward-line -1))
  15905. ((and off (looking-at re))
  15906. (replace-match "" t t nil 1))
  15907. ((not off) (org-move-to-column cc t) (insert ": ")))
  15908. (forward-line 1)))
  15909. (save-excursion
  15910. (org-back-to-heading)
  15911. (if (looking-at (concat outline-regexp
  15912. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15913. (replace-match "" t t nil 1)
  15914. (if (looking-at outline-regexp)
  15915. (progn
  15916. (goto-char (match-end 0))
  15917. (insert org-quote-string " "))))))))
  15918. (defun org-reftex-citation ()
  15919. "Use reftex-citation to insert a citation into the buffer.
  15920. This looks for a line like
  15921. #+BIBLIOGRAPHY: foo plain option:-d
  15922. and derives from it that foo.bib is the bibliography file relevant
  15923. for this document. It then installs the necessary environment for RefTeX
  15924. to work in this buffer and calls `reftex-citation' to insert a citation
  15925. into the buffer.
  15926. Export of such citations to both LaTeX and HTML is handled by the contributed
  15927. package org-exp-bibtex by Taru Karttunen."
  15928. (interactive)
  15929. (let ((reftex-docstruct-symbol 'rds)
  15930. (reftex-cite-format "\\cite{%l}")
  15931. rds bib)
  15932. (save-excursion
  15933. (save-restriction
  15934. (widen)
  15935. (let ((case-fold-search t)
  15936. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15937. (if (not (save-excursion
  15938. (or (re-search-forward re nil t)
  15939. (re-search-backward re nil t))))
  15940. (error "No bibliography defined in file")
  15941. (setq bib (concat (match-string 1) ".bib")
  15942. rds (list (list 'bib bib)))))))
  15943. (call-interactively 'reftex-citation)))
  15944. ;;;; Functions extending outline functionality
  15945. (defun org-beginning-of-line (&optional arg)
  15946. "Go to the beginning of the current line. If that is invisible, continue
  15947. to a visible line beginning. This makes the function of C-a more intuitive.
  15948. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15949. first attempt, and only move to after the tags when the cursor is already
  15950. beyond the end of the headline."
  15951. (interactive "P")
  15952. (let ((pos (point))
  15953. (special (if (consp org-special-ctrl-a/e)
  15954. (car org-special-ctrl-a/e)
  15955. org-special-ctrl-a/e))
  15956. refpos)
  15957. (if (org-bound-and-true-p line-move-visual)
  15958. (beginning-of-visual-line 1)
  15959. (beginning-of-line 1))
  15960. (if (and arg (fboundp 'move-beginning-of-line))
  15961. (call-interactively 'move-beginning-of-line)
  15962. (if (bobp)
  15963. nil
  15964. (backward-char 1)
  15965. (if (org-invisible-p)
  15966. (while (and (not (bobp)) (org-invisible-p))
  15967. (backward-char 1)
  15968. (beginning-of-line 1))
  15969. (forward-char 1))))
  15970. (when special
  15971. (cond
  15972. ((and (looking-at org-complex-heading-regexp)
  15973. (= (char-after (match-end 1)) ?\ ))
  15974. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15975. (point-at-eol)))
  15976. (goto-char
  15977. (if (eq special t)
  15978. (cond ((> pos refpos) refpos)
  15979. ((= pos (point)) refpos)
  15980. (t (point)))
  15981. (cond ((> pos (point)) (point))
  15982. ((not (eq last-command this-command)) (point))
  15983. (t refpos)))))
  15984. ((org-at-item-p)
  15985. (goto-char
  15986. (if (eq special t)
  15987. (cond ((> pos (match-end 4)) (match-end 4))
  15988. ((= pos (point)) (match-end 4))
  15989. (t (point)))
  15990. (cond ((> pos (point)) (point))
  15991. ((not (eq last-command this-command)) (point))
  15992. (t (match-end 4))))))))
  15993. (org-no-warnings
  15994. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15995. (defun org-end-of-line (&optional arg)
  15996. "Go to the end of the line.
  15997. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15998. first attempt, and only move to after the tags when the cursor is already
  15999. beyond the end of the headline."
  16000. (interactive "P")
  16001. (let ((special (if (consp org-special-ctrl-a/e)
  16002. (cdr org-special-ctrl-a/e)
  16003. org-special-ctrl-a/e)))
  16004. (if (or (not special)
  16005. (not (org-on-heading-p))
  16006. arg)
  16007. (call-interactively
  16008. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16009. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16010. (t 'end-of-line)))
  16011. (let ((pos (point)))
  16012. (beginning-of-line 1)
  16013. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16014. (if (eq special t)
  16015. (if (or (< pos (match-beginning 1))
  16016. (= pos (match-end 0)))
  16017. (goto-char (match-beginning 1))
  16018. (goto-char (match-end 0)))
  16019. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16020. (goto-char (match-end 0))
  16021. (goto-char (match-beginning 1))))
  16022. (call-interactively (if (fboundp 'move-end-of-line)
  16023. 'move-end-of-line
  16024. 'end-of-line)))))
  16025. (org-no-warnings
  16026. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16027. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16028. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16029. (define-key org-mode-map [home] 'org-beginning-of-line)
  16030. (define-key org-mode-map [end] 'org-end-of-line)
  16031. (defun org-backward-sentence (&optional arg)
  16032. "Go to beginning of sentence, or beginning of table field.
  16033. This will call `backward-sentence' or `org-table-beginning-of-field',
  16034. depending on context."
  16035. (interactive "P")
  16036. (cond
  16037. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16038. (t (call-interactively 'backward-sentence))))
  16039. (defun org-forward-sentence (&optional arg)
  16040. "Go to end of sentence, or end of table field.
  16041. This will call `forward-sentence' or `org-table-end-of-field',
  16042. depending on context."
  16043. (interactive "P")
  16044. (cond
  16045. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16046. (t (call-interactively 'forward-sentence))))
  16047. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16048. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16049. (defun org-kill-line (&optional arg)
  16050. "Kill line, to tags or end of line."
  16051. (interactive "P")
  16052. (cond
  16053. ((or (not org-special-ctrl-k)
  16054. (bolp)
  16055. (not (org-on-heading-p)))
  16056. (call-interactively 'kill-line))
  16057. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16058. (kill-region (point) (match-beginning 1))
  16059. (org-set-tags nil t))
  16060. (t (kill-region (point) (point-at-eol)))))
  16061. (define-key org-mode-map "\C-k" 'org-kill-line)
  16062. (defun org-yank (&optional arg)
  16063. "Yank. If the kill is a subtree, treat it specially.
  16064. This command will look at the current kill and check if is a single
  16065. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16066. cursor is at the beginning of a line or after the stars of a currently
  16067. empty headline, then the yank is handled specially. How exactly depends
  16068. on the value of the following variables, both set by default.
  16069. org-yank-folded-subtrees
  16070. When set, the subtree(s) will be folded after insertion, but only
  16071. if doing so would now swallow text after the yanked text.
  16072. org-yank-adjusted-subtrees
  16073. When set, the subtree will be promoted or demoted in order to
  16074. fit into the local outline tree structure, which means that the level
  16075. will be adjusted so that it becomes the smaller one of the two
  16076. *visible* surrounding headings.
  16077. Any prefix to this command will cause `yank' to be called directly with
  16078. no special treatment. In particular, a simple `C-u' prefix will just
  16079. plainly yank the text as it is.
  16080. \[1] The test checks if the first non-white line is a heading
  16081. and if there are no other headings with fewer stars."
  16082. (interactive "P")
  16083. (org-yank-generic 'yank arg))
  16084. (defun org-yank-generic (command arg)
  16085. "Perform some yank-like command.
  16086. This function implements the behavior described in the `org-yank'
  16087. documentation. However, it has been generalized to work for any
  16088. interactive command with similar behavior."
  16089. ;; pretend to be command COMMAND
  16090. (setq this-command command)
  16091. (if arg
  16092. (call-interactively command)
  16093. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16094. (and (org-kill-is-subtree-p)
  16095. (or (bolp)
  16096. (and (looking-at "[ \t]*$")
  16097. (string-match
  16098. "\\`\\*+\\'"
  16099. (buffer-substring (point-at-bol) (point)))))))
  16100. swallowp)
  16101. (cond
  16102. ((and subtreep org-yank-folded-subtrees)
  16103. (let ((beg (point))
  16104. end)
  16105. (if (and subtreep org-yank-adjusted-subtrees)
  16106. (org-paste-subtree nil nil 'for-yank)
  16107. (call-interactively command))
  16108. (setq end (point))
  16109. (goto-char beg)
  16110. (when (and (bolp) subtreep
  16111. (not (setq swallowp
  16112. (org-yank-folding-would-swallow-text beg end))))
  16113. (or (looking-at outline-regexp)
  16114. (re-search-forward (concat "^" outline-regexp) end t))
  16115. (while (and (< (point) end) (looking-at outline-regexp))
  16116. (hide-subtree)
  16117. (org-cycle-show-empty-lines 'folded)
  16118. (condition-case nil
  16119. (outline-forward-same-level 1)
  16120. (error (goto-char end)))))
  16121. (when swallowp
  16122. (message
  16123. "Inserted text not folded because that would swallow text"))
  16124. (goto-char end)
  16125. (skip-chars-forward " \t\n\r")
  16126. (beginning-of-line 1)
  16127. (push-mark beg 'nomsg)))
  16128. ((and subtreep org-yank-adjusted-subtrees)
  16129. (let ((beg (point-at-bol)))
  16130. (org-paste-subtree nil nil 'for-yank)
  16131. (push-mark beg 'nomsg)))
  16132. (t
  16133. (call-interactively command))))))
  16134. (defun org-yank-folding-would-swallow-text (beg end)
  16135. "Would hide-subtree at BEG swallow any text after END?"
  16136. (let (level)
  16137. (save-excursion
  16138. (goto-char beg)
  16139. (when (or (looking-at outline-regexp)
  16140. (re-search-forward (concat "^" outline-regexp) end t))
  16141. (setq level (org-outline-level)))
  16142. (goto-char end)
  16143. (skip-chars-forward " \t\r\n\v\f")
  16144. (if (or (eobp)
  16145. (and (bolp) (looking-at org-outline-regexp)
  16146. (<= (org-outline-level) level)))
  16147. nil ; Nothing would be swallowed
  16148. t)))) ; something would swallow
  16149. (define-key org-mode-map "\C-y" 'org-yank)
  16150. (defun org-invisible-p ()
  16151. "Check if point is at a character currently not visible."
  16152. ;; Early versions of noutline don't have `outline-invisible-p'.
  16153. (if (fboundp 'outline-invisible-p)
  16154. (outline-invisible-p)
  16155. (get-char-property (point) 'invisible)))
  16156. (defun org-invisible-p2 ()
  16157. "Check if point is at a character currently not visible."
  16158. (save-excursion
  16159. (if (and (eolp) (not (bobp))) (backward-char 1))
  16160. ;; Early versions of noutline don't have `outline-invisible-p'.
  16161. (if (fboundp 'outline-invisible-p)
  16162. (outline-invisible-p)
  16163. (get-char-property (point) 'invisible))))
  16164. (defun org-back-to-heading (&optional invisible-ok)
  16165. "Call `outline-back-to-heading', but provide a better error message."
  16166. (condition-case nil
  16167. (outline-back-to-heading invisible-ok)
  16168. (error (error "Before first headline at position %d in buffer %s"
  16169. (point) (current-buffer)))))
  16170. (defun org-before-first-heading-p ()
  16171. "Before first heading?"
  16172. (save-excursion
  16173. (null (re-search-backward "^\\*+ " nil t))))
  16174. (defun org-on-heading-p (&optional ignored)
  16175. (outline-on-heading-p t))
  16176. (defun org-at-heading-p (&optional ignored)
  16177. (outline-on-heading-p t))
  16178. (defun org-at-heading-or-item-p ()
  16179. (or (org-on-heading-p) (org-at-item-p)))
  16180. (defun org-on-target-p ()
  16181. (or (org-in-regexp org-radio-target-regexp)
  16182. (org-in-regexp org-target-regexp)))
  16183. (defun org-up-heading-all (arg)
  16184. "Move to the heading line of which the present line is a subheading.
  16185. This function considers both visible and invisible heading lines.
  16186. With argument, move up ARG levels."
  16187. (if (fboundp 'outline-up-heading-all)
  16188. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16189. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16190. (defun org-up-heading-safe ()
  16191. "Move to the heading line of which the present line is a subheading.
  16192. This version will not throw an error. It will return the level of the
  16193. headline found, or nil if no higher level is found.
  16194. Also, this function will be a lot faster than `outline-up-heading',
  16195. because it relies on stars being the outline starters. This can really
  16196. make a significant difference in outlines with very many siblings."
  16197. (let (start-level re)
  16198. (org-back-to-heading t)
  16199. (setq start-level (funcall outline-level))
  16200. (if (equal start-level 1)
  16201. nil
  16202. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16203. (if (re-search-backward re nil t)
  16204. (funcall outline-level)))))
  16205. (defun org-first-sibling-p ()
  16206. "Is this heading the first child of its parents?"
  16207. (interactive)
  16208. (let ((re (concat "^" outline-regexp))
  16209. level l)
  16210. (unless (org-at-heading-p t)
  16211. (error "Not at a heading"))
  16212. (setq level (funcall outline-level))
  16213. (save-excursion
  16214. (if (not (re-search-backward re nil t))
  16215. t
  16216. (setq l (funcall outline-level))
  16217. (< l level)))))
  16218. (defun org-goto-sibling (&optional previous)
  16219. "Goto the next sibling, even if it is invisible.
  16220. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16221. when a sibling was found. When none is found, return nil and don't
  16222. move point."
  16223. (let ((fun (if previous 're-search-backward 're-search-forward))
  16224. (pos (point))
  16225. (re (concat "^" outline-regexp))
  16226. level l)
  16227. (when (condition-case nil (org-back-to-heading t) (error nil))
  16228. (setq level (funcall outline-level))
  16229. (catch 'exit
  16230. (or previous (forward-char 1))
  16231. (while (funcall fun re nil t)
  16232. (setq l (funcall outline-level))
  16233. (when (< l level) (goto-char pos) (throw 'exit nil))
  16234. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16235. (goto-char pos)
  16236. nil))))
  16237. (defun org-show-siblings ()
  16238. "Show all siblings of the current headline."
  16239. (save-excursion
  16240. (while (org-goto-sibling) (org-flag-heading nil)))
  16241. (save-excursion
  16242. (while (org-goto-sibling 'previous)
  16243. (org-flag-heading nil))))
  16244. (defun org-show-hidden-entry ()
  16245. "Show an entry where even the heading is hidden."
  16246. (save-excursion
  16247. (org-show-entry)))
  16248. (defun org-flag-heading (flag &optional entry)
  16249. "Flag the current heading. FLAG non-nil means make invisible.
  16250. When ENTRY is non-nil, show the entire entry."
  16251. (save-excursion
  16252. (org-back-to-heading t)
  16253. ;; Check if we should show the entire entry
  16254. (if entry
  16255. (progn
  16256. (org-show-entry)
  16257. (save-excursion
  16258. (and (outline-next-heading)
  16259. (org-flag-heading nil))))
  16260. (outline-flag-region (max (point-min) (1- (point)))
  16261. (save-excursion (outline-end-of-heading) (point))
  16262. flag))))
  16263. (defun org-get-next-sibling ()
  16264. "Move to next heading of the same level, and return point.
  16265. If there is no such heading, return nil.
  16266. This is like outline-next-sibling, but invisible headings are ok."
  16267. (let ((level (funcall outline-level)))
  16268. (outline-next-heading)
  16269. (while (and (not (eobp)) (> (funcall outline-level) level))
  16270. (outline-next-heading))
  16271. (if (or (eobp) (< (funcall outline-level) level))
  16272. nil
  16273. (point))))
  16274. (defun org-get-last-sibling ()
  16275. "Move to previous heading of the same level, and return point.
  16276. If there is no such heading, return nil."
  16277. (let ((opoint (point))
  16278. (level (funcall outline-level)))
  16279. (outline-previous-heading)
  16280. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16281. (while (and (> (funcall outline-level) level)
  16282. (not (bobp)))
  16283. (outline-previous-heading))
  16284. (if (< (funcall outline-level) level)
  16285. nil
  16286. (point)))))
  16287. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16288. ;; This contains an exact copy of the original function, but it uses
  16289. ;; `org-back-to-heading', to make it work also in invisible
  16290. ;; trees. And is uses an invisible-OK argument.
  16291. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16292. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16293. ;; with many children and grandchildren etc, this can be much faster
  16294. ;; than the outline version.
  16295. (org-back-to-heading invisible-OK)
  16296. (let ((first t)
  16297. (level (funcall outline-level)))
  16298. (if (and (org-mode-p) (< level 1000))
  16299. ;; A true heading (not a plain list item), in Org-mode
  16300. ;; This means we can easily find the end by looking
  16301. ;; only for the right number of stars. Using a regexp to do
  16302. ;; this is so much faster than using a Lisp loop.
  16303. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16304. (forward-char 1)
  16305. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16306. ;; something else, do it the slow way
  16307. (while (and (not (eobp))
  16308. (or first (> (funcall outline-level) level)))
  16309. (setq first nil)
  16310. (outline-next-heading)))
  16311. (unless to-heading
  16312. (if (memq (preceding-char) '(?\n ?\^M))
  16313. (progn
  16314. ;; Go to end of line before heading
  16315. (forward-char -1)
  16316. (if (memq (preceding-char) '(?\n ?\^M))
  16317. ;; leave blank line before heading
  16318. (forward-char -1))))))
  16319. (point))
  16320. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16321. "Use Org version in org-mode, for dramatic speed-up."
  16322. (if (eq major-mode 'org-mode)
  16323. (progn
  16324. (org-end-of-subtree nil t)
  16325. (unless (eobp) (backward-char 1)))
  16326. ad-do-it))
  16327. (defun org-forward-same-level (arg &optional invisible-ok)
  16328. "Move forward to the arg'th subheading at same level as this one.
  16329. Stop at the first and last subheadings of a superior heading."
  16330. (interactive "p")
  16331. (org-back-to-heading invisible-ok)
  16332. (org-on-heading-p)
  16333. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16334. (re (format "^\\*\\{1,%d\\} " level))
  16335. l)
  16336. (forward-char 1)
  16337. (while (> arg 0)
  16338. (while (and (re-search-forward re nil 'move)
  16339. (setq l (- (match-end 0) (match-beginning 0) 1))
  16340. (= l level)
  16341. (not invisible-ok)
  16342. (progn (backward-char 1) (org-invisible-p)))
  16343. (if (< l level) (setq arg 1)))
  16344. (setq arg (1- arg)))
  16345. (beginning-of-line 1)))
  16346. (defun org-backward-same-level (arg &optional invisible-ok)
  16347. "Move backward to the arg'th subheading at same level as this one.
  16348. Stop at the first and last subheadings of a superior heading."
  16349. (interactive "p")
  16350. (org-back-to-heading)
  16351. (org-on-heading-p)
  16352. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16353. (re (format "^\\*\\{1,%d\\} " level))
  16354. l)
  16355. (while (> arg 0)
  16356. (while (and (re-search-backward re nil 'move)
  16357. (setq l (- (match-end 0) (match-beginning 0) 1))
  16358. (= l level)
  16359. (not invisible-ok)
  16360. (org-invisible-p))
  16361. (if (< l level) (setq arg 1)))
  16362. (setq arg (1- arg)))))
  16363. (defun org-show-subtree ()
  16364. "Show everything after this heading at deeper levels."
  16365. (outline-flag-region
  16366. (point)
  16367. (save-excursion
  16368. (org-end-of-subtree t t))
  16369. nil))
  16370. (defun org-show-entry ()
  16371. "Show the body directly following this heading.
  16372. Show the heading too, if it is currently invisible."
  16373. (interactive)
  16374. (save-excursion
  16375. (condition-case nil
  16376. (progn
  16377. (org-back-to-heading t)
  16378. (outline-flag-region
  16379. (max (point-min) (1- (point)))
  16380. (save-excursion
  16381. (if (re-search-forward
  16382. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16383. (match-beginning 1)
  16384. (point-max)))
  16385. nil)
  16386. (org-cycle-hide-drawers 'children))
  16387. (error nil))))
  16388. (defun org-make-options-regexp (kwds &optional extra)
  16389. "Make a regular expression for keyword lines."
  16390. (concat
  16391. "^"
  16392. "#?[ \t]*\\+\\("
  16393. (mapconcat 'regexp-quote kwds "\\|")
  16394. (if extra (concat "\\|" extra))
  16395. "\\):[ \t]*"
  16396. "\\(.*\\)"))
  16397. ;; Make isearch reveal the necessary context
  16398. (defun org-isearch-end ()
  16399. "Reveal context after isearch exits."
  16400. (when isearch-success ; only if search was successful
  16401. (if (featurep 'xemacs)
  16402. ;; Under XEmacs, the hook is run in the correct place,
  16403. ;; we directly show the context.
  16404. (org-show-context 'isearch)
  16405. ;; In Emacs the hook runs *before* restoring the overlays.
  16406. ;; So we have to use a one-time post-command-hook to do this.
  16407. ;; (Emacs 22 has a special variable, see function `org-mode')
  16408. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16409. isearch-mode-end-hook-quit)
  16410. ;; Only when the isearch was not quitted.
  16411. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16412. 'append 'local)))))
  16413. (defun org-isearch-post-command ()
  16414. "Remove self from hook, and show context."
  16415. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16416. (org-show-context 'isearch))
  16417. ;;;; Integration with and fixes for other packages
  16418. ;;; Imenu support
  16419. (defvar org-imenu-markers nil
  16420. "All markers currently used by Imenu.")
  16421. (make-variable-buffer-local 'org-imenu-markers)
  16422. (defun org-imenu-new-marker (&optional pos)
  16423. "Return a new marker for use by Imenu, and remember the marker."
  16424. (let ((m (make-marker)))
  16425. (move-marker m (or pos (point)))
  16426. (push m org-imenu-markers)
  16427. m))
  16428. (defun org-imenu-get-tree ()
  16429. "Produce the index for Imenu."
  16430. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16431. (setq org-imenu-markers nil)
  16432. (let* ((n org-imenu-depth)
  16433. (re (concat "^" outline-regexp))
  16434. (subs (make-vector (1+ n) nil))
  16435. (last-level 0)
  16436. m level head)
  16437. (save-excursion
  16438. (save-restriction
  16439. (widen)
  16440. (goto-char (point-max))
  16441. (while (re-search-backward re nil t)
  16442. (setq level (org-reduced-level (funcall outline-level)))
  16443. (when (<= level n)
  16444. (looking-at org-complex-heading-regexp)
  16445. (setq head (org-link-display-format
  16446. (org-match-string-no-properties 4))
  16447. m (org-imenu-new-marker))
  16448. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16449. (if (>= level last-level)
  16450. (push (cons head m) (aref subs level))
  16451. (push (cons head (aref subs (1+ level))) (aref subs level))
  16452. (loop for i from (1+ level) to n do (aset subs i nil)))
  16453. (setq last-level level)))))
  16454. (aref subs 1)))
  16455. (eval-after-load "imenu"
  16456. '(progn
  16457. (add-hook 'imenu-after-jump-hook
  16458. (lambda ()
  16459. (if (eq major-mode 'org-mode)
  16460. (org-show-context 'org-goto))))))
  16461. (defun org-link-display-format (link)
  16462. "Replace a link with either the description, or the link target
  16463. if no description is present"
  16464. (save-match-data
  16465. (if (string-match org-bracket-link-analytic-regexp link)
  16466. (replace-match (if (match-end 5)
  16467. (match-string 5 link)
  16468. (concat (match-string 1 link)
  16469. (match-string 3 link)))
  16470. nil t link)
  16471. link)))
  16472. ;; Speedbar support
  16473. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16474. "Overlay marking the agenda restriction line in speedbar.")
  16475. (org-overlay-put org-speedbar-restriction-lock-overlay
  16476. 'face 'org-agenda-restriction-lock)
  16477. (org-overlay-put org-speedbar-restriction-lock-overlay
  16478. 'help-echo "Agendas are currently limited to this item.")
  16479. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16480. (defun org-speedbar-set-agenda-restriction ()
  16481. "Restrict future agenda commands to the location at point in speedbar.
  16482. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16483. (interactive)
  16484. (require 'org-agenda)
  16485. (let (p m tp np dir txt)
  16486. (cond
  16487. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16488. 'org-imenu t))
  16489. (setq m (get-text-property p 'org-imenu-marker))
  16490. (with-current-buffer (marker-buffer m)
  16491. (goto-char m)
  16492. (org-agenda-set-restriction-lock 'subtree)))
  16493. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16494. 'speedbar-function 'speedbar-find-file))
  16495. (setq tp (previous-single-property-change
  16496. (1+ p) 'speedbar-function)
  16497. np (next-single-property-change
  16498. tp 'speedbar-function)
  16499. dir (speedbar-line-directory)
  16500. txt (buffer-substring-no-properties (or tp (point-min))
  16501. (or np (point-max))))
  16502. (with-current-buffer (find-file-noselect
  16503. (let ((default-directory dir))
  16504. (expand-file-name txt)))
  16505. (unless (org-mode-p)
  16506. (error "Cannot restrict to non-Org-mode file"))
  16507. (org-agenda-set-restriction-lock 'file)))
  16508. (t (error "Don't know how to restrict Org-mode's agenda")))
  16509. (org-move-overlay org-speedbar-restriction-lock-overlay
  16510. (point-at-bol) (point-at-eol))
  16511. (setq current-prefix-arg nil)
  16512. (org-agenda-maybe-redo)))
  16513. (eval-after-load "speedbar"
  16514. '(progn
  16515. (speedbar-add-supported-extension ".org")
  16516. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16517. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16518. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16519. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16520. (add-hook 'speedbar-visiting-tag-hook
  16521. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16522. ;;; Fixes and Hacks for problems with other packages
  16523. ;; Make flyspell not check words in links, to not mess up our keymap
  16524. (defun org-mode-flyspell-verify ()
  16525. "Don't let flyspell put overlays at active buttons."
  16526. (and (not (get-text-property (point) 'keymap))
  16527. (not (get-text-property (point) 'org-no-flyspell))))
  16528. (defun org-remove-flyspell-overlays-in (beg end)
  16529. "Remove flyspell overlays in region."
  16530. (and (org-bound-and-true-p flyspell-mode)
  16531. (fboundp 'flyspell-delete-region-overlays)
  16532. (flyspell-delete-region-overlays beg end))
  16533. (add-text-properties beg end '(org-no-flyspell t)))
  16534. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16535. (eval-after-load "bookmark"
  16536. '(if (boundp 'bookmark-after-jump-hook)
  16537. ;; We can use the hook
  16538. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16539. ;; Hook not available, use advice
  16540. (defadvice bookmark-jump (after org-make-visible activate)
  16541. "Make the position visible."
  16542. (org-bookmark-jump-unhide))))
  16543. ;; Make sure saveplace shows the location if it was hidden
  16544. (eval-after-load "saveplace"
  16545. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16546. "Make the position visible."
  16547. (org-bookmark-jump-unhide)))
  16548. ;; Make sure ecb shows the location if it was hidden
  16549. (eval-after-load "ecb"
  16550. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16551. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16552. (if (eq major-mode 'org-mode)
  16553. (org-show-context))))
  16554. (defun org-bookmark-jump-unhide ()
  16555. "Unhide the current position, to show the bookmark location."
  16556. (and (org-mode-p)
  16557. (or (org-invisible-p)
  16558. (save-excursion (goto-char (max (point-min) (1- (point))))
  16559. (org-invisible-p)))
  16560. (org-show-context 'bookmark-jump)))
  16561. ;; Make session.el ignore our circular variable
  16562. (eval-after-load "session"
  16563. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16564. ;;;; Experimental code
  16565. (defun org-closed-in-range ()
  16566. "Sparse tree of items closed in a certain time range.
  16567. Still experimental, may disappear in the future."
  16568. (interactive)
  16569. ;; Get the time interval from the user.
  16570. (let* ((time1 (org-float-time
  16571. (org-read-date nil 'to-time nil "Starting date: ")))
  16572. (time2 (org-float-time
  16573. (org-read-date nil 'to-time nil "End date:")))
  16574. ;; callback function
  16575. (callback (lambda ()
  16576. (let ((time
  16577. (org-float-time
  16578. (apply 'encode-time
  16579. (org-parse-time-string
  16580. (match-string 1))))))
  16581. ;; check if time in interval
  16582. (and (>= time time1) (<= time time2))))))
  16583. ;; make tree, check each match with the callback
  16584. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16585. ;;;; Finish up
  16586. (provide 'org)
  16587. (run-hooks 'org-load-hook)
  16588. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16589. ;;; org.el ends here